| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
#include <tads.h> |
| 4 |
#include <tadsnet.h> |
| 5 |
|
| 6 |
/* |
| 7 |
* Copyright (c) 1999, 2006 Michael J. Roberts |
| 8 |
* |
| 9 |
* This file is part of TADS 3 |
| 10 |
* |
| 11 |
* This file defines classes and properties used with the tads-net |
| 12 |
* intrinsic function set. If you're using this function set, you should |
| 13 |
* include this source file in your build by adding it to your project |
| 14 |
* makefile. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* include the tads-net intrinsic function set interface definition */ |
| 18 |
#include <tadsnet.h> |
| 19 |
|
| 20 |
/* ------------------------------------------------------------------------ */ |
| 21 |
/* |
| 22 |
* A NetEvent instance describes an event read via the getNetEvent() |
| 23 |
* function. |
| 24 |
* |
| 25 |
* In most cases, this base class will not be instantiated directly. |
| 26 |
* getNetEvent() will always construct the appropriate subclass for the |
| 27 |
* specific type of event being generated, if that subclass is defined in |
| 28 |
* the game program. However, it's possible that the game won't define |
| 29 |
* all necessary subclasses. For example, a game written for version 1 |
| 30 |
* of the networking package wouldn't include new subclasses added in |
| 31 |
* version 2, because those subclasses weren't defined at the time the |
| 32 |
* game was written. When getNetEvent() needs to instantiate a subclass |
| 33 |
* that isn't defined in the game program, it will instead create a base |
| 34 |
* NetEvent object, which will simply store the subclass-specific |
| 35 |
* arguments as a list. This could be useful for debugging purposes, |
| 36 |
* because it will at least let the programmer inspect the event details |
| 37 |
* with the interactive debugger. |
| 38 |
*/ |
| 39 |
class NetEvent: object |
| 40 |
/* |
| 41 |
* The event type. This is a NetEvXxx value (see tadsnet.h) |
| 42 |
* indicating which type of event this is. |
| 43 |
*/ |
| 44 |
evType = nil |
| 45 |
|
| 46 |
/* |
| 47 |
* Construction. getNetEvent() only constructs this object directly |
| 48 |
* when the subclass it's looking for isn't defined in the game |
| 49 |
* program. |
| 50 |
*/ |
| 51 |
construct(t, [args]) |
| 52 |
{ |
| 53 |
evType = t; |
| 54 |
evArgs = args; |
| 55 |
} |
| 56 |
|
| 57 |
/* |
| 58 |
* Extra event-specific arguments. This is primarily for debugging |
| 59 |
* purposes, since it's only used when getNetEvent() needs to |
| 60 |
* construct a NetEvent subclass that isn't defined in the game. In |
| 61 |
* this case, the absence of a subclass definition in the game |
| 62 |
* presumably means that the game isn't written to handle the type of |
| 63 |
* event generated (for example, because it was written for an older |
| 64 |
* interpreter version that didn't have the event type). |
| 65 |
*/ |
| 66 |
evArgs = nil |
| 67 |
; |
| 68 |
|
| 69 |
/* |
| 70 |
* Network Request Event. This type of event occurs when a server (such |
| 71 |
* as an HTTPServer object) receives a request from a network client. |
| 72 |
* |
| 73 |
* The evRequest member contains a request object describing the network |
| 74 |
* request. The class of this object depends on the type of server that |
| 75 |
* received the request. For example, for an HTTP server, this will be |
| 76 |
* an HTTPRequest object. To reply to the request, use the appropriate |
| 77 |
* method(s) in the request object - for details, see the specific |
| 78 |
* request classes for the server types you create in your program. |
| 79 |
*/ |
| 80 |
class NetRequestEvent: NetEvent |
| 81 |
/* construction */ |
| 82 |
construct(t, req) |
| 83 |
{ |
| 84 |
inherited(t, req); |
| 85 |
evRequest = req; |
| 86 |
} |
| 87 |
|
| 88 |
evType = NetEvRequest |
| 89 |
|
| 90 |
/* |
| 91 |
* The request object. When the event type is NetEvRequest, this |
| 92 |
* contains a request object describing the request. The class of |
| 93 |
* the request object varies according to the server type; you can |
| 94 |
* use ofKind() to check which type of request it is. For example, |
| 95 |
* for an HTTP request, this will be an object of class HTTPRequest. |
| 96 |
*/ |
| 97 |
evRequest = nil |
| 98 |
; |
| 99 |
|
| 100 |
/* |
| 101 |
* Network Timeout Event. getNetEvent() returns this type of event when |
| 102 |
* the timeout interval expires before any actual event occurs. |
| 103 |
*/ |
| 104 |
class NetTimeoutEvent: NetEvent |
| 105 |
evType = NetEvTimeout |
| 106 |
; |
| 107 |
|
| 108 |
/* |
| 109 |
* Network Reply event. This type of event occurs when we receive a |
| 110 |
* reply to a network request made with sendNetRequest(). |
| 111 |
*/ |
| 112 |
class NetReplyEvent: NetEvent |
| 113 |
/* construction */ |
| 114 |
construct(t, id, status, body, headers, loc) |
| 115 |
{ |
| 116 |
inherited(t, id, body, headers, loc); |
| 117 |
statusCode = status; |
| 118 |
requestID = id; |
| 119 |
replyBody = body; |
| 120 |
replyHeadersRaw = headers; |
| 121 |
redirectLoc = loc; |
| 122 |
|
| 123 |
/* parse the headers into a lookup table keyed by header name */ |
| 124 |
if (headers != nil) |
| 125 |
{ |
| 126 |
/* create the lookup table */ |
| 127 |
local ht = replyHeaders = new LookupTable(); |
| 128 |
|
| 129 |
/* split the headers at the CR-LF separators */ |
| 130 |
headers = headers.split('\r\n'); |
| 131 |
|
| 132 |
/* the first line of the headers is actually the HTTP status */ |
| 133 |
if (headers.length() > 1) |
| 134 |
{ |
| 135 |
/* save the status line */ |
| 136 |
httpStatusLine = headers[1]; |
| 137 |
headers = headers.sublist(2); |
| 138 |
} |
| 139 |
|
| 140 |
/* process the rest of the headers */ |
| 141 |
for (local h in headers) |
| 142 |
{ |
| 143 |
/* split the header at the ":", and trim spaces */ |
| 144 |
h = h.split(':', 2).mapAll( |
| 145 |
{ s: rexReplace('^<space>+|<space>+$', s, '') }); |
| 146 |
|
| 147 |
/* |
| 148 |
* If it looks like a header, add it to the table. If |
| 149 |
* the header is repeated, append it to the previous |
| 150 |
* value with a comma delimiter. |
| 151 |
*/ |
| 152 |
if (h.length() == 2) |
| 153 |
{ |
| 154 |
local name = h[1].toLower(), val = h[2]; |
| 155 |
if (ht.isKeyPresent(name)) |
| 156 |
val = '<<ht[name]>>, <<val>>'; |
| 157 |
ht[name] = val; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/* our default event type is NetEvReply */ |
| 164 |
evType = NetEvReply |
| 165 |
|
| 166 |
/* |
| 167 |
* The request identifier. This is the ID value provided by the |
| 168 |
* caller in the call to sendNetRequest(), so that the caller can |
| 169 |
* relate the reply back to the corresponding request. |
| 170 |
*/ |
| 171 |
requestID = nil |
| 172 |
|
| 173 |
/* |
| 174 |
* The network status code. This is an integer value indicating |
| 175 |
* whether the request was successful or failed with an error. A |
| 176 |
* negative value is a low-level TADS error indicating that the |
| 177 |
* request couldn't be sent to the server, or that a network error |
| 178 |
* occurred receiving the reply: |
| 179 |
* |
| 180 |
*. -1 - out of memory |
| 181 |
*. -2 - couldn't connect to host |
| 182 |
*. -3 - other network/socket error |
| 183 |
*. -4 - invalid parameters |
| 184 |
*. -5 - error reading the content data to send to the server |
| 185 |
*. -6 - error saving the reply data received from the server |
| 186 |
*. -7 - error retrieving reply headers |
| 187 |
*. -8 - error starting background thread |
| 188 |
*. -100 - other TADS/network error |
| 189 |
* |
| 190 |
* A positive value means that the network transaction itself was |
| 191 |
* successful, and reflects the status information returned by the |
| 192 |
* network server that handled the request. This must be interpreted |
| 193 |
* according to the protocol used to send the request: |
| 194 |
* |
| 195 |
* - For HTTP requests, the value is an HTTP status code. A code in |
| 196 |
* the 200 range generally indicates success, while other ranges |
| 197 |
* generally indicate errors. |
| 198 |
*/ |
| 199 |
statusCode = nil |
| 200 |
|
| 201 |
/* the content body from the reply */ |
| 202 |
replyBody = nil |
| 203 |
|
| 204 |
/* |
| 205 |
* the HTTP headers from the reply, as a lookup table indexed by |
| 206 |
* header name |
| 207 |
*/ |
| 208 |
replyHeaders = nil |
| 209 |
|
| 210 |
/* the HTTP status string (the first line of the headers) */ |
| 211 |
httpStatusLine = nil |
| 212 |
|
| 213 |
/* |
| 214 |
* the HTTP headers from the reply, in the raw text format - this is |
| 215 |
* simply a string of all the headers, separated by CR-LF (\r\n) |
| 216 |
* sequences |
| 217 |
*/ |
| 218 |
replyHeadersRaw = nil |
| 219 |
|
| 220 |
/* |
| 221 |
* Redirect location, if applicable. By default, this will be nil |
| 222 |
* whether or not a redirection took place, because sendNetRequest() |
| 223 |
* normally follows redirection links transparently, returning only |
| 224 |
* the final result from the final server we're redirected to. |
| 225 |
* However, you can override automatic redirection with an option |
| 226 |
* flag (NetReqNoRedirect) when calling sendNetRequest(). When that |
| 227 |
* option is selected, the function won't follow redirection links at |
| 228 |
* all, but will instead simply return the redirect information as |
| 229 |
* the result from the request. When that happens, this property is |
| 230 |
* set to a string giving the target of the redirect. You can then |
| 231 |
* follow the redirect manually, if desired, by sending a new request |
| 232 |
* to the target given here. |
| 233 |
*/ |
| 234 |
redirectLoc = nil |
| 235 |
; |
| 236 |
|
| 237 |
/* |
| 238 |
* Network Reply Done event. This type of event occurs when an |
| 239 |
* asynchronous network reply (such as HTTPRequest.sendReplyAsync()) |
| 240 |
* completes. |
| 241 |
*/ |
| 242 |
class NetReplyDoneEvent: NetEvent |
| 243 |
/* construction */ |
| 244 |
construct(t, req, err, msg) |
| 245 |
{ |
| 246 |
inherited(t, req, err, msg); |
| 247 |
requestObj = req; |
| 248 |
socketErr = err; |
| 249 |
errMsg = msg; |
| 250 |
} |
| 251 |
|
| 252 |
/* our default event type is NetEvReplyDone */ |
| 253 |
evType = NetEvReplyDone |
| 254 |
|
| 255 |
/* |
| 256 |
* The object representing the request we replied to. For HTTP |
| 257 |
* requests, this is an HTTPRequest object. |
| 258 |
*/ |
| 259 |
requestObj = nil |
| 260 |
|
| 261 |
/* was the reply successfully sent? */ |
| 262 |
isSuccessful() { return errMsg == nil; } |
| 263 |
|
| 264 |
/* |
| 265 |
* The socket error, if any. If the reply failed due to a network |
| 266 |
* error, this contains the error number. If no network error |
| 267 |
* occurred, this is zero. |
| 268 |
*/ |
| 269 |
socketErr = 0 |
| 270 |
|
| 271 |
/* |
| 272 |
* Error message, if any. If the reply failed, this contains a |
| 273 |
* string with a description of the error that occurred. If the |
| 274 |
* reply was sent successfully, this is nil. |
| 275 |
*/ |
| 276 |
errMsg = nil |
| 277 |
; |
| 278 |
|
| 279 |
/* ------------------------------------------------------------------------ */ |
| 280 |
/* |
| 281 |
* A FileUpload represents a file uploaded by a network client via a |
| 282 |
* protocol server, such as an HTTPServer. |
| 283 |
* |
| 284 |
* When your program is acting as a network server, a FileUpload object |
| 285 |
* represents a file received from the client. For example, |
| 286 |
* HTTPRequest.getFormFields() returns a FileUpload object to represent |
| 287 |
* each <INPUT TYPE=FILE> field in the posted form. |
| 288 |
* |
| 289 |
* When your program acts as a network client (via sendNetRequest), you |
| 290 |
* can create use FileUpload to post file attachments to posted forms. |
| 291 |
*/ |
| 292 |
class FileUpload: object |
| 293 |
construct(file, contentType, filename) |
| 294 |
{ |
| 295 |
self.file = file; |
| 296 |
self.contentType = contentType; |
| 297 |
self.filename = filename; |
| 298 |
} |
| 299 |
|
| 300 |
/* |
| 301 |
* The file data. |
| 302 |
* |
| 303 |
* When you create the FileUpload object for use with |
| 304 |
* sendNetRequest() to post form data, you must use a string or |
| 305 |
* ByteArray value for this property. |
| 306 |
* |
| 307 |
* When the FileUpload is created by HTTPRequest.getFormFields(), |
| 308 |
* this property contains a File object with the uploaded content. |
| 309 |
* This is open for read-only access. If the contentType parameter |
| 310 |
* is a text type ("text/html", "text/plain", etc), and the |
| 311 |
* interpreter recognizes the character set parameter in the |
| 312 |
* contentType, the file is in Text mode (FileModeText) with the |
| 313 |
* appropriate character mapper in effect. Otherwise, the file is in |
| 314 |
* raw binary mode (FileModeRaw). If you need the file to be opened |
| 315 |
* in a different mode, you can use setFileMode() on the file to |
| 316 |
* change the mode. |
| 317 |
*/ |
| 318 |
file = nil |
| 319 |
|
| 320 |
/* |
| 321 |
* The content type. This a string giving the MIME type specified by |
| 322 |
* the client with the upload. This is the full content-type string, |
| 323 |
* including any attributes, such "charset" for a text type. This |
| 324 |
* can be nil if the client doesn't specify a content-type at all. |
| 325 |
* |
| 326 |
* It's important to recognize that this information is supplied by |
| 327 |
* the client, and is NOT validated by the protocol server. At best |
| 328 |
* you should consider it a suggestion, and at worst a malicious lie. |
| 329 |
* The client could be innocently mistaken about the type, or could |
| 330 |
* even be intentionally misrepresenting it. You should always |
| 331 |
* validate the actual contents, rather than relying on the client's |
| 332 |
* description of the format; in particular, be careful not to assume |
| 333 |
* that expected data fields are present, in the valid range, etc. |
| 334 |
*/ |
| 335 |
contentType = nil |
| 336 |
|
| 337 |
/* |
| 338 |
* The client-side filename, if specified. This is a string giving |
| 339 |
* the name of the file on the client machine. This generally has no |
| 340 |
* particular meaning to the server, since we can't infer anything |
| 341 |
* about the directory structure or naming conventions on an |
| 342 |
* arbitrary client. However, this might be useful for reference, |
| 343 |
* such as showing information about the upload in a user interface. |
| 344 |
* It's sometimes also marginally useful to know the suffix |
| 345 |
* (extension) for making further guesses about the content type - |
| 346 |
* although as with the content-type, you can't rely upon this, but |
| 347 |
* can only use it as a suggestion from the client. |
| 348 |
* |
| 349 |
* The client won't necessarily specify a filename at all, in which |
| 350 |
* case this will be nil. |
| 351 |
*/ |
| 352 |
filename = nil |
| 353 |
; |
| 354 |
|
| 355 |
|
| 356 |
/* ------------------------------------------------------------------------ */ |
| 357 |
/* |
| 358 |
* A NetException is the base class for network errors. |
| 359 |
*/ |
| 360 |
class NetException: Exception |
| 361 |
construct(msg?, errno?) |
| 362 |
{ |
| 363 |
if (errMsg != nil) |
| 364 |
errMsg = 'Network error: <<msg>>'; |
| 365 |
if (errno != nil) |
| 366 |
errMsg += ' (system error code <<errno>>)'; |
| 367 |
} |
| 368 |
displayException() { "<<errMsg>>"; } |
| 369 |
|
| 370 |
/* a descriptive error message provided by the system */ |
| 371 |
errMsg = 'Network error' |
| 372 |
; |
| 373 |
|
| 374 |
/* |
| 375 |
* A NetSafetyException is thrown when the program attempts to perform a |
| 376 |
* network operation that isn't allowed by the current network safety |
| 377 |
* level settings. The user controls the safety level; the program can't |
| 378 |
* override this. |
| 379 |
*/ |
| 380 |
class NetSafetyException: NetException |
| 381 |
errMsg = 'Network operation prohibited by user-specified ' |
| 382 |
+ 'network safety level' |
| 383 |
; |
| 384 |
|
| 385 |
/* |
| 386 |
* A SocketDisconnectException is thrown when attempting to read or write |
| 387 |
* a network socket that's been closed, either by us or by the peer (the |
| 388 |
* computer on the other end of the network connection). If we didn't |
| 389 |
* close the socket on this side, this error usually means simply that |
| 390 |
* the peer program has terminated or otherwise disconnected, so we |
| 391 |
* should consider the conversation terminated. |
| 392 |
*/ |
| 393 |
class SocketDisconnectException: NetException |
| 394 |
errMsg = 'Network socket disconnected by peer or closed' |
| 395 |
; |
| 396 |
|
| 397 |
/* export the objects and properties used in the tads-net function set */ |
| 398 |
export NetEvent 'TadsNet.NetEvent'; |
| 399 |
export NetRequestEvent 'TadsNet.NetRequestEvent'; |
| 400 |
export NetTimeoutEvent 'TadsNet.NetTimeoutEvent'; |
| 401 |
export NetReplyEvent 'TadsNet.NetReplyEvent'; |
| 402 |
export NetReplyDoneEvent 'TadsNet.NetReplyDoneEvent'; |
| 403 |
export NetException 'TadsNet.NetException'; |
| 404 |
export SocketDisconnectException 'TadsNet.SocketDisconnectException'; |
| 405 |
export NetSafetyException 'TadsNet.NetSafetyException'; |
| 406 |
export FileUpload 'TadsNet.FileUpload'; |
| 407 |
export file 'TadsNet.FileUpload.file'; |
| 408 |
export contentType 'TadsNet.FileUpload.contentType'; |
| 409 |
export filename 'TadsNet.FileUpload.filename'; |
| 410 |
|