1
0
mirror of https://github.com/lammertb/libhttp.git synced 2026-01-27 08:02:47 +03:00

Moved delete_file to own file

This commit is contained in:
Lammert Bies
2016-12-10 22:23:37 +01:00
parent e7ccb97c19
commit ce757a70e4
4 changed files with 93 additions and 51 deletions

View File

@@ -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 \

87
src/httplib_delete_file.c Normal file
View File

@@ -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 */

View File

@@ -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 *) );

View File

@@ -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 */