Updated HttpResponse to support binary content.

This commit is contained in:
Taloth Saldono 2014-09-13 00:39:42 +02:00
parent 56436fea69
commit 80ed203258
6 changed files with 68 additions and 26 deletions

View File

@ -4,6 +4,7 @@ using System.IO;
using System.Net; using System.Net;
using NLog; using NLog;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
namespace NzbDrone.Common.Http namespace NzbDrone.Common.Http
{ {
@ -73,22 +74,19 @@ namespace NzbDrone.Common.Http
httpWebResponse = (HttpWebResponse)e.Response; httpWebResponse = (HttpWebResponse)e.Response;
} }
string content = null; Byte[] data = null;
using (var responseStream = httpWebResponse.GetResponseStream()) using (var responseStream = httpWebResponse.GetResponseStream())
{ {
if (responseStream != null) if (responseStream != null)
{ {
using (var reader = new StreamReader(responseStream)) data = responseStream.ToBytes();
{
content = reader.ReadToEnd();
}
} }
} }
stopWatch.Stop(); stopWatch.Stop();
var response = new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), content, httpWebResponse.StatusCode); var response = new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), data, httpWebResponse.StatusCode);
_logger.Trace("{0} ({1:n0} ms)", response, stopWatch.ElapsedMilliseconds); _logger.Trace("{0} ({1:n0} ms)", response, stopWatch.ElapsedMilliseconds);
if (!RuntimeInfoBase.IsProduction && if (!RuntimeInfoBase.IsProduction &&

View File

@ -1,24 +1,51 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Net; using System.Net;
using System.Text;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
namespace NzbDrone.Common.Http namespace NzbDrone.Common.Http
{ {
public class HttpResponse public class HttpResponse
{ {
public HttpResponse(HttpRequest request, HttpHeader headers, string content, HttpStatusCode statusCode) public HttpResponse(HttpRequest request, HttpHeader headers, Byte[] binaryData, HttpStatusCode statusCode = HttpStatusCode.OK)
{ {
Request = request; Request = request;
Headers = headers; Headers = headers;
Content = content; ResponseData = binaryData;
StatusCode = statusCode;
}
public HttpResponse(HttpRequest request, HttpHeader headers, String content, HttpStatusCode statusCode = HttpStatusCode.OK)
{
Request = request;
Headers = headers;
ResponseData = Encoding.UTF8.GetBytes(content);
_content = content;
StatusCode = statusCode; StatusCode = statusCode;
} }
public HttpRequest Request { get; private set; } public HttpRequest Request { get; private set; }
public HttpHeader Headers { get; private set; } public HttpHeader Headers { get; private set; }
public HttpStatusCode StatusCode { get; private set; } public HttpStatusCode StatusCode { get; private set; }
public string Content { get; private set; } public Byte[] ResponseData { get; private set; }
private String _content;
public String Content
{
get
{
if (_content == null)
{
_content = GetStringFromResponseData();
}
return _content;
}
}
public bool HasHttpError public bool HasHttpError
{ {
@ -40,14 +67,34 @@ namespace NzbDrone.Common.Http
return result; return result;
} }
public Stream GetStream() protected virtual String GetStringFromResponseData()
{ {
var stream = new MemoryStream(); Encoding encoding = null;
var writer = new StreamWriter(stream);
writer.Write(Content); if (Headers.ContentType.IsNotNullOrWhiteSpace())
writer.Flush(); {
stream.Position = 0; var charset = Headers.ContentType.ToLowerInvariant()
return stream; .Split(';', '=', ' ')
.SkipWhile(v => v != "charset")
.Skip(1).FirstOrDefault();
if (charset.IsNotNullOrWhiteSpace())
{
encoding = Encoding.GetEncoding(charset);
}
}
if (encoding == null)
{
// TODO: Find encoding by Byte order mask.
}
if (encoding == null)
{
encoding = Encoding.UTF8;
}
return encoding.GetString(ResponseData);
} }
} }
@ -55,7 +102,7 @@ namespace NzbDrone.Common.Http
public class HttpResponse<T> : HttpResponse where T : new() public class HttpResponse<T> : HttpResponse where T : new()
{ {
public HttpResponse(HttpResponse response) public HttpResponse(HttpResponse response)
: base(response.Request, response.Headers, response.Content, response.StatusCode) : base(response.Request, response.Headers, response.ResponseData, response.StatusCode)
{ {
Resource = Json.Deserialize<T>(response.Content); Resource = Json.Deserialize<T>(response.Content);
} }

View File

@ -33,8 +33,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(c => c.Get(It.IsAny<HttpRequest>())) .Setup(s => s.Get(It.IsAny<HttpRequest>()))
.Returns(new HttpResponse(null, null, "", HttpStatusCode.OK)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), new Byte[0]));
} }

View File

@ -24,9 +24,9 @@ namespace NzbDrone.Core.Test.IndexerTests
{ {
_series = Builder<Series>.CreateNew().Build(); _series = Builder<Series>.CreateNew().Build();
var response = new HttpResponse(null, new HttpHeader(), "<xml></xml>", System.Net.HttpStatusCode.OK);
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(s => s.Get(It.IsAny<HttpRequest>())).Returns(response); .Setup(o => o.Get(It.IsAny<HttpRequest>()))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), "<xml></xml>"));
} }
private IndexerBase<TestIndexerSettings> WithIndexer(bool paging, int resultCount) private IndexerBase<TestIndexerSettings> WithIndexer(bool paging, int resultCount)

View File

@ -20,7 +20,7 @@ namespace NzbDrone.Core.Test.MediaCoverTests
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
_httpResponse = new HttpResponse(null, new HttpHeader(), null, HttpStatusCode.OK); _httpResponse = new HttpResponse(null, new HttpHeader(), "", HttpStatusCode.OK);
Mocker.GetMock<IDiskProvider>().Setup(c => c.GetFileSize(It.IsAny<string>())).Returns(100); Mocker.GetMock<IDiskProvider>().Setup(c => c.GetFileSize(It.IsAny<string>())).Returns(100);
Mocker.GetMock<IHttpClient>().Setup(c => c.Head(It.IsAny<HttpRequest>())).Returns(_httpResponse); Mocker.GetMock<IHttpClient>().Setup(c => c.Head(It.IsAny<HttpRequest>())).Returns(_httpResponse);

View File

@ -52,10 +52,7 @@ namespace NzbDrone.Core.Download
try try
{ {
using (var nzb = _httpClient.Get(new HttpRequest(url)).GetStream()) nzbData = _httpClient.Get(new HttpRequest(url)).ResponseData;
{
nzbData = nzb.ToBytes();
}
} }
catch (WebException ex) catch (WebException ex)
{ {