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
22 exception Invalid_HTTP_version of string
23 exception Invalid_code of int
24 exception Invalid_status of Http_types.status
26 let http_version = `HTTP_1_1
28 let string_of_version = function
29 | `HTTP_1_0 -> "HTTP/1.0"
30 | `HTTP_1_1 -> "HTTP/1.1"
32 let version_of_string = function
33 | "HTTP/1.0" -> `HTTP_1_0
34 | "HTTP/1.1" -> `HTTP_1_1
35 | invalid_version -> raise (Invalid_HTTP_version invalid_version)
37 let status_of_code = function
38 | 100 -> `Informational `Continue
39 | 101 -> `Informational `Switching_protocols
41 | 201 -> `Success `Created
42 | 202 -> `Success `Accepted
43 | 203 -> `Success `Non_authoritative_information
44 | 204 -> `Success `No_content
45 | 205 -> `Success `Reset_content
46 | 206 -> `Success `Partial_content
47 | 300 -> `Redirection `Multiple_choices
48 | 301 -> `Redirection `Moved_permanently
49 | 302 -> `Redirection `Found
50 | 303 -> `Redirection `See_other
51 | 304 -> `Redirection `Not_modified
52 | 305 -> `Redirection `Use_proxy
53 | 307 -> `Redirection `Temporary_redirect
54 | 400 -> `Client_error `Bad_request
55 | 401 -> `Client_error `Unauthorized
56 | 402 -> `Client_error `Payment_required
57 | 403 -> `Client_error `Forbidden
58 | 404 -> `Client_error `Not_found
59 | 405 -> `Client_error `Method_not_allowed
60 | 406 -> `Client_error `Not_acceptable
61 | 407 -> `Client_error `Proxy_authentication_required
62 | 408 -> `Client_error `Request_time_out
63 | 409 -> `Client_error `Conflict
64 | 410 -> `Client_error `Gone
65 | 411 -> `Client_error `Length_required
66 | 412 -> `Client_error `Precondition_failed
67 | 413 -> `Client_error `Request_entity_too_large
68 | 414 -> `Client_error `Request_URI_too_large
69 | 415 -> `Client_error `Unsupported_media_type
70 | 416 -> `Client_error `Requested_range_not_satisfiable
71 | 417 -> `Client_error `Expectation_failed
72 | 500 -> `Server_error `Internal_server_error
73 | 501 -> `Server_error `Not_implemented
74 | 502 -> `Server_error `Bad_gateway
75 | 503 -> `Server_error `Service_unavailable
76 | 504 -> `Server_error `Gateway_time_out
77 | 505 -> `Server_error `HTTP_version_not_supported
78 | invalid_code -> raise (Invalid_code invalid_code)
80 let code_of_status = function
81 | `Informational `Continue -> 100
82 | `Informational `Switching_protocols -> 101
84 | `Success `Created -> 201
85 | `Success `Accepted -> 202
86 | `Success `Non_authoritative_information -> 203
87 | `Success `No_content -> 204
88 | `Success `Reset_content -> 205
89 | `Success `Partial_content -> 206
90 | `Redirection `Multiple_choices -> 300
91 | `Redirection `Moved_permanently -> 301
92 | `Redirection `Found -> 302
93 | `Redirection `See_other -> 303
94 | `Redirection `Not_modified -> 304
95 | `Redirection `Use_proxy -> 305
96 | `Redirection `Temporary_redirect -> 307
97 | `Client_error `Bad_request -> 400
98 | `Client_error `Unauthorized -> 401
99 | `Client_error `Payment_required -> 402
100 | `Client_error `Forbidden -> 403
101 | `Client_error `Not_found -> 404
102 | `Client_error `Method_not_allowed -> 405
103 | `Client_error `Not_acceptable -> 406
104 | `Client_error `Proxy_authentication_required -> 407
105 | `Client_error `Request_time_out -> 408
106 | `Client_error `Conflict -> 409
107 | `Client_error `Gone -> 410
108 | `Client_error `Length_required -> 411
109 | `Client_error `Precondition_failed -> 412
110 | `Client_error `Request_entity_too_large -> 413
111 | `Client_error `Request_URI_too_large -> 414
112 | `Client_error `Unsupported_media_type -> 415
113 | `Client_error `Requested_range_not_satisfiable -> 416
114 | `Client_error `Expectation_failed -> 417
115 | `Server_error `Internal_server_error -> 500
116 | `Server_error `Not_implemented -> 501
117 | `Server_error `Bad_gateway -> 502
118 | `Server_error `Service_unavailable -> 503
119 | `Server_error `Gateway_time_out -> 504
120 | `Server_error `HTTP_version_not_supported -> 505
122 let reason_phrase_of_code = function
124 | 101 -> "Switching protocols"
128 | 203 -> "Non authoritative information"
129 | 204 -> "No content"
130 | 205 -> "Reset content"
131 | 206 -> "Partial content"
132 | 300 -> "Multiple choices"
133 | 301 -> "Moved permanently"
136 | 304 -> "Not modified"
138 | 307 -> "Temporary redirect"
139 | 400 -> "Bad request"
140 | 401 -> "Unauthorized"
141 | 402 -> "Payment required"
144 | 405 -> "Method not allowed"
145 | 406 -> "Not acceptable"
146 | 407 -> "Proxy authentication required"
147 | 408 -> "Request time out"
150 | 411 -> "Length required"
151 | 412 -> "Precondition failed"
152 | 413 -> "Request entity too large"
153 | 414 -> "Request URI too large"
154 | 415 -> "Unsupported media type"
155 | 416 -> "Requested range not satisfiable"
156 | 417 -> "Expectation failed"
157 | 500 -> "Internal server error"
158 | 501 -> "Not implemented"
159 | 502 -> "Bad gateway"
160 | 503 -> "Service unavailable"
161 | 504 -> "Gateway time out"
162 | 505 -> "HTTP version not supported"
163 | invalid_code -> raise (Invalid_code invalid_code)
165 let reason_phrase_of_status s = reason_phrase_of_code (code_of_status s)
167 let is_informational code =
168 match status_of_code code with
169 | `Informational _ -> true
172 let is_success code =
173 match status_of_code code with
177 let is_redirection code =
178 match status_of_code code with
179 | `Redirection _ -> true
182 let is_client_error code =
183 match status_of_code code with
184 | `Client_error _ -> true
187 let is_server_error code =
188 match status_of_code code with
189 | `Server_error _ -> true
192 let is_error code = is_client_error code || is_server_error code