Replaced the internally used linked list with a call to the hashmap

module.  Code reuse is a good thing.
This commit is contained in:
Robert James Kaes 2002-04-09 20:04:39 +00:00
parent 607e4c4c6c
commit 7409281e34

View File

@ -1,8 +1,7 @@
/* $Id: anonymous.c,v 1.10 2001-12-15 20:02:59 rjkaes Exp $ /* $Id: anonymous.c,v 1.11 2002-04-09 20:04:39 rjkaes Exp $
* *
* Handles insertion and searches for headers which should be let through when * Handles insertion and searches for headers which should be let through when
* the anonymous feature is turned on. The headers are stored in a linked * the anonymous feature is turned on.
* list.
* *
* Copyright (C) 2000 Robert James Kaes (rjkaes@flarenet.com) * Copyright (C) 2000 Robert James Kaes (rjkaes@flarenet.com)
* *
@ -20,51 +19,33 @@
#include "tinyproxy.h" #include "tinyproxy.h"
#include "anonymous.h" #include "anonymous.h"
#include "hashmap.h"
#include "log.h" #include "log.h"
#include "utils.h" #include "utils.h"
/* static hashmap_t anonymous_map = NULL;
* Structure holding the various anonymous headers.
*/
struct anonymous_header_s {
char *header;
struct anonymous_header_s *next;
};
struct anonymous_header_s *anonymous_root = NULL;
inline short int short int
is_anonymous_enabled(void) is_anonymous_enabled(void)
{ {
return (anonymous_root) ? 1 : 0; return (anonymous_map != NULL) ? 1 : 0;
} }
/* /*
* Search through the linked list for the header. If it's found return 0 * Search for the header. If it's found return 0
* else return -1. * else return -1.
*/ */
int int
anonymous_search(char *s) anonymous_search(char *s)
{ {
struct anonymous_header_s *ptr = anonymous_root;
assert(s != NULL); assert(s != NULL);
assert(anonymous_root != NULL); assert(anonymous_map != NULL);
while (ptr) { return (hashmap_search(anonymous_map, s, NULL) > 0) ? 0 : -1;
if (ptr->header) {
if (strcasecmp(ptr->header, s) == 0)
return 0;
} else
return -1;
ptr = ptr->next;
}
return -1;
} }
/* /*
* Insert a new header into the linked list. * Insert a new header.
* *
* Return -1 if there is an error, 0 if the string already exists, and 1 if * Return -1 if there is an error, 0 if the string already exists, and 1 if
* it's been inserted. * it's been inserted.
@ -72,39 +53,23 @@ anonymous_search(char *s)
int int
anonymous_insert(char *s) anonymous_insert(char *s)
{ {
struct anonymous_header_s *ptr; int len;
struct anonymous_header_s **prev_ptr; char data = 1;
assert(s != NULL); assert(s != NULL);
if (!anonymous_root) { if (!anonymous_map) {
anonymous_root = safemalloc(sizeof(struct anonymous_header_s)); anonymous_map = hashmap_create(32);
if (!anonymous_root) if (!anonymous_map)
return -1; return -1;
}
anonymous_root->header = strdup(s); if (hashmap_search(anonymous_map, s, NULL) > 0) {
anonymous_root->next = NULL; /* The key was already found, so return a positive number. */
} else {
ptr = anonymous_root;
while (ptr) {
if (ptr->header) {
if (strcasecmp(ptr->header, s) == 0)
return 0; return 0;
} }
prev_ptr = &ptr; /* Insert the new key */
ptr = ptr->next; len = hashmap_insert(anonymous_map, s, &data, sizeof(data));
} return (len == 0) ? 1 : len;
ptr = (*prev_ptr)->next
= safemalloc(sizeof(struct anonymous_header_s));
if (!ptr)
return -1;
ptr->header = strdup(s);
ptr->next = NULL;
}
return 1;
} }