Class: Ji2p::Server::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/ji2p/server/http.rb

Constant Summary collapse

VERSION =
"HTTP/1.1"
CRLF =
"\r\n"

Instance Method Summary collapse

Constructor Details

#initialize(socket, application) ⇒ HTTP

Returns a new instance of HTTP.



8
9
10
11
# File 'lib/ji2p/server/http.rb', line 8

def initialize(socket, application)
  @socket = socket
  @application = application
end

Instance Method Details

#handleObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ji2p/server/http.rb', line 44

def handle
  env = parse

  status, headers, body = @application.call(env)

  time = Time.now.httpdate

  @socket.write "#{env['HTTP_VERSION']} #{status} #{Rack::Utils::HTTP_STATUS_CODES.fetch(status.to_i) { 'UNKNOWN' }}#{CRLF}"
  @socket.write "Date: #{time}#{CRLF}"
  @socket.write "Connection: close#{CRLF}"

  headers.each do |key, value|
    @socket.write "#{key}: #{value}#{CRLF}"
  end

  @socket.write(CRLF)

  body.each do |chunk|
    @socket.write(chunk)
  end

  Ji2p.logger.info("[#{time}] '#{env["REQUEST_METHOD"]} #{env["REQUEST_URI"]} #{env["HTTP_VERSION"]}' #{status}")
end

#parseObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ji2p/server/http.rb', line 13

def parse
  matches = /\A(?<method>\S+)\s+(?<uri>\S+)\s+(?<version>\S+)#{CRLF}\Z/.match(@socket.readline)
  uri = URI.parse(matches[:uri])

  env = {
    "rack.errors" => $stderr,
    "rack.version" => Rack::VERSION,
    "rack.url_scheme" => uri.scheme || "http",
    "REQUEST_METHOD" => matches[:method],
    "REQUEST_URI" => matches[:uri],
    "HTTP_VERSION" => matches[:version],
    "QUERY_STRING" => uri.query || "",
    "SERVER_PORT" => uri.port || 80,
    "SERVER_NAME" => uri.host || "localhost",
    "PATH_INFO" => uri.path || "",
    "SCRIPT_NAME" => "",
  }

  while matches = /\A(?<key>[^:]+):\s*(?<value>.+)#{CRLF}\Z/.match(hl = @socket.readline)
    case matches[:key]
    when Rack::ContentType then env["CONTENT_TYPE"] = matches[:value]
    when Rack::ContentLength then env["CONTENT_LENGTH"] = Integer(matches[:value])
    else env["HTTP_" + matches[:key].tr("-", "_").upcase] ||= matches[:value]
    end
  end

  env["rack.input"] = StringIO.new(@socket.read(env["CONTENT_LENGTH"] || 0))

  return env #.map { |_,v| String.new v }
end