HttpRequestParser.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Net.Http;
  3. using Jellyfin.Extensions;
  4. namespace Rssdp.Infrastructure
  5. {
  6. /// <summary>
  7. /// Parses a string into a <see cref="HttpRequestMessage"/> or throws an exception.
  8. /// </summary>
  9. public sealed class HttpRequestParser : HttpParserBase<HttpRequestMessage>
  10. {
  11. private readonly string[] ContentHeaderNames = new string[]
  12. {
  13. "Allow", "Content-Disposition", "Content-Encoding", "Content-Language", "Content-Length", "Content-Location", "Content-MD5", "Content-Range", "Content-Type", "Expires", "Last-Modified"
  14. };
  15. /// <summary>
  16. /// Parses the specified data into a <see cref="HttpRequestMessage"/> instance.
  17. /// </summary>
  18. /// <param name="data">A string containing the data to parse.</param>
  19. /// <returns>A <see cref="HttpRequestMessage"/> instance containing the parsed data.</returns>
  20. public override HttpRequestMessage Parse(string data)
  21. {
  22. HttpRequestMessage retVal = null;
  23. try
  24. {
  25. retVal = new HttpRequestMessage();
  26. Parse(retVal, retVal.Headers, data);
  27. return retVal;
  28. }
  29. finally
  30. {
  31. retVal?.Dispose();
  32. }
  33. }
  34. /// <summary>
  35. /// 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"/>.
  36. /// </summary>
  37. /// <param name="data">The first line of the HTTP message to be parsed.</param>
  38. /// <param name="message">Either a <see cref="HttpResponseMessage"/> or <see cref="HttpRequestMessage"/> to assign the parsed values to.</param>
  39. protected override void ParseStatusLine(string data, HttpRequestMessage message)
  40. {
  41. if (data == null)
  42. {
  43. throw new ArgumentNullException(nameof(data));
  44. }
  45. if (message == null)
  46. {
  47. throw new ArgumentNullException(nameof(message));
  48. }
  49. var parts = data.Split(' ');
  50. if (parts.Length < 2)
  51. {
  52. throw new ArgumentException("Status line is invalid. Insufficient status parts.", nameof(data));
  53. }
  54. message.Method = new HttpMethod(parts[0].Trim());
  55. if (Uri.TryCreate(parts[1].Trim(), UriKind.RelativeOrAbsolute, out var requestUri))
  56. {
  57. message.RequestUri = requestUri;
  58. }
  59. else
  60. {
  61. System.Diagnostics.Debug.WriteLine(parts[1]);
  62. }
  63. if (parts.Length >= 3)
  64. {
  65. message.Version = ParseHttpVersion(parts[2].Trim());
  66. }
  67. }
  68. /// <summary>
  69. /// Returns a boolean indicating whether the specified HTTP header name represents a content header (true), or a message header (false).
  70. /// </summary>
  71. /// <param name="headerName">A string containing the name of the header to return the type of.</param>
  72. protected override bool IsContentHeader(string headerName)
  73. {
  74. return ContentHeaderNames.Contains(headerName, StringComparison.OrdinalIgnoreCase);
  75. }
  76. }
  77. }