protocol 5.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/* Copyright 1998 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* Generic (c.protocol)
 *
 *
 * StB - 09/10/97 - started
 *
 */
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include" protocol.h"

#ifdef USE_SYSLOG
#include "<syslog$dir>.C-veneer.h.syslog"
#endif

/*#define DEBUG_FILENAME	"ADFS::4.$.Trace.ProtoHTTP"*/
#define DEBUG_FILENAME  "RAM:$.HTTP_Trace"

/*************************************************************/
/* static void protocol_debug(const char *format, ...	     */
/*************************************************************/
/* Output trace information to the TRACE_FILE		     */
/*************************************************************/
Stewart Brodie's avatar
Stewart Brodie committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#ifdef TRACE
void protocol_fast_debug(const char *format, ...)
{
		va_list ap;
		#ifdef USE_SYSLOG
		static char buffer[1024];
		va_start(ap, format);
		vsprintf(buffer, format, ap);
		va_end(ap);
		(void) xsyslog_logmessage("HTTP", buffer, 100);
		#else
		FILE *fp = fopen(DEBUG_FILENAME, "a");

		if (fp != 0) {
			va_start(ap, format);
			(void) vfprintf(fp, format, ap);
			va_end(ap);
			fclose(fp);
		}
		#endif
}
#endif

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
#ifdef TRACE
void protocol_debug(const char *format, ...)
{
	#ifdef TRACE
	if (getenv("NoTrace$HTTP")) return; else {
		va_list ap;
		#ifdef USE_SYSLOG
		static char buffer[1024];
		va_start(ap, format);
		vsprintf(buffer, format, ap);
		va_end(ap);
		(void) xsyslog_logmessage("HTTP", buffer, 100);
		#else
		FILE *fp = fopen(DEBUG_FILENAME, "a");

		if (fp != 0) {
			va_start(ap, format);
			(void) vfprintf(fp, format, ap);
			va_end(ap);
			fclose(fp);
		}
		#endif
	}
	#else
	(void) format;
	#endif
}
#endif


#ifdef TRACE

#ifndef USE_SYSLOG
static char *filter(char *buf, const char *ptr, int max)
{
	int i;
	for (i=0; i<=max; ++i) {
		int p = ptr[i];
		if (p<32 || p == 127) buf[i] = '.'; else buf[i] = p;
	}
	buf[i] = '\0';
	return buf;
}
#endif

void protocol_dump(const char *ptr, int size)
{
	#ifdef TRACE
	if (getenv("NoTrace$HTTP") || getenv("NoTrace$HTTPRaw")) return; else {
		#ifdef USE_SYSLOG
		(void) xsyslog_logdata("HTTP", 100, ptr, size, 0);
		#else
		FILE *fp = fopen(DEBUG_FILENAME, "a");
		if (fp != NULL) {
			int ctr;
			char buf[48];
			fprintf(fp, "%08x: ", 0);
			for (ctr=0; ctr<size; ++ctr) {
				fprintf(fp, "%02x ", ptr[ctr]);
				if ((ctr & 31) == 31) {
					fprintf(fp, "  %s",
						filter(buf, ptr+(ctr & ~31), ctr & 31));
					fprintf(fp, "\n%08x: ", ctr+1);
				}
			}
			if (ctr & 31) {
				const char *start = ptr + (ctr & ~31);
				while (ctr & 31) {
					fprintf(fp, "   ");
					++ctr;
				}
				fprintf(fp, "  %s", filter(buf, start, (size & 31)-1));
			}
			fputc('\n', fp);
			fclose(fp);
		}
		#endif
	}
	#else
	(void) ptr;
	(void) size;
	#endif
}
#endif

#ifdef TRACE
char *protocol_states(int state)
{
	switch (state) {
		case HTTP_NEWSESSION: return "HTTP_NEWSESSION";
		case HTTP_NOHOST: return "HTTP_NOHOST";
		case HTTP_DNS: return "HTTP_DNS";
		case HTTP_CONNECTING: return "HTTP_CONNECTING";
		case HTTP_SENDING_REQUEST: return "HTTP_SENDING_REQUEST";
		case HTTP_SENT_REQUEST: return "HTTP_SENT_REQUEST";
		case HTTP_RECEIVING: return "HTTP_RECEIVING";
		case HTTP_RECEIVING_BODY: return "HTTP_RECEIVING_BODY";
		case HTTP_RECEIVED: return "HTTP_RECEIVED";
		case HTTP_DONE: return "HTTP_DONE";
		case HTTP_CONNECT_TIMED_OUT: return "HTTP_CONNECT_TIMED_OUT";
		case HTTP_ERROR_READ: return "HTTP_ERROR_READ";
		case HTTP_ERROR_HOSTNAME: return "HTTP_ERROR_HOSTNAME";
		case HTTP_ERROR: return "HTTP_ERROR";
		case HTTP_ERROR_NOLINK: return "HTTP_ERROR_NOLINK";
		case HTTP_ERROR_BADSESSION: return "HTTP_ERROR_BADSESSION";
		case HTTP_ERROR_NOCONNECTION: return "HTTP_ERROR_NOCONNECTION";
		case HTTP_ERROR_NO_PARSE: return "HTTP_ERROR_NO_PARSE";
		case HTTP_ERROR_NO_PROXY: return "HTTP_ERROR_NO_PROXY";
		default: return "*UNKNOWN*";
	}
}
#endif

#ifdef TRACE
char *protocol_network_states(int status)
{
	switch (status) {
		case status_NOT_YET_CONNECTED: return "status_NOT_YET_CONNECTED";
		case status_CONNECTED_TO_SERVER: return "status_CONNECTED_TO_SERVER";
		case status_SENT_REQUEST: return "status_SENT_REQUEST";
		case status_SENT_DATA: return "status_SENT_DATA";
		case status_WAIT_INITIAL_RESPONSE: return "status_WAIT_INITIAL_RESPONSE";
		case status_HAVE_INITIAL_RESPONSE: return "status_HAVE_INITIAL_RESPONSE";
		case status_WAIT_FOR_BODY: return "status_WAIT_FOR_BODY";
		case status_TRANSFER_IN_PROGRESS: return "status_TRANSFER_IN_PROGRESS";
		case status_READING_REPLY: return "status_READING_REPLY";
		case status_ALL_DATA_RECEIVED: return "status_ALL_DATA_RECEIVED";
		case status_ABORTED: return "status_ABORTED";

		default:
		{
		        static char charbuf[16];
		        sprintf(charbuf, "&%08X", status);
		        return charbuf;
		}
	}
}
#endif