ocaml-cohttp
ocaml-cohttp copied to clipboard
Connexion to http proxy example
Hello, is there an example for sending an http request throught a proxy ? I did not seen anything for that in the Client module.
I figure how to write the code for that, but it require some change in the api.
In cohttp_lwt, we need to add the proxy information :
let call ?proxy ?(ctx=default_ctx) ?headers ?(body=`Empty) ?chunked meth uri =
let target = match proxy with
| None -> uri
| Some x -> x in
let headers = match headers with None -> Header.init () | Some h -> h in
Net.connect_uri ~ctx target >>= fun (conn, ic, oc) ->
…
But it also require to change the Request module in order to give the full destination URL :
let write_header req oc =
let scheme = match Uri.scheme req.uri with
| Some x -> x
| None -> "http" in
let fst_line =
Printf.sprintf "%s %s://%s%s %s\r\n"
(Code.string_of_method req.meth)
scheme
(Uri.host_with_default ~default:"localhost" req.uri)
(Uri.path_and_query req.uri)
(Code.string_of_version req.version) in
…
(I did not test with https proxy, which is not what I require). Can you tell me what do you think of thoses modifications, and I they could be integrated ?
It is possible to do this without changing the cohttp API (although that would be nice). 0install uses this code for it:
https://github.com/0install/0install/blob/6c0f5c51bc099370a367102e48723a42cd352b3b/ocaml/zeroinstall/http.cohttp.ml#L232-L256
The trick is to use callv
, which takes a Request.t
rather than just a URI. Then you can set the resource directly.
recently ran into this too. Would be great if cohttp supported/respected HTTP(S)_PROXY env vars, or at least provided an interface to make it easier for consumers of the library to do it themselves