private static WebRequest CreateInternal(Uri requestUri)
        {
            if (requestUri == null)
            {
                throw new ArgumentNullException();
            }

            // Makes LookupUri lowercase since we need case-insensitive compare
            // with prefix
            string lookupUri     = requestUri.AbsoluteUri.ToLower();
            int    lookupUriLent = lookupUri.Length;

            // Walk down the list of prefixes.
            int prefixListCount = s_PrefixList.Count;

            for (int i = 0; i < prefixListCount; i++)
            {
                WebRequestPrefixElement Current = (WebRequestPrefixElement)s_PrefixList[i];

                // See if this prefix is short enough.
                int prefixLen = Current.Prefix.Length;
                if (lookupUriLent >= prefixLen)
                {
                    // It is. See if these match.
                    if (ComparePrefixString(lookupUri, Current.Prefix, prefixLen) == 0)
                    {
                        return(Current.Creator.Create(requestUri));
                    }
                }
            }

            throw new NotSupportedException();
        }
Example #2
0
        public static bool RegisterPrefix(string prefix, IWebRequestCreate creator)
        {
            bool flag = false;

            if (prefix == null)
            {
                throw new ArgumentNullException("prefix");
            }
            if (creator == null)
            {
                throw new ArgumentNullException("creator");
            }
            ExceptionHelper.WebPermissionUnrestricted.Demand();
            lock (InternalSyncObject)
            {
                Uri       uri;
                ArrayList list = (ArrayList)PrefixList.Clone();
                if (Uri.TryCreate(prefix, UriKind.Absolute, out uri))
                {
                    string absoluteUri = uri.AbsoluteUri;
                    if (!prefix.EndsWith("/", StringComparison.Ordinal) && uri.GetComponents(UriComponents.Fragment | UriComponents.PathAndQuery, UriFormat.UriEscaped).Equals("/"))
                    {
                        absoluteUri = absoluteUri.Substring(0, absoluteUri.Length - 1);
                    }
                    prefix = absoluteUri;
                }
                int index = 0;
                while (index < list.Count)
                {
                    WebRequestPrefixElement element = (WebRequestPrefixElement)list[index];
                    if (prefix.Length > element.Prefix.Length)
                    {
                        break;
                    }
                    if ((prefix.Length == element.Prefix.Length) && (string.Compare(element.Prefix, prefix, StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        flag = true;
                        break;
                    }
                    index++;
                }
                if (!flag)
                {
                    list.Insert(index, new WebRequestPrefixElement(prefix, creator));
                    PrefixList = list;
                }
            }
            return(!flag);
        }
Example #3
0
        private static WebRequest Create(Uri requestUri, bool useUriBase)
        {
            string absoluteUri;

            if (Logging.On)
            {
                Logging.Enter(Logging.Web, "WebRequest", "Create", requestUri.ToString());
            }
            WebRequestPrefixElement element = null;
            bool flag = false;

            if (!useUriBase)
            {
                absoluteUri = requestUri.AbsoluteUri;
            }
            else
            {
                absoluteUri = requestUri.Scheme + ':';
            }
            int       length     = absoluteUri.Length;
            ArrayList prefixList = PrefixList;

            for (int i = 0; i < prefixList.Count; i++)
            {
                element = (WebRequestPrefixElement)prefixList[i];
                if ((length >= element.Prefix.Length) && (string.Compare(element.Prefix, 0, absoluteUri, 0, element.Prefix.Length, StringComparison.OrdinalIgnoreCase) == 0))
                {
                    flag = true;
                    break;
                }
            }
            WebRequest retObject = null;

            if (flag)
            {
                retObject = element.Creator.Create(requestUri);
                if (Logging.On)
                {
                    Logging.Exit(Logging.Web, "WebRequest", "Create", retObject);
                }
                return(retObject);
            }
            if (Logging.On)
            {
                Logging.Exit(Logging.Web, "WebRequest", "Create", (string)null);
            }
            throw new NotSupportedException(SR.GetString("net_unknown_prefix"));
        }
Example #4
0
        /*++
         *
         *  Create - Create a WebRequest.
         *
         *  This is the main creation routine. We take a Uri object, look
         *  up the Uri in the prefix match table, and invoke the appropriate
         *  handler to create the object. We also have a parameter that
         *  tells us whether or not to use the whole Uri or just the
         *  scheme portion of it.
         *
         *  Input:
         *
         *      RequestUri          - Uri object for request.
         *      UseUriBase          - True if we're only to look at the scheme
         *                              portion of the Uri.
         *
         *  Returns:
         *
         *      Newly created WebRequest.
         * --*/

        private static WebRequest Create(Uri requestUri, bool useUriBase)
        {
            if (Logging.On)
            {
                Logging.Enter(Logging.Web, "WebRequest", "Create", requestUri.ToString());
            }

            string LookupUri;
            WebRequestPrefixElement Current = null;
            bool Found = false;

            if (!useUriBase)
            {
                LookupUri = requestUri.AbsoluteUri;
            }
            else
            {
                //
                // schemes are registered as <schemeName>":", so add the separator
                // to the string returned from the Uri object
                //

                LookupUri = requestUri.Scheme + ':';
            }

            int LookupLength = LookupUri.Length;

            // Copy the prefix list so that if it is updated it will
            // not affect us on this thread.

            ArrayList prefixList = PrefixList;

            // Look for the longest matching prefix.

            // Walk down the list of prefixes. The prefixes are kept longest
            // first. When we find a prefix that is shorter or the same size
            // as this Uri, we'll do a compare to see if they match. If they
            // do we'll break out of the loop and call the creator.

            for (int i = 0; i < prefixList.Count; i++)
            {
                Current = (WebRequestPrefixElement)prefixList[i];

                //
                // See if this prefix is short enough.
                //

                if (LookupLength >= Current.Prefix.Length)
                {
                    //
                    // It is. See if these match.
                    //

                    if (String.Compare(Current.Prefix,
                                       0,
                                       LookupUri,
                                       0,
                                       Current.Prefix.Length,
                                       StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        //
                        // These match. Remember that we found it and break
                        // out.
                        //

                        Found = true;
                        break;
                    }
                }
            }

            WebRequest webRequest = null;

            if (Found)
            {
                //
                // We found a match, so just call the creator and return what it
                // does.
                //

                webRequest = Current.Creator.Create(requestUri);
                if (Logging.On)
                {
                    Logging.Exit(Logging.Web, "WebRequest", "Create", webRequest);
                }
                return(webRequest);
            }

            if (Logging.On)
            {
                Logging.Exit(Logging.Web, "WebRequest", "Create", null);
            }

            //
            // Otherwise no match, throw an exception.
            //

            throw new NotSupportedException(SR.GetString(SR.net_unknown_prefix));
        }