Module Header_name

HTTP Header Names as Polymorphic Variants

This module provides type-safe HTTP header names using polymorphic variants. All standard headers have dedicated variants, with `Other for non-standard or unknown headers.

Usage

  (* Use standard headers directly *)
  let headers = Headers.empty
    |> Headers.set `Content_type "application/json"
    |> Headers.set `Accept "text/html"

  (* Use custom headers with `Other *)
  let headers = headers
    |> Headers.set (`Other "X-Custom-Header") "value"

  (* Pattern match on headers *)
  match Headers.get `Content_type headers with
  | Some ct -> print_endline ct
  | None -> ()

Header names are case-insensitive per RFC 9110 Section 5.1.

Header definitions are based on the IANA HTTP Field Name Registry: IANA HTTP Field Name Registry

Types

type standard = [
  1. | `Accept
  2. | `Accept_encoding
  3. | `Accept_language
  4. | `Accept_ranges
  5. | `Allow
  6. | `Content_encoding
  7. | `Content_language
  8. | `Content_length
  9. | `Content_location
  10. | `Content_range
  11. | `Content_type
  12. | `Expect
  13. | `From
  14. | `Host
  15. | `Max_forwards
  16. | `Range
  17. | `Referer
  18. | `Te
  19. | `User_agent
  20. | `Location
  21. | `Retry_after
  22. | `Server
  23. | `Etag
  24. | `Last_modified
  25. | `Vary
  26. | `If_match
  27. | `If_modified_since
  28. | `If_none_match
  29. | `If_range
  30. | `If_unmodified_since
  31. | `Authorization
  32. | `Authentication_info
  33. | `Proxy_authenticate
  34. | `Proxy_authentication_info
  35. | `Proxy_authorization
  36. | `Www_authenticate
  37. | `Connection
  38. | `Upgrade
  39. | `Via
  40. | `Date
  41. | `Age
  42. | `Cache_control
  43. | `Expires
  44. | `Pragma
  45. | `Cache_status
  46. | `Keep_alive
  47. | `Trailer
  48. | `Transfer_encoding
  49. | `Cookie
  50. | `Access_control_allow_credentials
  51. | `Access_control_allow_headers
  52. | `Access_control_allow_methods
  53. | `Access_control_allow_origin
  54. | `Access_control_expose_headers
  55. | `Access_control_max_age
  56. | `Access_control_request_headers
  57. | `Access_control_request_method
  58. | `Origin
  59. | `Cross_origin_embedder_policy
  60. | `Cross_origin_embedder_policy_report_only
  61. | `Cross_origin_opener_policy
  62. | `Cross_origin_opener_policy_report_only
  63. | `Cross_origin_resource_policy
  64. | `Sec_fetch_dest
  65. | `Sec_fetch_mode
  66. | `Sec_fetch_site
  67. | `Sec_fetch_user
  68. | `Content_security_policy
  69. | `Content_security_policy_report_only
  70. | `Strict_transport_security
  71. | `X_content_type_options
  72. | `X_frame_options
  73. | `Referrer_policy
  74. | `Optional_www_authenticate
  75. | `Authentication_control
  76. | `Dpop
  77. | `Dpop_nonce
  78. | `Content_digest
  79. | `Repr_digest
  80. | `Want_content_digest
  81. | `Want_repr_digest
  82. | `Signature
  83. | `Signature_input
  84. | `Accept_signature
  85. | `Sec_websocket_key
  86. | `Sec_websocket_accept
  87. | `Sec_websocket_protocol
  88. | `Sec_websocket_version
  89. | `Sec_websocket_extensions
]

Standard HTTP header names.

These cover headers defined in:

type t = [
  1. | standard
  2. | `Other of string
]

Complete header name type including non-standard headers.

Use `Other name for headers not in the standard set. The name should be provided in its canonical form (e.g., "X-Custom-Header").

Conversion

val to_string : t -> string

to_string name converts a header name to its canonical wire format.

Standard headers use their canonical capitalization (e.g., `Content_type becomes "Content-Type"). `Other headers are returned as-is.

val of_string : string -> t

of_string s parses a string into a header name.

Performs case-insensitive matching against known headers. Unknown headers are wrapped in `Other.

val to_lowercase_string : t -> string

to_lowercase_string name returns the lowercase form for internal use.

Comparison

val compare : t -> t -> int

compare a b compares two header names case-insensitively.

val equal : t -> t -> bool

equal a b checks equality of two header names case-insensitively.

Pretty Printing

val pp : Format.formatter -> t -> unit

pp ppf name pretty-prints a header name.

Header Categories

val hop_by_hop_headers : t list

Default hop-by-hop headers per RFC 9110 Section 7.6.1.

These headers MUST be removed before forwarding a message: Connection, Keep-Alive, Proxy-Authenticate, Proxy-Authorization, TE, Trailer, Transfer-Encoding, Upgrade, Via.

val forbidden_trailer_headers : t list

Headers that MUST NOT appear in trailers per RFC 9110 Section 6.5.1.

Includes: Transfer-Encoding, Content-Length, Host, Content-Encoding, Content-Type, Trailer.

val cors_response_headers : t list

CORS response headers that control cross-origin access.

val cors_request_headers : t list

CORS request headers used in preflight requests.

val security_headers : t list

Headers related to web security policies.

val fetch_metadata_headers : t list

Browser-set headers providing request context.

val websocket_headers : t list

Headers used during WebSocket upgrade.

val is_hop_by_hop : t -> bool

is_hop_by_hop name returns true if name is a hop-by-hop header.

val is_forbidden_trailer : t -> bool

is_forbidden_trailer name returns true if name is forbidden in trailers.

val is_cors_response : t -> bool

is_cors_response name returns true if name is a CORS response header.

val is_cors_request : t -> bool

is_cors_request name returns true if name is a CORS request header.

val is_security : t -> bool

is_security name returns true if name is a security header.

val is_fetch_metadata : t -> bool

is_fetch_metadata name returns true if name is a fetch metadata header.

val is_websocket : t -> bool

is_websocket name returns true if name is a WebSocket header.