HttpRequestParser.cs 3.4 KB

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