ServerVariablesCollection.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // System.Web.ServerVariablesCollection
  3. //
  4. // Authors:
  5. // Alon Gazit ([email protected])
  6. //
  7. // (c) 2004 Mainsoft, Inc. (http://www.mainsoft.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Runtime.Serialization;
  32. using System.Globalization;
  33. namespace System.Web
  34. {
  35. internal class ServerVariablesCollection:HttpValueCollection
  36. {
  37. private HttpRequest _request;
  38. private bool _loaded = false;
  39. public ServerVariablesCollection(HttpRequest request):base()
  40. {
  41. _request = request;
  42. }
  43. private void loadServerVariablesCollection()
  44. {
  45. if (_loaded)
  46. return;
  47. MakeReadWrite();
  48. Add("ALL_HTTP", _request.GetAllHeaders(false));
  49. Add("ALL_RAW", _request.GetAllHeaders(true));
  50. Add("APPL_MD_PATH", _request.WorkerRequest.GetServerVariable("APPL_MD_PATH"));
  51. Add("AUTH_PASSWORD", _request.WorkerRequest.GetServerVariable("AUTH_PASSWORD"));
  52. Add("CERT_COOKIE", _request.WorkerRequest.GetServerVariable("CERT_COOKIE"));
  53. Add("CERT_FLAGS", _request.WorkerRequest.GetServerVariable("CERT_FLAGS"));
  54. Add("CERT_ISSUER", _request.WorkerRequest.GetServerVariable("CERT_ISSUER"));
  55. Add("CERT_KEYSIZE", _request.WorkerRequest.GetServerVariable("CERT_KEYSIZE"));
  56. Add("CERT_SECRETKEYSIZE", _request.WorkerRequest.GetServerVariable("CERT_SECRETKEYSIZE"));
  57. Add("CERT_SERIALNUMBER", _request.WorkerRequest.GetServerVariable("CERT_SERIALNUMBER"));
  58. Add("CERT_SERVER_ISSUER", _request.WorkerRequest.GetServerVariable("CERT_SERVER_ISSUER"));
  59. Add("CERT_SERVER_SUBJECT", _request.WorkerRequest.GetServerVariable("CERT_SERVER_SUBJECT"));
  60. Add("CERT_SUBJECT", _request.WorkerRequest.GetServerVariable("CERT_SUBJECT"));
  61. Add("GATEWAY_INTERFACE", _request.WorkerRequest.GetServerVariable("GATEWAY_INTERFACE"));
  62. Add("HTTPS", _request.WorkerRequest.GetServerVariable("HTTPS"));
  63. Add("HTTPS_KEYSIZE", _request.WorkerRequest.GetServerVariable("HTTPS_KEYSIZE"));
  64. Add("HTTPS_SECRETKEYSIZE", _request.WorkerRequest.GetServerVariable("HTTPS_SECRETKEYSIZE"));
  65. Add("CONTENT_TYPE", _request.ContentType);
  66. Add("HTTPS_SERVER_ISSUER", _request.WorkerRequest.GetServerVariable("HTTPS_SERVER_ISSUER"));
  67. Add("HTTPS_SERVER_SUBJECT", _request.WorkerRequest.GetServerVariable("HTTPS_SERVER_SUBJECT"));
  68. Add("INSTANCE_ID", _request.WorkerRequest.GetServerVariable("INSTANCE_ID"));
  69. Add("INSTANCE_META_PATH", _request.WorkerRequest.GetServerVariable("INSTANCE_META_PATH"));
  70. Add("LOCAL_ADDR", _request.WorkerRequest.GetLocalAddress());
  71. Add("REMOTE_ADDR", _request.UserHostAddress);
  72. Add("REMOTE_HOST", _request.UserHostName);
  73. Add("REMOTE_PORT", _request.WorkerRequest.GetRemotePort ().ToString ());
  74. Add("REQUEST_METHOD", _request.HttpMethod);
  75. Add("SERVER_NAME", _request.WorkerRequest.GetServerName());
  76. Add("SERVER_PORT", _request.WorkerRequest.GetLocalPort().ToString());
  77. Add("SERVER_PROTOCOL", _request.WorkerRequest.GetHttpVersion());
  78. Add("SERVER_SOFTWARE", _request.WorkerRequest.GetServerVariable("SERVER_SOFTWARE"));
  79. if (_request.WorkerRequest.IsSecure())
  80. Add("SERVER_PORT_SECURE", "1");
  81. else
  82. Add("SERVER_PORT_SECURE", "0");
  83. string sTmp = _request.WorkerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength);
  84. if (null != sTmp)
  85. Add("CONTENT_LENGTH", sTmp);
  86. // TODO: Should be dynamic
  87. if (null != _request.Context.User && _request.Context.User.Identity.IsAuthenticated)
  88. {
  89. Add("AUTH_TYPE", _request.Context.User.Identity.AuthenticationType);
  90. Add("AUTH_USER", _request.Context.User.Identity.Name);
  91. }
  92. else
  93. {
  94. Add("AUTH_TYPE", "");
  95. Add("AUTH_USER", "");
  96. }
  97. Add("PATH_INFO", _request.PathInfo);
  98. Add("PATH_TRANSLATED", _request.PhysicalPath);
  99. Add("QUERY_STRING", _request.QueryStringRaw);
  100. Add("SCRIPT_NAME", _request.FilePath);
  101. MakeReadOnly();
  102. _loaded = true;
  103. }
  104. public override string Get(int index)
  105. {
  106. loadServerVariablesCollection();
  107. return base.Get(index);
  108. }
  109. public override string Get(string name)
  110. {
  111. string text1;
  112. if (!_loaded)
  113. {
  114. text1 = GetServerVar(name);
  115. if (text1 != null)
  116. return text1;
  117. loadServerVariablesCollection();
  118. }
  119. return base.Get(name);
  120. }
  121. private string GetServerVar(string name)
  122. {
  123. if (((name == null) || (name.Length <= 8)) || (this._request == null))
  124. return null;
  125. if (string.Compare(name, "AUTH_TYPE", true, CultureInfo.InvariantCulture) == 0)
  126. {
  127. if (null != _request.Context.User && _request.Context.User.Identity.IsAuthenticated)
  128. return _request.Context.User.Identity.AuthenticationType;
  129. else
  130. return string.Empty;
  131. }
  132. else if (string.Compare(name, "AUTH_USER",true, CultureInfo.InvariantCulture) == 0)
  133. {
  134. if (null != _request.Context.User && _request.Context.User.Identity.IsAuthenticated)
  135. return _request.Context.User.Identity.Name;
  136. else
  137. return string.Empty;
  138. }
  139. else if (string.Compare(name, "QUERY_STRING", true, CultureInfo.InvariantCulture) == 0)
  140. return this._request.QueryStringRaw;
  141. else if (string.Compare(name, "PATH_INFO", true, CultureInfo.InvariantCulture) == 0)
  142. return this._request.PathInfo;
  143. else if (string.Compare(name, "PATH_TRANSLATED", true, CultureInfo.InvariantCulture) == 0)
  144. return this._request.PhysicalPath;
  145. else if (string.Compare(name, "REQUEST_METHOD", true, CultureInfo.InvariantCulture) == 0)
  146. return this._request.HttpMethod;
  147. else if (string.Compare(name, "REMOTE_ADDR", true, CultureInfo.InvariantCulture) == 0)
  148. return this._request.UserHostAddress;
  149. else if (string.Compare(name, "REMOTE_HOST", true, CultureInfo.InvariantCulture) == 0)
  150. return this._request.UserHostName;
  151. else if (string.Compare(name, "REMOTE_ADDRESS", true, CultureInfo.InvariantCulture) == 0)
  152. return this._request.UserHostAddress;
  153. else if (string.Compare(name, "SCRIPT_NAME", true, CultureInfo.InvariantCulture) == 0)
  154. return this._request.FilePath;
  155. else if (string.Compare(name, "LOCAL_ADDR", true, CultureInfo.InvariantCulture) == 0)
  156. return this._request.WorkerRequest.GetLocalAddress();
  157. else if (string.Compare(name, "SERVER_PROTOCOL", true, CultureInfo.InvariantCulture) == 0)
  158. return _request.WorkerRequest.GetHttpVersion();
  159. else if (string.Compare(name, "SERVER_SOFTWARE", true, CultureInfo.InvariantCulture) == 0)
  160. return _request.WorkerRequest.GetServerVariable("SERVER_SOFTWARE");
  161. return null;
  162. }
  163. public override string GetKey(int index)
  164. {
  165. loadServerVariablesCollection();
  166. return base.GetKey(index);
  167. }
  168. public override string[] GetValues(int index)
  169. {
  170. string text1;
  171. string[] array1;
  172. text1 = Get(index);
  173. if (text1 == null)
  174. {
  175. return null;
  176. }
  177. array1 = new string[1];
  178. array1[0] = text1;
  179. return array1;
  180. }
  181. public override string[] GetValues(string name)
  182. {
  183. string text1;
  184. string[] array1;
  185. text1 = Get(name);
  186. if (text1 == null)
  187. {
  188. return null;
  189. }
  190. array1 = new string[1];
  191. array1[0] = text1;
  192. return array1;
  193. }
  194. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  195. {
  196. throw new SerializationException();
  197. }
  198. public override string[] AllKeys
  199. {
  200. get
  201. {
  202. loadServerVariablesCollection();
  203. return base.AllKeys;
  204. }
  205. }
  206. public override int Count
  207. {
  208. get
  209. {
  210. loadServerVariablesCollection();
  211. return base.Count;
  212. }
  213. }
  214. }
  215. }