]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-http: Add http_client_request_lookup_header()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 27 May 2019 14:55:56 +0000 (17:55 +0300)
committerTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 27 May 2019 14:55:56 +0000 (17:55 +0300)
src/lib-http/http-client-request.c
src/lib-http/http-client.h
src/lib-http/test-http-client-request.c

index 7c2f2dd4229de3d27338beebff99b843049946d8..a38a91795d7f1849d75340734f609a0f132c849d 100644 (file)
@@ -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)
 {
index b246e0e3f18a74ddf7b4f1aa279ea178fa0ad500..1d5fa52ad8ee5cc2f811f8ff4697b4195931be3b 100644 (file)
@@ -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
index 92b1811385f20f178561d24477fba8a07569882c..344e0ad10fecd57dc1fa7f7510f8bacc219fb80e 100644 (file)
@@ -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);