parse x-www-form-urlencoded post body

This commit is contained in:
Seven Du 2014-09-06 16:59:53 +08:00
parent f5c0c13931
commit f3616557b6
1 changed files with 44 additions and 2 deletions

View File

@ -1412,6 +1412,45 @@ static void http_run(jsock_t *jsock)
goto err; goto err;
} }
if (!strncmp(request.method, "POST", 4) && request.content_length &&
!strncmp(request.content_type, "application/x-www-form-urlencoded", 33)) {
char *buffer = NULL;
int len = 0, bytes = 0;
if (request.content_length > 2 * 1024 * 1024 - 1) {
char *data = "HTTP/1.1 413 Request Entity Too Large\r\n"
"Connection: close\r\n\r\n";
ws_raw_write(&jsock->ws, data, strlen(data));
goto done;
}
if (!(buffer = malloc(2 * 1024 * 1024))) {
goto request_err;
}
if (request._unparsed_len) {
bytes += request._unparsed_len;
memcpy(buffer, request._unparsed_data, bytes);
}
while(bytes < request.content_length) {
len = request.content_length - bytes;
if ((len = ws_raw_read(&jsock->ws, buffer + bytes, len, jsock->ws.block)) < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Read error %d\n", len);
goto done;
}
bytes += len;
}
*(buffer + bytes) = '\0';
switch_http_parse_qs(&request, buffer);
free(buffer);
}
// switch_http_dump_request(&request); // switch_http_dump_request(&request);
/* TODO: parse virtual hosts here */ /* TODO: parse virtual hosts here */
@ -1468,8 +1507,7 @@ static void http_run(jsock_t *jsock)
} }
if (!(params = cJSON_CreateObject())) { if (!(params = cJSON_CreateObject())) {
switch_http_free_request(&request); goto request_err;
goto err;
} }
cJSON_AddItemToObject(params, "login", cJSON_CreateString(auth_user)); cJSON_AddItemToObject(params, "login", cJSON_CreateString(auth_user));
@ -1534,6 +1572,10 @@ done:
switch_http_free_request(&request); switch_http_free_request(&request);
return; return;
request_err:
switch_http_free_request(&request);
err: err:
data = "HTTP/1.1 500 Internal Server Error\r\n" data = "HTTP/1.1 500 Internal Server Error\r\n"
"Connection: close\r\n\r\n"; "Connection: close\r\n\r\n";