HttpRequestParser.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. 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. retVal.Dispose();
  33. }
  34. }
  35. /// <summary>
  36. /// 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"/>.
  37. /// </summary>
  38. /// <param name="data">The first line of the HTTP message to be parsed.</param>
  39. /// <param name="message">Either a <see cref="HttpResponseMessage"/> or <see cref="HttpRequestMessage"/> to assign the parsed values to.</param>
  40. protected override void ParseStatusLine(string data, HttpRequestMessage message)
  41. {
  42. if (data == null)
  43. {
  44. throw new ArgumentNullException(nameof(data));
  45. }
  46. if (message == null)
  47. {
  48. throw new ArgumentNullException(nameof(message));
  49. }
  50. var parts = data.Split(' ');
  51. if (parts.Length < 2)
  52. {
  53. throw new ArgumentException("Status line is invalid. Insufficient status parts.", nameof(data));
  54. }
  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. }
  75. }