Example #1
0
		static void AddPrefixInternal (string p, HttpListener listener)
		{
			ListenerPrefix lp = new ListenerPrefix (p);
			if (lp.Path.IndexOf ('%') != -1)
				throw new HttpListenerException (400, "Invalid path.");

			if (lp.Path.IndexOf ("//", StringComparison.Ordinal) != -1) // TODO: Code?
				throw new HttpListenerException (400, "Invalid path.");

			EndPointListener epl = GetEPListener (lp.Host, lp.Port, listener, lp.Secure);
			epl.AddPrefix (lp, listener);
		}
		static void AddPrefixInternal (string p, HttpListener listener)
		{
			ListenerPrefix lp = new ListenerPrefix (p);
			if (lp.Path.IndexOf ('%') != -1)
				throw new HttpListenerException (400, "Invalid path.");

			if (lp.Path.IndexOf ("//", StringComparison.Ordinal) != -1) // TODO: Code?
				throw new HttpListenerException (400, "Invalid path.");

			// Always listens on all the interfaces, no matter the host name/ip used.
			EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
			epl.AddPrefix (lp, listener);
		}
Example #3
0
        void RemoveSpecial(ArrayList coll, ListenerPrefix prefix)
        {
            if (coll == null)
            {
                return;
            }

            int c = coll.Count;

            for (int i = 0; i < c; i++)
            {
                ListenerPrefix p = (ListenerPrefix)coll [i];
                if (p.Path == prefix.Path)
                {
                    coll.RemoveAt(i);
                    CheckIfRemove();
                    return;
                }
            }
        }
Example #4
0
        bool RemoveSpecial(ArrayList coll, ListenerPrefix prefix)
        {
            if (coll == null)
            {
                return(false);
            }

            int c = coll.Count;

            for (int i = 0; i < c; i++)
            {
                ListenerPrefix p = (ListenerPrefix)coll [i];
                if (p.Path == prefix.Path)
                {
                    coll.RemoveAt(i);
                    return(true);
                }
            }
            return(false);
        }
        private bool RemoveSpecial(List <ListenerPrefix> list, ListenerPrefix prefix)
        {
            if (list == null)
            {
                return(false);
            }

            int c = list.Count;

            for (int i = 0; i < c; i++)
            {
                ListenerPrefix p = list[i];
                if (p.Path == prefix.Path)
                {
                    list.RemoveAt(i);
                    return(true);
                }
            }
            return(false);
        }
        public void RemovePrefix(ListenerPrefix prefix, HttpListener listener)
        {
            Hashtable obj = this.prefixes;

            lock (obj)
            {
                if (prefix.Host == "*")
                {
                    this.RemoveSpecial(this.unhandled, prefix);
                }
                else if (prefix.Host == "+")
                {
                    this.RemoveSpecial(this.all, prefix);
                }
                else if (this.prefixes.ContainsKey(prefix))
                {
                    this.prefixes.Remove(prefix);
                    this.CheckIfRemove();
                }
            }
        }
Example #7
0
        public void RemovePrefix(ListenerPrefix prefix, HttpListener listener)
        {
            lock (prefixes) {
                if (prefix.Host == "*")
                {
                    RemoveSpecial(unhandled, prefix);
                    return;
                }

                if (prefix.Host == "+")
                {
                    RemoveSpecial(all, prefix);
                    return;
                }

                if (prefixes.ContainsKey(prefix))
                {
                    prefixes.Remove(prefix);
                }
            }
        }
Example #8
0
        private static void AddPrefixInternal(string p, HttpListener listener)
        {
            int start = p.IndexOf(':') + 3;
            int colon = p.IndexOf(':', start);

            if (colon != -1)
            {
                // root can't be -1 here, since we've already checked for ending '/' in ListenerPrefix.
                int    root       = p.IndexOf('/', colon, p.Length - colon);
                string portString = p.Substring(colon + 1, root - colon - 1);

                int port;
                if (!int.TryParse(portString, out port) || port <= 0 || port >= 65536)
                {
                    throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_port);
                }
            }

            ListenerPrefix lp = new ListenerPrefix(p);

            if (lp.Host != "*" && lp.Host != "+" && Uri.CheckHostName(lp.Host) == UriHostNameType.Unknown)
            {
                throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_listener_host);
            }

            if (lp.Path.IndexOf('%') != -1)
            {
                throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_path);
            }

            if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_path);
            }

            // listens on all the interfaces if host name cannot be parsed by IPAddress.
            HttpEndPointListener epl = GetEPListener(lp.Host, lp.Port, listener, lp.Secure);

            epl.AddPrefix(lp, listener);
        }
