dovecot-2.2: lib-http client: Implemented simple authentication ...

dovecot at dovecot.org dovecot at dovecot.org
Tue Aug 18 18:02:35 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/58694b53e730
changeset: 18960:58694b53e730
user:      Stephan Bosch <stephan at rename-it.nl>
date:      Tue Aug 18 20:39:24 2015 +0300
description:
lib-http client: Implemented simple authentication using Basic scheme.

diffstat:

 src/lib-http/http-client-private.h |   2 ++
 src/lib-http/http-client-request.c |  28 ++++++++++++++++++++++++++++
 src/lib-http/http-client.h         |   3 +++
 src/lib-http/test-http-client.c    |   8 ++++++++
 4 files changed, 41 insertions(+), 0 deletions(-)

diffs (116 lines):

diff -r 34f78bc1eed4 -r 58694b53e730 src/lib-http/http-client-private.h
--- a/src/lib-http/http-client-private.h	Tue Aug 18 20:39:24 2015 +0300
+++ b/src/lib-http/http-client-private.h	Tue Aug 18 20:39:24 2015 +0300
@@ -57,6 +57,7 @@
 
 	const char *method, *target;
 	struct http_url origin_url;
+	const char *username, *password;
 
 	const struct http_url *host_url;
 	const char *authority;
@@ -95,6 +96,7 @@
 
 	enum http_request_state state;
 
+	unsigned int have_hdr_authorization:1;
 	unsigned int have_hdr_body_spec:1;
 	unsigned int have_hdr_connection:1;
 	unsigned int have_hdr_date:1;
diff -r 34f78bc1eed4 -r 58694b53e730 src/lib-http/http-client-request.c
--- a/src/lib-http/http-client-request.c	Tue Aug 18 20:39:24 2015 +0300
+++ b/src/lib-http/http-client-request.c	Tue Aug 18 20:39:24 2015 +0300
@@ -12,6 +12,7 @@
 #include "dns-lookup.h"
 #include "http-url.h"
 #include "http-date.h"
+#include "http-auth.h"
 #include "http-response-parser.h"
 #include "http-transfer.h"
 
@@ -104,6 +105,11 @@
 	req = http_client_request_new(client, method, callback, context);
 	http_url_copy_authority(req->pool, &req->origin_url, target_url);
 	req->target = p_strdup(req->pool, http_url_create_target(target_url));
+	if (target_url->user != NULL && *target_url->user != '\0' &&
+		target_url->password != NULL) {
+		req->username = p_strdup(req->pool, target_url->user);
+		req->password = p_strdup(req->pool, target_url->password);
+	}
 	return req;
 }
 
@@ -226,6 +232,10 @@
 
 	/* mark presence of special headers */
 	switch (key[0]) {
+	case 'a': case 'A':
+		if (strcasecmp(key, "Authorization") == 0)
+			req->have_hdr_authorization = TRUE;
+		break;
 	case 'c': case 'C':
 		if (strcasecmp(key, "Connection") == 0)
 			req->have_hdr_connection = TRUE;
@@ -338,6 +348,13 @@
 	req->timeout_msecs = 0;
 }
 
+void http_client_request_set_auth_simple(struct http_client_request *req,
+	const char *username, const char *password)
+{
+	req->username = p_strdup(req->pool, username);
+	req->password = p_strdup(req->pool, password);
+}
+
 void http_client_request_delay_until(struct http_client_request *req,
 	time_t time)
 {
@@ -744,6 +761,17 @@
 		str_append(rtext, http_date_create(req->date));
 		str_append(rtext, "\r\n");
 	}
+	if (!req->have_hdr_authorization &&
+		req->username != NULL && req->password != NULL) {
+		struct http_auth_credentials auth_creds;
+
+		http_auth_basic_credentials_init(&auth_creds,
+			req->username, req->password);
+
+		str_append(rtext, "Authorization: ");
+		http_auth_create_credentials(rtext, &auth_creds);
+		str_append(rtext, "\r\n");
+	}
 	if (!req->have_hdr_user_agent && req->client->set.user_agent != NULL) {
 		str_printfa(rtext, "User-Agent: %s\r\n",
 			    req->client->set.user_agent);
diff -r 34f78bc1eed4 -r 58694b53e730 src/lib-http/http-client.h
--- a/src/lib-http/http-client.h	Tue Aug 18 20:39:24 2015 +0300
+++ b/src/lib-http/http-client.h	Tue Aug 18 20:39:24 2015 +0300
@@ -208,6 +208,9 @@
 void http_client_request_set_timeout(struct http_client_request *req,
 	const struct timeval *time);
 
+void http_client_request_set_auth_simple(struct http_client_request *req,
+	const char *username, const char *password);
+
 void http_client_request_delay_until(struct http_client_request *req,
 	time_t time);
 void http_client_request_delay(struct http_client_request *req,
diff -r 34f78bc1eed4 -r 58694b53e730 src/lib-http/test-http-client.c
--- a/src/lib-http/test-http-client.c	Tue Aug 18 20:39:24 2015 +0300
+++ b/src/lib-http/test-http-client.c	Tue Aug 18 20:39:24 2015 +0300
@@ -260,6 +260,14 @@
 
 	test_req = i_new(struct http_test_request, 1);
 	http_req = http_client_request(http_client,
+		"GET", "jigsaw.w3.org", "/HTTP/Basic/",
+		got_request_response, test_req);
+	http_client_request_set_auth_simple
+		(http_req, "guest", "guest");
+	http_client_request_submit(http_req);
+
+	test_req = i_new(struct http_test_request, 1);
+	http_req = http_client_request(http_client,
 		"PUT", "test.dovecot.org", "/http/put/put.php",
 		got_request_response, test_req);
 	post_payload = i_stream_create_file("Makefile.am", 10);


More information about the dovecot-cvs mailing list