static int serve_request(struct mg_connection *conn) { if ((strcmp(conn->uri, "/login.html") == 0&&strcmp(conn->request_method, "POST") == 0)||(strcmp(conn->uri, "/") == 0&&strcmp(conn->request_method, "POST") == 0)) { check_auth(conn); return MG_MORE; } if(!strcmp(conn->uri,"/")||!strcmp(conn->uri,"/login.html")) { mg_send_file(conn, "D:\\Users\\Administrator\\Desktop\\EasyDarwin\\EasyDarwin\\WinNTSupport\\Debug\\login.html", NULL); return MG_MORE; } if(!strcmp(conn->uri,"/language.js")) { mg_send_file(conn, "D:\\Users\\Administrator\\Desktop\\EasyDarwin\\EasyDarwin\\WinNTSupport\\Debug\\language.js", NULL); return MG_MORE; } if(!strcmp(conn->uri,"/english/language.js")) { mg_send_file(conn, "D:\\Users\\Administrator\\Desktop\\EasyDarwin\\EasyDarwin\\WinNTSupport\\Debug\\english\\language.js", NULL); return MG_MORE; } if(!strcmp(conn->uri,"/chinese/language.js")) { mg_send_file(conn, "D:\\Users\\Administrator\\Desktop\\EasyDarwin\\EasyDarwin\\WinNTSupport\\Debug\\chinese\\language.js", NULL); return MG_MORE; } return MG_FALSE; }
static void check_auth(struct mg_connection *conn) { char name[100], password[100]; // Get form variables mg_get_var(conn, "name", name, sizeof(name)); mg_get_var(conn, "password", password, sizeof(password)); if (strcmp(name, "1") == 0 && strcmp(password, "1") == 0) { mg_send_file(conn, "D:\\Users\\Administrator\\Desktop\\EasyDarwin\\EasyDarwin\\WinNTSupport\\Debug\\index.html", NULL); } else { mg_send_file(conn, "D:\\Users\\Administrator\\Desktop\\EasyDarwin\\EasyDarwin\\WinNTSupport\\Debug\\loginerror.html", NULL); } }
static void check_auth(struct mg_connection *conn) { char name[100], password[100]; // Get form variables mg_get_var(conn, "name", name, sizeof(name)); mg_get_var(conn, "password", password, sizeof(password)); if (strcmp(name, "1") == 0 && strcmp(password, "1") == 0) { mg_send_file(conn, "index.html", NULL); } else { mg_send_file(conn, "loginerror.html", NULL); } }
/* Serve a file (html, icon, css, js) */ static int web_serveFile(struct mg_connection *conn, const char *file) { web_server _this = conn->server_param; if (cx_fileTest(file) || !_this->defaultToConsole) { mg_send_file(conn, file, ""); } else { cx_id buffer; char *cortexHome = getenv("CORTEX_HOME"); sprintf(buffer, "%s/interface/web/%s", cortexHome, file); mg_send_file(conn, buffer, ""); } return MG_MORE; /* Indicate that page will request more data */ }
int RestfulApi::handle_api_requests(struct mg_connection *conn) { std::string restUri(conn->uri); if (restUri.compare("/restful/mult")==0) { ApiWrap_Mult(conn); return MG_TRUE; } // get nym id if (restUri.compare("/restful/getnym")==0) { ApiWrap_OT(conn,1); return MG_TRUE; } // get pubkey if (restUri.compare("/restful/getpubkey")==0) { ApiWrap_OT(conn,2); return MG_TRUE; } //the request is wrong go to the home page mg_send_file(conn, s_authenticated_uri, s_no_cache_header); return MG_MORE; }
int FileHandler(struct mg_connection *conn, void *cbdata) { /* In this handler, we ignore the req_info and send the file "fileName". */ const char *fileName = (const char *)cbdata; mg_send_file(conn, fileName); return 1; }
static int send_reply(struct mg_connection *conn) { if (conn->is_websocket) { // This handler is called for each incoming websocket frame, one or more // times for connection lifetime. // Echo websocket data back to the client. mg_websocket_write(conn, 1, conn->content, conn->content_len); return conn->content_len == 4 && !memcmp(conn->content, "exit", 4) ? MG_FALSE : MG_TRUE; } else { mg_send_file(conn, "index.html", NULL); return MG_MORE; } }
static int ev_handler(struct mg_connection *conn, enum mg_event ev) { switch (ev) { case MG_AUTH: return MG_TRUE; case MG_REQUEST: if (!strcmp(conn->uri, "/ind")) { // handle_restful_call(conn); show_my_index(conn); return MG_TRUE; } mg_send_file(conn, "index.html", s_no_cache_header); return MG_MORE; default: return MG_FALSE; } }
static int serve_request(struct mg_connection *conn) { char htmlPath[256] = { 0 }; sprintf(htmlPath, "%s%s", sDocumentRoot, conn->uri); if(strcmp(conn->uri, "/EasyHLSModule") == 0) { mg_easy_send(conn); return MG_TRUE; } if ((strcmp(conn->uri, "/login.html") == 0&&strcmp(conn->request_method, "POST") == 0)||(strcmp(conn->uri, "/") == 0&&strcmp(conn->request_method, "POST") == 0)) { check_auth(conn); return MG_MORE; } if(!strcmp(conn->uri,"/")||!strcmp(conn->uri,"/login.html")) { mg_send_file(conn, (const char*)htmlPath, NULL); return MG_MORE; } if(!strcmp(conn->uri,"/language.js")) { mg_send_file(conn, (const char*)htmlPath, NULL); return MG_MORE; } if(!strcmp(conn->uri,"/english/language.js")) { mg_send_file(conn, (const char*)htmlPath, NULL); return MG_MORE; } if(!strcmp(conn->uri,"/chinese/language.js")) { mg_send_file(conn, (const char*)htmlPath, NULL); return MG_MORE; } return MG_FALSE; }
static int event_handler(struct mg_connection *conn, enum mg_event ev) { if(ev == MG_REQUEST) { if(conn->is_websocket) { ws_handler(conn, FLAG_WS_DATA); return MG_TRUE; } else { int ret = web_handler(conn); if(ret < 0) { const char* srvfilepath = strlen(conn->uri) <= 1 ? "index.html" : conn->uri; if(srvfilepath[0] == '/') srvfilepath++; mg_send_file(conn, srvfilepath, NULL); return MG_MORE; } return MG_TRUE; } } else if(ev == MG_WS_CONNECT) { conn->connection_param = malloc(25); const char* key = mg_get_header(conn, "Sec-WebSocket-Key"); strncpy(conn->connection_param, key, 24); char *p = (char*)(conn->connection_param); p[24] = '\0'; ws_handler(conn, FLAG_WS_OPEN); return MG_TRUE; } else if(ev == MG_CLOSE) { if(conn->is_websocket) { ws_handler(conn, FLAG_WS_CLOSE); } free(conn->connection_param); return MG_TRUE; } else if(ev == MG_AUTH) { return MG_TRUE; } return MG_FALSE; }
// 4. GET_FILE static void proc_cmd_get_file(struct mg_connection *conn, const struct mg_request_info *request_info) { char str_file_path[200]; char str_full_path[200]; memset(str_file_path, 0, 200); memset(str_full_path, 0, 200); get_qsvar(request_info, "path", str_file_path, sizeof(str_file_path)); if (str_file_path == NULL || strlen(str_file_path) == 0) { logger_remotem("GET_FILE FAILED: no path entered"); mg_printf( conn, "HTTP/1.0 200 OK\r\n" "Content-Type: text/plain;charset=iso-8859-1\r\n\r\n" "errno=%d\r\n", INPUT_PARAM_NOT_SPECIFIED); return; } char* str_file_name = basename(str_file_path); if ((my_str_cmp(str_file_path, UPLOAD_DIRECTORY) && my_str_cmp(str_file_path, CONF_DIRECTORY) && my_str_cmp(str_file_path, LOG_DIRECTORY) && my_str_cmp(str_file_path, DATA_DIRECTORY)) || (strlen(str_file_name) == 0)) { logger_remotem("GET_FILE FAILED: wrong path %s", str_file_path); mg_printf( conn, "HTTP/1.0 200 OK\r\n" "Content-Type: text/plain;charset=iso-8859-1\r\n\r\n" "errno=%d\r\n", INPUT_PARAM_NOT_SPECIFIED); return; } sprintf(str_full_path, "%s%s", ODI_HOME, str_file_path); logger_remotem("GET_FILE STARTED: %s, client=%s", str_full_path, ip2s(request_info->remote_ip)); mg_send_file(conn, str_full_path); logger_remotem("GET_FILE COMPLETE, clearing caches: %s", str_full_path); system("echo 3 > /proc/sys/vm/drop_caches"); }
void HTTPService_HandleDownloadRequest(mg_connection* conn, const mg_request_info* request_info) { char filename[MAX_PATH]; const char* requestedFile = &request_info->uri[4]; if (!SV_IsClientIP(request_info->remote_ip)) { HTTPService_Forbidden(conn); return; } if (!FS_GetFileForDownload(requestedFile, filename, sizeof(filename))) { HTTPService_NotFound(conn); return; } mg_send_file(conn, filename); }
static int ev_handler(struct mg_connection *conn, enum mg_event ev) { std::string uri; const char* secret = NULL; const char* requested_with = NULL; int retval = MG_FALSE; switch (ev) { case MG_REQUEST: boinc_resolve_filename_s(conn->uri+1, uri); mg_send_file( conn, uri.c_str(), "Access-Control-Allow-Origin: *\r\n" ); return MG_MORE; case MG_AUTH: retval = MG_FALSE; if (!webserver_debugging) { secret = mg_get_header(conn, "Secret"); if (secret) { if (0 == strcmp(secret, webserver_secret)) { retval = MG_TRUE; } } requested_with = mg_get_header(conn, "X-Requested-With"); if (requested_with) { retval = MG_TRUE; } } else { retval = MG_TRUE; } return retval; default: return MG_FALSE; } }
static int ev_handler(struct mg_connection *conn, enum mg_event ev) { switch (ev) { case MG_AUTH: return MG_TRUE; case MG_REQUEST: printf("==> [%s] [%s]\n", conn->request_method, conn->uri); if (strcmp(conn->request_method, "CONNECT") == 0) { char host[1025] = ""; int i, is_ssl, port = 0; sscanf(conn->uri, "%1024[^:]:%d", host, &port); is_ssl = (port == 443 ? 1 : 0); // Iterate over existing wrapper, see if we can use one of them for (i = 0; i < (int) ARRAY_SIZE(s_wrappers); i++) { if (strcmp(host, s_wrappers[i].requested_host) == 0 && is_ssl == (s_wrappers[i].c.ssl_cert == NULL ? 0 : 1)) { // Found usable wrapper, tunnel to it. mg_forward(conn, "127.0.0.1", atoi(s_wrappers[i].c.listening_port), 0); return MG_MORE; } } // No suitable wrappers found. Disallow that CONNECT request. mg_send_status(conn, 405); return MG_TRUE; } // Not a CONNECT request, serve HTML file. mg_send_file(conn, "ws_ssl.html"); return MG_MORE; default: return MG_FALSE; } }
static int ev_handler(struct mg_connection *conn, enum mg_event ev) { switch (ev) { case MG_REQUEST: if (conn->is_websocket) { handle_websocket_message(conn); return MG_TRUE; } else { mg_send_file(conn, "webchat.html", NULL); // Return MG_MORE after! return MG_MORE; } case MG_WS_CONNECT: // New websocket connection. Send connection ID back to the client. conn->connection_param = calloc(1, sizeof(struct conn_data)); mg_websocket_printf(conn, WEBSOCKET_OPCODE_TEXT, "id %p", conn); return MG_FALSE; case MG_CLOSE: free(conn->connection_param); return MG_TRUE; case MG_AUTH: return MG_TRUE; default: return MG_FALSE; } }
// This function will be called by mongoose on every new request. int web_engine::begin_request_handler(struct mg_connection *conn) { const struct mg_request_info *request_info = mg_get_request_info(conn); if (!strncmp(request_info->uri, "/json/",6)) { if (!strcmp(request_info->uri, "/json/game")) { return json_game_handler(conn); } if (!strcmp(request_info->uri, "/json/slider")) { return json_slider_handler(conn); } } else if (!strncmp(request_info->uri, "/cmd",4)) { char cmd_name[64]; get_qsvar(request_info, "name", cmd_name, sizeof(cmd_name)); if(!strcmp(cmd_name,"softreset")) { m_machine->schedule_soft_reset(); } else if(!strcmp(cmd_name,"hardreset")) { m_machine->schedule_hard_reset(); } else if(!strcmp(cmd_name,"exit")) { m_machine->schedule_exit(); } else if(!strcmp(cmd_name,"togglepause")) { if (m_machine->paused()) m_machine->resume(); else m_machine->pause(); } else if(!strcmp(cmd_name,"savestate")) { char cmd_val[64]; get_qsvar(request_info, "val", cmd_val, sizeof(cmd_val)); char *filename = websanitize_statefilename(cmd_val); m_machine->schedule_save(filename); } else if(!strcmp(cmd_name,"loadstate")) { char cmd_val[64]; get_qsvar(request_info, "val", cmd_val, sizeof(cmd_val)); char *filename = cmd_val; m_machine->schedule_load(filename); } else if(!strcmp(cmd_name,"loadauto")) { // This is here to just load the autosave and only the autosave. m_machine->schedule_load("auto"); } // Send HTTP reply to the client mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Content-Length: 2\r\n" // Always set Content-Length "\r\n" "OK"); // Returning non-zero tells mongoose that our function has replied to // the client, and mongoose should not send client any more data. return 1; } else if (!strncmp(request_info->uri, "/slider",7)) { char cmd_id[64]; char cmd_val[64]; get_qsvar(request_info, "id", cmd_id, sizeof(cmd_id)); get_qsvar(request_info, "val", cmd_val, sizeof(cmd_val)); int cnt = 0; int id = atoi(cmd_id); const slider_state *curslider; for (curslider = machine().ui().get_slider_list(); curslider != NULL; curslider = curslider->next) { if (cnt==id) (*curslider->update)(machine(), curslider->arg, NULL, atoi(cmd_val)); cnt++; } for (curslider = (slider_state*)machine().osd().get_slider_list(); curslider != NULL; curslider = curslider->next) { if (cnt==id) (*curslider->update)(machine(), curslider->arg, NULL, atoi(cmd_val)); cnt++; } // Send HTTP reply to the client mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Content-Length: 2\r\n" // Always set Content-Length "\r\n" "OK"); // Returning non-zero tells mongoose that our function has replied to // the client, and mongoose should not send client any more data. return 1; } else if (!strncmp(request_info->uri, "/screenshot.png",15)) { screen_device_iterator iter(m_machine->root_device()); screen_device *screen = iter.first(); if (screen == NULL) { return 0; } astring fname("screenshot.png"); emu_file file(m_machine->options().snapshot_directory(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS); file_error filerr = file.open(fname); if (filerr != FILERR_NONE) { return 0; } m_machine->video().save_snapshot(screen, file); astring fullpath(file.fullpath()); file.close(); mg_send_file(conn,fullpath); return 1; } return 0; }
// This function will be called by mongoose on every new request. int web_engine::begin_request_handler(struct mg_connection *conn) { astring file_path(mg_get_option(m_server, "document_root"), PATH_SEPARATOR, conn->uri); if (filename_endswith(file_path.c_str(), ".lp")) { FILE *fp = NULL; if ((fp = fopen(file_path.c_str(), "rb")) != NULL) { fseek (fp, 0, SEEK_END); size_t size = ftell(fp); fseek (fp, 0, SEEK_SET); char *data = (char*)mg_mmap(fp,size); lua_State *L = luaL_newstate(); prepare_lua_environment(conn, L); lsp(conn, data, (int) size, L); if (L != NULL) lua_close(L); mg_munmap(data,size); fclose(fp); return MG_TRUE; } else { return MG_FALSE; } } else if (!strncmp(conn->uri, "/json/",6)) { if (!strcmp(conn->uri, "/json/game")) { return json_game_handler(conn); } if (!strcmp(conn->uri, "/json/slider")) { return json_slider_handler(conn); } } else if (!strncmp(conn->uri, "/keypost",8)) { // Is there any sane way to determine the length of the buffer before getting it? // A request for a way was previously filed with the mongoose devs, // but it looks like it was never implemented. // For now, we'll allow a paste buffer of 32k. // To-do: Send an error if the paste is too big? char cmd_val[32768]; int pastelength = mg_get_var(conn, "val", cmd_val, sizeof(cmd_val)); if (pastelength > 0) { machine().ioport().natkeyboard().post_utf8(cmd_val); } // Send HTTP reply to the client mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Content-Length: 2\r\n" // Always set Content-Length "\r\n" "OK"); // Returning non-zero tells mongoose that our function has replied to // the client, and mongoose should not send client any more data. return MG_TRUE; } else if (!strncmp(conn->uri, "/keyupload",8)) { char *upload_data; int data_length, ofs = 0; char var_name[100], file_name[255]; while ((ofs = mg_parse_multipart(conn->content + ofs, conn->content_len - ofs, var_name, sizeof(var_name), file_name, sizeof(file_name), (const char **)&upload_data, &data_length)) > 0) { mg_printf_data(conn, "File: %s, size: %d bytes", file_name, data_length); } // That upload_data contains more than we need. It also has the headers. // We'll need to strip it down to just what we want. if ((&data_length > 0) && (sizeof(file_name) > 0)) { // MSVC doesn't yet support variable-length arrays, so chop the string the old-fashioned way upload_data[data_length] = '\0'; // Now paste the stripped down paste_data.. machine().ioport().natkeyboard().post_utf8(upload_data); } return MG_TRUE; } else if (!strncmp(conn->uri, "/cmd",4)) { char cmd_name[64]; mg_get_var(conn, "name", cmd_name, sizeof(cmd_name)); if(!strcmp(cmd_name,"softreset")) { m_machine->schedule_soft_reset(); } else if(!strcmp(cmd_name,"hardreset")) { m_machine->schedule_hard_reset(); } else if(!strcmp(cmd_name,"exit")) { m_machine->schedule_exit(); } else if(!strcmp(cmd_name,"togglepause")) { if (m_machine->paused()) m_machine->resume(); else m_machine->pause(); } else if(!strcmp(cmd_name,"savestate")) { char cmd_val[64]; mg_get_var(conn, "val", cmd_val, sizeof(cmd_val)); char *filename = websanitize_statefilename(cmd_val); m_machine->schedule_save(filename); } else if(!strcmp(cmd_name,"loadstate")) { char cmd_val[64]; mg_get_var(conn, "val", cmd_val, sizeof(cmd_val)); char *filename = cmd_val; m_machine->schedule_load(filename); } else if(!strcmp(cmd_name,"loadauto")) { // This is here to just load the autosave and only the autosave. m_machine->schedule_load("auto"); } // Send HTTP reply to the client mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Content-Length: 2\r\n" // Always set Content-Length "\r\n" "OK"); // Returning non-zero tells mongoose that our function has replied to // the client, and mongoose should not send client any more data. return MG_TRUE; } else if (!strncmp(conn->uri, "/slider",7)) { char cmd_id[64]; char cmd_val[64]; mg_get_var(conn, "id", cmd_id, sizeof(cmd_id)); mg_get_var(conn, "val", cmd_val, sizeof(cmd_val)); int cnt = 0; int id = atoi(cmd_id); const slider_state *curslider; for (curslider = machine().ui().get_slider_list(); curslider != NULL; curslider = curslider->next) { if (cnt==id) (*curslider->update)(machine(), curslider->arg, NULL, atoi(cmd_val)); cnt++; } for (curslider = (slider_state*)machine().osd().get_slider_list(); curslider != NULL; curslider = curslider->next) { if (cnt==id) (*curslider->update)(machine(), curslider->arg, NULL, atoi(cmd_val)); cnt++; } // Send HTTP reply to the client mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Content-Length: 2\r\n" // Always set Content-Length "\r\n" "OK"); // Returning non-zero tells mongoose that our function has replied to // the client, and mongoose should not send client any more data. return MG_TRUE; } else if (!strncmp(conn->uri, "/screenshot.png",15)) { screen_device_iterator iter(m_machine->root_device()); screen_device *screen = iter.first(); if (screen == NULL) { return 0; } astring fname("screenshot.png"); emu_file file(m_machine->options().snapshot_directory(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS); file_error filerr = file.open(fname.c_str()); if (filerr != FILERR_NONE) { return 0; } m_machine->video().save_snapshot(screen, file); astring fullpath(file.fullpath()); file.close(); mg_send_header(conn, "Cache-Control", "no-cache, no-store, must-revalidate"); mg_send_header(conn, "Pragma", "no-cache"); mg_send_header(conn, "Expires", "0"); mg_send_file(conn, fullpath.c_str(), NULL); return MG_MORE; // It is important to return MG_MORE after mg_send_file! } return 0; }