From 4d7166d432e2a70a381c580168cfcb6cebe3cd09 Mon Sep 17 00:00:00 2001 From: rhaas Date: Sun, 8 Jul 2012 17:33:35 +0000 Subject: use C++ map instead of Cactus hash to store page contents this is because the hash size can get out of hand (GBs) git-svn-id: http://svn.cactuscode.org/arrangements/CactusConnect/HTTPD/trunk@251 1faa4e14-9dd3-4be0-9f0e-ffe519881164 --- src/Authorisation.c | 32 ++++++++++++++++---------------- src/Map.cc | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Server.c | 18 +++++++++--------- src/http.c | 26 +++++++++++++------------- src/httpd_Map.h | 24 ++++++++++++++++++++++++ src/make.code.defn | 2 +- 6 files changed, 112 insertions(+), 39 deletions(-) create mode 100644 src/Map.cc create mode 100644 src/httpd_Map.h diff --git a/src/Authorisation.c b/src/Authorisation.c index b3e048d..3cde03d 100644 --- a/src/Authorisation.c +++ b/src/Authorisation.c @@ -22,7 +22,7 @@ #include #endif -#include "util_Hash.h" +#include "httpd_Map.h" #include "util_String.h" #include "httpRequest.h" @@ -49,7 +49,7 @@ struct httpUserData ********************* Local Routine Prototypes ********************* ********************************************************************/ -static int AddUser(uHash *database, +static int AddUser(uMap database, const char *name, const char *password, const char *encryption_scheme); @@ -66,7 +66,7 @@ static int VerifyPassword(const char *database, ********************* Local Data ***************************** ********************************************************************/ -static uHash *AuthDatabase = NULL; +static uMap AuthDatabase = NULL; #define INITIAL_SIZE 32 #define DECODED_SIZE 100 @@ -95,26 +95,26 @@ int HTTP_AuthAddUser(const char *database, const char *encryption_scheme) { int retcode = -1; - uHash *this_database = NULL; + uMap this_database = NULL; /* Create the master database if necessary */ if(!AuthDatabase) { - AuthDatabase = Util_HashCreate(INITIAL_SIZE); + AuthDatabase = Httpd_MapCreate(); } if(AuthDatabase) { /* Does this database exist ? */ - this_database = (uHash *)Util_HashData(AuthDatabase, strlen(database), database, 0); + this_database = (uMap)Httpd_MapData(AuthDatabase, strlen(database), database); if(!this_database) { - this_database = Util_HashCreate(INITIAL_SIZE); + this_database = Httpd_MapCreate(); if(this_database) { - Util_HashStore(AuthDatabase, strlen(database), database, 0, (void *)this_database); + Httpd_MapStore(AuthDatabase, strlen(database), database, (void *)this_database); } else { @@ -254,7 +254,7 @@ int HTTP_AuthenticateBasic(httpRequest *request, @endhistory @@*/ -static int AddUser(uHash *database, +static int AddUser(uMap database, const char *name, const char *password, const char *encryption_scheme) @@ -262,8 +262,8 @@ static int AddUser(uHash *database, int retcode = -1; /* Does this user already exist ? */ - struct httpUserData * this_user = (struct httpUserData *)Util_HashData( - database, strlen(name), name, 0); + struct httpUserData * this_user = (struct httpUserData *)Httpd_MapData( + database, strlen(name), name); if(!this_user) { @@ -276,7 +276,7 @@ static int AddUser(uHash *database, this_user->password = Util_Strdup(password); this_user->encryption_scheme = Util_Strdup(encryption_scheme); - retcode = Util_HashStore(database, strlen(name), name, 0, (void *)this_user); + retcode = Httpd_MapStore(database, strlen(name), name, (void *)this_user); } } else @@ -317,13 +317,13 @@ static int VerifyPassword(const char *database, if(AuthDatabase) { /* Does this database exist ? */ - uHash *this_database = (uHash *)Util_HashData(AuthDatabase, - strlen(database), database, 0); + uMap this_database = (uMap)Httpd_MapData(AuthDatabase, + strlen(database), database); if(this_database) { - struct httpUserData *data = (struct httpUserData *) Util_HashData( - this_database, strlen(user), user, 0); + struct httpUserData *data = (struct httpUserData *) Httpd_MapData( + this_database, strlen(user), user); if(data) { diff --git a/src/Map.cc b/src/Map.cc new file mode 100644 index 0000000..c4d6070 --- /dev/null +++ b/src/Map.cc @@ -0,0 +1,49 @@ +#include +#include + +#include "httpd_Map.h" + +typedef std::map iMap; + +extern "C" { + +uMap Httpd_MapCreate(void) +{ + return new iMap; +} + +void * Httpd_MapData(uMap map, size_t keylen, const char * key) +{ + iMap& imap(*static_cast(map)); + const std::string ikey(key, keylen); + iMap::iterator it(imap.find(ikey)); + + if(it != imap.end()) + return it->second; + else + return NULL; +} + +int Httpd_MapStore(uMap map, size_t keylen, const char * key, void * data) +{ + iMap& imap(*static_cast(map)); + const std::string ikey(key, keylen); + iMap::iterator it(imap.find(ikey)); + + if(it != imap.end()) + it->second = data; + else + imap[ikey] = data; + + return 0; +} + +void Httpd_MapDestroy(uMap map, void (*destroy)(void *)) +{ + iMap& imap(*static_cast(map)); + for(iMap::iterator it = imap.begin() ; it != imap.end() ; ++it) + destroy(it->second); + delete &imap; +} + +} diff --git a/src/Server.c b/src/Server.c index 5959c9c..0aee562 100644 --- a/src/Server.c +++ b/src/Server.c @@ -15,7 +15,7 @@ #include "cctk_Parameters.h" -#include "util_Hash.h" +#include "httpd_Map.h" #include "util_String.h" #include "httpd.h" @@ -61,7 +61,7 @@ static double evaluator(const char *name, void *data); ********************* Local Data ***************************** ********************************************************************/ -static uHash *pages = NULL; +static uMap pages = NULL; static const char *notfound_page = "\n\nError 404: Not Found\n\n" @@ -164,10 +164,10 @@ int HTTP_RegisterPage(const char *path, int (*function)(const cGH *, httpRequest /* Create the hash table if it's not already been created */ if(! pages) { - pages = Util_HashCreate(INITIAL_SIZE); + pages = Httpd_MapCreate(); } - if(Util_HashData(pages, strlen(path), path, 0)) + if(Httpd_MapData(pages, strlen(path), path)) { CCTK_VWarn(1, __LINE__,__FILE__,CCTK_THORNSTRING, "Page exists already:\n\"%s\"", path); @@ -178,7 +178,7 @@ int HTTP_RegisterPage(const char *path, int (*function)(const cGH *, httpRequest if(pagedata) { - retval = Util_HashStore(pages, strlen(path), path, 0, (void *)pagedata); + retval = Httpd_MapStore(pages, strlen(path), path, (void *)pagedata); } else { @@ -355,9 +355,9 @@ static httpPage *FindPage(const char *path, const char **residual) SetToCString( temp, path); ConcatCString( temp,"index.html"); - pagedata = Util_HashData(pages, Length(temp), GetBuffer(temp), 0); + pagedata = Httpd_MapData(pages, Length(temp), GetBuffer(temp)); } - else if((pagedata = Util_HashData(pages, strlen(path), path, 0))) + else if((pagedata = Httpd_MapData(pages, strlen(path), path))) { /* Or exact path */ } @@ -369,7 +369,7 @@ static httpPage *FindPage(const char *path, const char **residual) SetToCString( temp, path); ConcatCString( temp,"/index.html"); - pagedata = Util_HashData(pages, Length(temp), GetBuffer(temp), 0); + pagedata = Httpd_MapData(pages, Length(temp), GetBuffer(temp)); } *residual = NULL; @@ -385,7 +385,7 @@ static httpPage *FindPage(const char *path, const char **residual) #ifdef HTTP_DEBUG printf("Looking for '%s' less '%s' \n", path, position); #endif - if((pagedata = Util_HashData(pages, position-path, path, 0))) + if((pagedata = Httpd_MapData(pages, position-path, path))) { *residual = position+1; break; diff --git a/src/http.c b/src/http.c index 91fffa0..2f08209 100644 --- a/src/http.c +++ b/src/http.c @@ -32,7 +32,7 @@ #include "util_String.h" #include "httpd.h" -#include "util_Hash.h" +#include "httpd_Map.h" #include "SString_Namespace.h" static const char *rcsid = "$Header$"; @@ -78,12 +78,12 @@ typedef struct httpRequestTag void *connection; /* The request header lines */ - uHash *headers; + uMap headers; /* Stuff for arguments */ /* First a hash table to look the data up quickly */ - uHash *arguments; + uMap arguments; /* Now a linked list to allow walking. */ httpArg *firstarg; @@ -327,7 +327,7 @@ const char *HTTP_ArgumentValue(const httpRequest *request, const char *arg) { if(request->arguments) { - const httpArg *value = (httpArg *)Util_HashData(request->arguments, strlen(arg), arg, 0); + const httpArg *value = (httpArg *)Httpd_MapData(request->arguments, strlen(arg), arg); if(value) { @@ -386,7 +386,7 @@ const char *HTTP_HeaderValue(const httpRequest *request, const char *header) if(request->headers) { - header_line = Util_HashData(request->headers, strlen(header), header, 0); + header_line = Httpd_MapData(request->headers, strlen(header), header); if(header_line) { @@ -637,13 +637,13 @@ static int AddHeader(httpRequest *request, const char *line) if(!request->headers) { /* Need to create the hash table */ - request->headers = Util_HashCreate(INITIAL_SIZE); + request->headers = Httpd_MapCreate(); } if(request->headers) { /* Does the line already exist ? */ - header_line = (struct httpHeader *)Util_HashData(request->headers, keylength, line, 0); + header_line = (struct httpHeader *)Httpd_MapData(request->headers, keylength, line); if(header_line) { @@ -672,7 +672,7 @@ static int AddHeader(httpRequest *request, const char *line) { strcpy(header_line->line, value); - Util_HashStore(request->headers, keylength, line, 0, (void *)header_line); + Httpd_MapStore(request->headers, keylength, line, (void *)header_line); } } } @@ -708,7 +708,7 @@ static int StripArgs(httpRequest *request, char *request_uri) *position = 0; /* Create the hash table */ - request->arguments = Util_HashCreate(INITIAL_SIZE); + request->arguments = Httpd_MapCreate(); /* Parse the argument list */ position++; @@ -743,7 +743,7 @@ static int StripArgs(httpRequest *request, char *request_uri) argument->arg = Util_Strdup(token); argument->value = Util_Strdup(value); - if((stored = (httpArg *)Util_HashData(request->arguments, strlen(token), token, 0))) + if((stored = (httpArg *)Httpd_MapData(request->arguments, strlen(token), token))) { /* Argument already exists */ @@ -759,7 +759,7 @@ static int StripArgs(httpRequest *request, char *request_uri) } else { - Util_HashStore(request->arguments, strlen(token), token, 0, (void *)argument); + Httpd_MapStore(request->arguments, strlen(token), token, (void *)argument); request->n_arguments++; } /* Append to global list. */ @@ -857,12 +857,12 @@ static int ClearRequest(httpRequest *request) { if(request->arguments) { - Util_HashDestroy(request->arguments, (void (*)(void *))DestroyArgument); + Httpd_MapDestroy(request->arguments, (void (*)(void *))DestroyArgument); } if(request->headers) { - Util_HashDestroy(request->headers, (void (*)(void *))DestroyHeader); + Httpd_MapDestroy(request->headers, (void (*)(void *))DestroyHeader); } return 0; diff --git a/src/httpd_Map.h b/src/httpd_Map.h new file mode 100644 index 0000000..0cbac57 --- /dev/null +++ b/src/httpd_Map.h @@ -0,0 +1,24 @@ +#ifndef HTTPD_MAP_H_ +#define HTTPD_MAP_H_ 1 + +#include + +#if __cplusplus +extern "C" { +#define VOID +#else +#define VOID void +#endif + +typedef void * uMap; + +uMap Httpd_MapCreate(VOID); +void * Httpd_MapData(uMap map, size_t keylen, const char * key); +int Httpd_MapStore(uMap map, size_t keylen, const char * key, void * data); +void Httpd_MapDestroy(uMap map, void (*destroy)(void *)); + +#if __cplusplus +} +#endif + +#endif diff --git a/src/make.code.defn b/src/make.code.defn index 506d792..960ca5f 100644 --- a/src/make.code.defn +++ b/src/make.code.defn @@ -3,7 +3,7 @@ # Source files in this directory -MISC_SRC = Startup.c Expression.c +MISC_SRC = Startup.c Expression.c Map.cc SERVER_SRC = Server.c Sockets.c http.c -- cgit v1.2.3