mirror of
https://github.com/lammertb/libhttp.git
synced 2025-08-12 01:22:40 +03:00
Code cleanup
This commit is contained in:
@@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* ============
|
||||
* Release: 1.8
|
||||
* Release: 2.0
|
||||
*/
|
||||
|
||||
#include "httplib_main.h"
|
||||
@@ -46,33 +46,37 @@ static int url_encoded_field_found(const struct httplib_connection *conn,
|
||||
|
||||
if (((size_t)key_dec_len >= (size_t)sizeof(key_dec)) || (key_dec_len < 0)) return FORM_FIELD_STORAGE_SKIP;
|
||||
|
||||
if (filename) {
|
||||
filename_dec_len = httplib_url_decode(filename, (int)filename_len, filename_dec, (int)sizeof(filename_dec), 1);
|
||||
if ( filename != NULL ) {
|
||||
|
||||
if (((size_t)filename_dec_len >= (size_t)sizeof(filename_dec))
|
||||
|| (filename_dec_len < 0)) {
|
||||
/* Log error message and skip this field. */
|
||||
httplib_cry(conn, "%s: Cannot decode filename", __func__);
|
||||
filename_dec_len = httplib_url_decode( filename, (int)filename_len, filename_dec, (int)sizeof(filename_dec), 1 );
|
||||
|
||||
if (((size_t)filename_dec_len >= (size_t)sizeof(filename_dec)) || (filename_dec_len < 0)) {
|
||||
|
||||
/*
|
||||
* Log error message and skip this field.
|
||||
*/
|
||||
|
||||
httplib_cry( conn, "%s: Cannot decode filename", __func__ );
|
||||
return FORM_FIELD_STORAGE_SKIP;
|
||||
}
|
||||
|
||||
} else filename_dec[0] = 0;
|
||||
|
||||
ret = fdh->field_found(key_dec, filename_dec, path, path_len, fdh->user_data);
|
||||
ret = fdh->field_found( key_dec, filename_dec, path, path_len, fdh->user_data );
|
||||
|
||||
if ((ret & 0xF) == FORM_FIELD_STORAGE_GET) {
|
||||
if ( (ret & 0xF) == FORM_FIELD_STORAGE_GET ) {
|
||||
|
||||
if (fdh->field_get == NULL) {
|
||||
if ( fdh->field_get == NULL ) {
|
||||
|
||||
httplib_cry(conn, "%s: Function \"Get\" not available", __func__);
|
||||
httplib_cry( conn, "%s: Function \"Get\" not available", __func__ );
|
||||
return FORM_FIELD_STORAGE_SKIP;
|
||||
}
|
||||
}
|
||||
if ((ret & 0xF) == FORM_FIELD_STORAGE_STORE) {
|
||||
if ( (ret & 0xF) == FORM_FIELD_STORAGE_STORE ) {
|
||||
|
||||
if (fdh->field_store == NULL) {
|
||||
if ( fdh->field_store == NULL ) {
|
||||
|
||||
httplib_cry(conn, "%s: Function \"Store\" not available", __func__);
|
||||
httplib_cry( conn, "%s: Function \"Store\" not available", __func__ );
|
||||
return FORM_FIELD_STORAGE_SKIP;
|
||||
}
|
||||
}
|
||||
@@ -104,15 +108,16 @@ static int url_encoded_field_get(const struct httplib_connection *conn,
|
||||
return FORM_FIELD_STORAGE_ABORT;
|
||||
}
|
||||
|
||||
httplib_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1);
|
||||
httplib_url_decode( key, (int)key_len, key_dec, (int)sizeof(key_dec), 1 );
|
||||
|
||||
value_dec_len = httplib_url_decode(value, (int)value_len, value_dec, (int)value_len + 1, 1);
|
||||
value_dec_len = httplib_url_decode( value, (int)value_len, value_dec, (int)value_len + 1, 1 );
|
||||
|
||||
ret = fdh->field_get(key_dec, value_dec, (size_t)value_dec_len, fdh->user_data);
|
||||
ret = fdh->field_get( key_dec, value_dec, (size_t)value_dec_len, fdh->user_data );
|
||||
httplib_free( value_dec );
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
} /* url_encoded_field_get */
|
||||
|
||||
|
||||
static int unencoded_field_get(const struct httplib_connection *conn,
|
||||
@@ -123,36 +128,41 @@ static int unencoded_field_get(const struct httplib_connection *conn,
|
||||
struct httplib_form_data_handler *fdh) {
|
||||
|
||||
char key_dec[1024];
|
||||
(void)conn;
|
||||
|
||||
httplib_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1);
|
||||
UNUSED_PARAMETER(conn);
|
||||
|
||||
return fdh->field_get(key_dec, value, value_len, fdh->user_data);
|
||||
}
|
||||
httplib_url_decode( key, (int)key_len, key_dec, (int)sizeof(key_dec), 1 );
|
||||
|
||||
return fdh->field_get( key_dec, value, value_len, fdh->user_data );
|
||||
|
||||
} /* unencoded_field_get */
|
||||
|
||||
|
||||
static int field_stored(const struct httplib_connection *conn, const char *path, int64_t file_size, struct httplib_form_data_handler *fdh) {
|
||||
static int field_stored( const struct httplib_connection *conn, const char *path, int64_t file_size, struct httplib_form_data_handler *fdh ) {
|
||||
|
||||
/* Equivalent to "upload" callback of "httplib_upload". */
|
||||
/*
|
||||
* Equivalent to "upload" callback of "httplib_upload".
|
||||
*/
|
||||
|
||||
(void)conn; /* we do not need httplib_cry here, so conn is currently unused */
|
||||
UNUSED_PARAMETER(conn); /* we do not need httplib_cry here, so conn is currently unused */
|
||||
|
||||
return fdh->field_store(path, file_size, fdh->user_data);
|
||||
return fdh->field_store( path, file_size, fdh->user_data );
|
||||
}
|
||||
|
||||
|
||||
static const char * search_boundary(const char *buf, size_t buf_len, const char *boundary, size_t boundary_len) {
|
||||
|
||||
/* We must do a binary search here, not a string search, since the buffer
|
||||
* may contain '\x00' bytes, if binary data is transferred. */
|
||||
/*
|
||||
* We must do a binary search here, not a string search, since the buffer
|
||||
* may contain '\x00' bytes, if binary data is transferred.
|
||||
*/
|
||||
|
||||
int clen = (int)buf_len - (int)boundary_len - 4;
|
||||
int i;
|
||||
|
||||
for (i = 0; i <= clen; i++) {
|
||||
if (!memcmp(buf + i, "\r\n--", 4)) {
|
||||
if (!memcmp(buf + i + 4, boundary, boundary_len)) {
|
||||
return buf + i;
|
||||
}
|
||||
if (!memcmp(buf + i + 4, boundary, boundary_len)) return buf+i;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
@@ -186,34 +196,40 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
|
||||
* This is the typical way to handle file upload from a form.
|
||||
*/
|
||||
|
||||
if (!has_body_data) {
|
||||
if ( ! has_body_data ) {
|
||||
|
||||
const char *data;
|
||||
|
||||
if (strcmp(conn->request_info.request_method, "GET")) {
|
||||
/* No body data, but not a GET request.
|
||||
* This is not a valid form request. */
|
||||
if ( strcmp( conn->request_info.request_method, "GET" ) ) {
|
||||
/*
|
||||
* No body data, but not a GET request.
|
||||
* This is not a valid form request.
|
||||
*/
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* GET request: form data is in the query string. */
|
||||
/* The entire data has already been loaded, so there is no nead to
|
||||
/*
|
||||
* GET request: form data is in the query string.
|
||||
* The entire data has already been loaded, so there is no nead to
|
||||
* call httplib_read. We just need to split the query string into key-value
|
||||
* pairs. */
|
||||
data = conn->request_info.query_string;
|
||||
if ( data == NULL ) {
|
||||
/* No query string. */
|
||||
return -1;
|
||||
}
|
||||
* pairs.
|
||||
*/
|
||||
|
||||
data = conn->request_info.query_string;
|
||||
if ( data == NULL ) return -1; /* No query string. */
|
||||
|
||||
/*
|
||||
* Split data in a=1&b=xy&c=3&c=4 ...
|
||||
*/
|
||||
|
||||
while ( *data ) {
|
||||
|
||||
/* Split data in a=1&b=xy&c=3&c=4 ... */
|
||||
while (*data) {
|
||||
const char *val = strchr(data, '=');
|
||||
const char *next;
|
||||
ptrdiff_t keylen, vallen;
|
||||
|
||||
if (!val) {
|
||||
break;
|
||||
}
|
||||
if ( val == NULL ) break;
|
||||
keylen = val - data;
|
||||
|
||||
/*
|
||||
@@ -229,55 +245,78 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
|
||||
* FORM_FIELD_STORAGE_ABORT (flag) ... stop parsing
|
||||
*/
|
||||
|
||||
memset(path, 0, sizeof(path));
|
||||
memset( path, 0, sizeof(path) );
|
||||
field_count++;
|
||||
field_storage = url_encoded_field_found(conn, data, (size_t)keylen, NULL, 0, path, sizeof(path) - 1, fdh);
|
||||
field_storage = url_encoded_field_found( conn, data, (size_t)keylen, NULL, 0, path, sizeof(path) - 1, fdh );
|
||||
|
||||
val++;
|
||||
next = strchr(val, '&');
|
||||
if (next) {
|
||||
next = strchr( val, '&' );
|
||||
if ( next != NULL ) {
|
||||
|
||||
vallen = next - val;
|
||||
next++;
|
||||
} else {
|
||||
}
|
||||
|
||||
else {
|
||||
vallen = (ptrdiff_t)strlen(val);
|
||||
next = val + vallen;
|
||||
}
|
||||
|
||||
if (field_storage == FORM_FIELD_STORAGE_GET) {
|
||||
/* Call callback */
|
||||
url_encoded_field_get( conn, data, (size_t)keylen, val, (size_t)vallen, fdh);
|
||||
if ( field_storage == FORM_FIELD_STORAGE_GET ) {
|
||||
|
||||
/*
|
||||
* Call callback
|
||||
*/
|
||||
|
||||
url_encoded_field_get( conn, data, (size_t)keylen, val, (size_t)vallen, fdh );
|
||||
}
|
||||
if (field_storage == FORM_FIELD_STORAGE_STORE) {
|
||||
/* Store the content to a file */
|
||||
if (XX_httplib_fopen(conn, path, "wb", &fstore) == 0) fstore.fp = NULL;
|
||||
|
||||
if ( field_storage == FORM_FIELD_STORAGE_STORE ) {
|
||||
|
||||
/*
|
||||
* Store the content to a file
|
||||
*/
|
||||
|
||||
if ( XX_httplib_fopen( conn, path, "wb", &fstore ) == 0 ) fstore.fp = NULL;
|
||||
file_size = 0;
|
||||
if (fstore.fp != NULL) {
|
||||
|
||||
if ( fstore.fp != NULL ) {
|
||||
|
||||
size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp);
|
||||
if ((n != (size_t)vallen) || (ferror(fstore.fp))) {
|
||||
httplib_cry(conn, "%s: Cannot write file %s", __func__, path);
|
||||
fclose(fstore.fp);
|
||||
httplib_cry( conn, "%s: Cannot write file %s", __func__, path );
|
||||
fclose( fstore.fp );
|
||||
fstore.fp = NULL;
|
||||
XX_httplib_remove_bad_file(conn, path);
|
||||
XX_httplib_remove_bad_file( conn, path );
|
||||
}
|
||||
file_size += (int64_t)n;
|
||||
|
||||
if (fstore.fp) {
|
||||
r = fclose(fstore.fp);
|
||||
if ( fstore.fp ) {
|
||||
|
||||
r = fclose( fstore.fp );
|
||||
|
||||
if (r == 0) {
|
||||
/* stored successfully */
|
||||
field_stored(conn, path, file_size, fdh);
|
||||
} else {
|
||||
httplib_cry(conn, "%s: Error saving file %s", __func__, path);
|
||||
XX_httplib_remove_bad_file(conn, path);
|
||||
/*
|
||||
* stored successfully
|
||||
*/
|
||||
|
||||
field_stored( conn, path, file_size, fdh );
|
||||
}
|
||||
|
||||
else {
|
||||
httplib_cry( conn, "%s: Error saving file %s", __func__, path );
|
||||
XX_httplib_remove_bad_file( conn, path );
|
||||
}
|
||||
|
||||
fstore.fp = NULL;
|
||||
}
|
||||
|
||||
} else httplib_cry(conn, "%s: Cannot create file %s", __func__, path);
|
||||
} else httplib_cry( conn, "%s: Cannot create file %s", __func__, path );
|
||||
}
|
||||
|
||||
/* if (field_storage == FORM_FIELD_STORAGE_READ) { */
|
||||
/* The idea of "field_storage=read" is to let the API user read
|
||||
/*
|
||||
* if (field_storage == FORM_FIELD_STORAGE_READ) {
|
||||
* The idea of "field_storage=read" is to let the API user read
|
||||
* data chunk by chunk and to some data processing on the fly.
|
||||
* This should avoid the need to store data in the server:
|
||||
* It should neither be stored in memory, like
|
||||
@@ -286,36 +325,44 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
|
||||
* However, for a "GET" request this does not make any much
|
||||
* sense, since the data is already stored in memory, as it is
|
||||
* part of the query string.
|
||||
*
|
||||
* } */
|
||||
|
||||
if ((field_storage & FORM_FIELD_STORAGE_ABORT) == FORM_FIELD_STORAGE_ABORT) break; /* Stop parsing the request */
|
||||
|
||||
/*
|
||||
* Proceed to next entry
|
||||
*/
|
||||
/* } */
|
||||
|
||||
if ((field_storage & FORM_FIELD_STORAGE_ABORT) == FORM_FIELD_STORAGE_ABORT) {
|
||||
/* Stop parsing the request */
|
||||
break;
|
||||
}
|
||||
|
||||
/* Proceed to next entry */
|
||||
data = next;
|
||||
}
|
||||
|
||||
return field_count;
|
||||
}
|
||||
|
||||
content_type = httplib_get_header(conn, "Content-Type");
|
||||
content_type = httplib_get_header( conn, "Content-Type" );
|
||||
|
||||
if ( content_type == NULL
|
||||
|| ! httplib_strcasecmp(content_type, "APPLICATION/X-WWW-FORM-URLENCODED")
|
||||
|| ! httplib_strcasecmp(content_type, "APPLICATION/WWW-FORM-URLENCODED")) {
|
||||
|
||||
/*
|
||||
* The form data is in the request body data, encoded in key/value
|
||||
* pairs.
|
||||
*/
|
||||
|
||||
if (!content_type
|
||||
|| !httplib_strcasecmp(content_type, "APPLICATION/X-WWW-FORM-URLENCODED")
|
||||
|| !httplib_strcasecmp(content_type, "APPLICATION/WWW-FORM-URLENCODED")) {
|
||||
/* The form data is in the request body data, encoded in key/value
|
||||
* pairs. */
|
||||
int all_data_read = 0;
|
||||
|
||||
/* Read body data and split it in keys and values.
|
||||
/*
|
||||
* Read body data and split it in keys and values.
|
||||
* The encoding is like in the "GET" case above: a=1&b&c=3&c=4.
|
||||
* Here we use "POST", and read the data from the request body.
|
||||
* The data read on the fly, so it is not required to buffer the
|
||||
* entire request in memory before processing it. */
|
||||
* entire request in memory before processing it.
|
||||
*/
|
||||
|
||||
for (;;) {
|
||||
|
||||
const char *val;
|
||||
const char *next;
|
||||
ptrdiff_t keylen, vallen;
|
||||
@@ -326,43 +373,42 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
|
||||
if ((size_t)buf_fill < (sizeof(buf) - 1)) {
|
||||
|
||||
size_t to_read = sizeof(buf) - 1 - (size_t)buf_fill;
|
||||
r = httplib_read(conn, buf + (size_t)buf_fill, to_read);
|
||||
if (r < 0) {
|
||||
/* read error */
|
||||
return -1;
|
||||
}
|
||||
if (r != (int)to_read) {
|
||||
/* TODO: Create a function to get "all_data_read" from
|
||||
r = httplib_read( conn, buf + (size_t)buf_fill, to_read );
|
||||
if ( r < 0 ) return -1; /* read error */
|
||||
|
||||
if ( r != (int)to_read ) {
|
||||
/*
|
||||
* TODO: Create a function to get "all_data_read" from
|
||||
* the conn object. All data is read if the Content-Length
|
||||
* has been reached, or if chunked encoding is used and
|
||||
* the end marker has been read, or if the connection has
|
||||
* been closed. */
|
||||
* been closed.
|
||||
*/
|
||||
|
||||
all_data_read = 1;
|
||||
}
|
||||
buf_fill += r;
|
||||
buf[buf_fill] = 0;
|
||||
if (buf_fill < 1) break;
|
||||
if ( buf_fill < 1 ) break;
|
||||
}
|
||||
|
||||
val = strchr(buf, '=');
|
||||
|
||||
if (!val) {
|
||||
break;
|
||||
}
|
||||
if ( val == NULL ) break;
|
||||
keylen = val - buf;
|
||||
val++;
|
||||
|
||||
/* Call callback */
|
||||
memset(path, 0, sizeof(path));
|
||||
/*
|
||||
* Call callback
|
||||
*/
|
||||
|
||||
memset( path, 0, sizeof(path) );
|
||||
field_count++;
|
||||
field_storage = url_encoded_field_found(conn, buf, (size_t)keylen, NULL, 0, path, sizeof(path) - 1, fdh);
|
||||
field_storage = url_encoded_field_found( conn, buf, (size_t)keylen, NULL, 0, path, sizeof(path) - 1, fdh );
|
||||
|
||||
if ((field_storage & FORM_FIELD_STORAGE_ABORT) == FORM_FIELD_STORAGE_ABORT) {
|
||||
/* Stop parsing the request */
|
||||
break;
|
||||
}
|
||||
if ( (field_storage & FORM_FIELD_STORAGE_ABORT) == FORM_FIELD_STORAGE_ABORT ) break; /* Stop parsing the request */
|
||||
|
||||
if (field_storage == FORM_FIELD_STORAGE_STORE) {
|
||||
if ( field_storage == FORM_FIELD_STORAGE_STORE ) {
|
||||
|
||||
if (XX_httplib_fopen(conn, path, "wb", &fstore) == 0) fstore.fp = NULL;
|
||||
file_size = 0;
|
||||
@@ -370,19 +416,27 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
|
||||
}
|
||||
|
||||
get_block = 0;
|
||||
/* Loop to read values larger than sizeof(buf)-keylen-2 */
|
||||
|
||||
/*
|
||||
* Loop to read values larger than sizeof(buf)-keylen-2
|
||||
*/
|
||||
|
||||
do {
|
||||
next = strchr(val, '&');
|
||||
if (next) {
|
||||
|
||||
if (next != NULL ) {
|
||||
|
||||
vallen = next - val;
|
||||
next++;
|
||||
end_of_key_value_pair_found = 1;
|
||||
} else {
|
||||
}
|
||||
|
||||
else {
|
||||
vallen = (ptrdiff_t)strlen(val);
|
||||
next = val + vallen;
|
||||
}
|
||||
|
||||
if (field_storage == FORM_FIELD_STORAGE_GET) {
|
||||
if ( field_storage == FORM_FIELD_STORAGE_GET ) {
|
||||
#if 0
|
||||
if (!end_of_key_value_pair_found && !all_data_read) {
|
||||
/* This callback will deliver partial contents */
|
||||
@@ -400,7 +454,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
|
||||
fdh);
|
||||
get_block++;
|
||||
}
|
||||
if (fstore.fp) {
|
||||
if ( fstore.fp ) {
|
||||
size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp);
|
||||
if ((n != (size_t)vallen) || (ferror(fstore.fp))) {
|
||||
httplib_cry(conn, "%s: Cannot write file %s", __func__, path);
|
||||
@@ -411,24 +465,27 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
|
||||
file_size += (int64_t)n;
|
||||
}
|
||||
|
||||
if (!end_of_key_value_pair_found) {
|
||||
if ( ! end_of_key_value_pair_found ) {
|
||||
|
||||
used = next - buf;
|
||||
memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used);
|
||||
memmove( buf, buf + (size_t)used, sizeof(buf) - (size_t)used );
|
||||
buf_fill -= (int)used;
|
||||
if ((size_t)buf_fill < (sizeof(buf) - 1)) {
|
||||
|
||||
size_t to_read = sizeof(buf) - 1 - (size_t)buf_fill;
|
||||
r = httplib_read(conn, buf + (size_t)buf_fill, to_read);
|
||||
if (r < 0) {
|
||||
/* read error */
|
||||
return -1;
|
||||
}
|
||||
if ( r < 0 ) return -1; /* read error */
|
||||
|
||||
if (r != (int)to_read) {
|
||||
/* TODO: Create a function to get "all_data_read"
|
||||
|
||||
/*
|
||||
* TODO: Create a function to get "all_data_read"
|
||||
* from the conn object. All data is read if the
|
||||
* Content-Length has been reached, or if chunked
|
||||
* encoding is used and the end marker has been
|
||||
* read, or if the connection has been closed. */
|
||||
* read, or if the connection has been closed.
|
||||
*/
|
||||
|
||||
all_data_read = 1;
|
||||
}
|
||||
buf_fill += r;
|
||||
@@ -438,30 +495,40 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
|
||||
}
|
||||
}
|
||||
|
||||
} while (!end_of_key_value_pair_found);
|
||||
} while ( ! end_of_key_value_pair_found );
|
||||
|
||||
if (fstore.fp) {
|
||||
if ( fstore.fp ) {
|
||||
r = fclose(fstore.fp);
|
||||
if (r == 0) {
|
||||
/* stored successfully */
|
||||
field_stored(conn, path, file_size, fdh);
|
||||
} else {
|
||||
httplib_cry(conn, "%s: Error saving file %s", __func__, path);
|
||||
XX_httplib_remove_bad_file(conn, path);
|
||||
if ( r == 0 ) {
|
||||
|
||||
/*
|
||||
* stored successfully
|
||||
*/
|
||||
field_stored( conn, path, file_size, fdh );
|
||||
}
|
||||
|
||||
else {
|
||||
httplib_cry( conn, "%s: Error saving file %s", __func__, path );
|
||||
XX_httplib_remove_bad_file( conn, path );
|
||||
}
|
||||
|
||||
fstore.fp = NULL;
|
||||
}
|
||||
|
||||
/* Proceed to next entry */
|
||||
/*
|
||||
* Proceed to next entry
|
||||
*/
|
||||
|
||||
used = next - buf;
|
||||
memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used);
|
||||
memmove( buf, buf + (size_t)used, sizeof(buf) - (size_t)used );
|
||||
buf_fill -= (int)used;
|
||||
}
|
||||
|
||||
return field_count;
|
||||
}
|
||||
|
||||
if (!httplib_strncasecmp(content_type, "MULTIPART/FORM-DATA;", 20)) {
|
||||
if ( ! httplib_strncasecmp( content_type, "MULTIPART/FORM-DATA;", 20) ) {
|
||||
|
||||
/*
|
||||
* The form data is in the request body data, encoded as multipart
|
||||
* content (see https://www.ietf.org/rfc/rfc1867.txt,
|
||||
@@ -481,9 +548,12 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
|
||||
const char *content_disp;
|
||||
const char *next;
|
||||
|
||||
memset(&part_header, 0, sizeof(part_header));
|
||||
memset( & part_header, 0, sizeof(part_header) );
|
||||
|
||||
/*
|
||||
* Skip all spaces between MULTIPART/FORM-DATA; and BOUNDARY=
|
||||
*/
|
||||
|
||||
/* Skip all spaces between MULTIPART/FORM-DATA; and BOUNDARY= */
|
||||
bl = 20;
|
||||
while (content_type[bl] == ' ') bl++;
|
||||
|
||||
@@ -491,142 +561,175 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
|
||||
* There has to be a BOUNDARY definition in the Content-Type header
|
||||
*/
|
||||
|
||||
if (httplib_strncasecmp(content_type + bl, "BOUNDARY=", 9)) {
|
||||
/* Malformed request */
|
||||
if ( httplib_strncasecmp( content_type + bl, "BOUNDARY=", 9 ) ) {
|
||||
|
||||
/*
|
||||
* Malformed request
|
||||
*/
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
boundary = content_type + bl + 9;
|
||||
bl = strlen(boundary);
|
||||
bl = strlen( boundary );
|
||||
|
||||
if (bl + 800 > sizeof(buf)) {
|
||||
/* Sanity check: The algorithm can not work if bl >= sizeof(buf),
|
||||
if ( bl + 800 > sizeof(buf) ) {
|
||||
|
||||
/*
|
||||
* Sanity check: The algorithm can not work if bl >= sizeof(buf),
|
||||
* and it will not work effectively, if the buf is only a few byte
|
||||
* larger than bl, or it buf can not hold the multipart header
|
||||
* plus the boundary.
|
||||
* Check some reasonable number here, that should be fulfilled by
|
||||
* any reasonable request from every browser. If it is not
|
||||
* fulfilled, it might be a hand-made request, intended to
|
||||
* interfere with the algorithm. */
|
||||
* interfere with the algorithm.
|
||||
*/
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
size_t towrite, n;
|
||||
|
||||
size_t towrite;
|
||||
size_t n;
|
||||
int get_block;
|
||||
|
||||
r = httplib_read(conn, buf + (size_t)buf_fill, sizeof(buf) - 1 - (size_t)buf_fill);
|
||||
if (r < 0) {
|
||||
/* read error */
|
||||
return -1;
|
||||
}
|
||||
buf_fill += r;
|
||||
buf[buf_fill] = 0;
|
||||
if (buf_fill < 1) {
|
||||
/* No data */
|
||||
return -1;
|
||||
}
|
||||
r = httplib_read( conn, buf + (size_t)buf_fill, sizeof(buf) - 1 - (size_t)buf_fill );
|
||||
if ( r < 0 ) return -1; /* read error */
|
||||
|
||||
buf_fill += r;
|
||||
buf[buf_fill] = 0;
|
||||
|
||||
if ( buf_fill < 1 ) return -1; /* No data */
|
||||
|
||||
if ( buf[0] != '-' || buf[1] != '-' ) return -1; /* Malformed request */
|
||||
|
||||
if ( strncmp( buf + 2, boundary, bl ) ) return -1; /* Malformed request */
|
||||
|
||||
if ( buf[bl + 2] != '\r' || buf[bl + 3] != '\n' ) {
|
||||
|
||||
/*
|
||||
* Every part must end with \r\n, if there is another part.
|
||||
* The end of the request has an extra --
|
||||
*/
|
||||
|
||||
if (((size_t)buf_fill != (size_t)(bl + 6)) || (strncmp(buf + bl + 2, "--\r\n", 4))) return -1; /* Malformed request */
|
||||
|
||||
/*
|
||||
* End of the request
|
||||
*/
|
||||
|
||||
if (buf[0] != '-' || buf[1] != '-') {
|
||||
/* Malformed request */
|
||||
return -1;
|
||||
}
|
||||
if (strncmp(buf + 2, boundary, bl)) {
|
||||
/* Malformed request */
|
||||
return -1;
|
||||
}
|
||||
if (buf[bl + 2] != '\r' || buf[bl + 3] != '\n') {
|
||||
/* Every part must end with \r\n, if there is another part.
|
||||
* The end of the request has an extra -- */
|
||||
if (((size_t)buf_fill != (size_t)(bl + 6))
|
||||
|| (strncmp(buf + bl + 2, "--\r\n", 4))) {
|
||||
/* Malformed request */
|
||||
return -1;
|
||||
}
|
||||
/* End of the request */
|
||||
break;
|
||||
}
|
||||
|
||||
/* Next, we need to get the part header: Read until \r\n\r\n */
|
||||
/*
|
||||
* Next, we need to get the part header: Read until \r\n\r\n
|
||||
*/
|
||||
|
||||
hbuf = buf + bl + 4;
|
||||
hend = strstr(hbuf, "\r\n\r\n");
|
||||
if (!hend) {
|
||||
/* Malformed request */
|
||||
return -1;
|
||||
}
|
||||
if ( hend == NULL ) return -1; /* Malformed request */
|
||||
|
||||
XX_httplib_parse_http_headers(&hbuf, &part_header);
|
||||
if ((hend + 2) != hbuf) {
|
||||
/* Malformed request */
|
||||
return -1;
|
||||
}
|
||||
XX_httplib_parse_http_headers( &hbuf, &part_header );
|
||||
if ( (hend + 2) != hbuf ) return -1; /* Malformed request */
|
||||
|
||||
/*
|
||||
* Skip \r\n\r\n
|
||||
*/
|
||||
|
||||
/* Skip \r\n\r\n */
|
||||
hend += 4;
|
||||
|
||||
/* According to the RFC, every part has to have a header field like:
|
||||
* Content-Disposition: form-data; name="..." */
|
||||
content_disp = XX_httplib_get_header(&part_header, "Content-Disposition");
|
||||
if (!content_disp) {
|
||||
/* Malformed request */
|
||||
return -1;
|
||||
}
|
||||
/*
|
||||
* According to the RFC, every part has to have a header field like:
|
||||
* Content-Disposition: form-data; name="..."
|
||||
*/
|
||||
|
||||
content_disp = XX_httplib_get_header( & part_header, "Content-Disposition" );
|
||||
if ( ! content_disp ) return -1; /* Malformed request */
|
||||
|
||||
/*
|
||||
* Get the mandatory name="..." part of the Content-Disposition
|
||||
* header.
|
||||
*/
|
||||
|
||||
nbeg = strstr( content_disp, "name=\"" );
|
||||
if ( nbeg == NULL ) return -1; /* Malformed request */
|
||||
|
||||
/* Get the mandatory name="..." part of the Content-Disposition
|
||||
* header. */
|
||||
nbeg = strstr(content_disp, "name=\"");
|
||||
if (!nbeg) {
|
||||
/* Malformed request */
|
||||
return -1;
|
||||
}
|
||||
nbeg += 6;
|
||||
nend = strchr(nbeg, '\"');
|
||||
if (!nend) {
|
||||
/* Malformed request */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Get the optional filename="..." part of the Content-Disposition
|
||||
* header. */
|
||||
fbeg = strstr(content_disp, "filename=\"");
|
||||
if (fbeg) {
|
||||
if ( nend == NULL ) return -1; /* Malformed request */
|
||||
|
||||
/*
|
||||
* Get the optional filename="..." part of the Content-Disposition
|
||||
* header.
|
||||
*/
|
||||
|
||||
fbeg = strstr( content_disp, "filename=\"" );
|
||||
|
||||
if ( fbeg != NULL ) {
|
||||
|
||||
fbeg += 10;
|
||||
fend = strchr(fbeg, '\"');
|
||||
if (!fend) {
|
||||
/* Malformed request (the filename field is optional, but if
|
||||
* it exists, it needs to be terminated correctly). */
|
||||
fend = strchr(fbeg, '\"');
|
||||
|
||||
if ( fend == NULL ) {
|
||||
|
||||
/*
|
||||
* Malformed request (the filename field is optional, but if
|
||||
* it exists, it needs to be terminated correctly).
|
||||
*/
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* TODO: check Content-Type */
|
||||
/* Content-Type: application/octet-stream */
|
||||
/*
|
||||
* TODO: check Content-Type
|
||||
* Content-Type: application/octet-stream
|
||||
*/
|
||||
|
||||
} else fend = fbeg;
|
||||
|
||||
memset(path, 0, sizeof(path));
|
||||
memset( path, 0, sizeof(path) );
|
||||
field_count++;
|
||||
field_storage = url_encoded_field_found(conn, nbeg, (size_t)(nend - nbeg), fbeg, (size_t)(fend - fbeg), path, sizeof(path) - 1, fdh);
|
||||
field_storage = url_encoded_field_found( conn, nbeg, (size_t)(nend - nbeg), fbeg, (size_t)(fend - fbeg), path, sizeof(path) - 1, fdh );
|
||||
|
||||
/* If the boundary is already in the buffer, get the address,
|
||||
* otherwise next will be NULL. */
|
||||
next = search_boundary(hbuf, (size_t)((buf - hbuf) + buf_fill), boundary, bl);
|
||||
/*
|
||||
* If the boundary is already in the buffer, get the address,
|
||||
* otherwise next will be NULL.
|
||||
*/
|
||||
|
||||
if (field_storage == FORM_FIELD_STORAGE_STORE) {
|
||||
/* Store the content to a file */
|
||||
if (XX_httplib_fopen(conn, path, "wb", &fstore) == 0) fstore.fp = NULL;
|
||||
next = search_boundary( hbuf, (size_t)((buf - hbuf) + buf_fill), boundary, bl );
|
||||
|
||||
if ( field_storage == FORM_FIELD_STORAGE_STORE ) {
|
||||
|
||||
/*
|
||||
* Store the content to a file
|
||||
*/
|
||||
|
||||
if ( XX_httplib_fopen( conn, path, "wb", &fstore ) == 0 ) fstore.fp = NULL;
|
||||
file_size = 0;
|
||||
|
||||
if (!fstore.fp) httplib_cry(conn, "%s: Cannot create file %s", __func__, path);
|
||||
if ( ! fstore.fp ) httplib_cry( conn, "%s: Cannot create file %s", __func__, path );
|
||||
}
|
||||
|
||||
get_block = 0;
|
||||
while (!next) {
|
||||
/* Set "towrite" to the number of bytes available
|
||||
* in the buffer */
|
||||
|
||||
while ( ! next ) {
|
||||
|
||||
/*
|
||||
* Set "towrite" to the number of bytes available
|
||||
* in the buffer
|
||||
*/
|
||||
|
||||
towrite = (size_t)(buf - hend + buf_fill);
|
||||
/* Subtract the boundary length, to deal with
|
||||
|
||||
/*
|
||||
* Subtract the boundary length, to deal with
|
||||
* cases the boundary is only partially stored
|
||||
* in the buffer. */
|
||||
* in the buffer.
|
||||
*/
|
||||
|
||||
towrite -= bl + 4;
|
||||
|
||||
if (field_storage == FORM_FIELD_STORAGE_GET) {
|
||||
@@ -639,46 +742,58 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
|
||||
get_block++;
|
||||
}
|
||||
|
||||
if (field_storage == FORM_FIELD_STORAGE_STORE) {
|
||||
if ( field_storage == FORM_FIELD_STORAGE_STORE ) {
|
||||
|
||||
if (fstore.fp) {
|
||||
|
||||
/* Store the content of the buffer. */
|
||||
n = (size_t)fwrite(hend, 1, towrite, fstore.fp);
|
||||
if ((n != towrite) || (ferror(fstore.fp))) {
|
||||
httplib_cry(conn, "%s: Cannot write file %s", __func__, path);
|
||||
fclose(fstore.fp);
|
||||
/*
|
||||
* Store the content of the buffer.
|
||||
*/
|
||||
|
||||
n = (size_t)fwrite( hend, 1, towrite, fstore.fp );
|
||||
|
||||
if ( n != towrite || ferror( fstore.fp ) ) {
|
||||
|
||||
httplib_cry( conn, "%s: Cannot write file %s", __func__, path );
|
||||
fclose( fstore.fp );
|
||||
fstore.fp = NULL;
|
||||
XX_httplib_remove_bad_file(conn, path);
|
||||
XX_httplib_remove_bad_file( conn, path );
|
||||
}
|
||||
|
||||
file_size += (int64_t)n;
|
||||
}
|
||||
}
|
||||
|
||||
memmove(buf, hend + towrite, bl + 4);
|
||||
memmove( buf, hend + towrite, bl + 4 );
|
||||
buf_fill = (int)(bl + 4);
|
||||
hend = buf;
|
||||
|
||||
/* Read new data */
|
||||
r = httplib_read(conn, buf + (size_t)buf_fill, sizeof(buf) - 1 - (size_t)buf_fill);
|
||||
if (r < 0) {
|
||||
/* read error */
|
||||
return -1;
|
||||
}
|
||||
buf_fill += r;
|
||||
buf[buf_fill] = 0;
|
||||
if (buf_fill < 1) {
|
||||
/* No data */
|
||||
return -1;
|
||||
}
|
||||
/*
|
||||
* Read new data
|
||||
*/
|
||||
|
||||
/* Find boundary */
|
||||
next = search_boundary(buf, (size_t)buf_fill, boundary, bl);
|
||||
r = httplib_read( conn, buf + (size_t)buf_fill, sizeof(buf) - 1 - (size_t)buf_fill );
|
||||
if ( r < 0 ) return -1; /* read error */
|
||||
|
||||
buf_fill += r;
|
||||
buf[buf_fill] = 0;
|
||||
if (buf_fill < 1) return -1; /* No data */
|
||||
|
||||
/*
|
||||
* Find boundary
|
||||
*/
|
||||
|
||||
next = search_boundary( buf, (size_t)buf_fill, boundary, bl );
|
||||
}
|
||||
|
||||
towrite = (size_t)(next - hend);
|
||||
|
||||
if (field_storage == FORM_FIELD_STORAGE_GET) {
|
||||
/* Call callback */
|
||||
if ( field_storage == FORM_FIELD_STORAGE_GET ) {
|
||||
|
||||
/*
|
||||
* Call callback
|
||||
*/
|
||||
|
||||
unencoded_field_get(conn,
|
||||
((get_block > 0) ? NULL : nbeg),
|
||||
((get_block > 0) ? 0 : (size_t)(nend - nbeg)),
|
||||
@@ -687,48 +802,59 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
|
||||
fdh);
|
||||
}
|
||||
|
||||
if (field_storage == FORM_FIELD_STORAGE_STORE) {
|
||||
if ( field_storage == FORM_FIELD_STORAGE_STORE ) {
|
||||
|
||||
if (fstore.fp) {
|
||||
n = (size_t)fwrite(hend, 1, towrite, fstore.fp);
|
||||
if ((n != towrite) || (ferror(fstore.fp))) {
|
||||
httplib_cry(conn, "%s: Cannot write file %s", __func__, path);
|
||||
fclose(fstore.fp);
|
||||
if ( fstore.fp ) {
|
||||
|
||||
n = (size_t)fwrite( hend, 1, towrite, fstore.fp );
|
||||
|
||||
if ( n != towrite || ferror( fstore.fp ) ) {
|
||||
|
||||
httplib_cry( conn, "%s: Cannot write file %s", __func__, path );
|
||||
fclose( fstore.fp );
|
||||
fstore.fp = NULL;
|
||||
XX_httplib_remove_bad_file(conn, path);
|
||||
XX_httplib_remove_bad_file( conn, path );
|
||||
}
|
||||
file_size += (int64_t)n;
|
||||
}
|
||||
}
|
||||
|
||||
if (field_storage == FORM_FIELD_STORAGE_STORE) {
|
||||
if ( field_storage == FORM_FIELD_STORAGE_STORE ) {
|
||||
|
||||
if (fstore.fp) {
|
||||
r = fclose(fstore.fp);
|
||||
if (r == 0) {
|
||||
/* stored successfully */
|
||||
field_stored(conn, path, file_size, fdh);
|
||||
} else {
|
||||
httplib_cry(conn, "%s: Error saving file %s", __func__, path);
|
||||
XX_httplib_remove_bad_file(conn, path);
|
||||
if ( fstore.fp ) {
|
||||
|
||||
r = fclose( fstore.fp );
|
||||
if ( r == 0 ) {
|
||||
|
||||
/*
|
||||
* stored successfully
|
||||
*/
|
||||
|
||||
field_stored( conn, path, file_size, fdh );
|
||||
}
|
||||
|
||||
else {
|
||||
httplib_cry( conn, "%s: Error saving file %s", __func__, path );
|
||||
XX_httplib_remove_bad_file( conn, path );
|
||||
}
|
||||
fstore.fp = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if ((field_storage & FORM_FIELD_STORAGE_ABORT)
|
||||
== FORM_FIELD_STORAGE_ABORT) {
|
||||
/* Stop parsing the request */
|
||||
break;
|
||||
}
|
||||
if ( (field_storage & FORM_FIELD_STORAGE_ABORT) == FORM_FIELD_STORAGE_ABORT ) break; /* Stop parsing the request */
|
||||
|
||||
/*
|
||||
* Remove from the buffer
|
||||
*/
|
||||
|
||||
/* Remove from the buffer */
|
||||
used = next - buf + 2;
|
||||
memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used);
|
||||
buf_fill -= (int)used;
|
||||
}
|
||||
|
||||
/* All parts handled */
|
||||
/*
|
||||
* All parts handled
|
||||
*/
|
||||
return field_count;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user