Sha256: 8e4b8b0d4c802ecbc0ac4816241b044fd12051018e2389f969e80d05064c656a

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

module EventMachine
  module WebSocket
    class HandlerFactory
      PATH   = /^(\w+) (\/[^\s]*) HTTP\/1\.1$/
      HEADER = /^([^:]+):\s*(.+)$/

      def self.build(data, secure = false, debug = false)
        request = {}
        response = nil

        lines = data.split("\r\n")

        # extract request path
        first_line = lines.shift.match(PATH)
        request['Method'] = first_line[1].strip
        request['Path'] = first_line[2].strip

        unless request["Method"] == "GET"
          raise HandshakeError, "Must be GET request"
        end

        # extract query string values
        request['Query'] = Addressable::URI.parse(request['Path']).query_values ||= {}
        # extract remaining headers
        lines.each do |line|
          h = HEADER.match(line)
          request[h[1].strip] = h[2].strip if h
        end
        request['Third-Key'] = lines.last

        unless request['Connection'] == 'Upgrade' and request['Upgrade'] == 'WebSocket'
          raise HandshakeError, "Connection and Upgrade headers required"
        end

        # transform headers
        protocol = (secure ? "wss" : "ws")
        request['Host'] = Addressable::URI.parse("#{protocol}://"+request['Host'])

        version = request['Sec-WebSocket-Key1'] ? 76 : 75

        case version
        when 75
          Handler75.new(request, response, debug)
        when 76
          Handler76.new(request, response, debug)
        else
          raise "Must not happen"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
em-websocket-0.1.3 lib/em-websocket/handler_factory.rb
em-websocket-0.1.2 lib/em-websocket/handler_factory.rb