http2
Qstick 3 years ago
parent e04133d34a
commit fa304dcaca

@ -91,7 +91,9 @@ namespace NzbDrone.Common.Http
request.ContentData = null; request.ContentData = null;
} }
response = await ExecuteRequestAsync(request, cookieContainer); var redirectContainer = HandleRedirectCookies(request, response);
response = await ExecuteRequestAsync(request, redirectContainer);
} }
while (response.HasHttpRedirect); while (response.HasHttpRedirect);
} }
@ -162,6 +164,41 @@ namespace NzbDrone.Common.Http
return response; 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) private CookieContainer InitializeRequestCookies(HttpRequest request)
{ {
lock (_cookieContainerCache) lock (_cookieContainerCache)

@ -11,21 +11,19 @@ namespace NzbDrone.Common.Http
{ {
private static readonly Regex RegexSetCookie = new Regex("^(.*?)=(.*?)(?:;|$)", RegexOptions.Compiled); 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; Request = request;
Headers = headers; Headers = headers;
Cookies = cookies;
ResponseData = binaryData; ResponseData = binaryData;
StatusCode = statusCode; StatusCode = statusCode;
ElapsedTime = elapsedTime; 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; Request = request;
Headers = headers; Headers = headers;
Cookies = cookies;
ResponseData = Headers.GetEncodingFromContentType().GetBytes(content); ResponseData = Headers.GetEncodingFromContentType().GetBytes(content);
_content = content; _content = content;
StatusCode = statusCode; StatusCode = statusCode;
@ -34,7 +32,6 @@ namespace NzbDrone.Common.Http
public HttpRequest Request { get; private set; } public HttpRequest Request { get; private set; }
public HttpHeader Headers { get; private set; } public HttpHeader Headers { get; private set; }
public CookieCollection Cookies { get; private set; }
public HttpStatusCode StatusCode { get; private set; } public HttpStatusCode StatusCode { get; private set; }
public long ElapsedTime { get; private set; } public long ElapsedTime { get; private set; }
public byte[] ResponseData { get; private set; } public byte[] ResponseData { get; private set; }
@ -92,9 +89,14 @@ namespace NzbDrone.Common.Http
{ {
var result = new Dictionary<string, string>(); var result = new Dictionary<string, string>();
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; return result;

Loading…
Cancel
Save