2019-01-12 20:41:08 +00:00
|
|
|
using System;
|
2016-10-29 22:22:20 +00:00
|
|
|
using System.Net.Http;
|
2021-12-20 12:31:07 +00:00
|
|
|
using Jellyfin.Extensions;
|
2016-10-29 22:22:20 +00:00
|
|
|
|
|
|
|
namespace Rssdp.Infrastructure
|
|
|
|
{
|
2019-01-12 20:41:08 +00:00
|
|
|
/// <summary>
|
2019-01-13 20:37:13 +00:00
|
|
|
/// Parses a string into a <see cref="HttpRequestMessage"/> or throws an exception.
|
2019-01-12 20:41:08 +00:00
|
|
|
/// </summary>
|
|
|
|
public sealed class HttpRequestParser : HttpParserBase<HttpRequestMessage>
|
|
|
|
{
|
|
|
|
private readonly string[] ContentHeaderNames = new string[]
|
2020-06-20 06:20:33 +00:00
|
|
|
{
|
|
|
|
"Allow", "Content-Disposition", "Content-Encoding", "Content-Language", "Content-Length", "Content-Location", "Content-MD5", "Content-Range", "Content-Type", "Expires", "Last-Modified"
|
|
|
|
};
|
2019-01-12 20:41:08 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2019-01-13 20:37:13 +00:00
|
|
|
/// Parses the specified data into a <see cref="HttpRequestMessage"/> instance.
|
2019-01-12 20:41:08 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">A string containing the data to parse.</param>
|
2019-01-13 20:37:13 +00:00
|
|
|
/// <returns>A <see cref="HttpRequestMessage"/> instance containing the parsed data.</returns>
|
|
|
|
public override HttpRequestMessage Parse(string data)
|
2019-01-12 20:41:08 +00:00
|
|
|
{
|
2019-01-13 20:37:13 +00:00
|
|
|
HttpRequestMessage retVal = null;
|
2019-01-12 20:41:08 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2019-01-13 20:37:13 +00:00
|
|
|
retVal = new HttpRequestMessage();
|
2019-01-12 20:41:08 +00:00
|
|
|
|
|
|
|
Parse(retVal, retVal.Headers, data);
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (retVal != null)
|
2020-06-20 09:12:36 +00:00
|
|
|
{
|
2019-01-12 20:41:08 +00:00
|
|
|
retVal.Dispose();
|
2020-06-20 09:12:36 +00:00
|
|
|
}
|
2019-01-12 20:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Used to parse the first line of an HTTP request or response and assign the values to the appropriate properties on the <paramref name="message"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">The first line of the HTTP message to be parsed.</param>
|
2019-01-13 20:37:13 +00:00
|
|
|
/// <param name="message">Either a <see cref="HttpResponseMessage"/> or <see cref="HttpRequestMessage"/> to assign the parsed values to.</param>
|
2019-01-12 20:41:08 +00:00
|
|
|
protected override void ParseStatusLine(string data, HttpRequestMessage message)
|
|
|
|
{
|
2020-06-20 08:35:29 +00:00
|
|
|
if (data == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(message));
|
|
|
|
}
|
2019-01-12 20:41:08 +00:00
|
|
|
|
|
|
|
var parts = data.Split(' ');
|
2020-06-20 08:35:29 +00:00
|
|
|
if (parts.Length < 2)
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Status line is invalid. Insufficient status parts.", nameof(data));
|
|
|
|
}
|
2019-01-12 20:41:08 +00:00
|
|
|
|
|
|
|
message.Method = new HttpMethod(parts[0].Trim());
|
|
|
|
Uri requestUri;
|
|
|
|
if (Uri.TryCreate(parts[1].Trim(), UriKind.RelativeOrAbsolute, out requestUri))
|
2020-06-20 09:12:36 +00:00
|
|
|
{
|
2019-01-12 20:41:08 +00:00
|
|
|
message.RequestUri = requestUri;
|
2020-06-20 09:12:36 +00:00
|
|
|
}
|
2019-01-12 20:41:08 +00:00
|
|
|
else
|
2020-06-20 09:19:16 +00:00
|
|
|
{
|
2019-01-12 20:41:08 +00:00
|
|
|
System.Diagnostics.Debug.WriteLine(parts[1]);
|
2020-06-20 09:19:16 +00:00
|
|
|
}
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2017-11-27 19:08:55 +00:00
|
|
|
if (parts.Length >= 3)
|
|
|
|
{
|
|
|
|
message.Version = ParseHttpVersion(parts[2].Trim());
|
|
|
|
}
|
|
|
|
}
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2019-01-07 23:24:34 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns a boolean indicating whether the specified HTTP header name represents a content header (true), or a message header (false).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="headerName">A string containing the name of the header to return the type of.</param>
|
|
|
|
protected override bool IsContentHeader(string headerName)
|
|
|
|
{
|
2021-12-20 12:31:07 +00:00
|
|
|
return ContentHeaderNames.Contains(headerName, StringComparison.OrdinalIgnoreCase);
|
2019-01-07 23:24:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|