XHRWebHeaderCollection.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //Copyright 2010 Microsoft Corporation
  2. //
  3. //Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  4. //You may obtain a copy of the License at
  5. //
  6. //http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. //Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
  9. //"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. //See the License for the specific language governing permissions and limitations under the License.
  11. namespace System.Data.Services.Http
  12. {
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Diagnostics;
  16. internal sealed class XHRWebHeaderCollection : WebHeaderCollection
  17. {
  18. private const int ApproxHighAvgNumHeaders = 16;
  19. private static readonly System.Data.Services.Http.HeaderInfoTable headerTable = new System.Data.Services.Http.HeaderInfoTable();
  20. private NameValueFromDictionary innerCollection;
  21. private System.Data.Services.Http.WebHeaderCollectionType collectionType;
  22. public XHRWebHeaderCollection() : this(System.Data.Services.Http.WebHeaderCollectionType.Unknown)
  23. {
  24. }
  25. internal XHRWebHeaderCollection(System.Data.Services.Http.WebHeaderCollectionType type)
  26. {
  27. this.collectionType = type;
  28. }
  29. #region Properties.
  30. public override int Count
  31. {
  32. get
  33. {
  34. return this.InnerCollection.Count;
  35. }
  36. }
  37. public override ICollection<string> AllKeys
  38. {
  39. get
  40. {
  41. return this.InnerCollection.Keys;
  42. }
  43. }
  44. private bool AllowHttpRequestHeader
  45. {
  46. get
  47. {
  48. if (this.collectionType == System.Data.Services.Http.WebHeaderCollectionType.Unknown)
  49. {
  50. this.collectionType = System.Data.Services.Http.WebHeaderCollectionType.WebRequest;
  51. }
  52. return
  53. ((this.collectionType == System.Data.Services.Http.WebHeaderCollectionType.WebRequest) ||
  54. (this.collectionType == System.Data.Services.Http.WebHeaderCollectionType.HttpWebRequest));
  55. }
  56. }
  57. private NameValueFromDictionary InnerCollection
  58. {
  59. get
  60. {
  61. if (this.innerCollection == null)
  62. {
  63. this.innerCollection = new NameValueFromDictionary(ApproxHighAvgNumHeaders, System.Data.Services.Http.CaseInsensitiveAscii.StaticInstance);
  64. }
  65. return this.innerCollection;
  66. }
  67. }
  68. public override string this[string name]
  69. {
  70. get
  71. {
  72. return this.InnerCollection.Get(name);
  73. }
  74. set
  75. {
  76. name = System.Data.Services.Http.ValidationHelper.CheckBadChars(name, false);
  77. value = System.Data.Services.Http.ValidationHelper.CheckBadChars(value, true);
  78. this.ThrowOnRestrictedHeader(name);
  79. this.InnerCollection.Set(name, value);
  80. }
  81. }
  82. public override string this[System.Data.Services.Http.HttpRequestHeader header]
  83. {
  84. get
  85. {
  86. if (!this.AllowHttpRequestHeader)
  87. {
  88. throw new InvalidOperationException(
  89. System.Data.Services.Client.Strings.HttpWeb_Internal("WebHeaderCollection.this[HttpRequestHeader].get"));
  90. }
  91. return this[HttpHeaderToName.RequestHeaderNames[header]];
  92. }
  93. set
  94. {
  95. if (!this.AllowHttpRequestHeader)
  96. {
  97. throw new InvalidOperationException(
  98. System.Data.Services.Client.Strings.HttpWeb_Internal("WebHeaderCollection.this[HttpRequestHeader].set"));
  99. }
  100. this[HttpHeaderToName.RequestHeaderNames[header]] = value;
  101. }
  102. }
  103. #endregion Properties.
  104. internal void Add(string name, string value)
  105. {
  106. Debug.Assert(name != null, "name != null");
  107. Debug.Assert(value != null, "value != null");
  108. this.InnerCollection.Add(name, value);
  109. }
  110. internal void SetSpecialHeader(string headerName, string value)
  111. {
  112. Debug.Assert(headerName != null, "headerName != null");
  113. value = System.Data.Services.Http.ValidationHelper.CheckBadChars(value, true);
  114. this.InnerCollection.Remove(headerName);
  115. if (value.Length != 0)
  116. {
  117. this.InnerCollection.Add(headerName, value);
  118. }
  119. }
  120. internal DataParseStatus ParseHeaders(
  121. byte[] byteBuffer,
  122. int size,
  123. ref int unparsed,
  124. ref int totalResponseHeadersLength,
  125. int maximumResponseHeadersLength,
  126. ref WebParseError parseError)
  127. {
  128. if (byteBuffer.Length < size)
  129. {
  130. return DataParseStatus.NeedMoreData;
  131. }
  132. char ch;
  133. int headerNameStartOffset = -1;
  134. int headerNameEndOffset = -1;
  135. int headerValueStartOffset = -1;
  136. int headerValueEndOffset = -1;
  137. int numberOfLf = -1;
  138. int index = unparsed;
  139. bool spaceAfterLf;
  140. string headerMultiLineValue;
  141. string headerName;
  142. string headerValue;
  143. int localTotalResponseHeadersLength = totalResponseHeadersLength;
  144. WebParseErrorCode parseErrorCode = WebParseErrorCode.Generic;
  145. DataParseStatus parseStatus = DataParseStatus.Invalid;
  146. for (;;)
  147. {
  148. headerName = string.Empty;
  149. headerValue = string.Empty;
  150. spaceAfterLf = false;
  151. headerMultiLineValue = null;
  152. if (this.Count == 0)
  153. {
  154. while (index < size)
  155. {
  156. ch = (char)byteBuffer[index];
  157. if (ch == ' ' || ch == '\t')
  158. {
  159. ++index;
  160. if (maximumResponseHeadersLength >= 0 && ++localTotalResponseHeadersLength >= maximumResponseHeadersLength)
  161. {
  162. parseStatus = DataParseStatus.DataTooBig;
  163. goto quit;
  164. }
  165. }
  166. else
  167. {
  168. break;
  169. }
  170. }
  171. if (index == size)
  172. {
  173. parseStatus = DataParseStatus.NeedMoreData;
  174. goto quit;
  175. }
  176. }
  177. headerNameStartOffset = index;
  178. while (index < size)
  179. {
  180. ch = (char)byteBuffer[index];
  181. if (ch != ':' && ch != '\n')
  182. {
  183. if (ch > ' ')
  184. {
  185. headerNameEndOffset = index;
  186. }
  187. ++index;
  188. if (maximumResponseHeadersLength >= 0 && ++localTotalResponseHeadersLength >= maximumResponseHeadersLength)
  189. {
  190. parseStatus = DataParseStatus.DataTooBig;
  191. goto quit;
  192. }
  193. }
  194. else
  195. {
  196. if (ch == ':')
  197. {
  198. ++index;
  199. if (maximumResponseHeadersLength >= 0 && ++localTotalResponseHeadersLength >= maximumResponseHeadersLength)
  200. {
  201. parseStatus = DataParseStatus.DataTooBig;
  202. goto quit;
  203. }
  204. }
  205. break;
  206. }
  207. }
  208. if (index == size)
  209. {
  210. parseStatus = DataParseStatus.NeedMoreData;
  211. goto quit;
  212. }
  213. startOfValue:
  214. numberOfLf = (this.Count == 0 && headerNameEndOffset < 0) ? 1 : 0;
  215. while (index < size && numberOfLf < 2)
  216. {
  217. ch = (char)byteBuffer[index];
  218. if (ch <= ' ')
  219. {
  220. if (ch == '\n')
  221. {
  222. numberOfLf++;
  223. if (numberOfLf == 1)
  224. {
  225. if (index + 1 == size)
  226. {
  227. parseStatus = DataParseStatus.NeedMoreData;
  228. goto quit;
  229. }
  230. spaceAfterLf = (char)byteBuffer[index + 1] == ' ' || (char)byteBuffer[index + 1] == '\t';
  231. }
  232. }
  233. ++index;
  234. if (maximumResponseHeadersLength >= 0 && ++localTotalResponseHeadersLength >= maximumResponseHeadersLength)
  235. {
  236. parseStatus = DataParseStatus.DataTooBig;
  237. goto quit;
  238. }
  239. }
  240. else
  241. {
  242. break;
  243. }
  244. }
  245. if (numberOfLf == 2 || (numberOfLf == 1 && !spaceAfterLf))
  246. {
  247. goto addHeader;
  248. }
  249. if (index == size)
  250. {
  251. parseStatus = DataParseStatus.NeedMoreData;
  252. goto quit;
  253. }
  254. headerValueStartOffset = index;
  255. while (index < size)
  256. {
  257. ch = (char)byteBuffer[index];
  258. if (ch != '\n')
  259. {
  260. if (ch > ' ')
  261. {
  262. headerValueEndOffset = index;
  263. }
  264. ++index;
  265. if (maximumResponseHeadersLength >= 0 && ++localTotalResponseHeadersLength >= maximumResponseHeadersLength)
  266. {
  267. parseStatus = DataParseStatus.DataTooBig;
  268. goto quit;
  269. }
  270. }
  271. else
  272. {
  273. break;
  274. }
  275. }
  276. if (index == size)
  277. {
  278. parseStatus = DataParseStatus.NeedMoreData;
  279. goto quit;
  280. }
  281. numberOfLf = 0;
  282. while (index < size && numberOfLf < 2)
  283. {
  284. ch = (char)byteBuffer[index];
  285. if (ch == '\r' || ch == '\n')
  286. {
  287. if (ch == '\n')
  288. {
  289. numberOfLf++;
  290. }
  291. ++index;
  292. if (maximumResponseHeadersLength >= 0 && ++localTotalResponseHeadersLength >= maximumResponseHeadersLength)
  293. {
  294. parseStatus = DataParseStatus.DataTooBig;
  295. goto quit;
  296. }
  297. }
  298. else
  299. {
  300. break;
  301. }
  302. }
  303. if (index == size && numberOfLf < 2)
  304. {
  305. parseStatus = DataParseStatus.NeedMoreData;
  306. goto quit;
  307. }
  308. addHeader:
  309. if (headerValueStartOffset >= 0 && headerValueStartOffset > headerNameEndOffset && headerValueEndOffset >= headerValueStartOffset)
  310. {
  311. headerValue = System.Text.Encoding.UTF8.GetString(byteBuffer, headerValueStartOffset, headerValueEndOffset - headerValueStartOffset + 1);
  312. }
  313. headerMultiLineValue = (headerMultiLineValue == null ? headerValue : headerMultiLineValue + " " + headerValue);
  314. if (index < size && numberOfLf == 1)
  315. {
  316. ch = (char)byteBuffer[index];
  317. if (ch == ' ' || ch == '\t')
  318. {
  319. ++index;
  320. if (maximumResponseHeadersLength >= 0 && ++localTotalResponseHeadersLength >= maximumResponseHeadersLength)
  321. {
  322. parseStatus = DataParseStatus.DataTooBig;
  323. goto quit;
  324. }
  325. goto startOfValue;
  326. }
  327. }
  328. if (headerNameStartOffset >= 0 && headerNameEndOffset >= headerNameStartOffset)
  329. {
  330. headerName = System.Text.Encoding.UTF8.GetString(byteBuffer, headerNameStartOffset, headerNameEndOffset - headerNameStartOffset + 1);
  331. }
  332. if (headerName.Length > 0)
  333. {
  334. this.Add(headerName, headerMultiLineValue);
  335. }
  336. totalResponseHeadersLength = localTotalResponseHeadersLength;
  337. unparsed = index;
  338. if (numberOfLf == 2)
  339. {
  340. parseStatus = DataParseStatus.Done;
  341. goto quit;
  342. }
  343. }
  344. quit:
  345. if (parseStatus == DataParseStatus.Invalid)
  346. {
  347. parseError.Section = WebParseErrorSection.ResponseHeader;
  348. parseError.Code = parseErrorCode;
  349. }
  350. return parseStatus;
  351. }
  352. private void ThrowOnRestrictedHeader(string headerName)
  353. {
  354. if ((this.collectionType == System.Data.Services.Http.WebHeaderCollectionType.HttpWebRequest) && headerTable[headerName].IsRequestRestricted)
  355. {
  356. throw new InvalidOperationException(
  357. System.Data.Services.Client.Strings.HttpWeb_Internal("WebHeaderCollection.ThrowOnRestrictedHeader"));
  358. }
  359. }
  360. }
  361. }