jellyfin/RSSDP/HttpResponseParser.cs

94 lines
3.4 KiB
C#
Raw Normal View History

2019-01-12 21:41:08 +01:00
using System;
2016-10-30 00:22:20 +02:00
using System.Net;
using System.Net.Http;
2021-12-20 13:31:07 +01:00
using Jellyfin.Extensions;
2016-10-30 00:22:20 +02:00
namespace Rssdp.Infrastructure
{
2019-01-12 21:41:08 +01:00
/// <summary>
2019-01-13 21:37:13 +01:00
/// Parses a string into a <see cref="HttpResponseMessage"/> or throws an exception.
2019-01-12 21:41:08 +01:00
/// </summary>
2019-01-13 21:37:13 +01:00
public sealed class HttpResponseParser : HttpParserBase<HttpResponseMessage>
2019-01-12 21:41:08 +01:00
{
private readonly string[] ContentHeaderNames = new string[]
2020-06-20 08:20:33 +02:00
{
"Allow", "Content-Disposition", "Content-Encoding", "Content-Language", "Content-Length", "Content-Location", "Content-MD5", "Content-Range", "Content-Type", "Expires", "Last-Modified"
};
2019-01-12 21:41:08 +01:00
/// <summary>
2019-01-13 21:37:13 +01:00
/// Parses the specified data into a <see cref="HttpResponseMessage"/> instance.
2019-01-12 21:41:08 +01:00
/// </summary>
/// <param name="data">A string containing the data to parse.</param>
2019-01-13 21:37:13 +01:00
/// <returns>A <see cref="HttpResponseMessage"/> instance containing the parsed data.</returns>
2019-01-12 21:41:08 +01:00
public override HttpResponseMessage Parse(string data)
{
2019-01-13 21:37:13 +01:00
HttpResponseMessage retVal = null;
2019-01-12 21:41:08 +01:00
try
{
2019-01-13 21:37:13 +01:00
retVal = new HttpResponseMessage();
2019-01-12 21:41:08 +01:00
Parse(retVal, retVal.Headers, data);
return retVal;
}
catch
{
if (retVal != null)
2020-06-20 11:12:36 +02:00
{
2019-01-12 21:41:08 +01:00
retVal.Dispose();
2020-06-20 11:12:36 +02:00
}
2019-01-12 21:41:08 +01:00
throw;
}
}
/// <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>
/// <returns>A boolean, true if th specified header relates to HTTP content, otherwise false.</returns>
protected override bool IsContentHeader(string headerName)
{
2021-12-20 13:31:07 +01:00
return ContentHeaderNames.Contains(headerName, StringComparison.OrdinalIgnoreCase);
2019-01-12 21:41:08 +01: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 21:37:13 +01:00
/// <param name="message">Either a <see cref="HttpResponseMessage"/> or <see cref="HttpRequestMessage"/> to assign the parsed values to.</param>
2019-01-12 21:41:08 +01:00
protected override void ParseStatusLine(string data, HttpResponseMessage message)
{
2020-06-20 10:35:29 +02:00
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
2019-01-12 21:41:08 +01:00
var parts = data.Split(' ');
2020-06-20 10:35:29 +02:00
if (parts.Length < 2)
{
throw new ArgumentException("data status line is invalid. Insufficient status parts.", nameof(data));
}
2019-01-12 21:41:08 +01:00
message.Version = ParseHttpVersion(parts[0].Trim());
2023-04-06 19:21:29 +02:00
if (!Int32.TryParse(parts[1].Trim(), out var statusCode))
2020-06-20 11:12:36 +02:00
{
2019-01-12 21:41:08 +01:00
throw new ArgumentException("data status line is invalid. Status code is not a valid integer.", nameof(data));
2020-06-20 11:12:36 +02:00
}
2019-01-12 21:41:08 +01:00
message.StatusCode = (HttpStatusCode)statusCode;
2017-11-27 20:08:55 +01:00
if (parts.Length >= 3)
{
message.ReasonPhrase = parts[2].Trim();
}
}
2019-01-08 00:24:34 +01:00
}
}