From: Daniel Stenberg Date: Sat, 30 Apr 2005 23:30:55 +0000 (+0000) Subject: Moved common code to util.[ch] instead of having it duplicated in sws.c X-Git-Tag: curl-7_14_0~85 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=23da55a9f14ff5c48f0bc297f1ff745eb3dc7554;p=curl Moved common code to util.[ch] instead of having it duplicated in sws.c and sockfilt.c. For good-to-have functions for the servers written in C. --- diff --git a/tests/server/Makefile.am b/tests/server/Makefile.am index 4444ed5b4..ab36e2798 100644 --- a/tests/server/Makefile.am +++ b/tests/server/Makefile.am @@ -29,10 +29,10 @@ noinst_PROGRAMS = sws getpart sockfilt useful = getpart.c getpart.h $(top_srcdir)/lib/strequal.c \ $(top_srcdir)/lib/base64.c $(top_srcdir)/lib/mprintf.c \ - $(top_srcdir)/lib/memdebug.c + $(top_srcdir)/lib/memdebug.c $(top_srcdir)/lib/timeval.c -sws_SOURCES= sws.c $(useful) -sockfilt_SOURCES = sockfilt.c $(useful) $(top_srcdir)/lib/inet_pton.c +sws_SOURCES= sws.c util.c $(useful) +sockfilt_SOURCES = sockfilt.c util.c $(useful) $(top_srcdir)/lib/inet_pton.c getpart_SOURCES= testpart.c $(useful) extra_DIST = base64.pl diff --git a/tests/server/sockfilt.c b/tests/server/sockfilt.c index cf6cc499f..e177c676e 100644 --- a/tests/server/sockfilt.c +++ b/tests/server/sockfilt.c @@ -23,7 +23,8 @@ /* Purpose * - * 1. Accept a TCP connection on a custom port (ipv4 or ipv6). + * 1. Accept a TCP connection on a custom port (ipv4 or ipv6), or connect + * to a given (localhost) port. * * 2. Get commands on STDIN. Pass data on to the TCP stream. * Get data from TCP stream and pass on to STDOUT. @@ -77,6 +78,7 @@ #include "curlx.h" /* from the private lib dir */ #include "getpart.h" #include "inet_pton.h" +#include "util.h" #ifndef FALSE #define FALSE 0 @@ -118,43 +120,7 @@ const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }}; static volatile int sigpipe; /* Why? It's not used */ #endif -char *socklogfile = (char *)DEFAULT_LOGFILE; - -/* - * ourerrno() returns the errno (or equivalent) on this platform to - * hide platform specific for the function that calls this. - */ -static int ourerrno(void) -{ -#ifdef WIN32 - return (int)GetLastError(); -#else - return errno; -#endif -} - -static void logmsg(const char *msg, ...) -{ - time_t t = time(NULL); - va_list ap; - struct tm *curr_time = localtime(&t); - char buffer[256]; /* possible overflow if you pass in a huge string */ - FILE *logfp; - - va_start(ap, msg); - vsprintf(buffer, msg, ap); - va_end(ap); - - logfp = fopen(socklogfile, "a"); - - fprintf(logfp?logfp:stderr, /* write to stderr if the logfile doesn't open */ - "%02d:%02d:%02d %s\n", - curr_time->tm_hour, - curr_time->tm_min, - curr_time->tm_sec, buffer); - if(logfp) - fclose(logfp); -} +const char *serverlogfile = (char *)DEFAULT_LOGFILE; static void lograw(unsigned char *buffer, int len) { @@ -492,7 +458,7 @@ int main(int argc, char *argv[]) else if(!strcmp("--logfile", argv[arg])) { arg++; if(argc>arg) - socklogfile = argv[arg++]; + serverlogfile = argv[arg++]; } else if(!strcmp("--ipv6", argv[arg])) { #ifdef ENABLE_IPV6 diff --git a/tests/server/sws.c b/tests/server/sws.c index 3000579f2..852b27a2e 100644 --- a/tests/server/sws.c +++ b/tests/server/sws.c @@ -57,6 +57,7 @@ #include "curlx.h" /* from the private lib dir */ #include "getpart.h" +#include "util.h" #ifdef ENABLE_IPV6 #define SWS_IPV6 @@ -124,6 +125,8 @@ void storerequest(char *reqbuf); #define DEFAULT_LOGFILE "log/sws.log" #endif +const char *serverlogfile = DEFAULT_LOGFILE; + #define SWSVERSION "cURL test suite HTTP server/0.1" #define REQUEST_DUMP "log/server.input" @@ -185,30 +188,6 @@ static const char *doc404 = "HTTP/1.1 404 Not Found\r\n" static volatile int sigpipe; /* Why? It's not used */ #endif -static void logmsg(const char *msg, ...) -{ - time_t t = time(NULL); - va_list ap; - struct tm *curr_time = localtime(&t); - char buffer[256]; /* possible overflow if you pass in a huge string */ - FILE *logfp; - - va_start(ap, msg); - vsprintf(buffer, msg, ap); - va_end(ap); - - logfp = fopen(DEFAULT_LOGFILE, "a"); - - fprintf(logfp?logfp:stderr, /* write to stderr if the logfile doesn't open */ - "%02d:%02d:%02d %s\n", - curr_time->tm_hour, - curr_time->tm_min, - curr_time->tm_sec, buffer); - if(logfp) - fclose(logfp); -} - - #ifdef SIGPIPE static void sigpipe_handler(int sig) { diff --git a/tests/server/util.c b/tests/server/util.c new file mode 100644 index 000000000..74d5dcdfe --- /dev/null +++ b/tests/server/util.c @@ -0,0 +1,95 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) 1998 - 2005, Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at http://curl.haxx.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + * $Id$ + ***************************************************************************/ +#include "setup.h" /* portability help from the lib directory */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_SYS_SOCKET_H +#include +#endif +#ifdef HAVE_NETINET_IN_H +#include +#endif +#ifdef _XOPEN_SOURCE_EXTENDED +/* This define is "almost" required to build on HPUX 11 */ +#include +#endif +#ifdef HAVE_NETDB_H +#include +#endif + +#include "curlx.h" /* from the private lib dir */ +#include "getpart.h" +#include "util.h" + +/* + * ourerrno() returns the errno (or equivalent) on this platform to + * hide platform specific for the function that calls this. + */ +int ourerrno(void) +{ +#ifdef WIN32 + return (int)GetLastError(); +#else + return errno; +#endif +} + +/* someone else must set this properly */ +extern char *serverlogfile; + +void logmsg(const char *msg, ...) +{ + va_list ap; + char buffer[256]; /* possible overflow if you pass in a huge string */ + FILE *logfp; + + struct timeval tv = curlx_tvnow(); + struct tm *now = + localtime(&tv.tv_sec); /* not multithread safe but we don't care */ + + char timebuf[12]; + snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%02ld", + now->tm_hour, now->tm_min, now->tm_sec, + tv.tv_usec/10000); + + va_start(ap, msg); + vsprintf(buffer, msg, ap); + va_end(ap); + + logfp = fopen(serverlogfile, "a"); + fprintf(logfp?logfp:stderr, /* write to stderr if the logfile doesn't open */ + "%s %s\n", timebuf, buffer); + if(logfp) + fclose(logfp); +} diff --git a/tests/server/util.h b/tests/server/util.h new file mode 100644 index 000000000..a56c5c445 --- /dev/null +++ b/tests/server/util.h @@ -0,0 +1,30 @@ +#ifndef __SERVER_UTIL_H +#define __SERVER_UTIL_H +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) 1998 - 2005, Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at http://curl.haxx.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + * $Id$ + ***************************************************************************/ + +int ourerrno(void); +void logmsg(const char *msg, ...); + + +#endif