From f0af48a4f70dfed1372112f2247a4f3bbb10cf72 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sun, 12 Jun 2011 13:52:21 +0200 Subject: waitress: http "basic" authentication support Closes #109 --- src/libwaitress/waitress.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/libwaitress/waitress.c b/src/libwaitress/waitress.c index 3c48616..8bef2f6 100644 --- a/src/libwaitress/waitress.c +++ b/src/libwaitress/waitress.c @@ -466,6 +466,27 @@ static WaitressReturn_t WaitressPollRead (int sockfd, char *buf, size_t count, CLOSE_RET (wRet); \ } +/* send basic http authorization + * @param waitress handle + * @param url containing user/password + * @param header name prefix + */ +static bool WaitressFormatAuthorization (WaitressHandle_t *waith, + WaitressUrl_t *url, const char *prefix, char *writeBuf, + const size_t writeBufSize) { + if (url->user != NULL) { + char userPass[1024], *encodedUserPass; + snprintf (userPass, sizeof (userPass), "%s:%s", url->user, + (url->password != NULL) ? url->password : ""); + encodedUserPass = WaitressBase64Encode (userPass); + snprintf (writeBuf, writeBufSize, "%sAuthorization: Basic %s\r\n", + prefix, encodedUserPass); + free (encodedUserPass); + return true; + } + return false; +} + /* get default http port if none was given */ static const char *WaitressDefaultPort (WaitressUrl_t *url) { @@ -576,6 +597,16 @@ WaitressReturn_t WaitressFetchCall (WaitressHandle_t *waith) { strlen (waith->postData)); WRITE_RET (writeBuf, strlen (writeBuf)); } + + /* write authorization headers */ + if (WaitressFormatAuthorization (waith, &waith->url, "", writeBuf, + sizeof (writeBuf))) { + WRITE_RET (writeBuf, strlen (writeBuf)); + } + if (WaitressFormatAuthorization (waith, &waith->proxy, "Proxy-", + writeBuf, sizeof (writeBuf))) { + WRITE_RET (writeBuf, strlen (writeBuf)); + } if (waith->extraHeaders != NULL) { WRITE_RET (waith->extraHeaders, strlen (waith->extraHeaders)); -- cgit v1.2.3