1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-21 23:58:49 -05:00

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.
This commit is contained in:
Daniel Stenberg 2005-04-30 23:30:55 +00:00
parent 41e6292e7b
commit 23da55a9f1
5 changed files with 136 additions and 66 deletions

View File

@ -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

View File

@ -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

View File

@ -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)
{

95
tests/server/util.c Normal file
View File

@ -0,0 +1,95 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef _XOPEN_SOURCE_EXTENDED
/* This define is "almost" required to build on HPUX 11 */
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#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);
}

30
tests/server/util.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef __SERVER_UTIL_H
#define __SERVER_UTIL_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, 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