class http_protocol :The core event loop of the HTTP daemon
#http_protocol_config -> Unix.file_descr ->
fd one must pass the already connected socket. It must be in non-
How to use this class: Basically, one invokes
cycle until the whole
message exchange on
fd is processed.
cycle receives data from the
socket and sends data to the socket. There are two internal queues:
The receive queue stores parts of received requests as
One can take values from the front of this queue by calling
The response queue stores
http_response objects. Each of the objects
corresponds to a request that was received before. This queue is handled
fully automatically, but one can watch its length to see whether all responses
are actually transmitted over the wire.
The basic algorithm to process messages is:
let rec next_token () = if proto # recv_queue_len = 0 then ( proto # cycle (); next_token() ) else proto # receive() let cur_token = ref (next_token()) in while !cur_token <> `Eof do (* Process first token of next request: *) match !cur_token with | `Req_header(req_line, header, resp) -> (* Depending on [req_line], read further tokens until [`Req_end] *) ... (* Switch to the first token of the next message: *) cur_token := next_token() | `Timeout -> ... | `Bad_request_error(e,resp) -> (* Generate 400 error, send it to [resp] *) ... (* Switch to the first token of the next message: *) cur_token := next_token() | `Fatal_error e -> failwith "Crash" | _ -> assert false done; while proto # resp_queue_len > 0 do proto # cycle (); done; proto # shutdown()
See the file
tests/easy_daemon.ml for a complete implementation of this.
As one can see, it is essential to watch the lengths of the queues in order
to figure out what has happened during
When the body of the request is empty,
`Req_body tokens are omitted.
Note that for requests like
GET that always have an empty body, it is
still possible that an errorneous client sends a body, and that
tokens arrive. One must accept and ignore these tokens.
Error handling: For serious errors, the connection is immediately aborted.
In this case,
receive returns a
`Fatal_error token. Note that the
queued responses cannot be sent! An example of this is
There is a large class of non-serious errors, esp. format errors
in the header and body. It is typical of these errors that one cannot determine
the end of the request properly. For this reason, the daemon stops reading
further data from the request, but the response queue is still delivered.
For these errors,
receive returns a
This token contains a
http_response object that must be filled with a
400 error response.
req_tokens, and if possible, new
req_tokens are appended to the receive queue.
If the response queue has objects, and there is really data one can send, and if the socket allows one to send data, it is tried to send as much data as possible.
block (default: 0) can be set to wait until data
can be exchanged with the socket. This avoids busy waiting. The number
is the duration in seconds to wait until the connection times out
(0 means not to wait at all, -1 means to wait infinitely). When a timeout
happens, and there is nothing to send, and the last request was fully
receive will simply return
`Timeout (i.e. when
true). Otherwise, the
`Timeout is generated.
method receive :
unit -> req_token
req_tokenfrom the receive queue. Raises
Recv_queue_emptywhen the queue is empty (= has no new data)
method peek_recv :
unit -> req_token
Recv_queue_emptywhen the queue is empty.
http_responseobjects that have not yet fully processed)
`Req_endtokens minus number of responses in state
`Processed. Note that
-1when bad requests are responded.
falsewhile the request is being received.
`Normal: The normal timeout value applies
`Next_message: The timeout value applies while waiting for the next message
`None: The connection is output-driven, no input timeout value
method abort :
fatal_error -> unit
`Eof. The response queue is cleared. The
cyclemethod will return immediately without doing anything.
trueiff the protocol engine is interested in new data from the socket. Returns
falseafter EOF and after errors.
trueiff the protocol engine has data to output to the socket
truewhen a lingering close operation is needed to reliably shut down the socket. In many cases, this expensive operation is not necessary. See the class
method config :