Skip to content

Commit 6dd7a48

Browse files
authored
Merge pull request #5775 from last-genius/private/asultanov/uri-improvement
Remove parse_uri, switch to using Uri module instead
2 parents e61e0ac + e53ce67 commit 6dd7a48

File tree

3 files changed

+10
-52
lines changed

3 files changed

+10
-52
lines changed

ocaml/libs/http-lib/http.ml

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,6 @@ let parse_keyvalpairs xs =
208208
)
209209
kvpairs
210210

211-
let parse_uri x =
212-
match Astring.String.cuts ~sep:"?" x with
213-
| [uri] ->
214-
(uri, [])
215-
| [uri; params] ->
216-
(uri, parse_keyvalpairs params)
217-
| _ ->
218-
raise Http_parse_failure
219-
220211
type authorization = Basic of string * string | UnknownAuth of string
221212
[@@deriving rpc]
222213

@@ -629,42 +620,6 @@ module Request = struct
629620

630621
let get_version x = x.version
631622

632-
let of_request_line x =
633-
match Astring.String.fields ~empty:false x with
634-
| [m; uri; version] -> (
635-
(* Request-Line = Method SP Request-URI SP HTTP-Version CRLF *)
636-
let uri, query = parse_uri uri in
637-
(* strip the "HTTP/" prefix from the version string *)
638-
match Astring.String.cut ~sep:"/" version with
639-
| Some (_, version) ->
640-
{
641-
m= method_t_of_string m
642-
; frame= false
643-
; uri
644-
; query
645-
; content_length= None
646-
; transfer_encoding= None
647-
; accept= None
648-
; version
649-
; cookie= []
650-
; auth= None
651-
; task= None
652-
; subtask_of= None
653-
; content_type= None
654-
; host= None
655-
; user_agent= None
656-
; close= false
657-
; additional_headers= []
658-
; body= None
659-
; traceparent= None
660-
}
661-
| None ->
662-
error "Failed to parse: %s" x ;
663-
raise Http_parse_failure
664-
)
665-
| _ ->
666-
raise Http_parse_failure
667-
668623
let to_string x =
669624
let kvpairs x =
670625
String.concat "; " (List.map (fun (k, v) -> k ^ "=" ^ v) x)

ocaml/libs/http-lib/http.mli

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ module Request : sig
119119
val get_version : t -> string
120120
(** [get_version t] returns the HTTP protocol version *)
121121

122-
val of_request_line : string -> t
123-
(** [of_request_line l] parses [l] of the form "METHOD HTTP/VERSION" and
124-
returns the corresponding [t] *)
125-
126122
val to_string : t -> string
127123
(** [to_string t] returns a short string summarising [t] *)
128124

@@ -176,8 +172,6 @@ end
176172

177173
val authorization_of_string : string -> authorization
178174

179-
val parse_uri : string -> string * (string * string) list
180-
181175
val http_403_forbidden : ?version:string -> unit -> string list
182176

183177
val http_200_ok : ?version:string -> ?keep_alive:bool -> unit -> string list

ocaml/libs/http-lib/http_svr.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ let request_of_bio_exn ~proxy_seen ~read_timeout ~total_timeout ~max_length bio
359359
proxy |> Option.fold ~none:[] ~some:(fun p -> [("STUNNEL_PROXY", p)])
360360
in
361361
let open Http.Request in
362+
(* Below transformation only keeps one value per key, whereas
363+
a fully compliant implementation following Uri's interface
364+
would operate on list of values for each key instead *)
365+
let kvlist_flatten ls =
366+
List.map (function k, v :: _ -> (k, v) | k, [] -> (k, "")) ls
367+
in
362368
let request =
363369
Astring.String.cuts ~sep:"\n" headers
364370
|> List.fold_left
@@ -367,7 +373,10 @@ let request_of_bio_exn ~proxy_seen ~read_timeout ~total_timeout ~max_length bio
367373
match Astring.String.fields ~empty:false header with
368374
| [meth; uri; version] ->
369375
(* Request-Line = Method SP Request-URI SP HTTP-Version CRLF *)
370-
let uri, query = Http.parse_uri uri in
376+
let uri_t = Uri.of_string uri in
377+
if uri_t = Uri.empty then raise Http_parse_failure ;
378+
let uri = Uri.path uri_t |> Uri.pct_decode in
379+
let query = Uri.query uri_t |> kvlist_flatten in
371380
let m = Http.method_t_of_string meth in
372381
let version =
373382
let x = String.trim version in

0 commit comments

Comments
 (0)