Пример #1
0
/*
 * 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;
    }
  }
}
Пример #2
0
void sj_invoke_cb2(struct v7 *v7, v7_val_t cb, v7_val_t arg1, v7_val_t arg2) {
  sj_invoke_cb2_this(v7, cb, v7_get_global(v7), arg1, arg2);
}