/* * 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 handle_save(struct mg_connection *nc, struct http_message *hm) { // Get form variables and store settings values mg_get_http_var(&hm->body, "setting1", s_settings.setting1, sizeof(s_settings.setting1)); mg_get_http_var(&hm->body, "setting2", s_settings.setting2, sizeof(s_settings.setting2)); // Send response mg_printf(nc, "%s", "HTTP/1.1 302 OK\r\nLocation: /\r\n\r\n"); }
static void handle_save(struct mg_connection *nc, struct http_message *hm) { // Get form variables and store settings values mg_get_http_var(&hm->body, "setting1", s_settings.setting1, sizeof(s_settings.setting1)); mg_get_http_var(&hm->body, "setting2", s_settings.setting2, sizeof(s_settings.setting2)); // Send response mg_http_send_redirect(nc, 302, mg_mk_str("/"), mg_mk_str(NULL)); }
static void handle_sum_call(struct mg_connection *nc, struct http_message *hm) { char n1[100], n2[100]; double result; /* Get form variables */ mg_get_http_var(&hm->body, "n1", n1, sizeof(n1)); mg_get_http_var(&hm->body, "n2", n2, sizeof(n2)); /* Send headers */ mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); /* Compute the result and send it back as a JSON object */ result = strtod(n1, NULL) + strtod(n2, NULL); mg_printf_http_chunk(nc, "{ \"result\": %lf }", result); mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */ }
static std::string get_http_var(const struct http_message *hm, const char *name) { char data[4096]; data[0] = '\0'; mg_get_http_var(&hm->body, name, data, sizeof(data)); if (data[0] != '\0') { return data; } mg_get_http_var(&hm->query_string, name, data, sizeof(data)); if (data[0] != '\0') { return data; } return ""; }
bool ccMongooseWebServerRequest::DoHasVarInConnection(const std::string& name) const { if (_pMgHttpMessage == NULL) return false; char outbuf[CV_MAXGETSIZE]; return mg_get_http_var(&_pMgHttpMessage->body, name.c_str(), &outbuf[0], CV_MAXGETSIZE) > 0; }
bool RestServ::user_auth(mg_connection& nc, HttpMessage data) { char user[64]{0x00}, pass[64]{0x00}; auto ul = mg_get_http_var(&(data.get()->body), "user", user, sizeof(user)); auto pl = mg_get_http_var(&(data.get()->body), "pass", pass, sizeof(pass)); try{ if (ul > 0 && pl > 0){ //TODO Check valid }else{ } } catch(std::exception& e){ out_ << e.what(); return false; } return true; }
std::shared_ptr<Session> RestServ::push_session(HttpMessage data) { char user[64]{0x00}, pass[64]{0x00}; auto ul = mg_get_http_var(&(data.get()->body), "user", user, sizeof(user)); auto pl = mg_get_http_var(&(data.get()->body), "pass", pass, sizeof(pass)); auto s = std::make_shared<Session>(); s->created = s->last_used = mg_time(); s->user = std::string(user, ul); s->pass = std::string(pass, pl); s->id = std::hash<std::shared_ptr<Session>>()(s); std::string&& seed = std::string(user) + std::to_string(s->id); s->id = std::hash<std::string>()(seed); session_list_.push_back(s); return s; }
static void handle_switch_call(struct mg_connection *nc, struct http_message *hm) { int val; char *http_status; char type[BUF_SIZE]; char housecode[BUF_SIZE]; char code[BUF_SIZE]; char status[BUF_SIZE]; char tone[BUF_SIZE]; mg_get_http_var(&hm->body, "type", type, sizeof(type)); mg_get_http_var(&hm->body, "housecode", housecode, sizeof(housecode)); mg_get_http_var(&hm->body, "code", code, sizeof(code)); mg_get_http_var(&hm->body, "status", status, sizeof(status)); mg_get_http_var(&hm->body, "tone", tone, sizeof(tone)); if ((val = usbfunk_init()) != USBFUNK_SUCCESS) { http_status = HTTP_503_SERVICE_UNAVAILABLE ; } else if (mg_casecmp(type, "pt0") == 0 && (val = usbfunk_switch_0(atoi(code)) == USBFUNK_SUCCESS)) { http_status = HTTP_200_OK; } else if (mg_casecmp(type, "pt2") == 0 && (val = usbfunk_switch_2(atoi(housecode), code[0], mg_casecmp(status, "off") == 0 ? 0 : 1) == USBFUNK_SUCCESS)) { http_status = HTTP_200_OK; } else if (mg_casecmp(type, "pt4") == 0 && (val = usbfunk_switch_4(atoi(housecode), code[0], mg_casecmp(status, "off") == 0 ? 0 : 1) == USBFUNK_SUCCESS)) { http_status = HTTP_200_OK; } else if (mg_casecmp(type, "he") == 0 && (val = usbfunk_switch_he(atoi(code), mg_casecmp(status, "off") == 0 ? 0 : 1) == USBFUNK_SUCCESS)) { http_status = HTTP_200_OK; } else if (mg_casecmp(type, "raw") == 0 && (val = usbfunk_switch_raw(code) == USBFUNK_SUCCESS)) { http_status = HTTP_200_OK; } else if (mg_casecmp(type, "hx") == 0 && (val = usbfunk_bell(atoi(code), atoi(tone)) == USBFUNK_SUCCESS)) { http_status = HTTP_200_OK; } else { http_status = HTTP_400_BAD_REQUEST; } mg_printf(nc, "HTTP/1.1 %s\r\nTransfer-Encoding: chunked\r\n\r\n", http_status); mg_printf_http_chunk(nc, "%s", http_status); mg_send_http_chunk(nc, "", 0); }
static void op_set(struct mg_connection *nc, const struct http_message *hm, const struct mg_str *key, void *db) { sqlite3_stmt *stmt = NULL; char value[200]; const struct mg_str *body = hm->query_string.len > 0 ? &hm->query_string : &hm->body; mg_get_http_var(body, "value", value, sizeof(value)); if (sqlite3_prepare_v2(db, "INSERT OR REPLACE INTO kv VALUES (?, ?);", -1, &stmt, NULL) == SQLITE_OK) { sqlite3_bind_text(stmt, 1, key->p, key->len, SQLITE_STATIC); sqlite3_bind_text(stmt, 2, value, strlen(value), SQLITE_STATIC); sqlite3_step(stmt); sqlite3_finalize(stmt); } mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); }
std::string ccMongooseWebServerRequest::DoGetVarInConnection(const std::string& name) { std::string strGetData; strGetData.resize(CV_MAXGETSIZE); int sz = -1; if (_pMgHttpMessage != NULL) sz = mg_get_http_var(&_pMgHttpMessage->query_string, name.c_str(), &strGetData[0], strGetData.size()); if (sz == -1) return _strNullData; strGetData.resize(sz); return strGetData; }
static void handle_setled_call(struct mg_connection *nc, struct http_message *hm) { int val; char *http_status; char inverse[BUF_SIZE]; mg_get_http_var(&hm->body, "inverse", inverse, sizeof(inverse)); if ((val = usbfunk_init()) != USBFUNK_SUCCESS) { http_status = HTTP_503_SERVICE_UNAVAILABLE ; } else if ((val = usbfunk_setled(atoi(inverse)) == USBFUNK_SUCCESS)) { http_status = HTTP_200_OK; } else { http_status = HTTP_400_BAD_REQUEST; } mg_printf(nc, "HTTP/1.1 %s\r\nTransfer-Encoding: chunked\r\n\r\n", http_status); mg_printf_http_chunk(nc, "%s", http_status); mg_send_http_chunk(nc, "", 0); }
static void handle_play(struct mg_connection *con, struct http_message *msg) { char stream_uri[200]; mg_get_http_var(&msg->body, "streamURI", stream_uri, sizeof(stream_uri)); mg_printf(con, "%s", "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n"); play_stream(stream_uri); }
/** * Handle mongoose events. These are mostly requests to process incoming * browser requests. The ones we handle are: * GET / - Send the enter details page. * GET /set - Set the connection info (REST request). * POST /ssidSelected - Set the connection info (HTML FORM). */ static void mongoose_event_handler(struct mg_connection *nc, int ev, void *evData) { ESP_LOGD(tag, "- Event: %s", mongoose_eventToString(ev)); switch (ev) { case MG_EV_HTTP_REQUEST: { struct http_message *message = (struct http_message *) evData; char *uri = mgStrToStr(message->uri); ESP_LOGD(tag, " - uri: %s", uri); if (strcmp(uri, "/set") ==0 ) { connection_info_t connectionInfo; //fix saveConnectionInfo(&connectionInfo); ESP_LOGD(tag, "- Set the new connection info to ssid: %s, password: %s", connectionInfo.ssid, connectionInfo.password); mg_send_head(nc, 200, 0, "Content-Type: text/plain"); } if (strcmp(uri, "/") == 0) { mg_send_head(nc, 200, sizeof(selectAP_html), "Content-Type: text/html"); mg_send(nc, selectAP_html, sizeof(selectAP_html)); } // Handle /ssidSelected // This is an incoming form with properties: // * ssid - The ssid of the network to connect against. // * password - the password to use to connect. // * ip - Static IP address ... may be empty // * gw - Static GW address ... may be empty // * netmask - Static netmask ... may be empty if(strcmp(uri, "/ssidSelected") == 0) { // We have received a form page containing the details. The form body will // contain: // ssid=<value>&password=<value> ESP_LOGD(tag, "- body: %.*s", message->body.len, message->body.p); connection_info_t connectionInfo; mg_get_http_var(&message->body, "ssid", connectionInfo.ssid, SSID_SIZE); mg_get_http_var(&message->body, "password", connectionInfo.password, PASSWORD_SIZE); char ipBuf[20]; if (mg_get_http_var(&message->body, "ip", ipBuf, sizeof(ipBuf)) > 0) { inet_pton(AF_INET, ipBuf, &connectionInfo.ipInfo.ip); } else { connectionInfo.ipInfo.ip.addr = 0; } if (mg_get_http_var(&message->body, "gw", ipBuf, sizeof(ipBuf)) > 0) { inet_pton(AF_INET, ipBuf, &connectionInfo.ipInfo.gw); } else { connectionInfo.ipInfo.gw.addr = 0; } if (mg_get_http_var(&message->body, "netmask", ipBuf, sizeof(ipBuf)) > 0) { inet_pton(AF_INET, ipBuf, &connectionInfo.ipInfo.netmask); } else { connectionInfo.ipInfo.netmask.addr = 0; } ESP_LOGD(tag, "ssid: %s, password: %s", connectionInfo.ssid, connectionInfo.password); mg_send_head(nc, 200, 0, "Content-Type: text/plain"); saveConnectionInfo(&connectionInfo); bootWiFi2(); } // url is "/ssidSelected" // Else ... unknown URL else { mg_send_head(nc, 404, 0, "Content-Type: text/plain"); } nc->flags |= MG_F_SEND_AND_CLOSE; free(uri); break; } // MG_EV_HTTP_REQUEST } // End of switch } // End of mongoose_event_handler