X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2FSystem.Web%2FSystem.Web%2FServerVariablesCollection.cs;h=21677e8d0a4ae624b3ed846ffaac5e4cf96d90bd;hb=8e898619619222f9caf43c970fd7ff6c9f231ccb;hp=4224e8e68e6a59943da48073b4766bbca25a7c2c;hpb=948dbf8d4581ac17f5420cc4f7dc375e3c502576;p=mono.git diff --git a/mcs/class/System.Web/System.Web/ServerVariablesCollection.cs b/mcs/class/System.Web/System.Web/ServerVariablesCollection.cs index 4224e8e68e6..21677e8d0a4 100644 --- a/mcs/class/System.Web/System.Web/ServerVariablesCollection.cs +++ b/mcs/class/System.Web/System.Web/ServerVariablesCollection.cs @@ -7,7 +7,7 @@ // Gonzalo Paniagua Javier (gonzalo@novell.com) // // (c) 2004 Mainsoft, Inc. (http://www.mainsoft.com) -// Copyright (C) 2005 Novell, Inc (http://www.novell.com) +// Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com) // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -35,18 +35,40 @@ using System.Collections.Specialized; using System.Runtime.Serialization; using System.Globalization; using System.Security.Permissions; +using System.Security.Principal; +using System.Web.Util; namespace System.Web { - class ServerVariablesCollection : NameValueCollection + sealed class ServerVariablesCollection : BaseParamsCollection { - HttpRequest request; bool loaded; - public ServerVariablesCollection(HttpRequest request) + string QueryString { + get { + string qs = _request.QueryStringRaw; + + if (String.IsNullOrEmpty (qs)) + return qs; + + if (qs [0] == '?') + return qs.Substring (1); + + return qs; + } + } + + IIdentity UserIdentity { + get { + HttpContext context = _request != null ? _request.Context : null; + IPrincipal user = context != null ? context.User : null; + return user != null ? user.Identity : null; + } + } + + public ServerVariablesCollection(HttpRequest request) : base(request) { IsReadOnly = true; - this.request = request; } void AppendKeyValue (StringBuilder sb, string key, string value, bool standard) @@ -59,7 +81,7 @@ namespace System.Web // if (standard){ sb.Append ("HTTP_"); - sb.Append (key.ToUpper ().Replace ("-", "_")); + sb.Append (key.ToUpper (Helpers.InvariantCulture).Replace ('-', '_')); sb.Append (":"); } else { sb.Append (key); @@ -75,7 +97,7 @@ namespace System.Web for (int i = 0; i < HttpWorkerRequest.RequestHeaderMaximum; i++){ string val = wr.GetKnownRequestHeader (i); - if (val == null || val == "") + if (String.IsNullOrEmpty (val)) continue; string key = HttpWorkerRequest.GetKnownRequestHeaderName (i); AppendKeyValue (sb, key, val, standard); @@ -103,7 +125,7 @@ namespace System.Web if (null != hvalue && hvalue.Length > 0) { hname = HttpWorkerRequest.GetKnownRequestHeaderName (i); if (null != hname && hname.Length > 0) - Add ("HTTP_" + hname.ToUpper ().Replace ('-', '_'), hvalue); + Add ("HTTP_" + hname.ToUpper (Helpers.InvariantCulture).Replace ('-', '_'), hvalue); } } @@ -115,14 +137,14 @@ namespace System.Web if (hname == null) continue; hvalue = unknown [i][1]; - Add ("HTTP_" + hname.ToUpper ().Replace ('-', '_'), hvalue); + Add ("HTTP_" + hname.ToUpper (Helpers.InvariantCulture).Replace ('-', '_'), hvalue); } } } - private void loadServerVariablesCollection() + void loadServerVariablesCollection() { - HttpWorkerRequest wr = request.WorkerRequest; + HttpWorkerRequest wr = _request.WorkerRequest; if (loaded || (wr == null)) return; @@ -134,12 +156,14 @@ namespace System.Web Add("APPL_MD_PATH", wr.GetServerVariable("APPL_MD_PATH")); Add("APPL_PHYSICAL_PATH", wr.GetServerVariable("APPL_PHYSICAL_PATH")); - if (null != request.Context.User && request.Context.User.Identity.IsAuthenticated) { - Add ("AUTH_TYPE", request.Context.User.Identity.AuthenticationType); - Add ("AUTH_USER", request.Context.User.Identity.Name); + IIdentity identity = UserIdentity; + + if (identity != null && identity.IsAuthenticated) { + Add ("AUTH_TYPE", identity.AuthenticationType); + Add ("AUTH_USER", identity.Name); } else { - Add ("AUTH_TYPE", ""); - Add ("AUTH_USER", ""); + Add ("AUTH_TYPE", String.Empty); + Add ("AUTH_USER", String.Empty); } Add("AUTH_PASSWORD", wr.GetServerVariable("AUTH_PASSWORD")); @@ -158,7 +182,7 @@ namespace System.Web string sTmp = wr.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength); if (null != sTmp) Add ("CONTENT_LENGTH", sTmp); - Add ("CONTENT_TYPE", request.ContentType); + Add ("CONTENT_TYPE", _request.ContentType); Add("GATEWAY_INTERFACE", wr.GetServerVariable("GATEWAY_INTERFACE")); Add("HTTPS", wr.GetServerVariable("HTTPS")); @@ -169,14 +193,14 @@ namespace System.Web Add("INSTANCE_ID", wr.GetServerVariable("INSTANCE_ID")); Add("INSTANCE_META_PATH", wr.GetServerVariable("INSTANCE_META_PATH")); Add("LOCAL_ADDR", wr.GetLocalAddress()); - Add("PATH_INFO", request.PathInfo); - Add("PATH_TRANSLATED", request.PhysicalPath); - Add("QUERY_STRING", request.QueryStringRaw); - Add("REMOTE_ADDR", request.UserHostAddress); - Add("REMOTE_HOST", request.UserHostName); + Add("PATH_INFO", _request.PathInfo); + Add("PATH_TRANSLATED", _request.PhysicalPath); + Add("QUERY_STRING", QueryString); + Add("REMOTE_ADDR", _request.UserHostAddress); + Add("REMOTE_HOST", _request.UserHostName); Add("REMOTE_PORT", wr.GetRemotePort ().ToString ()); - Add("REQUEST_METHOD", request.HttpMethod); - Add("SCRIPT_NAME", request.FilePath); + Add("REQUEST_METHOD", _request.HttpMethod); + Add("SCRIPT_NAME", _request.FilePath); Add("SERVER_NAME", wr.GetServerName()); Add("SERVER_PORT", wr.GetLocalPort().ToString()); if (wr.IsSecure()) @@ -185,7 +209,7 @@ namespace System.Web Add("SERVER_PORT_SECURE", "0"); Add("SERVER_PROTOCOL", wr.GetHttpVersion()); Add("SERVER_SOFTWARE", wr.GetServerVariable("SERVER_SOFTWARE")); - Add ("URL", request.FilePath); + Add ("URL", _request.FilePath); AddHeaderVariables (wr); @@ -193,123 +217,102 @@ namespace System.Web loaded = true; } - - public override string Get(int index) - { - loadServerVariablesCollection(); - return base.Get(index); - } - - public override string Get(string name) + protected override void InsertInfo () { - string text1; - if (!loaded) { - text1 = GetServerVar(name); - if (text1 != null) - return text1; - loadServerVariablesCollection(); - } - return base.Get(name); - - + loadServerVariablesCollection (); } - private string GetServerVar(string name) - { - if (((name == null) || (name.Length <= 8)) || (this.request == null)) - return null; - - if (string.Compare(name, "AUTH_TYPE", true, CultureInfo.InvariantCulture) == 0) { - if (null != request.Context.User && request.Context.User.Identity.IsAuthenticated) - return request.Context.User.Identity.AuthenticationType; - else - return string.Empty; - } else if (string.Compare(name, "AUTH_USER",true, CultureInfo.InvariantCulture) == 0) { - if (null != request.Context.User && request.Context.User.Identity.IsAuthenticated) - return request.Context.User.Identity.Name; - else - return string.Empty; - } else if (string.Compare(name, "QUERY_STRING", true, CultureInfo.InvariantCulture) == 0) - return this.request.QueryStringRaw; - else if (string.Compare(name, "PATH_INFO", true, CultureInfo.InvariantCulture) == 0) - return this.request.PathInfo; - else if (string.Compare(name, "PATH_TRANSLATED", true, CultureInfo.InvariantCulture) == 0) - return this.request.PhysicalPath; - else if (string.Compare(name, "REQUEST_METHOD", true, CultureInfo.InvariantCulture) == 0) - return this.request.HttpMethod; - else if (string.Compare(name, "REMOTE_ADDR", true, CultureInfo.InvariantCulture) == 0) - return this.request.UserHostAddress; - else if (string.Compare(name, "REMOTE_HOST", true, CultureInfo.InvariantCulture) == 0) - return this.request.UserHostName; - else if (string.Compare(name, "REMOTE_ADDRESS", true, CultureInfo.InvariantCulture) == 0) - return this.request.UserHostAddress; - else if (string.Compare(name, "SCRIPT_NAME", true, CultureInfo.InvariantCulture) == 0) - return this.request.FilePath; - else if (string.Compare(name, "LOCAL_ADDR", true, CultureInfo.InvariantCulture) == 0) - return this.request.WorkerRequest.GetLocalAddress(); - else if (string.Compare(name, "SERVER_PROTOCOL", true, CultureInfo.InvariantCulture) == 0) - return request.WorkerRequest.GetHttpVersion(); - else if (string.Compare(name, "SERVER_SOFTWARE", true, CultureInfo.InvariantCulture) == 0) - return request.WorkerRequest.GetServerVariable("SERVER_SOFTWARE"); - return null; - } - - public override string GetKey(int index) + protected override string InternalGet (string name) { - loadServerVariablesCollection(); - return base.GetKey(index); - } - - public override string[] GetValues(int index) - { - string text1; - string[] array1; - - text1 = Get(index); - if (text1 == null) + if ((name == null) || (this._request == null)) return null; + name = name.ToUpper (Helpers.InvariantCulture); + IIdentity identity; - array1 = new string[1]; - array1[0] = text1; - - return array1; - } - - public override string[] GetValues(string name) - { - string text1; - string[] array1; - - text1 = Get(name); - if (text1 == null) - return null; - array1 = new string[1]; - array1[0] = text1; - - return array1; - } - - // not really useful except for not triggering Gendarme warnings - [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)] - public override void GetObjectData(SerializationInfo info, StreamingContext context) - { - throw new SerializationException(); - } - - public override string[] AllKeys - { - get { - loadServerVariablesCollection (); - return base.AllKeys; + switch (name) { + case "AUTH_TYPE": + identity = UserIdentity; + if (identity != null && identity.IsAuthenticated) + return identity.AuthenticationType; + else + return string.Empty; + case "AUTH_USER": + identity = UserIdentity; + if (identity != null && identity.IsAuthenticated) + return identity.Name; + else + return string.Empty; + case "QUERY_STRING": + return QueryString; + case "PATH_INFO": + return this._request.PathInfo; + case "PATH_TRANSLATED": + return this._request.PhysicalPath; + case "REQUEST_METHOD": + return this._request.HttpMethod; + case "REMOTE_ADDR": + return this._request.UserHostAddress; + case "REMOTE_HOST": + return this._request.UserHostName; + case "REMOTE_ADDRESS": + return this._request.UserHostAddress; + case "SCRIPT_NAME": + return this._request.FilePath; + case "LOCAL_ADDR": + return this._request.WorkerRequest.GetLocalAddress (); + case "SERVER_PROTOCOL": + return _request.WorkerRequest.GetHttpVersion (); + case "CONTENT_TYPE": + return _request.ContentType; + case "REMOTE_PORT": + return _request.WorkerRequest.GetRemotePort ().ToString (); + case "SERVER_NAME": + return _request.WorkerRequest.GetServerName (); + case "SERVER_PORT": + return _request.WorkerRequest.GetLocalPort ().ToString (); + case "APPL_PHYSICAL_PATH": + return _request.WorkerRequest.GetAppPathTranslated (); + case "URL": + return _request.FilePath; + case "SERVER_PORT_SECURE": + return (_request.WorkerRequest.IsSecure ()) ? "1" : "0"; + case "ALL_HTTP": + return Fill (_request.WorkerRequest, true); + case "ALL_RAW": + return Fill (_request.WorkerRequest, false); + case "REMOTE_USER": + case "SERVER_SOFTWARE": + case "APPL_MD_PATH": + case "AUTH_PASSWORD": + case "CERT_COOKIE": + case "CERT_FLAGS": + case "CERT_ISSUER": + case "CERT_KEYSIZE": + case "CERT_SECRETKEYSIZE": + case "CERT_SERIALNUMBER": + case "CERT_SERVER_ISSUER": + case "CERT_SERVER_SUBJECT": + case "GATEWAY_INTERFACE": + case "HTTPS": + case "HTTPS_KEYSIZE": + case "HTTPS_SECRETKEYSIZE": + case "HTTPS_SERVER_ISSUER": + case "HTTPS_SERVER_SUBJECT": + case "INSTANCE_ID": + case "INSTANCE_META_PATH": + case "LOGON_USER": + case "HTTP_ACCEPT": + case "HTTP_REFERER": + case "HTTP_ACCEPT_LANGUAGE": + case "HTTP_ACCEPT_ENCODING": + case "HTTP_CONNECTION": + case "HTTP_HOST": + case "HTTP_USER_AGENT": + case "HTTP_SOAPACTION": + return _request.WorkerRequest.GetServerVariable (name); + default: + return null; } } - - public override int Count - { - get { - loadServerVariablesCollection (); - return base.Count; - } - } } }