WebHeaderCollection.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. //
  2. // System.Net.WebHeaderCollection
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Miguel de Icaza ([email protected])
  8. // Marek Safar ([email protected])
  9. //
  10. // Copyright 2003 Ximian, Inc. (http://www.ximian.com)
  11. // Copyright 2007 Novell, Inc. (http://www.novell.com)
  12. // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
  13. //
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System;
  35. using System.Collections;
  36. using System.Collections.Generic;
  37. using System.Collections.Specialized;
  38. using System.Runtime.InteropServices;
  39. using System.Runtime.Serialization;
  40. using System.Text;
  41. // See RFC 2068 par 4.2 Message Headers
  42. namespace System.Net
  43. {
  44. [Serializable]
  45. [ComVisible(true)]
  46. public class WebHeaderCollection : NameValueCollection, ISerializable {
  47. [Flags]
  48. internal enum HeaderInfo
  49. {
  50. Request = 1,
  51. Response = 1 << 1,
  52. MultiValue = 1 << 10
  53. }
  54. static readonly bool[] allowed_chars = {
  55. false, false, false, false, false, false, false, false, false, false, false, false, false, false,
  56. false, false, false, false, false, false, false, false, false, false, false, false, false, false,
  57. false, false, false, false, false, true, false, true, true, true, true, false, false, false, true,
  58. true, false, true, true, false, true, true, true, true, true, true, true, true, true, true, false,
  59. false, false, false, false, false, false, true, true, true, true, true, true, true, true, true,
  60. true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
  61. false, false, false, true, true, true, true, true, true, true, true, true, true, true, true, true,
  62. true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
  63. false, true, false
  64. };
  65. static readonly Dictionary<string, HeaderInfo> headers;
  66. HeaderInfo? headerRestriction;
  67. HeaderInfo? headerConsistency;
  68. static WebHeaderCollection ()
  69. {
  70. headers = new Dictionary<string, HeaderInfo> (StringComparer.OrdinalIgnoreCase) {
  71. { "Allow", HeaderInfo.MultiValue },
  72. { "Accept", HeaderInfo.Request | HeaderInfo.MultiValue },
  73. { "Accept-Charset", HeaderInfo.MultiValue },
  74. { "Accept-Encoding", HeaderInfo.MultiValue },
  75. { "Accept-Language", HeaderInfo.MultiValue },
  76. { "Accept-Ranges", HeaderInfo.MultiValue },
  77. { "Authorization", HeaderInfo.MultiValue },
  78. { "Cache-Control", HeaderInfo.MultiValue },
  79. { "Cookie", HeaderInfo.MultiValue },
  80. { "Connection", HeaderInfo.Request | HeaderInfo.MultiValue },
  81. { "Content-Encoding", HeaderInfo.MultiValue },
  82. { "Content-Length", HeaderInfo.Request | HeaderInfo.Response },
  83. { "Content-Type", HeaderInfo.Request },
  84. { "Content-Language", HeaderInfo.MultiValue },
  85. { "Date", HeaderInfo.Request },
  86. { "Expect", HeaderInfo.Request | HeaderInfo.MultiValue},
  87. { "Host", HeaderInfo.Request },
  88. { "If-Match", HeaderInfo.MultiValue },
  89. { "If-Modified-Since", HeaderInfo.Request },
  90. { "If-None-Match", HeaderInfo.MultiValue },
  91. { "Keep-Alive", HeaderInfo.Response },
  92. { "Pragma", HeaderInfo.MultiValue },
  93. { "Proxy-Authenticate", HeaderInfo.MultiValue },
  94. { "Proxy-Authorization", HeaderInfo.MultiValue },
  95. { "Proxy-Connection", HeaderInfo.Request | HeaderInfo.MultiValue },
  96. { "Range", HeaderInfo.Request | HeaderInfo.MultiValue },
  97. { "Referer", HeaderInfo.Request },
  98. { "Set-Cookie", HeaderInfo.MultiValue },
  99. { "Set-Cookie2", HeaderInfo.MultiValue },
  100. { "TE", HeaderInfo.MultiValue },
  101. { "Trailer", HeaderInfo.MultiValue },
  102. { "Transfer-Encoding", HeaderInfo.Request | HeaderInfo.Response | HeaderInfo.MultiValue },
  103. { "Upgrade", HeaderInfo.MultiValue },
  104. { "User-Agent", HeaderInfo.Request },
  105. { "Vary", HeaderInfo.MultiValue },
  106. { "Via", HeaderInfo.MultiValue },
  107. { "Warning", HeaderInfo.MultiValue },
  108. { "WWW-Authenticate", HeaderInfo.Response | HeaderInfo. MultiValue }
  109. };
  110. }
  111. // Constructors
  112. public WebHeaderCollection ()
  113. {
  114. }
  115. internal WebHeaderCollection(WebHeaderCollectionType type)
  116. {
  117. // m_Type = type;
  118. // if (type == WebHeaderCollectionType.HttpWebResponse)
  119. // m_CommonHeaders = new string[s_CommonHeaderNames.Length - 1]; // Minus one for the sentinel.
  120. }
  121. protected WebHeaderCollection (SerializationInfo serializationInfo,
  122. StreamingContext streamingContext)
  123. {
  124. int count;
  125. try {
  126. count = serializationInfo.GetInt32("Count");
  127. for (int i = 0; i < count; i++)
  128. this.Add (serializationInfo.GetString (i.ToString ()),
  129. serializationInfo.GetString ((count + i).ToString ()));
  130. } catch (SerializationException){
  131. count = serializationInfo.GetInt32("count");
  132. for (int i = 0; i < count; i++)
  133. this.Add (serializationInfo.GetString ("k" + i),
  134. serializationInfo.GetString ("v" + i));
  135. }
  136. }
  137. internal WebHeaderCollection (HeaderInfo headerRestriction)
  138. {
  139. this.headerRestriction = headerRestriction;
  140. }
  141. // Methods
  142. public void Add (string header)
  143. {
  144. if (header == null)
  145. throw new ArgumentNullException ("header");
  146. int pos = header.IndexOf (':');
  147. if (pos == -1)
  148. throw new ArgumentException ("no colon found", "header");
  149. this.Add (header.Substring (0, pos), header.Substring (pos + 1));
  150. }
  151. public override void Add (string name, string value)
  152. {
  153. if (name == null)
  154. throw new ArgumentNullException ("name");
  155. CheckRestrictedHeader (name);
  156. this.AddWithoutValidate (name, value);
  157. }
  158. protected void AddWithoutValidate (string headerName, string headerValue)
  159. {
  160. if (!IsHeaderName (headerName))
  161. throw new ArgumentException ("invalid header name: " + headerName, "headerName");
  162. if (headerValue == null)
  163. headerValue = String.Empty;
  164. else
  165. headerValue = headerValue.Trim ();
  166. if (!IsHeaderValue (headerValue))
  167. throw new ArgumentException ("invalid header value: " + headerValue, "headerValue");
  168. AddInternal (headerName, headerValue);
  169. }
  170. internal void AddInternal (string headerName, string headerValue)
  171. {
  172. base.Add (headerName, headerValue);
  173. }
  174. internal string [] GetValues_internal (string header, bool split)
  175. {
  176. if (header == null)
  177. throw new ArgumentNullException ("header");
  178. string [] values = base.GetValues (header);
  179. if (values == null || values.Length == 0)
  180. return null;
  181. if (split && IsMultiValue (header)) {
  182. List<string> separated = null;
  183. foreach (var value in values) {
  184. if (value.IndexOf (',') < 0) {
  185. if (separated != null)
  186. separated.Add (value);
  187. continue;
  188. }
  189. if (separated == null) {
  190. separated = new List<string> (values.Length + 1);
  191. foreach (var v in values) {
  192. if (v == value)
  193. break;
  194. separated.Add (v);
  195. }
  196. }
  197. var slices = value.Split (',');
  198. var slices_length = slices.Length;
  199. if (value[value.Length - 1] == ',')
  200. --slices_length;
  201. for (int i = 0; i < slices_length; ++i ) {
  202. separated.Add (slices[i].Trim ());
  203. }
  204. }
  205. if (separated != null)
  206. return separated.ToArray ();
  207. }
  208. return values;
  209. }
  210. public override string [] GetValues (string header)
  211. {
  212. return GetValues_internal (header, true);
  213. }
  214. public override string[] GetValues (int index)
  215. {
  216. string[] values = base.GetValues (index);
  217. if (values == null || values.Length == 0) {
  218. return null;
  219. }
  220. return values;
  221. }
  222. public static bool IsRestricted (string headerName)
  223. {
  224. return IsRestricted (headerName, false);
  225. }
  226. public static bool IsRestricted (string headerName, bool response)
  227. {
  228. if (headerName == null)
  229. throw new ArgumentNullException ("headerName");
  230. if (headerName.Length == 0)
  231. throw new ArgumentException ("empty string", "headerName");
  232. if (!IsHeaderName (headerName))
  233. throw new ArgumentException ("Invalid character in header");
  234. HeaderInfo info;
  235. if (!headers.TryGetValue (headerName, out info))
  236. return false;
  237. var flag = response ? HeaderInfo.Response : HeaderInfo.Request;
  238. return (info & flag) != 0;
  239. }
  240. public override void OnDeserialization (object sender)
  241. {
  242. }
  243. public override void Remove (string name)
  244. {
  245. if (name == null)
  246. throw new ArgumentNullException ("name");
  247. CheckRestrictedHeader (name);
  248. base.Remove (name);
  249. }
  250. public override void Set (string name, string value)
  251. {
  252. if (name == null)
  253. throw new ArgumentNullException ("name");
  254. if (!IsHeaderName (name))
  255. throw new ArgumentException ("invalid header name");
  256. if (value == null)
  257. value = String.Empty;
  258. else
  259. value = value.Trim ();
  260. if (!IsHeaderValue (value))
  261. throw new ArgumentException ("invalid header value");
  262. CheckRestrictedHeader (name);
  263. base.Set (name, value);
  264. }
  265. public byte[] ToByteArray ()
  266. {
  267. return Encoding.UTF8.GetBytes(ToString ());
  268. }
  269. internal string ToStringMultiValue ()
  270. {
  271. StringBuilder sb = new StringBuilder();
  272. int count = base.Count;
  273. for (int i = 0; i < count ; i++) {
  274. string key = GetKey (i);
  275. if (IsMultiValue (key)) {
  276. foreach (string v in GetValues (i)) {
  277. sb.Append (key)
  278. .Append (": ")
  279. .Append (v)
  280. .Append ("\r\n");
  281. }
  282. } else {
  283. sb.Append (key)
  284. .Append (": ")
  285. .Append (Get (i))
  286. .Append ("\r\n");
  287. }
  288. }
  289. return sb.Append("\r\n").ToString();
  290. }
  291. public override string ToString ()
  292. {
  293. StringBuilder sb = new StringBuilder();
  294. int count = base.Count;
  295. for (int i = 0; i < count ; i++)
  296. sb.Append (GetKey (i))
  297. .Append (": ")
  298. .Append (Get (i))
  299. .Append ("\r\n");
  300. return sb.Append("\r\n").ToString();
  301. }
  302. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  303. StreamingContext streamingContext)
  304. {
  305. GetObjectData (serializationInfo, streamingContext);
  306. }
  307. public override void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
  308. {
  309. int count = base.Count;
  310. serializationInfo.AddValue ("Count", count);
  311. for (int i = 0; i < count; i++) {
  312. serializationInfo.AddValue (i.ToString (), GetKey (i));
  313. serializationInfo.AddValue ((count + i).ToString (), Get (i));
  314. }
  315. }
  316. public override string[] AllKeys {
  317. get {
  318. return base.AllKeys;
  319. }
  320. }
  321. public override int Count {
  322. get {
  323. return base.Count;
  324. }
  325. }
  326. public override KeysCollection Keys {
  327. get {
  328. return base.Keys;
  329. }
  330. }
  331. public override string Get (int index)
  332. {
  333. return base.Get (index);
  334. }
  335. public override string Get (string name)
  336. {
  337. return base.Get (name);
  338. }
  339. public override string GetKey (int index)
  340. {
  341. return base.GetKey (index);
  342. }
  343. public void Add (HttpRequestHeader header, string value)
  344. {
  345. Add (RequestHeaderToString (header), value);
  346. }
  347. public void Remove (HttpRequestHeader header)
  348. {
  349. Remove (RequestHeaderToString (header));
  350. }
  351. public void Set (HttpRequestHeader header, string value)
  352. {
  353. Set (RequestHeaderToString (header), value);
  354. }
  355. public void Add (HttpResponseHeader header, string value)
  356. {
  357. Add (ResponseHeaderToString (header), value);
  358. }
  359. public void Remove (HttpResponseHeader header)
  360. {
  361. Remove (ResponseHeaderToString (header));
  362. }
  363. public void Set (HttpResponseHeader header, string value)
  364. {
  365. Set (ResponseHeaderToString (header), value);
  366. }
  367. public string this [HttpRequestHeader header] {
  368. get {
  369. return Get (RequestHeaderToString (header));
  370. }
  371. set {
  372. Set (header, value);
  373. }
  374. }
  375. public string this [HttpResponseHeader header] {
  376. get {
  377. return Get (ResponseHeaderToString (header));
  378. }
  379. set {
  380. Set (header, value);
  381. }
  382. }
  383. public override void Clear ()
  384. {
  385. base.Clear ();
  386. }
  387. public override IEnumerator GetEnumerator ()
  388. {
  389. return base.GetEnumerator ();
  390. }
  391. // Internal Methods
  392. // With this we don't check for invalid characters in header. See bug #55994.
  393. internal void SetInternal (string header)
  394. {
  395. int pos = header.IndexOf (':');
  396. if (pos == -1)
  397. throw new ArgumentException ("no colon found", "header");
  398. SetInternal (header.Substring (0, pos), header.Substring (pos + 1));
  399. }
  400. internal void SetInternal (string name, string value)
  401. {
  402. if (value == null)
  403. value = String.Empty;
  404. else
  405. value = value.Trim ();
  406. if (!IsHeaderValue (value))
  407. throw new ArgumentException ("invalid header value");
  408. if (IsMultiValue (name)) {
  409. base.Add (name, value);
  410. } else {
  411. base.Remove (name);
  412. base.Set (name, value);
  413. }
  414. }
  415. internal void RemoveAndAdd (string name, string value)
  416. {
  417. if (value == null)
  418. value = String.Empty;
  419. else
  420. value = value.Trim ();
  421. base.Remove (name);
  422. base.Set (name, value);
  423. }
  424. internal void RemoveInternal (string name)
  425. {
  426. if (name == null)
  427. throw new ArgumentNullException ("name");
  428. base.Remove (name);
  429. }
  430. // Private Methods
  431. string RequestHeaderToString (HttpRequestHeader value)
  432. {
  433. CheckHeaderConsistency (HeaderInfo.Request);
  434. switch (value) {
  435. case HttpRequestHeader.CacheControl:
  436. return "Cache-Control";
  437. case HttpRequestHeader.Connection:
  438. return "Connection";
  439. case HttpRequestHeader.Date:
  440. return "Date";
  441. case HttpRequestHeader.KeepAlive:
  442. return "Keep-Alive";
  443. case HttpRequestHeader.Pragma:
  444. return "Pragma";
  445. case HttpRequestHeader.Trailer:
  446. return "Trailer";
  447. case HttpRequestHeader.TransferEncoding:
  448. return "Transfer-Encoding";
  449. case HttpRequestHeader.Upgrade:
  450. return "Upgrade";
  451. case HttpRequestHeader.Via:
  452. return "Via";
  453. case HttpRequestHeader.Warning:
  454. return "Warning";
  455. case HttpRequestHeader.Allow:
  456. return "Allow";
  457. case HttpRequestHeader.ContentLength:
  458. return "Content-Length";
  459. case HttpRequestHeader.ContentType:
  460. return "Content-Type";
  461. case HttpRequestHeader.ContentEncoding:
  462. return "Content-Encoding";
  463. case HttpRequestHeader.ContentLanguage:
  464. return "Content-Language";
  465. case HttpRequestHeader.ContentLocation:
  466. return "Content-Location";
  467. case HttpRequestHeader.ContentMd5:
  468. return "Content-MD5";
  469. case HttpRequestHeader.ContentRange:
  470. return "Content-Range";
  471. case HttpRequestHeader.Expires:
  472. return "Expires";
  473. case HttpRequestHeader.LastModified:
  474. return "Last-Modified";
  475. case HttpRequestHeader.Accept:
  476. return "Accept";
  477. case HttpRequestHeader.AcceptCharset:
  478. return "Accept-Charset";
  479. case HttpRequestHeader.AcceptEncoding:
  480. return "Accept-Encoding";
  481. case HttpRequestHeader.AcceptLanguage:
  482. return "accept-language";
  483. case HttpRequestHeader.Authorization:
  484. return "Authorization";
  485. case HttpRequestHeader.Cookie:
  486. return "Cookie";
  487. case HttpRequestHeader.Expect:
  488. return "Expect";
  489. case HttpRequestHeader.From:
  490. return "From";
  491. case HttpRequestHeader.Host:
  492. return "Host";
  493. case HttpRequestHeader.IfMatch:
  494. return "If-Match";
  495. case HttpRequestHeader.IfModifiedSince:
  496. return "If-Modified-Since";
  497. case HttpRequestHeader.IfNoneMatch:
  498. return "If-None-Match";
  499. case HttpRequestHeader.IfRange:
  500. return "If-Range";
  501. case HttpRequestHeader.IfUnmodifiedSince:
  502. return "If-Unmodified-Since";
  503. case HttpRequestHeader.MaxForwards:
  504. return "Max-Forwards";
  505. case HttpRequestHeader.ProxyAuthorization:
  506. return "Proxy-Authorization";
  507. case HttpRequestHeader.Referer:
  508. return "Referer";
  509. case HttpRequestHeader.Range:
  510. return "Range";
  511. case HttpRequestHeader.Te:
  512. return "TE";
  513. case HttpRequestHeader.Translate:
  514. return "Translate";
  515. case HttpRequestHeader.UserAgent:
  516. return "User-Agent";
  517. default:
  518. throw new InvalidOperationException ();
  519. }
  520. }
  521. string ResponseHeaderToString (HttpResponseHeader value)
  522. {
  523. CheckHeaderConsistency (HeaderInfo.Response);
  524. switch (value) {
  525. case HttpResponseHeader.CacheControl:
  526. return "Cache-Control";
  527. case HttpResponseHeader.Connection:
  528. return "Connection";
  529. case HttpResponseHeader.Date:
  530. return "Date";
  531. case HttpResponseHeader.KeepAlive:
  532. return "Keep-Alive";
  533. case HttpResponseHeader.Pragma:
  534. return "Pragma";
  535. case HttpResponseHeader.Trailer:
  536. return "Trailer";
  537. case HttpResponseHeader.TransferEncoding:
  538. return "Transfer-Encoding";
  539. case HttpResponseHeader.Upgrade:
  540. return "Upgrade";
  541. case HttpResponseHeader.Via:
  542. return "Via";
  543. case HttpResponseHeader.Warning:
  544. return "Warning";
  545. case HttpResponseHeader.Allow:
  546. return "Allow";
  547. case HttpResponseHeader.ContentLength:
  548. return "Content-Length";
  549. case HttpResponseHeader.ContentType:
  550. return "Content-Type";
  551. case HttpResponseHeader.ContentEncoding:
  552. return "Content-Encoding";
  553. case HttpResponseHeader.ContentLanguage:
  554. return "Content-Language";
  555. case HttpResponseHeader.ContentLocation:
  556. return "Content-Location";
  557. case HttpResponseHeader.ContentMd5:
  558. return "Content-MD5";
  559. case HttpResponseHeader.ContentRange:
  560. return "Content-Range";
  561. case HttpResponseHeader.Expires:
  562. return "Expires";
  563. case HttpResponseHeader.LastModified:
  564. return "Last-Modified";
  565. case HttpResponseHeader.AcceptRanges:
  566. return "Accept-Ranges";
  567. case HttpResponseHeader.Age:
  568. return "Age";
  569. case HttpResponseHeader.ETag:
  570. return "ETag";
  571. case HttpResponseHeader.Location:
  572. return "Location";
  573. case HttpResponseHeader.ProxyAuthenticate:
  574. return "Proxy-Authenticate";
  575. case HttpResponseHeader.RetryAfter:
  576. return "Retry-After";
  577. case HttpResponseHeader.Server:
  578. return "Server";
  579. case HttpResponseHeader.SetCookie:
  580. return "Set-Cookie";
  581. case HttpResponseHeader.Vary:
  582. return "Vary";
  583. case HttpResponseHeader.WwwAuthenticate:
  584. return "WWW-Authenticate";
  585. default:
  586. throw new InvalidOperationException ();
  587. }
  588. }
  589. void CheckRestrictedHeader (string headerName)
  590. {
  591. if (!headerRestriction.HasValue)
  592. return;
  593. HeaderInfo info;
  594. if (!headers.TryGetValue (headerName, out info))
  595. return;
  596. if ((info & headerRestriction.Value) != 0)
  597. throw new ArgumentException ("This header must be modified with the appropriate property.");
  598. }
  599. void CheckHeaderConsistency (HeaderInfo value)
  600. {
  601. if (!headerConsistency.HasValue) {
  602. headerConsistency = value;
  603. return;
  604. }
  605. if ((headerConsistency & value) == 0)
  606. throw new InvalidOperationException ();
  607. }
  608. internal static bool IsMultiValue (string headerName)
  609. {
  610. if (headerName == null)
  611. return false;
  612. HeaderInfo info;
  613. return headers.TryGetValue (headerName, out info) && (info & HeaderInfo.MultiValue) != 0;
  614. }
  615. internal static bool IsHeaderValue (string value)
  616. {
  617. // TEXT any 8 bit value except CTL's (0-31 and 127)
  618. // but including \r\n space and \t
  619. // after a newline at least one space or \t must follow
  620. // certain header fields allow comments ()
  621. int len = value.Length;
  622. for (int i = 0; i < len; i++) {
  623. char c = value [i];
  624. if (c == 127)
  625. return false;
  626. if (c < 0x20 && (c != '\r' && c != '\n' && c != '\t'))
  627. return false;
  628. if (c == '\n' && ++i < len) {
  629. c = value [i];
  630. if (c != ' ' && c != '\t')
  631. return false;
  632. }
  633. }
  634. return true;
  635. }
  636. internal static bool IsHeaderName (string name)
  637. {
  638. if (name == null || name.Length == 0)
  639. return false;
  640. int len = name.Length;
  641. for (int i = 0; i < len; i++) {
  642. char c = name [i];
  643. if (c > 126 || !allowed_chars [c])
  644. return false;
  645. }
  646. return true;
  647. }
  648. }
  649. }