]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-http: http-client: Requests now automatically generate a Date header.
authorStephan Bosch <stephan@rename-it.nl>
Sun, 15 Sep 2013 00:33:44 +0000 (03:33 +0300)
committerStephan Bosch <stephan@rename-it.nl>
Sun, 15 Sep 2013 00:33:44 +0000 (03:33 +0300)
The used date value is normally the submission time of the request, but it
can be set explicitly.

src/lib-http/http-client-private.h
src/lib-http/http-client-request.c
src/lib-http/http-client.h

index d80d09583e79d35bf4fff5586e8115b78dd840a1..23e942f38368ab40b5272623d6bdd8319ba9fcfc 100644 (file)
@@ -48,6 +48,8 @@ struct http_client_request {
        struct http_client_connection *conn;
 
        string_t *headers;
+       time_t date;
+
        struct istream *payload_input;
        uoff_t payload_size, payload_offset;
        struct ostream *payload_output;
index 09eb3e3da20934e935e65feb432ba76476dc138e..6bee4b66eedfbfce47370e155aa9c244a4b04c11 100644 (file)
@@ -8,6 +8,7 @@
 #include "istream.h"
 #include "ostream.h"
 #include "http-url.h"
+#include "http-date.h"
 #include "http-response-parser.h"
 #include "http-transfer.h"
 
@@ -72,6 +73,7 @@ http_client_request(struct http_client *client,
        req->callback = callback;
        req->context = context;
        req->headers = str_new(default_pool, 256);
+       req->date = (time_t)-1;
 
        req->state = HTTP_REQUEST_STATE_NEW;
        return req;
@@ -149,9 +151,20 @@ void http_client_request_add_header(struct http_client_request *req,
                                    const char *key, const char *value)
 {
        i_assert(req->state == HTTP_REQUEST_STATE_NEW);
+       /* don't allow setting Date header directly;
+          this is ignored for now for backwards compatibility */
+       if (strcasecmp(key, "Date") == 0)
+               return;
        str_printfa(req->headers, "%s: %s\r\n", key, value);
 }
 
+void http_client_request_set_date(struct http_client_request *req,
+                                   time_t date)
+{
+       i_assert(req->state == HTTP_REQUEST_STATE_NEW);
+       req->date = date;
+}
+
 void http_client_request_set_payload(struct http_client_request *req,
                                     struct istream *input, bool sync)
 {
@@ -188,6 +201,10 @@ static void http_client_request_do_submit(struct http_client_request *req)
        struct http_client_host *host;
 
        i_assert(req->state == HTTP_REQUEST_STATE_NEW);
+
+       /* use submission date if no date is set explicitly */
+       if (req->date == (time_t)-1)
+               req->date = ioloop_time;
        
        host = http_client_host_get(req->client, req->hostname);
        req->state = HTTP_REQUEST_STATE_QUEUED;
@@ -369,13 +386,14 @@ static int http_client_request_send_real(struct http_client_request *req,
        str_append(rtext, req->method);
        str_append(rtext, " ");
        str_append(rtext, req->target);
-       str_append(rtext, " HTTP/1.1\r\n");
-       str_append(rtext, "Host: ");
+       str_append(rtext, " HTTP/1.1\r\nHost: ");
        str_append(rtext, req->hostname);
        if ((!req->ssl &&req->port != HTTP_DEFAULT_PORT) ||
                (req->ssl && req->port != HTTPS_DEFAULT_PORT)) {
                str_printfa(rtext, ":%u", req->port);
        }
+       str_append(rtext, "\r\nDate: ");
+       str_append(rtext, http_date_create(req->date));
        str_append(rtext, "\r\n");
        if (req->payload_sync) {
                str_append(rtext, "Expect: 100-continue\r\n");
index 4ea566e500169b6572262ce2f6a17545ac673b44..627d141e73a4c45f9516d645a6e78aa971833cf2 100644 (file)
@@ -98,6 +98,9 @@ void http_client_request_set_urgent(struct http_client_request *req);
 
 void http_client_request_add_header(struct http_client_request *req,
                                    const char *key, const char *value);
+void http_client_request_set_date(struct http_client_request *req,
+                                   time_t date);
+
 void http_client_request_set_payload(struct http_client_request *req,
                                     struct istream *input, bool sync);