Skip to content

Commit 2f2a549

Browse files
authored
Merge pull request #26 from robur-coop/expose-request
Expose the request used to communicate with the server
2 parents 0c478c6 + 34134a7 commit 2f2a549

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

src/httpcats.ml

+28-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Server = Http_miou_server
88
module Version = H1.Version
99
module Status = H2.Status
1010
module Headers = H2.Headers
11+
module Method = H2.Method
1112

1213
let ( % ) f g x = f (g x)
1314

@@ -107,6 +108,8 @@ let add_authentication ?(meth = `Basic) ~add headers user_pass =
107108

108109
let user_agent = "hurl/%%VERSION_NUM%%"
109110

111+
type request = { meth: Method.t; target: string; headers: Headers.t }
112+
110113
type response = {
111114
version: Version.t
112115
; status: Status.t
@@ -135,7 +138,7 @@ let pp_error ppf = function
135138

136139
type body = String of string | Stream of string Seq.t
137140
type meta = (Ipaddr.t * int) * Tls.Core.epoch_data option
138-
type 'a handler = meta -> response -> 'a -> string option -> 'a
141+
type 'a handler = meta -> request -> response -> 'a -> string option -> 'a
139142

140143
type config = {
141144
meth: H2.Method.t
@@ -204,7 +207,7 @@ let prep_h2_headers cfg body =
204207
let hdr = List.sort (fun (a, _) (b, _) -> String.compare a b) hdr in
205208
H2.Headers.of_list hdr
206209

207-
let from_h1 response =
210+
let resp_from_h1 response =
208211
{
209212
version= response.H1.Response.version
210213
; status= (response.H1.Response.status :> H2.Status.t)
@@ -213,14 +216,28 @@ let from_h1 response =
213216
H2.Headers.of_list (H1.Headers.to_list response.H1.Response.headers)
214217
}
215218

216-
let from_h2 response =
219+
let req_from_h1 req =
220+
{
221+
meth= req.H1.Request.meth
222+
; target= req.H1.Request.target
223+
; headers= H2.Headers.of_list (H1.Headers.to_list req.H1.Request.headers)
224+
}
225+
226+
let resp_from_h2 response =
217227
{
218228
version= { major= 2; minor= 0 }
219229
; status= response.H2.Response.status
220230
; reason= H2.Status.to_string response.H2.Response.status
221231
; headers= response.H2.Response.headers
222232
}
223233

234+
let req_from_h2 req =
235+
{
236+
meth= req.H2.Request.meth
237+
; target= req.H2.Request.target
238+
; headers= req.H2.Request.headers
239+
}
240+
224241
let _is_a_valid_redirection resp ~uri =
225242
if Status.is_redirection resp.status then
226243
match Headers.get resp.headers "location" with
@@ -253,7 +270,7 @@ let[@warning "-8"] single_http_1_1_request ?(config = H1.Config.default) flow
253270
let request = H1.Request.create ~headers meth path in
254271
let meta = ((cfg.ipaddr, cfg.port), cfg.epoch) in
255272
let f (`V1 resp : Client.response) acc str =
256-
fn meta (from_h1 resp) acc (Some str)
273+
fn meta (req_from_h1 request) (resp_from_h1 resp) acc (Some str)
257274
in
258275
let finally () =
259276
Log.debug (fun m -> m "close the underlying socket");
@@ -283,8 +300,9 @@ let[@warning "-8"] single_http_1_1_request ?(config = H1.Config.default) flow
283300
let* resp = Result.map_error (on_error % fst) resp in
284301
let* () = Result.map_error on_error (Miou.await sender) in
285302
let* () = Result.map_error on_error (Miou.await process) in
286-
let resp = from_h1 resp in
287-
Ok (resp, fn meta resp !acc None)
303+
let req = req_from_h1 request in
304+
let resp = resp_from_h1 resp in
305+
Ok (resp, fn meta req resp !acc None)
288306

289307
let h2_writer body seq () =
290308
let rec next seq reason =
@@ -313,7 +331,7 @@ let[@warning "-8"] single_h2_request ?(config = H2.Config.default) flow cfg
313331
let request = H2.Request.create ~scheme ~headers meth path in
314332
let meta = ((cfg.ipaddr, cfg.port), cfg.epoch) in
315333
let f (`V2 response : Client.response) acc str =
316-
fn meta (from_h2 response) acc (Some str)
334+
fn meta (req_from_h2 request) (resp_from_h2 response) acc (Some str)
317335
in
318336
let (Client.Process { version= V2; acc; response; body; process; _ }) =
319337
Client.run ~f acc (`V2 config) flow (`V2 request)
@@ -336,8 +354,9 @@ let[@warning "-8"] single_h2_request ?(config = H2.Config.default) flow cfg
336354
let* resp = Result.map_error (on_error % fst) resp in
337355
let* () = Result.map_error on_error (Miou.await sender) in
338356
let* () = Result.map_error on_error (Miou.await process) in
339-
let resp = from_h2 resp in
340-
Ok (resp, fn meta resp !acc None)
357+
let req = req_from_h2 request in
358+
let resp = resp_from_h2 resp in
359+
Ok (resp, fn meta req resp !acc None)
341360

342361
let alpn_protocol = function
343362
| `Tcp _ -> None

src/httpcats.mli

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ module Headers = H2.Headers
3131
3232
Case-insensitive key-value pairs. *)
3333

34+
module Method = H2.Method
35+
36+
type request = { meth: Method.t; target: string; headers: Headers.t }
37+
3438
type response = {
3539
version: Version.t
3640
; status: Status.t
@@ -54,7 +58,7 @@ type meta = (Ipaddr.t * int) * Tls.Core.epoch_data option
5458
address and the configuration chosen during the TLS handshake). In this
5559
sense, all this information is condensed into the meta type. *)
5660

