HttpResponseParser.cs 3.4 KB

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