diff --git a/lib/header.ml b/lib/header.ml index 9a57bfa1f9..0fbea90c5d 100644 --- a/lib/header.ml +++ b/lib/header.ml @@ -228,9 +228,9 @@ let get_location headers = | Some u -> Some (Uri.of_string u) let get_links headers = - List.fold_left - (fun list link_s -> (Link.of_string link_s)@list) - [] (get_multi headers "link") + List.rev (List.fold_left + (fun list link_s -> List.rev_append (Link.of_string link_s) list) + [] (get_multi headers "link")) let add_links headers links = add_multi headers "link" (List.map Link.to_string links) diff --git a/lib/header_io.ml b/lib/header_io.ml index 339111ca71..a769b3a42c 100644 --- a/lib/header_io.ml +++ b/lib/header_io.ml @@ -19,7 +19,7 @@ let split_header str = match Stringext.split ~max:2 ~on:':' str with - | x::y::[] -> [x; Stringext.trim_left y] + | x::y::[] -> [x; String.trim y] | x -> x module Make(IO : S.IO) = struct @@ -27,11 +27,13 @@ module Make(IO : S.IO) = struct module Transfer_IO = Transfer_io.Make(IO) + let rev _k v = List.rev v + let parse ic = (* consume also trailing "^\r\n$" line *) let rec parse_headers' headers = read_line ic >>= function - |Some "" | None -> return headers + |Some "" | None -> return (Header.map rev headers) |Some line -> begin match split_header line with | [hd;tl] -> diff --git a/lib_test/test_header.ml b/lib_test/test_header.ml index f7957ac9f4..d8e5523722 100644 --- a/lib_test/test_header.ml +++ b/lib_test/test_header.ml @@ -388,6 +388,23 @@ let link_ext_star () = }; ]) (H.get_links headers) +let trim_ws () = + let resp = get_resp ["Age: 281 "] in + let headers = headers_of_response "trim whitespace" resp in + assert_equal + ~printer:(function + | None -> "None" + | Some x -> "\"" ^ x ^ "\"") (H.get headers "age") (Some "281") + +let test_cachecontrol_concat () = + let resp = get_resp ["Cache-Control: public"; + "Cache-Control: max-age:86400"] in + let h = headers_of_response "concat Cache-Control" resp in + assert_equal + ~printer:(function + | None -> "None" + | Some x -> x) (Some "public,max-age:86400") (H.get h "Cache-Control") + ;; Printexc.record_backtrace true; Alcotest.run "test_header" [ @@ -424,7 +441,11 @@ Alcotest.run "test_header" [ "content-length", `Quick, Content_range.content_length; "content-range", `Quick, Content_range.content_range; ]; + "Cache Control", [ + "concat", `Quick, test_cachecontrol_concat + ]; "Header", [ "get list valued", `Quick, list_valued_header; + "trim whitespace", `Quick, trim_ws; ]; ]