Example #9
0
        private HttpListener MatchFromList(string host, string path, ArrayList list, out ListenerPrefix prefix)
        {
            prefix = null;
            if (list == null)
            {
                return(null);
            }
            HttpListener result = null;
            int          num    = -1;

            foreach (ListenerPrefix item in list)
            {
                string path2 = item.Path;
                if (path2.Length >= num && path.StartsWith(path2))
                {
                    num    = path2.Length;
                    result = item.Listener;
                    prefix = item;
                }
            }
            return(result);
        }
Example #10
0
        public void AddPrefix(ListenerPrefix prefix, HttpListener listener)
        {
            lock (prefixes) {
                if (prefix.Host == "*")
                {
                    if (unhandled == null)
                    {
                        unhandled = new ArrayList();
                    }

                    prefix.Listener = listener;
                    AddSpecial(unhandled, prefix);
                    return;
                }

                if (prefix.Host == "+")
                {
                    if (all == null)
                    {
                        all = new ArrayList();
                    }
                    prefix.Listener = listener;
                    AddSpecial(all, prefix);
                    return;
                }

                if (prefixes.ContainsKey(prefix))
                {
                    HttpListener other = (HttpListener)prefixes [prefix];
                    if (other != listener)                     // TODO: code.
                    {
                        throw new HttpListenerException(400, "There's another listener for " + prefix);
                    }
                    return;
                }

                prefixes [prefix] = listener;
            }
        }
        public void AddPrefix(ListenerPrefix prefix, HttpListener listener)
        {
            Hashtable obj = this.prefixes;

            lock (obj)
            {
                if (prefix.Host == "*")
                {
                    if (this.unhandled == null)
                    {
                        this.unhandled = new ArrayList();
                    }
                    prefix.Listener = listener;
                    this.AddSpecial(this.unhandled, prefix);
                }
                else if (prefix.Host == "+")
                {
                    if (this.all == null)
                    {
                        this.all = new ArrayList();
                    }
                    prefix.Listener = listener;
                    this.AddSpecial(this.all, prefix);
                }
                else if (this.prefixes.ContainsKey(prefix))
                {
                    HttpListener httpListener = (HttpListener)this.prefixes[prefix];
                    if (httpListener != listener)
                    {
                        throw new HttpListenerException(400, "There's another listener for " + prefix);
                    }
                }
                else
                {
                    this.prefixes[prefix] = listener;
                }
            }
        }
Example #12
0
		void AddSpecial (ArrayList coll, ListenerPrefix prefix)
		{
			if (coll == null)
				return;

			try {
				plock.AcquireReaderLock (-1);
				foreach (ListenerPrefix p in coll) {
					if (p.Path == prefix.Path) //TODO: code
						throw new HttpListenerException (400, "Prefix already in use.");
				}
				plock.UpgradeToWriterLock (-1);
				coll.Add (prefix);
			} finally {
				try {
					plock.ReleaseReaderLock (); // This releases the writer lock if held.
				} catch { }
			}
		}
Example #13
0
		void RemoveSpecial (ArrayList coll, ListenerPrefix prefix)
		{
			if (coll == null)
				return;

			try {
				plock.AcquireReaderLock (-1);
				int c = coll.Count;
				for (int i = 0; i < c; i++) {
					ListenerPrefix p = (ListenerPrefix) coll [i];
					if (p.Path == prefix.Path) {
						plock.UpgradeToWriterLock (-1);
						coll.RemoveAt (i);
						CheckIfRemove ();
						return;
					}
				}
			} finally {
				try {
					plock.ReleaseReaderLock (); // Releases the writer lock if held
				} catch {}
			}
		}
