3 OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
5 Copyright (C) <2002-2004> 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 General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 Netdate.mk_mail_date ~zone:Netdate.localzone (Unix.time ())
27 let is_directory name =
28 match Unix.lstat name with
29 | { Unix.st_kind = Unix.S_DIR } -> true
32 let filesize fname = (Unix.stat fname).Unix.st_size
34 let strip_trailing_slash =
35 let rex = Pcre.regexp "/$" in
36 fun s -> Pcre.replace ~rex ~templ:"" s
38 let strip_heading_slash =
39 let rex = Pcre.regexp "^/" in
40 fun s -> Pcre.replace ~rex ~templ:"" s
44 try ls' ((Unix.readdir dir)::entries) with End_of_file -> entries
48 let string_explode s =
49 let rec string_explode' acc = function
51 | s -> string_explode' (s.[0] :: acc) (String.sub s 1 (String.length s - 1))
53 List.rev (string_explode' [] s)
55 let string_implode = List.fold_left (fun s c -> s ^ (String.make 1 c)) ""
57 let reason_phrase_of_code = function
59 | 101 -> "Switching protocols"
63 | 203 -> "Non authoritative information"
65 | 205 -> "Reset content"
66 | 206 -> "Partial content"
67 | 300 -> "Multiple choices"
68 | 301 -> "Moved permanently"
71 | 304 -> "Not modified"
73 | 307 -> "Temporary redirect"
74 | 400 -> "Bad request"
75 | 401 -> "Unauthorized"
76 | 402 -> "Payment required"
79 | 405 -> "Method not allowed"
80 | 406 -> "Not acceptable"
81 | 407 -> "Proxy authentication required"
82 | 408 -> "Request time out"
85 | 411 -> "Length required"
86 | 412 -> "Precondition failed"
87 | 413 -> "Request entity too large"
88 | 414 -> "Request URI too large"
89 | 415 -> "Unsupported media type"
90 | 416 -> "Requested range not satisfiable"
91 | 417 -> "Expectation failed"
92 | 500 -> "Internal server error"
93 | 501 -> "Not implemented"
94 | 502 -> "Bad gateway"
95 | 503 -> "Service unavailable"
96 | 504 -> "Gateway time out"
97 | 505 -> "HTTP version not supported"
98 | invalid_code -> raise (Invalid_code invalid_code)
100 let build_sockaddr (addr, port) =
102 Unix.ADDR_INET ((Unix.gethostbyname addr).Unix.h_addr_list.(0), port)
103 with Not_found -> failwith ("OCaml-HTTP, can't resolve hostname: " ^ addr)
105 let explode_sockaddr = function
106 | Unix.ADDR_INET (addr, port) -> (Unix.string_of_inet_addr addr, port)
107 | _ -> assert false (* can explode only inet address *)
109 let peername_of_out_channel outchan =
110 Unix.getpeername (Unix.descr_of_out_channel outchan)
111 let peername_of_in_channel inchan =
112 Unix.getpeername (Unix.descr_of_in_channel inchan)
113 let sockname_of_out_channel outchan =
114 Unix.getsockname (Unix.descr_of_out_channel outchan)
115 let sockname_of_in_channel inchan =
116 Unix.getsockname (Unix.descr_of_in_channel inchan)
118 let buf_of_inchan ?limit ic =
119 let buf = Buffer.create 10240 in
120 let tmp = String.make 1024 '\000' in
121 let rec buf_of_inchan' limit =
124 let bytes = input ic tmp 0 1024 in
125 if bytes > 0 then begin
126 Buffer.add_substring buf tmp 0 bytes;
129 | Some lim -> (* TODO what about using a single really_input call? *)
130 let bytes = input ic tmp 0 (min lim 1024) in
131 if bytes > 0 then begin
132 Buffer.add_substring buf tmp 0 bytes;
133 buf_of_inchan' (Some (lim - bytes))
136 (try buf_of_inchan' limit with End_of_file -> ());
139 let list_assoc_all key pairs =
140 snd (List.split (List.filter (fun (k, v) -> k = key) pairs))