From fa304dcaca20f9e44a994fde25ea128d64f34b81 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 2 Jan 2022 23:39:55 -0600 Subject: [PATCH] Random --- src/NzbDrone.Common/Http/HttpClient.cs | 39 +++++++++++++++++++++++- src/NzbDrone.Common/Http/HttpResponse.cs | 16 +++++----- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index 47286b3b0..00ec3898f 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -91,7 +91,9 @@ namespace NzbDrone.Common.Http request.ContentData = null; } - response = await ExecuteRequestAsync(request, cookieContainer); + var redirectContainer = HandleRedirectCookies(request, response); + + response = await ExecuteRequestAsync(request, redirectContainer); } while (response.HasHttpRedirect); } @@ -162,6 +164,41 @@ namespace NzbDrone.Common.Http return response; } + private CookieContainer HandleRedirectCookies(HttpRequest request, HttpResponse response) + { + var sourceContainer = new CookieContainer(); + + var responseCookies = response.GetCookies(); + + if (responseCookies.Count != 0) + { + foreach (var pair in responseCookies) + { + Cookie cookie; + if (pair.Value == null) + { + cookie = new Cookie(pair.Key, "", "/") + { + Expires = DateTime.Now.AddDays(-1) + }; + } + else + { + cookie = new Cookie(pair.Key, pair.Value, "/") + { + // Use Now rather than UtcNow to work around Mono cookie expiry bug. + // See https://gist.github.com/ta264/7822b1424f72e5b4c961 + Expires = DateTime.Now.AddHours(1) + }; + } + + sourceContainer.Add((Uri)request.Url, cookie); + } + } + + return sourceContainer; + } + private CookieContainer InitializeRequestCookies(HttpRequest request) { lock (_cookieContainerCache) diff --git a/src/NzbDrone.Common/Http/HttpResponse.cs b/src/NzbDrone.Common/Http/HttpResponse.cs index d108705b3..84839bd6c 100644 --- a/src/NzbDrone.Common/Http/HttpResponse.cs +++ b/src/NzbDrone.Common/Http/HttpResponse.cs @@ -11,21 +11,19 @@ namespace NzbDrone.Common.Http { private static readonly Regex RegexSetCookie = new Regex("^(.*?)=(.*?)(?:;|$)", RegexOptions.Compiled); - public HttpResponse(HttpRequest request, HttpHeader headers, CookieCollection cookies, byte[] binaryData, long elapsedTime = 0, HttpStatusCode statusCode = HttpStatusCode.OK) + public HttpResponse(HttpRequest request, HttpHeader headers, byte[] binaryData, long elapsedTime = 0, HttpStatusCode statusCode = HttpStatusCode.OK) { Request = request; Headers = headers; - Cookies = cookies; ResponseData = binaryData; StatusCode = statusCode; ElapsedTime = elapsedTime; } - public HttpResponse(HttpRequest request, HttpHeader headers, CookieCollection cookies, string content, long elapsedTime = 0, HttpStatusCode statusCode = HttpStatusCode.OK) + public HttpResponse(HttpRequest request, HttpHeader headers, string content, long elapsedTime = 0, HttpStatusCode statusCode = HttpStatusCode.OK) { Request = request; Headers = headers; - Cookies = cookies; ResponseData = Headers.GetEncodingFromContentType().GetBytes(content); _content = content; StatusCode = statusCode; @@ -34,7 +32,6 @@ namespace NzbDrone.Common.Http public HttpRequest Request { get; private set; } public HttpHeader Headers { get; private set; } - public CookieCollection Cookies { get; private set; } public HttpStatusCode StatusCode { get; private set; } public long ElapsedTime { get; private set; } public byte[] ResponseData { get; private set; } @@ -92,9 +89,14 @@ namespace NzbDrone.Common.Http { var result = new Dictionary(); - foreach (Cookie cookie in Cookies) + var setCookieHeaders = CookieUtil.CookieHeaderToDictionary(); + foreach (var cookie in setCookieHeaders) { - result[cookie.Name] = cookie.Value; + var match = RegexSetCookie.Match(cookie); + if (match.Success) + { + result[match.Groups[1].Value] = match.Groups[2].Value; + } } return result;