From 2d96914bfaa823ff68444e2b5a91defcce2dab5c Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 17 Mar 2016 20:15:53 +0100 Subject: [PATCH] Send Http auth without waiting for challenge. --- .../Http/Dispatchers/CurlHttpDispatcher.cs | 9 +-------- .../Http/Dispatchers/ManagedHttpDispatcher.cs | 6 ------ src/NzbDrone.Common/Http/HttpRequest.cs | 1 - src/NzbDrone.Common/Http/HttpRequestBuilder.cs | 8 +++++++- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/NzbDrone.Common/Http/Dispatchers/CurlHttpDispatcher.cs b/src/NzbDrone.Common/Http/Dispatchers/CurlHttpDispatcher.cs index 3b9583b66..5dcb084a9 100644 --- a/src/NzbDrone.Common/Http/Dispatchers/CurlHttpDispatcher.cs +++ b/src/NzbDrone.Common/Http/Dispatchers/CurlHttpDispatcher.cs @@ -41,13 +41,6 @@ public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies) throw new ApplicationException("Curl failed to initialize."); } - if (request.NetworkCredential != null) - { - var authInfo = request.NetworkCredential.UserName + ":" + request.NetworkCredential.Password; - authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); - request.Headers["Authorization"] = "Basic " + authInfo; - } - lock (CurlGlobalHandle.Instance) { Stream responseStream = new MemoryStream(); @@ -66,7 +59,7 @@ public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies) headerStream.Write(b, 0, s * n); return s * n; }; - + curlEasy.Url = request.Url.FullUri; switch (request.Method) { diff --git a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs index 72aa23033..6c6a9d103 100644 --- a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs +++ b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs @@ -15,18 +15,12 @@ public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies) //http://stackoverflow.com/questions/8490718/how-to-decompress-stream-deflated-with-java-util-zip-deflater-in-net webRequest.AutomaticDecompression = DecompressionMethods.GZip; - webRequest.Credentials = request.NetworkCredential; webRequest.Method = request.Method.ToString(); webRequest.UserAgent = UserAgentBuilder.UserAgent; webRequest.KeepAlive = false; webRequest.AllowAutoRedirect = request.AllowAutoRedirect; webRequest.CookieContainer = cookies; - if (request.NetworkCredential != null) - { - webRequest.PreAuthenticate = true; - } - if (request.RequestTimeout != TimeSpan.Zero) { webRequest.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalMilliseconds); diff --git a/src/NzbDrone.Common/Http/HttpRequest.cs b/src/NzbDrone.Common/Http/HttpRequest.cs index a855605dc..bd4af45e0 100644 --- a/src/NzbDrone.Common/Http/HttpRequest.cs +++ b/src/NzbDrone.Common/Http/HttpRequest.cs @@ -33,7 +33,6 @@ public HttpRequest(string url, HttpAccept httpAccept = null) public HttpHeader Headers { get; set; } public byte[] ContentData { get; set; } public string ContentSummary { get; set; } - public NetworkCredential NetworkCredential { get; set; } public bool SuppressHttpError { get; set; } public bool AllowAutoRedirect { get; set; } public Dictionary Cookies { get; private set; } diff --git a/src/NzbDrone.Common/Http/HttpRequestBuilder.cs b/src/NzbDrone.Common/Http/HttpRequestBuilder.cs index c175056dd..d4b3b5c96 100644 --- a/src/NzbDrone.Common/Http/HttpRequestBuilder.cs +++ b/src/NzbDrone.Common/Http/HttpRequestBuilder.cs @@ -98,7 +98,13 @@ protected virtual void Apply(HttpRequest request) request.Method = Method; request.SuppressHttpError = SuppressHttpError; request.AllowAutoRedirect = AllowAutoRedirect; - request.NetworkCredential = NetworkCredential; + + if (NetworkCredential != null) + { + var authInfo = NetworkCredential.UserName + ":" + NetworkCredential.Password; + authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(authInfo)); + request.Headers.Set("Authorization", "Basic " + authInfo); + } foreach (var header in Headers) {