Debian Codegraph @

src/tools/WebServer.nml

raw
1 /*
2 * Copyright (C)2005-2022 Haxe Foundation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 type main_function;
24
25 type client {
26 sock : Net.socket;
27 in : IO.input;
28 out : IO.output;
29 mutable headers : (string, string) list;
30 mutable headers_sent : bool;
31 mutable return_code : (int, string);
32 mutable main : main_function option;
33 }
34
35 var mime = [
36 ("gif" , "image/gif");
37 ("jpeg", "image/jpeg");
38 ("jpg", "image/jpeg");
39 ("png", "image/png");
40 ("css", "text/css");
41 ("html", "text/html");
42 ("htm", "text/html");
43 ("txt", "text/plain");
44 ("js", "application/javascript");
45 ("pdf", "application/pdf"); // do we really want that ??
46 ("xml", "text/xml");
47 ("wav", "audio/x-wav")
48 ];
49
50 var cur_path = &Sys.get_cwd();
51 var file_log = &None;
52 var module_cache = Hashtbl.create();
53 var mod_rewrite = &false;
54
55 function sys_is_file(file) {
56 neko "$loader.loadprim('std@sys_file_type',1)(file)" == "file"
57 }
58
59 function default_page_404(url) {
60 "<HTML><HEAD>
61 <TITLE>404 Not Found</TITLE>
62 </HEAD><BODY>
63 <H1>Not Found</H1>
64 The requested URL "+url+" was not found on this server.<P>
65 <HR>
66 <ADDRESS>Neko Web DevServer</ADDRESS>
67 </BODY></HTML>"
68 }
69
70 function page_config() {
71 var path = String.concat " - " neko("@List.@make($loader.path)");
72 "<html>
73 <head><title>Neko WebServer Config</title></head>
74 <body>
75 <h1>Neko WebServer Configuration</h1>
76 <form action=\"/server:config\" method=\"POST\">
77 <table>
78 <tr>
79 <td>Loader path :</td>
80 <td>"+path+"</td>
81 </tr>
82 <tr>
83 <td>Server directory :</td>
84 <td><input type=\"input\" size=\"50\" name=\"path\" value=\""+(*cur_path)+"\"/></td>
85 </tr>
86 <tr>
87 <td>&nbsp;</td>
88 <td><input type=\"submit\" value=\"Modify\"/></td>
89 </tr>
90 </form>
91 <a href=\"/\">Visit website</a>
92 </body>
93 </html>"
94 }
95
96 type http_method {
97 MethodGet;
98 MethodPost;
99 MethodCustom : string;
100 }
101
102 type http_request {
103 method : http_method;
104 res : string;
105 url : (string, string);
106 version : string;
107 ctype : string option;
108 headers : (string,string) list;
109 mutable params : (string, string) list;
110 mutable post_data : string option;
111 }
112
113 exception Invalid_char;
114
115 function invalid_char(l) { throw Invalid_char };
116
117 function part(lex,p,l) { var s = Lexer.current lex; String.sub s p (String.length s - (p+l)) }
118
119 function parse_get_params(str) {
120 var params = String.split str ";";
121 var params = List.concat (List.map (function(s) { String.split s "&" }) params);
122 List.map (function(p) {
123 match String.split p "=" {
124 | [] -> ("","")
125 | [p] -> (Net.url_decode p,"")
126 | p :: val -> (Net.url_decode p , Net.url_decode (String.concat "=" val))
127 }
128 }) params;
129 }
130
131 var http_request_method = Lexer.build [
132 ("GET", function(l) { MethodGet });
133 ("POST", function(l) { MethodPost });
134 ("PUT", function(l) { MethodCustom "PUT" });
135 ("HEAD", function(l) { MethodCustom "HEAD" });
136 ("DELETE", function(l) { MethodCustom "DELETE" });
137 ("TRACE",function(l) { MethodCustom "TRACE" });
138 ("CONNECT",function(l) { MethodCustom "CONNECT" });
139 ("OPTIONS",function(l) { MethodCustom "OPTIONS" });
140 ] invalid_char;
141
142 var http_request_url = Lexer.build [
143 (" [^ ]+ ", function(l) {
144 var url = part l 1 1;
145 try {
146 var p = String.find url 0 "?";
147 var base = String.sub url 0 p;
148 var params = String.sub url (p + 1) (String.length url - (p+1));
149 (base, params)
150 } catch {
151 Not_found -> (url,"")
152 }
153 })
154 ] invalid_char;
155
156 var http_request_version = Lexer.build [
157 ("HTTP/[0-9.]+\r\n", function(l) { part l 5 2 });
158 ] invalid_char;
159
160 var http_header_value = Lexer.build [
161 ("[^\r]+\r\n", function(l) { part l 0 2 })
162 ] invalid_char;
163
164 var http_headers = &Lexer.empty();
165 http_headers := Lexer.build [
166 ("\r", function(l) {
167 if Lexer.read l != '\n' then throw Invalid_char;
168 []
169 });
170 ("[-A-Za-z0-9_]+: ", function(l) {
171 var s = part l 0 2;
172 var v = Lexer.token l http_header_value;
173 (s,v) :: Lexer.token l (*http_headers)
174 });
175 ] invalid_char;
176
177 function header(name,headers) {
178 try {
179 Some snd(List.find (function((n,_)) { String.lowercase n == String.lowercase name }) headers)
180 } catch {
181 Not_found -> None
182 }
183 }
184
185 function parse_http_request(c) {
186 var l = Lexer.create();
187 Lexer.input l "http-stream" c.in 1 0;
188 var meth = Lexer.token l http_request_method;
189 var url = Lexer.token l http_request_url;
190 var version = Lexer.token l http_request_version;
191 var headers = Lexer.token l (*http_headers);
192 var params = parse_get_params snd(url);
193 var ctype = header "Content-Type" headers;
194 var post_data, params = if meth == MethodPost && ctype != Some "multipart/form-data" then {
195 var len = match header "Content-Length" headers { None -> 0 | Some len -> int len };
196 if len >= (1 << 18) then {
197 IO.write c.out "HTTP/1.1 200 OK\r\n";
198 IO.write c.out "\n\n";
199 IO.write c.out "Max post data exceeded";
200 printf "Max post data exceeded (%d bytes)\n" len;
201 throw IO.Eof;
202 }
203 var b = Buffer.create();
204 var i = &0;
205 while *i < len {
206 Buffer.add_char b (Lexer.read l);
207 i := *i + 1;
208 }
209 var data = Buffer.string b;
210 (Some data , List.append (parse_get_params data) params);
211 } else
212 (None , params);
213 {
214 method = meth;
215 res = Net.url_decode fst(url);
216 url = url;
217 params = params;
218 version = version;
219 headers = headers;
220 post_data = post_data;
221 ctype = ctype;
222 }
223 }
224
225 function flog(s) {
226 match *file_log {
227 | None -> ()
228 | Some f ->
229 IO.write f s;
230 IO.flush f;
231 }
232 }
233
234 function log( fmt : 'a format, args : 'a ) {
235 var s = sprintf fmt args + "\n";
236 print s;
237 flog s;
238 }
239
240 function init_client(s) {
241 var read, write = Net.socket_io s;
242 var ip , port = Net.socket_peer s;
243 {
244 sock = s;
245 in = read;
246 out = write;
247 headers = [("Content-Type","text/html")];
248 headers_sent = false;
249 return_code = (200,"OK");
250 main = None;
251 }
252 }
253
254 function rec find_url_file(url,recursive) {
255 var len = String.length url;
256 if len > 0 && String.get url (len - 1) == '/' then {
257 function rec loop(l) {
258 match l {
259 | [] ->
260 if *mod_rewrite then
261 find_url_file (String.sub url 0 (len - 1)) true
262 else
263 None
264 | f :: l ->
265 match find_url_file(url + f) false {
266 | None -> loop l
267 | x -> x
268 }
269 }
270 }
271 loop ["index.html"; "index.htm"; "index.n"];
272 } else {
273 var path = if len > 0 && String.get url 0 == '/' then String.sub url 1 (len - 1) else url;
274 var path = *cur_path + path;
275 if Sys.exists path && sys_is_file path then
276 Some path
277 else if recursive && *mod_rewrite then
278 // if path like /path/to/file then lookup /path/to/file.n
279 match find_url_file (url + ".n") false {
280 | Some url -> Some url
281 | None ->
282 // lookup in subdirectory
283 match List.rev String.split(url,"/") {
284 | [] | [_] -> None
285 | _ :: dir -> find_url_file (String.concat "/" List.rev(dir) + "/") true
286 }
287 }
288 else
289 None
290 }
291 }
292
293 function log_exception(e) {
294 Stack.dump IO.stderr Stack.exc();
295 IO.printf IO.stderr "Exception : %s\n" string(e);
296 }
297
298 function print_exception(e) {
299 var stack = Stack.exc();
300 function format(s) {
301 var s = String.concat "&lt;" (String.split s "<");
302 var s = String.concat "&gt;" (String.split s ">");
303 s
304 };
305 Array.iter (function(s) {
306 match s {
307 | Stack.CFunction -> print "Called from a C function<br>"
308 | Stack.Module m -> printf "Called from %s (no debug available)<br>" format(m)
309 | Stack.Pos (file,line) -> printf "Called from %s line %d<br>" (format file,line)
310 }
311 }) stack
312 var s = string(e);
313 printf "Exception : %s" format(s);
314 }
315
316 function send_headers(c) {
317 if !c.headers_sent then {
318 c.headers_sent := true;
319 IO.write c.out (sprintf "HTTP/1.1 %d %s\r\n" c.return_code);
320 List.iter (function((name,v)) {
321 IO.write c.out (sprintf "%s: %s\r\n" (name,v));
322 }) (List.rev c.headers);
323 IO.write c.out "\r\n";
324 }
325 }
326 function headers_not_sent(c,s) {
327 // should we send an object like mod_neko failure does ?
328 if c.headers_sent then (neko "$throw")("Cannot set "+s+" : Headers already sent");
329 }
330
331 function set_header(c,n,v) {
332 headers_not_sent c n;
333 c.headers := (n,v) :: List.filter (function((n2,_)) { n != n2 }) c.headers;
334 }
335
336 var cur_client : client option ref = &None;
337 var cur_request : http_request option ref = &None;
338
339 function client() {
340 match *cur_client {
341 | None -> assert()
342 | Some c -> c
343 }
344 }
345
346 function request() {
347 match *cur_request {
348 | None -> assert()
349 | Some r -> r
350 }
351 }
352
353 function do_print(v) {
354 var c = client();
355 try {
356 send_headers(c);
357 var s = string(v);
358 IO.write c.out s;
359 } catch {
360 Neko_error e when magic e == "std@socket_send" -> ()
361 }
362 }
363
364 function init_mod_neko() {
365 // simulate a mod_neko environment
366 var hmethods = Hashtbl.create();
367 function rec flatten(l) {
368 match l {
369 | [] -> neko "null"
370 | (x,y) :: l ->
371 var l = flatten l;
372 neko "$array(x,y,l)"
373 }
374 }
375 Sys.put_env "MOD_NEKO" "1";
376 var jit = Sys.get_env "MOD_NEKO_JIT" != None;
377 neko "$loader.loadprim('std@enable_jit',1)(jit)";
378 function def(name : string,f) {
379 Hashtbl.replace hmethods name magic(f);
380 }
381 function no_param(name,f) {
382 def name neko("function() { f(null) }");
383 }
384 no_param "get_cookies" (function() {
385 flatten (
386 match header "Cookie" request().headers {
387 | None -> []
388 | Some k ->
389 var l = String.split k "; ";
390 List.map (function(k) {
391 match String.split k "=" {
392 | [] | [_] -> (k,"")
393 | k :: l -> (k,String.concat "=" l)
394 }
395 }) l
396 }
397 )
398 });
399 def "set_cookie" (function(name,val) {
400 var c = client();
401 headers_not_sent c "Cookie";
402 c.headers := ("Set-Cookie",name+"="+val+";") :: c.headers;
403 });
404 no_param "get_host_name" (function() {
405 Net.host_to_string fst(Net.socket_host client().sock);
406 });
407 no_param "get_client_ip" (function() {
408 Net.host_to_string fst(Net.socket_peer client().sock);
409 });
410 no_param "get_uri" (function() {
411 var r = request();
412 fst r.url
413 });
414 def "redirect" (function(url) {
415 var c = client();
416 headers_not_sent c "Redirection";
417 set_header c "Location" url;
418 c.return_code := (302, "Found");
419 });
420 def "set_return_code" (function(i) {
421 var c = client();
422 headers_not_sent c "Return code";
423 c.return_code := (i, "OK");
424 });
425 def "set_header" (function(name,val) {
426 var c = client();
427 headers_not_sent c name;
428 set_header c name val;
429 });
430 def "get_client_header" (function(name) {
431 match header name request().headers {
432 | None -> neko "null"
433 | Some h -> h
434 }
435 });
436 no_param "get_client_headers" (function() {
437 flatten request().headers
438 });
439 no_param "get_params_string" (function() {
440 snd request().url
441 });
442 no_param "get_post_data" (function() {
443 match request().post_data {
444 | None -> neko "null"
445 | Some d -> d
446 }
447 });
448 no_param "get_params" (function() {
449 flatten request().params
450 });
451 no_param "cgi_get_cwd" (function() {
452 *cur_path
453 });
454 def "cgi_set_main" (function(f) {
455 client().main := (if f == neko "null" then None else Some f);
456 });
457 no_param "cgi_flush" (function() {
458 IO.flush client().out;
459 });
460 def "parse_multipart_data" (function(part,data) {
461 if request().ctype == Some "multipart/form-data" then (neko "$throw")("parse_multipart_data not implemented");
462 });
463 no_param "get_http_method" (function() {
464 match request().method {
465 | MethodGet -> "GET"
466 | MethodPost -> "POST"
467 | MethodCustom r -> r
468 }
469 });
470 def "log_message" (function (s) {
471 IO.printf IO.stderr "[log] %s\n" s;
472 });
473 function resolve_method(f:string) {
474 try {
475 Hashtbl.find hmethods f
476 } catch {
477 Not_found -> neko "null"
478 }
479 }
480 var loader = neko "{
481 args => $array(),
482 path => $loader.path,
483 cache => $loader.cache,
484 loadmodule => function(name,l) {
485 $loader.cache = this.cache;
486 $loader.loadmodule(name,l);
487 },
488 loadprim => function(prim,nargs) {
489 var l = $ssize(prim);
490 if l > 9 && $ssub(prim,0,9) == 'mod_neko@' {
491 prim = $ssub(prim,9,l-9);
492 var f = resolve_method(prim);
493 if( f == null ) $throw('Unknown mod_neko primitive : '+prim);
494 if( $nargs(f) != nargs ) $throw('Invalid number of arguments for '+prim);
495 return f;
496 }
497 return $loader.loadprim(prim,nargs);
498 }
499 }";
500 var redirect = neko "$loader.loadprim('std@print_redirect',1)";
501 var old_cache = neko "$new($loader.cache)";
502 function(c,r,file) {
503 cur_client := Some c;
504 cur_request := Some r;
505 var main_fun = try {
506 var f = Hashtbl.find module_cache file;
507 c.main := Some f;
508 f
509 } catch {
510 Not_found -> neko "null"
511 };
512 try {
513 redirect(do_print);
514 if main_fun == neko "null" then neko "loader.loadmodule(file,loader)" else neko "main_fun()";
515 redirect(neko "null");
516 } catch {
517 e ->
518 log_exception(e); // stderr
519 try { print_exception(e) } catch { e -> () }; // on screen
520 redirect(neko "null");
521 }
522 if !c.headers_sent then do_print " ";
523 match c.main {
524 | None ->
525 neko "
526 $loader.cache = $new(old_cache);
527 loader.cache = $loader.cache;
528 ";
529 | Some f ->
530 Hashtbl.add module_cache file f;
531 }
532 }
533 }
534
535 var mod_neko = init_mod_neko();
536
537 function config(c,r) {
538 try {
539 var dir = List.assoc "path" r.params;
540 var old = Sys.get_cwd();
541 try {
542 Sys.set_cwd dir;
543 cur_path := Sys.get_cwd();
544 Sys.set_cwd old;
545 } catch {
546 e -> log_exception e
547 }
548 } catch {
549 Not_found -> ()
550 }
551 do_print page_config()
552 }
553
554 function write_page(c,file,ext,code) {
555 IO.write c.out (sprintf "HTTP/1.1 %d %s\r\n" code);
556 var ctype = try { List.assoc ext mime } catch { Not_found -> "unknown/unknown" };
557 var file_size : int = neko "$loader.loadprim('std@sys_stat',1)(file).size";
558 IO.write c.out ("Content-Type: " + ctype + "\r\n");
559 IO.write c.out ("Content-Length: " + file_size + "\r\n");
560 IO.write c.out "\r\n";
561 var fin = IO.read_file file true;
562 var bufsize = 100000;
563 var n = &(file_size / bufsize);
564 try {
565 while *n > 0 {
566 IO.write c.out IO.read(fin,bufsize);
567 n := *n - 1;
568 }
569 IO.write c.out IO.read(fin,file_size % bufsize);
570 } catch {
571 e -> IO.close_in fin; throw e
572 }
573 IO.close_in fin
574 }
575
576 function client_msg(c) {
577 var r = parse_http_request c;
578 cur_client := Some c;
579 if r.res == "/server:config" then
580 config(c,r)
581 else match find_url_file r.res true {
582 | None ->
583 function rec loop(l) {
584 match l {
585 | [] ->
586 c.return_code := (404, "Not Found");
587 do_print default_page_404(r.res);
588 | f :: l ->
589 match find_url_file f false {
590 | Some page_404 ->
591 var ext = String.lowercase (Sys.extension page_404);
592 write_page c page_404 ext (404, "Not Found")
593 | None -> loop(l)
594 }
595 }
596 }
597 loop ["404.html"; "404.htm"];
598 | Some file ->
599 var ext = String.lowercase (Sys.extension file);
600 if ext == "n" then {
601 log "Request %s [%s]" (r.res, match r.params {
602 | [] -> ""
603 | l -> "\n " + (String.concat "\n " (List.map (function((p,v)) { p + " => "+ v }) l)) + "\n"
604 });
605 mod_neko c r file
606 } else {
607 // directly send the file content
608 write_page c file ext (200, "OK");
609 }
610 }
611 }
612
613 function client_msg_safe(c) {
614 try {
615 client_msg(c);
616 } catch {
617 | Invalid_char -> printf "Invalid char received\n" ();
618 | IO.Eof -> printf "Connection aborted\n" ();
619 | Neko_error e when Reflect.value e == Reflect.VString "std@socket_send" -> printf "Sending file aborted\n" ();
620 }
621 Net.socket_close c.sock;
622 neko "$loader.loadprim('std@run_gc',1)(true)";
623 false
624 }
625
626 function init() {
627 var head = "Neko Web Server v1.0 - (c)2005-2022 Haxe Foundation";
628 var port = &2000;
629 var host = &"localhost";
630 var decl = [
631 ("-p", Args.Int (function(n) { port := n }) , "<port> : change server port");
632 ("-h", Args.String (function(h) { host := h }) , "<host> : change server host");
633 ("-d", Args.String (function(d) {
634 var old = Sys.get_cwd();
635 Sys.set_cwd d;
636 cur_path := Sys.get_cwd();
637 Sys.set_cwd old;
638 }), "<dir> : change the server base directory");
639 ("-log", Args.String (function(s) { file_log := Some (IO.write_file s true) }), "<file> : set log file");
640 ("-rewrite", Args.Void (function() { mod_rewrite := true }), ": activate pseudo mod-rewrite for smart urls");
641 ];
642 Args.parse head decl (function(f) { throw Args.Invalid });
643 log "Starting Neko Server on %s:%d" (*host,*port);
644 Net.start_server Net.host_resolve(*host) (*port) init_client client_msg_safe;
645 }