diff --git a/doc/nasal-http-test-web.html b/doc/nasal-http-test-web.html new file mode 100644 index 0000000..81fbeef --- /dev/null +++ b/doc/nasal-http-test-web.html @@ -0,0 +1,64 @@ + + + nasal-http-test-web + + + + + +
+ + + +
+ + Hello, this is a simple HTML document just for test. This simple http server is written in nasal.
+ Nasal is an ECMAscript-like programming language that used in Flightgear designed by Andy Ross.
+ 这是个用于测试的简易HTML文档。该http服务器是用nasal编写的。
+ Nasal是类似ECMAscript的一款编程语言,用于飞行模拟器Flightgear。该语言由Andy Ross设计完成。

+ The interpreter is totally rewritten by ValKmjolnir using C++(-std=c++11) without reusing the code in Andy Ross's nasal interpreter.
+ But we really appreciate that Andy created this amazing programming language and his interpreter project.

+ Now this project uses MIT license(2021/5/4). Edit it if you want, use this project to learn or create more interesting things(But don't forget me XD).
+
+ +
+ + Here is the benchmark of different versions of nasal interpreter(left).
+ Look at this beautiful picture(right).
+ Nasal can run this test file(test/bfcolored.nas) in about 220 seconds.
+ In fact this test file cost over 2200 seconds before ver 8.0 .
+
+
+            1    var nasal=func(lexical,syntax){
+            2        return {
+            3            print  : func{println("hello world")},
+            4            set_lex: func(lex){lexical=lex},
+            5            get_lex: func{return lexical},
+            6            set_syn: func(syn){syntax=syn},
+            7            get_syn: func{return syntax}
+            8        };
+            9    }
+        
+ + \ No newline at end of file diff --git a/test/httptest.nas b/test/httptest.nas index 4e726e0..0bbd57e 100644 --- a/test/httptest.nas +++ b/test/httptest.nas @@ -3,41 +3,58 @@ import("module/libsock.nas"); var sd=socket.socket(socket.AF_INET,socket.SOCK_STREAM,socket.IPPROTO_IP); socket.bind(sd,"127.0.0.1",8080); socket.listen(sd,1); -var httpheader="Http/1.1 200 OK\n\n"; -var httptail="\n"; -var index=" - - nasal-http-test-web - - -
- - Hello, this is a simple HTML document just for test. This simple http server is written in nasal.
- Nasal is an ECMAscript-like programming language that used in Flightgear designed by Andy Ross.

- The interpreter is totally rewritten by ValKmjolnir using C++(-std=c++11) without reusing the code in Andy Ross's nasal interpreter.
- But we really appreciate that Andy created this amazing programming language and his interpreter project.

- Now this project uses MIT license(2021/5/4). Edit it if you want, use this project to learn or create more interesting things(But don't forget me XD).
-
- -
- - Here is the benchmark of different versions of nasal interpreter(left).
- Look at this beautiful picture(right).
- Nasal can run this test file(test/bfcolored.nas) in about 220 seconds.
- In fact this test file cost over 2200 seconds before ver 8.0 .
-
- -"; -var notfound=" - - nasal-http-test-web - - - - 404 NOT FOUND! - - -"; + +# var filter={ +# " ":" ", +# "&":"&", +# "<":"<", +# ">":">", +# "\"":""", +# "'":"&qpos;" +# }; +# var html_read_file=func(filename){ +# var fd=io.open(filename,"rb"); +# var content=""; +# while(1){ +# var s=mut(" "); +# if(io.read(fd,s,1)==0) +# break; +# if(contains(filter,chr(s[0]))) +# content~=filter[chr(s[0])]; +# else +# content~=s; +# } +# io.close(fd); +# return content; +# } + +var respond={ + ok:func(html){ + return "Http/1.1 200 OK\n\n"~html~"\n"; + }, + not_found:"Http/1.1 404 NOT FOUND\n\n + + 404 not found + + + + + 404 NOT FOUND! + + + \n", + teapot:"Http/1.1 418 I'm a teapot\n\n + + I'm a teapot + + + + + This server cannot brew coffee because it is a teapot. + + + \n" +}; var files=func(){ var res={}; @@ -57,22 +74,24 @@ while(1){ var first=split("\n",recieve_data.str)[0]; var (type,path)=split(" ",first)[0,1]; println("[",os.time(),"] ",client.ip," request ",type," [",path,"]"); - if(path=="/") - socket.send(client.sd,httpheader~index~httptail); + if(path=="/" or path=="/index") + socket.send(client.sd,respond.ok(io.fin("./doc/nasal-http-test-web.html"))); elsif(path=="/favicon.ico") - socket.send(client.sd,httpheader~io.fin("./pic/favicon.ico")~httptail); - elsif(path=="/nasal.png") - socket.send(client.sd,httpheader~io.fin("./pic/nasal.png")~httptail); - elsif(path=="/benchmark.png") - socket.send(client.sd,httpheader~io.fin("./pic/benchmark.png")~httptail); - elsif(path=="/mandelbrot.png") - socket.send(client.sd,httpheader~io.fin("./pic/mandelbrot.png")~httptail); + socket.send(client.sd,respond.ok(io.fin("./pic/favicon.ico"))); + elsif(path=="/pic/nasal.png" or path=="/pic/benchmark.png" or path=="/pic/mandelbrot.png") + socket.send(client.sd,respond.ok(io.fin("."~path))); else{ var filename=substr(path,1,size(path)-1); - if(contains(files,filename)) - socket.send(client.sd,httpheader~io.fin("./test/"~filename)~httptail) + if(contains(files,filename)){ + # var page=" "~filename~" \n
\n";
+            # var page_back="
\n\n"; + # socket.send(client.sd,respond.ok(page~io.fin("./test/"~filename)~page_back)); + socket.send(client.sd,respond.ok(io.fin("./test/"~filename))); + } + elsif(filename=="teapot") + socket.send(client.sd,respond.teapot); else - socket.send(client.sd,httpheader~notfound~httptail); + socket.send(client.sd,respond.not_found); } socket.closesocket(client.sd); }