1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
open Monads
module Status = H2.Status
type t =
{
status : Status.t
; headers : Headers.t
; version : Versions.HTTP.t
; body : Body.t
}
let create
?(version = Versions.HTTP.v1_1)
?( = Headers.empty)
?(body = Body.empty)
status
=
{ status; headers; version; body }
let of_string ?version ? ~body status =
create ?version ?headers ~body:(Body.of_string body) status
let of_bigstring ?version ? ~body status =
create ?version ?headers ~body:(Body.of_bigstring body) status
let of_string_stream ?version ? ~body status =
create ?version ?headers ~body:(Body.of_string_stream body) status
let of_stream ?version ? ~body status =
create ?version ?headers ~body:(Body.of_stream body) status
let of_file ?version ?( = Headers.empty) path =
let open Lwt.Syntax in
let mime = Magic_mime.lookup path in
let =
Headers.(add_unless_exists headers Well_known.content_type mime)
in
let* channel = Lwt_io.open_file ~flags:[ O_RDONLY ] ~mode:Lwt_io.input path in
let+ length = Lwt_io.length channel in
let remaining = ref (Int64.to_int length) in
let stream =
Lwt_stream.from (fun () ->
if !remaining = 0 then
Lwt.return_none
else
let* payload =
Lwt_io.read
~count:(min 0x4000 !remaining)
channel
in
let read = String.length payload in
remaining := !remaining - read;
Lwt.return_some payload)
in
Lwt.on_success (Lwt_stream.closed stream) (fun () ->
Lwt.ignore_result (Lwt_io.close channel));
create
?version
~headers
~body:(Body.of_string_stream ~length:`Chunked stream)
`OK
let upgrade ?version ?( = Headers.empty) upgrade_handler =
create
?version
~headers
~body:
(Body.create
~length:(`Fixed 0L)
(`Empty (Body.Optional_handler.some upgrade_handler)))
`Switching_protocols
let of_http1 ?(body = Body.empty) response =
let { Httpaf.Response.status; version; ; _ } = response in
{ status = (status :> Status.t)
; headers = H2.Headers.of_rev_list (Httpaf.Headers.to_rev_list headers)
; version
; body
}
let to_http1 { status; ; version; _ } =
let =
Httpaf.Headers.of_rev_list (H2.Headers.to_rev_list headers)
in
let status =
match status with
| #Httpaf.Status.t as http1_status ->
http1_status
| `Misdirected_request ->
`Code (H2.Status.to_code status)
in
Httpaf.Response.create ~version ~headers:http1_headers status
let of_h2 ?(body = Body.empty) response =
let { H2.Response.status; } = response in
let = H2.Headers.remove headers ":status" in
{ status; headers; version = { major = 2; minor = 0 }; body }
let persistent_connection { version; ; _ } =
Message.persistent_connection version headers
let pp_hum formatter { ; status; version; _ } =
let formatter (name, value) =
Format.fprintf formatter "%s: %s" name value
in
let reason_phrase =
match status with
| #Status.standard as st ->
Format.asprintf " %s" (Status.default_reason_phrase st)
| `Code _ ->
""
in
Format.fprintf
formatter
"@[%a %a%s@]@\n@[%a@]"
Versions.HTTP.pp_hum
version
Status.pp_hum
status
reason_phrase
(Format.pp_print_list
~pp_sep:(fun f () -> Format.fprintf f "@\n")
format_header)
(Headers.to_list headers)