Example #14
0
		HttpListener SearchListener (string host, Uri uri, out ListenerPrefix prefix)
		{
			prefix = null;
			if (uri == null)
				return null;

			//TODO: We should use a ReaderWriterLock between this and the add/remove operations.
			if (host != null) {
				int colon = host.IndexOf (':');
				if (colon >= 0)
					host = host.Substring (0, colon);
			}

			string path = HttpUtility.UrlDecode (uri.AbsolutePath);
			string path_slash = path [path.Length - 1] == '/' ? path : path + "/";
			
			HttpListener best_match = null;
			int best_length = -1;

			try {
				plock.AcquireReaderLock (-1);
				if (host != null && host != "") {
					foreach (ListenerPrefix p in prefixes.Keys) {
						string ppath = p.Path;
						if (ppath.Length < best_length)
							continue;

						if (p.Host == host && (path.StartsWith (ppath) || path_slash.StartsWith (ppath))) {
							best_length = ppath.Length;
							best_match = (HttpListener) prefixes [p];
							prefix = p;
						}
					}
					if (best_length != -1)
						return best_match;
				}

				best_match = MatchFromList (host, path, unhandled, out prefix);
				if (best_match != null)
					return best_match;

				best_match = MatchFromList (host, path, all, out prefix);
				if (best_match != null)
					return best_match;
			} finally {
				try {
					plock.ReleaseReaderLock ();
				} catch {}
			}
			return null;
		}
Example #15
0
		HttpListener MatchFromList (string host, string path, ArrayList list, out ListenerPrefix prefix)
		{
			prefix = null;
			if (list == null)
				return null;

			HttpListener best_match = null;
			int best_length = -1;
			
			foreach (ListenerPrefix p in list) {
				string ppath = p.Path;
				if (ppath.Length < best_length)
					continue;

				if (path.StartsWith (ppath)) {
					best_length = ppath.Length;
					best_match = p.Listener;
					prefix = p;
				}
			}

			return best_match;
		}
Example #16
0
        HttpListener SearchListener(string host, Uri uri, out ListenerPrefix prefix)
        {
            prefix = null;
            if (uri == null)
            {
                return(null);
            }

            //TODO: We should use a ReaderWriterLock between this and the add/remove operations.
            if (host != null)
            {
                int colon = host.IndexOf(':');
                if (colon >= 0)
                {
                    host = host.Substring(0, colon);
                }
            }

            string path       = HttpUtility.UrlDecode(uri.AbsolutePath);
            string path_slash = path [path.Length - 1] == '/' ? path : path + "/";

            HttpListener best_match  = null;
            int          best_length = -1;

            lock (prefixes) {
                if (host != null && host != "")
                {
                    foreach (ListenerPrefix p in prefixes.Keys)
                    {
                        string ppath = p.Path;
                        if (ppath.Length < best_length)
                        {
                            continue;
                        }

                        if (p.Host == host && (path.StartsWith(ppath) || path_slash.StartsWith(ppath)))
                        {
                            best_length = ppath.Length;
                            best_match  = (HttpListener)prefixes [p];
                            prefix      = p;
                        }
                    }
                    if (best_length != -1)
                    {
                        return(best_match);
                    }
                }

                best_match = MatchFromList(host, path, unhandled, out prefix);
                if (best_match != null)
                {
                    return(best_match);
                }

                best_match = MatchFromList(host, path, all, out prefix);
                if (best_match != null)
                {
                    return(best_match);
                }
            }
            return(null);
        }
        public override bool Equals(object o)
        {
            ListenerPrefix listenerPrefix = o as ListenerPrefix;

            return(listenerPrefix != null && this.original == listenerPrefix.original);
        }
Example #18
0
		static void RemovePrefixInternal (string prefix, HttpListener listener)
		{
			ListenerPrefix lp = new ListenerPrefix (prefix);
			if (lp.Path.IndexOf ('%') != -1)
				return;

			if (lp.Path.IndexOf ("//") != -1)
				return;

			EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
			epl.RemovePrefix (lp, listener);
		}
