From 53341e25fe70ee2242835452580037808d8419c5 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Mon, 27 May 2019 17:55:56 +0300 Subject: [PATCH] lib-http: Add http_client_request_lookup_header() --- src/lib-http/http-client-request.c | 14 ++++++++++++++ src/lib-http/http-client.h | 3 +++ src/lib-http/test-http-client-request.c | 16 ++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/lib-http/http-client-request.c b/src/lib-http/http-client-request.c index 7c2f2dd422..a38a91795d 100644 --- a/src/lib-http/http-client-request.c +++ b/src/lib-http/http-client-request.c @@ -496,6 +496,20 @@ void http_client_request_remove_header(struct http_client_request *req, str_delete(req->headers, key_pos, next_pos - key_pos); } +const char *http_client_request_lookup_header(struct http_client_request *req, + const char *key) +{ + size_t key_pos, value_pos, next_pos; + + if (!http_client_request_lookup_header_pos(req, key, &key_pos, + &value_pos, &next_pos)) + return NULL; + + /* don't return CRLF */ + return t_strndup(str_data(req->headers) + value_pos, + next_pos - value_pos - 2); +} + void http_client_request_set_date(struct http_client_request *req, time_t date) { diff --git a/src/lib-http/http-client.h b/src/lib-http/http-client.h index b246e0e3f1..1d5fa52ad8 100644 --- a/src/lib-http/http-client.h +++ b/src/lib-http/http-client.h @@ -301,6 +301,9 @@ void http_client_request_add_header(struct http_client_request *req, headers. */ void http_client_request_remove_header(struct http_client_request *req, const char *key); +/* lookup the value for a header added earlier. Returns NULL if not found. */ +const char *http_client_request_lookup_header(struct http_client_request *req, + const char *key); /* set the value of the "Date" header for the request using a time_t value. Use this instead of setting it directly using diff --git a/src/lib-http/test-http-client-request.c b/src/lib-http/test-http-client-request.c index 92b1811385..344e0ad10f 100644 --- a/src/lib-http/test-http-client-request.c +++ b/src/lib-http/test-http-client-request.c @@ -23,39 +23,55 @@ static void test_http_client_request_headers(void) req = http_client_request(client, "GET", "host", "target", test_http_client_request_callback, NULL); + test_assert(http_client_request_lookup_header(req, "qwe") == NULL); + /* add the first */ http_client_request_add_header(req, "qwe", "value1"); + test_assert_strcmp(http_client_request_lookup_header(req, "qwe"), "value1"); test_assert_strcmp(str_c(req->headers), "qwe: value1\r\n"); /* replace the first with the same length */ http_client_request_add_header(req, "qwe", "234567"); + test_assert_strcmp(http_client_request_lookup_header(req, "qwe"), "234567"); test_assert_strcmp(str_c(req->headers), "qwe: 234567\r\n"); /* replace the first with smaller length */ http_client_request_add_header(req, "qwe", "xyz"); + test_assert_strcmp(http_client_request_lookup_header(req, "qwe"), "xyz"); test_assert_strcmp(str_c(req->headers), "qwe: xyz\r\n"); /* replace the first with longer length */ http_client_request_add_header(req, "qwe", "abcdefg"); + test_assert_strcmp(http_client_request_lookup_header(req, "qwe"), "abcdefg"); test_assert_strcmp(str_c(req->headers), "qwe: abcdefg\r\n"); /* add the second */ http_client_request_add_header(req, "xyz", "1234"); + test_assert_strcmp(http_client_request_lookup_header(req, "qwe"), "abcdefg"); + test_assert_strcmp(http_client_request_lookup_header(req, "xyz"), "1234"); test_assert_strcmp(str_c(req->headers), "qwe: abcdefg\r\nxyz: 1234\r\n"); /* replace second */ http_client_request_add_header(req, "xyz", "yuiop"); + test_assert_strcmp(http_client_request_lookup_header(req, "qwe"), "abcdefg"); + test_assert_strcmp(http_client_request_lookup_header(req, "xyz"), "yuiop"); test_assert_strcmp(str_c(req->headers), "qwe: abcdefg\r\nxyz: yuiop\r\n"); /* replace the first again */ http_client_request_add_header(req, "qwe", "1234"); + test_assert_strcmp(http_client_request_lookup_header(req, "qwe"), "1234"); + test_assert_strcmp(http_client_request_lookup_header(req, "xyz"), "yuiop"); test_assert_strcmp(str_c(req->headers), "qwe: 1234\r\nxyz: yuiop\r\n"); /* remove the headers */ http_client_request_remove_header(req, "qwe"); + test_assert(http_client_request_lookup_header(req, "qwe") == NULL); + test_assert_strcmp(http_client_request_lookup_header(req, "xyz"), "yuiop"); test_assert_strcmp(str_c(req->headers), "xyz: yuiop\r\n"); http_client_request_remove_header(req, "xyz"); + test_assert(http_client_request_lookup_header(req, "qwe") == NULL); + test_assert(http_client_request_lookup_header(req, "xyz") == NULL); test_assert_strcmp(str_c(req->headers), ""); http_client_request_abort(&req); -- 2.47.3