|
| 1 | +# Request and Response Handling |
| 2 | + |
| 3 | +This guide explains how to work with requests and responses when bridging between Rack and `Protocol::HTTP`, covering advanced use cases and edge cases. |
| 4 | + |
| 5 | +## Request Conversion |
| 6 | + |
| 7 | +The {ruby Protocol::Rack::Request} class converts Rack environment hashes into rich `Protocol::HTTP` request objects, providing access to modern HTTP features while maintaining compatibility with Rack. |
| 8 | + |
| 9 | +### Basic Request Access |
| 10 | + |
| 11 | +```ruby |
| 12 | +require "protocol/rack/request" |
| 13 | + |
| 14 | +run do |env| |
| 15 | + request = Protocol::Rack::Request[env] |
| 16 | + |
| 17 | + # Access request properties: |
| 18 | + puts request.method # "GET", "POST", etc. |
| 19 | + puts request.path # "/users/123" |
| 20 | + puts request.url_scheme # "http" or "https" |
| 21 | + puts request.authority # "example.com:80" |
| 22 | +end |
| 23 | +``` |
| 24 | + |
| 25 | +### Headers |
| 26 | + |
| 27 | +Headers are automatically extracted from Rack's `HTTP_*` environment variables: |
| 28 | + |
| 29 | +```ruby |
| 30 | +run do |env| |
| 31 | + request = Protocol::Rack::Request[env] |
| 32 | + |
| 33 | + # Headers are available as a `Protocol::HTTP::Headers` object: |
| 34 | + user_agent = request.headers["user-agent"] |
| 35 | + content_type = request.headers["content-type"] |
| 36 | + |
| 37 | + # Headers are case-insensitive: |
| 38 | + user_agent = request.headers["User-Agent"] # Same as above |
| 39 | +end |
| 40 | +``` |
| 41 | + |
| 42 | +The adapter converts Rack's `HTTP_ACCEPT_ENCODING` format to standard HTTP header names (`accept-encoding`). |
| 43 | + |
| 44 | +### Request Body |
| 45 | + |
| 46 | +The request body is wrapped in a `Protocol::HTTP`-compatible interface: |
| 47 | + |
| 48 | +```ruby |
| 49 | +run do |env| |
| 50 | + request = Protocol::Rack::Request[env] |
| 51 | + |
| 52 | + # Read the entire body: |
| 53 | + body = request.body.read |
| 54 | + |
| 55 | + # Or stream it: |
| 56 | + request.body.each do |chunk| |
| 57 | + process_chunk(chunk) |
| 58 | + end |
| 59 | + |
| 60 | + # The body supports rewind if the underlying Rack input supports it: |
| 61 | + request.body.rewind |
| 62 | +end |
| 63 | +``` |
| 64 | + |
| 65 | +The body wrapper handles Rack's `rack.input` interface, which may or may not support `rewind` depending on the server. |
| 66 | + |
| 67 | +### Query Parameters |
| 68 | + |
| 69 | +Query parameters are parsed from the request path: |
| 70 | + |
| 71 | +```ruby |
| 72 | +run do |env| |
| 73 | + request = Protocol::Rack::Request[env] |
| 74 | + |
| 75 | + # Access query string: |
| 76 | + query = request.query # "name=value&other=123" |
| 77 | + |
| 78 | + # Parse query parameters (if using a helper): |
| 79 | + params = URI.decode_www_form(query).to_h |
| 80 | +end |
| 81 | +``` |
| 82 | + |
| 83 | +### Protocol Upgrades |
| 84 | + |
| 85 | +The adapter handles protocol upgrade requests (like WebSockets): |
| 86 | + |
| 87 | +```ruby |
| 88 | +run do |env| |
| 89 | + request = Protocol::Rack::Request[env] |
| 90 | + |
| 91 | + # Check for upgrade protocols: |
| 92 | + if protocols = request.protocol |
| 93 | + # protocols is an array: ["websocket"]: |
| 94 | + if protocols.include?("websocket") |
| 95 | + # Handle WebSocket upgrade. |
| 96 | + end |
| 97 | + end |
| 98 | +end |
| 99 | +``` |
| 100 | + |
| 101 | +Protocols are extracted from either `rack.protocol` or the `HTTP_UPGRADE` header. |
| 102 | + |
| 103 | +## Response Conversion |
| 104 | + |
| 105 | +The {ruby Protocol::Rack::Response} class and {ruby Protocol::Rack::Adapter.make_response} handle converting `Protocol::HTTP` responses back to Rack format. |
| 106 | + |
| 107 | +### Basic Response |
| 108 | + |
| 109 | +```ruby |
| 110 | +require "protocol/rack/adapter" |
| 111 | + |
| 112 | +run do |env| |
| 113 | + request = Protocol::Rack::Request[env] |
| 114 | + |
| 115 | + # Create a `Protocol::HTTP` response: |
| 116 | + response = Protocol::HTTP::Response[ |
| 117 | + 200, |
| 118 | + {"content-type" => "text/html"}, |
| 119 | + ["<h1>Hello</h1>"] |
| 120 | + ] |
| 121 | + |
| 122 | + # Convert to Rack format: |
| 123 | + Protocol::Rack::Adapter.make_response(env, response) |
| 124 | +end |
| 125 | +``` |
| 126 | + |
| 127 | +### Response Bodies |
| 128 | + |
| 129 | +The adapter handles different types of response bodies: |
| 130 | + |
| 131 | +#### Enumerable Bodies |
| 132 | + |
| 133 | +```ruby |
| 134 | +# Array bodies: |
| 135 | +response = Protocol::HTTP::Response[ |
| 136 | + 200, |
| 137 | + {"content-type" => "text/plain"}, |
| 138 | + ["Hello", " ", "World"] |
| 139 | +] |
| 140 | + |
| 141 | +# Enumerable bodies: |
| 142 | +response = Protocol::HTTP::Response[ |
| 143 | + 200, |
| 144 | + {"content-type" => "text/plain"}, |
| 145 | + Enumerator.new do |yielder| |
| 146 | + yielder << "Chunk 1\n" |
| 147 | + yielder << "Chunk 2\n" |
| 148 | + end |
| 149 | +] |
| 150 | +``` |
| 151 | + |
| 152 | +#### Streaming Bodies |
| 153 | + |
| 154 | +```ruby |
| 155 | +# Streaming response body: |
| 156 | +body = Protocol::HTTP::Body::Buffered.new(["Streaming content"]) |
| 157 | + |
| 158 | +response = Protocol::HTTP::Response[ |
| 159 | + 200, |
| 160 | + {"content-type" => "text/plain"}, |
| 161 | + body |
| 162 | +] |
| 163 | +``` |
| 164 | + |
| 165 | +#### File Bodies |
| 166 | + |
| 167 | +```ruby |
| 168 | +# File-based responses: |
| 169 | +body = Protocol::HTTP::Body::File.open("path/to/file.txt") |
| 170 | + |
| 171 | +response = Protocol::HTTP::Response[ |
| 172 | + 200, |
| 173 | + {"content-type" => "text/plain"}, |
| 174 | + body |
| 175 | +] |
| 176 | +``` |
| 177 | + |
| 178 | +### HEAD Requests |
| 179 | + |
| 180 | +The adapter automatically handles HEAD requests by removing response bodies: |
| 181 | + |
| 182 | +```ruby |
| 183 | +run do |env| |
| 184 | + request = Protocol::Rack::Request[env] |
| 185 | + |
| 186 | + # Create a response with a body: |
| 187 | + response = Protocol::HTTP::Response[ |
| 188 | + 200, |
| 189 | + {"content-type" => "text/html"}, |
| 190 | + ["<h1>Full Response</h1>"] |
| 191 | + ] |
| 192 | + |
| 193 | + # For HEAD requests, the body is automatically removed: |
| 194 | + Protocol::Rack::Adapter.make_response(env, response) |
| 195 | +end |
| 196 | +``` |
| 197 | + |
| 198 | +### Status Codes Without Bodies |
| 199 | + |
| 200 | +Certain status codes (204 No Content, 205 Reset Content, 304 Not Modified) should not include response bodies. The adapter handles this automatically: |
| 201 | + |
| 202 | +```ruby |
| 203 | +response = Protocol::HTTP::Response[ |
| 204 | + 204, # No Content |
| 205 | + {}, |
| 206 | + ["This body will be removed"] |
| 207 | +] |
| 208 | + |
| 209 | +# The adapter automatically removes the body for 204 responses. |
| 210 | +``` |
| 211 | + |
| 212 | +### Rack-Specific Features |
| 213 | + |
| 214 | +#### Hijacking |
| 215 | + |
| 216 | +Rack supports response hijacking, which allows taking over the connection: |
| 217 | + |
| 218 | +```ruby |
| 219 | +# In a Rack application: |
| 220 | +[200, {"rack.hijack" => proc{|io| io.write("Hijacked!")}}, []] |
| 221 | + |
| 222 | +# The adapter handles hijacking automatically using streaming responses. |
| 223 | +``` |
| 224 | + |
| 225 | +#### Response Finished Callbacks |
| 226 | + |
| 227 | +Rack 2+ supports `rack.response_finished` callbacks: |
| 228 | + |
| 229 | +```ruby |
| 230 | +env["rack.response_finished"] ||= [] |
| 231 | +env["rack.response_finished"] << proc do |env, status, headers, error| |
| 232 | + # Cleanup or logging after response is sent |
| 233 | + puts "Response finished: #{status}" |
| 234 | +end |
| 235 | +``` |
| 236 | + |
| 237 | +The adapter invokes these callbacks in reverse order of registration, as specified by the Rack specification. |
| 238 | + |
| 239 | +### Hop Headers |
| 240 | + |
| 241 | +HTTP hop-by-hop headers (like `Connection`, `Transfer-Encoding`) are automatically removed from responses, as they should not be forwarded through proxies: |
| 242 | + |
| 243 | +```ruby |
| 244 | +response = Protocol::HTTP::Response[ |
| 245 | + 200, |
| 246 | + { |
| 247 | + "content-type" => "text/plain", |
| 248 | + "connection" => "close", # This will be removed |
| 249 | + "transfer-encoding" => "chunked" # This will be removed |
| 250 | + }, |
| 251 | + ["Body"] |
| 252 | +] |
| 253 | +``` |
0 commit comments