ocaml-cohttp icon indicating copy to clipboard operation
ocaml-cohttp copied to clipboard

Bad GET request when host is an IPv6 litteral results in 400 bad Request

Open benjamreis opened this issue 2 years ago • 2 comments

Hello!

I've been having trouble making IPv6 GET requests with the cohttp lib: when the host part of a Uri is an IPv6 litteral brackets needs to be added around it when creating the Host header of the request, please see below:

A request created by this lib: bad-get

it does not have the brackets and so the Full requst uri field is not valid, whereas a simple wget from the same machine: good-get does have them an the URI is valid.

I've been working around it by manually adding the host field with the brackets:

          let headers = Cohttp.Header.init () in
          let headers =
            Cohttp.Header.add_unless_exists headers "host"
              (match Uri.scheme uri with
              | Some "httpunix" -> ""
              | _ -> (
                (Http.Url.maybe_wrap_IPv6_literal (Uri.host_with_default ~default:"localhost" uri))
                  ^
                  match Uri.port uri with Some p -> ":" ^ string_of_int p | None -> ""))
          in
          let request = Cohttp.Request.make ~meth:`GET ?headers:(Some headers) uri in

But the default host when not manually provided should also have them. From what i understand the brackets should be added here when the host of uri is in IPv6 litteral.

Thanks

benjamreis avatar Sep 01 '23 08:09 benjamreis

I think this should be fixed in the uri package, then all dependent libraries would benefit directly from the fix.

mseri avatar Sep 15 '23 14:09 mseri

Thanks @mseri!

If I read @dinosaure's comment correctly, he agrees with you that it should be fixed in uri: https://github.com/mirage/ocaml-uri/pull/169#discussion_r1330382757:

It seems correct, the domain part of email addresses also use brackets to delimit user-defined domains or IPv6 address.

bernhardkaindl avatar Sep 19 '23 14:09 bernhardkaindl