3 OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
5 Copyright (C) <2002-2005> Stefano Zacchiroli <zack@cs.unibo.it>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation, version 2.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 (** send raw data on outchan, flushing it afterwards *)
30 let send_raw ~data outchan =
31 output_string outchan data;
34 let send_CRLF = send_raw ~data:crlf
36 let send_header ~header ~value =
37 Http_parser_sanity.heal_header (header, value);
38 send_raw ~data:(header ^ ": " ^ value ^ crlf)
40 let send_headers ~headers outchan =
41 List.iter (fun (header, value) -> send_header ~header ~value outchan) headers
43 (** internal: low level for send_status_line *)
44 let send_status_line' ~version code =
48 [ string_of_version version;
50 Http_misc.reason_phrase_of_code code ]
52 send_raw ~data:(status_line ^ crlf)
54 let int_of_code = function
56 | `Status status -> code_of_status status
58 let send_status_line ?(version = http_version) ~(code: status_code) outchan =
59 send_status_line' ~version (int_of_code code) outchan
61 (* FIXME duplication of code between this and response#addBasicHeaders *)
62 let send_basic_headers ?(version = http_version) ~(code: status_code) outchan =
63 send_status_line' ~version (int_of_code code) outchan;
65 ~headers:["Date", Http_misc.date_822 (); "Server", server_string]
68 (** internal: given a status code and an additional body return a string
69 representing an HTML document that explains the meaning of given status code.
70 Additional data can be added to the body via 'body' argument *)
71 let foo_body code body =
72 let reason_phrase = Http_misc.reason_phrase_of_code code in
74 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
80 code reason_phrase code reason_phrase body
82 (** internal: send a fooish body explaining in HTML form the 'reason phrase'
83 of an HTTP response; body, if given, will be appended to the body *)
84 let send_foo_body code body = send_raw ~data:(foo_body code body)
86 (* Warning: keep default values in sync with Http_response.response class *)
87 let respond ?(body = "") ?(headers = []) ?version ?(code = `Code 200) outchan =
88 send_basic_headers ?version ~code outchan;
89 send_headers ~headers outchan;
90 send_header "Content-Length" (string_of_int (String.length body)) outchan;
92 send_raw ~data:body outchan
94 (** internal: low level for respond_redirect, respond_error, ...
95 This function send a status line corresponding to a given code, some basic
96 headers, the additional headers (if given) and an HTML page containing the
97 reason phrase; if body is given it will be included in the body of the HTML
99 let send_empty_response
100 func_name ?(is_valid_status = fun _ -> true) ?(headers=[]) ?(body="") () =
101 fun ?version code outchan ->
102 if not (is_valid_status (int_of_code code)) then
104 (sprintf "'%d' isn't a valid status code for %s"
105 (int_of_code code) func_name)
106 else begin (* status code suitable for answering *)
108 [ "Connection", "close";
109 "Content-Type", "text/html; charset=iso-8859-1" ] @ headers
111 let body = (foo_body (int_of_code code) body) ^ body in
112 respond ?version ~code ~headers ~body outchan
116 ~location ?body ?version ?(code = `Code 301) outchan
118 send_empty_response "Daemon.respond_redirect" ~is_valid_status:is_redirection
119 ~headers:["Location", location] ?body () ?version code outchan
121 let respond_error ?body ?version ?(code = `Code 400) outchan =
122 send_empty_response "Daemon.respond_error" ~is_valid_status:is_error
123 ?body () ?version code outchan
125 let respond_not_found ~url ?version outchan =
126 send_empty_response "Daemon.respond_not_found" () ?version (`Code 404) outchan
128 let respond_forbidden ~url ?version outchan =
129 send_empty_response "Daemon.respond_permission_denied" () ?version
132 let respond_unauthorized ?version ?(realm = server_string) outchan =
134 sprintf "401 - Unauthorized - Authentication failed for realm \"%s\"" realm
136 respond ~headers:["WWW-Authenticate", sprintf "Basic realm=\"%s\"" realm]
137 ~code:(`Code 401) ~body outchan
139 let send_file ~src outchan =
141 let buf = String.make buflen ' ' in
143 let (file, cleanup) =
145 | FileSrc fname -> (* if we open the file, we close it before returning *)
146 let f = open_in fname in
147 f, (fun () -> close_in f)
148 | InChanSrc inchan -> inchan, ignore
152 let bytes = input file buf 0 buflen in
156 output outchan buf 0 bytes
165 (* TODO interface is too ugly to advertise this function in .mli *)
166 (** create a minimal HTML directory listing of a given directory and send it
167 over an out_channel, directory is passed as a dir_handle; name is the
168 directory name, used for pretty printing purposes; path is the opened dir
169 path, used to test its contents with stat *)
170 let send_dir_listing ~dir ~name ~path outchan =
171 fprintf outchan "<html>\n<head><title>%s</title></head>\n<body>\n" name;
173 List.partition (fun e -> Http_misc.is_directory (path ^ e)) (Http_misc.ls dir)
176 (fun d -> fprintf outchan "<a href=\"%s/\">%s/</a><br />\n" d d)
177 (List.sort compare dirs);
179 (fun f -> fprintf outchan "<a href=\"%s\">%s</a><br />\n" f f)
180 (List.sort compare files);
181 fprintf outchan "</body>\n</html>";
184 let respond_file ~fname ?(version = http_version) outchan =
185 (** ASSUMPTION: 'fname' doesn't begin with a "/"; it's relative to the current
186 document root (usually the daemon's cwd) *)
187 let droot = Sys.getcwd () in (* document root *)
188 let path = droot ^ "/" ^ fname in (* full path to the desired file *)
189 if not (Sys.file_exists path) then (* file not found *)
190 respond_not_found ~url:fname outchan
193 if Http_misc.is_directory path then begin (* file found, is a dir *)
194 let dir = Unix.opendir path in
195 send_basic_headers ~version ~code:(`Code 200) outchan;
196 send_header "Content-Type" "text/html" outchan;
198 send_dir_listing ~dir ~name:fname ~path outchan;
200 end else begin (* file found, is something else *)
201 let file = open_in fname in
202 send_basic_headers ~version ~code:(`Code 200) outchan;
204 ~header:"Content-Length"
205 ~value:(string_of_int (Http_misc.filesize fname))
208 send_file ~src:(InChanSrc file) outchan;
212 | Unix.Unix_error (Unix.EACCES, _, _)
214 respond_forbidden ~url:fname ~version outchan
217 let respond_with (res: Http_types.response) outchan =
218 res#serialize outchan;
221 (** internal: this exception is raised after a malformed request has been read
222 by a serving process to signal main server (or itself if mode = `Single) to
223 skip to next request *)
227 sprintf "HTTP request parse error: %s" (Printexc.to_string e)
229 (* given a Http_parser.parse_request like function, wrap it in a function that
230 do the same and additionally catch parsing exception sending HTTP error
231 messages back to client as needed. Returned function raises Again when it
232 encounter a parse error (name 'Again' is intended for future versions that
233 will support http keep alive signaling that a new request has to be parsed
235 let rec wrap_parse_request_w_safety parse_function inchan outchan =
238 parse_function inchan
240 | (End_of_file) as e ->
241 debug_print (pp_parse_exc e);
242 respond_error ~code:(`Code 400) ~body:"Unexpected End Of File" outchan;
244 | (Malformed_request req) as e ->
245 debug_print (pp_parse_exc e);
246 respond_error ~code:(`Code 400)
247 ~body:("request 1st line format should be: '<method> <url> <version>'" ^
248 "<br />\nwhile received request 1st line was:<br />\n" ^ req)
251 | (Invalid_HTTP_method meth) as e ->
252 debug_print (pp_parse_exc e);
253 respond_error ~code:(`Code 501)
254 ~body:("Method '" ^ meth ^ "' isn't supported (yet)")
257 | (Malformed_request_URI uri) as e ->
258 debug_print (pp_parse_exc e);
259 respond_error ~code:(`Code 400) ~body:("Malformed URL: '" ^ uri ^ "'")
262 | (Invalid_HTTP_version version) as e ->
263 debug_print (pp_parse_exc e);
264 respond_error ~code:(`Code 505)
265 ~body:("HTTP version '" ^ version ^ "' isn't supported (yet)")
268 | (Malformed_query query) as e ->
269 debug_print (pp_parse_exc e);
270 respond_error ~code:(`Code 400)
271 ~body:(sprintf "Malformed query string '%s'" query) outchan;
273 | (Malformed_query_part (binding, query)) as e ->
274 debug_print (pp_parse_exc e);
275 respond_error ~code:(`Code 400)
276 ~body:(sprintf "Malformed query part '%s' in query '%s'" binding query)
279 (* (* preliminary support for HTTP keep alive connections ... *)
281 wrap_parse_request_w_safety parse_function inchan outchan
284 (* wrapper around Http_parser.parse_request which catch parsing exceptions and
285 return error messages to client as needed
286 @param inchan in_channel from which read incoming requests
287 @param outchan out_channl on which respond with error messages if needed
289 let safe_parse_request = wrap_parse_request_w_safety parse_request
291 (* as above but for OO version (Http_parser.parse_request') *)
292 let safe_parse_request' = wrap_parse_request_w_safety (new Http_request.request)
294 let chdir_to_document_root = function (* chdir to document root *)
295 | Some dir -> Sys.chdir dir
298 let server_of_mode = function
299 | `Single -> Http_tcp_server.simple
300 | `Fork -> Http_tcp_server.fork
301 | `Thread -> Http_tcp_server.thread
303 (* TODO what happens when a Quit exception is raised by a callback? Do other
304 callbacks keep on living until the end or are them all killed immediatly?
305 The right semantics should obviously be the first one *)
307 let handle_manual_auth outchan f =
311 | Unauthorized realm -> respond_unauthorized ~realm outchan
314 let handle_auth req spec outchan =
316 (match (spec.auth, req#authorization) with
317 | None, _ -> spec.callback req outchan (* no auth required *)
318 | Some (realm, `Basic (spec_username, spec_password)),
319 Some (`Basic (username, password))
320 when (username = spec_username) && (password = spec_password) ->
322 spec.callback req outchan
323 | Some (realm, _), _ -> raise (Unauthorized realm)) (* auth failure *)
325 | Unauthorized realm -> respond_unauthorized ~realm outchan
328 (* TODO support also chroot to 'root', not only chdir *)
329 (* TODO deprecated: remove from future versions *)
330 (* curried request *)
332 ?(addr = default_addr) ?(port = default_port)
333 ?(timeout = default_timeout) ?(mode = default_mode) ?root callback
336 "Http_daemon.start is deprecated in favour of Http_daemon.main and will be removed in future versions of the library";
337 chdir_to_document_root root;
338 let sockaddr = Http_misc.build_sockaddr (addr, port) in
339 let daemon_callback inchan outchan =
340 handle_manual_auth outchan (fun () ->
341 let (path, parameters) = safe_parse_request inchan outchan in
342 callback path parameters outchan;
346 (server_of_mode mode) ~sockaddr ~timeout daemon_callback
350 (* TODO deprecated: remove from future versions *)
352 ?(addr = default_addr) ?(port = default_port)
353 ?(timeout = default_timeout) ?(mode = default_mode) ?root callback
356 "Http_daemon.start' is deprecated in favour of Http_daemon.main and will be removed in future versions of the library";
357 chdir_to_document_root root;
358 let sockaddr = Http_misc.build_sockaddr (addr, port) in
359 let daemon_callback inchan outchan =
360 handle_manual_auth outchan (fun () ->
361 let req = safe_parse_request' inchan outchan in
362 callback req outchan;
366 (server_of_mode mode) ~sockaddr ~timeout daemon_callback
370 chdir_to_document_root spec.root_dir;
371 let sockaddr = Http_misc.build_sockaddr (spec.address, spec.port) in
372 let daemon_callback inchan outchan =
374 let req = safe_parse_request' inchan outchan in
375 handle_auth req spec outchan;
378 (match spec.exn_handler with
380 debug_print "uncaught exception: executing handler";
383 debug_print "uncaught exception but no handler given: re-raising";
387 (server_of_mode spec.mode) ~sockaddr ~timeout:spec.timeout daemon_callback
392 let heading_slash_RE = Pcre.regexp "^/"
394 let trivial_callback req outchan =
395 let path = req#path in
396 if not (Pcre.pmatch ~rex:heading_slash_RE path) then
397 respond_error ~code:(`Code 400) outchan
399 respond_file ~fname:(Http_misc.strip_heading_slash path) outchan
401 let callback = trivial_callback
403 let main spec = main { spec with callback = trivial_callback }
406 (* @param inchan input channel connected to client
407 @param outchan output channel connected to client
408 @param sockaddr client socket address *)
409 class connection inchan outchan sockaddr =
410 (* ASSUMPTION: inchan and outchan are channels built on top of the same
411 Unix.file_descr thus closing one of them will close also the other *)
412 let close' o = o#close in
415 initializer Gc.finalise close' self
417 val mutable closed = false
419 method private assertNotClosed =
421 failwith "Http_daemon.connection: connection is closed"
424 self#assertNotClosed;
426 Some (safe_parse_request' inchan outchan)
429 method respond_with res =
430 self#assertNotClosed;
431 respond_with res outchan
434 self#assertNotClosed;
435 close_in inchan; (* this close also outchan *)
440 class daemon ?(addr = "0.0.0.0") ?(port = 80) () =
444 Http_tcp_server.init_socket (Http_misc.build_sockaddr (addr, port))
447 let (cli_suck, cli_sockaddr) = Unix.accept suck in (* may block *)
448 let (inchan, outchan) =
449 (Unix.in_channel_of_descr cli_suck, Unix.out_channel_of_descr cli_suck)
451 new connection inchan outchan cli_sockaddr
454 let conn = self#accept in
455 match conn#getRequest with
459 | Some req -> (req, conn)
466 address = default_addr;
468 callback = default_callback;
471 root_dir = default_root_dir;
472 exn_handler = default_exn_handler;
473 timeout = default_timeout;
477 ?(address = default_addr) ?(auth = default_auth)
478 ?(callback = default_callback) ?(mode = default_mode) ?(port = default_port)
479 ?(root_dir = default_root_dir) ?(exn_handler = default_exn_handler)
480 ?(timeout = default_timeout)
490 exn_handler = exn_handler;