diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2026-09-01 07:54:16 +0200 |
|---|---|---|
| committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2026-09-01 07:54:16 +0200 |
| commit | 0a4fa559e44ed3e77f723092feab977539b8aab0 (patch) | |
| tree | ab5a94d816ae176f9f1319222b26cc1758a3c764 /docs | |
| parent | 3581b58b9d5c09e3e37b7e20b41205ee3553aaa4 (diff) | |
| download | luasocket-0a4fa559e44ed3e77f723092feab977539b8aab0.tar.gz luasocket-0a4fa559e44ed3e77f723092feab977539b8aab0.tar.bz2 luasocket-0a4fa559e44ed3e77f723092feab977539b8aab0.zip | |
Address PR review: pass raw socket to headers callback, use ok/err return conventionhttp-headers-callback
Rename response_headers to headers_callback, pass the raw connection as a
4th argument (for use cases like WebSocket upgrade handoff), and switch its
return contract to the idiomatic ok/err shape used elsewhere in this file:
truthy ok continues (optionally swapping in a new sink), falsy ok closes the
connection and propagates the callback's error via socket.protect instead of
faking a success return.
Claude-Session: https://claude.ai/code/session_01S4imKCU4hxDg96DXj9hxSB
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/http.html | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/docs/http.html b/docs/http.html index c6423ba..15f9c26 100644 --- a/docs/http.html +++ b/docs/http.html | |||
| @@ -136,7 +136,8 @@ http.<b>request{</b><br> | |||
| 136 | [proxy = <i>string</i>,]<br> | 136 | [proxy = <i>string</i>,]<br> |
| 137 | [redirect = <i>boolean</i>,]<br> | 137 | [redirect = <i>boolean</i>,]<br> |
| 138 | [create = <i>function</i>,]<br> | 138 | [create = <i>function</i>,]<br> |
| 139 | [maxredirects = <i>number</i>]<br> | 139 | [maxredirects = <i>number</i>,]<br> |
| 140 | [headers_callback = <i>function</i>]<br> | ||
| 140 | <b>}</b> | 141 | <b>}</b> |
| 141 | </p> | 142 | </p> |
| 142 | 143 | ||
| @@ -189,6 +190,39 @@ function from automatically following 301 or 302 server redirect messages;</li> | |||
| 189 | <li><tt>maxredirects</tt>: An optional number specifying the maximum number of | 190 | <li><tt>maxredirects</tt>: An optional number specifying the maximum number of |
| 190 | redirects to follow. Defaults to <tt>5</tt> if not specified. A boolean | 191 | redirects to follow. Defaults to <tt>5</tt> if not specified. A boolean |
| 191 | <tt>false</tt> value means no maximum (unlimited).</li> | 192 | <tt>false</tt> value means no maximum (unlimited).</li> |
| 193 | <li><tt>headers_callback</tt>: An optional function, called once per request | ||
| 194 | right after the headers have been received (this | ||
| 195 | is after any redirect has already been followed, so it only fires for the | ||
| 196 | final response) and before the response body would be read. It is called | ||
| 197 | as: <tt>ok, new_sink = headers_callback(code, headers, status, sock)</tt>, where | ||
| 198 | <tt>sock</tt> is the raw socket used for the request (the | ||
| 199 | <a href="tcp.html#socket.tcp"><tt>socket.tcp</tt></a>-like object returned | ||
| 200 | by <tt>create</tt>, or the default one). It is not called for an HTTP/0.9 | ||
| 201 | reply (no headers at all), for a <tt>408</tt> response, or for a response | ||
| 202 | that is going to be redirected. | ||
| 203 | <br> | ||
| 204 | The callback should return two values, <tt>ok</tt> and a second value whose | ||
| 205 | meaning depends on <tt>ok</tt>: | ||
| 206 | <ul> | ||
| 207 | <li>If <tt>ok</tt> is falsy, the second value is used as an error | ||
| 208 | message: the connection is closed and <tt>request</tt> returns | ||
| 209 | <tt><b>nil</b></tt> followed by that message, just like any other request | ||
| 210 | failure.</li> | ||
| 211 | <li>If <tt>ok</tt> is truthy, the request continues normally. If the | ||
| 212 | second value is also provided, it replaces <tt>sink</tt> for reading the | ||
| 213 | response body; otherwise the original sink is used.</li> | ||
| 214 | </ul> | ||
| 215 | Note that a swapped-in sink is only ever read from when the response | ||
| 216 | actually has a body to receive: for a <tt>HEAD</tt> request or a | ||
| 217 | <tt>204</tt>/<tt>304</tt> response there is nothing to read regardless of | ||
| 218 | which sink is set. | ||
| 219 | <br> | ||
| 220 | This makes it possible to inspect headers before committing to read a | ||
| 221 | body -- for example to reject a response early based on | ||
| 222 | <tt>content-type</tt> or <tt>content-length</tt>, to pick a different | ||
| 223 | sink depending on the headers, or -- combined with the raw | ||
| 224 | <tt>sock</tt> -- to hand the socket off for a protocol upgrade | ||
| 225 | such as WebSockets (<tt>101 Switching Protocols</tt>).</li> | ||
| 192 | </ul> | 226 | </ul> |
| 193 | 227 | ||
| 194 | <p class="return"> | 228 | <p class="return"> |
