diff --git a/cohttp/src/request.ml b/cohttp/src/request.ml index 96a251932..e1aa41960 100644 --- a/cohttp/src/request.ml +++ b/cohttp/src/request.ml @@ -204,7 +204,9 @@ module Make (IO : S.IO) = struct Header.add_transfer_encoding headers req.encoding else headers in + print_endline (Header.to_string headers); let headers = Header.Private.move_to_front headers "host" in + print_endline (Header.to_string headers); IO.write oc fst_line >>= fun _ -> Header_IO.write headers oc let make_body_writer ?flush req oc = diff --git a/cohttp/test/test_request.ml b/cohttp/test/test_request.ml index 28c734487..bc999c22c 100644 --- a/cohttp/test/test_request.ml +++ b/cohttp/test/test_request.ml @@ -308,8 +308,12 @@ module Request = Request.Private.Make (Test_io) let null_content_length_header () = let output = Buffer.create 1024 in let () = + (* The user-agent in releases contentsontains the version, we need to strip + it for the test *) + let headers = Cohttp.Header.of_list [ ("user-agent", "ocaml-cohttp") ] in let r = - Cohttp.Request.make_for_client ~chunked:false ~body_length:0L `PUT + Cohttp.Request.make_for_client ~headers ~chunked:false ~body_length:0L + `PUT (Uri.of_string "http://someuri.com") in Request.write_header r output @@ -318,7 +322,7 @@ let null_content_length_header () = "null content-length header are sent" "PUT / HTTP/1.1\r\n\ host: someuri.com\r\n\ - user-agent: ocaml-cohttp/\r\n\ + user-agent: ocaml-cohttp\r\n\ content-length: 0\r\n\ \r\n" (Buffer.to_string output) diff --git a/http/src/http.ml b/http/src/http.ml index 6301943fd..0c185f349 100644 --- a/http/src/http.ml +++ b/http/src/http.ml @@ -162,14 +162,20 @@ module Header = struct add_multi h k xs let move_to_front t hdr_name = - match t with - | (k, _) :: _ when caseless_equal k hdr_name -> t - | _ -> ( - match get t hdr_name with - | Some v -> - let headers = remove t hdr_name in - add headers hdr_name v - | None -> t) + (* Headers are manipulated in reverse order for convenience, so we + need to reverse them, prepend what we need, and reverse again *) + let t = List.rev t in + let t = + match t with + | (k, _) :: _ when caseless_equal k hdr_name -> t + | _ -> ( + match get t hdr_name with + | Some v -> + let headers = remove t hdr_name in + add headers hdr_name v + | None -> t) + in + List.rev t let map (f : string -> string -> string) (h : t) : t = List.map @@ -349,7 +355,7 @@ module Header = struct module Private = struct let caseless_equal = caseless_equal - let first = first + let first l = first (List.rev l) let move_to_front = move_to_front end end