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
This commit is contained in:
abraunegg 2018-11-12 06:01:57 +11:00 committed by GitHub
parent 7f5b5d981b
commit 8a98bf3e30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -60,9 +60,8 @@ final class OneDriveApi
http.dnsTimeout = (dur!"seconds"(5)); http.dnsTimeout = (dur!"seconds"(5));
http.dataTimeout = (dur!"seconds"(3600)); http.dataTimeout = (dur!"seconds"(3600));
// Specify which HTTP version to use // Specify how many redirects should be allowed
// Curl 7.62.0 defaults to http2, we need to use http 1.1 http.maxRedirects(5);
http.handle.set(CurlOption.http_version,2);
if (debugHttp) { if (debugHttp) {
http.verbose = true; http.verbose = true;
@ -252,8 +251,11 @@ final class OneDriveApi
} }
http.method = HTTP.Method.put; http.method = HTTP.Method.put;
http.url = uploadUrl; 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; import std.conv;
string contentRange = "bytes " ~ to!string(offset) ~ "-" ~ to!string(offset + offsetSize - 1) ~ "/" ~ to!string(fileSize); string contentRange = "bytes " ~ to!string(offset) ~ "-" ~ to!string(offset + offsetSize - 1) ~ "/" ~ to!string(fileSize);
http.addRequestHeader("Content-Range", contentRange); http.addRequestHeader("Content-Range", contentRange);
@ -558,6 +560,10 @@ final class OneDriveApi
//log.vlog("OneDrive Response: '", http.statusLine.code, " - ", http.statusLine.reason, "'"); //log.vlog("OneDrive Response: '", http.statusLine.code, " - ", http.statusLine.reason, "'");
break; break;
// 302 - resource found and available at another location, redirect
case 302:
break;
// 400 - Bad Request // 400 - Bad Request
case 400: case 400:
// Bad Request .. how should we act? // 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"); log.vlog("OneDrive returned a 'HTTP 5xx Server Side Error' - gracefully handling error");
break; break;
// Default - all other errors that are not a 2xx // Default - all other errors that are not a 2xx or a 302
default: 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); throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response);
break; break;
} }
@ -671,6 +677,5 @@ unittest
assert(e.httpStatusCode == 412); assert(e.httpStatusCode == 412);
} }
onedrive.deleteById(item["id"].str, item["eTag"].str); onedrive.deleteById(item["id"].str, item["eTag"].str);
onedrive.http.shutdown(); onedrive.http.shutdown();
} }