Example #19
0
        private HttpListener SearchListener(Uri uri, out ListenerPrefix prefix)
        {
            prefix = null;
            if (uri == null)
            {
                return(null);
            }

            string host      = uri.Host;
            int    port      = uri.Port;
            string path      = WebUtility.UrlDecode(uri.AbsolutePath);
            string pathSlash = path[path.Length - 1] == '/' ? path : path + "/";

            HttpListener bestMatch  = null;
            int          bestLength = -1;

            if (host != null && host != "")
            {
                Dictionary <ListenerPrefix, HttpListener> localPrefixes = _prefixes;
                foreach (ListenerPrefix p in localPrefixes.Keys)
                {
                    string ppath = p.Path;
                    if (ppath.Length < bestLength)
                    {
                        continue;
                    }

                    if (p.Host != host || p.Port != port)
                    {
                        continue;
                    }

                    if (path.StartsWith(ppath) || pathSlash.StartsWith(ppath))
                    {
                        bestLength = ppath.Length;
                        bestMatch  = localPrefixes[p];
                        prefix     = p;
                    }
                }
                if (bestLength != -1)
                {
                    return(bestMatch);
                }
            }

            List <ListenerPrefix> list = _unhandledPrefixes;

            bestMatch = MatchFromList(host, path, list, out prefix);

            if (path != pathSlash && bestMatch == null)
            {
                bestMatch = MatchFromList(host, pathSlash, list, out prefix);
            }

            if (bestMatch != null)
            {
                return(bestMatch);
            }

            list      = _allPrefixes;
            bestMatch = MatchFromList(host, path, list, out prefix);

            if (path != pathSlash && bestMatch == null)
            {
                bestMatch = MatchFromList(host, pathSlash, list, out prefix);
            }

            if (bestMatch != null)
            {
                return(bestMatch);
            }

            return(null);
        }
Example #20
0
		public void RemovePrefix (ListenerPrefix prefix, HttpListener listener)
		{
			ArrayList current;
			ArrayList future;
			if (prefix.Host == "*") {
				do {
					current = unhandled;
					future = (current != null) ? (ArrayList) current.Clone () : new ArrayList ();
					if (!RemoveSpecial (future, prefix))
						break; // Prefix not found
				} while (Interlocked.CompareExchange (ref unhandled, future, current) != current);
				CheckIfRemove ();
				return;
			}

			if (prefix.Host == "+") {
				do {
					current = all;
					future = (current != null) ? (ArrayList) current.Clone () : new ArrayList ();
					if (!RemoveSpecial (future, prefix))
						break; // Prefix not found
				} while (Interlocked.CompareExchange (ref all, future, current) != current);
				CheckIfRemove ();
				return;
			}

			Hashtable prefs, p2;
			do {
				prefs = prefixes;
				if (!prefs.ContainsKey (prefix))
					break;

				p2 = (Hashtable) prefs.Clone ();
				p2.Remove (prefix);
			} while (Interlocked.CompareExchange (ref prefixes, p2, prefs) != prefs);
			CheckIfRemove ();
		}
Example #21
0
        HttpListener SearchListener(Uri uri, out ListenerPrefix prefix)
        {
            prefix = null;
            if (uri == null)
            {
                return(null);
            }

            string host       = uri.Host;
            int    port       = uri.Port;
            string path       = HttpUtility.UrlDecode(uri.AbsolutePath);
            string path_slash = path [path.Length - 1] == '/' ? path : path + "/";

            HttpListener best_match  = null;
            int          best_length = -1;

            if (host != null && host != "")
            {
                Hashtable p_ro = prefixes;
                foreach (ListenerPrefix p in p_ro.Keys)
                {
                    string ppath = p.Path;
                    if (ppath.Length < best_length)
                    {
                        continue;
                    }

                    if (p.Host != host || p.Port != port)
                    {
                        continue;
                    }

                    if (path.StartsWith(ppath) || path_slash.StartsWith(ppath))
                    {
                        best_length = ppath.Length;
                        best_match  = (HttpListener)p_ro [p];
                        prefix      = p;
                    }
                }
                if (best_length != -1)
                {
                    return(best_match);
                }
            }

            ArrayList list = unhandled;

            best_match = MatchFromList(host, path, list, out prefix);
            if (path != path_slash && best_match == null)
            {
                best_match = MatchFromList(host, path_slash, list, out prefix);
            }
            if (best_match != null)
            {
                return(best_match);
            }

            list       = all;
            best_match = MatchFromList(host, path, list, out prefix);
            if (path != path_slash && best_match == null)
            {
                best_match = MatchFromList(host, path_slash, list, out prefix);
            }
            if (best_match != null)
            {
                return(best_match);
            }

            return(null);
        }
