HttpRequestParser.cs 3.3 KB

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