Class: Protocol::Rack::Request

Inherits:
HTTP::Request
  • Object
show all
Defined in:
lib/protocol/rack/request.rb

Overview

A Rack-compatible HTTP request wrapper. This class provides a bridge between Rack’s environment hash and Protocol::HTTP::Request. It handles conversion of Rack environment variables to HTTP request properties.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Request

Initialize a new Request instance from a Rack environment.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/protocol/rack/request.rb', line 30

def initialize(env)
  @env = env
  
  super(
    @env["rack.url_scheme"],
    @env["HTTP_HOST"],
    @env["REQUEST_METHOD"],
    @env["PATH_INFO"],
    @env["SERVER_PROTOCOL"],
    self.class.headers(@env),
    Body::InputWrapper.new(@env["rack.input"]),
    self.class.protocol(@env)
  )
end

Class Method Details

.[](env) ⇒ Object

Get or create a Request instance for the given Rack environment. The request is cached in the environment to avoid creating multiple instances.



23
24
25
# File 'lib/protocol/rack/request.rb', line 23

def self.[](env)
  env["protocol.http.request"] ||= new(env)
end

.headers(env) ⇒ Object

Extract HTTP headers from the Rack environment. Converts Rack’s ‘HTTP_*` environment variables to proper HTTP headers.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/protocol/rack/request.rb', line 63

def self.headers(env)
  headers = ::Protocol::HTTP::Headers.new
  env.each do |key, value|
    if key.start_with?("HTTP_")
      next if key == "HTTP_HOST"
      headers[key[5..-1].gsub("_", "-").downcase] = value
    end
  end
  
  return headers
end

.protocol(env) ⇒ Object

Extract the protocol list from the Rack environment. Checks both ‘rack.protocol` and `HTTP_UPGRADE` headers.



50
51
52
53
54
55
56
# File 'lib/protocol/rack/request.rb', line 50

def self.protocol(env)
  if protocols = env["rack.protocol"]
    return Array(protocols)
  elsif protocols = env[CGI::HTTP_UPGRADE]
    return protocols.split(/\s*,\s*/)
  end
end