Change web_forward_record from a hand-rolled linked list to an array.

Free it before exiting.
This commit is contained in:
Emil Mikulic 2013-04-28 23:48:16 +10:00
parent ab7af194e6
commit e6e17fa0f3

View File

@ -227,13 +227,12 @@ struct connection {
total_sent; /* header + body = total, for logging */ total_sent; /* header + body = total, for logging */
}; };
struct web_forward_record { struct forward_mapping {
const char *host; const char *host, *target_url; /* These point at argv. */
const char *target;
struct web_forward_record *next;
}; };
struct web_forward_record *web_forward = NULL; static struct forward_mapping *forward_map = NULL;
static size_t forward_map_size = 0;
struct mime_mapping { struct mime_mapping {
char *extension, *mimetype; char *extension, *mimetype;
@ -504,7 +503,7 @@ static char *make_safe_url(char *url) {
ends_in_slash = 1; ends_in_slash = 1;
/* count the slashes */ /* count the slashes */
for (i=0, num_slashes=0; i<urllen; i++) for (i=0, num_slashes=0; i < urllen; i++)
if (url[i] == '/') if (url[i] == '/')
num_slashes++; num_slashes++;
@ -560,19 +559,13 @@ static char *make_safe_url(char *url) {
return url; return url;
} }
static void add_web_forward(const char * const host, const char * const target) { static void add_forward_mapping(const char * const host,
if (debug) const char * const target_url) {
printf("add web forward %s -> %s\n", forward_map_size++;
host, target); forward_map = xrealloc(forward_map,
sizeof(*forward_map) * forward_map_size);
struct web_forward_record **ref_ptr = &web_forward; forward_map[forward_map_size - 1].host = host;
while(*ref_ptr) { forward_map[forward_map_size - 1].target_url = target_url;
ref_ptr = &((*ref_ptr)->next);
}
(*ref_ptr) = xmalloc(sizeof(struct web_forward_record));
(*ref_ptr)->host = host;
(*ref_ptr)->target = target;
(*ref_ptr)->next = NULL;
} }
/* Associates an extension with a mimetype in the mime_map. Entries are in /* Associates an extension with a mimetype in the mime_map. Entries are in
@ -893,8 +886,8 @@ static void usage(const char *argv0) {
printf("\t--forward host url (default: don't forward)\n" printf("\t--forward host url (default: don't forward)\n"
"\t\tWeb forward (301 redirect).\n" "\t\tWeb forward (301 redirect).\n"
"\t\tRequests to the host are redirected to the corresponding url.\n" "\t\tRequests to the host are redirected to the corresponding url.\n"
"\t\tThe option may be specified multiple times. In the case\n" "\t\tThe option may be specified multiple times, in which case\n"
"\t\tthe host is matched in the order of appearance.\n\n"); "\t\tthe host is matched in order of appearance.\n\n");
} }
/* Returns 1 if string is a number, 0 otherwise. Set num to NULL if /* Returns 1 if string is a number, 0 otherwise. Set num to NULL if
@ -1007,13 +1000,14 @@ static void parse_commandline(const int argc, char *argv[]) {
want_accf = 1; want_accf = 1;
} }
else if (strcmp(argv[i], "--forward") == 0) { else if (strcmp(argv[i], "--forward") == 0) {
const char *host, *url;
if (++i >= argc) if (++i >= argc)
errx(1, "missing host after --forward"); errx(1, "missing host after --forward");
const char * const host = argv[i]; host = argv[i];
if (++i >= argc) if (++i >= argc)
errx(1, "missing url after --forward"); errx(1, "missing url after --forward");
const char * const url = argv[i]; url = argv[i];
add_web_forward(host, url); add_forward_mapping(host, url);
} }
else else
errx(1, "unknown argument `%s'", argv[i]); errx(1, "unknown argument `%s'", argv[i]);
@ -1749,23 +1743,17 @@ static void process_get(struct connection *conn) {
/* test the host against web forward options */ /* test the host against web forward options */
{ {
char *host = parse_field(conn, "Host: "); char *host = parse_field(conn, "Host: ");
if(host) { if (host) {
size_t i;
if (debug) if (debug)
printf("host=\"%s\"\n", host); printf("host=\"%s\"\n", host);
for (i = 0; i < forward_map_size; i++) {
struct web_forward_record *ptr = web_forward; if (strcasecmp(forward_map[i].host, host) == 0) {
for(; ptr; ptr=ptr->next) { redirect(conn, "%s%s",
if (debug) forward_map[i].target_url, decoded_url);
printf("test for web forward record \"%s\" -> \"%s\"\n", ptr->host, ptr->target);
if(!strcasecmp(ptr->host, host)) {
redirect(conn, "%s%s", ptr->target, decoded_url);
free(decoded_url);
free(host); free(host);
free(decoded_url);
return; return;
} }
} }
free(host); free(host);
@ -2521,12 +2509,13 @@ int main(int argc, char **argv) {
/* free the mallocs */ /* free the mallocs */
{ {
size_t i; size_t i;
for (i=0; i<mime_map_size; i++) { for (i=0; i<mime_map_size; i++) {
free(mime_map[i].extension); free(mime_map[i].extension);
free(mime_map[i].mimetype); free(mime_map[i].mimetype);
} }
free(mime_map); free(mime_map);
if (forward_map)
free(forward_map);
free(keep_alive_field); free(keep_alive_field);
free(wwwroot); free(wwwroot);
} }