00001
00002 #include <stdio.h>
00003 #include <limits.h>
00004 #include <assert.h>
00005 #include "../../../autoconf.h"
00006 #ifdef STDC_HEADERS
00007
00008 #include <unistd.h>
00009 #include <stdlib.h>
00010 #endif
00011
00012 #include "proxytrace.h"
00013
00014 static
00015 const char *MethodStrings[] = {
00016 "NONE",
00017 "GET",
00018 "POST",
00019 "HEAD",
00020 "CONNECT",
00021 "BAD#"
00022 };
00023
00024 #define MAX_METHODS (sizeof(MethodStrings)/sizeof(*MethodStrings)-1)
00025
00026 static
00027 const char *ProtocolStrings[] = {
00028 "NONE",
00029 "http",
00030 "ftp",
00031 "gopher",
00032 "wais",
00033 "cache_object",
00034 "TOTAL",
00035 "BAD#"
00036 };
00037
00038 #define MAX_PROTOCOLS (sizeof(ProtocolStrings)/sizeof(*ProtocolStrings)-1)
00039
00040 static
00041 const char *ExtensionStrings[] = {
00042 "",
00043 ".html",
00044 ".gif",
00045 ".cgi",
00046 ".data",
00047 ".class",
00048 ".map",
00049 ".jpeg",
00050 ".mpeg",
00051 ".OTHER",
00052 ".BAD#"
00053 };
00054
00055 #define MAX_EXTENSIONS (sizeof(ExtensionStrings)/sizeof(*ExtensionStrings)-1)
00056
00057 const char *MethodStr(int method) {
00058 assert (MAX_METHODS < INT_MAX);
00059 if (method < 0 || method > ((int)MAX_METHODS))
00060 method = MAX_METHODS;
00061
00062 return MethodStrings[method];
00063 }
00064
00065 const char *ProtocolStr(int protocol) {
00066 assert (MAX_PROTOCOLS < INT_MAX);
00067 if (protocol < 0 || protocol > ((int)MAX_PROTOCOLS))
00068 protocol = MAX_PROTOCOLS;
00069
00070 return ProtocolStrings[protocol];
00071 }
00072
00073 const char *ExtensionStr(int type) {
00074 assert (MAX_EXTENSIONS < INT_MAX);
00075 if (type < 0 || type > ((int)MAX_EXTENSIONS))
00076 type = MAX_EXTENSIONS;
00077
00078 return ExtensionStrings[type];
00079 }
00080
00081 const char *ExtensionTypeStr(int type) {
00082
00083 return (type == 0) ? "NONE" : (ExtensionStr(type)+1);
00084 }
00085
00086
00087 size_t ReadHeader(FILE *in_file, void *userBuf) {
00088 static char defaultBuf[TRACE_HEADER_SIZE];
00089
00090 void *buf = (userBuf) ? userBuf : defaultBuf;
00091
00092 if (fread(buf, TRACE_HEADER_SIZE, 1, in_file) != 1) {
00093 fprintf(stderr, "%s:%d error reading file header (%d bytes)\n",
00094 __FILE__, __LINE__, TRACE_HEADER_SIZE);
00095 exit(-2);
00096 }
00097
00098 return TRACE_HEADER_SIZE;
00099 }
00100
00101 size_t ReadEntry(FILE *in_file, TEntry *entry) {
00102
00103 size_t items_read = fread(entry, sizeof(TEntry), 1, in_file);
00104
00105 if (items_read < 0 || items_read > 1) {
00106 fprintf(stderr, "%s:%d error reading trace entry (%u bytes)\n",
00107 __FILE__, __LINE__, (unsigned)sizeof(TEntry));
00108 exit(-2);
00109 }
00110
00111 return items_read*sizeof(TEntry);
00112 }
00113
00114 size_t ReadEntryV1(FILE *in_file, TEntry *entry) {
00115
00116 size_t items_read = fread(&entry -> head, sizeof(TEntryHeader), 1, in_file);
00117
00118 if (items_read == 1)
00119 items_read = fread(&entry -> tail, sizeof(TEntryTail), 1, in_file);
00120
00121 if (items_read < 0 || items_read > 1) {
00122 fprintf(stderr, "%s:%d error reading v1 trace entry (%u bytes)\n",
00123 __FILE__, __LINE__, (unsigned)(sizeof(TEntryHeader)+sizeof(TEntryTail)));
00124 exit(-2);
00125 }
00126
00127 entry -> url = 0;
00128
00129 return items_read*(sizeof(TEntryHeader)+sizeof(TEntryTail));
00130 }