]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/ocaml-http/http_types.ml
Initial revision
[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_query of string
132 exception Malformed_query_part of string * string
133 exception Unsupported_method of string
134 exception Unsupported_HTTP_version of string
135 exception Malformed_request_URI of string
136 exception Malformed_request of string
137
138 exception Param_not_found of string
139
140 exception Invalid_status_line of string
141 exception Header_not_found of string
142
143   (** raisable by callback functions to make main daemon quit, this is the only
144   'clean' way to make start functions return *)
145 exception Quit
146
147 class type message = object
148
149     method version: version
150     method setVersion: version -> unit
151
152     method body: string
153     method setBody: string -> unit
154     method bodyBuf: Buffer.t
155     method setBodyBuf: Buffer.t -> unit
156     method addBody: string -> unit
157     method addBodyBuf: Buffer.t -> unit
158
159     (** header name comparison are performed in a case-insensitive manner as
160     required by RFC2616, actually the implementation works converting all header
161     names in lowercase *)
162
163     method addHeader: name:string -> value:string -> unit
164     method addHeaders: (string * string) list -> unit
165     method replaceHeader: name:string -> value:string -> unit
166     method replaceHeaders: (string * string) list -> unit
167     method removeHeader: name:string -> unit
168     method hasHeader: name:string -> bool
169     method header: name:string -> string
170     method headers: (string * string) list
171
172     method clientSockaddr: Unix.sockaddr
173     method clientAddr: string
174     method clientPort: int
175
176     method serverSockaddr: Unix.sockaddr
177     method serverAddr: string
178     method serverPort: int
179
180     method toString: string
181     method serialize: out_channel -> unit
182
183   end
184
185 class type request = object
186
187     inherit message
188
189     method meth: meth
190     method uri: string
191     method path: string
192     method param: ?meth:meth -> string -> string
193     method paramAll: ?meth:meth -> string -> string list
194     method params: (string * string) list
195     method params_GET: (string * string) list
196     method params_POST: (string * string) list
197
198   end
199
200 class type response = object
201
202     inherit message
203
204     method code: int
205     method setCode: int -> unit
206     method status: status
207     method setStatus: status -> unit
208     method reason: string
209     method setReason: string -> unit
210     method statusLine: string
211     method setStatusLine: string -> unit
212
213     method isInformational: bool
214     method isSuccess: bool
215     method isRedirection: bool
216     method isClientError: bool
217     method isServerError: bool
218     method isError: bool
219
220     method addBasicHeaders: unit
221     method contentType: string
222     method setContentType: string -> unit
223     method contentEncoding: string
224     method setContentEncoding: string -> unit
225     method date: string
226     method setDate: string -> unit
227     method expires: string
228     method setExpires: string -> unit
229     method server: string
230     method setServer: string -> unit
231
232   end
233
234 class type connection =
235   object
236     method getRequest: request option
237     method respond_with: response -> unit
238     method close: unit
239   end
240
241 class type daemon =
242   object
243     method accept: connection
244     method getRequest: request * connection
245   end
246