Ejemplo n.º 1
0
REPLACE_STATIC void handle_lsp_request(struct mg_connection *conn, const char *path,
                               struct file *filep) {
  void *p = NULL;
  lua_State *L = NULL;

  if (!mg_fopen(conn, path, "r", filep)) {
    send_http_error(conn, 404, "Not Found", "%s", "File not found");
  } else if (filep->membuf == NULL &&
             (p = mmap(NULL, filep->size, PROT_READ, MAP_PRIVATE,
                       fileno(filep->fp), 0)) == MAP_FAILED) {
    send_http_error(conn, 500, http_500_error, "%s", "x");
  } else if ((L = luaL_newstate()) == NULL) {
    send_http_error(conn, 500, http_500_error, "%s", "y");
  } else {
    mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n"
              "Content-Type: text/html\r\nConnection: close\r\n\r\n");
    prepare_lua_environment(conn, L);
    conn->request_info.ev_data = L;
    call_user(conn, MG_INIT_LUA);
    lsp(conn, filep->membuf == NULL ? p : filep->membuf, filep->size, L);
  }

  if (L) lua_close(L);
  if (p) munmap(p, filep->size);
  mg_fclose(filep);
}
Ejemplo n.º 2
0
static void test_user_data(void) {
  struct mg_context *ctx;

  ASSERT((ctx = mg_start(user_data_tester, (void *) 123, OPTIONS)) != NULL);
  ASSERT(ctx->user_data == (void *) 123);
  call_user(fc(ctx), MG_NEW_REQUEST);
  mg_stop(ctx);
}
Ejemplo n.º 3
0
// This is the heart of the Mongoose's logic.
// This function is called when the request is read, parsed and validated,
// and Mongoose must decide what action to take: serve a file, or
// a directory, or call embedded function, etcetera.
static void handle_request(struct mg_connection *conn) {
  struct mg_request_info *ri = &conn->request_info;
  int uri_len;

  if ((conn->request_info.query_string = strchr(ri->uri, '?')) != NULL) {
    * conn->request_info.query_string++ = '\0';
  }
  uri_len = strlen(ri->uri);
  (void) url_decode(ri->uri, (size_t)uri_len, ri->uri, (size_t)(uri_len + 1), 0);
  remove_double_dots_and_double_slashes(ri->uri);

  DEBUG_TRACE(("%s", ri->uri));
  if (call_user(conn, MG_NEW_REQUEST) == NULL) {
    mg_send_http_error(conn, 404, "Not Found", "%s", "File not found");
  }
}
Ejemplo n.º 4
0
// Print error message to the opened error log stream.
static void cry(struct mg_connection *conn, const char *fmt, ...) {
  char buf[BUFSIZ];
  va_list ap;

  va_start(ap, fmt);
  (void) vsnprintf(buf, sizeof(buf), fmt, ap);
  va_end(ap);

  // Do not lock when getting the callback value, here and below.
  // I suppose this is fine, since function cannot disappear in the
  // same way string option can.
  conn->request_info.log_message = buf;
  if (call_user(conn, MG_EVENT_LOG) == NULL) {
      DEBUG_TRACE(("[%s]", buf));
  }
  conn->request_info.log_message = NULL;
}