Class: HttpKit::HttpHandler

Inherits:
RackHttpHandler
  • Object
show all
Defined in:
lib/http_kit/rack_handler.rb

Instance Method Summary collapse

Instance Method Details

#http_request_to_rack(req) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/http_kit/rack_handler.rb', line 23

def http_request_to_rack(req)
  env = {}

  body = req.getBody() || Rack::RewindableInput.new(StringIO.new(""))

  env["SERVER_SOFTWARE"]      = "HTTP Kit"
  env["SERVER_NAME"]          = req.serverName
  env["rack.input"]           = body
  env["rack.version"]         = [1, 0]
  env["rack.errors"]          = $stderr
  env["rack.multithread"]     = true
  env["rack.multiprocess"]    = false
  env["rack.run_once"]        = false
  env["REQUEST_METHOD"]       = req.method.KEY.to_s.gsub(':', '').upcase
  env["REQUEST_PATH"]         = req.uri
  env["PATH_INFO"]            = req.uri
  env["REQUEST_URI"]          = "http://%s:%s%s" % [req.serverName, req.serverPort.to_s, req.uri]
  env["HTTP_VERSION"]         = "HTTP/1.1"
  env["HTTP_HOST"]            = "%s:%s" % [req.serverName, req.serverPort.to_s]
  env["HTTP_ACCEPT"]          = "*/*"
  env["SERVER_PORT"]          = req.serverPort.to_s
  env["QUERY_STRING"]         = req.queryString || ""
  env["SERVER_PROTOCOL"]      = "HTTP/1.1"
  env["rack.url_scheme"]      = "http" # only http is supported
  env["REMOTE_ADDR"]          = req.getRemoteAddr()

  env["CONTENT_TYPE"]         = req.contentType || ""
  env["CONTENT_LENGTH"]       = req.contentLength.to_s

  # // m.put(URI, req.uri);
  # // m.put(ASYC_CHANNEL, req.channel);
  # // m.put(WEBSOCKET, req.isWebSocket);

  # // // key is already lower cased, required by ring spec
  # // m.put(HEADERS, PersistentArrayMap.create(req.headers));
  # // m.put(CONTENT_TYPE, req.contentType);
  # // m.put(CONTENT_LENGTH, req.contentLength);
  # // m.put(CHARACTER_ENCODING, req.charset);
  # // m.put(BODY, req.getBody());

  env
end

#runObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/http_kit/rack_handler.rb', line 66

def run
  begin
    resp = handler.call(http_request_to_rack(req))
    if ! resp
      cb.run(HttpUtils.HttpEncode(404, HeaderMap.new(), nil));
    else
      status, headers, body = resp

      if ! body.is_a?(AsyncChannel)
        b = DynamicBytes.new(512)

        body.each do |chunk|
          b.append(chunk.to_s)
        end

        body = ByteBuffer.wrap(b.get(), 0, b.length())

        headers = HeaderMap.camelCase(headers)

        # In HTTP 1.1, all connections are considered persistent unless declared otherwise.
        if req.version == HttpVersion::HTTP_1_0 && req.isKeepAlive
          headers.put("Connection", "Keep-Alive")
        end
        cb.run(HttpUtils.HttpEncode(status.to_i, headers, body))
      end
    end
  rescue => e
    cb.run(HttpUtils.HttpEncode(500, HeaderMap.new(), e.message))
    HttpUtils.printError(req.method + " " + req.uri, e);
  end
end