]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/ocaml-http/http_types.ml
added support for "ancient" HTTP requests like "GET /foo"
[helm.git] / helm / DEVEL / ocaml-http / http_types.ml
1
2 (*
3   OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
4
5   Copyright (C) <2002> 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 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.
11
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.
16
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
20 *)
21
22   (** HTTP version, actually only 1.0 and 1.1 are supported. Note that
23   'supported' here means only 'accepted inside a HTTP request line', no
24   different behaviours are actually implemented depending on HTTP version *)
25 type version =
26   [ `HTTP_1_0
27   | `HTTP_1_1
28   ]
29
30   (** HTTP method, actually only GET and POST methods are supported *)
31 type meth =
32   [ `GET
33   | `POST
34   ]
35
36   (** Daemon behaviour wrt request handling. `Single mode use a single process
37   to handle all requests, no request is served until a previous one has been
38   fully served. `Fork mode fork a new process for each request, the new process
39   will execute the callback function and then exit. `Thread mode create a new
40   thread for each request, the new thread will execute the callback function and
41   then exit, threads can communicate using standard OCaml Thread library. *)
42 type daemon_mode = [ `Single | `Fork | `Thread ]
43
44 type tcp_server =
45   sockaddr:Unix.sockaddr -> timeout:int option ->
46   (in_channel -> out_channel -> unit) ->
47     unit
48
49 type informational_substatus =
50   [ `Continue
51   | `Switching_protocols
52   ]
53
54 type success_substatus =
55   [ `OK
56   | `Created
57   | `Accepted
58   | `Non_authoritative_information
59   | `No_content
60   | `Reset_content
61   | `Partial_content
62   ]
63
64 type redirection_substatus =
65   [ `Multiple_choices
66   | `Moved_permanently
67   | `Found
68   | `See_other
69   | `Not_modified
70   | `Use_proxy
71   | `Temporary_redirect
72   ]
73
74 type client_error_substatus =
75   [ `Bad_request
76   | `Unauthorized
77   | `Payment_required
78   | `Forbidden
79   | `Not_found
80   | `Method_not_allowed
81   | `Not_acceptable
82   | `Proxy_authentication_required
83   | `Request_time_out
84   | `Conflict
85   | `Gone
86   | `Length_required
87   | `Precondition_failed
88   | `Request_entity_too_large
89   | `Request_URI_too_large
90   | `Unsupported_media_type
91   | `Requested_range_not_satisfiable
92   | `Expectation_failed
93   ]
94
95 type server_error_substatus =
96   [ `Internal_server_error
97   | `Not_implemented
98   | `Bad_gateway
99   | `Service_unavailable
100   | `Gateway_time_out
101   | `HTTP_version_not_supported
102   ]
103
104 type informational_status = [ `Informational of informational_substatus ]
105 type success_status = [ `Success of success_substatus ]
106 type redirection_status = [ `Redirection of redirection_substatus ]
107 type client_error_status = [ `Client_error of client_error_substatus ]
108 type server_error_status = [ `Server_error of server_error_substatus ]
109
110 type error_status =
111   [ client_error_status
112   | server_error_status
113   ]
114
115 type status =
116   [ informational_status
117   | success_status
118   | redirection_status
119   | client_error_status
120   | server_error_status
121   ]
122
123 exception Invalid_header of string
124 exception Invalid_header_name of string
125 exception Invalid_header_value of string
126 exception Invalid_HTTP_version of string
127 exception Invalid_HTTP_method of string
128 exception Invalid_code of int
129 exception Invalid_status of status
130
131 exception Malformed_URL of string
132 exception Malformed_query of string
133 exception Malformed_query_part of string * string
134 exception Unsupported_method of string
135 exception Unsupported_HTTP_version of string
136 exception Malformed_request_URI of string
137 exception Malformed_request of string
138
139 exception Param_not_found of string
140
141 exception Invalid_status_line of string
142 exception Header_not_found of string
143
144   (** raisable by callback functions to make main daemon quit, this is the only
145   'clean' way to make start functions return *)
146 exception Quit
147
148 class type message = object
149
150     method version: version option
151     method setVersion: version -> unit
152
153     method body: string
154     method setBody: string -> unit
155     method bodyBuf: Buffer.t
156     method setBodyBuf: Buffer.t -> unit
157     method addBody: string -> unit
158     method addBodyBuf: Buffer.t -> unit
159
160     (** header name comparison are performed in a case-insensitive manner as
161     required by RFC2616, actually the implementation works converting all header
162     names in lowercase *)
163
164     method addHeader: name:string -> value:string -> unit
165     method addHeaders: (string * string) list -> unit
166     method replaceHeader: name:string -> value:string -> unit
167     method replaceHeaders: (string * string) list -> unit
168     method removeHeader: name:string -> unit
169     method hasHeader: name:string -> bool
170     method header: name:string -> string
171     method headers: (string * string) list
172
173     method clientSockaddr: Unix.sockaddr
174     method clientAddr: string
175     method clientPort: int
176
177     method serverSockaddr: Unix.sockaddr
178     method serverAddr: string
179     method serverPort: int
180
181     method toString: string
182     method serialize: out_channel -> unit
183
184   end
185
186 class type request = object
187
188     inherit message
189
190     method meth: meth
191     method uri: string
192     method path: string
193     method param: ?meth:meth -> string -> string
194     method paramAll: ?meth:meth -> string -> string list
195     method params: (string * string) list
196     method params_GET: (string * string) list
197     method params_POST: (string * string) list
198
199   end
200
201 class type response = object
202
203     inherit message
204
205     method code: int
206     method setCode: int -> unit
207     method status: status
208     method setStatus: status -> unit
209     method reason: string
210     method setReason: string -> unit
211     method statusLine: string
212     method setStatusLine: string -> unit
213
214     method isInformational: bool
215     method isSuccess: bool
216     method isRedirection: bool
217     method isClientError: bool
218     method isServerError: bool
219     method isError: bool
220
221     method addBasicHeaders: unit
222     method contentType: string
223     method setContentType: string -> unit
224     method contentEncoding: string
225     method setContentEncoding: string -> unit
226     method date: string
227     method setDate: string -> unit
228     method expires: string
229     method setExpires: string -> unit
230     method server: string
231     method setServer: string -> unit
232
233   end
234
235 class type connection =
236   object
237     method getRequest: request option
238     method respond_with: response -> unit
239     method close: unit
240   end
241
242 class type daemon =
243   object
244     method accept: connection
245     method getRequest: request * connection
246   end
247