You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Readarr/src/NzbDrone.Common/Http/HttpResponse.cs

112 lines
3.0 KiB

10 years ago
using System;
using System.IO;
using System.Linq;
10 years ago
using System.Net;
using System.Text;
10 years ago
using NzbDrone.Common.Serializer;
namespace NzbDrone.Common.Http
{
public class HttpResponse
{
public HttpResponse(HttpRequest request, HttpHeader headers, Byte[] binaryData, HttpStatusCode statusCode = HttpStatusCode.OK)
10 years ago
{
Request = request;
Headers = headers;
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;
10 years ago
StatusCode = statusCode;
}
public HttpRequest Request { get; private set; }
public HttpHeader Headers { get; private set; }
public HttpStatusCode StatusCode { get; private set; }
public Byte[] ResponseData { get; private set; }
private String _content;
public String Content
{
get
{
if (_content == null)
{
_content = GetStringFromResponseData();
}
return _content;
}
}
10 years ago
public bool HasHttpError
{
get
{
return (int)StatusCode >= 400;
}
}
public override string ToString()
{
var result = string.Format("Res: [{0}] {1} : {2}.{3}", Request.Method, Request.Url, (int)StatusCode, StatusCode);
if (HasHttpError)
{
result += Environment.NewLine + Content;
}
return result;
}
protected virtual String GetStringFromResponseData()
10 years ago
{
Encoding encoding = null;
if (Headers.ContentType.IsNotNullOrWhiteSpace())
{
var charset = Headers.ContentType.ToLowerInvariant()
.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);
10 years ago
}
}
public class HttpResponse<T> : HttpResponse where T : new()
{
public HttpResponse(HttpResponse response)
: base(response.Request, response.Headers, response.ResponseData, response.StatusCode)
10 years ago
{
Resource = Json.Deserialize<T>(response.Content);
}
public T Resource { get; private set; }
}
}