An OCaml webserver, but the allocating version (vs httpz which doesnt)
at main 101 lines 3.9 kB view raw
1(** HTTP response writing utilities. *) 2 3(** HTTP response status codes per 4 {{:https://datatracker.ietf.org/doc/html/rfc7231#section-6}RFC 7231 Section 6}. *) 5type status = 6 (* 1xx Informational *) 7 | Continue (** 100 - for Expect: 100-continue *) 8 | Switching_protocols (** 101 - for Upgrade *) 9 (* 2xx Success *) 10 | Success (** 200 OK *) 11 | Created (** 201 *) 12 | Accepted (** 202 *) 13 | No_content (** 204 *) 14 | Partial_content (** 206 - for Range requests *) 15 (* 3xx Redirection *) 16 | Moved_permanently (** 301 *) 17 | Found (** 302 *) 18 | See_other (** 303 *) 19 | Not_modified (** 304 *) 20 | Temporary_redirect (** 307 *) 21 | Permanent_redirect (** 308 *) 22 (* 4xx Client Error *) 23 | Bad_request (** 400 *) 24 | Unauthorized (** 401 *) 25 | Forbidden (** 403 *) 26 | Not_found (** 404 *) 27 | Method_not_allowed (** 405 *) 28 | Not_acceptable (** 406 *) 29 | Request_timeout (** 408 *) 30 | Conflict (** 409 *) 31 | Gone (** 410 *) 32 | Length_required (** 411 *) 33 | Precondition_failed (** 412 *) 34 | Payload_too_large (** 413 *) 35 | Uri_too_long (** 414 *) 36 | Unsupported_media_type (** 415 *) 37 | Range_not_satisfiable (** 416 *) 38 | Expectation_failed (** 417 *) 39 | Unprocessable_entity (** 422 *) 40 | Upgrade_required (** 426 *) 41 | Precondition_required (** 428 *) 42 | Too_many_requests (** 429 *) 43 (* 5xx Server Error *) 44 | Internal_server_error (** 500 *) 45 | Not_implemented (** 501 *) 46 | Bad_gateway (** 502 *) 47 | Service_unavailable (** 503 *) 48 | Gateway_timeout (** 504 *) 49 | Http_version_not_supported (** 505 *) 50 51(** Get numeric status code. *) 52val status_code : status -> int 53 54(** Get reason phrase. *) 55val status_reason : status -> string 56 57(** Get "CODE Reason" string. *) 58val status_to_string : status -> string 59 60(** Pretty-print status. *) 61val pp_status : Stdlib.Format.formatter -> status -> unit 62 63(** {2 Response Writers} *) 64 65(** Write "HTTP/1.x CODE Reason\r\n" at offset. Returns new offset. *) 66val write_status_line : Base_bigstring.t -> off:int -> status -> Version.t -> int 67 68(** Write "Name: Value\r\n" at offset using string name. Returns new offset. *) 69val write_header : Base_bigstring.t -> off:int -> string -> string -> int 70 71(** Write header with integer value using string name. Returns new offset. *) 72val write_header_int : Base_bigstring.t -> off:int -> string -> int -> int 73 74(** Write "Name: Value\r\n" using typed header name. Returns new offset. *) 75val write_header_name : Base_bigstring.t -> off:int -> Header_name.t -> string -> int 76 77(** Write header with integer value using typed header name. Returns new offset. *) 78val write_header_name_int : Base_bigstring.t -> off:int -> Header_name.t -> int -> int 79 80(** Write "\r\n". Returns new offset. *) 81val write_crlf : Base_bigstring.t -> off:int -> int 82 83(** Write "Content-Length: N\r\n". Returns new offset. *) 84val write_content_length : Base_bigstring.t -> off:int -> int -> int 85 86(** Write "Connection: keep-alive\r\n" or "Connection: close\r\n". *) 87val write_connection : Base_bigstring.t -> off:int -> keep_alive:bool -> int 88 89(** {2 Chunked Transfer Encoding} *) 90 91(** Write "Transfer-Encoding: chunked\r\n". Returns new offset. *) 92val write_transfer_encoding_chunked : Base_bigstring.t -> off:int -> int 93 94(** Write chunk header "<hex-size>\r\n". Returns new offset. *) 95val write_chunk_header : Base_bigstring.t -> off:int -> size:int -> int 96 97(** Write chunk footer "\r\n" after chunk data. Returns new offset. *) 98val write_chunk_footer : Base_bigstring.t -> off:int -> int 99 100(** Write final chunk "0\r\n\r\n". Returns new offset. *) 101val write_final_chunk : Base_bigstring.t -> off:int -> int