HttpResponseParser.cs 3.5 KB

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