HttpResponseParser.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Net.Http;
  5. namespace Rssdp.Infrastructure
  6. {
  7. /// <summary>
  8. /// Parses a string into a <see cref="HttpResponseMessage"/> or throws an exception.
  9. /// </summary>
  10. public sealed class HttpResponseParser : HttpParserBase<HttpResponseMessage>
  11. {
  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. /// <summary>
  17. /// Parses the specified data into a <see cref="HttpResponseMessage"/> instance.
  18. /// </summary>
  19. /// <param name="data">A string containing the data to parse.</param>
  20. /// <returns>A <see cref="HttpResponseMessage"/> instance containing the parsed data.</returns>
  21. public override HttpResponseMessage Parse(string data)
  22. {
  23. HttpResponseMessage retVal = null;
  24. try
  25. {
  26. retVal = new HttpResponseMessage();
  27. Parse(retVal, retVal.Headers, data);
  28. return retVal;
  29. }
  30. catch
  31. {
  32. if (retVal != null)
  33. retVal.Dispose();
  34. throw;
  35. }
  36. }
  37. /// <summary>
  38. /// Returns a boolean indicating whether the specified HTTP header name represents a content header (true), or a message header (false).
  39. /// </summary>
  40. /// <param name="headerName">A string containing the name of the header to return the type of.</param>
  41. /// <returns>A boolean, true if th specified header relates to HTTP content, otherwise false.</returns>
  42. protected override bool IsContentHeader(string headerName)
  43. {
  44. return ContentHeaderNames.Contains(headerName, StringComparer.OrdinalIgnoreCase);
  45. }
  46. /// <summary>
  47. /// 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"/>.
  48. /// </summary>
  49. /// <param name="data">The first line of the HTTP message to be parsed.</param>
  50. /// <param name="message">Either a <see cref="HttpResponseMessage"/> or <see cref="HttpRequestMessage"/> to assign the parsed values to.</param>
  51. protected override void ParseStatusLine(string data, HttpResponseMessage message)
  52. {
  53. if (data == null)
  54. {
  55. throw new ArgumentNullException(nameof(data));
  56. }
  57. if (message == null)
  58. {
  59. throw new ArgumentNullException(nameof(message));
  60. }
  61. var parts = data.Split(' ');
  62. if (parts.Length < 2)
  63. {
  64. throw new ArgumentException("data status line is invalid. Insufficient status parts.", nameof(data));
  65. }
  66. message.Version = ParseHttpVersion(parts[0].Trim());
  67. int statusCode = -1;
  68. if (!Int32.TryParse(parts[1].Trim(), out statusCode))
  69. throw new ArgumentException("data status line is invalid. Status code is not a valid integer.", nameof(data));
  70. message.StatusCode = (HttpStatusCode)statusCode;
  71. if (parts.Length >= 3)
  72. {
  73. message.ReasonPhrase = parts[2].Trim();
  74. }
  75. }
  76. }
  77. }