$Log$
+Revision 323.1 2002/01/24 23:50:36 error
+* Trace file using lprintf() similarly to citserver
+
Revision 323.0 2002/01/13 09:07:06 ajc
* THIS IS 3.23
1998-12-03 Nathan Bryant <bryant@cs.usm.maine.edu>
* webserver.c: warning fix
+
} while (strlen(buf) > 0);
strcpy(buf, req->line);
- fprintf(stderr, "%s\n", buf);
+ lprintf(5, "HTTP: %s\n", buf);
/* Check for bogus requests */
if (is_bogus(buf)) goto bail;
* Create a new session if we have to
*/
if (TheSession == NULL) {
- fprintf(stderr, "Creating a new session\n");
+ lprintf(3, "Creating a new session\n");
TheSession = (struct wcsession *)
malloc(sizeof(struct wcsession));
memset(TheSession, 0, sizeof(struct wcsession));
#include <sys/stat.h>
#include <errno.h>
#include "webcit.h"
+#include "webserver.h"
void extract_key(char *target, char *source, char *key)
char *decoded;
size_t bytes_decoded = 0;
- fprintf(stderr, "mime_decode() called\n");
+ lprintf(9, "mime_decode() called\n");
/* Some encodings aren't really encodings */
if (!strcasecmp(encoding, "7bit"))
if ((strcasecmp(encoding, "base64"))
&& (strcasecmp(encoding, "quoted-printable"))) {
- fprintf(stderr, "ERROR: unknown MIME encoding '%s'\n", encoding);
+ lprintf(1, "ERROR: unknown MIME encoding '%s'\n", encoding);
return;
}
/*
* will never be larger than the encoded data. This is a safe
* assumption with base64, uuencode, and quoted-printable.
*/
- fprintf(stderr, "About to allocate %d bytes for decoded part\n",
+ lprintf(9, "About to allocate %d bytes for decoded part\n",
length+2048);
decoded = malloc(length+2048);
if (decoded == NULL) {
- fprintf(stderr, "ERROR: cannot allocate memory.\n");
+ lprintf(1, "ERROR: cannot allocate memory.\n");
return;
}
- fprintf(stderr, "Got it!\n");
+ lprintf(9, "Got it!\n");
- fprintf(stderr, "Decoding %s\n", encoding);
+ lprintf(9, "Decoding %s\n", encoding);
if (!strcasecmp(encoding, "base64")) {
bytes_decoded = decode_base64(decoded, part_start);
}
bytes_decoded = decode_quoted_printable(decoded,
part_start, length);
}
- fprintf(stderr, "Bytes decoded: %d\n", bytes_decoded);
+ lprintf(9, "Bytes decoded: %d\n", bytes_decoded);
if (bytes_decoded > 0) if (CallBack != NULL) {
CallBack(name, filename, fixed_partnum(partnum),
size_t length;
char nested_partnum[SIZ];
- fprintf(stderr, "the_mime_parser() called\n");
+ lprintf(9, "the_mime_parser() called\n");
ptr = content_start;
content_length = 0;
if (!strncasecmp(header, "Content-type: ", 14)) {
strcpy(content_type, &header[14]);
extract_key(name, content_type, "name");
- fprintf(stderr, "Extracted content-type <%s>\n",
+ lprintf(9, "Extracted content-type <%s>\n",
content_type);
}
if (!strncasecmp(header, "Content-Disposition: ", 21)) {
is_multipart = 0;
}
- fprintf(stderr, "is_multipart=%d, boundary=<%s>\n",
+ lprintf(9, "is_multipart=%d, boundary=<%s>\n",
is_multipart, boundary);
/* If this is a multipart message, then recursively process it */
do {
if ( (!strncasecmp(ptr, startary, strlen(startary)))
|| (!strncasecmp(ptr, endary, strlen(endary))) ) {
- fprintf(stderr, "hit boundary!\n");
+ lprintf(9, "hit boundary!\n");
if (part_start != NULL) {
if (strlen(partnum) > 0) {
sprintf(nested_partnum, "%s.%d",
/* If it's not a multipart message, then do something with it */
if (!is_multipart) {
- fprintf(stderr, "doing non-multipart thing\n");
+ lprintf(9, "doing non-multipart thing\n");
part_start = ptr;
length = 0;
while (ptr < content_end) {
/* Truncate if the header told us to */
if ( (content_length > 0) && (length > content_length) ) {
length = content_length;
- fprintf(stderr, "truncated to %ld\n", (long)content_length);
+ lprintf(9, "truncated to %ld\n", (long)content_length);
}
mime_decode(partnum,
)
{
- fprintf(stderr, "mime_parser() called\n");
+ lprintf(9, "mime_parser() called\n");
the_mime_parser("", content_start, content_end,
CallBack,
PreMultiPartCallBack,
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
+#include "webserver.h"
static int needed(const char *fmt, va_list argp)
{
int size;
if ((p = malloc(needed(fmt, argp) + 1)) == NULL) {
- fprintf(stderr, "vsnprintf: malloc failed, aborting\n");
+ lprintf(1, "vsnprintf: malloc failed, aborting\n");
abort();
}
if ((size = vsprintf(p, fmt, argp)) >= max)
#include <pthread.h>
#include <signal.h>
#include "webcit.h"
+#include "webserver.h"
+
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
RETSIGTYPE timeout(int signum)
{
- fprintf(stderr, "Connection timed out.\n");
+ lprintf(1, "Connection timed out.\n");
exit(3);
}
s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s < 0) {
- fprintf(stderr, "Can't create socket: %s\n",
+ lprintf(1, "Can't create socket: %s\n",
strerror(errno));
return(-1);
}
if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
- fprintf(stderr, "can't connect: %s\n",
+ lprintf(1, "Can't connect: %s\n",
strerror(errno));
return(-1);
}
if (pse) {
sin.sin_port = pse->s_port;
} else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
- fprintf(stderr, "Can't get %s service entry\n", service);
+ lprintf(1, "Can't get %s service entry\n", service);
return (-1);
}
phe = gethostbyname(host);
if (phe) {
memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
} else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
- fprintf(stderr, "Can't get %s host entry: %s\n",
+ lprintf(1, "Can't get %s host entry: %s\n",
host, strerror(errno));
return (-1);
}
if ((ppe = getprotobyname("tcp")) == 0) {
- fprintf(stderr, "Can't get TCP protocol entry: %s\n",
+ lprintf(1, "Can't get TCP protocol entry: %s\n",
strerror(errno));
return (-1);
}
s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
if (s < 0) {
- fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
+ lprintf(1, "Can't create socket: %s\n", strerror(errno));
return (-1);
}
signal(SIGALRM, timeout);
alarm(30);
if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
- fprintf(stderr, "can't connect to %s.%s: %s\n",
+ lprintf(1, "Can't connect to %s.%s: %s\n",
host, service, strerror(errno));
return (-1);
}
while (len < bytes) {
rlen = read(WC->serv_sock, &buf[len], bytes - len);
if (rlen < 1) {
- fprintf(stderr, "Server connection broken: %s\n",
+ lprintf(1, "Server connection broken: %s\n",
strerror(errno));
WC->connected = 0;
WC->logged_in = 0;
strbuf[len++] = ch;
} while ((ch != 10) && (ch != 13) && (ch != 0) && (len < 255));
strbuf[len - 1] = 0;
- /* fprintf(stderr, ">%s\n", strbuf); */
+ /* lprintf(9, ">%s\n", strbuf); */
}
retval = write(WC->serv_sock, &buf[bytes_written],
nbytes - bytes_written);
if (retval < 1) {
- fprintf(stderr, "Server connection broken: %s\n",
+ lprintf(1, "Server connection broken: %s\n",
strerror(errno));
WC->connected = 0;
WC->logged_in = 0;
strcat(buf, "\n");
serv_write(buf, strlen(buf));
- /* fprintf(stderr, "<%s", buf); */
+ /* lprintf(9, "<%s", buf); */
}
#include <signal.h>
#include <sys/time.h>
#include "webcit.h"
+#include "webserver.h"
char *ascmonths[] = {
char *safestrncpy(char *dest, const char *src, size_t n)
{
if (dest == NULL || src == NULL) {
- fprintf(stderr, "safestrncpy: NULL argument\n");
+ lprintf(1, "safestrncpy: NULL argument\n");
abort();
}
strncpy(dest, src, n);
#include <pthread.h>
#include <signal.h>
#include "webcit.h"
+#include "webserver.h"
#include "mime_parser.h"
/*
fstat(fileno(fp), &statbuf);
bytes = statbuf.st_size;
- fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
+ lprintf(5, "Static: %s, %ld bytes\n", what, bytes);
wprintf("Content-length: %ld\n", (long) bytes);
wprintf("\n");
bigbuffer = malloc(bytes);
char *encoding, void *userdata)
{
- fprintf(stderr, "UPLOAD HANDLER CALLED\n");
- fprintf(stderr, " name = %s\n", name);
- fprintf(stderr, "filename = %s\n", filename);
- fprintf(stderr, "encoding = %s\n", encoding);
- fprintf(stderr, " type = %s\n", cbtype);
- fprintf(stderr, " length = %ld\n", (long)length);
+ lprintf(9, "UPLOAD HANDLER CALLED\n");
+ lprintf(9, " name = %s\n", name);
+ lprintf(9, "filename = %s\n", filename);
+ lprintf(9, "encoding = %s\n", encoding);
+ lprintf(9, " type = %s\n", cbtype);
+ lprintf(9, " length = %ld\n", (long)length);
if (length > 0) {
WC->upload = malloc(length);
memcpy(WC->upload, content, length);
}
else {
- fprintf(stderr, "malloc() failed: %s\n",
+ lprintf(1, "malloc() failed: %s\n",
strerror(errno));
}
}
}
if (ContentLength > 0) {
- fprintf(stderr, "Content length: %d\n", ContentLength);
+ lprintf(5, "Content length: %d\n", ContentLength);
content = malloc(ContentLength + 1);
memset(content, 0, ContentLength+1);
BytesRead = 0;
#include <pthread.h>
#include <signal.h>
#include "webcit.h"
+#include "webserver.h"
#ifndef HAVE_SNPRINTF
int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
#endif
+int verbosity = 9; /* Logging level */
int msock; /* master listening socket */
extern void *context_loop(int);
extern void *housekeeping_loop(void);
sin.sin_addr.s_addr = INADDR_ANY;
if (port_number == 0) {
- fprintf(stderr,
- "webcit: Cannot start: no port number specified.\n");
+ lprintf(1, "Cannot start: no port number specified.\n");
exit(1);
}
sin.sin_port = htons((u_short) port_number);
s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
if (s < 0) {
- fprintf(stderr, "webcit: Can't create a socket: %s\n",
+ lprintf(1, "Can't create a socket: %s\n",
strerror(errno));
exit(errno);
}
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
- fprintf(stderr, "webcit: Can't bind: %s\n", strerror(errno));
+ lprintf(1, "Can't bind: %s\n", strerror(errno));
exit(errno);
}
if (listen(s, queue_len) < 0) {
- fprintf(stderr, "webcit: Can't listen: %s\n", strerror(errno));
+ lprintf(1, "Can't listen: %s\n", strerror(errno));
exit(errno);
}
return (s);
}
rlen = read(sock, &buf[len], bytes - len);
if (rlen < 1) {
- fprintf(stderr, "client_read() failed: %s\n",
+ lprintf(2, "client_read() failed: %s\n",
strerror(errno));
return(-1);
}
pthread_t SessThread; /* Thread descriptor */
pthread_attr_t attr; /* Thread attributes */
- fprintf(stderr, "Creating a new thread\n");
+ lprintf(3, "Creating a new thread\n");
/* set attributes for the new thread */
pthread_attr_init(&attr);
if (pthread_create(&SessThread, &attr,
(void *(*)(void *)) worker_entry, NULL)
!= 0) {
- fprintf(stderr, "webcit: can't create thread: %s\n",
+ lprintf(1, "Can't create thread: %s\n",
strerror(errno));
}
}
freopen(tracefile, "w", stderr);
freopen(tracefile, "r", stdin);
break;
+ case 'x':
+ verbosity = atoi(optarg);
+ break;
case 'c':
server_cookie = malloc(SIZ);
if (server_cookie != NULL) {
if (gethostname(
&server_cookie[strlen(server_cookie)],
200) != 0) {
- fprintf(stderr, "gethostname: %s\n",
+ lprintf(2, "gethostname: %s\n",
strerror(errno));
free(server_cookie);
}
defaultport = argv[optind];
}
/* Tell 'em who's in da house */
- fprintf(stderr, SERVER "\n"
+ lprintf(1, SERVER "\n"
"Copyright (C) 1996-2001 by the Citadel/UX development team.\n"
"This software is distributed under the terms of the GNU General Public\n"
"License. If you paid for this software, someone is ripping you off.\n\n");
* wcsession struct to which the thread is currently bound.
*/
if (pthread_key_create(&MyConKey, NULL) != 0) {
- fprintf(stderr, "Can't create TSD key: %s\n", strerror(errno));
+ lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
}
/*
* There is no need to check for errors, because ig_tcp_server()
* exits if it doesn't succeed.
*/
- fprintf(stderr, "Attempting to bind to port %d...\n", port);
+ lprintf(2, "Attempting to bind to port %d...\n", port);
msock = ig_tcp_server(port, LISTEN_QUEUE_LENGTH);
- fprintf(stderr, "Listening on socket %d\n", msock);
+ lprintf(2, "Listening on socket %d\n", msock);
signal(SIGPIPE, SIG_IGN);
pthread_mutex_init(&SessionListMutex, NULL);
}
if (ssock < 0) {
- fprintf(stderr, "webcit: accept() failed: %s\n",
- strerror(errno));
+ lprintf(2, "accept() failed: %s\n", strerror(errno));
} else {
/* Set the SO_REUSEADDR socket option */
i = 1;
pthread_exit(NULL);
}
+
+
+int lprintf(int loglevel, const char *format, ...)
+{
+ va_list ap;
+ char buf[4096];
+
+ va_start(ap, format);
+ vsprintf(buf, format, ap);
+ va_end(ap);
+
+ if (loglevel <= verbosity) {
+ struct timeval tv;
+ struct tm *tim;
+
+ gettimeofday(&tv, NULL);
+ tim = localtime(&(tv.tv_sec));
+
+ if (WC && WC->wc_session) {
+ fprintf(stderr,
+ "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%ld:%d] %s",
+ tim->tm_year + 1900, tim->tm_mon + 1,
+ tim->tm_mday, tim->tm_hour, tim->tm_min,
+ tim->tm_sec, (long)tv.tv_usec / 1000,
+ pthread_self(), WC->wc_session, buf);
+ } else {
+ fprintf(stderr,
+ "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%ld] %s",
+ tim->tm_year + 1900, tim->tm_mon + 1,
+ tim->tm_mday, tim->tm_hour, tim->tm_min,
+ tim->tm_sec, (long)tv.tv_usec / 1000,
+ pthread_self(), buf);
+ }
+ }
+ return 1;
+}
/* $Id$ */
int client_gets(int sock, char *buf);
int client_read(int sock, char *buf, int bytes);
+int lprintf(int loglevel, const char *format, ...);