HttpParserBase.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Text;
  6. using System.IO;
  7. namespace Rssdp.Infrastructure
  8. {
  9. /// <summary>
  10. /// A base class for the <see cref="HttpResponseParser"/> and <see cref="HttpRequestParser"/> classes. Not intended for direct use.
  11. /// </summary>
  12. /// <typeparam name="T"></typeparam>
  13. public abstract class HttpParserBase<T> where T : new()
  14. {
  15. #region Fields
  16. private readonly string[] LineTerminators = new string[] { "\r\n", "\n" };
  17. private readonly char[] SeparatorCharacters = new char[] { ',', ';' };
  18. #endregion
  19. #region Public Methods
  20. /// <summary>
  21. /// Parses the <paramref name="data"/> provided into either a <see cref="HttpRequestMessage"/> or <see cref="HttpResponseMessage"/> object.
  22. /// </summary>
  23. /// <param name="data">A string containing the HTTP message to parse.</param>
  24. /// <returns>Either a <see cref="HttpRequestMessage"/> or <see cref="HttpResponseMessage"/> object containing the parsed data.</returns>
  25. public abstract T Parse(string data);
  26. /// <summary>
  27. /// Parses a string containing either an HTTP request or response into a <see cref="HttpRequestMessage"/> or <see cref="HttpResponseMessage"/> object.
  28. /// </summary>
  29. /// <param name="message">A <see cref="HttpRequestMessage"/> or <see cref="HttpResponseMessage"/> object representing the parsed message.</param>
  30. /// <param name="headers">A reference to the <see cref="System.Net.Http.Headers.HttpHeaders"/> collection for the <paramref name="message"/> object.</param>
  31. /// <param name="data">A string containing the data to be parsed.</param>
  32. /// <returns>An <see cref="HttpContent"/> object containing the content of the parsed message.</returns>
  33. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Honestly, it's fine. MemoryStream doesn't mind.")]
  34. protected virtual void Parse(T message, System.Net.Http.Headers.HttpHeaders headers, string data)
  35. {
  36. if (data == null) throw new ArgumentNullException(nameof(data));
  37. if (data.Length == 0) throw new ArgumentException("data cannot be an empty string.", nameof(data));
  38. if (!LineTerminators.Any(data.Contains)) throw new ArgumentException("data is not a valid request, it does not contain any CRLF/LF terminators.", nameof(data));
  39. using (var retVal = new ByteArrayContent(Array.Empty<byte>()))
  40. {
  41. var lines = data.Split(LineTerminators, StringSplitOptions.None);
  42. //First line is the 'request' line containing http protocol details like method, uri, http version etc.
  43. ParseStatusLine(lines[0], message);
  44. ParseHeaders(headers, retVal.Headers, lines);
  45. }
  46. }
  47. /// <summary>
  48. /// 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"/>.
  49. /// </summary>
  50. /// <param name="data">The first line of the HTTP message to be parsed.</param>
  51. /// <param name="message">Either a <see cref="HttpResponseMessage"/> or <see cref="HttpRequestMessage"/> to assign the parsed values to.</param>
  52. protected abstract void ParseStatusLine(string data, T message);
  53. /// <summary>
  54. /// Returns a boolean indicating whether the specified HTTP header name represents a content header (true), or a message header (false).
  55. /// </summary>
  56. /// <param name="headerName">A string containing the name of the header to return the type of.</param>
  57. protected abstract bool IsContentHeader(string headerName);
  58. /// <summary>
  59. /// Parses the HTTP version text from an HTTP request or response status line and returns a <see cref="Version"/> object representing the parsed values.
  60. /// </summary>
  61. /// <param name="versionData">A string containing the HTTP version, from the message status line.</param>
  62. /// <returns>A <see cref="Version"/> object containing the parsed version data.</returns>
  63. protected Version ParseHttpVersion(string versionData)
  64. {
  65. if (versionData == null) throw new ArgumentNullException(nameof(versionData));
  66. var versionSeparatorIndex = versionData.IndexOf('/');
  67. if (versionSeparatorIndex <= 0 || versionSeparatorIndex == versionData.Length) throw new ArgumentException("request header line is invalid. Http Version not supplied or incorrect format.", nameof(versionData));
  68. return Version.Parse(versionData.Substring(versionSeparatorIndex + 1));
  69. }
  70. #endregion
  71. #region Private Methods
  72. /// <summary>
  73. /// Parses a line from an HTTP request or response message containing a header name and value pair.
  74. /// </summary>
  75. /// <param name="line">A string containing the data to be parsed.</param>
  76. /// <param name="headers">A reference to a <see cref="System.Net.Http.Headers.HttpHeaders"/> collection to which the parsed header will be added.</param>
  77. /// <param name="contentHeaders">A reference to a <see cref="System.Net.Http.Headers.HttpHeaders"/> collection for the message content, to which the parsed header will be added.</param>
  78. private void ParseHeader(string line, System.Net.Http.Headers.HttpHeaders headers, System.Net.Http.Headers.HttpHeaders contentHeaders)
  79. {
  80. //Header format is
  81. //name: value
  82. var headerKeySeparatorIndex = line.IndexOf(":", StringComparison.OrdinalIgnoreCase);
  83. var headerName = line.Substring(0, headerKeySeparatorIndex).Trim();
  84. var headerValue = line.Substring(headerKeySeparatorIndex + 1).Trim();
  85. //Not sure how to determine where request headers and and content headers begin,
  86. //at least not without a known set of headers (general headers first the content headers)
  87. //which seems like a bad way of doing it. So we'll assume if it's a known content header put it there
  88. //else use request headers.
  89. var values = ParseValues(headerValue);
  90. var headersToAddTo = IsContentHeader(headerName) ? contentHeaders : headers;
  91. if (values.Count > 1)
  92. headersToAddTo.TryAddWithoutValidation(headerName, values);
  93. else
  94. headersToAddTo.TryAddWithoutValidation(headerName, values.First());
  95. }
  96. private int ParseHeaders(System.Net.Http.Headers.HttpHeaders headers, System.Net.Http.Headers.HttpHeaders contentHeaders, string[] lines)
  97. {
  98. //Blank line separates headers from content, so read headers until we find blank line.
  99. int lineIndex = 1;
  100. string line = null, nextLine = null;
  101. while (lineIndex + 1 < lines.Length && !String.IsNullOrEmpty((line = lines[lineIndex++])))
  102. {
  103. //If the following line starts with space or tab (or any whitespace), it is really part of this header but split for human readability.
  104. //Combine these lines into a single comma separated style header for easier parsing.
  105. while (lineIndex < lines.Length && !String.IsNullOrEmpty((nextLine = lines[lineIndex])))
  106. {
  107. if (nextLine.Length > 0 && Char.IsWhiteSpace(nextLine[0]))
  108. {
  109. line += "," + nextLine.TrimStart();
  110. lineIndex++;
  111. }
  112. else
  113. break;
  114. }
  115. ParseHeader(line, headers, contentHeaders);
  116. }
  117. return lineIndex;
  118. }
  119. private IList<string> ParseValues(string headerValue)
  120. {
  121. // This really should be better and match the HTTP 1.1 spec,
  122. // but this should actually be good enough for SSDP implementations
  123. // I think.
  124. var values = new List<string>();
  125. if (headerValue == "\"\"")
  126. {
  127. values.Add(String.Empty);
  128. return values;
  129. }
  130. var indexOfSeparator = headerValue.IndexOfAny(SeparatorCharacters);
  131. if (indexOfSeparator <= 0)
  132. values.Add(headerValue);
  133. else
  134. {
  135. var segments = headerValue.Split(SeparatorCharacters);
  136. if (headerValue.Contains("\""))
  137. {
  138. for (int segmentIndex = 0; segmentIndex < segments.Length; segmentIndex++)
  139. {
  140. var segment = segments[segmentIndex];
  141. if (segment.Trim().StartsWith("\"", StringComparison.OrdinalIgnoreCase))
  142. segment = CombineQuotedSegments(segments, ref segmentIndex, segment);
  143. values.Add(segment);
  144. }
  145. }
  146. else
  147. values.AddRange(segments);
  148. }
  149. return values;
  150. }
  151. private string CombineQuotedSegments(string[] segments, ref int segmentIndex, string segment)
  152. {
  153. var trimmedSegment = segment.Trim();
  154. for (int index = segmentIndex; index < segments.Length; index++)
  155. {
  156. if (trimmedSegment == "\"\"" ||
  157. (
  158. trimmedSegment.EndsWith("\"", StringComparison.OrdinalIgnoreCase)
  159. && !trimmedSegment.EndsWith("\"\"", StringComparison.OrdinalIgnoreCase)
  160. && !trimmedSegment.EndsWith("\\\"", StringComparison.OrdinalIgnoreCase))
  161. )
  162. {
  163. segmentIndex = index;
  164. return trimmedSegment.Substring(1, trimmedSegment.Length - 2);
  165. }
  166. if (index + 1 < segments.Length)
  167. trimmedSegment += "," + segments[index + 1].TrimEnd();
  168. }
  169. segmentIndex = segments.Length;
  170. if (trimmedSegment.StartsWith("\"", StringComparison.OrdinalIgnoreCase) && trimmedSegment.EndsWith("\"", StringComparison.OrdinalIgnoreCase))
  171. return trimmedSegment.Substring(1, trimmedSegment.Length - 2);
  172. else
  173. return trimmedSegment;
  174. }
  175. #endregion
  176. }
  177. }