📝 update test/httptest.nas

This commit is contained in:
ValKmjolnir 2022-06-08 20:01:22 +08:00
parent 12d7dde42d
commit 04806fc2c5
2 changed files with 32 additions and 10 deletions

BIN
pic/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -4,13 +4,9 @@ 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 client=socket.accept(sd);
println("request ip: ",client.ip);
var rv=socket.recv(client.sd,1024);
println(rv.str);
socket.send(client.sd,"Http/1.1 200 OK
<html>
var httpheader="Http/1.1 200 OK\n\n";
var httptail="\n";
var index="<html>
<head>
<title> nasal-http-test-web </title>
</head>
@ -27,8 +23,34 @@ socket.send(client.sd,"Http/1.1 200 OK
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).
</text>
</body>
</html>
</html>";
var notfound="<html>
<head>
<title> nasal-http-test-web </title>
</head>
<body>
<text>
404 NOT FOUND!
</text>
</body>
</html>";
");
socket.closesocket(client.sd);
while(1){
var client=socket.accept(sd);
var recieve_data=socket.recv(client.sd,1024);
if(!recieve_data.size){
println("[",client.ip,"] request connection closed");
break;
}
var first=split("\n",recieve_data.str)[0];
var (type,path)=split(" ",first)[0,1];
println("[",client.ip,"] request ",type," [",path,"]");
if(path=="/")
socket.send(client.sd,httpheader~index~httptail);
elsif(path=="/favicon.ico")
socket.send(client.sd,httpheader~io.fin("pic/favicon.ico")~httptail);
else
socket.send(client.sd,httpheader~notfound~httptail);
socket.closesocket(client.sd);
}
socket.closesocket(sd);