diff --git a/Makefile b/Makefile index a4a91592..926edafb 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/src/httplib_put_dir.c b/src/httplib_put_dir.c new file mode 100644 index 00000000..8fe8d232 --- /dev/null +++ b/src/httplib_put_dir.c @@ -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 */ diff --git a/src/libhttp.c b/src/libhttp.c index d87ce3d0..82444ef6 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -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 */