Skip to content

Commit

Permalink
Factor out Http auth parse logic, test it. (#4211)
Browse files Browse the repository at this point in the history
  • Loading branch information
toots authored Nov 18, 2024
1 parent 34f50be commit 44eb58d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
19 changes: 7 additions & 12 deletions src/core/harbor/harbor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,9 @@ module Make (T : Transport_t) : T with type socket = T.socket = struct
let websocket_error n msg = Websocket.to_string (`Close (Some (n, msg)))

let parse_icy_request_line ~port h r =
let auth_data = Re.Pcre.split ~rex:(Re.Pcre.regexp ":") r in
let requested_user, password =
match auth_data with
| user :: password :: _ -> (user, password)
| _ -> ("", r)
let { Liq_http.user = requested_user; password } =
try Liq_http.parse_auth r
with Not_found -> { Liq_http.user = ""; password = r }
in
let* s =
try Duppy.Monad.return (find_source "/" (port - 1))
Expand Down Expand Up @@ -451,14 +449,11 @@ module Make (T : Transport_t) : T with type socket = T.socket = struct
let auth = assoc_uppercase "AUTHORIZATION" headers in
let data = Re.Pcre.split ~rex:(Re.Pcre.regexp "[ \t]+") auth in
match data with
| "Basic" :: x :: _ -> (
let auth_data =
Re.Pcre.split ~rex:(Re.Pcre.regexp ":")
(Lang_string.decode64 x)
| "Basic" :: x :: _ ->
let { Liq_http.user; password } =
Liq_http.parse_auth (Lang_string.decode64 x)
in
match auth_data with
| x :: y :: _ -> (x, y)
| _ -> raise Not_found)
(user, password)
| _ -> raise Not_found
with Not_found -> (
match query with
Expand Down
8 changes: 8 additions & 0 deletions src/core/tools/liq_http.ml
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,11 @@ let set_socket_default ~read_timeout ~write_timeout fd =
Unix.set_close_on_exec fd;
Unix.setsockopt_float fd Unix.SO_RCVTIMEO read_timeout;
Unix.setsockopt_float fd Unix.SO_SNDTIMEO write_timeout
type auth = { user : string; password : string }
let parse_auth s =
match Re.Pcre.split ~rex:(Re.Pcre.regexp ":") s with
| user :: (_ :: _ as password) ->
{ user; password = String.concat ":" password }
| _ -> raise Not_found
7 changes: 6 additions & 1 deletion src/core/tools/liq_http.mli
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,10 @@ val read : timeout:float -> socket -> int -> string
(** Read [len] bytes *)
val really_read : timeout:float -> socket -> int -> string

(* Read chunked data. *)
(** Read chunked data. *)
val read_chunked : timeout:float -> socket -> string * int

type auth = { user : string; password : string }

(** Split authentication string. Raises [Not_found] if failed. *)
val parse_auth : string -> auth
14 changes: 14 additions & 0 deletions tests/core/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@
(action (run %{generator_test} )))


(executable
(name http_test)
(modules http_test)
(libraries liquidsoap_core liquidsoap_optionals))

(rule
(alias citest)
(package liquidsoap)
(deps

(:http_test http_test.exe))
(action (run %{http_test} )))


(executable
(name is_url)
(modules is_url)
Expand Down
4 changes: 4 additions & 0 deletions tests/core/http_test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let () =
let { Liq_http.user; password } = Liq_http.parse_auth "foo:bar:gni" in
assert (user = "foo");
assert (password = "bar:gni")

0 comments on commit 44eb58d

Please sign in to comment.