Example #1
0
        public object Create(object parent, object configContextObj, XmlNode section)
        {
            // if called through client config don't even load HttpRuntime
            if (!HandlerBase.IsServerConfiguration(configContextObj))
            {
                return(null);
            }

            // unwrap the parent object
            NameValueCollection nvcParent = null;

            if (parent != null)
            {
                nvcParent = ((ClientTargetConfiguration)parent).Configuration;
            }

            // deleage the real work to NameValueSectionHandler
            ClientTargetNameValueHandler nvcHandler = new ClientTargetNameValueHandler();
            NameValueCollection          nvcResult  = (NameValueCollection)nvcHandler.Create(nvcParent, configContextObj, section);

            if (nvcResult == null)
            {
                return(null);
            }

            //
            // Return config data wrapped in an internal class, so
            // semi-trusted code cannot leak configuration data.
            //
            ClientTargetConfiguration clientTargetResult = new ClientTargetConfiguration();

            clientTargetResult.Configuration = nvcResult;

            return(clientTargetResult);
        }
        internal static string GetUserAgent(HttpRequest request)
        {
            if (request.ClientTarget.Length > 0)
            {
                // Lookup ClientTarget section in config.
                ClientTargetConfiguration clientTargetConfig     = (ClientTargetConfiguration)request.Context.GetConfig("system.web/clientTarget");
                NameValueCollection       clientTargetDictionary = null;
                if (clientTargetConfig != null)
                {
                    clientTargetDictionary = clientTargetConfig.Configuration;
                }

                if (clientTargetDictionary != null)
                {
                    // Found it
                    // Try to map the alias
                    string useUserAgent = clientTargetDictionary[request.ClientTarget];
                    if (useUserAgent != null)
                    {
                        return(useUserAgent);
                    }
                }

                throw new HttpException(HttpRuntime.FormatResourceString(SR.Invalid_client_target, request.ClientTarget));
            }

            // Protect against attacks with long User-Agent headers
            String userAgent = request.UserAgent;

            if (userAgent != null && userAgent.Length > 256)
            {
                userAgent = String.Empty;
            }
            return(userAgent);
        }