From 70b354510175beb64d534f55a0ad8ae7ceb7a316 Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Tue, 5 Nov 2024 10:56:05 +0100 Subject: [PATCH 1/3] hurl: fix output to a (non-existing) file --- app/hurl.ml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/hurl.ml b/app/hurl.ml index c944a59..8cf6348 100644 --- a/app/hurl.ml +++ b/app/hurl.ml @@ -18,7 +18,9 @@ let jump () protocol uri meth headers input output no_follow = let fd, close = match output with | None -> Unix.stdout, fun () -> () | Some fn -> - let fd = Unix.openfile fn [ Unix.O_WRONLY ] 0o644 in + if Sys.file_exists fn then + invalid_arg ("output file " ^ fn ^ " already exists"); + let fd = Unix.openfile fn [ Unix.O_WRONLY; Unix.O_CREAT ] 0o644 in fd, fun () -> Unix.close fd in let reply _response () data = @@ -58,7 +60,7 @@ let uri = let output = let doc = "Save body output to a file" in - Arg.(value & opt (some file) None & info [ "output" ] ~doc ~docv:"OUTPUT") + Arg.(value & opt (some string) None & info [ "output" ] ~doc ~docv:"OUTPUT") let input = let doc = "Upload a file as body" in From 2a2e1fd601f60ee474254ee24c0591a4f6194106 Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Tue, 5 Nov 2024 10:56:40 +0100 Subject: [PATCH 2/3] if a redirect with a body is provided, we need to skip the body --- src/http_lwt_client.ml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/http_lwt_client.ml b/src/http_lwt_client.ml index 68d9233..52259ad 100644 --- a/src/http_lwt_client.ml +++ b/src/http_lwt_client.ml @@ -373,6 +373,12 @@ let request if count = 0 then Lwt.return (Error (`Msg "redirect limit exceeded")) else + let f response acc data = + if Status.is_redirection response.status then + Lwt.return acc + else + f response acc data + in single_request happy_eyeballs ?config tls_config ~meth ~headers ?body uri f f_init >>= fun (resp, body) -> if Status.is_redirection resp.status then From be2485e5d366398943697d8be2dab0b5d25f5a0a Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Tue, 5 Nov 2024 12:24:49 +0100 Subject: [PATCH 3/3] clarify how f and follow_redirect interact --- src/http_lwt_client.mli | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/http_lwt_client.mli b/src/http_lwt_client.mli index 367eee2..8883c21 100644 --- a/src/http_lwt_client.mli +++ b/src/http_lwt_client.mli @@ -38,6 +38,8 @@ val pp_response : Format.formatter -> response -> unit and returns the response. Each time part of the body is received, [f acc part] is called, with [acc] being the last return value of [f] (or [init] if it is the first time) and [part] being the body part received. + If [follow_redirect] is true (the default), and there's a redirection, [f] + is not called with the (potential) body of the redirection page. By default, up to 5 redirects ([max_redirect]) are followed. If [follow_redirect] is false, no redirect is followed (defaults to true).