diff --git a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs index b571ee9fa..02fde70da 100644 --- a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs +++ b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs @@ -57,6 +57,8 @@ namespace NzbDrone.Common.Test.Http var exception = Assert.Throws(() => Subject.Get(request)); exception.Response.StatusCode.Should().Be(statusCode); + + ExceptionVerification.IgnoreWarns(); } @@ -66,11 +68,34 @@ namespace NzbDrone.Common.Test.Http { var request = new HttpRequest("http://eu.httpbin.org/status/" + (int)statusCode); - Assert.Throws(() => Subject.Get(request)); - + Assert.Throws(() => Subject.Get(request)); } - } + [Test] + public void should_send_user_agent() + { + var request = new HttpRequest("http://eu.httpbin.org/get"); + + var response = Subject.Get(request); + + response.Resource.Headers.Should().ContainKey("User-Agent"); + + var userAgent = response.Resource.Headers["User-Agent"].ToString(); + + userAgent.Should().Contain("NzbDrone"); + } + + [TestCase("Accept", "text/xml, text/rss+xml, application/rss+xml")] + public void should_send_headers(String header, String value) + { + var request = new HttpRequest("http://eu.httpbin.org/get"); + request.Headers.Add(header, value); + + var response = Subject.Get(request); + + response.Resource.Headers[header].ToString().Should().Be(value); + } + } public class HttpBinResource { @@ -78,8 +103,4 @@ namespace NzbDrone.Common.Test.Http public string Origin { get; set; } public string Url { get; set; } } - - - - } \ No newline at end of file diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index 7aead3467..4da22ba66 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -25,7 +25,9 @@ namespace NzbDrone.Common.Http public HttpClient(Logger logger) { _logger = logger; - _userAgent = String.Format("NzbDrone {0}", BuildInfo.Version); + _userAgent = String.Format("NzbDrone/{0} ({1} {2})", + BuildInfo.Version, + OsInfo.Os, OsInfo.Version.ToString(2)); ServicePointManager.DefaultConnectionLimit = 12; } @@ -42,6 +44,7 @@ namespace NzbDrone.Common.Http webRequest.Credentials = request.NetworkCredential; webRequest.Method = request.Method.ToString(); + webRequest.UserAgent = _userAgent; webRequest.KeepAlive = false; if (!RuntimeInfoBase.IsProduction) @@ -51,6 +54,11 @@ namespace NzbDrone.Common.Http var stopWatch = Stopwatch.StartNew(); + if (request.Headers != null) + { + AddRequestHeaders(webRequest, request.Headers); + } + if (!request.Body.IsNullOrWhiteSpace()) { var bytes = new byte[request.Body.Length * sizeof(char)]; @@ -154,5 +162,55 @@ namespace NzbDrone.Common.Http return Execute(request); } + 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 NzbDrone not allowed."); + case "Proxy-Connection": + throw new NotImplementedException(); + break; + default: + webRequest.Headers.Add(header.Key, header.Value.ToString()); + break; + } + } + } } } \ No newline at end of file diff --git a/src/NzbDrone.Common/Http/HttpRequest.cs b/src/NzbDrone.Common/Http/HttpRequest.cs index 8ba571207..6fba92e10 100644 --- a/src/NzbDrone.Common/Http/HttpRequest.cs +++ b/src/NzbDrone.Common/Http/HttpRequest.cs @@ -14,8 +14,6 @@ namespace NzbDrone.Common.Http UriBuilder = new UriBuilder(url); Headers = new HttpHeader(); _segments = new Dictionary(); - - Headers.Accept = "application/json"; } public UriBuilder UriBuilder { get; private set; }