out_channel ->
unit
+ (** respond with a 401 (Unauthorized) response asking for authentication
+ * against given realm (default is the server name) *)
+val respond_unauthorized:
+ ?version: Http_types.version -> ?realm:string -> out_channel -> unit
+
(** send an "error" response (i.e. 400 <= status < 600), optional body
argument as per send_redirect, default response status is 400 (bad request),
only error status are accepted by this function, other values will
(String.concat ";"
(List.map (fun (h,v) -> String.concat "=" [h;v]) params)))
+let auth_sep_RE = Pcre.regexp ":"
+let basic_auth_RE = Pcre.regexp "^Basic\\s+"
+
exception Fallback;; (* used internally by request class *)
class request ic =
sprintf "%s %s %s" method_string self#uri (string_of_version version)
| None -> sprintf "%s %s" method_string self#uri
+ method authorization: auth_info option =
+ try
+ let credentials =
+ Netencoding.Base64.decode
+ (Pcre.replace ~rex:basic_auth_RE (self#header "authorization"))
+ in
+ debug_print ("HTTP Basic auth credentials: " ^ credentials);
+ (match Pcre.split ~rex:auth_sep_RE credentials with
+ | [username; password] -> Some (`Basic (username, password))
+ | l -> raise Exit)
+ with Header_not_found _ | Invalid_argument _ | Exit -> None
+
end
(in_channel -> out_channel -> unit) ->
unit
+ (** authentication information *)
+type auth_info =
+ [ `Basic of string * string (* username, password *)
+(* | `Digest of ... (* TODO digest authentication *) *)
+ ]
+
(** informational HTTP status, see RFC2616 *)
type informational_substatus =
[ `Continue
(** an header you were looking for was not found *)
exception Header_not_found of string
- (** raisable by callback functions to make main daemon quit, this is the only
+ (** raisable by callbacks to make main daemon quit, this is the only
'clean' way to make start functions return *)
exception Quit
+ (** raisable by callbacks to force a 401 (unauthorized) HTTP answer.
+ * This exception should be raised _before_ sending any data over given out
+ * channel.
+ * @param realm authentication realm (usually needed to prompt user) *)
+exception Unauthorized of string
+
(** {2 OO representation of HTTP messages} *)
(** HTTP generic messages. See {! Http_message.message} *)
(** @return the list of all parameter received via POST *)
method params_POST: (string * string) list
+ (** @return authorization information, if given by the client *)
+ method authorization: auth_info option
+
end
(** HTTP responses *)