Example #22
0
		public void AddPrefix (ListenerPrefix prefix, HttpListener listener)
		{
			ArrayList current;
			ArrayList future;
			if (prefix.Host == "*") {
				do {
					current = unhandled;
					future = (current != null) ? (ArrayList) current.Clone () : new ArrayList ();
					prefix.Listener = listener;
					AddSpecial (future, prefix);
				} while (Interlocked.CompareExchange (ref unhandled, future, current) != current);
				return;
			}

			if (prefix.Host == "+") {
				do {
					current = all;
					future = (current != null) ? (ArrayList) current.Clone () : new ArrayList ();
					prefix.Listener = listener;
					AddSpecial (future, prefix);
				} while (Interlocked.CompareExchange (ref all, future, current) != current);
				return;
			}

			Hashtable prefs, p2;
			do {
				prefs = prefixes;
				if (prefs.ContainsKey (prefix)) {
					HttpListener other = (HttpListener) prefs [prefix];
					if (other != listener) // TODO: code.
						throw new HttpListenerException (400, "There's another listener for " + prefix);
					return;
				}
				p2 = (Hashtable) prefs.Clone ();
				p2 [prefix] = listener;
			} while (Interlocked.CompareExchange (ref prefixes, p2, prefs) != prefs);
		}
Example #23
0
		bool RemoveSpecial (ArrayList coll, ListenerPrefix prefix)
		{
			if (coll == null)
				return false;

			int c = coll.Count;
			for (int i = 0; i < c; i++) {
				ListenerPrefix p = (ListenerPrefix) coll [i];
				if (p.Path == prefix.Path) {
					coll.RemoveAt (i);
					return true;
				}
			}
			return false;
		}
Example #24
0
		HttpListener SearchListener (Uri uri, out ListenerPrefix prefix)
		{
			prefix = null;
			if (uri == null)
				return null;

			string host = uri.Host;
			int port = uri.Port;
			string path = WebUtility.UrlDecode (uri.AbsolutePath);
			string path_slash = path [path.Length - 1] == '/' ? path : path + "/";
			
			HttpListener best_match = null;
			int best_length = -1;

			if (host != null && host != "") {
				Hashtable p_ro = prefixes;
				foreach (ListenerPrefix p in p_ro.Keys) {
					string ppath = p.Path;
					if (ppath.Length < best_length)
						continue;

					if (p.Host != host || p.Port != port)
						continue;

					if (path.StartsWith (ppath) || path_slash.StartsWith (ppath)) {
						best_length = ppath.Length;
						best_match = (HttpListener) p_ro [p];
						prefix = p;
					}
				}
				if (best_length != -1)
					return best_match;
			}

			ArrayList list = unhandled;
			best_match = MatchFromList (host, path, list, out prefix);
			if (path != path_slash && best_match == null)
				best_match = MatchFromList (host, path_slash, list, out prefix);
			if (best_match != null)
				return best_match;

			list = all;
			best_match = MatchFromList (host, path, list, out prefix);
			if (path != path_slash && best_match == null)
				best_match = MatchFromList (host, path_slash, list, out prefix);
			if (best_match != null)
				return best_match;

			return null;
		}
