Replaced the internally used linked list with a call to the hashmap
module. Code reuse is a good thing.
This commit is contained in:
parent
607e4c4c6c
commit
7409281e34
@ -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
|
||||
* the anonymous feature is turned on. The headers are stored in a linked
|
||||
* list.
|
||||
* the anonymous feature is turned on.
|
||||
*
|
||||
* Copyright (C) 2000 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
@ -20,51 +19,33 @@
|
||||
#include "tinyproxy.h"
|
||||
|
||||
#include "anonymous.h"
|
||||
#include "hashmap.h"
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
|
||||
/*
|
||||
* Structure holding the various anonymous headers.
|
||||
*/
|
||||
struct anonymous_header_s {
|
||||
char *header;
|
||||
struct anonymous_header_s *next;
|
||||
};
|
||||
struct anonymous_header_s *anonymous_root = NULL;
|
||||
static hashmap_t anonymous_map = NULL;
|
||||
|
||||
inline short int
|
||||
short int
|
||||
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.
|
||||
*/
|
||||
int
|
||||
anonymous_search(char *s)
|
||||
{
|
||||
struct anonymous_header_s *ptr = anonymous_root;
|
||||
|
||||
assert(s != NULL);
|
||||
assert(anonymous_root != NULL);
|
||||
assert(anonymous_map != NULL);
|
||||
|
||||
while (ptr) {
|
||||
if (ptr->header) {
|
||||
if (strcasecmp(ptr->header, s) == 0)
|
||||
return 0;
|
||||
} else
|
||||
return -1;
|
||||
|
||||
ptr = ptr->next;
|
||||
}
|
||||
|
||||
return -1;
|
||||
return (hashmap_search(anonymous_map, s, NULL) > 0) ? 0 : -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
|
||||
* it's been inserted.
|
||||
@ -72,39 +53,23 @@ anonymous_search(char *s)
|
||||
int
|
||||
anonymous_insert(char *s)
|
||||
{
|
||||
struct anonymous_header_s *ptr;
|
||||
struct anonymous_header_s **prev_ptr;
|
||||
int len;
|
||||
char data = 1;
|
||||
|
||||
assert(s != NULL);
|
||||
|
||||
if (!anonymous_root) {
|
||||
anonymous_root = safemalloc(sizeof(struct anonymous_header_s));
|
||||
if (!anonymous_root)
|
||||
if (!anonymous_map) {
|
||||
anonymous_map = hashmap_create(32);
|
||||
if (!anonymous_map)
|
||||
return -1;
|
||||
|
||||
anonymous_root->header = strdup(s);
|
||||
anonymous_root->next = NULL;
|
||||
} else {
|
||||
ptr = anonymous_root;
|
||||
|
||||
while (ptr) {
|
||||
if (ptr->header) {
|
||||
if (strcasecmp(ptr->header, s) == 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
prev_ptr = &ptr;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
|
||||
ptr = (*prev_ptr)->next
|
||||
= safemalloc(sizeof(struct anonymous_header_s));
|
||||
if (!ptr)
|
||||
return -1;
|
||||
|
||||
ptr->header = strdup(s);
|
||||
ptr->next = NULL;
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
if (hashmap_search(anonymous_map, s, NULL) > 0) {
|
||||
/* The key was already found, so return a positive number. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Insert the new key */
|
||||
len = hashmap_insert(anonymous_map, s, &data, sizeof(data));
|
||||
return (len == 0) ? 1 : len;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user