Fix enduser_setup default POST request (#2852)

This commit is contained in:
Gregor Hartmann 2019-08-01 21:31:18 +02:00 committed by Marcel Stör
parent 49ac968bde
commit 0659e5529e
1 changed files with 36 additions and 26 deletions

View File

@ -397,18 +397,25 @@ static err_t close_once_sent (void *arg, struct tcp_pcb *pcb, u16_t len)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
/** /**
* Search String * Get length of param value
* *
* Search string for first occurence of any char in srch_str. * This is being called with a fragment of the parameters passed in the
* URL for GET requests or part of the body of a POST request.
* The string will look like one of these
* "SecretPassword HTTP/1.1"
* "SecretPassword&wifi_ssid=..."
* "SecretPassword"
* The string is searched for the first occurence of deliemiter '&' or ' '.
* If found return the length up to that position.
* If not found return the length of the string.
* *
* @return -1 if no occurence of char was found.
*/ */
static int enduser_setup_srch_str(const char *str, const char *srch_str) static int enduser_setup_get_lenth_of_param_value(const char *str)
{ {
char *found = strpbrk (str, srch_str); char *found = strpbrk (str, "& ");
if (!found) if (!found)
{ {
return -1; return strlen(str);
} }
else else
{ {
@ -469,6 +476,11 @@ static int enduser_setup_http_load_payload(void)
{ {
ENDUSER_SETUP_DEBUG("Unable to load file enduser_setup.html, loading default HTML..."); ENDUSER_SETUP_DEBUG("Unable to load file enduser_setup.html, loading default HTML...");
if (f)
{
vfs_close(f);
}
sprintf(cl_hdr, http_header_content_len_fmt, sizeof(enduser_setup_html_default)); sprintf(cl_hdr, http_header_content_len_fmt, sizeof(enduser_setup_html_default));
cl_len = strlen(cl_hdr); cl_len = strlen(cl_hdr);
int html_len = LITLEN(enduser_setup_html_default); int html_len = LITLEN(enduser_setup_html_default);
@ -801,8 +813,8 @@ static int enduser_setup_http_handle_credentials(char *data, unsigned short data
state->success = 0; state->success = 0;
state->lastStationStatus = 0; state->lastStationStatus = 0;
char *name_str = (char *) ((uint32_t)strstr(&(data[6]), "wifi_ssid=")); char *name_str = strstr(data, "wifi_ssid=");
char *pwd_str = (char *) ((uint32_t)strstr(&(data[6]), "wifi_password=")); char *pwd_str = strstr(data, "wifi_password=");
if (name_str == NULL || pwd_str == NULL) if (name_str == NULL || pwd_str == NULL)
{ {
ENDUSER_SETUP_DEBUG("Password or SSID string not found"); ENDUSER_SETUP_DEBUG("Password or SSID string not found");
@ -814,13 +826,8 @@ static int enduser_setup_http_handle_credentials(char *data, unsigned short data
char *name_str_start = name_str + name_field_len; char *name_str_start = name_str + name_field_len;
char *pwd_str_start = pwd_str + pwd_field_len; char *pwd_str_start = pwd_str + pwd_field_len;
int name_str_len = enduser_setup_srch_str(name_str_start, "& "); int name_str_len = enduser_setup_get_lenth_of_param_value(name_str_start);
int pwd_str_len = enduser_setup_srch_str(pwd_str_start, "& "); int pwd_str_len = enduser_setup_get_lenth_of_param_value(pwd_str_start);
if (name_str_len == -1 || pwd_str_len == -1)
{
ENDUSER_SETUP_DEBUG("Password or SSID HTTP paramter divider not found");
return 1;
}
struct station_config *cnf = luaM_malloc(lua_getstate(), sizeof(struct station_config)); struct station_config *cnf = luaM_malloc(lua_getstate(), sizeof(struct station_config));
@ -1111,22 +1118,25 @@ static void enduser_setup_handle_OPTIONS (struct tcp_pcb *http_client, char *dat
} }
static err_t enduser_setup_handle_POST(struct tcp_pcb *http_client, char* data, size_t data_len) static void enduser_setup_handle_POST(struct tcp_pcb *http_client, char* data, size_t data_len)
{ {
ENDUSER_SETUP_DEBUG("Handling POST"); ENDUSER_SETUP_DEBUG("Handling POST");
if (strncmp(data + 5, "/setwifi ", 9) == 0) // User clicked the submit button if (strncmp(data + 5, "/setwifi ", 9) == 0) // User clicked the submit button
{ {
switch (enduser_setup_http_handle_credentials(data, data_len)) char* body=strstr(data, "\r\n\r\n");
char *content_length_str = strstr(data, "Content-Length: ");
if( body == NULL || content_length_str == NULL)
{
enduser_setup_http_serve_header(http_client, http_header_400, LITLEN(http_header_400));
return;
}
int bodylength = atoi(content_length_str + 16);
body += 4; // length of the double CRLF found above
switch (enduser_setup_http_handle_credentials(body, bodylength))
{ {
case 0: { case 0: {
// all went fine, extract all the form data into a file // all went fine, extract all the form data into a file
char* body=strstr(data, "\r\n\r\n");
char *content_length_str = strstr(data, "Content-Length: ");
if( body != NULL && content_length_str != NULL){
int bodylength = atoi(content_length_str + 16);
body += 4; // length of the double CRLF found above
enduser_setup_write_file_with_extra_configuration_data(body, bodylength); enduser_setup_write_file_with_extra_configuration_data(body, bodylength);
}
// redirect user to the base page with the trying flag // redirect user to the base page with the trying flag
enduser_setup_http_serve_header(http_client, http_header_302_trying, LITLEN(http_header_302_trying)); enduser_setup_http_serve_header(http_client, http_header_302_trying, LITLEN(http_header_302_trying));
break; break;
@ -1135,7 +1145,7 @@ static err_t enduser_setup_handle_POST(struct tcp_pcb *http_client, char* data,
enduser_setup_http_serve_header(http_client, http_header_400, LITLEN(http_header_400)); enduser_setup_http_serve_header(http_client, http_header_400, LITLEN(http_header_400));
break; break;
default: default:
ENDUSER_SETUP_ERROR("http_recvcb failed. Failed to handle wifi credentials.", ENDUSER_SETUP_ERR_UNKOWN_ERROR, ENDUSER_SETUP_ERR_NONFATAL); ENDUSER_SETUP_ERROR_VOID("http_recvcb failed. Failed to handle wifi credentials.", ENDUSER_SETUP_ERR_UNKOWN_ERROR, ENDUSER_SETUP_ERR_NONFATAL);
break; break;
} }
} }