Example #25
0
		public void AddPrefix (ListenerPrefix prefix, HttpListener listener)
		{
			if (prefix.Host == "*") {
				if (unhandled == null)
					unhandled = new ArrayList ();

				prefix.Listener = listener;
				AddSpecial (unhandled, prefix);
				return;
			}

			if (prefix.Host == "+") {
				if (all == null)
					all = new ArrayList ();
				prefix.Listener = listener;
				AddSpecial (all, prefix);
				return;
			}

			try { 
				plock.AcquireReaderLock (-1);
				if (prefixes.ContainsKey (prefix)) {
					HttpListener other = (HttpListener) prefixes [prefix];
					if (other != listener) // TODO: code.
						throw new HttpListenerException (400, "There's another listener for " + prefix);
					return;
				}
				plock.UpgradeToWriterLock (-1);
				prefixes [prefix] = listener;
			} finally {
				try {
					plock.ReleaseReaderLock ();
				} catch {}
			}
		}
		public void AddPrefix (ListenerPrefix prefix, HttpListener listener)
		{
			lock (prefixes) {
				if (prefix.Host == "*") {
					if (unhandled == null)
						unhandled = new ArrayList ();

					prefix.Listener = listener;
					AddSpecial (unhandled, prefix);
					return;
				}

				if (prefix.Host == "+") {
					if (all == null)
						all = new ArrayList ();
					prefix.Listener = listener;
					AddSpecial (all, prefix);
					return;
				}

				if (prefixes.ContainsKey (prefix)) {
					HttpListener other = (HttpListener) prefixes [prefix];
					if (other != listener) // TODO: code.
						throw new HttpListenerException (400, "There's another listener for " + prefix);
					return;
				}

				prefixes [prefix] = listener;
			}
		}
Example #27
0
		public void RemovePrefix (ListenerPrefix prefix, HttpListener listener)
		{
			if (prefix.Host == "*") {
				RemoveSpecial (unhandled, prefix);
				return;
			}

			if (prefix.Host == "+") {
				RemoveSpecial (all, prefix);
				return;
			}

			try {
				plock.AcquireReaderLock (-1);
				if (prefixes.ContainsKey (prefix)) {
					plock.UpgradeToWriterLock (-1);
					prefixes.Remove (prefix);
					CheckIfRemove ();
				}
			} finally {
				try {
					plock.ReleaseReaderLock ();
				} catch {}
			}
		}
