The Labs \ Source Viewer \ SSCLI \ System.Net \ HeaderInfoTable

  1. //------------------------------------------------------------------------------
  2. // <copyright file="_HeaderInfoTable.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Net
  16. {
  17.    
  18.     using System.Collections;
  19.     using System.Collections.Specialized;
  20.     using System.Globalization;
  21.    
  22.     internal class HeaderInfoTable
  23.     {
  24.        
  25.         private static Hashtable HeaderHashTable;
  26.         private static HeaderInfo UnknownHeaderInfo = new HeaderInfo(string.Empty, false, false, false, SingleParser);
  27.         private static HeaderParser SingleParser = new HeaderParser(ParseSingleValue);
  28.         private static HeaderParser MultiParser = new HeaderParser(ParseMultiValue);
  29.        
  30.         private static string[] ParseSingleValue(string value)
  31.         {
  32.             return new string[1] {value};
  33.         }
  34.        
  35.         //
  36.         //
  37.        
  38.         private static string[] ParseMultiValue(string value)
  39.         {
  40.             StringCollection tempStringCollection = new StringCollection();
  41.            
  42.             bool inquote = false;
  43.             int chIndex = 0;
  44.             char[] vp = new char[value.Length];
  45.             string singleValue;
  46.            
  47.             for (int i = 0; i < value.Length; i++) {
  48.                 if (value[i] == '"') {
  49.                     inquote = !inquote;
  50.                 }
  51.                 else if ((value[i] == ',') && !inquote) {
  52.                     singleValue = new string(vp, 0, chIndex);
  53.                     tempStringCollection.Add(singleValue.Trim());
  54.                     chIndex = 0;
  55.                     continue;
  56.                 }
  57.                 vp[chIndex++] = value[i];
  58.             }
  59.            
  60.             //
  61.             // Now add the last of the header values to the stringtable.
  62.             //
  63.            
  64.             if (chIndex != 0) {
  65.                 singleValue = new string(vp, 0, chIndex);
  66.                 tempStringCollection.Add(singleValue.Trim());
  67.             }
  68.            
  69.             string[] stringArray = new string[tempStringCollection.Count];
  70.             tempStringCollection.CopyTo(stringArray, 0);
  71.             return stringArray;
  72.         }
  73.        
  74.         static HeaderInfoTable()
  75.         {
  76.            
  77.             HeaderInfo[] InfoArray = new HeaderInfo[] {new HeaderInfo(HttpKnownHeaderNames.Age, false, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.Allow, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.Accept, true, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.Authorization, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.AcceptRanges, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.AcceptCharset, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.AcceptEncoding, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.AcceptLanguage, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.Cookie, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.Connection, true, false, true, MultiParser),
  78.             new HeaderInfo(HttpKnownHeaderNames.ContentMD5, false, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.ContentType, true, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.CacheControl, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.ContentRange, false, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.ContentLength, true, true, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.ContentEncoding, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.ContentLanguage, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.ContentLocation, false, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.Date, true, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.ETag, false, false, false, SingleParser),
  79.             new HeaderInfo(HttpKnownHeaderNames.Expect, true, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.Expires, false, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.From, false, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.Host, true, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.IfMatch, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.IfRange, false, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.IfNoneMatch, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.IfModifiedSince, true, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.IfUnmodifiedSince, false, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.KeepAlive, false, true, false, SingleParser),
  80.             new HeaderInfo(HttpKnownHeaderNames.Location, false, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.LastModified, false, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.MaxForwards, false, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.Pragma, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.ProxyAuthenticate, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.ProxyAuthorization, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.ProxyConnection, true, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.Range, true, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.Referer, true, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.RetryAfter, false, false, false, SingleParser),
  81.             new HeaderInfo(HttpKnownHeaderNames.Server, false, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.SetCookie, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.SetCookie2, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.TE, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.Trailer, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.TransferEncoding, true, true, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.Upgrade, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.UserAgent, true, false, false, SingleParser), new HeaderInfo(HttpKnownHeaderNames.Via, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.Vary, false, false, true, MultiParser),
  82.             new HeaderInfo(HttpKnownHeaderNames.Warning, false, false, true, MultiParser), new HeaderInfo(HttpKnownHeaderNames.WWWAuthenticate, false, true, true, SingleParser)};
  83.            
  84.             HeaderHashTable = new Hashtable(InfoArray.Length * 2, CaseInsensitiveAscii.StaticInstance);
  85.             for (int i = 0; i < InfoArray.Length; i++) {
  86.                 HeaderHashTable[InfoArray[i].HeaderName] = InfoArray[i];
  87.             }
  88.         }
  89.        
  90.         internal HeaderInfo this[string name]
  91.         {
  92.             get {
  93.                 HeaderInfo tempHeaderInfo = (HeaderInfo)HeaderHashTable[name];
  94.                 if (tempHeaderInfo == null) {
  95.                     return UnknownHeaderInfo;
  96.                 }
  97.                 return tempHeaderInfo;
  98.             }
  99.         }
  100.        
  101.     }
  102.    
  103. }

Developer Fusion