// Called when mongoose has finished processing request. static void end_request(const struct mg_connection* conn, int reply_status_code) { mg_request_info* request = mg_get_request_info(const_cast<mg_connection*>(conn)); std::string message; message.append(request->request_method); message.append(" "); message.append(std::to_string(reply_status_code)); message.append(" "); message.append(request->uri); if (request->query_string) { message.append("?"); message.append(request->query_string); } //LOG_INFO << message; qDebug() << message; }
int BXHandler(struct mg_connection *conn, void *cbdata) { /* Handler may access the request info using mg_get_request_info */ const struct mg_request_info *req_info = mg_get_request_info(conn); mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " "close\r\n\r\n"); mg_printf(conn, "<html><body>"); mg_printf(conn, "<h2>This is the BX handler %p!!!</h2>", cbdata); mg_printf(conn, "<p>The actual uri is %s</p>", req_info->local_uri); mg_printf(conn, "</body></html>\n"); return 1; }
static void *query_tunet_rm_user(struct mg_connection *conn) { const struct mg_request_info *ri = mg_get_request_info(conn); char user[BUFFER_LEN]; int qlen = strlen(ri->query_string); memset(user, 0, BUFFER_LEN); mg_get_var(ri->query_string, qlen, "user", user, BUFFER_LEN); mg_printf(conn, HTTP_HEADER_PLAIN "%d", ta_del_user(user)); return ""; }
static void *event_handler(enum mg_event event, struct mg_connection *conn) { const struct mg_request_info *request_info = mg_get_request_info(conn); if (event == MG_NEW_REQUEST && !strcmp(request_info->uri, "/data")) { mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Length: %d\r\n" "Content-Type: text/plain\r\n\r\n" "%s", (int) strlen(fetch_data), fetch_data); return ""; } else if (event == MG_EVENT_LOG) { printf("%s\n", mg_get_log_message(conn)); } return NULL; }
static int mongooseRequestHandler(struct mg_connection *conn) { const struct mg_request_info *ri = mg_get_request_info(conn); if (strcmp(ri->uri, "/assignment") == 0 && strcmp(ri->request_method, "POST") == 0) { // return a 200 mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n"); // upload the file mg_upload(conn, "/tmp"); return 1; } else { // have mongoose process this request from the document_root return 0; } }
void *ws_server_thread(void *parm) { wsserver::ws_connection* ws_conn = static_cast<wsserver::ws_connection*>(parm); mg_connection *conn = ws_conn->conn; wsserver* object = static_cast<wsserver*>(mg_get_request_info(conn)->user_data); int timer = 0; if(object->getDebug()) post("ws_server_thread %d\n", ws_conn->index); /* While the connection is open, send periodic updates */ while(!ws_conn->closing) { usleep(object->mClientUpdateRateMs * 1000); timer++; WDL_MutexLock(&ws_conn->mutex); if (ws_conn->update && ws_conn->newdatafromserver) { if (!ws_conn->closing) { if (ws_conn->fromserver.GetLength()) mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, ws_conn->fromserver.Get(), ws_conn->fromserver.GetLength()); ws_conn->newdatafromserver = false; } } /* Send periodic PING to assure websocket remains connected, except if we are closing */ if (timer%100 == 0 && !ws_conn->closing) mg_websocket_write(conn, WEBSOCKET_OPCODE_PING, NULL, 0); } if(object->getDebug()) post("ws_server_thread %d exiting\n", ws_conn->index); WDL_MutexLock(&ws_conn->mutex); // reset connection information to allow reuse by new client ws_conn->update = 0; ws_conn->index = -1; ws_conn->newdatafromserver = false; ws_conn->newdatatoserver = true; ws_conn->fromserver.Set(""); if(ws_conn->toserver.Get()) ws_conn->toserver.SetFormatted(MAX_STRING, "dx"); ws_conn->conn = NULL; ws_conn->closing = 2; return NULL; }
/*_________________---------------------------__________________ _________________ mongoose callback __________________ -----------------___________________________------------------ */ static int *mongoose_callback( struct mg_connection *conn ) { const struct mg_request_info *request_info = mg_get_request_info(conn); char *ret_strings = ""; /* Iterate all the url_mapping data */ hash_iterator iter; hash_entry *e; int reti; init_hash_iterator( url_mapping_db.url_mapping, &iter ); while ( ( e = iterate_hash_next( &iter ) ) != NULL ) { url_mapping *url_mapping_data = ( url_mapping * ) e->value; /* Compare the string with regex */ reti = regexec( &url_mapping_data->regex, request_info->uri , 0, NULL, 0 ); if( !reti ){ /* Match the URI */ if( strcmp( request_info->request_method , url_mapping_data->method ) == 0 ) { /* Match the REST API */ ret_strings = ( *url_mapping_data->restapi_requested_callback )( request_info, NULL ); } else { info( "REST API not found \n" ); } } } /* Uncomment these and will see additional port number info from mongoose */ //int ret_length = strlen( ret_strings ); //char content[ ret_length + 1024 ]; //int content_length = snprintf(content, sizeof(content), // "Hello from mongoose! Remote port: %d, content:%s\n", // request_info->remote_port, ret_strings ); mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Content-Length: %d\r\n" // Always set Content-Length "\r\n" "%s", strlen(ret_strings), ret_strings); return 1; }
// Once websocket negotiation is complete, start a server for the connection void websocket_ready_handler(struct mg_connection *conn) { wsserver* object = static_cast<wsserver*>(mg_get_request_info(conn)->user_data); if(object->getDebug()) post("ready handler\n"); for(int i=0; i < object->getMaxNConnections(); ++i) { if (object->getConnection(i)->conn == conn) { if(object->getDebug()) post("...start server %d\n", i); mg_start_thread(ws_server_thread, (void *) object->getConnection(i)); break; } } }
/** Fired when client is connected */ void SocketDwarf::Server::DwarfServer::OnClientConnectedS(struct mg_connection * conn) { try { const struct mg_request_info * requestInfo = mg_get_request_info(conn); DwarfServer * server = static_cast<DwarfServer*>(requestInfo->user_data); if (server == 0) { throw new std::runtime_error("internal error: invalid server"); } server->OnClientConnected(conn); } catch(std::exception & excp) { std::cerr << excp.what(); } }
static void *callback(enum mg_event event, struct mg_connection *conn) { const struct mg_request_info *request_info = mg_get_request_info(conn); int i; for (i = 0; test_config[i].uri != NULL; i++) { if (event == test_config[i].event && (event == MG_HTTP_ERROR || !strcmp(request_info->uri, test_config[i].uri))) { test_config[i].func(conn, request_info); return "processed"; } } return NULL; }
void render(struct mg_connection *conn) { const struct mg_request_info *request_info = mg_get_request_info(conn); std::string str = "HTTP/1.1 200 OK\r\n" "Content-Type: image/bmp\r\n" "\r\n" ; mg_write(conn, str.c_str(), str.length()); model* m = _model; quad_model* qm = static_cast<quad_model*>(m); write_quad_model_as_bmp(conn, qm); }
int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len) { struct mg_request_info * rq = mg_get_request_info(conn); tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data; char msg[128]; pthread_mutex_lock(&sMutex); if (flags==136) { // close websock websocket_done(wsock); rq->conn_data = 0; pthread_mutex_unlock(&sMutex); return 1; } if ((data_len>=5) && (data_len<100) && (flags==129) || (flags==130)) { // init command if ((wsock->webSockState==1) && (!memcmp(data,"init ",5))) { char * chk; unsigned long gid; memcpy(msg,data+5,data_len-5); msg[data_len-5]=0; gid = strtoul(msg,&chk,10); wsock->initId = gid; if (gid>0 && chk!=NULL && *chk==0) { wsock->webSockState = 2; } pthread_mutex_unlock(&sMutex); return 1; } // chat message if ((wsock->webSockState==2) && (!memcmp(data,"msg ",4))) { send_to_all_websockets(data, data_len); pthread_mutex_unlock(&sMutex); return 1; } } // keep alive if ((data_len==4) && !memcmp(data,"ping",4)) { pthread_mutex_unlock(&sMutex); return 1; } pthread_mutex_unlock(&sMutex); return 0; }
int FooHandler(struct mg_connection *conn, void *cbdata) { /* Handler may access the request info using mg_get_request_info */ const struct mg_request_info *req_info = mg_get_request_info(conn); mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"); mg_printf(conn, "<html><body>"); mg_printf(conn, "<h2>This is the Foo handler!!!</h2>"); mg_printf(conn, "<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>", req_info->request_method, req_info->uri, req_info->http_version); mg_printf(conn, "</body></html>\n"); return 1; }
static void test_post(struct mg_connection *conn) { const char *cl; char *buf; int len; const struct mg_request_info *ri = mg_get_request_info(conn); send_standard_reply_head(conn); if (strcmp(ri->request_method, "POST") == 0 && (cl = mg_get_header(conn, "Content-Length")) != NULL) { len = atoi(cl); if ((buf = malloc(len)) != NULL) { mg_write(conn, buf, len); free(buf); } } }
static int begin_request_handler_cb(struct mg_connection *conn) { const struct mg_request_info *ri = mg_get_request_info(conn); if (!strcmp(ri->uri, "/data")) { mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Length: %d\r\n" "Content-Type: text/plain\r\n\r\n" "%s", (int) strlen(fetch_data), fetch_data); return 1; } if (!strcmp(ri->uri, "/upload")) { ASSERT(mg_upload(conn, ".") == 1); } return 0; }
static int api_callback(struct mg_connection *conn) { struct mg_request_info *ri = mg_get_request_info(conn); char post_data[100] = ""; ASSERT(ri->user_data == (void *) 123); ASSERT(ri->num_headers == 2); ASSERT(strcmp(mg_get_header(conn, "host"), "blah.com") == 0); ASSERT(mg_read(conn, post_data, sizeof(post_data)) == 3); ASSERT(memcmp(post_data, "b=1", 3) == 0); ASSERT(ri->query_string != NULL); ASSERT(ri->remote_ip > 0); ASSERT(ri->remote_port > 0); ASSERT(strcmp(ri->http_version, "1.0") == 0); mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\n"); return 1; }
static void *mongoose_callback(enum mg_event ev, struct mg_connection *conn) { hls_dbg("mongoose callback to proxy_live\n"); if (ev == MG_EVENT_LOG) { hls_dbg("%s\n", (const char *)mg_get_request_info(conn)->ev_data); } if (ev == MG_NEW_REQUEST) { hls_info("mongoose proxy get queue from hls\n"); mg_get_info_from_hls(conn, hls_ctx_g); } // Returning NULL marks request as not handled, signalling mongoose to // proceed with handling it. return NULL; }
static void test_get_header(struct mg_connection *conn) { const char *value; int i; const struct mg_request_info *ri = mg_get_request_info(conn); send_standard_reply_head(conn); printf("HTTP headers: %d\n", ri->num_headers); for (i = 0; i < ri->num_headers; i++) { printf("[%s]: [%s]\n", ri->http_headers[i].name, ri->http_headers[i].value); } value = mg_get_header(conn, "Host"); if (value != NULL) { mg_printf(conn, "Value: [%s]", value); } }
bool handlePost(CivetServer *server, struct mg_connection *conn) { /* Handler may access the request info using mg_get_request_info */ const struct mg_request_info *req_info = mg_get_request_info(conn); long long rlen, wlen; long long nlen = 0; long long tlen = req_info->content_length; char buf[1024]; mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: " "text/html\r\nConnection: close\r\n\r\n"); mg_printf(conn, "<html><body>\n"); mg_printf(conn, "<h2>This is the Foo POST handler!!!</h2>\n"); mg_printf(conn, "<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>\n", req_info->request_method, req_info->uri, req_info->http_version); mg_printf(conn, "<p>Content Length: %li</p>\n", (long)tlen); mg_printf(conn, "<pre>\n"); while (nlen < tlen) { rlen = tlen - nlen; if (rlen > sizeof(buf)) { rlen = sizeof(buf); } rlen = mg_read(conn, buf, (size_t)rlen); if (rlen <= 0) { break; } wlen = mg_write(conn, buf, (size_t)rlen); if (rlen != rlen) { break; } nlen += wlen; } mg_printf(conn, "\n</pre>\n"); mg_printf(conn, "</body></html>\n"); return true; }
/*! * \brief * Sends a map part definition to the client if the request is valid. * * \param connection * The connection the response is to be sent to. * * \param request_info * Pointer to the request information sent to the server by the client. * * Sends a map part definition to the client if the request is valid. * * \remarks * A client error status code is returned to the client if the request is incomplete or if no matching map is found. */ static void ProcessMapPartDefinitionRequest(mg_connection* connection, std::string map_name) { const mg_request_info* request_info = mg_get_request_info(connection); // look for the map definition element c_map_element* map_element = FindMap(map_name.c_str()); if(!map_element) { // the map wasn't found so respond with the file not found status code SendResponse(connection, Enums::_http_status_code_client_error_not_found); return; } // the map was found so respond with its map part definition SendMapPartDefinition(connection, map_element); }
static void *query_phone_stop(struct mg_connection *conn) { const struct mg_request_info *ri = mg_get_request_info(conn); char name[BUFFER_LEN]; char buffer[BUFFER_LEN]; int qlen = strlen(ri->query_string); memset(name, 0, BUFFER_LEN); mg_get_var(ri->query_string, qlen, "name", name, BUFFER_LEN); sprintf(buffer, "stop:%s", name); printf("send:%s\n", buffer); udp_broadcast(buffer); mg_printf(conn, HTTP_HEADER_PLAIN "0"); return ""; }
static int begin_request_handler_cb(struct mg_connection *conn) { const struct mg_request_info *ri = mg_get_request_info(conn); if (!strcmp(ri->uri, "/data")) { mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n\r\n" "%s", fetch_data); close_connection(conn); return 1; } if (!strcmp(ri->uri, "/upload")) { ASSERT(ri->query_string != NULL); ASSERT(mg_upload(conn, ".") == atoi(ri->query_string)); } return 0; }
bool CivetServer::handleRequest(struct mg_connection *conn) { struct mg_request_info *request_info = mg_get_request_info(conn); CivetHandler *handler = getHandler(request_info->uri); if (handler) { if (strcmp(request_info->request_method, "GET") == 0) { return handler->handleGet(this, conn); } else if (strcmp(request_info->request_method, "POST") == 0) { return handler->handlePost(this, conn); } else if (strcmp(request_info->request_method, "PUT") == 0) { return !handler->handlePost(this, conn); } else if (strcmp(request_info->request_method, "DELETE") == 0) { return !handler->handlePost(this, conn); } } return false; // No handler found }
/** Fired when client sends data */ int SocketDwarf::Server::DwarfServer::OnClientDataReceivedS(struct mg_connection * conn, int flags, char *data, size_t data_len) { try { const struct mg_request_info * requestInfo = mg_get_request_info(conn); DwarfServer * server = static_cast<DwarfServer*>(requestInfo->user_data); if (server == 0) { throw new std::runtime_error("internal error: invalid server"); } std::string managedData; managedData.assign(data, data_len); return server->OnClientDataReceived(conn, managedData); } catch (std::exception & excp) { std::cerr << excp.what(); } return WEBSOCKET_OPCODE_CONNECTION_CLOSE; }
int CookieHandler(struct mg_connection *conn, void *cbdata) { /* Handler may access the request info using mg_get_request_info */ const struct mg_request_info *req_info = mg_get_request_info(conn); const char *cookie = mg_get_header(conn, "Cookie"); char first_str[64], count_str[64]; int count; (void)mg_get_cookie(cookie, "first", first_str, sizeof(first_str)); (void)mg_get_cookie(cookie, "count", count_str, sizeof(count_str)); mg_printf(conn, "HTTP/1.1 200 OK\r\nConnection: close\r\n"); if (first_str[0] == 0) { time_t t = time(0); struct tm *ptm = localtime(&t); mg_printf(conn, "Set-Cookie: first=%04i-%02i-%02iT%02i:%02i:%02i\r\n", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec); } count = (count_str[0] == 0) ? 0 : atoi(count_str); mg_printf(conn, "Set-Cookie: count=%i\r\n", count + 1); mg_printf(conn, "Content-Type: text/html\r\n\r\n"); mg_printf(conn, "<html><body>"); mg_printf(conn, "<h2>This is the CookieHandler.</h2>"); mg_printf(conn, "<p>The actual uri is %s</p>", req_info->local_uri); if (first_str[0] == 0) { mg_printf(conn, "<p>This is the first time, you opened this page</p>"); } else { mg_printf(conn, "<p>You opened this page %i times before.</p>", count); mg_printf(conn, "<p>You first opened this page on %s.</p>", first_str); } mg_printf(conn, "</body></html>\n"); return 1; }
int FormHandler(struct mg_connection *conn, void *cbdata) { /* Handler may access the request info using mg_get_request_info */ const struct mg_request_info *req_info = mg_get_request_info(conn); int ret; struct mg_form_data_handler fdh = {field_found, field_get, 0}; /* TODO: Checks before calling handle_form_data ? */ (void)req_info; mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n"); fdh.user_data = (void *)conn; /* TODO: Handle the return value */ ret = mg_handle_form_data(conn, &fdh); return 1; }
int CheckSumHandler(struct mg_connection *conn, void *cbdata) { /* Handler may access the request info using mg_get_request_info */ const struct mg_request_info *req_info = mg_get_request_info(conn); int i, j, ret; struct tfiles_checksums chksums; md5_byte_t digest[16]; struct mg_form_data_handler fdh = {field_disp_read_on_the_fly, field_get_checksum, 0, (void *)&chksums}; /* It would be possible to check the request info here before calling * mg_handle_form_request. */ (void)req_info; memset(&chksums, 0, sizeof(chksums)); mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Connection: close\r\n\r\n"); /* Call the form handler */ mg_printf(conn, "File checksums:"); ret = mg_handle_form_request(conn, &fdh); for (i = 0; i < chksums.index; i++) { md5_finish(&(chksums.file[i].chksum), digest); /* Visual Studio 2010+ support llu */ mg_printf(conn, "\r\n%s %llu ", chksums.file[i].name, chksums.file[i].length); for (j = 0; j < 16; j++) { mg_printf(conn, "%02x", (unsigned int)digest[j]); } } mg_printf(conn, "\r\n%i files\r\n", ret); return 1; }
// When websocket is closed, tell the associated server to shut down void websocket_close_handler(struct mg_connection *conn) { wsserver* object = static_cast<wsserver*>(mg_get_request_info(conn)->user_data); if(object->getDebug()) post("close handler\n"); /* called for every close, not just websockets */ for(int i=0; i < object->getMaxNConnections(); ++i) { wsserver::ws_connection* ws_conn = object->getConnection(i); WDL_MutexLock(&ws_conn->mutex); if (ws_conn->conn == conn) { if(object->getDebug()) post("...close server %d\n", i); ws_conn->closing = 1; } } }
int EquipletScada::mongooseBeginRequestCallback(mg_connection* connection) { mg_request_info *request_info = mg_get_request_info(connection); int processed = 1; if (strcmp(request_info->uri, "/remote/equipletInfo") == 0) { mongooseProcessEquipletInfo(connection, request_info); } else if (strcmp(request_info->uri, "/remote/moduleInfo") == 0) { mongooseProcessModuleInfo(connection, request_info); } else if (strcmp(request_info->uri, "/remote/changeModuleMode") == 0) { mongooseProcessChangeModuleMode(connection, request_info); } else if (strcmp(request_info->uri, "/remote/changeEquipletMode") == 0) { mongooseProcessChangeEquipletMode(connection, request_info); } else if (strcmp(request_info->uri, "/remote/changeEquipletState") == 0) { mongooseProcessChangeEquipletState(connection, request_info); } else { processed = 0; } return processed; }
static void test_get_var(struct mg_connection *conn) { char *var, *buf; size_t buf_len; const char *cl; int var_len; const struct mg_request_info *ri = mg_get_request_info(conn); int is_form_enc = 0; send_standard_reply_head(conn); buf_len = 0; var = buf = NULL; cl = mg_get_header(conn, "Content-Length"); mg_printf(conn, "cl: %p\n", cl); if ((!strcmp(ri->request_method, "POST") || !strcmp(ri->request_method, "PUT")) && cl != NULL) { buf_len = atoi(cl); buf = malloc(buf_len); /* Read in two pieces, to test continuation */ if (buf_len > 2) { mg_read(conn, buf, 2); mg_read(conn, buf + 2, buf_len - 2); } else { mg_read(conn, buf, buf_len); } is_form_enc = 1; } else { MG_ASSERT(ri->query_string != NULL); // query_string ~ "" when no query string was specified in the request buf_len = strlen(ri->query_string); buf = malloc(buf_len + 1); strcpy(buf, ri->query_string); is_form_enc = 1; } var = malloc(buf_len + 1); var_len = mg_get_var(buf, buf_len, "my_var", var, buf_len + 1, is_form_enc); mg_printf(conn, "Value: [%s]\n", var); mg_printf(conn, "Value size: [%d]\n", var_len); free(buf); free(var); }