dovecot-2.2: lib-http: http-client: Requests now automatically g...

dovecot at dovecot.org dovecot at dovecot.org
Sun Sep 15 03:38:25 EEST 2013


details:   http://hg.dovecot.org/dovecot-2.2/rev/e83f3d16a31d
changeset: 16737:e83f3d16a31d
user:      Stephan Bosch <stephan at rename-it.nl>
date:      Sun Sep 15 03:33:44 2013 +0300
description:
lib-http: http-client: Requests now automatically generate a Date header.
The used date value is normally the submission time of the request, but it
can be set explicitly.

diffstat:

 src/lib-http/http-client-private.h |   2 ++
 src/lib-http/http-client-request.c |  22 ++++++++++++++++++++--
 src/lib-http/http-client.h         |   3 +++
 3 files changed, 25 insertions(+), 2 deletions(-)

diffs (93 lines):

diff -r bb6506d2d084 -r e83f3d16a31d src/lib-http/http-client-private.h
--- a/src/lib-http/http-client-private.h	Sun Sep 15 03:33:04 2013 +0300
+++ b/src/lib-http/http-client-private.h	Sun Sep 15 03:33:44 2013 +0300
@@ -48,6 +48,8 @@
 	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;
diff -r bb6506d2d084 -r e83f3d16a31d src/lib-http/http-client-request.c
--- a/src/lib-http/http-client-request.c	Sun Sep 15 03:33:04 2013 +0300
+++ b/src/lib-http/http-client-request.c	Sun Sep 15 03:33:44 2013 +0300
@@ -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 @@
 	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 @@
 				    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 @@
 	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 @@
 	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");
diff -r bb6506d2d084 -r e83f3d16a31d src/lib-http/http-client.h
--- a/src/lib-http/http-client.h	Sun Sep 15 03:33:04 2013 +0300
+++ b/src/lib-http/http-client.h	Sun Sep 15 03:33:44 2013 +0300
@@ -98,6 +98,9 @@
 
 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);
 


More information about the dovecot-cvs mailing list