HttpRequestParser.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. Uri requestUri;
  59. if (Uri.TryCreate(parts[1].Trim(), UriKind.RelativeOrAbsolute, out requestUri))
  60. {
  61. message.RequestUri = requestUri;
  62. }
  63. else
  64. {
  65. System.Diagnostics.Debug.WriteLine(parts[1]);
  66. }
  67. if (parts.Length >= 3)
  68. {
  69. message.Version = ParseHttpVersion(parts[2].Trim());
  70. }
  71. }
  72. /// <summary>
  73. /// Returns a boolean indicating whether the specified HTTP header name represents a content header (true), or a message header (false).
  74. /// </summary>
  75. /// <param name="headerName">A string containing the name of the header to return the type of.</param>
  76. protected override bool IsContentHeader(string headerName)
  77. {
  78. return ContentHeaderNames.Contains(headerName, StringComparison.OrdinalIgnoreCase);
  79. }
  80. }
  81. }