Backport from sid to buster
[hcoop/debian/mlton.git] / lib / mlton / basic / http.sig
1 (* Copyright (C) 2009 Matthew Fluet.
2 * Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 *
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
7 *)
8
9 (* Based on RFC 2616. *)
10 signature HTTP =
11 sig
12 structure Method:
13 sig
14 datatype t =
15 Connect
16 | Delete
17 | Get
18 | Head
19 | Options
20 | Post
21 | Put
22 | Trace
23 | Extension of string
24 end
25
26 structure RequestUrl:
27 sig
28 datatype t =
29 Star
30 | Url of Url.t
31 | Path of {path: Url.Path.t,
32 query: string option}
33 | Authority of string
34
35 val toString: t -> string
36 end
37
38 structure Version:
39 sig
40 datatype t = T of {major: int,
41 minor: int}
42
43 val v10: t
44 val v11: t
45 end
46
47 structure Header:
48 sig
49 datatype t =
50 Accept of string
51 | AcceptCharset of string
52 | AcceptEncoding of string
53 | AcceptLanguage of string
54 | AcceptRanges of string
55 | Age of string
56 | Allow of string
57 | Authorization of string
58 | CacheControl of string
59 | Connection of string
60 | ContentEncoding of string
61 | ContentLanguage of string
62 | ContentLength of int
63 | ContentLocation of string
64 | ContentMD5 of string
65 | ContentRange of string
66 | ContentType of string
67 | Cookie of string
68 | Date of string
69 | ETag of string
70 | Expect of string
71 | Expires of string
72 | Extension of {name: string, value: string}
73 | From of string
74 | Host of string
75 | IfMatch of string
76 | LastModified of string
77 | Location of string
78 | Pragma of string
79 | ProxyAuthenticate of string
80 | ProxyConnection of string
81 | Referer of string
82 | RetryAfter of string
83 | Server of string
84 | SetCookie of string
85 | Trailer of string
86 | TransferEncoding of string
87 | Upgrade of string
88 | UserAgent of string
89 | Vary of string
90 | Via of string
91 | WWWAuthenticate of string
92 | Warning of string
93
94 val fromString: string -> t list Result.t
95 val input: In.t -> t list Result.t
96 val toString: t -> string
97 end
98
99 structure Request:
100 sig
101 datatype t = T of {method: Method.t,
102 url: RequestUrl.t,
103 version: Version.t,
104 headers: Header.t list}
105
106 val input: In.t -> t Result.t
107 val layout: t -> Layout.t
108 val output: t * Out.t -> unit
109 val regexp: unit -> Regexp.Compiled.t
110 val requestLine: string -> {method: Method.t,
111 url: RequestUrl.t,
112 version: Version.t} option
113 val toString: t -> string
114 end
115
116 structure Status:
117 sig
118 datatype t =
119 Accepted
120 | BadGateway
121 | BadRequest
122 | Conflict
123 | Continue
124 | Created
125 | ExpectationFailed
126 | Extension of string
127 | Forbidden
128 | Found
129 | GatewayTimeout
130 | Gone
131 | HTTPVersionNotSupported
132 | InternalServerError
133 | LengthRequired
134 | MethodNotAllowed
135 | MovedPermanently
136 | MultipleChoices
137 | NoContent
138 | NonAuthoritativeInformation
139 | NotAcceptable
140 | NotFound
141 | NotImplemented
142 | NotModified
143 | OK
144 | PartialContent
145 | PaymentRequired
146 | PreconditionFailed
147 | ProxyAuthenticationRequired
148 | RequestEntityTooLarge
149 | RequestTimeout
150 | RequestUriTooLarge
151 | RequestedRangeNotSatisfiable
152 | ResetContent
153 | SeeOther
154 | ServiceUnavailable
155 | SwitchingProtocols
156 | TemporaryRedirect
157 | Unauthorized
158 | UnsupportedMediaType
159 | UseProxy
160
161 val code: t -> string
162 val fromString: string -> t (* string is a code, eg "502" *)
163 val reason: t -> string
164 end
165
166 structure Response:
167 sig
168 datatype t = T of {headers: Header.t list,
169 status: Status.t,
170 version: Version.t}
171
172 val input: In.t -> t Result.t
173 val layout: t -> Layout.t
174 val output: t * Out.t -> unit
175 val regexp: unit -> Regexp.Compiled.t
176 val toString: t -> string
177 end
178
179 structure Post:
180 sig
181 structure Encoding:
182 sig
183 datatype t = Url | Multipart
184 end
185
186 structure Value:
187 sig
188 type t
189
190 val file: File.t -> t
191 val string: string -> t
192 end
193
194 datatype t =
195 T of {encoding: Encoding.t,
196 fields: {name: string,
197 value: Value.t} list}
198 end
199
200 val fetch:
201 {head: bool,
202 headers: Header.t list,
203 post: Post.t option,
204 proxy: {host: string, port: int} option,
205 url: Url.t} -> In.t
206 end
207
208
209 functor TestHttp (S: HTTP): sig end =
210 struct
211
212 open S
213
214 val _ =
215 Assert.assert
216 ("TestHttp", fn () =>
217 Regexp.Compiled.matchesAll (Request.regexp (),
218 "CONNECT trading.etrade.com:443 HTTP/1.0\r\n")
219 andalso
220 isSome (Request.requestLine "GET http://Norma140.emp3.com/ HTTP/1.0\n")
221 andalso
222 let
223 val s =
224 "Date: Wed, 08 Mar 2000 09:26:18 GMT\r\n\
225 \Server: Apache/1.3.6 (Unix) (Red Hat/Linux)\r\n\
226 \Last-Modified: Thu, 02 Mar 2000 22:55:44 GMT\r\n\
227 \ETag: \"23a07c-2ae-38bef170\"\r\n\
228 \Accept-Ranges: bytes\r\n\
229 \Content-Length: 686\r\n\
230 \Connection: close\r\n\
231 \Content-Type: text/html\r\n"
232 val zzz = "GET http://www.nytimes.com/auth/chk_login?is_continue=true&URI=http%3A%2F%2Fwww.nytimes.com%2Flibrary%2Ftech%2Fyr%2Fmo%2Fbiztech%2Farticles%2F17blue.html&Tag=&site=&banner=&sweeps=&USERID=cypherpunk&PASSWORD=cypherpunk&SAVEOPTION=YES HTTP/1.0\r\n"
233 val s =
234 "Referer: http://www.nytimes.com/auth/chk_login?is_continue=true&URI=http%3A%2F%2Fwww.nytimes.com%2Flibrary%2Ftech%2Fyr%2Fmo%2Fbiztech%2Farticles%2F17blue.html&Tag=&site=&banner=&sweeps=&USERID=hqbovik&PASSWORD=hqbovik&SAVEOPTION=YES\r\nUser-Agent: Mozilla/4.7 [en]\r\nHost: www.nytimes.com\r\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*\r\nAccept-Encoding: gzip\r\nAccept-Language: en\r\nAccept-Charset: iso-8859-1,*,utf-8\r\nCookie: RMID=c603a30338b9ce60; NYT-S=0UtWyAdJ/Hc94BS7pHO0q4Pek6E1oJ.FMxFTIduykzwDgubECS6cqpWk.Duqut/D9GDBO6lz6cXYs0; PW=\161%.69,.)03\223; ID=\161%.69,.)03\223; RDB=C80200D6EF0000555301001E2719270101000000000002\r\n"
235
236 val s = "Cookie: PW=\161%.69\r\n"
237 in Result.isYes (Header.fromString s)
238 end)
239
240 end