From ce757a70e4010e3161cd495e7165f7c2e4e73a78 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Sat, 10 Dec 2016 22:23:37 +0100 Subject: [PATCH] Moved delete_file to own file --- Makefile | 1 + src/httplib_delete_file.c | 87 +++++++++++++++++++++++++++++++++++++++ src/libhttp-private.h | 1 + src/libhttp.c | 55 ++----------------------- 4 files changed, 93 insertions(+), 51 deletions(-) create mode 100644 src/httplib_delete_file.c diff --git a/Makefile b/Makefile index c895d675..ac33dfe4 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_connect_client.c \ src/httplib_connect_websocket_client.c \ src/httplib_consume_socket.c \ + src/httplib_delete_file.c \ src/httplib_download.c \ src/httplib_free_context.c \ src/httplib_get_first_ssl_listener_index.c \ diff --git a/src/httplib_delete_file.c b/src/httplib_delete_file.c new file mode 100644 index 00000000..c9234e62 --- /dev/null +++ b/src/httplib_delete_file.c @@ -0,0 +1,87 @@ +/* + * 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_delete_file( struct mg_connection *conn, const char *path ); + * + * The function XX_httplib_delete_file() deletes a file after a request over a + * connection. + */ + +#if !defined(NO_FILES) + +void XX_httplib_delete_file( struct mg_connection *conn, const char *path ) { + + struct de de; + + memset(&de.file, 0, sizeof(de.file)); + if (!XX_httplib_stat(conn, path, &de.file)) { + /* XX_httplib_stat returns 0 if the file does not exist */ + XX_httplib_send_http_error(conn, 404, "Error: Cannot delete file\nFile %s not found", path); + return; + } + + if (de.file.membuf != NULL) { + /* the file is cached in memory */ + XX_httplib_send_http_error( conn, 405, "Error: Delete not possible\nDeleting %s is not supported", path); + return; + } + + if (de.file.is_directory) { + if (XX_httplib_remove_directory(conn, path)) { + /* Delete is successful: Return 204 without content. */ + XX_httplib_send_http_error(conn, 204, "%s", ""); + } else { + /* Delete is not successful: Return 500 (Server error). */ + XX_httplib_send_http_error(conn, 500, "Error: Could not delete %s", path); + } + return; + } + + /* This is an existing file (not a directory). + * Check if write permission is granted. */ + if (access(path, W_OK) != 0) { + /* File is read only */ + XX_httplib_send_http_error( conn, 403, "Error: Delete not possible\nDeleting %s is not allowed", path); + return; + } + + /* Try to delete it. */ + if (mg_remove(conn, path) == 0) { + /* Delete was successful: Return 204 without content. */ + XX_httplib_send_http_error(conn, 204, "%s", ""); + } else { + /* Delete not successful (file locked). */ + XX_httplib_send_http_error(conn, 423, "Error: Cannot delete file\nremove(%s): %s", path, strerror(ERRNO)); + } + +} /* XX_httplib_delete_file */ + +#endif /* !NO_FILES */ diff --git a/src/libhttp-private.h b/src/libhttp-private.h index 9e9e0357..d2b797dc 100644 --- a/src/libhttp-private.h +++ b/src/libhttp-private.h @@ -922,6 +922,7 @@ void XX_httplib_read_websocket( struct mg_connection *conn, mg_websocket_data_ 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 ); +int XX_httplib_remove_directory( struct mg_connection *conn, const char *dir ); void XX_httplib_remove_double_dots_and_double_slashes( char *s ); void XX_httplib_reset_per_request_attributes( struct mg_connection *conn ); int XX_httplib_scan_directory( struct mg_connection *conn, const char *dir, void *data, void (*cb)(struct de *, void *) ); diff --git a/src/libhttp.c b/src/libhttp.c index 3b77d297..00ffc011 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -4598,7 +4598,7 @@ int XX_httplib_scan_directory( struct mg_connection *conn, const char *dir, void #if !defined(NO_FILES) -static int remove_directory(struct mg_connection *conn, const char *dir) { +int XX_httplib_remove_directory( struct mg_connection *conn, const char *dir ) { char path[PATH_MAX]; struct dirent *dp; @@ -4639,7 +4639,7 @@ static int remove_directory(struct mg_connection *conn, const char *dir) { if (de.file.membuf == NULL) { /* file is not in memory */ if (de.file.is_directory) { - if (remove_directory(conn, path) == 0) ok = 0; + if (XX_httplib_remove_directory(conn, path) == 0) ok = 0; } else { if (mg_remove(conn, path) == 0) ok = 0; } @@ -4654,7 +4654,8 @@ static int remove_directory(struct mg_connection *conn, const char *dir) { } return ok; -} + +} /* XX_httplib_remove_directory */ #endif @@ -6108,52 +6109,4 @@ void XX_httplib_put_file( struct mg_connection *conn, const char *path ) { } /* XX_httplib_put_file */ - -void XX_httplib_delete_file( struct mg_connection *conn, const char *path ) { - - struct de de; - - memset(&de.file, 0, sizeof(de.file)); - if (!XX_httplib_stat(conn, path, &de.file)) { - /* XX_httplib_stat returns 0 if the file does not exist */ - XX_httplib_send_http_error(conn, 404, "Error: Cannot delete file\nFile %s not found", path); - return; - } - - if (de.file.membuf != NULL) { - /* the file is cached in memory */ - XX_httplib_send_http_error( conn, 405, "Error: Delete not possible\nDeleting %s is not supported", path); - return; - } - - if (de.file.is_directory) { - if (remove_directory(conn, path)) { - /* Delete is successful: Return 204 without content. */ - XX_httplib_send_http_error(conn, 204, "%s", ""); - } else { - /* Delete is not successful: Return 500 (Server error). */ - XX_httplib_send_http_error(conn, 500, "Error: Could not delete %s", path); - } - return; - } - - /* This is an existing file (not a directory). - * Check if write permission is granted. */ - if (access(path, W_OK) != 0) { - /* File is read only */ - XX_httplib_send_http_error( conn, 403, "Error: Delete not possible\nDeleting %s is not allowed", path); - return; - } - - /* Try to delete it. */ - if (mg_remove(conn, path) == 0) { - /* Delete was successful: Return 204 without content. */ - XX_httplib_send_http_error(conn, 204, "%s", ""); - } else { - /* Delete not successful (file locked). */ - XX_httplib_send_http_error(conn, 423, "Error: Cannot delete file\nremove(%s): %s", path, strerror(ERRNO)); - } - -} /* XX_httplib_delete_file */ - #endif /* !NO_FILES */