+ (** internal: this exception is raised after a malformed request has been read
+ by a serving process to signal main server (or itself if mode = `Single) to
+ skip to next request *)
+exception Again;;
+
+let pp_parse_exc e =
+ sprintf "HTTP request parse error: %s" (Printexc.to_string e)
+
+ (* given a Http_parser.parse_request like function, wrap it in a function that
+ do the same and additionally catch parsing exception sending HTTP error
+ messages back to client as needed. Returned function raises Again when it
+ encounter a parse error (name 'Again' is intended for future versions that
+ will support http keep alive signaling that a new request has to be parsed
+ from client) *)
+let rec wrap_parse_request_w_safety parse_function inchan outchan =
+(* try *)
+ (try
+ parse_function inchan
+ with
+ | (End_of_file) as e ->
+ debug_print (pp_parse_exc e);
+ respond_error ~code:(`Code 400) ~body:"Unexpected End Of File" outchan;
+ raise Again
+ | (Malformed_request req) as e ->
+ debug_print (pp_parse_exc e);
+ respond_error ~code:(`Code 400)
+ ~body:("request 1st line format should be: '<method> <url> <version>'" ^
+ "<br />\nwhile received request 1st line was:<br />\n" ^ req)
+ outchan;
+ raise Again
+ | (Invalid_HTTP_method meth) as e ->
+ debug_print (pp_parse_exc e);
+ respond_error ~code:(`Code 501)
+ ~body:("Method '" ^ meth ^ "' isn't supported (yet)")
+ outchan;
+ raise Again
+ | (Malformed_request_URI uri) as e ->
+ debug_print (pp_parse_exc e);
+ respond_error ~code:(`Code 400) ~body:("Malformed URL: '" ^ uri ^ "'")
+ outchan;
+ raise Again
+ | (Invalid_HTTP_version version) as e ->
+ debug_print (pp_parse_exc e);
+ respond_error ~code:(`Code 505)
+ ~body:("HTTP version '" ^ version ^ "' isn't supported (yet)")
+ outchan;
+ raise Again
+ | (Malformed_query query) as e ->
+ debug_print (pp_parse_exc e);
+ respond_error ~code:(`Code 400)
+ ~body:(sprintf "Malformed query string '%s'" query) outchan;
+ raise Again
+ | (Malformed_query_part (binding, query)) as e ->
+ debug_print (pp_parse_exc e);
+ respond_error ~code:(`Code 400)
+ ~body:(sprintf "Malformed query part '%s' in query '%s'" binding query)
+ outchan;
+ raise Again)
+(* (* preliminary support for HTTP keep alive connections ... *)
+ with Again ->
+ wrap_parse_request_w_safety parse_function inchan outchan
+*)
+
+ (* wrapper around Http_parser.parse_request which catch parsing exceptions and
+ return error messages to client as needed
+ @param inchan in_channel from which read incoming requests
+ @param outchan out_channl on which respond with error messages if needed
+ *)
+let safe_parse_request = wrap_parse_request_w_safety parse_request
+
+ (* as above but for OO version (Http_parser.parse_request') *)
+let safe_parse_request' = wrap_parse_request_w_safety (new Http_request.request)
+
+let chdir_to_document_root = function (* chdir to document root *)
+ | Some dir -> Sys.chdir dir
+ | None -> ()
+
+let server_of_mode = function
+ | `Single -> Http_tcp_server.simple
+ | `Fork -> Http_tcp_server.fork
+ | `Thread -> Http_tcp_server.thread
+
+ (* TODO what happens when a Quit exception is raised by a callback? Do other
+ callbacks keep on living until the end or are them all killed immediatly?
+ The right semantics should obviously be the first one *)
+
+ (* TODO support also chroot to 'root', not only chdir *)