Plasma GitLab Archive
Projects Blog Knowledge

Class type Nethttpd_kernel.http_response

class type http_response = object .. end

Encapsultation of the HTTP response for a single request

Represents the action of sending the response

This class has an internal queue of response tokens that are not yet processed. One can easily add new tokens to the end of the queue (send).

The class is responsible for determining the transfer encoding:

  • When the HTTP version is 1.0, the encoding is always "identity", and the connection will be closed after the response.
  • When the HTTP version is 1.1, and there is a Content-length header, the encoding will be selected as "identity". It is checked whether the body has really this length. If too short, it is suggested to close the connection. If too long, the extra part of the message is silently dropped.
  • When the HTTP version is 1.1, and there is no Content-length header, the encoding will be selected as "chunked".

Currently, the TE request header is not taken into account. The trailer is always empty.

The following headers are set (or removed) by this class:

  • Transfer-Encoding
  • Trailer
  • Date
  • Connection
  • Upgrade
  • Server (it is appended to this field)

Responses for HEAD requests have the special behaviour that the body is silently dropped. The calculation of header fields is not affected by this. This means that HEAD can be easily implemented by doing the same as for GET.

Responses for other requests that must not include a body must set Content-Length to 0.

These methods can be called by the content provider:

method state : resp_state

Reports the state. The initial state is `Inhibited

method bidirectional_phase : bool

The bidrectional phase starts after "100 Continue" has been sent to the client, and stops when the response body begins. The bidirectional phase is special for the calculation of timeout values (input determines the timeout although the response has started).

method send : resp_token -> unit

Add token to the end of the send queue

method send_queue_empty : bool

Return whether the send queue is empty. When the state is `Inhibited, this method fakes an empty queue.

method protocol : Nethttp.protocol

The HTTP version of the response. This is currently always HTTP/1.1, but maybe we need to fake lower versions for buggy clients. Let's see what comes.

method close_connection : bool

Returns whether the connection should be closed after this response. This flag should be evaluated when the `Resp_end front token has been reached.

method transfer_encoding : transfer_coding

Returns the selected transfer encoding. This is valid after the header has been passed to this object with send.

method front_token : front_token

The first token of the queue, represented as data_chunk. Raises Send_queue_empty when there is currently no front token, or the state is `Inhibited. If there is a front token, it will never have length 0.

Note that Unix_error exceptions can be raised when `Resp_action tokens are processed.

method set_callback : (unit -> unit) -> unit

The function will be called when either set_state changes the state, or when the send queue becomes empty. Note that the callback must never fail, it is called in situations that make it hard to recover from errors.

method body_size : int64

Accumulated size of the response body

These methods must only be called by the HTTP protocol processor:

method set_state : resp_state -> unit

Sets the state

method advance : int -> unit

Tell this object that n bytes of the front token could be really sent using Unix.write. If this means that the whole front token has been sent, the next token is pulled from the queue and is made the new front token. Otherwise, the data chunk representing the front token is modified such that the position is advanced by n, and the length is reduced by n.

This web site is published by Informatikbüro Gerd Stolpmann
Powered by Caml