From 540069551da7a9c56874e568c449cfa3d72e4af4 Mon Sep 17 00:00:00 2001 From: Greg Date: Tue, 27 Dec 2016 12:18:16 -0500 Subject: [PATCH 1/2] Change signature for lookup_variable to take map instead of connptr --- src/html-error.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/html-error.c b/src/html-error.c index 972d265..640ac53 100644 --- a/src/html-error.c +++ b/src/html-error.c @@ -86,18 +86,18 @@ static char *get_html_file (unsigned int errornum) /* * Look up the value for a variable. */ -static char *lookup_variable (struct conn_s *connptr, const char *varname) +static char *lookup_variable (hashmap_t map, const char *varname) { hashmap_iter result_iter; char *key; char *data; - result_iter = hashmap_find (connptr->error_variables, varname); + result_iter = hashmap_find (map, varname); - if (hashmap_is_end (connptr->error_variables, result_iter)) + if (hashmap_is_end (map, result_iter)) return (NULL); - if (hashmap_return_entry (connptr->error_variables, result_iter, + if (hashmap_return_entry (map, result_iter, &key, (void **) &data) < 0) return (NULL); @@ -126,7 +126,7 @@ send_html_file (FILE *infile, struct conn_s *connptr) if (in_variable) { *p = '\0'; varval = (const char *) - lookup_variable (connptr, + lookup_variable (connptr->error_variables, varstart); if (!varval) varval = "(unknown)"; @@ -212,7 +212,7 @@ int send_http_error_message (struct conn_s *connptr) error_file = get_html_file (connptr->error_number); if (!(infile = fopen (error_file, "r"))) { - char *detail = lookup_variable (connptr, "detail"); + char *detail = lookup_variable (connptr->error_variables, "detail"); return (write_message (connptr->client_fd, fallback_error, connptr->error_number, connptr->error_string, From fdb1aae3a98d94da90b2ee36bd5ae4423e3b213b Mon Sep 17 00:00:00 2001 From: Greg Date: Tue, 27 Dec 2016 12:23:51 -0500 Subject: [PATCH 2/2] Move lookup_variable into hashmap.c / hashmap.h --- src/hashmap.c | 21 +++++++++++++++++++++ src/hashmap.h | 5 +++++ src/html-error.c | 21 --------------------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/hashmap.c b/src/hashmap.c index b99f838..7793d08 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -493,3 +493,24 @@ ssize_t hashmap_remove (hashmap_t map, const char *key) /* The key was not found, so return 0 */ return deleted; } + +/* + * Look up the value for a variable. + */ +char *lookup_variable (hashmap_t map, const char *varname) +{ + hashmap_iter result_iter; + char *key; + char *data; + + result_iter = hashmap_find (map, varname); + + if (hashmap_is_end (map, result_iter)) + return (NULL); + + if (hashmap_return_entry (map, result_iter, + &key, (void **) &data) < 0) + return (NULL); + + return (data); +} diff --git a/src/hashmap.h b/src/hashmap.h index e8fa819..9206737 100644 --- a/src/hashmap.h +++ b/src/hashmap.h @@ -117,4 +117,9 @@ extern ssize_t hashmap_search (hashmap_t map, const char *key); */ extern ssize_t hashmap_remove (hashmap_t map, const char *key); +/* + * Look up the value for a variable. + */ +extern char *lookup_variable (hashmap_t map, const char *varname); + #endif /* _HASHMAP_H */ diff --git a/src/html-error.c b/src/html-error.c index 640ac53..38adf85 100644 --- a/src/html-error.c +++ b/src/html-error.c @@ -83,27 +83,6 @@ static char *get_html_file (unsigned int errornum) return (val); } -/* - * Look up the value for a variable. - */ -static char *lookup_variable (hashmap_t map, const char *varname) -{ - hashmap_iter result_iter; - char *key; - char *data; - - result_iter = hashmap_find (map, varname); - - if (hashmap_is_end (map, result_iter)) - return (NULL); - - if (hashmap_return_entry (map, result_iter, - &key, (void **) &data) < 0) - return (NULL); - - return (data); -} - /* * Send an already-opened file to the client with variable substitution. */