From 8e94d40e90b5ed006090af64ff7017a2d4f1a802 Mon Sep 17 00:00:00 2001 From: snowdrop Date: Sun, 9 Jul 2006 16:24:18 +0000 Subject: ported logging function to a seperate file (nanohttp-logging.*) and renamed all logging functions. --- nanohttp/Makefile.am | 4 +- nanohttp/nanohttp-client.c | 5 +- nanohttp/nanohttp-common.c | 158 +------------------------------------------ nanohttp/nanohttp-common.h | 140 ++++++++++++-------------------------- nanohttp/nanohttp-mime.c | 3 +- nanohttp/nanohttp-request.c | 19 +++++- nanohttp/nanohttp-response.c | 3 +- nanohttp/nanohttp-server.c | 3 +- nanohttp/nanohttp-socket.c | 3 +- nanohttp/nanohttp-ssl.c | 3 +- nanohttp/nanohttp-stream.c | 3 +- 11 files changed, 80 insertions(+), 264 deletions(-) (limited to 'nanohttp') diff --git a/nanohttp/Makefile.am b/nanohttp/Makefile.am index fc05a84..b585026 100644 --- a/nanohttp/Makefile.am +++ b/nanohttp/Makefile.am @@ -5,12 +5,12 @@ libnanohttp_ladir=$(includedir)/nanohttp-@nanohttp_release@/nanohttp libnanohttp_la_SOURCES=nanohttp-common.c nanohttp-socket.c nanohttp-client.c \ nanohttp-server.c nanohttp-stream.c nanohttp-mime.c \ nanohttp-request.c nanohttp-response.c \ - nanohttp-base64.c nanohttp-ssl.c + nanohttp-base64.c nanohttp-ssl.c nanohttp-logging.c libnanohttp_la_HEADERS=nanohttp-common.h nanohttp-socket.h nanohttp-client.h \ nanohttp-server.h nanohttp-stream.h nanohttp-mime.h \ nanohttp-request.h nanohttp-response.h \ - nanohttp-base64.h nanohttp-ssl.h + nanohttp-base64.h nanohttp-ssl.h nanohttp-logging.h libnanohttp_la_LDFLAGS= -version-info @nanohttp_version@ -release @nanohttp_release@ libnanohttp_la_CFLAGS=-I${top_srcdir} diff --git a/nanohttp/nanohttp-client.c b/nanohttp/nanohttp-client.c index 32df75f..7b9376f 100644 --- a/nanohttp/nanohttp-client.c +++ b/nanohttp/nanohttp-client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-client.c,v 1.40 2006/03/06 13:37:38 m0gg Exp $ +* $Id: nanohttp-client.c,v 1.41 2006/07/09 16:24:19 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -52,6 +52,7 @@ #include "nanohttp-client.h" #include "nanohttp-socket.h" #include "nanohttp-base64.h" +#include "nanohttp-logging.h" /*-------------------------------------------------- FUNCTION: httpc_init @@ -94,7 +95,7 @@ httpc_new(void) if ((status = hsocket_init(&res->sock)) != H_OK) { - log_warn("hsocket_init failed (%s)", herror_message(status)); + log_warn2("hsocket_init failed (%s)", herror_message(status)); return NULL; } diff --git a/nanohttp/nanohttp-common.c b/nanohttp/nanohttp-common.c index b6f0e40..0784684 100644 --- a/nanohttp/nanohttp-common.c +++ b/nanohttp/nanohttp-common.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-common.c,v 1.29 2006/03/06 13:37:38 m0gg Exp $ +* $Id: nanohttp-common.c,v 1.30 2006/07/09 16:24:19 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -54,6 +54,7 @@ #endif #include "nanohttp-common.h" +#include "nanohttp-logging.h" static int strcmpigcase(const char *s1, const char *s2) @@ -78,24 +79,6 @@ strcmpigcase(const char *s1, const char *s2) return 1; } -#ifdef WIN32 -#ifndef __MINGW32__ - -/* not thread safe!*/ -char * -VisualC_funcname(const char *file, int line) -{ - static char buffer[256]; - int i = strlen(file) - 1; - while (i > 0 && file[i] != '\\') - i--; - sprintf(buffer, "%s:%d", (file[i] != '\\') ? file : (file + i + 1), line); - return buffer; -} - -#endif -#endif - typedef struct _herror_impl_t { int errcode; @@ -156,143 +139,6 @@ herror_release(herror_t err) } - -static log_level_t loglevel = HLOG_DEBUG; -static char logfile[75] = { '\0' }; -static int log_background = 0; - -log_level_t -log_set_level(log_level_t level) -{ - log_level_t old = loglevel; - loglevel = level; - return old; -} - - -log_level_t -log_get_level() -{ - return loglevel; -} - - -void -log_set_file(const char *filename) -{ - if (filename) - strncpy(logfile, filename, 75); - else - logfile[0] = '\0'; -} - -void -log_set_background(int state) -{ - log_background = state; -} - -char * -log_get_file() -{ - if (logfile[0] == '\0') - return NULL; - return logfile; -} - -static void -log_write(log_level_t level, const char *prefix, - const char *func, const char *format, va_list ap) -{ - char buffer[1054]; - char buffer2[1054]; - FILE *f; - - if (level < loglevel) - return; - - if (!log_background || log_get_file()) - { -#ifdef WIN32 - sprintf(buffer, "*%s*: [%s] %s\n", prefix, func, format); -#else - sprintf(buffer, "*%s*:(%ld) [%s] %s\n", - prefix, pthread_self(), func, format); -#endif - vsprintf(buffer2, buffer, ap); - if (!log_background) - { - printf(buffer2); - fflush(stdout); - } - - if (log_get_file()) - { - f = fopen(log_get_file(), "a"); - if (!f) - f = fopen(log_get_file(), "w"); - if (f) - { - fprintf(f, buffer2); - fflush(f); - fclose(f); - } - } - } -} - -void -log_verbose(const char *FUNC, const char *format, ...) -{ - va_list ap; - - va_start(ap, format); - log_write(HLOG_VERBOSE, "VERBOSE", FUNC, format, ap); - va_end(ap); -} - -void -log_debug(const char *FUNC, const char *format, ...) -{ - va_list ap; - - va_start(ap, format); - log_write(HLOG_DEBUG, "DEBUG", FUNC, format, ap); - va_end(ap); -} - -void -log_info(const char *FUNC, const char *format, ...) -{ - va_list ap; - - va_start(ap, format); - log_write(HLOG_INFO, "INFO", FUNC, format, ap); - va_end(ap); -} - -void -log_warn(const char *FUNC, const char *format, ...) -{ - va_list ap; - - va_start(ap, format); - log_write(HLOG_WARN, "WARN", FUNC, format, ap); - va_end(ap); -} - -void -log_error(const char *FUNC, const char *format, ...) -{ - va_list ap; - - va_start(ap, format); - log_write(HLOG_ERROR, "ERROR", FUNC, format, ap); - va_end(ap); -} - - - hpair_t * hpairnode_new(const char *key, const char *value, hpair_t * next) { diff --git a/nanohttp/nanohttp-common.h b/nanohttp/nanohttp-common.h index c018d45..2cd4bb8 100644 --- a/nanohttp/nanohttp-common.h +++ b/nanohttp/nanohttp-common.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-common.h,v 1.30 2006/05/01 07:30:34 m0gg Exp $ + * $Id: nanohttp-common.h,v 1.31 2006/07/09 16:24:19 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -29,7 +29,7 @@ #define HEADER_CONTENT_ID "Content-Id" #define HEADER_CONTENT_TRANSFER_ENCODING "Content-Transfer-Encoding" -#define TRANSFER_ENCODING_CHUNKED "chunked" +#define TRANSFER_ENCODING_CHUNKED "chunked" /** * @@ -38,6 +38,7 @@ * There are a few header fields which have general applicability for both * request and response messages, but which do not apply to the entity being * transferred. These header fields apply only to the message being transmitted. + * (see RFC2616) * */ #define HEADER_CACHE_CONTROL "Cache-Control" @@ -126,13 +127,16 @@ #define HEADER_TRANSFER_EXTENSION "TE" #define HEADER_USER_AGENT "User-Agent" +/** + * + * nanohttp command line flags + * + */ #define NHTTPD_ARG_PORT "-NHTTPport" #define NHTTPD_ARG_TERMSIG "-NHTTPtsig" #define NHTTPD_ARG_MAXCONN "-NHTTPmaxconn" #define NHTTPD_ARG_TIMEOUT "-NHTTPtimeout" -#define NHTTP_ARG_LOGFILE "-NHTTPlog" - #define NHTTP_ARG_CERT "-NHTTPcert" #define NHTTP_ARG_CERTPASS "-NHTTPcertpass" #define NHTTP_ARG_CA "-NHTTPCA" @@ -180,49 +184,49 @@ #define FILE_ERROR_READ 8001 /* Socket errors */ -#define HSOCKET_ERROR_CREATE 1001 -#define HSOCKET_ERROR_GET_HOSTNAME 1002 -#define HSOCKET_ERROR_CONNECT 1003 -#define HSOCKET_ERROR_SEND 1004 -#define HSOCKET_ERROR_RECEIVE 1005 -#define HSOCKET_ERROR_BIND 1006 -#define HSOCKET_ERROR_LISTEN 1007 -#define HSOCKET_ERROR_ACCEPT 1008 -#define HSOCKET_ERROR_NOT_INITIALIZED 1009 -#define HSOCKET_ERROR_IOCTL 1010 -#define HSOCKET_ERROR_SSLCLOSE 1011 -#define HSOCKET_ERROR_SSLCTX 1011 +#define HSOCKET_ERROR_CREATE 1001 +#define HSOCKET_ERROR_GET_HOSTNAME 1002 +#define HSOCKET_ERROR_CONNECT 1003 +#define HSOCKET_ERROR_SEND 1004 +#define HSOCKET_ERROR_RECEIVE 1005 +#define HSOCKET_ERROR_BIND 1006 +#define HSOCKET_ERROR_LISTEN 1007 +#define HSOCKET_ERROR_ACCEPT 1008 +#define HSOCKET_ERROR_NOT_INITIALIZED 1009 +#define HSOCKET_ERROR_IOCTL 1010 +#define HSOCKET_ERROR_SSLCLOSE 1011 +#define HSOCKET_ERROR_SSLCTX 1011 /* URL errors */ -#define URL_ERROR_UNKNOWN_PROTOCOL 1101 -#define URL_ERROR_NO_PROTOCOL 1102 -#define URL_ERROR_NO_HOST 1103 +#define URL_ERROR_UNKNOWN_PROTOCOL 1101 +#define URL_ERROR_NO_PROTOCOL 1102 +#define URL_ERROR_NO_HOST 1103 /* Stream errors */ -#define STREAM_ERROR_INVALID_TYPE 1201 -#define STREAM_ERROR_SOCKET_ERROR 1202 -#define STREAM_ERROR_NO_CHUNK_SIZE 1203 -#define STREAM_ERROR_WRONG_CHUNK_SIZE 1204 +#define STREAM_ERROR_INVALID_TYPE 1201 +#define STREAM_ERROR_SOCKET_ERROR 1202 +#define STREAM_ERROR_NO_CHUNK_SIZE 1203 +#define STREAM_ERROR_WRONG_CHUNK_SIZE 1204 /* MIME errors */ -#define MIME_ERROR_NO_BOUNDARY_PARAM 1301 -#define MIME_ERROR_NO_START_PARAM 1302 -#define MIME_ERROR_PARSE_ERROR 1303 -#define MIME_ERROR_NO_ROOT_PART 1304 -#define MIME_ERROR_NOT_MIME_MESSAGE 1305 +#define MIME_ERROR_NO_BOUNDARY_PARAM 1301 +#define MIME_ERROR_NO_START_PARAM 1302 +#define MIME_ERROR_PARSE_ERROR 1303 +#define MIME_ERROR_NO_ROOT_PART 1304 +#define MIME_ERROR_NOT_MIME_MESSAGE 1305 /* General errors */ -#define GENERAL_INVALID_PARAM 1400 +#define GENERAL_INVALID_PARAM 1400 #define GENERAL_HEADER_PARSE_ERROR 1401 /* Thread errors */ -#define THREAD_BEGIN_ERROR 1500 +#define THREAD_BEGIN_ERROR 1500 /* XML Errors */ -#define XML_ERROR_EMPTY_DOCUMENT 1600 -#define XML_ERROR_PARSE 1601 +#define XML_ERROR_EMPTY_DOCUMENT 1600 +#define XML_ERROR_PARSE 1601 /* SSL Errors */ #define HSSL_ERROR_CA_LIST 1710 @@ -259,7 +263,14 @@ typedef enum _http_version typedef enum _hreq_method { HTTP_REQUEST_POST, - HTTP_REQUEST_GET + HTTP_REQUEST_GET, + HTTP_REQUEST_OPTIONS, + HTTP_REQUEST_HEAD, + HTTP_REQUEST_PUT, + HTTP_REQUEST_DELETE, + HTTP_REQUEST_TRACE, + HTTP_REQUEST_CONNECT, + HTTP_REQUEST_UNKOWN } hreq_method_t; @@ -547,69 +558,6 @@ attachments_t *attachments_new(); /* should be used internally */ void attachments_free(attachments_t * message); void attachments_add_part(attachments_t * attachments, part_t * part); - -/* logging stuff */ -typedef enum log_level -{ - HLOG_VERBOSE, - HLOG_DEBUG, - HLOG_INFO, - HLOG_WARN, - HLOG_ERROR, - HLOG_FATAL -} log_level_t; - - - -log_level_t log_set_level(log_level_t level); -log_level_t log_get_level(); - -void log_set_file(const char *filename); -char *log_get_file(); - -#ifdef WIN32 -#if defined(_MSC_VER) && _MSC_VER <= 1200 -char *VisualC_funcname(const char *file, int line); /* not thread safe! */ -#define __FUNCTION__ VisualC_funcname(__FILE__, __LINE__) -#endif -#endif - -#define log_verbose1(a1) log_verbose(__FUNCTION__, a1) -#define log_verbose2(a1,a2) log_verbose(__FUNCTION__, a1,a2) -#define log_verbose3(a1,a2,a3) log_verbose(__FUNCTION__, a1,a2,a3) -#define log_verbose4(a1,a2,a3,a4) log_verbose(__FUNCTION__, a1,a2,a3,a4) -#define log_verbose5(a1,a2,a3,a4,a5) log_verbose(__FUNCTION__, a1,a2,a3,a4,a5) - -#define log_debug1(a1) log_debug(__FUNCTION__, a1) -#define log_debug2(a1,a2) log_debug(__FUNCTION__, a1,a2) -#define log_debug3(a1,a2,a3) log_debug(__FUNCTION__, a1,a2,a3) -#define log_debug4(a1,a2,a3,a4) log_debug(__FUNCTION__, a1,a2,a3,a4) -#define log_debug5(a1,a2,a3,a4,a5) log_debug(__FUNCTION__, a1,a2,a3,a4,a5) - -#define log_info1(a1) log_info(__FUNCTION__, a1) -#define log_info2(a1,a2) log_info(__FUNCTION__, a1,a2) -#define log_info3(a1,a2,a3) log_info(__FUNCTION__, a1,a2,a3) -#define log_info4(a1,a2,a3,a4) log_info(__FUNCTION__, a1,a2,a3,a4) -#define log_info5(a1,a2,a3,a4,a5) log_info(__FUNCTION__, a1,a2,a3,a4,a5) - -#define log_warn1(a1) log_warn(__FUNCTION__, a1) -#define log_warn2(a1,a2) log_warn(__FUNCTION__, a1,a2) -#define log_warn3(a1,a2,a3) log_warn(__FUNCTION__, a1,a2,a3) -#define log_warn4(a1,a2,a3,a4) log_warn(__FUNCTION__, a1,a2,a3,a4) -#define log_warn5(a1,a2,a3,a4,a5) log_warn(__FUNCTION__, a1,a2,a3,a4,a5) - -#define log_error1(a1) log_error(__FUNCTION__, a1) -#define log_error2(a1,a2) log_error(__FUNCTION__, a1,a2) -#define log_error3(a1,a2,a3) log_error(__FUNCTION__, a1,a2,a3) -#define log_error4(a1,a2,a3,a4) log_error(__FUNCTION__, a1,a2,a3,a4) -#define log_error5(a1,a2,a3,a4,a5) log_error(__FUNCTION__, a1,a2,a3,a4,a5) - -void log_verbose(const char *FUNC, const char *format, ...); -void log_debug(const char *FUNC, const char *format, ...); -void log_info(const char *FUNC, const char *format, ...); -void log_warn(const char *FUNC, const char *format, ...); -void log_error(const char *FUNC, const char *format, ...); - #ifdef __cplusplus } #endif diff --git a/nanohttp/nanohttp-mime.c b/nanohttp/nanohttp-mime.c index 4733dff..6a2e6d7 100755 --- a/nanohttp/nanohttp-mime.c +++ b/nanohttp/nanohttp-mime.c @@ -3,7 +3,7 @@ * | \/ | | | | \/ | | _/ * |_''_| |_| |_''_| |_'/ PARSER * -* $Id: nanohttp-mime.c,v 1.12 2006/03/06 13:37:38 m0gg Exp $ +* $Id: nanohttp-mime.c,v 1.13 2006/07/09 16:24:19 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -45,6 +45,7 @@ Buffered Reader. A helper object to read bytes from a source #include #endif +#include "nanohttp-logging.h" #include "nanohttp-mime.h" diff --git a/nanohttp/nanohttp-request.c b/nanohttp/nanohttp-request.c index 04adf00..8350848 100755 --- a/nanohttp/nanohttp-request.c +++ b/nanohttp/nanohttp-request.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-request.c,v 1.13 2006/03/07 16:20:37 m0gg Exp $ +* $Id: nanohttp-request.c,v 1.14 2006/07/09 16:24:19 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -41,6 +41,7 @@ #include #endif +#include "nanohttp-logging.h" #include "nanohttp-common.h" #include "nanohttp-request.h" @@ -108,8 +109,22 @@ _hrequest_parse_header(char *data) { if (!strcmp(key, "POST")) req->method = HTTP_REQUEST_POST; - else + else if (!strcmp(key, "GET")) req->method = HTTP_REQUEST_GET; + else if (!strcmp(key, "OPTIONS")) + req->method = HTTP_REQUEST_OPTIONS; + else if (!strcmp(key, "HEAD")) + req->method = HTTP_REQUEST_HEAD; + else if (!strcmp(key, "PUT")) + req->method = HTTP_REQUEST_PUT; + else if (!strcmp(key, "DELETE")) + req->method = HTTP_REQUEST_DELETE; + else if (!strcmp(key, "TRACE")) + req->method = HTTP_REQUEST_TRACE; + else if (!strcmp(key, "CONNECT")) + req->method = HTTP_REQUEST_CONNECT; + else + req->method = HTTP_REQUEST_UNKOWN; } /* below is key the path and tmp2 the spec */ key = (char *) strtok_r(tmp2, " ", &saveptr2); diff --git a/nanohttp/nanohttp-response.c b/nanohttp/nanohttp-response.c index cb24d10..879fd6c 100755 --- a/nanohttp/nanohttp-response.c +++ b/nanohttp/nanohttp-response.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-response.c,v 1.10 2006/03/06 13:37:38 m0gg Exp $ +* $Id: nanohttp-response.c,v 1.11 2006/07/09 16:24:19 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -41,6 +41,7 @@ #include #endif +#include "nanohttp-logging.h" #include "nanohttp-common.h" #include "nanohttp-response.h" diff --git a/nanohttp/nanohttp-server.c b/nanohttp/nanohttp-server.c index 4995723..d5d77dd 100644 --- a/nanohttp/nanohttp-server.c +++ b/nanohttp/nanohttp-server.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-server.c,v 1.61 2006/05/31 19:39:34 mrcsys Exp $ +* $Id: nanohttp-server.c,v 1.62 2006/07/09 16:24:19 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -77,6 +77,7 @@ #include #endif +#include "nanohttp-logging.h" #include "nanohttp-server.h" #include "nanohttp-base64.h" #include "nanohttp-ssl.h" diff --git a/nanohttp/nanohttp-socket.c b/nanohttp/nanohttp-socket.c index 7ce9a25..dbb79c7 100644 --- a/nanohttp/nanohttp-socket.c +++ b/nanohttp/nanohttp-socket.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-socket.c,v 1.60 2006/05/01 17:51:50 mrcsys Exp $ +* $Id: nanohttp-socket.c,v 1.61 2006/07/09 16:24:19 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -85,6 +85,7 @@ typedef int ssize_t; #include #endif +#include "nanohttp-logging.h" #include "nanohttp-socket.h" #include "nanohttp-common.h" #include "nanohttp-ssl.h" diff --git a/nanohttp/nanohttp-ssl.c b/nanohttp/nanohttp-ssl.c index 5a08486..a1f4ffb 100644 --- a/nanohttp/nanohttp-ssl.c +++ b/nanohttp/nanohttp-ssl.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-ssl.c,v 1.27 2006/05/15 13:54:25 mrcsys Exp $ +* $Id: nanohttp-ssl.c,v 1.28 2006/07/09 16:24:19 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2001-2005 Rochester Institute of Technology @@ -71,6 +71,7 @@ #endif #endif +#include "nanohttp-logging.h" #include "nanohttp-common.h" #include "nanohttp-socket.h" #include "nanohttp-ssl.h" diff --git a/nanohttp/nanohttp-stream.c b/nanohttp/nanohttp-stream.c index ba36725..bda1a2c 100755 --- a/nanohttp/nanohttp-stream.c +++ b/nanohttp/nanohttp-stream.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-stream.c,v 1.12 2006/03/06 13:37:38 m0gg Exp $ +* $Id: nanohttp-stream.c,v 1.13 2006/07/09 16:24:19 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -41,6 +41,7 @@ #include #endif +#include "nanohttp-logging.h" #include "nanohttp-stream.h" /* -- cgit v1.1-32-gdbae