HttpResponseParser.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using Jellyfin.Extensions;
  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. {
  34. retVal.Dispose();
  35. }
  36. throw;
  37. }
  38. }
  39. /// <summary>
  40. /// Returns a boolean indicating whether the specified HTTP header name represents a content header (true), or a message header (false).
  41. /// </summary>
  42. /// <param name="headerName">A string containing the name of the header to return the type of.</param>
  43. /// <returns>A boolean, true if th specified header relates to HTTP content, otherwise false.</returns>
  44. protected override bool IsContentHeader(string headerName)
  45. {
  46. return ContentHeaderNames.Contains(headerName, StringComparison.OrdinalIgnoreCase);
  47. }
  48. /// <summary>
  49. /// 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"/>.
  50. /// </summary>
  51. /// <param name="data">The first line of the HTTP message to be parsed.</param>
  52. /// <param name="message">Either a <see cref="HttpResponseMessage"/> or <see cref="HttpRequestMessage"/> to assign the parsed values to.</param>
  53. protected override void ParseStatusLine(string data, HttpResponseMessage message)
  54. {
  55. if (data == null)
  56. {
  57. throw new ArgumentNullException(nameof(data));
  58. }
  59. if (message == null)
  60. {
  61. throw new ArgumentNullException(nameof(message));
  62. }
  63. var parts = data.Split(' ');
  64. if (parts.Length < 2)
  65. {
  66. throw new ArgumentException("data status line is invalid. Insufficient status parts.", nameof(data));
  67. }
  68. message.Version = ParseHttpVersion(parts[0].Trim());
  69. if (!Int32.TryParse(parts[1].Trim(), out var statusCode))
  70. {
  71. throw new ArgumentException("data status line is invalid. Status code is not a valid integer.", nameof(data));
  72. }
  73. message.StatusCode = (HttpStatusCode)statusCode;
  74. if (parts.Length >= 3)
  75. {
  76. message.ReasonPhrase = parts[2].Trim();
  77. }
  78. }
  79. }
  80. }