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

Moved put_dir to own file

This commit is contained in:
Lammert Bies
2016-12-11 00:26:51 +01:00
parent 685f53ab01
commit 52aecb1642
3 changed files with 76 additions and 41 deletions

View File

@@ -87,6 +87,7 @@ LIB_SOURCES = src/libhttp.c \
src/httplib_prepare_cgi_environment.c \
src/httplib_process_new_connection.c \
src/httplib_produce_socket.c \
src/httplib_put_dir.c \
src/httplib_put_file.c \
src/httplib_read_request.c \
src/httplib_read_websocket.c \

75
src/httplib_put_dir.c Normal file
View File

@@ -0,0 +1,75 @@
/*
* 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"
/*
* int XX_httplib_put_dir( struct mg_connection *conn, const char *path );
*
* The function XX_httplib_put_dir() creates a directory mentioned in a PUT
* request including all intermediate subdirectories. The following values can
* be returned:
* Return 0 if the path itself is a directory.
* Return 1 if the path leads to a file.
* Return -1 for if the path is too long.
* Return -2 if path can not be created.
*/
int XX_httplib_put_dir( struct mg_connection *conn, const char *path ) {
char buf[PATH_MAX];
const char *s;
const char *p;
struct file file = STRUCT_FILE_INITIALIZER;
size_t len;
int res = 1;
for (s = p = path + 2; (p = strchr(s, '/')) != NULL; s = ++p) {
len = (size_t)(p - path);
if (len >= sizeof(buf)) {
/* path too long */
res = -1;
break;
}
memcpy(buf, path, len);
buf[len] = '\0';
/* Try to create intermediate directory */
if (!XX_httplib_stat(conn, buf, &file) && mg_mkdir(conn, buf, 0755) != 0) {
/* path does not exixt and can not be created */
res = -2;
break;
}
/* Is path itself a directory? */
if (p[1] == '\0') res = 0;
}
return res;
} /* XX_httplib_put_dir */

View File

@@ -5092,44 +5092,3 @@ void mg_send_mime_file2( struct mg_connection *conn, const char *path, const cha
}
} else XX_httplib_send_http_error(conn, 404, "%s", "Error: File not found");
}
/* For a given PUT path, create all intermediate subdirectories.
* Return 0 if the path itself is a directory.
* Return 1 if the path leads to a file.
* Return -1 for if the path is too long.
* Return -2 if path can not be created.
*/
int XX_httplib_put_dir( struct mg_connection *conn, const char *path ) {
char buf[PATH_MAX];
const char *s;
const char *p;
struct file file = STRUCT_FILE_INITIALIZER;
size_t len;
int res = 1;
for (s = p = path + 2; (p = strchr(s, '/')) != NULL; s = ++p) {
len = (size_t)(p - path);
if (len >= sizeof(buf)) {
/* path too long */
res = -1;
break;
}
memcpy(buf, path, len);
buf[len] = '\0';
/* Try to create intermediate directory */
if (!XX_httplib_stat(conn, buf, &file) && mg_mkdir(conn, buf, 0755) != 0) {
/* path does not exixt and can not be created */
res = -2;
break;
}
/* Is path itself a directory? */
if (p[1] == '\0') res = 0;
}
return res;
} /* XX_httplib_put_dir */