Example #28
0
		static void RemovePrefixInternal (string prefix, HttpListener listener)
		{
			ListenerPrefix lp = new ListenerPrefix (prefix);
			if (lp.Path.IndexOf ('%') != -1)
				return;

			if (lp.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
				return;

			EndPointListener epl = GetEPListener (lp.Host, lp.Port, listener, lp.Secure);
			epl.RemovePrefix (lp, listener);
		}
Example #29
0
        private HttpListener MatchFromList(string host, string path, List <ListenerPrefix> list, out ListenerPrefix prefix)
        {
            prefix = null;
            if (list == null)
            {
                return(null);
            }

            HttpListener bestMatch  = null;
            int          bestLength = -1;

            foreach (ListenerPrefix p in list)
            {
                string ppath = p.Path;
                if (ppath.Length < bestLength)
                {
                    continue;
                }

                if (path.StartsWith(ppath))
                {
                    bestLength = ppath.Length;
                    bestMatch  = p._listener;
                    prefix     = p;
                }
            }

            return(bestMatch);
        }
Example #30
0
		HttpListener SearchListener (Uri uri, out ListenerPrefix prefix)
		{
			prefix = null;
			if (uri == null)
				return null;

			string host = uri.Host;
			int port = uri.Port;
			string path = HttpUtility.UrlDecode (uri.AbsolutePath);
			string path_slash = path [path.Length - 1] == '/' ? path : path + "/";
			
			HttpListener best_match = null;
			int best_length = -1;

			lock (prefixes) {
				if (host != null && host != "") {
					foreach (ListenerPrefix p in prefixes.Keys) {
						string ppath = p.Path;
						if (ppath.Length < best_length)
							continue;

						if (p.Host != host || p.Port != port)
							continue;

						if (path.StartsWith (ppath) || path_slash.StartsWith (ppath)) {
							best_length = ppath.Length;
							best_match = (HttpListener) prefixes [p];
							prefix = p;
						}
					}
					if (best_length != -1)
						return best_match;
				}

				best_match = MatchFromList (host, path, unhandled, out prefix);
				if (best_match != null)
					return best_match;

				best_match = MatchFromList (host, path, all, out prefix);
				if (best_match != null)
					return best_match;
			}
			return null;
		}
		public void RemovePrefix (ListenerPrefix prefix, HttpListener listener)
		{
			lock (prefixes) {
				if (prefix.Host == "*") {
					RemoveSpecial (unhandled, prefix);
					return;
				}

				if (prefix.Host == "+") {
					RemoveSpecial (all, prefix);
					return;
				}

				if (prefixes.ContainsKey (prefix)) {
					prefixes.Remove (prefix);
					CheckIfRemove ();
				}
			}
		}
Example #32
0
        private HttpListener SearchListener(Uri uri, out ListenerPrefix prefix)
        {
            prefix = null;
            if (uri == null)
            {
                return(null);
            }

            var host      = uri.Host;
            var port      = uri.Port;
            var path      = WebUtility.UrlDecode(uri.AbsolutePath);
            var pathSlash = path[path.Length - 1] == '/' ? path : path + "/";

            HttpListener bestMatch  = null;
            var          bestLength = -1;

            if (!string.IsNullOrEmpty(host))
            {
                var pRo = _prefixes;
                foreach (ListenerPrefix p in pRo.Keys)
                {
                    var ppath = p.Path;
                    if (ppath.Length < bestLength)
                    {
                        continue;
                    }

                    if (p.Host != host || p.Port != port)
                    {
                        continue;
                    }

                    if (path.StartsWith(ppath) || pathSlash.StartsWith(ppath))
                    {
                        bestLength = ppath.Length;
                        bestMatch  = (HttpListener)pRo[p];
                        prefix     = p;
                    }
                }
                if (bestLength != -1)
                {
                    return(bestMatch);
                }
            }

            var list = _unhandled;

            bestMatch = MatchFromList(host, path, list, out prefix);
            if (path != pathSlash && bestMatch == null)
            {
                bestMatch = MatchFromList(host, pathSlash, list, out prefix);
            }
            if (bestMatch != null)
            {
                return(bestMatch);
            }

            list      = _all;
            bestMatch = MatchFromList(host, path, list, out prefix);
            if (path != pathSlash && bestMatch == null)
            {
                bestMatch = MatchFromList(host, pathSlash, list, out prefix);
            }

            return(bestMatch);
        }
		void AddSpecial (ArrayList coll, ListenerPrefix prefix)
		{
			if (coll == null)
				return;

			foreach (ListenerPrefix p in coll) {
				if (p.Path == prefix.Path) //TODO: code
					throw new HttpListenerException (400, "Prefix already in use.");
			}

			coll.Add (prefix);
		}
Example #34
0
		void Init ()
		{
			context_bound = false;
			i_stream = null;
			o_stream = null;
			prefix = null;
			chunked = false;
			ms = new MemoryStream ();
			position = 0;
			input_state = InputState.RequestLine;
			line_state = LineState.None;
			context = new HttpListenerContext (this);
		}
Example #35
0
		void Init ()
		{
			if (ssl_stream != null) {
				ssl_stream.AuthenticateAsServer (cert, true, (SslProtocols)ServicePointManager.SecurityProtocol, false);
			}

			context_bound = false;
			i_stream = null;
			o_stream = null;
			prefix = null;
			chunked = false;
			ms = new MemoryStream ();
			position = 0;
			input_state = InputState.RequestLine;
			line_state = LineState.None;
			context = new HttpListenerContext (this);
		}
		void RemoveSpecial (ArrayList coll, ListenerPrefix prefix)
		{
			if (coll == null)
				return;

			int c = coll.Count;
			for (int i = 0; i < c; i++) {
				ListenerPrefix p = (ListenerPrefix) coll [i];
				if (p.Path == prefix.Path) {
					coll.RemoveAt (i);
					CheckIfRemove ();
					return;
				}
			}
		}