diff --git a/Makefile b/Makefile index 4afc8f1c..cac93e55 100644 --- a/Makefile +++ b/Makefile @@ -54,6 +54,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_construct_etag.c \ src/httplib_consume_socket.c \ src/httplib_delete_file.c \ + src/httplib_dir_scan_callback.c \ src/httplib_download.c \ src/httplib_fclose_on_exec.c \ src/httplib_forward_body_data.c \ diff --git a/src/httplib_dir_scan_callback.c b/src/httplib_dir_scan_callback.c new file mode 100644 index 00000000..f0144f6d --- /dev/null +++ b/src/httplib_dir_scan_callback.c @@ -0,0 +1,49 @@ +/* + * 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_dir_scan_callback( struct de *de, void *data ) { + + struct dir_scan_data *dsd = (struct dir_scan_data *)data; + + if (dsd->entries == NULL || dsd->num_entries >= dsd->arr_size) { + dsd->arr_size *= 2; + dsd->entries = XX_httplib_realloc2(dsd->entries, dsd->arr_size * sizeof(dsd->entries[0])); + } + if (dsd->entries == NULL) { + /* TODO(lsm, low): propagate an error to the caller */ + dsd->num_entries = 0; + } else { + dsd->entries[dsd->num_entries].file_name = XX_httplib_strdup(de->file_name); + dsd->entries[dsd->num_entries].file = de->file; + dsd->entries[dsd->num_entries].conn = de->conn; + dsd->num_entries++; + } + +} /* XX_httplib_dir_scan_callback */ diff --git a/src/libhttp-private.h b/src/libhttp-private.h index 4ed5899a..4005cb77 100644 --- a/src/libhttp-private.h +++ b/src/libhttp-private.h @@ -964,6 +964,7 @@ int XX_httplib_put_dir( struct mg_connection *conn, const char *path ); void XX_httplib_put_file( struct mg_connection *conn, const char *path ); int XX_httplib_read_request( FILE *fp, struct mg_connection *conn, char *buf, int bufsiz, int *nread ); void XX_httplib_read_websocket( struct mg_connection *conn, mg_websocket_data_handler ws_data_handler, void *callback_data ); +void * XX_httplib_realloc2( void *ptr, size_t size ); void XX_httplib_redirect_to_https_port( struct mg_connection *conn, int ssl_index ); int XX_httplib_refresh_trust( struct mg_connection *conn ); void XX_httplib_remove_bad_file( const struct mg_connection *conn, const char *path ); diff --git a/src/libhttp.c b/src/libhttp.c index 1c7b4af6..1bd09f79 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -4675,31 +4675,11 @@ int XX_httplib_remove_directory( struct mg_connection *conn, const char *dir ) { /* Behaves like realloc(), but frees original pointer on failure */ -static void * realloc2(void *ptr, size_t size) { +void *XX_httplib_realloc2( void *ptr, size_t size ) { void *new_ptr = XX_httplib_realloc(ptr, size); if (new_ptr == NULL) XX_httplib_free(ptr); return new_ptr; -} - -void XX_httplib_dir_scan_callback( struct de *de, void *data ) { - - struct dir_scan_data *dsd = (struct dir_scan_data *)data; - - if (dsd->entries == NULL || dsd->num_entries >= dsd->arr_size) { - dsd->arr_size *= 2; - dsd->entries = (struct de *)realloc2(dsd->entries, dsd->arr_size * sizeof(dsd->entries[0])); - } - if (dsd->entries == NULL) { - /* TODO(lsm, low): propagate an error to the caller */ - dsd->num_entries = 0; - } else { - dsd->entries[dsd->num_entries].file_name = XX_httplib_strdup(de->file_name); - dsd->entries[dsd->num_entries].file = de->file; - dsd->entries[dsd->num_entries].conn = de->conn; - dsd->num_entries++; - } - -} /* XX_httplib_dir_scan_callback */ +} /* XX_httplib_realloc2 */