From 8a98bf3e30011fe57b66f1d8e8d8a48ee46c3509 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Mon, 12 Nov 2018 06:01:57 +1100 Subject: [PATCH] Add handling for HTTP Code 302 generated by HTTP/2 calls (#229) * Update initial Curl 7.62.0 fix with a better solution which handles the redirect's given by HTTP/2 connections (credit @Popa21) * Use HTTP/1.1 for session uploads otherwise the response when using HTTP/2 generates a 'JSONValue is not an object' error --- src/onedrive.d | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/onedrive.d b/src/onedrive.d index 784cccb9..bf1f3795 100644 --- a/src/onedrive.d +++ b/src/onedrive.d @@ -60,9 +60,8 @@ final class OneDriveApi http.dnsTimeout = (dur!"seconds"(5)); http.dataTimeout = (dur!"seconds"(3600)); - // Specify which HTTP version to use - // Curl 7.62.0 defaults to http2, we need to use http 1.1 - http.handle.set(CurlOption.http_version,2); + // Specify how many redirects should be allowed + http.maxRedirects(5); if (debugHttp) { http.verbose = true; @@ -252,8 +251,11 @@ final class OneDriveApi } http.method = HTTP.Method.put; http.url = uploadUrl; - // when using microsoft graph the auth code is different - //addAccessTokenHeader(); + + // Specify which HTTP version to use for session uploads + // Curl 7.62.0 defaults to HTTP/2, we need to use HTTP/1.1 + http.handle.set(CurlOption.http_version,2); + import std.conv; string contentRange = "bytes " ~ to!string(offset) ~ "-" ~ to!string(offset + offsetSize - 1) ~ "/" ~ to!string(fileSize); http.addRequestHeader("Content-Range", contentRange); @@ -558,6 +560,10 @@ final class OneDriveApi //log.vlog("OneDrive Response: '", http.statusLine.code, " - ", http.statusLine.reason, "'"); break; + // 302 - resource found and available at another location, redirect + case 302: + break; + // 400 - Bad Request case 400: // Bad Request .. how should we act? @@ -631,9 +637,9 @@ final class OneDriveApi log.vlog("OneDrive returned a 'HTTP 5xx Server Side Error' - gracefully handling error"); break; - // Default - all other errors that are not a 2xx + // Default - all other errors that are not a 2xx or a 302 default: - if (http.statusLine.code / 100 != 2) { + if (http.statusLine.code / 100 != 2 && http.statusLine.code != 302) { throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response); break; } @@ -671,6 +677,5 @@ unittest assert(e.httpStatusCode == 412); } onedrive.deleteById(item["id"].str, item["eTag"].str); - onedrive.http.shutdown(); }