| 1 |
#include <tads.h> |
| 2 |
#include <tadsnet.h> |
| 3 |
#include <httpsrv.h> |
| 4 |
#include <httpreq.h> |
| 5 |
#include <strbuf.h> |
| 6 |
#include <file.h> |
| 7 |
|
| 8 |
main(args) |
| 9 |
{ |
| 10 |
local host; |
| 11 |
if (args.length() >= 2 && args.sublist(2).indexOf('-local') != nil) |
| 12 |
host = 'localhost'; |
| 13 |
else |
| 14 |
host = getLocalIP(); |
| 15 |
|
| 16 |
/* set up the server */ |
| 17 |
local srv = new HTTPServer(host, nil, 65535); |
| 18 |
"HTTP server listening on <<host>>:<<srv.getPortNum()>><br>"; |
| 19 |
|
| 20 |
/* connect */ |
| 21 |
connectWebUI(srv, '/ui'); |
| 22 |
|
| 23 |
/* handle network events */ |
| 24 |
for (;;) |
| 25 |
{ |
| 26 |
try |
| 27 |
{ |
| 28 |
/* get the next network event */ |
| 29 |
inputEvent(0); |
| 30 |
local evt = getNetEvent(); |
| 31 |
|
| 32 |
/* see what we have */ |
| 33 |
if (evt.evType == NetEvRequest |
| 34 |
&& evt.evRequest.ofKind(HTTPRequest)) |
| 35 |
{ |
| 36 |
/* |
| 37 |
* HTTP request - get the query string, minus the leading |
| 38 |
* slash. For example, if they asked for |
| 39 |
* http://server/x/y, this will give us 'x/y'. |
| 40 |
*/ |
| 41 |
local req = evt.evRequest; |
| 42 |
local res = req.parseQuery()[1].substr(2); |
| 43 |
|
| 44 |
if (res == 'ui') |
| 45 |
{ |
| 46 |
/* send the main UI */ |
| 47 |
req.sendReply( |
| 48 |
'<html><title>Test UI</title>' |
| 49 |
+ '<body><h1>Test UI</h1>' |
| 50 |
+ 'If you\'re seeing this, it means that your ' |
| 51 |
+ 'TADS Web-based game is working!' |
| 52 |
+ '<p><a href="quit">Shut down server</a><br>' |
| 53 |
+ '</body></html>', 'text/html'); |
| 54 |
} |
| 55 |
else if (res == 'quit') |
| 56 |
{ |
| 57 |
req.sendReply('Shutting down server!', 'text/plain'); |
| 58 |
break; |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
req.sendReply('file not found', 'text/plain', 404); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
catch (SocketDisconnectException sdx) |
| 67 |
{ |
| 68 |
/* |
| 69 |
* ignore these - they just mean that the client closed its |
| 70 |
* connection before we could send the reply; their loss |
| 71 |
*/ |
| 72 |
} |
| 73 |
catch (NetException nx) |
| 74 |
{ |
| 75 |
"<<nx.displayException()>><br>"; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
"Shutting down the HTTP server...<br>"; |
| 80 |
srv.shutdown(true); |
| 81 |
} |
| 82 |
|