Skip to content

Commit d49809c

Browse files
committed
add user agent header and fix host/content-length httpun headers
1 parent e22fdfa commit d49809c

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/common/req.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ let header_params ?(debug=false) s =
9292
acc
9393
) [] l in
9494
value, params
95+
96+
let user_agent_header = ref (Some ("user-agent", "ez_api/2.1.1"))

src/request/ezRequest.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ module Make(S : Interface) : S = struct
119119
let meth = Service.meth service.s in
120120
let input_encoding = Service.input service.s in
121121
let url = forge api service arg params in
122+
let headers = match !Req.user_agent_header, headers with
123+
| None, _ -> headers
124+
| Some h, None -> Some [ h ]
125+
| Some h, Some l ->
126+
if List.exists (fun (k, _) -> String.lowercase_ascii k = "user-agent") l then Some l
127+
else Some (h :: l) in
122128
match input_encoding with
123129
| Empty ->
124130
if post then

src/request/ezRequest_lwt.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ module Make(S : Interface) : S = struct
113113
let meth = Service.meth service.s in
114114
let input_encoding = Service.input service.s in
115115
let url = forge api service arg params in
116+
let headers = match !Req.user_agent_header, headers with
117+
| None, _ -> headers
118+
| Some h, None -> Some [ h ]
119+
| Some h, Some l ->
120+
if List.exists (fun (k, _) -> String.lowercase_ascii k = "user-agent") l then Some l
121+
else Some (h :: l) in
116122
begin match input_encoding with
117123
| Empty ->
118124
if post then

src/request/unix/httpun/httpun_client.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ let perform ?msg ?meth ?content ?content_type ?(headers=[]) ?timeout handler url
125125
let> addresses = Lwt_unix.getaddrinfo hostname (Int.to_string port) [Unix.(AI_FAMILY PF_INET)] in
126126
let socket = Lwt_unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
127127
let> () = Lwt_unix.connect socket (List.hd addresses).Unix.ai_addr in
128+
let host = if (port <> 80 && port <> 443) then Format.sprintf "%s:%d" hostname port else hostname in
128129
let headers = Headers.of_list @@
129-
[ "host", hostname ] @ headers @
130-
Option.fold ~none:[] ~some:(fun c -> [ "content-length", string_of_int (String.length c)]) content @
130+
[ "host", host ] @ headers @
131+
Option.fold ~none:[ "content-length", "0" ] ~some:(fun c -> [ "content-length", string_of_int (String.length c)]) content @
131132
Option.fold ~none:[] ~some:(fun c -> [ "content-type", c]) content_type in
132133
let req = Request.create ~headers meth path in
133134
let error_handler = error_handler (Lwt.wakeup_later notify) in

0 commit comments

Comments
 (0)