Debian Codegraph @

terps/tads/tads3/vmhttpsrv.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header$";
4 #endif
5
6 /*
7 * Copyright (c) <year> Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 vmhttpsrv.cpp - CVmObjHTTPServer object
15 Function
16
17 Notes
18
19 Modified
20 04/22/10 MJRoberts - Creation
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
26
27 #include "t3std.h"
28 #include "vmhttpsrv.h"
29 #include "vmerr.h"
30 #include "vmerrnum.h"
31 #include "vmtype.h"
32 #include "vmglob.h"
33 #include "vmobj.h"
34 #include "vmundo.h"
35 #include "vmmcreg.h"
36 #include "vmfile.h"
37 #include "vmbif.h"
38 #include "vmmeta.h"
39 #include "vmstack.h"
40 #include "vmrun.h"
41 #include "vmstr.h"
42 #include "utf8.h"
43 #include "vmimport.h"
44 #include "vmpredef.h"
45 #include "vmhost.h"
46
47
48 /* ------------------------------------------------------------------------ */
49 /*
50 * Allocate an extension structure
51 */
52 vm_httpsrv_ext *vm_httpsrv_ext::alloc_ext(
53 VMG_ CVmObjHTTPServer *self)
54 {
55 /* calculate how much space we need */
56 size_t siz = sizeof(vm_httpsrv_ext);
57
58 /* allocate the memory */
59 vm_httpsrv_ext *ext = (vm_httpsrv_ext *)
60 G_mem->get_var_heap()->alloc_mem(siz, self);
61
62 /* we don't have a listener or binding address yet */
63 ext->l = 0;
64 ext->addr = 0;
65
66 /* return the new extension */
67 return ext;
68 }
69
70 /* ------------------------------------------------------------------------ */
71 /*
72 * CVmObjHTTPServer object statics
73 */
74
75 /* metaclass registration object */
76 static CVmMetaclassHTTPServer metaclass_reg_obj;
77 CVmMetaclass *CVmObjHTTPServer::metaclass_reg_ = &metaclass_reg_obj;
78
79 /* function table */
80 int (CVmObjHTTPServer::*CVmObjHTTPServer::func_table_[])(
81 VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc) =
82 {
83 &CVmObjHTTPServer::getp_undef,
84 &CVmObjHTTPServer::getp_shutdown,
85 &CVmObjHTTPServer::getp_get_addr,
86 &CVmObjHTTPServer::getp_get_ip,
87 &CVmObjHTTPServer::getp_get_port
88 };
89
90 /* ------------------------------------------------------------------------ */
91 /*
92 * CVmObjHTTPServer intrinsic class implementation
93 */
94
95 /*
96 * construction
97 */
98 CVmObjHTTPServer::CVmObjHTTPServer(VMG_ int)
99 {
100 /* allocate our extension structure */
101 ext_ = (char *)vm_httpsrv_ext::alloc_ext(vmg_ this);
102 }
103
104 /*
105 * create dynamically using stack arguments
106 */
107 vm_obj_id_t CVmObjHTTPServer::create_from_stack(
108 VMG_ const uchar **pc_ptr, uint argc)
109 {
110 vm_obj_id_t id;
111 const char *errmsg;
112
113 /*
114 * parse arguments - new HTTPServer(addr?, port?)
115 */
116 if (argc > 3)
117 err_throw(VMERR_WRONG_NUM_OF_ARGS);
118
119 /* check for the address - use "localhost" by default */
120 char addr[256] = "localhost";
121 if (argc >= 1)
122 {
123 /* get the address - this must be a string or nil */
124 if (G_stk->get(0)->typ == VM_NIL)
125 G_stk->discard();
126 else
127 CVmBif::pop_str_val_buf(vmg_ addr, sizeof(addr));
128 }
129
130 /* check for a port - use 0 by default */
131 int port = 0;
132 if (argc >= 2)
133 {
134 /* get the port number - this must be an integer or nil */
135 if (G_stk->get(0)->typ == VM_NIL)
136 G_stk->discard();
137 else
138 port = CVmBif::pop_int_val(vmg0_);
139 }
140
141 /* check for an upload size limit */
142 int32_t ulim;
143 if (argc >= 3)
144 {
145 if (G_stk->get(0)->typ == VM_NIL)
146 G_stk->discard();
147 else
148 ulim = CVmBif::pop_long_val(vmg0_);
149 }
150
151 /* get the network safety level, to determine if this is allowed */
152 int client_level, server_level;
153 G_host_ifc->get_net_safety(&client_level, &server_level);
154
155 /* check the network server safety level */
156 switch (server_level)
157 {
158 case VM_NET_SAFETY_MINIMUM:
159 /* all access allowed - proceed */
160 break;
161
162 case VM_NET_SAFETY_LOCALHOST:
163 /*
164 * localhost only - if the host isn't 'localhost' or '127.0.0.1',
165 * don't allow it
166 */
167 if (stricmp(addr, "localhost") != 0
168 && stricmp(addr, "127.0.0.1") != 0)
169 goto access_error;
170
171 /* allow it */
172 break;
173
174 case VM_NET_SAFETY_MAXIMUM:
175 access_error:
176 /* no access allowed */
177 errmsg = "prohibited network access";
178 G_interpreter->push_string(vmg_ errmsg);
179 G_interpreter->throw_new_class(vmg_ G_predef->net_safety_exception,
180 1, errmsg);
181 AFTER_ERR_THROW(break;)
182 }
183
184 /*
185 * allocate the object ID - this type of construction never creates a
186 * root object
187 */
188 id = vm_new_id(vmg_ FALSE, FALSE, FALSE);
189
190 /* http servers are inherently transient */
191 G_obj_table->set_obj_transient(id);
192
193 /* create the object */
194 CVmObjHTTPServer *l = new (vmg_ id) CVmObjHTTPServer(vmg_ TRUE);
195
196 /* get the extension */
197 vm_httpsrv_ext *ext = l->get_ext();
198
199 /* create the listener thread object */
200 TadsHttpListenerThread *ht = new TadsHttpListenerThread(
201 id, G_net_queue, ulim);
202
203 /* launch the listener */
204 ext->l = TadsListener::launch(addr, (ushort)port, ht);
205
206 /* we're done with the thread object (the listener took it over) */
207 ht->release_ref();
208
209 /* if the launch failed, throw an error */
210 if (ext->l == 0)
211 {
212 /* throw a network error */
213 const char *errmsg = "Unable to start HTTP server";
214 G_interpreter->push_string(vmg_ errmsg);
215 G_interpreter->throw_new_class(vmg_ G_predef->net_exception, 1, errmsg);
216 }
217
218 /* remember my requested binding address */
219 ext->addr = lib_copy_str(addr);
220
221 /* return the new ID */
222 return id;
223 }
224
225 /*
226 * notify of deletion
227 */
228 void CVmObjHTTPServer::notify_delete(VMG_ int /*in_root_set*/)
229 {
230 /* free our additional data, if we have any */
231 vm_httpsrv_ext *ext = get_ext();
232 if (ext != 0)
233 {
234 /* shut down the listener */
235 if (ext->l != 0)
236 {
237 /* tell it to terminate */
238 ext->l->shutdown();
239
240 /*
241 * Notify the listener thread that we're being deleted, so that
242 * it can drop its reference on us. The listener thread object
243 * keeps a reference on us, but it isn't itself a
244 * garbage-collected object, so its reference on us won't
245 * prevent us from being deleted. We therefore need to
246 * manually remove its reference on us when we're about to be
247 * deleted.
248 */
249 ((TadsHttpListenerThread *)ext->l->get_thread())
250 ->detach_server_obj();
251
252 /* we're done with the listener object */
253 ext->l->release_ref();
254 }
255
256 /* delete our address string */
257 lib_free_str(ext->addr);
258
259 /* delete the extension */
260 G_mem->get_var_heap()->free_mem(ext_);
261 }
262 }
263
264 /*
265 * set a property
266 */
267 void CVmObjHTTPServer::set_prop(VMG_ class CVmUndo *undo,
268 vm_obj_id_t self, vm_prop_id_t prop,
269 const vm_val_t *val)
270 {
271 /* no settable properties - throw an error */
272 err_throw(VMERR_INVALID_SETPROP);
273 }
274
275 /*
276 * get a property
277 */
278 int CVmObjHTTPServer::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval,
279 vm_obj_id_t self, vm_obj_id_t *source_obj,
280 uint *argc)
281 {
282 uint func_idx;
283
284 /* translate the property into a function vector index */
285 func_idx = G_meta_table
286 ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop);
287
288 /* call the appropriate function */
289 if ((this->*func_table_[func_idx])(vmg_ self, retval, argc))
290 {
291 *source_obj = metaclass_reg_->get_class_obj(vmg0_);
292 return TRUE;
293 }
294
295 /* inherit default handling from our base class */
296 return CVmObject::get_prop(vmg_ prop, retval, self, source_obj, argc);
297 }
298
299 /*
300 * load from an image file
301 */
302 void CVmObjHTTPServer::load_from_image(VMG_ vm_obj_id_t self,
303 const char *ptr, size_t siz)
304 {
305 /* load our image data */
306 load_image_data(vmg_ ptr, siz);
307
308 /*
309 * save our image data pointer in the object table, so that we can
310 * access it (without storing it ourselves) during a reload
311 */
312 G_obj_table->save_image_pointer(self, ptr, siz);
313 }
314
315 /*
316 * reload from the image file
317 */
318 void CVmObjHTTPServer::reload_from_image(VMG_ vm_obj_id_t self,
319 const char *ptr, size_t siz)
320 {
321 /* load our image data */
322 load_image_data(vmg_ ptr, siz);
323 }
324
325 /*
326 * load or reload data from the image
327 */
328 void CVmObjHTTPServer::load_image_data(VMG_ const char *ptr, size_t siz)
329 {
330 /* free our existing extension, if we have one */
331 notify_delete(vmg_ FALSE);
332
333 /* allocate the extension */
334 vm_httpsrv_ext *ext = vm_httpsrv_ext::alloc_ext(vmg_ this);
335 ext_ = (char *)ext;
336 }
337
338
339 /*
340 * save to a file
341 */
342 void CVmObjHTTPServer::save_to_file(VMG_ class CVmFile *fp)
343 {
344 /* we're inherently transient, so we should never do this */
345 assert(FALSE);
346 }
347
348 /*
349 * restore from a file
350 */
351 void CVmObjHTTPServer::restore_from_file(VMG_ vm_obj_id_t self,
352 CVmFile *fp, CVmObjFixup *fixups)
353 {
354 /* http servers are always transient, so we should never do this */
355 }
356
357 /*
358 * Shut down the server
359 */
360 int CVmObjHTTPServer::getp_shutdown(VMG_ vm_obj_id_t self,
361 vm_val_t *retval, uint *oargc)
362 {
363 uint argc = (oargc != 0 ? *oargc : 0);
364 static CVmNativeCodeDesc desc(0, 1);
365 if (get_prop_check_argc(retval, oargc, &desc))
366 return TRUE;
367
368 /* check arguments */
369 int wait = FALSE;
370 if (argc >= 1)
371 wait = CVmBif::pop_bool_val(vmg0_);
372
373 /* if we have a listener, tell it to shut down */
374 vm_httpsrv_ext *ext = get_ext();
375 if (ext != 0 && ext->l != 0)
376 {
377 /* add a reference on the listener while we're using it */
378 ext->l->add_ref();
379
380 /* shut down the server */
381 ext->l->shutdown();
382
383 /* if we're waiting, wait for the listener thread to exit */
384 if (wait)
385 ext->l->get_thread()->wait();
386
387 /*
388 * Return true if the thread is done, nil if it's still running.
389 * The main listener thread waits for all of its launched server
390 * threads to exit before it itself exits, so once the listener
391 * thread is done, we can be sure that all of its launched threads
392 * are done as well.
393 */
394 retval->set_logical(ext->l->get_thread()->test());
395
396 /* done with the listener */
397 ext->l->release_ref();
398 }
399 else
400 {
401 /* there is no server, so it's already shut down */
402 retval->set_true();
403 }
404
405 /* handled */
406 return TRUE;
407 }
408
409 /*
410 * Internal get-port interface
411 */
412 int CVmObjHTTPServer::get_listener_addr(
413 char *&addr, char *&ip, int &port) const
414 {
415 /* if we have a listener, get its port number */
416 vm_httpsrv_ext *ext = get_ext();
417 if (ext != 0 && ext->l != 0 && ext->addr != 0
418 && ext->l->get_thread()->get_local_addr(ip, port))
419 {
420 /* got the IP and port - also return the constructor address string */
421 addr = lib_copy_str(ext->addr);
422 return TRUE;
423 }
424 else
425 {
426 /* the address isn't available */
427 return FALSE;
428 }
429 }
430
431
432 /*
433 * Get the port number
434 */
435 int CVmObjHTTPServer::getp_get_port(VMG_ vm_obj_id_t self,
436 vm_val_t *retval, uint *oargc)
437 {
438 static CVmNativeCodeDesc desc(0);
439 if (get_prop_check_argc(retval, oargc, &desc))
440 return TRUE;
441
442 /* return the port number */
443 char *addr, *ip;
444 int port;
445 if (get_listener_addr(addr, ip, port))
446 {
447 retval->set_int(port);
448 lib_free_str(addr);
449 lib_free_str(ip);
450 }
451 else
452 retval->set_nil();
453
454 /* handled */
455 return TRUE;
456 }
457
458 /*
459 * Get the listener address
460 */
461 int CVmObjHTTPServer::getp_get_addr(VMG_ vm_obj_id_t self,
462 vm_val_t *retval, uint *oargc)
463 {
464 static CVmNativeCodeDesc desc(0);
465 if (get_prop_check_argc(retval, oargc, &desc))
466 return TRUE;
467
468 /* if we have an address string saved, return it */
469 vm_httpsrv_ext *ext = get_ext();
470 if (ext != 0 && ext->addr != 0)
471 {
472 /* got it - return the string value */
473 G_interpreter->push_string(vmg_ ext->addr);
474 G_stk->pop(retval);
475 }
476 else
477 {
478 /* no address value - return nil */
479 retval->set_nil();
480 }
481
482 /* handled */
483 return TRUE;
484 }
485
486 /*
487 * Get the listener IP address
488 */
489 int CVmObjHTTPServer::getp_get_ip(VMG_ vm_obj_id_t self,
490 vm_val_t *retval, uint *oargc)
491 {
492 static CVmNativeCodeDesc desc(0);
493 if (get_prop_check_argc(retval, oargc, &desc))
494 return TRUE;
495
496 /* return the IP address */
497 char *ip, *addr;
498 int port;
499 if (get_listener_addr(addr, ip, port))
500 {
501 /* got it - return the string value */
502 G_interpreter->push_string(vmg_ ip);
503 G_stk->pop(retval);
504
505 /* free the source strings */
506 lib_free_str(addr);
507 lib_free_str(ip);
508 }
509 else
510 {
511 /* no IP address - return nil */
512 retval->set_nil();
513 }
514
515 /* handled */
516 return TRUE;
517 }
518