Fixed: Now sends appropriate http Accept header to indexer.

pull/3113/head
Taloth Saldono 10 years ago
parent 525f1aa9dd
commit b3f086fe93

@ -57,6 +57,8 @@ namespace NzbDrone.Common.Test.Http
var exception = Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request));
exception.Response.StatusCode.Should().Be(statusCode);
ExceptionVerification.IgnoreWarns();
}
@ -67,10 +69,33 @@ namespace NzbDrone.Common.Test.Http
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 @@ namespace NzbDrone.Common.Test.Http
public string Origin { get; set; }
public string Url { get; set; }
}
}

@ -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;
}
}
}
}
}

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

Loading…
Cancel
Save