| 1 |
#include <tads.h> |
| 2 |
#include <tadsnet.h> |
| 3 |
#include <httpsrv.h> |
| 4 |
#include <httpreq.h> |
| 5 |
#include <bignum.h> |
| 6 |
#include <strbuf.h> |
| 7 |
|
| 8 |
modify List |
| 9 |
join(sep = '') { return listJoin(self, sep); } |
| 10 |
; |
| 11 |
listJoin(lst, delim) |
| 12 |
{ |
| 13 |
/* if the list is empty, return an empty string */ |
| 14 |
local len = lst.length(); |
| 15 |
if (len == 0) |
| 16 |
return ''; |
| 17 |
|
| 18 |
/* concatenate the elements */ |
| 19 |
local str = lst[1]; |
| 20 |
for (local i = 2 ; i <= len ; ++i) |
| 21 |
str += delim + lst[i]; |
| 22 |
|
| 23 |
/* return the finished string */ |
| 24 |
return str; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
main(args) |
| 29 |
{ |
| 30 |
/* set up the server */ |
| 31 |
"Network name is <<getHostName()>>, IP is <<getLocalIP()>>\n"; |
| 32 |
local l = new HTTPServer('localhost', nil, 65535); |
| 33 |
"HTTP server listening on localhost port <<l.getPortNum()>>\n"; |
| 34 |
|
| 35 |
try |
| 36 |
{ |
| 37 |
/* handle network events */ |
| 38 |
for (local done = nil ; !done ; ) |
| 39 |
{ |
| 40 |
/* get the next network event */ |
| 41 |
local evt = getNetEvent(60000); |
| 42 |
|
| 43 |
/* see what we have */ |
| 44 |
switch (evt.evType) |
| 45 |
{ |
| 46 |
case NetEvRequest: |
| 47 |
/* got a request */ |
| 48 |
local req = evt.evRequest; |
| 49 |
"Got a request! query=<<req.getQuery()>>: "; |
| 50 |
local prs = req.parseQuery(); |
| 51 |
local hdrs = req.getHeaders(); |
| 52 |
local cookies = req.getCookies(); |
| 53 |
prs.forEachAssoc({key, val: "<<key>>=<<val>>; "}); |
| 54 |
"<br>"; |
| 55 |
|
| 56 |
if (prs['wait'] != nil) |
| 57 |
{ |
| 58 |
"Waiting to reply - press Return..."; |
| 59 |
inputLine(); |
| 60 |
} |
| 61 |
|
| 62 |
try |
| 63 |
{ |
| 64 |
if (prs['chunk'] != nil) |
| 65 |
{ |
| 66 |
req.startChunkedReply( |
| 67 |
'text/html', nil, |
| 68 |
['Set-Cookie: TEST=chunk-asdf; path=/', |
| 69 |
'Set-Cookie: TEST2=chunk-jkl; path/']); |
| 70 |
|
| 71 |
req.sendReplyChunk( |
| 72 |
'<html><head>' |
| 73 |
+ '<title>TADS HTTP Server</title>' |
| 74 |
+ '</head><body>' |
| 75 |
+ '<h1>TADS HTPP Server</h1>' |
| 76 |
+ 'Here we are with a <b>chunked reply</b>!!!'); |
| 77 |
|
| 78 |
req.sendReplyChunk('<p>Here are the request ' |
| 79 |
+ 'parameters...<br><ul>'); |
| 80 |
prs.forEachAssoc( |
| 81 |
{ k, v: req.sendReplyChunk('<li><<k>>=<<v>>') }); |
| 82 |
req.sendReplyChunk('</ul>'); |
| 83 |
|
| 84 |
local sb = new StringBuffer(); |
| 85 |
sb.append('<p>This is some additional content that ' |
| 86 |
+ 'we\'re storing in a string buffer, to ' |
| 87 |
+ 'see how that goes. For a really good ' |
| 88 |
+ 'test, we\'ll need to make sure we go ' |
| 89 |
+ 'beyond the internal buffer so that we ' |
| 90 |
+ 'have to iterate a bit. To that end, ' |
| 91 |
+ 'well fill this up with some generated ' |
| 92 |
+ 'stuff...<p>'); |
| 93 |
|
| 94 |
for (local i = 1 ; i <= 100 ; ++i) |
| 95 |
sb.append('<<i>> = <<spellNumber(i)>><br>'); |
| 96 |
|
| 97 |
req.sendReplyChunk(sb); |
| 98 |
|
| 99 |
req.sendReplyChunk( |
| 100 |
'<p>Okay, that should about do it!<br>' |
| 101 |
+ '</body></html>'); |
| 102 |
|
| 103 |
req.endChunkedReply(); |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
local body = |
| 108 |
'<html><head>' |
| 109 |
+ '<title>TADS HTTP Server</title>' |
| 110 |
+ '</head><body>' |
| 111 |
+ '<h1>TADS HTPP Server</h1>' |
| 112 |
+ 'This is the server reply! ' |
| 113 |
+ 'Here are the request parameters:<br><ul>' |
| 114 |
+ prs.keysToList().mapAll( |
| 115 |
{ k: '<li><<k>>=<<prs[k]>>' }).join() |
| 116 |
+ '</ul><p>And here are the headers:<br><ul>' |
| 117 |
+ hdrs.keysToList().mapAll( |
| 118 |
{ k: '<li><<k>>=<<hdrs[k]>>' }).join() |
| 119 |
+ '</ul><p>And the cookies:<br><ul>' |
| 120 |
+ cookies.keysToList().mapAll( |
| 121 |
{ k: '<li><<k>>=<<cookies[k]>>' }).join() |
| 122 |
+ '</ul>' |
| 123 |
+ '</body></html>'; |
| 124 |
req.sendReply(body, nil, nil, |
| 125 |
['Set-Cookie: TEST=regular-asdf; path=/', |
| 126 |
'Set-Cookie: TEST2=regular-jkl; path/']); |
| 127 |
} |
| 128 |
} |
| 129 |
catch(SocketDisconnectException sde) |
| 130 |
{ |
| 131 |
"Socket reset - peer disconnected\n"; |
| 132 |
} |
| 133 |
|
| 134 |
if (req.getQuery() == '/quit') |
| 135 |
{ |
| 136 |
"Got quit request - waiting for server to shut down\n"; |
| 137 |
done = true; |
| 138 |
} |
| 139 |
break; |
| 140 |
|
| 141 |
case NetEvTimeout: |
| 142 |
/* timeout */ |
| 143 |
"Timeout in getNetEvent()!\n"; |
| 144 |
break; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
finally |
| 149 |
{ |
| 150 |
/* shut down the server */ |
| 151 |
l.shutdown(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
spellNumber(n) |
| 156 |
{ |
| 157 |
/* get the number formatting options */ |
| 158 |
local dot = '.', comma = ','; |
| 159 |
|
| 160 |
/* if it's a BigNumber with a fractional part, write as digits */ |
| 161 |
if (dataType(n) == TypeObject |
| 162 |
&& n.ofKind(BigNumber) |
| 163 |
&& n.getFraction() != 0) |
| 164 |
{ |
| 165 |
/* |
| 166 |
* format it, and convert decimals and group separators per the |
| 167 |
* options |
| 168 |
*/ |
| 169 |
local f = { m, i, s: m == '.' ? dot : comma }; |
| 170 |
return rexReplace('<.|,>', |
| 171 |
n.formatString(n.getPrecision(), BignumCommas), |
| 172 |
f, ReplaceAll); |
| 173 |
} |
| 174 |
|
| 175 |
/* if it's less than zero, use "minus seven" or "-123" */ |
| 176 |
if (n < 0) |
| 177 |
{ |
| 178 |
/* get the spelled version of the absolute value */ |
| 179 |
local s = spellNumber(-n); |
| 180 |
|
| 181 |
/* if it has any letters, use "minus", otherwise "-" */ |
| 182 |
return (rexSearch('<alpha>', s) != nil ? 'minus ' : '-') + s; |
| 183 |
} |
| 184 |
|
| 185 |
/* spell anything less than 100 */ |
| 186 |
if (n < 100) |
| 187 |
{ |
| 188 |
if (n < 20) |
| 189 |
return ['zero', 'one', 'two', 'three', 'four', 'five', 'six', |
| 190 |
'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', |
| 191 |
'thirteen', 'fourteen', 'fifteen', 'sixteen', |
| 192 |
'seventeen', 'eighteen', 'nineteen'][n+1]; |
| 193 |
else |
| 194 |
return ['twenty', 'thirty', 'forty', 'fifty', 'sixty', |
| 195 |
'seventy', 'eighty', 'ninety'][n/10-1] |
| 196 |
+ ['', '-one', '-two', '-three', '-four', '-five', '-six', |
| 197 |
'-seven', '-eight', '-nine'][n%10 + 1]; |
| 198 |
} |
| 199 |
|
| 200 |
/* |
| 201 |
* if it's a small (<100) multiple of 100, 1,000, 1,000,000, or |
| 202 |
* 1,000,000,000, spell it out |
| 203 |
*/ |
| 204 |
if (n % 1000000000 == 0 && n/1000000000 < 100) |
| 205 |
return '<<spellNumber(n/1000000000)>> billion'; |
| 206 |
if (n % 1000000 == 0 && n/1000000 < 100) |
| 207 |
return '<<spellNumber(n/1000000)>> million'; |
| 208 |
if (n % 1000 == 0 && n/1000 < 100) |
| 209 |
return '<<spellNumber(n/1000)>> thousand'; |
| 210 |
if (n % 100 == 0 && n/100 < 100) |
| 211 |
return '<<spellNumber(n/100)>> hundred'; |
| 212 |
|
| 213 |
/* |
| 214 |
* check to see if it can be expressed as a number of millions or |
| 215 |
* billions with one digit after the decimal place ("7.8 million") |
| 216 |
*/ |
| 217 |
if (n % 1000000000 == 0 && n/1000000000 < 1000) |
| 218 |
return '<<n/1000000000>> billion'; |
| 219 |
if (n % 100000000 == 0 && n/1000000000 < 1000) |
| 220 |
return '<<n/1000000000>><<dot>><<n%1000000000 / 100000000>> billion'; |
| 221 |
if (n % 1000000 == 0 && n/1000000 < 1000) |
| 222 |
return '<<n/1000000>> million'; |
| 223 |
if (n % 100000 == 0 && n/1000000 < 1000) |
| 224 |
return '<<n/1000000>><<dot>><<n%1000000 / 100000>> million'; |
| 225 |
|
| 226 |
/* convert to digits */ |
| 227 |
local s = toString(n); |
| 228 |
|
| 229 |
/* insert commas at the thousands */ |
| 230 |
for (local i = s.length() - 2 ; i > 1 ; i -= 3) |
| 231 |
s = s.splice(i, 0, comma); |
| 232 |
|
| 233 |
/* return the result */ |
| 234 |
return s; |
| 235 |
} |