/
Web.hx
409 lines (381 loc) · 11.9 KB
/
Web.hx
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package neko;
import haxe.ds.List;
/**
This class is used for accessing the local Web server and the current
client request and information.
**/
class Web {
/**
Returns the GET and POST parameters.
**/
public static function getParams() {
var p = _get_params();
var h = new haxe.ds.StringMap<String>();
var k = "";
while (p != null) {
untyped k.__s = p[0];
h.set(k, new String(p[1]));
p = untyped p[2];
}
return h;
}
/**
Returns an Array of Strings built using GET / POST values.
If the URL contains the parameters `[a[]=foo;a[]=hello;a[5]=bar;a[3]=baz]` then
`neko.Web.getParamValues("a")` will return `["foo","hello",null,"baz",null,"bar"]`
**/
public static function getParamValues(param:String):Array<String> {
var reg = new EReg("^" + param + "(\\[|%5B)([0-9]*?)(\\]|%5D)=(.*?)$", "");
var res = new Array<String>();
var explore = function(data:String) {
if (data == null || data.length == 0)
return;
for (part in data.split("&")) {
if (reg.match(part)) {
var idx = reg.matched(2);
var val = StringTools.urlDecode(reg.matched(4));
if (idx == "")
res.push(val);
else
res[Std.parseInt(idx)] = val;
}
}
}
explore(StringTools.replace(getParamsString(), ";", "&"));
explore(getPostData());
if (res.length == 0)
return null;
return res;
}
/**
Returns the local server host name.
**/
public static function getHostName() {
return new String(_get_host_name());
}
/**
Surprisingly returns the client IP address.
**/
public static function getClientIP() {
return new String(_get_client_ip());
}
/**
Returns the original request URL (before any server internal redirections)
**/
public static function getURI() {
return new String(_get_uri());
}
/**
Tell the client to redirect to the given url ("Location" header)
**/
public static function redirect(url:String) {
_cgi_redirect(untyped url.__s);
}
/**
Set an output header value. If some data have been printed, the headers have
already been sent so this will raise an exception.
**/
public static function setHeader(h:String, v:String) {
_cgi_set_header(untyped h.__s, untyped v.__s);
}
/**
Set the HTTP return code. Same remark as setHeader.
**/
public static function setReturnCode(r:Int) {
_set_return_code(r);
}
/**
Retrieve a client header value sent with the request.
**/
public static function getClientHeader(k:String) {
var v = _get_client_header(untyped k.__s);
if (v == null)
return null;
return new String(v);
}
/**
Retrieve all the client headers.
**/
public static function getClientHeaders() {
var v = _get_client_headers();
var a = new List();
while (v != null) {
a.add({header: new String(v[0]), value: new String(v[1])});
v = cast v[2];
}
return a;
}
/**
Returns all the GET parameters String.
**/
public static function getParamsString() {
var p = _get_params_string();
return if (p == null) "" else new String(p);
}
/**
Returns all the POST data. POST Data is always parsed as
being `application/x-www-form-urlencoded` and is stored into
the getParams hashtable. POST Data is maximimized to 256K
unless the content type is `multipart/form-data`. In that
case, you will have to use `getMultipart` or `parseMultipart`
methods.
**/
public static function getPostData() {
var v = _get_post_data();
if (v == null)
return null;
return new String(v);
}
/**
Returns an hashtable of all Cookies sent by the client.
Modifying the hashtable will not modify the cookie, use `setCookie` instead.
**/
public static function getCookies():Map<String, String> {
var p = _get_cookies();
var h = new haxe.ds.StringMap<String>();
var k = "";
while (p != null) {
untyped k.__s = p[0];
h.set(k, new String(p[1]));
p = untyped p[2];
}
return h;
}
/**
Set a Cookie value in the HTTP headers. Same remark as `setHeader`.
**/
public static function setCookie(key:String, value:String, ?expire:Date, ?domain:String, ?path:String, ?secure:Bool, ?httpOnly:Bool) {
var buf = new StringBuf();
buf.add(value);
if (expire != null)
addPair(buf, "expires=", DateTools.format(expire, "%a, %d-%b-%Y %H:%M:%S GMT"));
addPair(buf, "domain=", domain);
addPair(buf, "path=", path);
if (secure)
addPair(buf, "secure", "");
if (httpOnly)
addPair(buf, "HttpOnly", "");
var v = buf.toString();
_set_cookie(untyped key.__s, untyped v.__s);
}
static function addPair(buf:StringBuf, name:String, value:String) {
if (value == null)
return;
buf.add("; ");
buf.add(name);
buf.add(value);
}
/**
Returns an object with the authorization sent by the client (Basic scheme only).
**/
public static function getAuthorization():{user:String, pass:String} {
var h = getClientHeader("Authorization");
var reg = ~/^Basic ([^=]+)=*$/;
if (h != null && reg.match(h)) {
var val = reg.matched(1);
untyped val = new String(_base_decode(val.__s, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".__s));
var a = val.split(":");
if (a.length != 2) {
throw "Unable to decode authorization.";
}
return {user: a[0], pass: a[1]};
}
return null;
}
/**
Get the current script directory in the local filesystem.
**/
public static function getCwd() {
return new String(_get_cwd());
}
/**
Set the main entry point function used to handle requests.
Setting it back to null will disable code caching.
**/
public static function cacheModule(f:Void->Void) {
_set_main(f);
}
/**
Get the multipart parameters as an hashtable. The data
cannot exceed the maximum size specified.
**/
public static function getMultipart(maxSize:Int):Map<String, String> {
var h = new haxe.ds.StringMap();
var buf:haxe.io.BytesBuffer = null;
var curname = null;
parseMultipart(function(p, _) {
if (curname != null)
h.set(curname, neko.Lib.stringReference(buf.getBytes()));
curname = p;
buf = new haxe.io.BytesBuffer();
maxSize -= p.length;
if (maxSize < 0)
throw "Maximum size reached";
}, function(str, pos, len) {
maxSize -= len;
if (maxSize < 0)
throw "Maximum size reached";
buf.addBytes(str, pos, len);
});
if (curname != null)
h.set(curname, neko.Lib.stringReference(buf.getBytes()));
return h;
}
/**
Parse the multipart data. Call `onPart` when a new part is found
with the part name and the filename if present
and `onData` when some part data is read. You can this way
directly save the data on hard drive in the case of a file upload.
**/
public static function parseMultipart(onPart:String->String->Void, onData:haxe.io.Bytes->Int->Int->Void):Void {
_parse_multipart(function(p, f) {
onPart(new String(p), if (f == null) null else new String(f));
}, function(buf, pos, len) {
onData(untyped new haxe.io.Bytes(__dollar__ssize(buf), buf), pos, len);
});
}
/**
Flush the data sent to the client. By default on Apache, outgoing data is buffered so
this can be useful for displaying some long operation progress.
**/
public static function flush():Void {
_flush();
}
/**
Get the HTTP method used by the client. This API requires Neko 1.7.1+.
**/
public static function getMethod():String {
return new String(_get_http_method());
}
/**
Write a message into the web server log file. This API requires Neko 1.7.1+.
**/
public static function logMessage(msg:String) {
_log_message(untyped msg.__s);
}
public static var isModNeko(default, null):Bool;
public static var isTora(default, null):Bool;
static var _set_main:Dynamic;
static var _get_host_name:Dynamic;
static var _get_client_ip:Dynamic;
static var _get_uri:Dynamic;
static var _cgi_redirect:Dynamic;
static var _cgi_set_header:Dynamic;
static var _set_return_code:Dynamic;
static var _get_client_header:Dynamic;
static var _get_params_string:Dynamic;
static var _get_post_data:Dynamic;
static var _get_params:Dynamic;
static var _get_cookies:Dynamic;
static var _set_cookie:Dynamic;
static var _get_cwd:Dynamic;
static var _parse_multipart:Dynamic;
static var _flush:Dynamic;
static var _get_client_headers:Dynamic;
static var _get_http_method:Dynamic;
static var _base_decode = Lib.load("std", "base_decode", 2);
static var _log_message:Dynamic;
static function __init__() {
var get_env = Lib.load("std", "get_env", 1);
var ver = untyped get_env("MOD_NEKO".__s);
untyped isModNeko = (ver != null);
if (isModNeko) {
var lib = "mod_neko" + if (ver == untyped "1".__s) "" else ver;
_set_main = Lib.load(lib, "cgi_set_main", 1);
_get_host_name = Lib.load(lib, "get_host_name", 0);
_get_client_ip = Lib.load(lib, "get_client_ip", 0);
_get_uri = Lib.load(lib, "get_uri", 0);
_cgi_redirect = Lib.load(lib, "redirect", 1);
_cgi_set_header = Lib.load(lib, "set_header", 2);
_set_return_code = Lib.load(lib, "set_return_code", 1);
_get_client_header = Lib.load(lib, "get_client_header", 1);
_get_params_string = Lib.load(lib, "get_params_string", 0);
_get_post_data = Lib.load(lib, "get_post_data", 0);
_get_params = Lib.load(lib, "get_params", 0);
_get_cookies = Lib.load(lib, "get_cookies", 0);
_set_cookie = Lib.load(lib, "set_cookie", 2);
_get_cwd = Lib.load(lib, "cgi_get_cwd", 0);
_get_http_method = Lib.loadLazy(lib, "get_http_method", 0);
_parse_multipart = Lib.loadLazy(lib, "parse_multipart_data", 2);
_flush = Lib.loadLazy(lib, "cgi_flush", 0);
_get_client_headers = Lib.loadLazy(lib, "get_client_headers", 0);
_log_message = Lib.loadLazy(lib, "log_message", 1);
isTora = try Lib.load(lib, "tora_infos", 0) != null catch (e:Dynamic) false;
} else {
var a0 = untyped __dollar__loader.args[0];
if (a0 != null)
a0 = new String(a0);
_set_main = function(f) {};
_get_host_name = function() {
return untyped "localhost".__s;
};
_get_client_ip = function() {
return untyped "127.0.0.1".__s;
};
_get_uri = function() {
return untyped (if (a0 == null) "/" else a0).__s;
};
_cgi_redirect = function(v) {
Lib.print("Location: " + v + "\n");
};
_cgi_set_header = function(h, v) {};
_set_return_code = function(i) {};
_get_client_header = function(h) {
return null;
};
_get_client_headers = function() {
return null;
};
_get_params_string = function() {
return untyped (if (a0 == null) "" else a0).__s;
};
_get_post_data = function() {
return null;
};
_get_params = function() {
var l = null;
if (a0 == null)
return null;
for (p in a0.split(";")) {
var k = p.split("=");
if (k.length == 2)
l = untyped [k[0].__s, k[1].__s, l];
}
return l;
};
_get_cookies = function() {
return null;
}
_set_cookie = function(k, v) {};
_get_cwd = Lib.load("std", "get_cwd", 0);
_get_http_method = function() return untyped "GET".__s;
_parse_multipart = function(a, b) {
throw "Not supported";
};
_flush = function() {};
_log_message = function(s) {};
isTora = false;
}
}
}