]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/ocaml-http/http_misc.ml
ocaml 3.09 transition
[helm.git] / helm / DEVEL / ocaml-http / http_misc.ml
1
2 (*
3   OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
4
5   Copyright (C) <2002-2005> Stefano Zacchiroli <zack@cs.unibo.it>
6
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.
10
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.
15
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
19   USA
20 *)
21
22 open Printf
23
24 open Http_types
25
26 let date_822 () =
27   Netdate.mk_mail_date ~zone:Netdate.localzone (Unix.time ())
28
29 let is_directory name =
30   match Unix.lstat name with
31   | { Unix.st_kind = Unix.S_DIR } -> true
32   | _ -> false
33
34 let filesize fname = (Unix.stat fname).Unix.st_size
35
36 let strip_trailing_slash =
37   let rex = Pcre.regexp "/$" in
38   fun s -> Pcre.replace ~rex ~templ:"" s
39
40 let strip_heading_slash =
41   let rex = Pcre.regexp "^/" in
42   fun s -> Pcre.replace ~rex ~templ:"" s
43
44 let ls dir =
45   let rec ls' entries =
46     try ls' ((Unix.readdir dir)::entries) with End_of_file -> entries
47   in
48   ls' []
49
50 let string_explode s =
51   let rec string_explode' acc = function
52     | "" -> acc
53     | s -> string_explode' (s.[0] :: acc) (String.sub s 1 (String.length s - 1))
54   in
55   List.rev (string_explode' [] s)
56
57 let string_implode = List.fold_left (fun s c -> s ^ (String.make 1 c)) ""
58
59 let reason_phrase_of_code = function
60   | 100 -> "Continue"
61   | 101 -> "Switching protocols"
62   | 200 -> "OK"
63   | 201 -> "Created"
64   | 202 -> "Accepted"
65   | 203 -> "Non authoritative information"
66   | 204 -> "No content"
67   | 205 -> "Reset content"
68   | 206 -> "Partial content"
69   | 300 -> "Multiple choices"
70   | 301 -> "Moved permanently"
71   | 302 -> "Found"
72   | 303 -> "See other"
73   | 304 -> "Not modified"
74   | 305 -> "Use proxy"
75   | 307 -> "Temporary redirect"
76   | 400 -> "Bad request"
77   | 401 -> "Unauthorized"
78   | 402 -> "Payment required"
79   | 403 -> "Forbidden"
80   | 404 -> "Not found"
81   | 405 -> "Method not allowed"
82   | 406 -> "Not acceptable"
83   | 407 -> "Proxy authentication required"
84   | 408 -> "Request time out"
85   | 409 -> "Conflict"
86   | 410 -> "Gone"
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)
101
102 let build_sockaddr (addr, port) =
103   try
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)
106
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 *)
110
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)
119
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 =
124     (match limit with
125     | None ->
126         let bytes = input ic tmp 0 1024 in
127         if bytes > 0 then begin
128           Buffer.add_substring buf tmp 0 bytes;
129           buf_of_inchan' None
130         end
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))
136         end)
137   in
138   (try buf_of_inchan' limit with End_of_file -> ());
139   buf
140
141 let list_assoc_all key pairs =
142   snd (List.split (List.filter (fun (k, v) -> k = key) pairs))
143
144 let warn msg  = prerr_endline (sprintf "ocaml-http WARNING: %s" msg)
145 let error msg = prerr_endline (sprintf "ocaml-http ERROR:   %s" msg)
146