| 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 |
loadFile(fname) |
| 9 |
{ |
| 10 |
/* file extension to MIME-type map */ |
| 11 |
local mimeMap = [ |
| 12 |
'htm' -> 'text/html', |
| 13 |
'html' -> 'text/html', |
| 14 |
'txt' -> 'text/plain', |
| 15 |
'jpg' -> 'image/jpeg', |
| 16 |
'jpeg' -> 'image/jpeg', |
| 17 |
'png' -> 'image/png', |
| 18 |
* -> 'application/octet-stream']; |
| 19 |
|
| 20 |
try |
| 21 |
{ |
| 22 |
/* get the extension */ |
| 23 |
local ext = ''; |
| 24 |
|
| 25 |
if (rexMatch('.*<dot>([a-z0-9_]+)$', fname)) |
| 26 |
ext = rexGroup(1)[3]; |
| 27 |
|
| 28 |
/* look up the MIME type for this extension */ |
| 29 |
local mime = mimeMap[ext]; |
| 30 |
|
| 31 |
/* read the text or binary file */ |
| 32 |
local f, body; |
| 33 |
if (mime.startsWith('text/')) |
| 34 |
{ |
| 35 |
/* read the text file */ |
| 36 |
f = File.openTextFile(fname, FileAccessRead); |
| 37 |
|
| 38 |
/* read the file into a StringBuffer */ |
| 39 |
body = new StringBuffer(f.getFileSize()); |
| 40 |
for (;;) |
| 41 |
{ |
| 42 |
/* read the next line; stop if done */ |
| 43 |
local t = f.readFile(); |
| 44 |
if (t == nil) |
| 45 |
break; |
| 46 |
|
| 47 |
/* add it to the string buffer */ |
| 48 |
body.append(t); |
| 49 |
} |
| 50 |
} |
| 51 |
else |
| 52 |
{ |
| 53 |
/* read the raw binary file */ |
| 54 |
f = File.openRawFile(fname, FileAccessRead); |
| 55 |
|
| 56 |
/* read it in */ |
| 57 |
body = new ByteArray(f.getFileSize()); |
| 58 |
f.readBytes(body, 1, f.getFileSize()); |
| 59 |
} |
| 60 |
|
| 61 |
/* done with the file */ |
| 62 |
f.closeFile(); |
| 63 |
|
| 64 |
/* return the file information */ |
| 65 |
return new FileInfo(body, mime); |
| 66 |
} |
| 67 |
catch (FileException fex) |
| 68 |
{ |
| 69 |
/* couldn't open the file - return nil */ |
| 70 |
"error opening <<fname>>: <<fex.displayException()>>\n"; |
| 71 |
return nil; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
class FileInfo: object |
| 76 |
construct(body, mime) |
| 77 |
{ |
| 78 |
self.body = body; |
| 79 |
self.mimeType = mime; |
| 80 |
} |
| 81 |
|
| 82 |
body = nil |
| 83 |
mimeType = nil |
| 84 |
; |
| 85 |
|
| 86 |
main(args) |
| 87 |
{ |
| 88 |
local host; |
| 89 |
if (args.length() >= 2 && args.sublist(2).indexOf('-local') != nil) |
| 90 |
host = 'localhost'; |
| 91 |
else |
| 92 |
host = getLocalIP(); |
| 93 |
|
| 94 |
/* set up the server */ |
| 95 |
local l = new HTTPServer(host, nil, 65535); |
| 96 |
"HTTP server listening on <<host>>:<<l.getPortNum()>><br>"; |
| 97 |
|
| 98 |
/* no uploads yet */ |
| 99 |
local uploads = new LookupTable(); |
| 100 |
|
| 101 |
/* handle network events */ |
| 102 |
for (;;) |
| 103 |
{ |
| 104 |
try |
| 105 |
{ |
| 106 |
/* get the next network event */ |
| 107 |
inputEvent(0); |
| 108 |
local evt = getNetEvent(); |
| 109 |
|
| 110 |
/* see what we have */ |
| 111 |
if (evt.evType == NetEvRequest |
| 112 |
&& evt.evRequest.ofKind(HTTPRequest)) |
| 113 |
{ |
| 114 |
/* |
| 115 |
* HTTP request - get the query string, minus the leading |
| 116 |
* slash. For example, if they asked for |
| 117 |
* http://server/x/y, this will give us 'x/y'. |
| 118 |
*/ |
| 119 |
local req = evt.evRequest; |
| 120 |
local res = req.parseQuery()[1].substr(2); |
| 121 |
local flds = req.getFormFields(); |
| 122 |
|
| 123 |
// local addr = req.getClientAddress(); |
| 124 |
// "Got request: res=<<res>>, verb=<<req.getVerb()>>, |
| 125 |
// from=<<addr[1]>>:<<addr[2]>><br>"; |
| 126 |
|
| 127 |
if (req.getVerb() == 'GET' && res.startsWith('uploads/')) |
| 128 |
{ |
| 129 |
// "Serving uploads/ request for <<res.substr(9)>>\n"; |
| 130 |
local f = uploads[res.substr(9)]; |
| 131 |
if (f != nil) |
| 132 |
req.sendReply(f.file); |
| 133 |
else |
| 134 |
req.sendReply('File not found', 'text/plain', 404); |
| 135 |
|
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
if (req.getVerb() == 'POST') |
| 140 |
{ |
| 141 |
foreach (local f in flds) |
| 142 |
{ |
| 143 |
if (dataType(f) == TypeObject && f.ofKind(FileUpload)) |
| 144 |
uploads[f.filename] = f; |
| 145 |
} |
| 146 |
|
| 147 |
local body = req.getBody(); |
| 148 |
if (body == nil) |
| 149 |
{ |
| 150 |
"No body in PUT<br>"; |
| 151 |
} |
| 152 |
else if (body.getFileMode() == FileModeRaw) |
| 153 |
{ |
| 154 |
// "Binary message body in PUT<br>"; |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
"POST message body:<br><hr>"; |
| 159 |
for (;;) |
| 160 |
{ |
| 161 |
local l = body.readFile(); |
| 162 |
if (l == nil) |
| 163 |
break; |
| 164 |
tadsSay(l.htmlify()); |
| 165 |
} |
| 166 |
"<hr><br>"; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/* if this is the special "QUIT!" request, shut down */ |
| 171 |
if (res == 'QUIT!') |
| 172 |
{ |
| 173 |
req.sendReply('QUIT! request received - shutting down'); |
| 174 |
break; |
| 175 |
} |
| 176 |
|
| 177 |
/* try opening the file */ |
| 178 |
local f = loadFile(res); |
| 179 |
if (f != nil) |
| 180 |
{ |
| 181 |
if (rexMatch('([^/]*/)*test-upload.htm$', res)) |
| 182 |
{ |
| 183 |
f.body = toString(f.body); |
| 184 |
|
| 185 |
f.body = f.body.findReplace( |
| 186 |
['value.username', 'value.email'], |
| 187 |
['value="Mike!"', 'value="[email protected]"']); |
| 188 |
|
| 189 |
if (flds != nil) |
| 190 |
{ |
| 191 |
f.body += '<p>Form fields:<br><ul>' |
| 192 |
+ flds.keysToList().mapAll({ |
| 193 |
k: '<li><<k>>=<<descField(flds[k])>>' }) |
| 194 |
.join() |
| 195 |
+ '</ul>'; |
| 196 |
} |
| 197 |
|
| 198 |
f.body += '<p>Uploads available:<br><ul>' |
| 199 |
+ uploads.keysToList().mapAll(new function(k) { |
| 200 |
local f = uploads[k]; |
| 201 |
if (f.contentType.startsWith('image/')) |
| 202 |
return '<li><a href="/uploads/<<k>>">' |
| 203 |
+ '<img src="/uploads/<<k>>" border=0><br>' |
| 204 |
+ '<<k.htmlify()>></a>'; |
| 205 |
else |
| 206 |
return '<li><a href="/uploads/<<k>>">' |
| 207 |
+ '<<k.htmlify()>></a>'; |
| 208 |
}).join() + '</ul>'; |
| 209 |
} |
| 210 |
req.sendReply(f.body, f.mimeType); |
| 211 |
} |
| 212 |
else |
| 213 |
req.sendReply('FNF File not found', 'text/plain', 404); |
| 214 |
} |
| 215 |
} |
| 216 |
catch (SocketDisconnectException sdx) |
| 217 |
{ |
| 218 |
/* |
| 219 |
* ignore these - they just mean that the client closed its |
| 220 |
* connection before we could send the reply; their loss |
| 221 |
*/ |
| 222 |
} |
| 223 |
catch (NetException nx) |
| 224 |
{ |
| 225 |
"<<nx.displayException()>><br>"; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
"Shutting down the HTTP server...<br>"; |
| 230 |
l.shutdown(true); |
| 231 |
} |
| 232 |
|
| 233 |
descField(fld) |
| 234 |
{ |
| 235 |
if (dataType(fld) == TypeSString) |
| 236 |
return fld; |
| 237 |
else if (dataType(fld) == TypeObject && fld.ofKind(FileUpload)) |
| 238 |
return 'File(name=<<fld.filename>>, content-type=<<fld.contentType>>)'; |
| 239 |
else |
| 240 |
return 'Unknown field type'; |
| 241 |
} |
| 242 |
|
| 243 |
modify List |
| 244 |
join(sep = '') { return listJoin(self, sep); } |
| 245 |
; |
| 246 |
|
| 247 |
listJoin(lst, delim) |
| 248 |
{ |
| 249 |
/* if the list is empty, return an empty string */ |
| 250 |
local len = lst.length(); |
| 251 |
if (len == 0) |
| 252 |
return ''; |
| 253 |
|
| 254 |
/* concatenate the elements */ |
| 255 |
local str = lst[1]; |
| 256 |
for (local i = 2 ; i <= len ; ++i) |
| 257 |
str += delim + lst[i]; |
| 258 |
|
| 259 |
/* return the finished string */ |
| 260 |
return str; |
| 261 |
} |
| 262 |
|
| 263 |
|