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
27 Netdate.mk_mail_date ~zone:Netdate.localzone (Unix.time ())
29 let is_directory name =
30 match Unix.lstat name with
31 | { Unix.st_kind = Unix.S_DIR } -> true
34 let filesize fname = (Unix.stat fname).Unix.st_size
36 let strip_trailing_slash =
37 let rex = Pcre.regexp "/$" in
38 fun s -> Pcre.replace ~rex ~templ:"" s
40 let strip_heading_slash =
41 let rex = Pcre.regexp "^/" in
42 fun s -> Pcre.replace ~rex ~templ:"" s
46 try ls' ((Unix.readdir dir)::entries) with End_of_file -> entries
50 let string_explode s =
51 let rec string_explode' acc = function
53 | s -> string_explode' (s.[0] :: acc) (String.sub s 1 (String.length s - 1))
55 List.rev (string_explode' [] s)
57 let string_implode = List.fold_left (fun s c -> s ^ (String.make 1 c)) ""
59 let reason_phrase_of_code = function
61 | 101 -> "Switching protocols"
65 | 203 -> "Non authoritative information"
67 | 205 -> "Reset content"
68 | 206 -> "Partial content"
69 | 300 -> "Multiple choices"
70 | 301 -> "Moved permanently"
73 | 304 -> "Not modified"
75 | 307 -> "Temporary redirect"
76 | 400 -> "Bad request"
77 | 401 -> "Unauthorized"
78 | 402 -> "Payment required"
81 | 405 -> "Method not allowed"
82 | 406 -> "Not acceptable"
83 | 407 -> "Proxy authentication required"
84 | 408 -> "Request time out"
87 | 411 -> "Length required"
88 | 412 -> "Precondition failed"
89 | 413 -> "Request entity too large"
90 | 414 -> "Request URI too large"
91 | 415 -> "Unsupported media type"
92 | 416 -> "Requested range not satisfiable"
93 | 417 -> "Expectation failed"
94 | 500 -> "Internal server error"
95 | 501 -> "Not implemented"
96 | 502 -> "Bad gateway"
97 | 503 -> "Service unavailable"
98 | 504 -> "Gateway time out"
99 | 505 -> "HTTP version not supported"
100 | invalid_code -> raise (Invalid_code invalid_code)
102 let build_sockaddr (addr, port) =
104 Unix.ADDR_INET ((Unix.gethostbyname addr).Unix.h_addr_list.(0), port)
105 with Not_found -> failwith ("OCaml-HTTP, can't resolve hostname: " ^ addr)
107 let explode_sockaddr = function
108 | Unix.ADDR_INET (addr, port) -> (Unix.string_of_inet_addr addr, port)
109 | _ -> assert false (* can explode only inet address *)
111 let peername_of_out_channel outchan =
112 Unix.getpeername (Unix.descr_of_out_channel outchan)
113 let peername_of_in_channel inchan =
114 Unix.getpeername (Unix.descr_of_in_channel inchan)
115 let sockname_of_out_channel outchan =
116 Unix.getsockname (Unix.descr_of_out_channel outchan)
117 let sockname_of_in_channel inchan =
118 Unix.getsockname (Unix.descr_of_in_channel inchan)
120 let buf_of_inchan ?limit ic =
121 let buf = Buffer.create 10240 in
122 let tmp = String.make 1024 '\000' in
123 let rec buf_of_inchan' limit =
126 let bytes = input ic tmp 0 1024 in
127 if bytes > 0 then begin
128 Buffer.add_substring buf tmp 0 bytes;
131 | Some lim -> (* TODO what about using a single really_input call? *)
132 let bytes = input ic tmp 0 (min lim 1024) in
133 if bytes > 0 then begin
134 Buffer.add_substring buf tmp 0 bytes;
135 buf_of_inchan' (Some (lim - bytes))
138 (try buf_of_inchan' limit with End_of_file -> ());
141 let list_assoc_all key pairs =
142 snd (List.split (List.filter (fun (k, v) -> k = key) pairs))
144 let warn msg = prerr_endline (sprintf "ocaml-http WARNING: %s" msg)
145 let error msg = prerr_endline (sprintf "ocaml-http ERROR: %s" msg)
147 let finally at_end f arg =
150 with exn -> at_end (); raise exn