From e929e81a55241b63d8921c071806195eae91ab91 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Wed, 16 Sep 2020 01:25:59 +0100 Subject: [PATCH] add_header: use sblist note that the old code inserted added headers at the beginning of the list, reasoning unknown. this seems counter-intuitive as the headers would end up in the request in the reverse order they were added, but this was irrelevant, as the headers were originally first put into the hashmap hashofheaders before sending it to the client. since the hashmap didn't preserve ordering, the headers would appear in random order anyway. --- src/conf.c | 26 ++++++++++++-------------- src/conf.h | 2 +- src/reqs.c | 8 ++++---- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/conf.c b/src/conf.c index 20d60d6..b29c1fb 100644 --- a/src/conf.c +++ b/src/conf.c @@ -271,19 +271,20 @@ struct { const unsigned int ndirectives = sizeof (directives) / sizeof (directives[0]); static void -free_added_headers (vector_t add_headers) +free_added_headers (sblist* add_headers) { - ssize_t i; + size_t i; - for (i = 0; i < vector_length (add_headers); i++) { - http_header_t *header = (http_header_t *) - vector_getentry (add_headers, i, NULL); + if (!add_headers) return; + + for (i = 0; i < sblist_getsize (add_headers); i++) { + http_header_t *header = sblist_get (add_headers, i); safefree (header->name); safefree (header->value); } - vector_delete (add_headers); + sblist_free (add_headers); } void free_config (struct config_s *conf) @@ -848,19 +849,16 @@ static HANDLE_FUNC (handle_addheader) { char *name = get_string_arg (line, &match[2]); char *value = get_string_arg (line, &match[3]); - http_header_t *header; + http_header_t header; if (!conf->add_headers) { - conf->add_headers = vector_create (); + conf->add_headers = sblist_new (sizeof(http_header_t), 16); } - header = (http_header_t *) safemalloc (sizeof (http_header_t)); - header->name = name; - header->value = value; + header.name = name; + header.value = value; - vector_prepend (conf->add_headers, header, sizeof *header); - - safefree (header); + sblist_add (conf->add_headers, &header); /* Don't free name or value here, as they are referenced in the * struct inserted into the vector. */ diff --git a/src/conf.h b/src/conf.h index 128d0d2..3ed774b 100644 --- a/src/conf.h +++ b/src/conf.h @@ -110,7 +110,7 @@ struct config_s { /* * Extra headers to be added to outgoing HTTP requests. */ - vector_t add_headers; + sblist* add_headers; }; extern int reload_config_file (const char *config_fname, struct config_s *conf); diff --git a/src/reqs.c b/src/reqs.c index 6bb456b..5f26333 100644 --- a/src/reqs.c +++ b/src/reqs.c @@ -1511,7 +1511,7 @@ void handle_connection (struct conn_s *connptr, union sockaddr_union* addr) while(0) int got_headers = 0, fd = connptr->client_fd; - ssize_t i; + size_t i; struct request_s *request = NULL; struct timeval tv; orderedmap hashofheaders = NULL; @@ -1648,9 +1648,9 @@ e401: * Add any user-specified headers (AddHeader directive) to the * outgoing HTTP request. */ - for (i = 0; i < vector_length (config->add_headers); i++) { - http_header_t *header = (http_header_t *) - vector_getentry (config->add_headers, i, NULL); + if (config->add_headers) + for (i = 0; i < sblist_getsize (config->add_headers); i++) { + http_header_t *header = sblist_get (config->add_headers, i); orderedmap_append (hashofheaders, header->name, header->value); }