diff --git a/Makefile b/Makefile index bc5552d0..fedda64e 100644 --- a/Makefile +++ b/Makefile @@ -94,6 +94,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_parse_net.c \ src/httplib_parse_range_header.c \ src/httplib_prepare_cgi_environment.c \ + src/httplib_print_dir_entry.c \ src/httplib_process_new_connection.c \ src/httplib_produce_socket.c \ src/httplib_put_dir.c \ diff --git a/src/httplib_print_dir_entry.c b/src/httplib_print_dir_entry.c new file mode 100644 index 00000000..f4ef0c3b --- /dev/null +++ b/src/httplib_print_dir_entry.c @@ -0,0 +1,71 @@ +/* + * 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_print_dir_entry( struct de *de ) { + + char size[64]; + char mod[64]; + char href[PATH_MAX * 3 /* worst case */]; + struct tm *tm; + + if ( de->file.is_directory ) XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%s", "[DIRECTORY]" ); + else { + /* We use (signed) cast below because MSVC 6 compiler cannot + * convert unsigned __int64 to double. Sigh. */ + if ( de->file.size < 1024) XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%d", (int) de->file.size ); + else if ( de->file.size < 0x100000 ) XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%.1fk", ((double)de->file.size) / 1024.0 ); + else if ( de->file.size < 0x40000000 ) XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%.1fM", ((double)de->file.size) / 1048576.0 ); + else XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%.1fG", ((double)de->file.size) / 1073741824.0 ); + } + + /* Note: XX_httplib_snprintf will not cause a buffer overflow above. + * So, string truncation checks are not required here. */ + + tm = localtime(&de->file.last_modified); + if (tm != NULL) { + strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", tm); + } else { + XX_httplib_strlcpy(mod, "01-Jan-1970 00:00", sizeof(mod)); + mod[sizeof(mod) - 1] = '\0'; + } + mg_url_encode(de->file_name, href, sizeof(href)); + de->conn->num_bytes_sent += + mg_printf(de->conn, + "%s%s" + " %s  %s\n", + de->conn->request_info.local_uri, + href, + de->file.is_directory ? "/" : "", + de->file_name, + de->file.is_directory ? "/" : "", + mod, + size); + +} /* XX_httplib_print_dir_entry */ diff --git a/src/libhttp.c b/src/libhttp.c index 9148075b..c6b7f6b1 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -4457,7 +4457,7 @@ int XX_httplib_connect_socket( struct mg_context *ctx, const char *host, int por } /* XX_httplib_connect_socket */ -int mg_url_encode(const char *src, char *dst, size_t dst_len) { +int mg_url_encode( const char *src, char *dst, size_t dst_len ) { static const char *dont_escape = "._-$,;~()"; static const char *hex = "0123456789abcdef"; @@ -4478,47 +4478,5 @@ int mg_url_encode(const char *src, char *dst, size_t dst_len) { *pos = '\0'; return (*src == '\0') ? (int)(pos - dst) : -1; -} - -void XX_httplib_print_dir_entry( struct de *de ) { - - char size[64]; - char mod[64]; - char href[PATH_MAX * 3 /* worst case */]; - struct tm *tm; - - if ( de->file.is_directory ) XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%s", "[DIRECTORY]" ); - else { - /* We use (signed) cast below because MSVC 6 compiler cannot - * convert unsigned __int64 to double. Sigh. */ - if ( de->file.size < 1024) XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%d", (int) de->file.size ); - else if ( de->file.size < 0x100000 ) XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%.1fk", ((double)de->file.size) / 1024.0 ); - else if ( de->file.size < 0x40000000 ) XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%.1fM", ((double)de->file.size) / 1048576.0 ); - else XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%.1fG", ((double)de->file.size) / 1073741824.0 ); - } - - /* Note: XX_httplib_snprintf will not cause a buffer overflow above. - * So, string truncation checks are not required here. */ - - tm = localtime(&de->file.last_modified); - if (tm != NULL) { - strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", tm); - } else { - XX_httplib_strlcpy(mod, "01-Jan-1970 00:00", sizeof(mod)); - mod[sizeof(mod) - 1] = '\0'; - } - mg_url_encode(de->file_name, href, sizeof(href)); - de->conn->num_bytes_sent += - mg_printf(de->conn, - "%s%s" - " %s  %s\n", - de->conn->request_info.local_uri, - href, - de->file.is_directory ? "/" : "", - de->file_name, - de->file.is_directory ? "/" : "", - mod, - size); - -} /* XX_httplib_print_dir_entry */ +} /* mg_url_encode */