Source file client.ml
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
open Import
module Logs = (val Logging.setup ~src:"piaf.client" ~doc:"Piaf Client module")
module Connection_info = Connection.Info
type t =
{ mutable conn : Connection.t
; env : Eio_unix.Stdenv.base
; sw : Switch.t
}
let create_http_connection ~sw ~config ~conn_info ~uri fd =
let (module Http), version =
match
( config.Config.http2_prior_knowledge
, config.max_http_version
, config.h2c_upgrade )
with
| true, _, _ -> (module Http2.HTTP : Http_intf.HTTP), Versions.HTTP.HTTP_2
| false, HTTP_2, true -> (module Http1.HTTP : Http_intf.HTTP), HTTP_1_1
| false, _, _ ->
let version =
if Versions.HTTP.Raw.(compare (of_version config.max_http_version) v2_0)
>= 0
then Versions.HTTP.HTTP_1_1
else config.max_http_version
in
(module Http1.HTTP : Http_intf.HTTP), version
in
Http_impl.create_connection
(module Http)
~sw
~config
~conn_info
~uri
~version
~fd
fd
let create_https_connection ~sw ~config ~conn_info ~uri fd =
let { Connection_info.host; _ } = conn_info in
let*! { ssl = ssl_client; ssl_ctx } =
Openssl.connect ~config ~hostname:host fd
in
let (module Https), version =
let ssl_socket = Eio_ssl.Context.ssl_socket ssl_ctx in
match Ssl.get_negotiated_alpn_protocol ssl_socket with
| None ->
Logs.warn (fun m ->
let alpn_protocols =
Versions.ALPN.protocols_of_version config.Config.max_http_version
in
let protos =
String.concat
", "
(List.map (fun proto -> Format.asprintf "%S" proto) alpn_protocols)
in
m "ALPN: Failed to negotiate requested protocols (%s)" protos);
let impl, version =
if config.http2_prior_knowledge
then (module Http2.HTTPS : Http_intf.HTTPS), Versions.HTTP.HTTP_2
else
( (module Http1.HTTPS : Http_intf.HTTPS)
, if Versions.HTTP.Raw.(
compare (of_version config.max_http_version) v2_0)
>= 0
then HTTP_1_1
else config.max_http_version )
in
Logs.info (fun m -> m "Defaulting to %a" Versions.HTTP.pp version);
impl, version
| Some negotiated_proto ->
Logs.info (fun m -> m "ALPN: server agreed to use %s" negotiated_proto);
(match Versions.ALPN.of_string negotiated_proto with
| Some version ->
( (match version with
| HTTP_1_0 | HTTP_1_1 -> (module Http1.HTTPS : Http_intf.HTTPS)
| HTTP_2 -> (module Http2.HTTPS : Http_intf.HTTPS))
, version )
| None ->
assert false)
in
Http_impl.create_connection
(module Https)
~sw
~config
~conn_info
~uri
~version
~fd
ssl_client
let open_connection ~sw ~config ~uri env conn_info =
let*! socket =
let clock = Eio.Stdenv.clock env in
let network = Eio.Stdenv.net env in
Connection.connect ~sw ~clock ~network ~config conn_info
in
(if config.Config.tcp_nodelay
then
let fd = Eio_unix.Resource.fd_opt socket |> Option.get in
Eio_unix.Fd.use_exn "Client.open_connection" fd (fun fd ->
Unix.setsockopt fd TCP_NODELAY true;
Logs.debug (fun m -> m "TCP_NODELAY set")));
Logs.info (fun m -> m "Connected to %a" Connection_info.pp_hum conn_info);
match conn_info.scheme with
| `HTTP -> create_http_connection ~sw ~config ~conn_info ~uri socket
| `HTTPS -> create_https_connection ~sw ~config ~conn_info ~uri socket
let shutdown_conn (Connection.Conn { impl; connection; info; fd; _ }) =
Logs.info (fun m ->
m
"Tearing down %s connection to %a"
(String.uppercase_ascii (Scheme.to_string info.scheme))
Connection_info.pp_hum
info);
Http_impl.shutdown (module (val impl)) ~fd connection
let change_connection t (conn_info : Connection_info.t) =
let { sw; conn = Connection.Conn { config; _ } as conn; env; _ } = t in
Fiber.fork ~sw (fun () -> shutdown_conn conn);
let+! conn' = open_connection ~sw ~config ~uri:conn_info.uri env conn_info in
t.conn <- conn'
let shutdown t = shutdown_conn t.conn
let reuse_or_set_up_new_connection
({ conn =
Connection.Conn
({ impl = (module Http); connection; config; info; _ } as conn)
; env
; _
} as t)
new_uri
=
match Scheme.of_uri new_uri with
| Error _ as e -> e
| Ok new_scheme ->
let new_conn_info =
{ info with
port = Connection_info.infer_port ~scheme:new_scheme new_uri
; scheme = new_scheme
; host = Uri.host_exn new_uri
; uri = new_uri
}
in
if (not conn.persistent) || Http_impl.is_closed (module Http) connection
then
let*! new_addresses =
Connection.resolve_host
env
~config
~port:new_conn_info.port
new_conn_info.host
in
let new_conn_info = { new_conn_info with addresses = new_addresses } in
let+! () = change_connection t new_conn_info in
false
else if Connection_info.equal_without_resolving info new_conn_info
then (
Logs.debug (fun m ->
m "Reusing the same connection as the host / port didn't change");
conn.info <- new_conn_info;
Ok true)
else
let*! new_addresses =
Connection.resolve_host
env
~config
~port:new_conn_info.port
new_conn_info.host
in
let new_conn_info = { new_conn_info with addresses = new_addresses } in
if Connection_info.equal info new_conn_info
then (
Logs.debug (fun m ->
m "Reusing the same connection as the remote address didn't change");
conn.info <- new_conn_info;
Ok true)
else
let+! () = change_connection t new_conn_info in
false
type request_info =
{ remaining_redirects : int
; headers : (string * string) list
; request : Request.t
; meth : H2.Method.t
; target : string
; is_h2c_upgrade : bool
}
let rec return_response
({ conn; sw; _ } as t)
({ request; _ } as request_info)
({ Response.status; ; version; body } as response)
=
let (Connection.Conn
{ impl = (module Http)
; runtime
; info = { Connection_info.scheme; _ } as conn_info
; uri
; fd
; config
; _
})
=
conn
in
match request_info.is_h2c_upgrade, scheme, version, status, config with
| ( true
, `HTTP
, HTTP_1_1
, `Switching_protocols
, { Config.h2c_upgrade = true
; max_http_version = HTTP_2
; http2_prior_knowledge = false
; _
} ) ->
(match
Headers.(
get headers Well_known.connection, get headers Well_known.upgrade)
with
| Some ("Upgrade" | "upgrade"), Some "h2c" ->
Logs.debug (fun m -> m "Received 101, server accepted HTTP/2 upgrade");
let (module Http2) = (module Http2.HTTP : Http_intf.HTTP2) in
let*! () = Body.drain body in
(match
Http_impl.create_h2c_connection
~config
~conn_info
~uri
~sw
~fd
~http_request:request
runtime
with
| Error #Error.client as err -> err
| Ok (h2_conn, response) ->
t.conn <- h2_conn;
return_response t request_info response)
| _ -> Ok response)
| _ -> Ok response
let is_h2c_upgrade ~config ~version ~scheme =
match
( config.Config.http2_prior_knowledge
, version
, config.max_http_version
, config.h2c_upgrade
, scheme )
with
| false, Versions.HTTP.HTTP_1_1, HTTP_2, true, `HTTP -> true
| _ -> false
let make_request_info
{ conn = Connection.Conn { version; info; config; _ }; _ }
?(remaining_redirects = config.max_redirects)
~meth
~
~body
target
=
let { Connection_info.host; scheme; _ } = info in
let is_h2c_upgrade = is_h2c_upgrade ~config ~version ~scheme in
let request =
let =
let h2_settings =
H2.Settings.to_base64 (Config.to_http2_settings config)
in
let =
let open Headers in
if is_h2c_upgrade
then
(Well_known.connection, "Upgrade, HTTP2-Settings")
:: (Well_known.upgrade, "h2c")
:: ("HTTP2-Settings", Result.get_ok h2_settings)
:: headers
else headers
in
Headers.canonicalize_headers
~version
~host
~body_length:body.Body.length
headers
in
Request.create
~meth
~version
~scheme
~headers:canonical_headers
~body
target
in
{ remaining_redirects; headers; request; meth; target; is_h2c_upgrade }
let rec send_request_and_handle_response
t
({ remaining_redirects; request; ; meth; _ } as request_info)
=
let { conn = Conn ({ info; uri; config; _ } as conn); _ } = t in
match Http_impl.send_request ~sw:t.sw t.conn request with
| Error (#Error.client as err) -> Error err
| Ok response ->
if conn.persistent
then conn.persistent <- Response.persistent_connection response;
(match
( H2.Status.is_redirection response.status
, config.follow_redirects
, remaining_redirects
, Headers.(get response.headers Well_known.location) )
with
| true, true, 0, _ ->
let msg =
Format.asprintf "Maximum (%d) redirects followed" config.max_redirects
in
Logs.err (fun m -> m "%s" msg);
Error (`Connect_error msg)
| true, true, _, Some location ->
let { Connection_info.scheme; _ } = info in
let new_uri = Uri.parse_with_base_uri ~scheme ~uri location in
(match reuse_or_set_up_new_connection t new_uri with
| Error #Error.client as err -> err
| Ok did_reuse ->
if did_reuse
then
Fiber.fork ~sw:t.sw (fun () ->
let (_drained : _ result) = Body.drain response.body in
());
if Status.is_permanent_redirection response.status
then conn.uri <- new_uri;
let request_info' =
let target = Uri.path_and_query new_uri in
let meth' =
match meth, response.status with
| `POST, (`Found | `Moved_permanently) -> `GET
| _ -> meth
in
make_request_info
t
~remaining_redirects:(remaining_redirects - 1)
~meth:meth'
~headers
~body:request.body
target
in
send_request_and_handle_response t request_info')
| false, _, _, _ | _, false, _, _ | true, true, _, None ->
return_response t request_info response)
let create ?(config = Config.default) ~sw env uri =
let*! conn_info = Connection_info.of_uri env ~config uri in
match open_connection ~config ~sw ~uri:conn_info.uri env conn_info with
| Ok conn -> Ok { conn; env; sw }
| Error (#Error.client as err) -> Error err
let call t ~meth ?( = []) ?(body = Body.empty) target =
let (Connection.Conn { impl = (module Http); connection; config; uri; _ }) =
t.conn
in
let reused =
if config.follow_redirects || Http_impl.is_closed (module Http) connection
then reuse_or_set_up_new_connection t uri
else Ok true
in
match reused with
| Error #Error.client as err -> err
| Ok _ ->
let request_info =
let = config.default_headers @ headers in
make_request_info t ~meth ~headers ~body target
in
let (Connection.Conn conn) = t.conn in
conn.persistent <- Request.persistent_connection request_info.request;
send_request_and_handle_response t request_info
let request t ? ?body ~meth target = call t ?headers ?body ~meth target
let send t { ; body; meth; target; _ } =
call t ~headers:(Headers.to_list headers) ~body ~meth target
let head t ? target = call t ?headers ~meth:`HEAD target
let get t ? target = call t ?headers ~meth:`GET target
let post t ? ?body target = call t ?headers ?body ~meth:`POST target
let put t ? ?body target = call t ?headers ?body ~meth:`PUT target
let patch t ? ?body target =
call t ?headers ?body ~meth:(`Other "PATCH") target
let delete t ? ?body target = call t ?headers ?body ~meth:`DELETE target
let ws_upgrade :
t
-> ?headers:(string * string) list
-> string
-> (Ws.Descriptor.t, [> Error.client ]) result
=
fun t ?( = []) target ->
let*! response =
let request =
let (Conn { info; _ }) = t.conn in
let nonce = Openssl.random_string 16 in
Ws.upgrade_request
~headers:(Httpun.Headers.of_list headers)
~scheme:info.scheme
~nonce
target
in
send t request
in
match Body.drain response.body with
| Error #Error.t as err -> err
| Ok () -> Http_impl.upgrade_connection ~sw:t.sw t.conn
module Oneshot = struct
let call
?(config = Config.default)
?( = [])
?(body = Body.empty)
~sw
~meth
env
uri
=
let*! ({ conn = Connection.Conn conn; _ } as t) =
create ~config ~sw env uri
in
let target = Uri.path_and_query conn.uri in
let = conn.config.default_headers @ headers in
let request_info = make_request_info t ~meth ~headers ~body target in
conn.persistent <- Request.persistent_connection request_info.request;
let response_result = send_request_and_handle_response t request_info in
Fiber.fork ~sw (fun () ->
(match response_result with
| Ok { Response.body; _ } ->
ignore (Body.closed body : (unit, Error.t) result)
| Error _ -> ());
shutdown t);
response_result
let request ?config ? ?body ~sw env ~meth uri =
call ?config ?headers ?body ~meth ~sw env uri
let head ?config ? ~sw env uri =
call ?config ~sw env ~meth:`HEAD ?headers uri
let get ?config ? ~sw env uri =
call ?config ~sw env ~meth:`GET ?headers uri
let post ?config ? ?body ~sw env uri =
call ?config ?headers ?body ~sw env ~meth:`POST uri
let put ?config ? ?body ~sw env uri =
call ?config ?headers ?body ~sw env ~meth:`PUT uri
let patch ?config ? ?body ~sw env uri =
call ?config ?headers ?body ~sw env ~meth:(`Other "PATCH") uri
let delete ?config ? ?body ~sw env uri =
call ?config ?headers ?body ~sw env ~meth:`DELETE uri
end