2014-09-11 23:49:41 +00:00
|
|
|
using System;
|
2016-02-28 15:41:22 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2014-09-11 23:49:41 +00:00
|
|
|
using System.Net;
|
2016-02-28 15:41:22 +00:00
|
|
|
using System.Text;
|
|
|
|
using NzbDrone.Common.Extensions;
|
2014-09-11 23:49:41 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Common.Http
|
|
|
|
{
|
|
|
|
public class HttpRequestBuilder
|
|
|
|
{
|
2016-02-28 15:41:22 +00:00
|
|
|
public HttpMethod Method { get; set; }
|
|
|
|
public HttpAccept HttpAccept { get; set; }
|
2016-02-29 23:33:28 +00:00
|
|
|
public HttpUri BaseUrl { get; private set; }
|
2016-02-28 15:41:22 +00:00
|
|
|
public string ResourceUrl { get; set; }
|
|
|
|
public List<KeyValuePair<string, string>> QueryParams { get; private set; }
|
|
|
|
public List<KeyValuePair<string, string>> SuffixQueryParams { get; private set; }
|
|
|
|
public Dictionary<string, string> Segments { get; private set; }
|
|
|
|
public HttpHeader Headers { get; private set; }
|
|
|
|
public bool SuppressHttpError { get; set; }
|
2021-03-19 20:31:46 +00:00
|
|
|
public bool LogHttpError { get; set; }
|
2016-08-10 18:45:48 +00:00
|
|
|
public bool UseSimplifiedUserAgent { get; set; }
|
2016-02-28 15:41:22 +00:00
|
|
|
public bool AllowAutoRedirect { get; set; }
|
2016-03-19 11:13:34 +00:00
|
|
|
public bool ConnectionKeepAlive { get; set; }
|
2016-09-29 19:40:52 +00:00
|
|
|
public TimeSpan RateLimit { get; set; }
|
2016-04-06 20:53:14 +00:00
|
|
|
public bool LogResponseContent { get; set; }
|
2014-09-11 23:49:41 +00:00
|
|
|
public NetworkCredential NetworkCredential { get; set; }
|
2016-02-28 15:41:22 +00:00
|
|
|
public Dictionary<string, string> Cookies { get; private set; }
|
2016-02-28 15:41:28 +00:00
|
|
|
public List<HttpFormData> FormData { get; private set; }
|
2014-09-11 23:49:41 +00:00
|
|
|
|
|
|
|
public Action<HttpRequest> PostProcess { get; set; }
|
|
|
|
|
2016-02-28 15:41:22 +00:00
|
|
|
public HttpRequestBuilder(string baseUrl)
|
2014-09-11 23:49:41 +00:00
|
|
|
{
|
2016-02-29 23:33:28 +00:00
|
|
|
BaseUrl = new HttpUri(baseUrl);
|
2016-02-28 15:41:22 +00:00
|
|
|
ResourceUrl = string.Empty;
|
|
|
|
Method = HttpMethod.GET;
|
|
|
|
QueryParams = new List<KeyValuePair<string, string>>();
|
|
|
|
SuffixQueryParams = new List<KeyValuePair<string, string>>();
|
|
|
|
Segments = new Dictionary<string, string>();
|
|
|
|
Headers = new HttpHeader();
|
|
|
|
Cookies = new Dictionary<string, string>();
|
2016-02-28 15:41:28 +00:00
|
|
|
FormData = new List<HttpFormData>();
|
2021-03-19 20:31:46 +00:00
|
|
|
LogHttpError = true;
|
2014-09-11 23:49:41 +00:00
|
|
|
}
|
|
|
|
|
2016-02-28 15:41:22 +00:00
|
|
|
public HttpRequestBuilder(bool useHttps, string host, int port, string urlBase = null)
|
|
|
|
: this(BuildBaseUrl(useHttps, host, port, urlBase))
|
2014-09-11 23:49:41 +00:00
|
|
|
{
|
2016-02-28 15:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static string BuildBaseUrl(bool useHttps, string host, int port, string urlBase = null)
|
|
|
|
{
|
|
|
|
var protocol = useHttps ? "https" : "http";
|
|
|
|
|
|
|
|
if (urlBase.IsNotNullOrWhiteSpace() && !urlBase.StartsWith("/"))
|
2014-09-11 23:49:41 +00:00
|
|
|
{
|
2016-02-28 15:41:22 +00:00
|
|
|
urlBase = "/" + urlBase;
|
2014-09-11 23:49:41 +00:00
|
|
|
}
|
|
|
|
|
2016-02-28 15:41:22 +00:00
|
|
|
return string.Format("{0}://{1}:{2}{3}", protocol, host, port, urlBase);
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual HttpRequestBuilder Clone()
|
|
|
|
{
|
|
|
|
var clone = MemberwiseClone() as HttpRequestBuilder;
|
|
|
|
clone.QueryParams = new List<KeyValuePair<string, string>>(clone.QueryParams);
|
|
|
|
clone.SuffixQueryParams = new List<KeyValuePair<string, string>>(clone.SuffixQueryParams);
|
|
|
|
clone.Segments = new Dictionary<string, string>(clone.Segments);
|
|
|
|
clone.Headers = new HttpHeader(clone.Headers);
|
|
|
|
clone.Cookies = new Dictionary<string, string>(clone.Cookies);
|
2016-02-28 15:41:28 +00:00
|
|
|
clone.FormData = new List<HttpFormData>(clone.FormData);
|
2016-02-28 15:41:22 +00:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2016-02-29 23:33:28 +00:00
|
|
|
protected virtual HttpUri CreateUri()
|
2016-02-28 15:41:22 +00:00
|
|
|
{
|
2016-02-29 23:33:28 +00:00
|
|
|
var url = BaseUrl.CombinePath(ResourceUrl).AddQueryParams(QueryParams.Concat(SuffixQueryParams));
|
2016-09-29 19:40:52 +00:00
|
|
|
|
2016-02-28 15:41:22 +00:00
|
|
|
if (Segments.Any())
|
|
|
|
{
|
2016-02-29 23:33:28 +00:00
|
|
|
var fullUri = url.FullUri;
|
2016-02-28 15:41:22 +00:00
|
|
|
|
|
|
|
foreach (var segment in Segments)
|
|
|
|
{
|
2016-02-29 23:33:28 +00:00
|
|
|
fullUri = fullUri.Replace(segment.Key, segment.Value);
|
2016-02-28 15:41:22 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 23:33:28 +00:00
|
|
|
url = new HttpUri(fullUri);
|
2016-02-28 15:41:22 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 23:33:28 +00:00
|
|
|
return url;
|
2016-02-28 15:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual HttpRequest CreateRequest()
|
|
|
|
{
|
2016-02-29 23:33:28 +00:00
|
|
|
return new HttpRequest(CreateUri().FullUri, HttpAccept);
|
2016-02-28 15:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Apply(HttpRequest request)
|
|
|
|
{
|
|
|
|
request.Method = Method;
|
|
|
|
request.SuppressHttpError = SuppressHttpError;
|
2021-03-19 20:31:46 +00:00
|
|
|
request.LogHttpError = LogHttpError;
|
2016-08-10 18:45:48 +00:00
|
|
|
request.UseSimplifiedUserAgent = UseSimplifiedUserAgent;
|
2016-02-28 15:41:22 +00:00
|
|
|
request.AllowAutoRedirect = AllowAutoRedirect;
|
2016-03-19 11:13:34 +00:00
|
|
|
request.ConnectionKeepAlive = ConnectionKeepAlive;
|
2016-09-29 19:40:52 +00:00
|
|
|
request.RateLimit = RateLimit;
|
2016-04-06 20:53:14 +00:00
|
|
|
request.LogResponseContent = LogResponseContent;
|
2016-03-17 19:15:53 +00:00
|
|
|
|
|
|
|
if (NetworkCredential != null)
|
|
|
|
{
|
|
|
|
var authInfo = NetworkCredential.UserName + ":" + NetworkCredential.Password;
|
|
|
|
authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(authInfo));
|
|
|
|
request.Headers.Set("Authorization", "Basic " + authInfo);
|
|
|
|
}
|
2016-02-28 15:41:22 +00:00
|
|
|
|
|
|
|
foreach (var header in Headers)
|
|
|
|
{
|
|
|
|
request.Headers.Set(header.Key, header.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var cookie in Cookies)
|
|
|
|
{
|
|
|
|
request.Cookies[cookie.Key] = cookie.Value;
|
|
|
|
}
|
2016-02-28 15:41:28 +00:00
|
|
|
|
|
|
|
ApplyFormData(request);
|
2016-02-28 15:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual HttpRequest Build()
|
|
|
|
{
|
|
|
|
var request = CreateRequest();
|
|
|
|
|
|
|
|
Apply(request);
|
2014-09-11 23:49:41 +00:00
|
|
|
|
|
|
|
if (PostProcess != null)
|
|
|
|
{
|
|
|
|
PostProcess(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
2016-02-28 15:41:22 +00:00
|
|
|
|
|
|
|
public IHttpRequestBuilderFactory CreateFactory()
|
|
|
|
{
|
|
|
|
return new HttpRequestBuilderFactory(this);
|
|
|
|
}
|
|
|
|
|
2016-02-28 15:41:28 +00:00
|
|
|
protected virtual void ApplyFormData(HttpRequest request)
|
|
|
|
{
|
2021-08-03 04:43:28 +00:00
|
|
|
if (FormData.Empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-02-28 15:41:28 +00:00
|
|
|
|
|
|
|
if (request.ContentData != null)
|
|
|
|
{
|
|
|
|
throw new ApplicationException("Cannot send HttpRequest Body and FormData simultaneously.");
|
|
|
|
}
|
|
|
|
|
|
|
|
var shouldSendAsMultipart = FormData.Any(v => v.ContentType != null || v.FileName != null || v.ContentData.Length > 1024);
|
|
|
|
|
|
|
|
if (shouldSendAsMultipart)
|
|
|
|
{
|
|
|
|
var boundary = "-----------------------------" + DateTime.Now.Ticks.ToString("x14");
|
|
|
|
var partBoundary = string.Format("--{0}\r\n", boundary);
|
|
|
|
var endBoundary = string.Format("--{0}--\r\n", boundary);
|
|
|
|
|
|
|
|
var bodyStream = new MemoryStream();
|
|
|
|
var summary = new StringBuilder();
|
|
|
|
|
|
|
|
using (var writer = new StreamWriter(bodyStream, new UTF8Encoding(false)))
|
|
|
|
{
|
|
|
|
foreach (var formData in FormData)
|
|
|
|
{
|
|
|
|
writer.Write(partBoundary);
|
|
|
|
|
|
|
|
writer.Write("Content-Disposition: form-data");
|
2021-08-03 04:43:28 +00:00
|
|
|
if (formData.Name.IsNotNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
writer.Write("; name=\"{0}\"", formData.Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (formData.FileName.IsNotNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
writer.Write("; filename=\"{0}\"", formData.FileName);
|
|
|
|
}
|
|
|
|
|
2016-02-28 15:41:28 +00:00
|
|
|
writer.Write("\r\n");
|
|
|
|
|
2021-08-03 04:43:28 +00:00
|
|
|
if (formData.ContentType.IsNotNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
writer.Write("Content-Type: {0}\r\n", formData.ContentType);
|
|
|
|
}
|
2016-02-28 15:41:28 +00:00
|
|
|
|
|
|
|
writer.Write("\r\n");
|
|
|
|
writer.Flush();
|
|
|
|
|
|
|
|
bodyStream.Write(formData.ContentData, 0, formData.ContentData.Length);
|
|
|
|
writer.Write("\r\n");
|
|
|
|
|
|
|
|
if (formData.FileName.IsNotNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
summary.AppendFormat("\r\n{0}={1} ({2} bytes)", formData.Name, formData.FileName, formData.ContentData.Length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
summary.AppendFormat("\r\n{0}={1}", formData.Name, Encoding.UTF8.GetString(formData.ContentData));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Write(endBoundary);
|
|
|
|
}
|
|
|
|
|
|
|
|
var body = bodyStream.ToArray();
|
|
|
|
|
|
|
|
// TODO: Scan through body to see if we have a boundary collision?
|
|
|
|
|
|
|
|
request.Headers.ContentType = "multipart/form-data; boundary=" + boundary;
|
|
|
|
request.SetContent(body);
|
|
|
|
|
|
|
|
if (request.ContentSummary == null)
|
|
|
|
{
|
|
|
|
request.ContentSummary = summary.ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var parameters = FormData.Select(v => string.Format("{0}={1}", v.Name, Uri.EscapeDataString(Encoding.UTF8.GetString(v.ContentData))));
|
|
|
|
var urlencoded = string.Join("&", parameters);
|
|
|
|
var body = Encoding.UTF8.GetBytes(urlencoded);
|
|
|
|
|
|
|
|
request.Headers.ContentType = "application/x-www-form-urlencoded";
|
|
|
|
request.SetContent(body);
|
|
|
|
|
|
|
|
if (request.ContentSummary == null)
|
|
|
|
{
|
|
|
|
request.ContentSummary = urlencoded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 15:41:22 +00:00
|
|
|
public virtual HttpRequestBuilder Resource(string resourceUrl)
|
|
|
|
{
|
|
|
|
if (!ResourceUrl.IsNotNullOrWhiteSpace() || resourceUrl.StartsWith("/"))
|
|
|
|
{
|
|
|
|
ResourceUrl = resourceUrl.TrimStart('/');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ResourceUrl = string.Format("{0}/{1}", ResourceUrl.TrimEnd('/'), resourceUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-03-19 11:13:34 +00:00
|
|
|
public virtual HttpRequestBuilder KeepAlive(bool keepAlive = true)
|
|
|
|
{
|
|
|
|
ConnectionKeepAlive = keepAlive;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-09-29 19:40:52 +00:00
|
|
|
public virtual HttpRequestBuilder WithRateLimit(double seconds)
|
|
|
|
{
|
|
|
|
RateLimit = TimeSpan.FromSeconds(seconds);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-02-28 15:41:22 +00:00
|
|
|
public virtual HttpRequestBuilder Post()
|
|
|
|
{
|
|
|
|
Method = HttpMethod.POST;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual HttpRequestBuilder Accept(HttpAccept accept)
|
|
|
|
{
|
|
|
|
HttpAccept = accept;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual HttpRequestBuilder SetHeader(string name, string value)
|
|
|
|
{
|
|
|
|
Headers.Set(name, value);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-03-19 11:13:34 +00:00
|
|
|
public virtual HttpRequestBuilder AddPrefixQueryParam(string key, object value, bool replace = false)
|
|
|
|
{
|
|
|
|
if (replace)
|
|
|
|
{
|
|
|
|
QueryParams.RemoveAll(v => v.Key == key);
|
|
|
|
SuffixQueryParams.RemoveAll(v => v.Key == key);
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryParams.Insert(0, new KeyValuePair<string, string>(key, value.ToString()));
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-02-28 15:41:22 +00:00
|
|
|
public virtual HttpRequestBuilder AddQueryParam(string key, object value, bool replace = false)
|
|
|
|
{
|
|
|
|
if (replace)
|
|
|
|
{
|
|
|
|
QueryParams.RemoveAll(v => v.Key == key);
|
|
|
|
SuffixQueryParams.RemoveAll(v => v.Key == key);
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryParams.Add(key, value.ToString());
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual HttpRequestBuilder AddSuffixQueryParam(string key, object value, bool replace = false)
|
|
|
|
{
|
|
|
|
if (replace)
|
|
|
|
{
|
|
|
|
QueryParams.RemoveAll(v => v.Key == key);
|
|
|
|
SuffixQueryParams.RemoveAll(v => v.Key == key);
|
|
|
|
}
|
|
|
|
|
|
|
|
SuffixQueryParams.Add(new KeyValuePair<string, string>(key, value.ToString()));
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual HttpRequestBuilder SetSegment(string segment, string value, bool dontCheck = false)
|
|
|
|
{
|
|
|
|
var key = string.Concat("{", segment, "}");
|
|
|
|
|
|
|
|
if (!dontCheck && !CreateUri().ToString().Contains(key))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException(string.Format("Segment {0} is not defined in Uri", segment));
|
|
|
|
}
|
|
|
|
|
|
|
|
Segments[key] = value;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual HttpRequestBuilder SetCookies(IEnumerable<KeyValuePair<string, string>> cookies)
|
|
|
|
{
|
|
|
|
foreach (var cookie in cookies)
|
|
|
|
{
|
|
|
|
Cookies[cookie.Key] = cookie.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual HttpRequestBuilder SetCookie(string key, string value)
|
|
|
|
{
|
|
|
|
Cookies[key] = value;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2016-02-28 15:41:28 +00:00
|
|
|
|
|
|
|
public virtual HttpRequestBuilder AddFormParameter(string key, object value)
|
|
|
|
{
|
|
|
|
if (Method != HttpMethod.POST)
|
|
|
|
{
|
|
|
|
throw new NotSupportedException("HttpRequest Method must be POST to add FormParameter.");
|
|
|
|
}
|
|
|
|
|
|
|
|
FormData.Add(new HttpFormData
|
|
|
|
{
|
|
|
|
Name = key,
|
2019-03-03 18:24:01 +00:00
|
|
|
ContentData = Encoding.UTF8.GetBytes(Convert.ToString(value, System.Globalization.CultureInfo.InvariantCulture))
|
2016-02-28 15:41:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual HttpRequestBuilder AddFormUpload(string name, string fileName, byte[] data, string contentType = "application/octet-stream")
|
|
|
|
{
|
|
|
|
if (Method != HttpMethod.POST)
|
|
|
|
{
|
|
|
|
throw new NotSupportedException("HttpRequest Method must be POST to add FormUpload.");
|
|
|
|
}
|
|
|
|
|
|
|
|
FormData.Add(new HttpFormData
|
|
|
|
{
|
|
|
|
Name = name,
|
|
|
|
FileName = fileName,
|
|
|
|
ContentData = data,
|
|
|
|
ContentType = contentType
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2014-09-11 23:49:41 +00:00
|
|
|
}
|
2016-09-29 19:40:52 +00:00
|
|
|
}
|