File: http_common.ml

package info (click to toggle)
ocaml-http 0.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 376 kB
  • sloc: ml: 2,112; makefile: 190
file content (194 lines) | stat: -rw-r--r-- 6,372 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

(*
  OCaml HTTP - do it yourself (fully OCaml) HTTP daemon

  Copyright (C) <2002-2005> Stefano Zacchiroli <zack@cs.unibo.it>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as
  published by the Free Software Foundation, version 2.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  USA
*)

open Http_types;;
open Printf;;

let debug = ref false
let debug_print s =
  if !debug then
    prerr_endline (sprintf "[OCaml HTTP] DEBUG: %s" s)

let http_version = Http_constants.version
let server_string = Http_constants.server_string

let string_of_version = function
  | `HTTP_1_0 -> "HTTP/1.0"
  | `HTTP_1_1 -> "HTTP/1.1"

let version_of_string = function
  | "HTTP/1.0" -> `HTTP_1_0
  | "HTTP/1.1" -> `HTTP_1_1
  | invalid_version -> raise (Invalid_HTTP_version invalid_version)

let string_of_method = function
  | `GET -> "GET"
  | `POST -> "POST"
  | `HEAD -> "HEAD"
  | `PUT -> "PUT"
  | `DELETE -> "DELETE"
  | `OPTIONS -> "OPTIONS"
  | `TRACE -> "TRACE"

let method_of_string = function
  | "GET" -> `GET
  | "POST" -> `POST
  | "HEAD" -> `HEAD
  | "PUT" -> `PUT
  | "DELETE" -> `DELETE
  | "OPTIONS" -> `OPTIONS
  | "TRACE" -> `TRACE
  | invalid_method -> raise (Invalid_HTTP_method invalid_method)

let string_of_request req = 
  let buffer = Buffer.create 1024 in
  Buffer.add_string buffer (string_of_method req#meth);
  Buffer.add_char buffer ' ';
  Buffer.add_string buffer req#uri;
  Buffer.add_char buffer ' ';
  (match req#version with
  | Some v -> Buffer.add_string buffer (string_of_version v)
  | None -> ());
  Buffer.add_string buffer "\r\n";
  List.iter
    (fun (param_name, param_value) ->
      Buffer.add_string buffer param_name;
      Buffer.add_string buffer ": ";
      Buffer.add_string buffer param_value;
      Buffer.add_string buffer "\r\n";
    )
    req#headers;
  Buffer.add_string buffer "\r\n";
  Buffer.add_string buffer req#body;
  Buffer.contents buffer

let status_of_code = function
  | 100 -> `Informational `Continue
  | 101 -> `Informational `Switching_protocols
  | 200 -> `Success `OK
  | 201 -> `Success `Created
  | 202 -> `Success `Accepted
  | 203 -> `Success `Non_authoritative_information
  | 204 -> `Success `No_content
  | 205 -> `Success `Reset_content
  | 206 -> `Success `Partial_content
  | 300 -> `Redirection `Multiple_choices
  | 301 -> `Redirection `Moved_permanently
  | 302 -> `Redirection `Found
  | 303 -> `Redirection `See_other
  | 304 -> `Redirection `Not_modified
  | 305 -> `Redirection `Use_proxy
  | 307 -> `Redirection `Temporary_redirect
  | 400 -> `Client_error `Bad_request
  | 401 -> `Client_error `Unauthorized
  | 402 -> `Client_error `Payment_required
  | 403 -> `Client_error `Forbidden
  | 404 -> `Client_error `Not_found
  | 405 -> `Client_error `Method_not_allowed
  | 406 -> `Client_error `Not_acceptable
  | 407 -> `Client_error `Proxy_authentication_required
  | 408 -> `Client_error `Request_time_out
  | 409 -> `Client_error `Conflict
  | 410 -> `Client_error `Gone
  | 411 -> `Client_error `Length_required
  | 412 -> `Client_error `Precondition_failed
  | 413 -> `Client_error `Request_entity_too_large
  | 414 -> `Client_error `Request_URI_too_large
  | 415 -> `Client_error `Unsupported_media_type
  | 416 -> `Client_error `Requested_range_not_satisfiable
  | 417 -> `Client_error `Expectation_failed
  | 500 -> `Server_error `Internal_server_error
  | 501 -> `Server_error `Not_implemented
  | 502 -> `Server_error `Bad_gateway
  | 503 -> `Server_error `Service_unavailable
  | 504 -> `Server_error `Gateway_time_out
  | 505 -> `Server_error `HTTP_version_not_supported
  | invalid_code -> raise (Invalid_code invalid_code)

let code_of_status = function
  | `Informational `Continue -> 100
  | `Informational `Switching_protocols -> 101
  | `Success `OK -> 200
  | `Success `Created -> 201
  | `Success `Accepted -> 202
  | `Success `Non_authoritative_information -> 203
  | `Success `No_content -> 204
  | `Success `Reset_content -> 205
  | `Success `Partial_content -> 206
  | `Redirection `Multiple_choices -> 300
  | `Redirection `Moved_permanently -> 301
  | `Redirection `Found -> 302
  | `Redirection `See_other -> 303
  | `Redirection `Not_modified -> 304
  | `Redirection `Use_proxy -> 305
  | `Redirection `Temporary_redirect -> 307
  | `Client_error `Bad_request -> 400
  | `Client_error `Unauthorized -> 401
  | `Client_error `Payment_required -> 402
  | `Client_error `Forbidden -> 403
  | `Client_error `Not_found -> 404
  | `Client_error `Method_not_allowed -> 405
  | `Client_error `Not_acceptable -> 406
  | `Client_error `Proxy_authentication_required -> 407
  | `Client_error `Request_time_out -> 408
  | `Client_error `Conflict -> 409
  | `Client_error `Gone -> 410
  | `Client_error `Length_required -> 411
  | `Client_error `Precondition_failed -> 412
  | `Client_error `Request_entity_too_large -> 413
  | `Client_error `Request_URI_too_large -> 414
  | `Client_error `Unsupported_media_type -> 415
  | `Client_error `Requested_range_not_satisfiable -> 416
  | `Client_error `Expectation_failed -> 417
  | `Server_error `Internal_server_error -> 500
  | `Server_error `Not_implemented -> 501
  | `Server_error `Bad_gateway -> 502
  | `Server_error `Service_unavailable -> 503
  | `Server_error `Gateway_time_out -> 504
  | `Server_error `HTTP_version_not_supported -> 505

let is_informational code =
  match status_of_code code with
  | `Informational _ -> true
  | _ -> false

let is_success code =
  match status_of_code code with
  | `Success _ -> true
  | _ -> false

let is_redirection code =
  match status_of_code code with
  | `Redirection _ -> true
  | _ -> false

let is_client_error code =
  match status_of_code code with
  | `Client_error _ -> true
  | _ -> false

let is_server_error code =
  match status_of_code code with
  | `Server_error _ -> true
  | _ -> false

let is_error code = is_client_error code || is_server_error code