static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { switch (ev) { case MG_EV_HTTP_REQUEST: mg_serve_http(nc, (struct http_message *)ev_data, s_http_server_opts); break; case MG_EV_WEBSOCKET_HANDSHAKE_DONE: { DEBUG("[Server] Client connected!\n"); break; } case MG_EV_WEBSOCKET_FRAME: { struct websocket_message *wm = (struct websocket_message *)ev_data; memcpy(input, wm->data, wm->size); if (BUFFER_SIZE != fwrite(input, sizeof(float), BUFFER_SIZE, dsp_in)) { DEBUG("[Server] Terminated (fread).\n"); s_signal_received = 9; break; } fflush(dsp_in); DEBUG("[Server] Samples sent.\n"); if (BUFFER_SIZE != fread(output, sizeof(float), BUFFER_SIZE, dsp_out)) { printf("Server Terminated (fwrite).\n"); s_signal_received = 9; break; } DEBUG("[Server] Samples received.\n"); mg_send_websocket_frame(nc, WEBSOCKET_OP_BINARY, output, BUFFER_SIZE * sizeof(float)); break; } } }
/* * If requested via GET, serves the login page. * If requested via POST (form submission), checks password and logs user in. */ static void login_handler(struct mg_connection *nc, int ev, void *p) { struct http_message *hm = (struct http_message *) p; if (mg_vcmp(&hm->method, "POST") != 0) { /* Serve login.html */ mg_serve_http(nc, (struct http_message *) p, s_http_server_opts); } else { /* Perform password check. */ char user[50], pass[50]; int ul = mg_get_http_var(&hm->body, "user", user, sizeof(user)); int pl = mg_get_http_var(&hm->body, "pass", pass, sizeof(pass)); if (ul > 0 && pl > 0) { if (check_pass(user, pass)) { struct session *s = create_session(user, hm); mg_printf(nc, "HTTP/1.0 302 Found\r\n"); set_session_cookie(nc, s); mg_printf(nc, "Location: /\r\n"); mg_printf(nc, "\r\nHello, %s!\r\n", s->user); fprintf(stderr, "%s logged in, sid %" INT64_X_FMT "\n", s->user, s->id); } else { mg_printf(nc, "HTTP/1.0 403 Unauthorized\r\n\r\nWrong password.\r\n"); } } else { mg_printf(nc, "HTTP/1.0 400 Bad Request\r\n\r\nuser, pass required.\r\n"); } nc->flags |= MG_F_SEND_AND_CLOSE; } (void) ev; }
static void mg_ev_handler(struct mg_connection *nc, int ev, void *ev_data) { switch (ev) { case MG_EV_ACCEPT: { char addr[32]; mg_conn_addr_to_str(nc, addr, sizeof(addr), MG_SOCK_STRINGIFY_REMOTE | MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); LOG(LL_INFO, ("%p conn from %s", nc, addr)); break; } case MG_EV_HTTP_REQUEST: { char addr[32]; struct http_message *hm = (struct http_message *) ev_data; cs_stat_t st; mg_conn_addr_to_str(nc, addr, sizeof(addr), MG_SOCK_STRINGIFY_REMOTE | MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); LOG(LL_INFO, ("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len, hm->method.p, (int) hm->uri.len, hm->uri.p)); if (mg_vcmp(&hm->uri, "/upload") == 0 || (mg_vcmp(&hm->uri, "/") == 0 && mg_stat("SL:index.html", &st) != 0)) { mg_send(nc, upload_form, strlen(upload_form)); nc->flags |= MG_F_SEND_AND_CLOSE; break; } struct mg_serve_http_opts opts; memset(&opts, 0, sizeof(opts)); opts.document_root = "SL:"; mg_serve_http(nc, hm, opts); break; } case MG_EV_CLOSE: { LOG(LL_INFO, ("%p closed", nc)); break; } case MG_EV_WEBSOCKET_HANDSHAKE_DONE: { LOG(LL_INFO, ("%p switching to data mode", nc)); nc->handler = data_conn_handler; nc->ev_timer_time = mg_time(); /* Immediately */ break; } case MG_EV_TIMER: { data_collect(); nc->ev_timer_time = mg_time() + (DATA_COLLECTION_INTERVAL_MS * 0.001); } case MG_EV_HTTP_PART_BEGIN: case MG_EV_HTTP_PART_DATA: case MG_EV_HTTP_PART_END: { struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) ev_data; if (ev == MG_EV_HTTP_PART_BEGIN) { LOG(LL_INFO, ("Begin file upload: %s", mp->file_name)); } else if (ev == MG_EV_HTTP_PART_END) { LOG(LL_INFO, ("End file upload: %s", mp->file_name)); } mg_file_upload_handler(nc, ev, ev_data, upload_fname); } } }
static void ev_handler(struct mg_connection *con, int ev, void *ev_data) { struct http_message *msg = (struct http_message *)ev_data; switch (ev) { case MG_EV_HTTP_REQUEST: if (mg_vcmp(&msg->uri, "/play") == 0) { handle_play(con, msg); } else if (mg_vcmp(&msg->uri, "/pause") == 0) { if (playback_pause) { playback_pause = 0; } else { playback_pause = 1; } mg_printf(con, "HTTP/1.1 200 OK\r\n\r\n%s"); } else if (mg_vcmp(&msg->uri, "/stop") == 0) { handle_stop(con, msg); } else if (mg_vcmp(&msg->uri, "/init") == 0) { if (tpod_mg_str_cmp(&msg->method, &msg_http_method_get)) { /* char query_string[msg->query_string.len + 1]; */ /* strncpy(query_string, msg->query_string.p, msg->query_string.len); */ char *jsnstr_podcasts_obj = load_episodes(); mg_printf(con, "HTTP/1.1 200 OK\r\nContent-Type: " "application/json\r\nContent-Length: %d\r\n\r\n%s", (int)strlen(jsnstr_podcasts_obj), jsnstr_podcasts_obj); } } else { mg_serve_http(con, msg, s_http_server_opts); } break; default: break; } }
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { static const struct mg_str api_prefix = MG_MK_STR("/api/v1"); struct http_message *hm = (struct http_message *) ev_data; struct mg_str key; switch (ev) { case MG_EV_HTTP_REQUEST: if (has_prefix(&hm->uri, &api_prefix)) { key.p = hm->uri.p + api_prefix.len; key.len = hm->uri.len - api_prefix.len; if (is_equal(&hm->method, &s_get_method)) { db_op(nc, hm, &key, s_db_handle, API_OP_GET); } else if (is_equal(&hm->method, &s_put_method)) { db_op(nc, hm, &key, s_db_handle, API_OP_SET); } else if (is_equal(&hm->method, &s_delele_method)) { db_op(nc, hm, &key, s_db_handle, API_OP_DEL); } else { mg_printf(nc, "%s", "HTTP/1.0 501 Not Implemented\r\n" "Content-Length: 0\r\n\r\n"); } } else { mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */ } break; default: break; } }
/* * Serve static files. * * Takes an object containing mongoose http server options. * Commonly used properties: * - `document_root`: Path to the web root directory * - `enable_directory_listing`: Set to "no" to disable directory listing. * Enabled by default. * - `extra_headers`: Extra HTTP headers to add to each server response. * * For the full option object definition see: * https://docs.cesanta.com/mongoose/dev/index.html#/c-api/http.h/struct_mg_serve_http_opts/ */ SJ_PRIVATE enum v7_err Http_response_serve(struct v7 *v7, v7_val_t *res) { struct mg_serve_http_opts opts; struct http_message hm; enum v7_err rcode = V7_OK; DECLARE_CONN(); size_t i, n; v7_val_t request = v7_get(v7, v7_get_this(v7), "_r", ~0); v7_val_t url_v = v7_get(v7, request, "url", ~0); const char *url = v7_get_string_data(v7, &url_v, &n); const char *quest = strchr(url, '?'); memset(&opts, 0, sizeof(opts)); memset(&hm, 0, sizeof(hm)); /* Set up "fake" parsed HTTP message */ hm.uri.p = url; hm.uri.len = quest == NULL ? n : n - (quest - url); if (v7_argc(v7) > 0) { populate_opts_from_js_argument(v7, v7_arg(v7, 0), &opts); } mg_serve_http(c, &hm, opts); for (i = 0; i < ARRAY_SIZE(s_map); i++) { free(*(char **) ((char *) &opts + s_map[i].offset)); } *res = v7_get_this(v7); clean: return rcode; }
static void ev_handler(struct mg_connection *nc, int ev, void *p) { if (ev == MG_EV_HTTP_REQUEST) { mg_serve_http(nc, (struct http_message *) p, s_http_server_opts); } }
static void mongoose_ev_handler(struct mg_connection *c, int ev, void *p) { LOG(LL_VERBOSE_DEBUG, ("%p ev %d p %p fl %lx l %lu %lu", c, ev, p, c->flags, (unsigned long) c->recv_mbuf.len, (unsigned long) c->send_mbuf.len)); switch (ev) { case MG_EV_ACCEPT: { char addr[32]; mg_sock_addr_to_str(&c->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); LOG(LL_INFO, ("%p HTTP connection from %s", c, addr)); break; } case MG_EV_HTTP_REQUEST: { struct http_message *hm = (struct http_message *) p; LOG(LL_INFO, ("%p %.*s %.*s", c, (int) hm->method.len, hm->method.p, (int) hm->uri.len, hm->uri.p)); mg_serve_http(c, p, s_http_server_opts); c->flags |= MG_F_SEND_AND_CLOSE; break; } case MG_EV_CLOSE: { /* If we've sent the reply to the server, and should reboot, reboot */ if (c->flags & MG_F_RELOAD_CONFIG) { c->flags &= ~MG_F_RELOAD_CONFIG; device_reboot(); } break; } } }
static void frontend_handler(struct mg_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; switch (ev) { case MG_EV_HTTP_REQUEST: mg_serve_http(nc, hm, s_frontend_server_opts); /* Serve static content */ break; default: break; } }
/* * 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; } } }
void Server::event_handler(struct mg_connection *conn, int ev, void *p) { struct http_message *hm = (struct http_message *) p; if (ev == MG_EV_SSI_CALL) { mbuf_resize(&conn->send_mbuf, conn->send_mbuf.size * 2); std::string resp(conn->send_mbuf.buf, conn->send_mbuf.len); boost::replace_all(resp, "href=\"/", std::string("href=\"") + CONFIG_STRING(m_config, "service.base_location")); boost::replace_all(resp, "src=\"/", std::string("src=\"") + CONFIG_STRING(m_config, "service.base_location")); boost::replace_all(resp, "action=\"/", std::string("action=\"") + CONFIG_STRING(m_config, "service.base_location")); strcpy(conn->send_mbuf.buf, resp.c_str()); mbuf_trim(&conn->send_mbuf); return; } if (ev != MG_EV_HTTP_REQUEST) { return; } hm->uri.p += CONFIG_STRING(m_config, "service.base_location").size() - 1; hm->uri.len -= CONFIG_STRING(m_config, "service.base_location").size() - 1; if (!is_authorized(conn, hm)) { redirect_to(conn, hm, "/login"); } else if (mg_vcmp(&hm->uri, "/authorize") == 0) { authorize(conn, hm); } else if (mg_vcmp(&hm->uri, "/logout") == 0) { serve_logout(conn, hm); // } else if (mg_vcmp(&hm->uri, "/users") == 0) { // serve_users(conn, hm); // } else if (mg_vcmp(&hm->uri, "/users/add") == 0) { // serve_users_add(conn, hm); // } else if (mg_vcmp(&hm->uri, "/users/remove") == 0) { // serve_users_remove(conn, hm); } else if (has_prefix(&hm->uri, "/oauth2")) { serve_oauth2(conn, hm); } else if (has_prefix(&hm->uri, "/api/v1/")) { m_apiServer->handleRequest(this, get_session(hm), conn, hm); } else { if (hm->uri.p[hm->uri.len - 1] != '/') { std::string url(hm->uri.p, hm->uri.len); if (url.find(".") == std::string::npos) { url += "/"; redirect_to(conn, hm, url.c_str()); conn->flags |= MG_F_SEND_AND_CLOSE; return; } } mg_serve_http(conn, hm, s_http_server_opts); } conn->flags |= MG_F_SEND_AND_CLOSE; }
void mg_ev_handler(struct mg_connection *nc, int ev, void *ev_data) { LOG(LL_DEBUG, ("%p ev %d", nc, ev)); switch (ev) { case MG_EV_ACCEPT: { char addr[32]; mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); LOG(LL_INFO, ("Connection %p from %s", nc, addr)); break; } case MG_EV_HTTP_REQUEST: { char addr[32]; struct http_message *hm = (struct http_message *) ev_data; cs_stat_t st; mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); LOG(LL_INFO, ("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len, hm->method.p, (int) hm->uri.len, hm->uri.p)); if (mg_vcmp(&hm->uri, "/upload") == 0 || (mg_vcmp(&hm->uri, "/") == 0 && mg_stat("SL:index.html", &st) != 0)) { mg_send(nc, upload_form, strlen(upload_form)); nc->flags |= MG_F_SEND_AND_CLOSE; break; } struct mg_serve_http_opts opts; memset(&opts, 0, sizeof(opts)); opts.document_root = "SL:"; mg_serve_http(nc, hm, opts); break; } case MG_EV_CLOSE: { LOG(LL_INFO, ("Connection %p closed", nc)); break; } case MG_EV_HTTP_PART_BEGIN: case MG_EV_HTTP_PART_DATA: case MG_EV_HTTP_PART_END: { struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) ev_data; if (ev == MG_EV_HTTP_PART_BEGIN) { LOG(LL_INFO, ("Begin file upload: %s", mp->file_name)); } else if (ev == MG_EV_HTTP_PART_END) { LOG(LL_INFO, ("End file upload: %s", mp->file_name)); } mg_file_upload_handler(nc, ev, ev_data, upload_fname); } } }
static void mg_ev_handler(struct mg_connection *nc, int ev, void *p) { switch (ev) { case MG_EV_ACCEPT: { char addr[32]; mg_conn_addr_to_str(nc, addr, sizeof(addr), MG_SOCK_STRINGIFY_REMOTE | MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); LOG(LL_INFO, ("%p conn from %s", nc, addr)); break; } case MG_EV_HTTP_REQUEST: { char addr[32]; struct http_message *hm = (struct http_message *) p; mg_conn_addr_to_str(nc, addr, sizeof(addr), MG_SOCK_STRINGIFY_REMOTE | MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); LOG(LL_INFO, ("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len, hm->method.p, (int) hm->uri.len, hm->uri.p)); struct mg_serve_http_opts opts; memset(&opts, 0, sizeof(opts)); opts.document_root = "SL:"; mg_serve_http(nc, (struct http_message *) p, opts); break; } case MG_EV_CLOSE: { LOG(LL_INFO, ("%p closed", nc)); break; } case MG_EV_WEBSOCKET_HANDSHAKE_DONE: { LOG(LL_INFO, ("%p switching to data mode", nc)); nc->handler = data_conn_handler; nc->ev_timer_time = mg_time(); /* Immediately */ break; } case MG_EV_TIMER: { data_collect(); nc->ev_timer_time = mg_time() + (DATA_COLLECTION_INTERVAL_MS * 0.001); } case MG_EV_HTTP_PART_BEGIN: case MG_EV_HTTP_PART_DATA: case MG_EV_HTTP_PART_END: { mg_file_upload_handler(nc, ev, p, upload_fname); } } }
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; switch (ev) { case MG_EV_HTTP_REQUEST: if (mg_vcmp(&hm->uri, "/save") == 0) { handle_save(nc, hm); } else { mg_serve_http(nc, hm, s_http_server_opts); // Serve static content } break; case MG_EV_SSI_CALL: handle_ssi_call(nc, ev_data); break; default: break; } }
void ccMongooseWebServer::ev_handler(struct mg_connection *nc, int ev, void *p) { bool bIsBuiltinProcess = true; ccMongooseWebServer* pServer = (ccMongooseWebServer*)nc->mgr->user_data; /* HTTP and websocket events. void *ev_data is described in a comment. */ switch (ev) { case MG_EV_HTTP_REQUEST: /* struct http_message * */ if (pServer->_pEventListener != NULL) { ccMongooseWebServerRequest oRequest(nc, (http_message *)p); ccMongooseWebServerResponse oResponse(nc); bIsBuiltinProcess = !pServer->_pEventListener->OnWebServerRequest(&oRequest, &oResponse); } if (bIsBuiltinProcess) mg_serve_http(nc, (struct http_message *)p, s_http_server_opts); break; case MG_EV_HTTP_REPLY: /* struct http_message * */ break; case MG_EV_HTTP_CHUNK: /* struct http_message * */ break; case MG_EV_SSI_CALL: /* char * */ break; case MG_EV_WEBSOCKET_HANDSHAKE_REQUEST: /* NULL */ break; case MG_EV_WEBSOCKET_HANDSHAKE_DONE: /* NULL */ break; case MG_EV_WEBSOCKET_FRAME: /* struct websocket_message * */ break; case MG_EV_WEBSOCKET_CONTROL_FRAME: /* struct websocket_message * */ break; } }
/* Main event handler. */ static void ev_handler(struct mg_connection *nc, int ev, void *p) { switch (ev) { case MG_EV_HTTP_REQUEST: { struct http_message *hm = (struct http_message *) p; struct session *s = get_session(hm); /* Ask the user to log in if they did not present a valid cookie. */ if (s == NULL) { mg_printf(nc, "HTTP/1.0 302 Found\r\n" "Location: /login.html\r\n" "\r\n" "Please log in"); nc->flags |= MG_F_SEND_AND_CLOSE; break; } /* * Serve the page that was requested. * Save session in user_data for use by SSI calls. */ fprintf(stderr, "%s (sid %" INT64_X_FMT ") requested %.*s\n", s->user, s->id, (int) hm->uri.len, hm->uri.p); nc->user_data = s; mg_serve_http(nc, (struct http_message *) p, s_http_server_opts); break; } case MG_EV_SSI_CALL: { /* Expand variables in a page by using session data. */ const char *var = (const char *) p; const struct session *s = (const struct session *) nc->user_data; if (strcmp(var, "user") == 0) { mg_printf_html_escape(nc, "%s", s->user); } else if (strcmp(var, "lucky_number") == 0) { mg_printf_html_escape(nc, "%d", s->lucky_number); } break; } case MG_EV_TIMER: { /* Perform session maintenance. */ check_sessions(); mg_set_timer(nc, mg_time() + SESSION_CHECK_INTERVAL); break; } } }
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; switch (ev) { case MG_EV_HTTP_REQUEST: if (mg_vcmp(&hm->uri, "/api/v1/sum") == 0) { handle_sum_call(nc, hm); /* Handle RESTful call */ } else if (mg_vcmp(&hm->uri, "/printcontent") == 0) { char buf[100] = {0}; memcpy(buf, hm->body.p, sizeof(buf) - 1 < hm->body.len ? sizeof(buf) - 1 : hm->body.len); printf("%s\n", buf); } else { mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */ } break; default: break; } }
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; switch (ev) { case MG_EV_HTTP_REQUEST: if (mg_vcmp(&hm->uri, "/api/v1/status") == 0) { handle_status_call(nc, hm); } else if (mg_vcmp(&hm->uri, "/api/v1/setrepeat") == 0) { handle_setrepeat_call(nc, hm); } else if (mg_vcmp(&hm->uri, "/api/v1/setled") == 0) { handle_setled_call(nc, hm); } else if (mg_vcmp(&hm->uri, "/api/v1/switch") == 0) { handle_switch_call(nc, hm); } else { mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */ } break; default: break; } }
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { switch (ev) { case MG_EV_HTTP_REQUEST: { mg_serve_http(nc, (struct http_message *) ev_data, s_http_server_opts); break; } case MG_EV_WEBSOCKET_HANDSHAKE_DONE: { #if 0 /* New websocket connection. */ // send something... #endif break; } case MG_EV_WEBSOCKET_FRAME: { struct websocket_message *wm = (struct websocket_message *) ev_data; /* New websocket message. Tell everybody. */ // struct mg_str d = {(char *) wm->data, wm->size}; msg_command( nc, wm->data, wm->size ); fflush(stdout); break; } case MG_EV_CLOSE: { #if 0 /* Disconnect. Tell everybody. */ if (is_websocket(nc)) { // send something... } #endif break; } } }
void db_event_handler(struct mg_connection* nc, int ev, void* ev_data) { struct http_message* hm = (struct http_message*) ev_data; switch (ev) { case MG_EV_HTTP_REQUEST: if (mg_vcmp(&hm->uri, "/pictDB/list") == 0) { handle_list_call(nc); } else if (mg_vcmp(&hm->uri, "/pictDB/read") == 0) { handle_read_call(nc, hm); } else if (mg_vcmp(&hm->uri, "/pictDB/insert") == 0) { handle_insert_call(nc, hm); } else if (mg_vcmp(&hm->uri, "/pictDB/delete") == 0) { handle_delete_call(nc, hm); } else { mg_serve_http(nc, hm, s_http_server_opts); // Serve static content } break; default: break; } }
/* * Main event handler. Receives data events and dispatches to * the appropriate handler function. * * 1. RESTful API requests are handled by send_command_to_the_device. * 2. requests to /mpeg are established and left open waiting for data to arrive * from WebSocket. * 3. WebSocket frames are handled by push_frame_to_clients. * 4. All other connections are passed to the mg_serve_http handler * which serves static files. */ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { struct websocket_message *wm = (struct websocket_message *) ev_data; struct http_message *hm = (struct http_message *) ev_data; switch (ev) { case MG_EV_HTTP_REQUEST: if (mg_vcmp(&hm->uri, "/mjpg") == 0) { nc->flags |= MG_F_USER_2; /* Set a mark on image requests */ mg_printf(nc, "%s", "HTTP/1.0 200 OK\r\n" "Cache-Control: no-cache\r\n" "Pragma: no-cache\r\n" "Expires: Thu, 01 Dec 1994 16:00:00 GMT\r\n" "Connection: close\r\n" "Content-Type: multipart/x-mixed-replace; " "boundary=--w00t\r\n\r\n"); } else if (mg_vcmp(&hm->uri, "/api") == 0 && hm->body.len > 0) { /* * RESTful API call. HTTP message body should be a JSON message. * We should parse it and take appropriate action. * In our case, simply forward that call to the device. */ printf("API CALL: [%.*s] [%.*s]\n", (int) hm->method.len, hm->method.p, (int) hm->body.len, hm->body.p); send_command_to_the_device(nc->mgr, &hm->body); mg_printf(nc, "HTTP/1.0 200 OK\nContent-Length: 0\n\n"); } else { /* Delegate to the static web server handler for all other paths. */ mg_serve_http(nc, hm, web_root_opts); } break; case MG_EV_WEBSOCKET_FRAME: printf("Got websocket frame, size %lu\n", (unsigned long) wm->size); push_frame_to_clients(nc->mgr, wm); break; } }
void RestServ::httpStatic(mg_connection& nc, HttpMessage data) { mg_serve_http(&nc, data.get(), httpoptions_); }
static void ev_handler(struct mg_connection *nc, int ev, void *p) { if (ev == NS_HTTP_REQUEST) { mg_serve_http(nc, p, s_http_server_opts); } }
static void mg_ev_handler(struct mg_connection *nc, int ev, void *ev_data) { switch (ev) { case MG_EV_ACCEPT: { char addr[32]; mg_conn_addr_to_str(nc, addr, sizeof(addr), MG_SOCK_STRINGIFY_REMOTE | MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); LOG(LL_INFO, ("%p conn from %s", nc, addr)); break; } case MG_EV_HTTP_REQUEST: { char addr[32]; struct http_message *hm = (struct http_message *) ev_data; cs_stat_t st; mg_conn_addr_to_str(nc, addr, sizeof(addr), MG_SOCK_STRINGIFY_REMOTE | MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); LOG(LL_INFO, ("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len, hm->method.p, (int) hm->uri.len, hm->uri.p)); if (mg_vcmp(&hm->uri, "/upload") == 0 || (mg_vcmp(&hm->uri, "/") == 0 && mg_stat("SL:index.html", &st) != 0)) { mg_send(nc, upload_form, strlen(upload_form)); nc->flags |= MG_F_SEND_AND_CLOSE; break; } struct mg_serve_http_opts opts; memset(&opts, 0, sizeof(opts)); opts.document_root = "SL:"; mg_serve_http(nc, hm, opts); break; } case MG_EV_CLOSE: { LOG(LL_INFO, ("%p closed", nc)); break; } case MG_EV_WEBSOCKET_HANDSHAKE_DONE: { LOG(LL_INFO, ("%p switching to data mode", nc)); nc->handler = data_conn_handler; nc->ev_timer_time = mg_time(); /* Immediately */ break; } case MG_EV_TIMER: { data_collect(); nc->ev_timer_time = mg_time() + (DATA_COLLECTION_INTERVAL_MS * 0.001); break; } /* SimpleLink FS requires pre-declaring max file size. We use Content-Length * for that purpose - it will not exactly match file size, but is guaranteed * to exceed it and should be close enough. */ case MG_EV_HTTP_MULTIPART_REQUEST: { struct http_message *hm = (struct http_message *) ev_data; struct mg_str *cl_header = mg_get_http_header(hm, "Content-Length"); intptr_t cl = -1; if (cl_header != NULL && cl_header->len < 20) { char buf[20]; memcpy(buf, cl_header->p, cl_header->len); buf[cl_header->len] = '\0'; cl = atoi(buf); if (cl < 0) cl = -1; } nc->user_data = (void *) cl; break; } case MG_EV_HTTP_PART_BEGIN: case MG_EV_HTTP_PART_DATA: case MG_EV_HTTP_PART_END: { struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) ev_data; if (ev == MG_EV_HTTP_PART_BEGIN) { LOG(LL_INFO, ("Begin file upload: %s", mp->file_name)); } else if (ev == MG_EV_HTTP_PART_END) { LOG(LL_INFO, ("End file upload: %s", mp->file_name)); } mg_file_upload_handler(nc, ev, ev_data, upload_fname); } } }
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { if (ev == MG_EV_HTTP_REQUEST) { mg_serve_http(nc, ev_data, s_http_server_opts); } }