/* * Mongoose event handler. If JavaScript callback was provided, call it */ static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) { struct user_data *ud = (struct user_data *) c->user_data; if (ev == MG_EV_HTTP_REQUEST) { /* HTTP request has arrived */ if (v7_is_callable(ud->v7, ud->handler)) { /* call provided JavaScript callback with `request` and `response` */ v7_val_t request = v7_mk_object(ud->v7); v7_own(ud->v7, &request); v7_val_t response = v7_mk_object(ud->v7); v7_own(ud->v7, &response); setup_request_object(ud->v7, request, ev_data); setup_response_object(ud->v7, response, c, request); sj_invoke_cb2_this(ud->v7, ud->handler, ud->obj, request, response); v7_disown(ud->v7, &request); v7_disown(ud->v7, &response); } else { /* * no JavaScript callback provided; serve the request with the default * options by `mg_serve_http()` */ struct mg_serve_http_opts opts; memset(&opts, 0, sizeof(opts)); mg_serve_http(c, ev_data, opts); } } else if (ev == MG_EV_HTTP_REPLY) { /* HTTP response has arrived */ /* if JavaScript callback was provided, call it with `response` */ if (v7_is_callable(ud->v7, ud->handler)) { v7_val_t response = v7_mk_object(ud->v7); v7_own(ud->v7, &response); setup_request_object(ud->v7, response, ev_data); sj_invoke_cb1_this(ud->v7, ud->handler, ud->obj, response); v7_disown(ud->v7, &response); } if (c->flags & MG_F_CLOSE_CONNECTION_AFTER_RESPONSE) { c->flags |= MG_F_CLOSE_IMMEDIATELY; } } else if (ev == MG_EV_TIMER) { sj_invoke_cb0_this(ud->v7, ud->timeout_callback, ud->obj); } else if (ev == MG_EV_CLOSE) { if (c->listener == NULL && ud != NULL) { v7_set(ud->v7, ud->obj, "_c", ~0, v7_mk_undefined()); v7_disown(ud->v7, &ud->obj); v7_disown(ud->v7, &ud->timeout_callback); free(ud); c->user_data = NULL; } } }
static int run_slash_script(request_rec* r, void* stack_top) { sl_vm_t* vm; slash_context_t ctx; sl_vm_frame_t exit_frame, exception_frame; char* last_slash; SLVAL error; sl_static_init(); vm = sl_init("apache2"); sl_gc_set_stack_top(vm->arena, stack_top); vm->cwd = sl_alloc_buffer(vm->arena, strlen(r->canonical_filename) + 10); strcpy(vm->cwd, r->canonical_filename); last_slash = strrchr(vm->cwd, '/'); if(last_slash) { *last_slash = 0; } SL_TRY(exit_frame, SL_UNWIND_ALL, { SL_TRY(exception_frame, SL_UNWIND_EXCEPTION, { ctx.headers_sent = 0; ctx.vm = vm; ctx.r = r; vm->data = &ctx; setup_request_object(vm, r); setup_response_object(vm); ap_set_content_type(r, "text/html; charset=utf-8"); sl_do_file(vm, r->canonical_filename); }, error, { sl_response_clear(vm); sl_render_error_page(vm, error); });
static void run_slash_script(slash_api_base_t* api, cgi_options* options, void* stack_top) { sl_vm_t* vm; slash_context_t ctx; sl_vm_frame_t exit_frame, exception_frame; char* canonical_filename; SLVAL error; sl_static_init(); vm = sl_init("cgi-fcgi"); bzero(&ctx, sizeof(ctx)); ctx.api = api; ctx.headers_sent = 0; ctx.vm = vm; vm->data = &ctx; sl_gc_set_stack_top(vm->arena, stack_top); load_cgi_options(vm, options); load_request_info(vm, options, api, &ctx.info); #ifdef SL_TEST register_cgi_test_utils(vm); #endif canonical_filename = ctx.info.real_canonical_filename; #ifndef SL_TEST vm->cwd = ctx.info.real_canonical_dir; #endif if(canonical_filename) { SL_TRY(exit_frame, SL_UNWIND_ALL, { SL_TRY(exception_frame, SL_UNWIND_EXCEPTION, { bool request_valid = setup_request_object(vm, &ctx.info); setup_response_object(vm); if(!request_valid) { sl_error(vm, vm->lib.Error, "Invalid Request"); } sl_do_file_hashbang(vm, canonical_filename, api->type == SLASH_REQUEST_CGI); }, error, { sl_response_clear(vm); sl_render_error_page(vm, error); }); }, error, {});
static void handle_request(slash_context_t* ctx, void* dummy) { sl_catch_frame_t frame; SLVAL err; char* script_filename = ""; sl_vm_t* vm = sl_init(); pthread_t me = ctx->thread; ctx->vm = vm; ctx->vm->data = ctx; ctx->sent_headers = 0; sl_gc_set_stack_top(ctx->vm->arena, dummy); setup_request_object(ctx->vm, &ctx->request, &script_filename); setup_response_object(ctx->vm); SL_TRY(frame, SL_UNWIND_ALL, { sl_do_file(ctx->vm, (uint8_t*)script_filename); }, err, {