3 OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
5 Copyright (C) <2002> 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
28 prerr_endline (sprintf "DEBUG: %s" s)
30 let http_version = Http_constants.version
31 let server_string = Http_constants.server_string
33 let string_of_version = function
34 | `HTTP_1_0 -> "HTTP/1.0"
35 | `HTTP_1_1 -> "HTTP/1.1"
37 let version_of_string = function
38 | "HTTP/1.0" -> `HTTP_1_0
39 | "HTTP/1.1" -> `HTTP_1_1
40 | invalid_version -> raise (Invalid_HTTP_version invalid_version)
42 let status_of_code = function
43 | 100 -> `Informational `Continue
44 | 101 -> `Informational `Switching_protocols
46 | 201 -> `Success `Created
47 | 202 -> `Success `Accepted
48 | 203 -> `Success `Non_authoritative_information
49 | 204 -> `Success `No_content
50 | 205 -> `Success `Reset_content
51 | 206 -> `Success `Partial_content
52 | 300 -> `Redirection `Multiple_choices
53 | 301 -> `Redirection `Moved_permanently
54 | 302 -> `Redirection `Found
55 | 303 -> `Redirection `See_other
56 | 304 -> `Redirection `Not_modified
57 | 305 -> `Redirection `Use_proxy
58 | 307 -> `Redirection `Temporary_redirect
59 | 400 -> `Client_error `Bad_request
60 | 401 -> `Client_error `Unauthorized
61 | 402 -> `Client_error `Payment_required
62 | 403 -> `Client_error `Forbidden
63 | 404 -> `Client_error `Not_found
64 | 405 -> `Client_error `Method_not_allowed
65 | 406 -> `Client_error `Not_acceptable
66 | 407 -> `Client_error `Proxy_authentication_required
67 | 408 -> `Client_error `Request_time_out
68 | 409 -> `Client_error `Conflict
69 | 410 -> `Client_error `Gone
70 | 411 -> `Client_error `Length_required
71 | 412 -> `Client_error `Precondition_failed
72 | 413 -> `Client_error `Request_entity_too_large
73 | 414 -> `Client_error `Request_URI_too_large
74 | 415 -> `Client_error `Unsupported_media_type
75 | 416 -> `Client_error `Requested_range_not_satisfiable
76 | 417 -> `Client_error `Expectation_failed
77 | 500 -> `Server_error `Internal_server_error
78 | 501 -> `Server_error `Not_implemented
79 | 502 -> `Server_error `Bad_gateway
80 | 503 -> `Server_error `Service_unavailable
81 | 504 -> `Server_error `Gateway_time_out
82 | 505 -> `Server_error `HTTP_version_not_supported
83 | invalid_code -> raise (Invalid_code invalid_code)
85 let code_of_status = function
86 | `Informational `Continue -> 100
87 | `Informational `Switching_protocols -> 101
89 | `Success `Created -> 201
90 | `Success `Accepted -> 202
91 | `Success `Non_authoritative_information -> 203
92 | `Success `No_content -> 204
93 | `Success `Reset_content -> 205
94 | `Success `Partial_content -> 206
95 | `Redirection `Multiple_choices -> 300
96 | `Redirection `Moved_permanently -> 301
97 | `Redirection `Found -> 302
98 | `Redirection `See_other -> 303
99 | `Redirection `Not_modified -> 304
100 | `Redirection `Use_proxy -> 305
101 | `Redirection `Temporary_redirect -> 307
102 | `Client_error `Bad_request -> 400
103 | `Client_error `Unauthorized -> 401
104 | `Client_error `Payment_required -> 402
105 | `Client_error `Forbidden -> 403
106 | `Client_error `Not_found -> 404
107 | `Client_error `Method_not_allowed -> 405
108 | `Client_error `Not_acceptable -> 406
109 | `Client_error `Proxy_authentication_required -> 407
110 | `Client_error `Request_time_out -> 408
111 | `Client_error `Conflict -> 409
112 | `Client_error `Gone -> 410
113 | `Client_error `Length_required -> 411
114 | `Client_error `Precondition_failed -> 412
115 | `Client_error `Request_entity_too_large -> 413
116 | `Client_error `Request_URI_too_large -> 414
117 | `Client_error `Unsupported_media_type -> 415
118 | `Client_error `Requested_range_not_satisfiable -> 416
119 | `Client_error `Expectation_failed -> 417
120 | `Server_error `Internal_server_error -> 500
121 | `Server_error `Not_implemented -> 501
122 | `Server_error `Bad_gateway -> 502
123 | `Server_error `Service_unavailable -> 503
124 | `Server_error `Gateway_time_out -> 504
125 | `Server_error `HTTP_version_not_supported -> 505
127 let is_informational code =
128 match status_of_code code with
129 | `Informational _ -> true
132 let is_success code =
133 match status_of_code code with
137 let is_redirection code =
138 match status_of_code code with
139 | `Redirection _ -> true
142 let is_client_error code =
143 match status_of_code code with
144 | `Client_error _ -> true
147 let is_server_error code =
148 match status_of_code code with
149 | `Server_error _ -> true
152 let is_error code = is_client_error code || is_server_error code