57-
type 'a handler = meta -> response -> 'a -> string option -> 'a
61+
type 'a handler = meta -> request -> response -> 'a -> string option -> 'a
5862
(** The handler is a function that is called each time a new part of the
5963
response body is retrieved. The end of the response content is notified by
6064
[None]. The user can then evolve a ['a] value between each call to the

test/test.ml

+10-10
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ let test00 =
137137
let daemon, resolver = Happy_eyeballs_miou_unix.create () in
138138
match
139139
Httpcats.request ~resolver:(`Happy resolver)
140-
~f:(fun _ _resp buf -> function
140+
~f:(fun _ _req _resp buf -> function
141141
| Some str -> Buffer.add_string buf str; buf | None -> buf)
142142
~uri:"http://127.0.0.1:4000/" (Buffer.create 0x10)
143143
with
@@ -195,7 +195,7 @@ let test01 =
195195
let daemon, resolver = Happy_eyeballs_miou_unix.create () in
196196
match
197197
Httpcats.request ~resolver:(`Happy resolver)
198-
~f:(fun _ _resp buf -> function
198+
~f:(fun _ _req _resp buf -> function
199199
| Some str -> Buffer.add_string buf str; buf | None -> buf)
200200
~uri:"http://127.0.0.1:4000" (Buffer.create 0x1000)
201201
with
@@ -292,7 +292,7 @@ let test02 =
292292
in
293293
match
294294
Httpcats.request ~resolver:(`Happy resolver) ~meth:`POST ~body
295-
~f:(fun _ _resp buf -> function
295+
~f:(fun _ _req _resp buf -> function
296296
| Some str -> Buffer.add_string buf str; buf | None -> buf)
297297
~uri:"http://127.0.0.1:4000" (Buffer.create 0x1000)
298298
with
@@ -358,7 +358,7 @@ let test03 =
358358
|> Result.get_ok
359359
in
360360
Httpcats.request ~resolver:(`Happy resolver) ~tls_config
361-
~f:(fun _ _resp buf -> function
361+
~f:(fun _ _req _resp buf -> function
362362
| Some str -> Buffer.add_string buf str; buf | None -> buf)
363363
~uri:"https://127.0.0.1:4000" (Buffer.create 0x10)
364364
|> R.reword_error (R.msgf "%a" Httpcats.pp_error)
@@ -367,7 +367,7 @@ let test03 =
367367
Miou.async @@ fun () ->
368368
let* res =
369369
Httpcats.request ~resolver:(`Happy resolver) ~authenticator
370-
~f:(fun _ _resp buf -> function
370+
~f:(fun _ _req _resp buf -> function
371371
| Some str -> Buffer.add_string buf str; buf | None -> buf)
372372
~uri:"https://127.0.0.1:4000" (Buffer.create 0x10)
373373
|> R.reword_error (R.msgf "%a" Httpcats.pp_error)
@@ -458,15 +458,15 @@ let test04 =
458458
|> Result.get_ok
459459
in
460460
Httpcats.request ~resolver:(`Happy resolver) ~tls_config
461-
~f:(fun _ _resp buf -> function
461+
~f:(fun _ _req _resp buf -> function
462462
| Some str -> Buffer.add_string buf str; buf | None -> buf)
463463
~uri:"https://127.0.0.1:4000" (Buffer.create 0x1000)
464464
|> R.reword_error (R.msgf "%a" Httpcats.pp_error)
465465
in
466466
let h2 =
467467
Miou.async @@ fun () ->
468468
Httpcats.request ~resolver:(`Happy resolver) ~authenticator
469-
~f:(fun _ _resp buf -> function
469+
~f:(fun _ _req _resp buf -> function
470470
| Some str -> Buffer.add_string buf str; buf | None -> buf)
471471
~uri:"https://127.0.0.1:4000" (Buffer.create 0x10)
472472
|> R.reword_error (R.msgf "%a" Httpcats.pp_error)
@@ -546,7 +546,7 @@ let test05 =
546546
in
547547
Httpcats.request ~resolver:(`Happy resolver) ~tls_config ~meth:`POST
548548
~body:(Httpcats.string body)
549-
~f:(fun _ _resp buf -> function
549+
~f:(fun _ _req _resp buf -> function
550550
| Some str -> Buffer.add_string buf str; buf | None -> buf)
551551
~uri:"https://127.0.0.1:4000" (Buffer.create 0x1000)
552552
|> R.reword_error (R.msgf "%a" Httpcats.pp_error)
@@ -555,7 +555,7 @@ let test05 =
555555
Miou.async @@ fun () ->
556556
Httpcats.request ~resolver:(`Happy resolver) ~authenticator ~meth:`POST
557557
~body:(Httpcats.string body)
558-
~f:(fun _ _resp buf -> function
558+
~f:(fun _ _req _resp buf -> function
559559
| Some str -> Buffer.add_string buf str; buf | None -> buf)
560560
~uri:"https://127.0.0.1:4000" (Buffer.create 0x1000)
561561
|> R.reword_error (R.msgf "%a" Httpcats.pp_error)
@@ -607,7 +607,7 @@ let test06 =
607607
let request =
608608
Miou.async @@ fun () ->
609609
Httpcats.request ~resolver:(`Happy resolver) ~authenticator
610-
~f:(fun _ _resp buf -> function
610+
~f:(fun _ _req _resp buf -> function
611611
| Some str -> Buffer.add_string buf str; buf | None -> buf)
612612
~headers:
613613
[

0 commit comments

Comments
 (0)