HttpRequestParser.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. if (retVal != null)
  32. {
  33. retVal.Dispose();
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// 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"/>.
  39. /// </summary>
  40. /// <param name="data">The first line of the HTTP message to be parsed.</param>
  41. /// <param name="message">Either a <see cref="HttpResponseMessage"/> or <see cref="HttpRequestMessage"/> to assign the parsed values to.</param>
  42. protected override void ParseStatusLine(string data, HttpRequestMessage message)
  43. {
  44. if (data == null)
  45. {
  46. throw new ArgumentNullException(nameof(data));
  47. }
  48. if (message == null)
  49. {
  50. throw new ArgumentNullException(nameof(message));
  51. }
  52. var parts = data.Split(' ');
  53. if (parts.Length < 2)
  54. {
  55. throw new ArgumentException("Status line is invalid. Insufficient status parts.", nameof(data));
  56. }
  57. message.Method = new HttpMethod(parts[0].Trim());
  58. if (Uri.TryCreate(parts[1].Trim(), UriKind.RelativeOrAbsolute, out var requestUri))
  59. {
  60. message.RequestUri = requestUri;
  61. }
  62. else
  63. {
  64. System.Diagnostics.Debug.WriteLine(parts[1]);
  65. }
  66. if (parts.Length >= 3)
  67. {
  68. message.Version = ParseHttpVersion(parts[2].Trim());
  69. }
  70. }
  71. /// <summary>
  72. /// Returns a boolean indicating whether the specified HTTP header name represents a content header (true), or a message header (false).
  73. /// </summary>
  74. /// <param name="headerName">A string containing the name of the header to return the type of.</param>
  75. protected override bool IsContentHeader(string headerName)
  76. {
  77. return ContentHeaderNames.Contains(headerName, StringComparison.OrdinalIgnoreCase);
  78. }
  79. }
  80. }