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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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
|
#include <stdio.h>
#include <limits.h>
#include <assert.h>
#include "../../../autoconf.h"
#ifdef STDC_HEADERS
// for exit()
#include <unistd.h>
#include <stdlib.h>
#endif /* STDC_HEADERS */
#include "proxytrace.h"
static
const char *MethodStrings[] = {
"NONE",
"GET",
"POST",
"HEAD",
"CONNECT",
"BAD#"
};
#define MAX_METHODS (sizeof(MethodStrings)/sizeof(*MethodStrings)-1)
static
const char *ProtocolStrings[] = {
"NONE",
"http",
"ftp",
"gopher",
"wais",
"cache_object",
"TOTAL",
"BAD#"
};
#define MAX_PROTOCOLS (sizeof(ProtocolStrings)/sizeof(*ProtocolStrings)-1)
static
const char *ExtensionStrings[] = {
"",
".html",
".gif",
".cgi",
".data",
".class",
".map",
".jpeg",
".mpeg",
".OTHER",
".BAD#"
};
#define MAX_EXTENSIONS (sizeof(ExtensionStrings)/sizeof(*ExtensionStrings)-1)
const char *MethodStr(int method) {
assert (MAX_METHODS < INT_MAX);
if (method < 0 || method > ((int)MAX_METHODS))
method = MAX_METHODS;
return MethodStrings[method];
}
const char *ProtocolStr(int protocol) {
assert (MAX_PROTOCOLS < INT_MAX);
if (protocol < 0 || protocol > ((int)MAX_PROTOCOLS))
protocol = MAX_PROTOCOLS;
return ProtocolStrings[protocol];
}
const char *ExtensionStr(int type) {
assert (MAX_EXTENSIONS < INT_MAX);
if (type < 0 || type > ((int)MAX_EXTENSIONS))
type = MAX_EXTENSIONS;
return ExtensionStrings[type];
}
const char *ExtensionTypeStr(int type) {
return (type == 0) ? "NONE" : (ExtensionStr(type)+1);
}
size_t ReadHeader(FILE *in_file, void *userBuf) {
static char defaultBuf[TRACE_HEADER_SIZE];
void *buf = (userBuf) ? userBuf : defaultBuf;
if (fread(buf, TRACE_HEADER_SIZE, 1, in_file) != 1) {
fprintf(stderr, "%s:%d error reading file header (%d bytes)\n",
__FILE__, __LINE__, TRACE_HEADER_SIZE);
exit(-2);
}
return TRACE_HEADER_SIZE;
}
size_t ReadEntry(FILE *in_file, TEntry *entry) {
size_t items_read = fread(entry, sizeof(TEntry), 1, in_file);
if (items_read < 0 || items_read > 1) {
fprintf(stderr, "%s:%d error reading trace entry (%u bytes)\n",
__FILE__, __LINE__, (unsigned)sizeof(TEntry));
exit(-2);
}
return items_read*sizeof(TEntry);
}
size_t ReadEntryV1(FILE *in_file, TEntry *entry) {
size_t items_read = fread(&entry -> head, sizeof(TEntryHeader), 1, in_file);
if (items_read == 1)
items_read = fread(&entry -> tail, sizeof(TEntryTail), 1, in_file);
if (items_read < 0 || items_read > 1) {
fprintf(stderr, "%s:%d error reading v1 trace entry (%u bytes)\n",
__FILE__, __LINE__, (unsigned)(sizeof(TEntryHeader)+sizeof(TEntryTail)));
exit(-2);
}
entry -> url = 0; /* initialize unused field */
return items_read*(sizeof(TEntryHeader)+sizeof(TEntryTail));
}
|