diff --git a/Makefile b/Makefile index 711b3e21..27166f0b 100644 --- a/Makefile +++ b/Makefile @@ -61,6 +61,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_consume_socket.c \ src/httplib_delete_file.c \ src/httplib_dir_scan_callback.c \ + src/httplib_discard_unread_request_data.c \ src/httplib_download.c \ src/httplib_fclose_on_exec.c \ src/httplib_fgets.c \ diff --git a/src/httplib_discard_unread_request_data.c b/src/httplib_discard_unread_request_data.c new file mode 100644 index 00000000..400b3ee7 --- /dev/null +++ b/src/httplib_discard_unread_request_data.c @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2016 Lammert Bies + * Copyright (c) 2013-2016 the Civetweb developers + * Copyright (c) 2004-2013 Sergey Lyubka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + + +#include "libhttp-private.h" + + + +void XX_httplib_discard_unread_request_data( struct mg_connection *conn ) { + + char buf[MG_BUF_LEN]; + size_t to_read; + int nread; + + if (conn == NULL) { + return; + } + + to_read = sizeof(buf); + + if (conn->is_chunked) { + /* Chunked encoding: 1=chunk not read completely, 2=chunk read + * completely */ + while (conn->is_chunked == 1) { + nread = mg_read(conn, buf, to_read); + if (nread <= 0) { + break; + } + } + + } else { + /* Not chunked: content length is known */ + while (conn->consumed_content < conn->content_len) { + if (to_read + > (size_t)(conn->content_len - conn->consumed_content)) { + to_read = (size_t)(conn->content_len - conn->consumed_content); + } + + nread = mg_read(conn, buf, to_read); + if (nread <= 0) break; + } + } + +} /* XX_httplib_discard_unread_request_data */ diff --git a/src/libhttp.c b/src/libhttp.c index fda75a49..bc4c621d 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -2835,41 +2835,3 @@ int XX_httplib_pull_all( FILE *fp, struct mg_connection *conn, char *buf, int le return nread; } /* XX_httplib_pull_all */ - - -void XX_httplib_discard_unread_request_data( struct mg_connection *conn ) { - - char buf[MG_BUF_LEN]; - size_t to_read; - int nread; - - if (conn == NULL) { - return; - } - - to_read = sizeof(buf); - - if (conn->is_chunked) { - /* Chunked encoding: 1=chunk not read completely, 2=chunk read - * completely */ - while (conn->is_chunked == 1) { - nread = mg_read(conn, buf, to_read); - if (nread <= 0) { - break; - } - } - - } else { - /* Not chunked: content length is known */ - while (conn->consumed_content < conn->content_len) { - if (to_read - > (size_t)(conn->content_len - conn->consumed_content)) { - to_read = (size_t)(conn->content_len - conn->consumed_content); - } - - nread = mg_read(conn, buf, to_read); - if (nread <= 0) break; - } - } - -} /* XX_httplib_discard_unread_request_data */