Debian Codegraph @

terps/tads/tads3/vmhttpreq.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 2010 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmhttpreq.h - CVmObjHTTPRequest object
12 Function
13 CVmObjHTTPRequest is the TADS intrinsic class wrapper for our native
14 (C++) object TadsHttpRequest. The native object is part of the vmnet
15 layer, which uses reference counting to manage memory. This VM object
16 wrapper brings the object into the VM's garbage collection system, and also
17 provides method access to the byte-code program.
18
19 The native network layer's HTTP server creates the C++ TadsHttpRequest
20 object when a request comes in from a network client. The request object
21 goes into the message queue, which the main game thread reads via the
22 getNetEvent() intrinsic function. When that function pulls a request
23 object off the queue, it synthesizes the appropriate wrapper to return to
24 the byte-code program. That's when CVmObjHTTPRequest is instatiated. The
25 wrapper then keeps track of the VM's reference on the underlying request
26 object, so that the request object stays in memory as long as the VM has a
27 reference. Once the VM wrapper goes out of scope and is collected by the
28 garbage collector, the wrapper releases its reference on the underlying C++
29 object.
30 Notes
31
32 Modified
33 MJRoberts - Creation
34 */
35
36 #ifndef VMHTTPREQ_H
37 #define VMHTTPREQ_H
38
39 #include "t3std.h"
40 #include "vmtype.h"
41 #include "vmglob.h"
42 #include "vmobj.h"
43 #include "vmundo.h"
44
45
46 /* ------------------------------------------------------------------------ */
47 /*
48 * Image file data: this intrinsic class is inherently transient, so it's
49 * not stored in an image file. We thus don't need any image file data
50 * structure.
51 */
52
53
54 /* ------------------------------------------------------------------------ */
55 /*
56 * Our in-memory extension data structure. This is usually a 'struct'
57 * that contains the same information as in the image file, but using
58 * native types.
59 */
60 struct vm_httpreq_ext
61 {
62 /* allocate the structure */
63 static vm_httpreq_ext *alloc_ext(VMG_ class CVmObjHTTPRequest *self);
64
65 /* the network message object */
66 class TadsHttpRequest *req;
67
68 /* list of cookies to be sent with the reply */
69 struct vm_httpreq_cookie *cookies;
70
71 /* the HTTPServer object for the server that created the request */
72 vm_obj_id_t server;
73 };
74
75 /*
76 * Cookie tracker
77 */
78 struct vm_httpreq_cookie
79 {
80 vm_httpreq_cookie(const char *name, size_t namelen,
81 const char *val, size_t vallen)
82 {
83 this->name = lib_copy_str(name, namelen);
84 this->val = lib_copy_str(val, vallen);
85 nxt = 0;
86 }
87
88 ~vm_httpreq_cookie()
89 {
90 lib_free_str(name);
91 lib_free_str(val);
92 if (nxt != 0)
93 delete nxt;
94 }
95
96 /* clone this cookie and our list tail */
97 vm_httpreq_cookie *clone()
98 {
99 /* clone myself */
100 vm_httpreq_cookie *c = new vm_httpreq_cookie(
101 name, strlen(name), val, val != 0 ? strlen(val) : 0);
102
103 /* clone my list tail */
104 if (nxt != 0)
105 c->nxt = nxt->clone();
106
107 /* return the clone */
108 return c;
109 }
110
111 /* send the cookie; returns true on success, false on failure */
112 int send(class TadsServerThread *t);
113
114 /* name of the cookie */
115 char *name;
116
117 /* value (including expiration, path, domain, etc) */
118 char *val;
119
120 /* next in list */
121 vm_httpreq_cookie *nxt;
122 };
123
124
125 /* ------------------------------------------------------------------------ */
126 /*
127 * CVmObjHTTPRequest intrinsic class definition
128 */
129
130 class CVmObjHTTPRequest: public CVmObject
131 {
132 friend class CVmMetaclassHTTPRequest;
133
134 public:
135 /* metaclass registration object */
136 static class CVmMetaclass *metaclass_reg_;
137 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
138
139 /* am I of the given metaclass? */
140 virtual int is_of_metaclass(class CVmMetaclass *meta) const
141 {
142 /* try my own metaclass and my base class */
143 return (meta == metaclass_reg_
144 || CVmObject::is_of_metaclass(meta));
145 }
146
147 /* is this a CVmObjHTTPRequest object? */
148 static int is_vmhttpreq_obj(VMG_ vm_obj_id_t obj)
149 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
150
151 /*
152 * Create the request object. 'srv_obj' is the HTTPServer object for
153 * the listener that received the request.
154 */
155 static vm_obj_id_t create(VMG_ int in_root_set,
156 class TadsHttpRequest *req,
157 vm_obj_id_t srv_obj);
158
159 /* create dynamically using stack arguments */
160 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
161 uint argc);
162
163 /*
164 * call a static property - we don't have any of our own, so simply
165 * "inherit" the base class handling
166 */
167 static int call_stat_prop(VMG_ vm_val_t *result,
168 const uchar **pc_ptr, uint *argc,
169 vm_prop_id_t prop)
170 {
171 return CVmObject::
172 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
173 }
174
175 /* notify of deletion */
176 void notify_delete(VMG_ int in_root_set);
177
178 /* set a property */
179 void set_prop(VMG_ class CVmUndo *undo,
180 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
181
182 /* get a property */
183 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
184 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
185
186 /*
187 * receive savepoint notification - we don't keep any
188 * savepoint-relative records, so we don't need to do anything here
189 */
190 void notify_new_savept() { }
191
192 /* we don't participate in undo */
193 void apply_undo(VMG_ struct CVmUndoRecord *) { }
194 void discard_undo(VMG_ struct CVmUndoRecord *) { }
195 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
196
197 /* mark references */
198 void mark_refs(VMG_ uint state);
199
200 /* we don't have any weak references */
201 void remove_stale_weak_refs(VMG0_) { }
202 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
203
204 /* load from an image file */
205 void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
206
207 /* reload from an image file */
208 void reload_from_image(VMG_ vm_obj_id_t self,
209 const char *ptr, size_t siz);
210
211 /* rebuild for image file */
212 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
213
214 /* save to a file */
215 void save_to_file(VMG_ class CVmFile *fp);
216
217 /* restore from a file */
218 void restore_from_file(VMG_ vm_obj_id_t self,
219 class CVmFile *fp, class CVmObjFixup *fixups);
220
221 /*
222 * determine if we've been changed since loading - assume we have (if
223 * we haven't, the only harm is the cost of unnecessarily reloading or
224 * saving)
225 */
226 int is_changed_since_load() const { return TRUE; }
227
228 protected:
229 /* get my extension data */
230 vm_httpreq_ext *get_ext() const { return (vm_httpreq_ext *)ext_; }
231
232 /* load or reload image data */
233 void load_image_data(VMG_ const char *ptr, size_t siz);
234
235 /* create a with no initial contents */
236 CVmObjHTTPRequest() { ext_ = 0; }
237
238 /* create with contents ('flag' is just for overloading resolution) */
239 CVmObjHTTPRequest(VMG_ int flag);
240
241 /* property evaluator - undefined function */
242 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
243
244 /* get the server object */
245 int getp_getServer(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
246
247 /* get the HTTP verb */
248 int getp_getVerb(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
249
250 /* get the query string */
251 int getp_getQuery(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
252
253 /* parse the query string into the resource name and parameters */
254 int getp_parseQuery(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
255
256 /* parse the query string and find a given parameter by name */
257 int getp_getQueryParam(VMG_ vm_obj_id_t self,
258 vm_val_t *retval, uint *argc);
259
260 /* get the request headers */
261 int getp_getHeaders(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
262
263 /* get a single cookie */
264 int getp_getCookie(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
265
266 /* get the cookies */
267 int getp_getCookies(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
268
269 /* set a cookie for the reply */
270 int getp_setCookie(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
271
272 /* get the cookies */
273 int getp_getFormFields(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
274
275 /* get the request message body */
276 int getp_getBody(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
277
278 /* get the client network address */
279 int getp_getClientAddress(
280 VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
281
282 /* send a reply */
283 int getp_sendReply(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
284
285 /* common handler for sendReply and sendReplyAsync */
286 int common_sendReply(
287 VMG_ vm_obj_id_t self, vm_val_t *retval, uint *oargc, int wait);
288
289 /* start a chunked reply */
290 int getp_startChunkedReply(VMG_ vm_obj_id_t self, vm_val_t *retval,
291 uint *argc);
292
293 /* send a piece of a chunked reply */
294 int getp_sendReplyChunk(VMG_ vm_obj_id_t self, vm_val_t *retval,
295 uint *argc);
296
297 /* finish a chunked reply */
298 int getp_endChunkedReply(VMG_ vm_obj_id_t self, vm_val_t *retval,
299 uint *argc);
300
301 /* send a reply asynchronously */
302 int getp_sendReplyAsync(
303 VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
304
305 /* property evaluation function table */
306 static int (CVmObjHTTPRequest::*func_table_[])(VMG_ vm_obj_id_t self,
307 vm_val_t *retval, uint *argc);
308 };
309
310
311 /* ------------------------------------------------------------------------ */
312 /*
313 * CVmObjHTTPRequest metaclass registration table object
314 */
315 class CVmMetaclassHTTPRequest: public CVmMetaclass
316 {
317 public:
318 /* get the global name */
319 const char *get_meta_name() const { return "http-request/030001"; }
320
321 /* create from image file */
322 void create_for_image_load(VMG_ vm_obj_id_t id)
323 {
324 new (vmg_ id) CVmObjHTTPRequest();
325 G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE);
326 G_obj_table->set_obj_transient(id);
327 }
328
329 /* create from restoring from saved state */
330 void create_for_restore(VMG_ vm_obj_id_t id)
331 {
332 new (vmg_ id) CVmObjHTTPRequest();
333 G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE);
334 G_obj_table->set_obj_transient(id);
335 }
336
337 /* create dynamically using stack arguments */
338 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
339 { return CVmObjHTTPRequest::create_from_stack(vmg_ pc_ptr, argc); }
340
341 /* call a static property */
342 int call_stat_prop(VMG_ vm_val_t *result,
343 const uchar **pc_ptr, uint *argc,
344 vm_prop_id_t prop)
345 {
346 return CVmObjHTTPRequest::
347 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
348 }
349 };
350
351 #endif /* VMHTTPREQ_H */
352
353 /*
354 * Register the class
355 */
356 VM_REGISTER_METACLASS(CVmObjHTTPRequest)