using System; using System.Diagnostics; using System.IO; using System.Net; using NLog; using NzbDrone.Common.Cache; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Extensions; using NzbDrone.Common.TPL; namespace NzbDrone.Common.Http { public interface IHttpClient { HttpResponse Execute(HttpRequest request); void DownloadFile(string url, string fileName); HttpResponse Get(HttpRequest request); HttpResponse Get(HttpRequest request) where T : new(); HttpResponse Head(HttpRequest request); HttpResponse Post(HttpRequest request); HttpResponse Post(HttpRequest request) where T : new(); } public class HttpClient : IHttpClient { private readonly Logger _logger; private readonly IRateLimitService _rateLimitService; private readonly ICached _cookieContainerCache; public HttpClient(ICacheManager cacheManager, IRateLimitService rateLimitService, Logger logger) { _logger = logger; _rateLimitService = rateLimitService; ServicePointManager.DefaultConnectionLimit = 12; _cookieContainerCache = cacheManager.GetCache(typeof(HttpClient)); } public HttpResponse Execute(HttpRequest request) { if (request.RateLimit != TimeSpan.Zero) { _rateLimitService.WaitAndPulse(request.Url.Host, request.RateLimit); } _logger.Trace(request); var webRequest = (HttpWebRequest)WebRequest.Create(request.Url); // Deflate is not a standard and could break depending on implementation. // we should just stick with the more compatible Gzip //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.ContentLength = 0; if (!RuntimeInfoBase.IsProduction) { webRequest.AllowAutoRedirect = false; } var stopWatch = Stopwatch.StartNew(); if (request.Headers != null) { AddRequestHeaders(webRequest, request.Headers); } var cookieContainer = _cookieContainerCache.Get("container", () => new CookieContainer()); if (request.Cookies.Count != 0) { foreach (var pair in request.Cookies) { cookieContainer.Add(new Cookie(pair.Key, pair.Value, "/", request.Url.Host) { Expires = DateTime.UtcNow.AddHours(1) }); } } if (request.StoreResponseCookie) { webRequest.CookieContainer = cookieContainer; } else { webRequest.CookieContainer = new CookieContainer(); webRequest.CookieContainer.Add(cookieContainer.GetCookies(request.Url)); } if (!request.Body.IsNullOrWhiteSpace()) { var bytes = request.Headers.GetEncodingFromContentType().GetBytes(request.Body.ToCharArray()); webRequest.ContentLength = bytes.Length; using (var writeStream = webRequest.GetRequestStream()) { writeStream.Write(bytes, 0, bytes.Length); } } HttpWebResponse httpWebResponse; try { httpWebResponse = (HttpWebResponse)webRequest.GetResponse(); } catch (WebException e) { httpWebResponse = (HttpWebResponse)e.Response; if (httpWebResponse == null) { throw; } } Byte[] data = null; using (var responseStream = httpWebResponse.GetResponseStream()) { if (responseStream != null) { data = responseStream.ToBytes(); } } stopWatch.Stop(); var response = new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), data, httpWebResponse.StatusCode); _logger.Trace("{0} ({1:n0} ms)", response, stopWatch.ElapsedMilliseconds); if (request.AllowAutoRedirect && !RuntimeInfoBase.IsProduction && (response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.MovedPermanently || response.StatusCode == HttpStatusCode.Found)) { _logger.Error("Server requested a redirect to [" + response.Headers["Location"] + "]. Update the request URL to avoid this redirect."); } if (!request.SuppressHttpError && response.HasHttpError) { _logger.Warn("HTTP Error - {0}", response); throw new HttpException(request, response); } return response; } public void DownloadFile(string url, string fileName) { try { var fileInfo = new FileInfo(fileName); if (fileInfo.Directory != null && !fileInfo.Directory.Exists) { fileInfo.Directory.Create(); } _logger.Debug("Downloading [{0}] to [{1}]", url, fileName); var stopWatch = Stopwatch.StartNew(); var webClient = new GZipWebClient(); webClient.Headers.Add(HttpRequestHeader.UserAgent, UserAgentBuilder.UserAgent); webClient.DownloadFile(url, fileName); stopWatch.Stop(); _logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds); } catch (WebException e) { _logger.Warn("Failed to get response from: {0} {1}", url, e.Message); throw; } catch (Exception e) { _logger.WarnException("Failed to get response from: " + url, e); throw; } } public HttpResponse Get(HttpRequest request) { request.Method = HttpMethod.GET; return Execute(request); } public HttpResponse Get(HttpRequest request) where T : new() { var response = Get(request); return new HttpResponse(response); } public HttpResponse Head(HttpRequest request) { request.Method = HttpMethod.HEAD; return Execute(request); } public HttpResponse Post(HttpRequest request) { request.Method = HttpMethod.POST; return Execute(request); } public HttpResponse Post(HttpRequest request) where T : new() { var response = Post(request); return new HttpResponse(response); } protected virtual void AddRequestHeaders(HttpWebRequest webRequest, HttpHeader headers) { foreach (var header in headers) { switch (header.Key) { case "Accept": webRequest.Accept = header.Value.ToString(); break; case "Connection": webRequest.Connection = header.Value.ToString(); break; case "Content-Length": webRequest.ContentLength = Convert.ToInt64(header.Value); break; case "Content-Type": webRequest.ContentType = header.Value.ToString(); break; case "Date": webRequest.Date = (DateTime)header.Value; break; case "Expect": webRequest.Expect = header.Value.ToString(); break; case "Host": webRequest.Host = header.Value.ToString(); break; case "If-Modified-Since": webRequest.IfModifiedSince = (DateTime)header.Value; break; case "Range": throw new NotImplementedException(); break; case "Referer": webRequest.Referer = header.Value.ToString(); break; case "Transfer-Encoding": webRequest.TransferEncoding = header.Value.ToString(); break; case "User-Agent": throw new NotSupportedException("User-Agent other than Sonarr not allowed."); case "Proxy-Connection": throw new NotImplementedException(); break; default: webRequest.Headers.Add(header.Key, header.Value.ToString()); break; } } } } }