1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-29 11:25:26 +00:00

Fixed: Now sends appropriate http Accept header to indexer.

This commit is contained in:
Taloth Saldono 2014-09-14 11:47:46 +02:00
parent 525f1aa9dd
commit b3f086fe93
3 changed files with 87 additions and 10 deletions

View file

@ -57,6 +57,8 @@ public void should_throw_on_unsuccessful_status_codes(HttpStatusCode statusCode)
var exception = Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request));
exception.Response.StatusCode.Should().Be(statusCode);
ExceptionVerification.IgnoreWarns();
}
@ -67,10 +69,33 @@ public void should_not_follow_redirects_when_not_in_production(HttpStatusCode st
var request = new HttpRequest("http://eu.httpbin.org/status/" + (int)statusCode);
Assert.Throws<Exception>(() => Subject.Get<HttpBinResource>(request));
}
}
[Test]
public void should_send_user_agent()
{
var request = new HttpRequest("http://eu.httpbin.org/get");
var response = Subject.Get<HttpBinResource>(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<HttpBinResource>(request);
response.Resource.Headers[header].ToString().Should().Be(value);
}
}
public class HttpBinResource
{
@ -78,8 +103,4 @@ public class HttpBinResource
public string Origin { get; set; }
public string Url { get; set; }
}
}

View file

@ -25,7 +25,9 @@ public class HttpClient : IHttpClient
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 @@ public HttpResponse Execute(HttpRequest request)
webRequest.Credentials = request.NetworkCredential;
webRequest.Method = request.Method.ToString();
webRequest.UserAgent = _userAgent;
webRequest.KeepAlive = false;
if (!RuntimeInfoBase.IsProduction)
@ -51,6 +54,11 @@ public HttpResponse Execute(HttpRequest request)
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 @@ public HttpResponse Head(HttpRequest request)
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;
}
}
}
}
}

View file

@ -14,8 +14,6 @@ public HttpRequest(string url)
UriBuilder = new UriBuilder(url);
Headers = new HttpHeader();
_segments = new Dictionary<string, string>();
Headers.Accept = "application/json";
}
public UriBuilder UriBuilder { get; private set; }