| @@ -14,10 +14,14 @@ |
| 14 | 14 | /// - an HTTP/1.1 connection stays open unless the request or the response |
| 15 | 15 | /// has `Connection: close`; an HTTP/1.0 one only if the request has |
| 16 | 16 | /// `Connection: keep-alive`; |
| 17 | | −/// - a connection that waits longer than the idle timeout for a request, |
| 18 | | −/// or for more of one, is closed, and so is one whose response takes |
| 19 | | −/// longer than the write timeout to send (a client that stops reading); |
| 20 | | −/// the handler is not timed; |
| 17 | +/// - a connection that waits longer than the idle timeout for a request |
| 18 | +/// to start, or for more of a body, is closed; so is one whose request |
| 19 | +/// head, once begun, takes longer than the head timeout to arrive whole |
| 20 | +/// (a client that trickles it), and one whose response takes longer |
| 21 | +/// than the write timeout to send (a client that stops reading); the |
| 22 | +/// handler is not timed; |
| 23 | +/// - at most `max_open` connections are open at once: at the limit, the |
| 24 | +/// server waits for one to end before accepting another; |
| 21 | 25 | /// - request bodies are read whole, sized by `Content-Length` or chunked |
| 22 | 26 | /// (`Transfer-Encoding: chunked`), up to a size limit (413 beyond it); a |
| 23 | 27 | /// malformed request gets an error response, and the connection is |
| @@ -53,18 +57,28 @@ pub type Settings { |
| 53 | 57 | /// - `connections`: serve this many connections, then return (all of |
| 54 | 58 | /// them if negative); |
| 55 | 59 | /// - `idle_timeout`: close a connection that waits this many |
| 56 | | − /// milliseconds for a request or for more of one (never if negative); |
| 60 | + /// milliseconds for a request to start, or for more of a body (never |
| 61 | + /// if negative); |
| 57 | 62 | /// - `write_timeout`: close a connection whose response takes longer |
| 58 | 63 | /// than this many milliseconds to send (never if negative); |
| 59 | 64 | /// - `max_body`: answer 413 to a request whose body is larger than this |
| 60 | | − /// many bytes, and close the connection (no limit if negative). |
| 61 | | − Settings(connections: Int, idle_timeout: Int, write_timeout: Int, max_body: Int) |
| 62 | | −} |
| 63 | | − |
| 64 | | −/// Every connection; closed after 5 idle seconds, or when a response |
| 65 | | −/// takes over 30 seconds to send; bodies up to 1 MiB. |
| 65 | + /// many bytes, and close the connection (no limit if negative); |
| 66 | + /// - `max_open`: keep at most this many connections open at once, |
| 67 | + /// waiting for one to end before accepting another (no limit if |
| 68 | + /// negative); |
| 69 | + /// - `head_timeout`: close a connection whose request head, once begun, |
| 70 | + /// takes longer than this many milliseconds to arrive whole (never if |
| 71 | + /// negative). |
| 72 | + Settings(connections: Int, idle_timeout: Int, write_timeout: Int, max_body: Int, max_open: Int, head_timeout: Int) |
| 73 | +} |
| 74 | + |
| 75 | +/// Every connection, at most 512 at once (under the usual limit of 1024 |
| 76 | +/// open files, so the limit is reached before `accept` fails); closed |
| 77 | +/// after 5 idle seconds, when a request head takes over 10 seconds to |
| 78 | +/// arrive, or when a response takes over 30 seconds to send; bodies up to |
| 79 | +/// 1 MiB. |
| 66 | 80 | pub fn defaults() -> Settings { |
| 67 | | − Settings(connections: -1, idle_timeout: 5000, write_timeout: 30000, max_body: 1048576) |
| 81 | + Settings(connections: -1, idle_timeout: 5000, write_timeout: 30000, max_body: 1048576, max_open: 512, head_timeout: 10000) |
| 68 | 82 | } |
| 69 | 83 | |
| 70 | 84 | /// The value of header `name` (case-insensitive), if present. |
| @@ -121,25 +135,53 @@ pub fn serve(server: Resource<TcpListener>, handler: fn(Request) -> Response / < |
| 121 | 135 | /// Serve with `settings`: one actor per connection. When this returns, |
| 122 | 136 | /// the last connections' actors may still be running. |
| 123 | 137 | pub fn serve_with(server: Resource<TcpListener>, settings: Settings, handler: fn(Request) -> Response / <io, proc, clock, random, ffi>) -> () / <io, proc> { |
| 138 | + accept_all(server, settings, handler, process.subject(), 0, 10) |
| 139 | +} |
| 140 | + |
| 141 | +/// Accept connections while `settings.connections` lasts. `open` of them |
| 142 | +/// are running: their watchdogs send `()` on `ended` when they end. |
| 143 | +/// After a failed `accept` (out of file descriptors, say), wait `pause` |
| 144 | +/// milliseconds, doubling up to a second while it keeps failing (ADR |
| 145 | +/// 0012, Revision 2). |
| 146 | +fn accept_all(server, settings: Settings, handler, ended: Subject<()>, open: Int, pause: Int) -> () / <io, proc> { |
| 124 | 147 | if settings.connections != 0 { |
| 148 | + let open = still_open(ended, open, settings.max_open) |
| 149 | + let next = Settings(..settings, connections: settings.connections - 1) |
| 125 | 150 | match net.accept(server) { |
| 126 | 151 | Ok(conn) -> { |
| 127 | | − process.spawn(fn() { handle_connection(conn, settings, handler) }) |
| 128 | | − () |
| 152 | + let _ = process.spawn(fn() { handle_connection(conn, settings, handler, ended) }) |
| 153 | + accept_all(server, next, handler, ended, open + 1, 10) |
| 154 | + }, |
| 155 | + Error(e) -> { |
| 156 | + io.eprintln("http: accept failed: " <> e) |
| 157 | + process.sleep(pause) |
| 158 | + accept_all(server, next, handler, ended, open, if pause >= 500 { 1000 } else { pause * 2 }) |
| 129 | 159 | }, |
| 130 | | − Error(e) -> io.eprintln("http: accept failed: " <> e), |
| 131 | 160 | } |
| 132 | | − serve_with(server, Settings(..settings, connections: settings.connections - 1), handler) |
| 133 | 161 | } else { |
| 134 | 162 | () |
| 135 | 163 | } |
| 136 | 164 | } |
| 137 | 165 | |
| 166 | +/// How many of the `open` connections are still open, once the ends |
| 167 | +/// already reported are counted; at `max_open`, after waiting for one. |
| 168 | +fn still_open(ended: Subject<()>, open: Int, max_open: Int) -> Int / <proc> { |
| 169 | + let full = max_open >= 0 && open >= max_open |
| 170 | + match process.receive(ended, if full { -1 } else { 0 }) { |
| 171 | + Some(_) -> still_open(ended, open - 1, max_open), |
| 172 | + None -> open, |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 138 | 176 | /// What a connection's watchdog learns. |
| 139 | 177 | type Activity { |
| 140 | | − /// The connection's actor waits for a request, or for more of one: |
| 141 | | − /// timed. |
| 178 | + /// The connection's actor waits for a request to start, or for more of |
| 179 | + /// a body: timed by `idle_timeout` from each read. |
| 142 | 180 | Reading |
| 181 | + /// A request head has begun and is not whole yet: timed by |
| 182 | + /// `head_timeout` from here, once, since nothing else is sent until the |
| 183 | + /// head is read. |
| 184 | + HeadStarted |
| 143 | 185 | /// It runs the handler: not timed. |
| 144 | 186 | Handling |
| 145 | 187 | /// It sends a response: timed. |
| @@ -148,20 +190,22 @@ type Activity { |
| 148 | 190 | Stopped |
| 149 | 191 | } |
| 150 | 192 | |
| 151 | | −fn handle_connection(conn: Resource<TcpStream>, settings: Settings, handler: fn(Request) -> Response / <io, proc, clock, random, ffi>) -> () / <io, proc, clock, random, ffi> { |
| 193 | +fn handle_connection(conn: Resource<TcpStream>, settings: Settings, handler: fn(Request) -> Response / <io, proc, clock, random, ffi>, ended: Subject<()>) -> () / <io, proc, clock, random, ffi> { |
| 152 | 194 | let me = process.self() |
| 153 | 195 | let ready = process.subject() |
| 154 | | − let _ = process.spawn(fn() { watchdog(conn, me, settings, ready) }) |
| 196 | + let _ = process.spawn(fn() { watchdog(conn, me, settings, ready, ended) }) |
| 155 | 197 | let activity = process.receive_forever(ready) |
| 156 | 198 | serve_requests(conn, activity, settings.max_body, handler, bytes.empty(), 0) |
| 157 | 199 | net.close(conn) |
| 158 | 200 | } |
| 159 | 201 | |
| 160 | | −/// Close the connection when it waits too long for a request, or takes |
| 161 | | −/// too long to send a response: its actor then reads the end of the |
| 162 | | −/// stream, or fails to write, and stops. The watchdog monitors that actor, |
| 163 | | −/// and stops with it, releasing its reference to the socket. |
| 164 | | −fn watchdog(conn: Resource<TcpStream>, owner: Pid, settings: Settings, ready: Subject<Subject<Activity>>) -> () / <io, proc> { |
| 202 | +/// Close the connection when it waits too long for a request, takes too |
| 203 | +/// long to send its head, or takes too long to send a response: its actor |
| 204 | +/// then reads the end of the stream, or fails to write, and stops. The |
| 205 | +/// watchdog monitors that actor, and stops with it, releasing its |
| 206 | +/// reference to the socket. Either way it tells the acceptor, once, that |
| 207 | +/// the connection ended (`ended`, for `max_open`). |
| 208 | +fn watchdog(conn: Resource<TcpStream>, owner: Pid, settings: Settings, ready: Subject<Subject<Activity>>, ended: Subject<()>) -> () / <io, proc> { |
| 165 | 209 | let activity = process.subject() |
| 166 | 210 | let down = process.monitor(owner) |
| 167 | 211 | process.send(ready, activity) |
| @@ -169,12 +213,14 @@ fn watchdog(conn: Resource<TcpStream>, owner: Pid, settings: Settings, ready: Su |
| 169 | 213 | |> process.selecting(activity, fn(a) { a }) |
| 170 | 214 | |> process.selecting(down, fn(_) { Stopped }) |
| 171 | 215 | watch(conn, settings, events, settings.idle_timeout) |
| 216 | + process.send(ended, ()) |
| 172 | 217 | } |
| 173 | 218 | |
| 174 | 219 | fn watch(conn, settings: Settings, events, timeout) { |
| 175 | 220 | match process.select(events, timeout) { |
| 176 | 221 | None -> net.close(conn), |
| 177 | 222 | Some(Reading) -> watch(conn, settings, events, settings.idle_timeout), |
| 223 | + Some(HeadStarted) -> watch(conn, settings, events, settings.head_timeout), |
| 178 | 224 | Some(Handling) -> watch(conn, settings, events, -1), |
| 179 | 225 | Some(Writing) -> watch(conn, settings, events, settings.write_timeout), |
| 180 | 226 | Some(Stopped) -> (), |
| @@ -224,26 +270,81 @@ fn send_response(conn: Resource<TcpStream>, activity: Subject<Activity>, data: B |
| 224 | 270 | net.write(conn, data) |
| 225 | 271 | } |
| 226 | 272 | |
| 273 | +/// The largest request head, in bytes (431 beyond it). |
| 274 | +fn head_limit() -> Int { |
| 275 | + 65536 |
| 276 | +} |
| 277 | + |
| 227 | 278 | fn read_request(conn, activity, max_body: Int, buffer: Bytes, at: Int) -> Result<Incoming, Failure> / <io, proc> { |
| 228 | 279 | match parse_head(buffer, at) { |
| 229 | | − Complete(method, path, version, headers, end, length) -> |
| 230 | | − match read_body(conn, activity, max_body, headers, length, buffer, end) { |
| 231 | | − Ok((body, buffer, at)) -> Ok(Incoming(Request(method, path, headers, body), keeps_alive(version, headers), buffer, at)), |
| 232 | | − Error(e) -> Error(e), |
| 233 | | − }, |
| 234 | | − Invalid(reason) -> Error(Bad(400, "bad request: " <> reason)), |
| 235 | 280 | Partial -> |
| 236 | | − if bytes.length(buffer) - at > 65536 { |
| 281 | + if bytes.length(buffer) > at { |
| 282 | + // A head has begun: it must arrive whole within `head_timeout`. |
| 283 | + process.send(activity, HeadStarted) |
| 284 | + let begun = bytes.slice(buffer, at, bytes.length(buffer)) |
| 285 | + read_head(conn, activity, max_body, bytes.append(bytes.builder(), begun), bytes.length(begun), last_bytes(begun)) |
| 286 | + } else { |
| 287 | + // Waiting for a request to start: `idle_timeout`. |
| 288 | + match read_some(conn, activity) { |
| 289 | + Ok(more) -> read_request(conn, activity, max_body, more, 0), |
| 290 | + Error(e) -> Error(e), |
| 291 | + } |
| 292 | + }, |
| 293 | + head -> request_of(conn, activity, max_body, head, buffer, at), |
| 294 | + } |
| 295 | +} |
| 296 | + |
| 297 | +/// The request whose head, from offset `at` of `buffer`, is `head`. |
| 298 | +fn request_of(conn, activity, max_body: Int, head: Head, buffer: Bytes, at: Int) -> Result<Incoming, Failure> / <io, proc> { |
| 299 | + match head { |
| 300 | + Complete(method, path, version, headers, end, length) -> |
| 301 | + if end - at > head_limit() { |
| 237 | 302 | Error(Bad(431, "request head too large")) |
| 238 | 303 | } else { |
| 239 | | − match receive_more(conn, activity, buffer, at) { |
| 240 | | − Ok(buffer) -> read_request(conn, activity, max_body, buffer, 0), |
| 304 | + match read_body(conn, activity, max_body, headers, length, buffer, end) { |
| 305 | + Ok((body, buffer, at)) -> Ok(Incoming(Request(method, path, headers, body), keeps_alive(version, headers), buffer, at)), |
| 241 | 306 | Error(e) -> Error(e), |
| 242 | 307 | } |
| 243 | 308 | }, |
| 309 | + Invalid(reason) -> Error(Bad(400, "bad request: " <> reason)), |
| 310 | + // Its end was found, yet it is incomplete: blank lines before the |
| 311 | + // request line, which would otherwise restart `head_timeout`. |
| 312 | + Partial -> Error(Bad(400, "bad request: an empty head")), |
| 313 | + } |
| 314 | +} |
| 315 | + |
| 316 | +/// Read the rest of a head begun in `received` (`size` bytes, ending in |
| 317 | +/// `tail`), not reported to the watchdog, which times it by |
| 318 | +/// `head_timeout`. Each read is scanned once for the head's end, with |
| 319 | +/// the 3 bytes before it; then the head is built and parsed once (ADR |
| 320 | +/// 0012, Revision 2). |
| 321 | +fn read_head(conn, activity, max_body: Int, received: Builder, size: Int, tail: Bytes) -> Result<Incoming, Failure> / <io, proc> { |
| 322 | + if size > head_limit() { |
| 323 | + Error(Bad(431, "request head too large")) |
| 324 | + } else { |
| 325 | + match read_raw(conn) { |
| 326 | + Ok(more) -> { |
| 327 | + let window = bytes.concat(tail, more) |
| 328 | + let received = bytes.append(received, more) |
| 329 | + if parse_head_end(window, 0) >= 0 { |
| 330 | + let buffer = bytes.build(received) |
| 331 | + request_of(conn, activity, max_body, parse_head(buffer, 0), buffer, 0) |
| 332 | + } else { |
| 333 | + read_head(conn, activity, max_body, received, size + bytes.length(more), last_bytes(window)) |
| 334 | + } |
| 335 | + }, |
| 336 | + Error(e) -> Error(e), |
| 337 | + } |
| 244 | 338 | } |
| 245 | 339 | } |
| 246 | 340 | |
| 341 | +/// The last 3 bytes of `b` (all of them if fewer): an end of head may |
| 342 | +/// straddle two reads. |
| 343 | +fn last_bytes(b: Bytes) -> Bytes { |
| 344 | + let n = bytes.length(b) |
| 345 | + bytes.slice(b, if n > 3 { n - 3 } else { 0 }, n) |
| 346 | +} |
| 347 | + |
| 247 | 348 | /// Whether the client wants the connection kept open after its request. |
| 248 | 349 | fn keeps_alive(version: Int, headers: List<(String, String)>) -> Bool { |
| 249 | 350 | let connection = option.unwrap_or(find_header(headers, "connection"), "") |
| @@ -369,9 +470,14 @@ fn receive_more(conn, activity, buffer: Bytes, at: Int) -> Result<Bytes, Failure |
| 369 | 470 | } |
| 370 | 471 | } |
| 371 | 472 | |
| 372 | | −/// The next bytes from the connection (timed by the watchdog). |
| 473 | +/// The next bytes from the connection, timed by `idle_timeout` from now. |
| 373 | 474 | fn read_some(conn: Resource<TcpStream>, activity: Subject<Activity>) -> Result<Bytes, Failure> / <io, proc> { |
| 374 | 475 | process.send(activity, Reading) |
| 476 | + read_raw(conn) |
| 477 | +} |
| 478 | + |
| 479 | +/// The next bytes from the connection, under whatever timeout runs. |
| 480 | +fn read_raw(conn: Resource<TcpStream>) -> Result<Bytes, Failure> / <io> { |
| 375 | 481 | match net.read(conn, 65536) { |
| 376 | 482 | Ok(more) -> if bytes.length(more) == 0 { Error(Closed) } else { Ok(more) }, |
| 377 | 483 | Error(_) -> Error(Closed), |
| @@ -388,6 +494,11 @@ type Head { |
| 388 | 494 | @extern(c, "tl_http_parse_request") |
| 389 | 495 | fn parse_head(buffer: Bytes, at: Int) -> Head |
| 390 | 496 | |
| 497 | +/// Where the head in `buffer` ends (just past its empty line), if it ends |
| 498 | +/// at or after `from`; else -1. |
| 499 | +@extern(c, "tl_http_parse_head_end") |
| 500 | +fn parse_head_end(buffer: Bytes, from: Int) -> Int |
| 501 | + |
| 391 | 502 | type Chunk { |
| 392 | 503 | ChunkPartial |
| 393 | 504 | ChunkInvalid(reason: String) |