From 8e5aec7eb7e3c485800f55a9bcabe305a92bb217 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Sun, 11 Dec 2016 15:20:08 +0100 Subject: [PATCH] Moved mg_cry to own file --- Makefile | 1 + src/httplib_cry.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++ src/libhttp.c | 61 +-------------------------------- 3 files changed, 89 insertions(+), 60 deletions(-) create mode 100644 src/httplib_cry.c diff --git a/Makefile b/Makefile index 93ba120f..3911d25b 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_connect_websocket_client.c \ src/httplib_construct_etag.c \ src/httplib_consume_socket.c \ + src/httplib_cry.c \ src/httplib_delete_file.c \ src/httplib_difftimespec.c \ src/httplib_dir_scan_callback.c \ diff --git a/src/httplib_cry.c b/src/httplib_cry.c new file mode 100644 index 00000000..2197a429 --- /dev/null +++ b/src/httplib_cry.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" + + +/* Print error message to the opened error log stream. */ +void mg_cry(const struct mg_connection *conn, const char *fmt, ...) { + + char buf[MG_BUF_LEN]; + char src_addr[IP_ADDR_STR_LEN]; + va_list ap; + struct file fi; + time_t timestamp; + + va_start(ap, fmt); + IGNORE_UNUSED_RESULT(vsnprintf_impl(buf, sizeof(buf), fmt, ap)); + va_end(ap); + buf[sizeof(buf) - 1] = 0; + + if (!conn) { + puts(buf); + return; + } + + /* Do not lock when getting the callback value, here and below. + * I suppose this is fine, since function cannot disappear in the + * same way string option can. */ + if ((conn->ctx->callbacks.log_message == NULL) + || (conn->ctx->callbacks.log_message(conn, buf) == 0)) { + + if (conn->ctx->config[ERROR_LOG_FILE] != NULL) { + if (XX_httplib_fopen(conn, conn->ctx->config[ERROR_LOG_FILE], "a+", &fi) + == 0) { + fi.fp = NULL; + } + } else fi.fp = NULL; + + if (fi.fp != NULL) { + flockfile(fi.fp); + timestamp = time(NULL); + + XX_httplib_sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa); + fprintf(fi.fp, + "[%010lu] [error] [client %s] ", + (unsigned long)timestamp, + src_addr); + + if (conn->request_info.request_method != NULL) { + fprintf(fi.fp, + "%s %s: ", + conn->request_info.request_method, + conn->request_info.request_uri); + } + + fprintf(fi.fp, "%s", buf); + fputc('\n', fi.fp); + fflush(fi.fp); + funlockfile(fi.fp); + XX_httplib_fclose(&fi); + } + } + +} /* mg_cry */ diff --git a/src/libhttp.c b/src/libhttp.c index 31907d1d..3090251a 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -828,68 +828,9 @@ void XX_httplib_set_thread_name(const char *threadName) { -/* Print error message to the opened error log stream. */ -void mg_cry(const struct mg_connection *conn, const char *fmt, ...) { - - char buf[MG_BUF_LEN]; - char src_addr[IP_ADDR_STR_LEN]; - va_list ap; - struct file fi; - time_t timestamp; - - va_start(ap, fmt); - IGNORE_UNUSED_RESULT(vsnprintf_impl(buf, sizeof(buf), fmt, ap)); - va_end(ap); - buf[sizeof(buf) - 1] = 0; - - if (!conn) { - puts(buf); - return; - } - - /* Do not lock when getting the callback value, here and below. - * I suppose this is fine, since function cannot disappear in the - * same way string option can. */ - if ((conn->ctx->callbacks.log_message == NULL) - || (conn->ctx->callbacks.log_message(conn, buf) == 0)) { - - if (conn->ctx->config[ERROR_LOG_FILE] != NULL) { - if (XX_httplib_fopen(conn, conn->ctx->config[ERROR_LOG_FILE], "a+", &fi) - == 0) { - fi.fp = NULL; - } - } else fi.fp = NULL; - - if (fi.fp != NULL) { - flockfile(fi.fp); - timestamp = time(NULL); - - XX_httplib_sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa); - fprintf(fi.fp, - "[%010lu] [error] [client %s] ", - (unsigned long)timestamp, - src_addr); - - if (conn->request_info.request_method != NULL) { - fprintf(fi.fp, - "%s %s: ", - conn->request_info.request_method, - conn->request_info.request_uri); - } - - fprintf(fi.fp, "%s", buf); - fputc('\n', fi.fp); - fflush(fi.fp); - funlockfile(fi.fp); - XX_httplib_fclose(&fi); - } - } -} - - /* Return fake connection structure. Used for logging, if connection * is not applicable at the moment of logging. */ -struct mg_connection * XX_httplib_fc( struct mg_context *ctx ) { +struct mg_connection *XX_httplib_fc( struct mg_context *ctx ) { static struct mg_connection fake_connection;