summaryrefslogtreecommitdiff
path: root/src/http
diff options
context:
space:
mode:
authorMichał Cichoń <michcic@gmail.com>2017-05-18 18:25:59 +0200
committerMichał Cichoń <michcic@gmail.com>2017-05-18 18:25:59 +0200
commitcd5c65097ecf03480f477e47e0bb706dbf705a21 (patch)
tree21f4419d9dceae3226286585b8b5ca87ea1934ce /src/http
parent6dee813dad2dd0b741025be046dfe3c7019bddb9 (diff)
parent593b2d6f18eb21309b82f7cad4a56f7bb1218180 (diff)
downloadpianobar-windows-cd5c65097ecf03480f477e47e0bb706dbf705a21.tar.gz
pianobar-windows-cd5c65097ecf03480f477e47e0bb706dbf705a21.tar.bz2
pianobar-windows-cd5c65097ecf03480f477e47e0bb706dbf705a21.zip
Merge branch 'release/2017.05.18'2017.05.18
Diffstat (limited to 'src/http')
-rw-r--r--src/http/http.c49
-rw-r--r--src/http/http.h5
2 files changed, 49 insertions, 5 deletions
diff --git a/src/http/http.c b/src/http/http.c
index dd64ad1..4576d04 100644
--- a/src/http/http.c
+++ b/src/http/http.c
@@ -44,6 +44,7 @@ static wchar_t* HttpToWideString(const char* string, int size);
static bool HttpCreateConnection (http_t http);
static void HttpCloseConnection (http_t http);
static void HttpSetLastError (http_t http, const char* message);
+static void HttpSetLastErrorW (http_t http, const wchar_t* message);
static void HttpSetLastErrorFromWinHttp (http_t http);
static char* HttpFormatWinApiError (DWORD errorCode, HINSTANCE module);
static char* HttpFormatWinHttpError (DWORD errorCode);
@@ -118,11 +119,19 @@ static void HttpCloseConnection (http_t http) {
static void HttpSetLastError (http_t http, const char* message) {
free(http->error);
http->error = NULL;
-
+
if (message)
http->error = strdup(message);
}
+static void HttpSetLastErrorW (http_t http, const wchar_t* message) {
+ free(http->error);
+ http->error = NULL;
+
+ if (message)
+ http->error = HttpToString(message, wcslen(message));
+}
+
static void HttpSetLastErrorFromWinHttp (http_t http) {
free(http->error);
http->error = NULL;
@@ -241,6 +250,32 @@ bool HttpSetAutoProxy (http_t http, const char* url) {
return false;
}
+static void HttpUrlDecodeInplace (wchar_t* url)
+{
+ wchar_t* input = url;
+ wchar_t* output = url;
+ size_t size = wcslen (url);
+ while (size > 0) {
+ if (input[0] == '%' && iswxdigit(input[1]) && iswxdigit(input[2])) {
+ wchar_t hex[3];
+ hex[0] = input[1];
+ hex[1] = input[2];
+ hex[2] = 0;
+ *output++ = (wchar_t)(wcstol (hex, NULL, 16));
+ input += 3;
+ size -= 3;
+ }
+ else {
+ *output++ = *input++;
+ --size;
+ }
+ }
+
+ if (output < input) {
+ *output = '\0';
+ }
+}
+
bool HttpSetProxy (http_t http, const char* url) {
URL_COMPONENTS urlComponents;
wchar_t* wideUrl = NULL;
@@ -258,10 +293,12 @@ bool HttpSetProxy (http_t http, const char* url) {
if (urlComponents.lpszUserName && urlComponents.dwUserNameLength > 0) {
wideUsername = wcsdup(urlComponents.lpszUserName);
wideUsername[urlComponents.dwUserNameLength] = 0;
+ HttpUrlDecodeInplace (wideUsername);
}
if (urlComponents.lpszPassword && urlComponents.dwPasswordLength > 0) {
widePassword = wcsdup(urlComponents.lpszPassword);
widePassword[urlComponents.dwPasswordLength] = 0;
+ HttpUrlDecodeInplace (widePassword);
}
}
@@ -396,6 +433,13 @@ bool HttpRequest(http_t http, PianoRequest_t * const request) {
}
if (succeeded && statusCode == 407) {
+ wchar_t statusText[256] = { 0 };
+ DWORD statusTextSize = sizeof(statusText) - 1;
+ WinHttpQueryHeaders(handle,
+ WINHTTP_QUERY_STATUS_TEXT,
+ WINHTTP_HEADER_NAME_BY_INDEX,
+ statusText, &statusTextSize, WINHTTP_NO_HEADER_INDEX);
+ HttpSetLastErrorW (http, statusText);
requestSent = false;
retry = true;
}
@@ -467,6 +511,9 @@ bool HttpRequest(http_t http, PianoRequest_t * const request) {
HttpSetLastError (http, "Maximum retries count exceeded");
}
+ if (retryLimit == 0)
+ goto done;
+
complete = true;
HttpSetLastError (http, NULL);
diff --git a/src/http/http.h b/src/http/http.h
index a321cd3..5ea617f 100644
--- a/src/http/http.h
+++ b/src/http/http.h
@@ -21,8 +21,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
-#ifndef SRC_HTTP_H_CN979RE9
-#define SRC_HTTP_H_CN979RE9
+#pragma once
#include "config.h"
@@ -42,5 +41,3 @@ bool HttpSetProxy(http_t, const char*);
bool HttpRequest (http_t, PianoRequest_t * const);
const char* HttpGetError (http_t);
-#endif /* SRC_HTTP_H_CN979RE9 */
-