Skip to content

Commit

Permalink
Merge pull request #26 from robur-coop/fixes
Browse files Browse the repository at this point in the history
Fixes: fetch a page which has a redirect with a non-empty body
  • Loading branch information
hannesm authored Nov 5, 2024
2 parents 8f12b31 + be2485e commit 1fb50c0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/hurl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/http_lwt_client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/http_lwt_client.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit 1fb50c0

Please sign in to comment.