HttpResponseParser.cs 3.5 KB

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