void AddSpecial (List<ListenerPrefix> 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);
		}
		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.");

			// listens on all the interfaces if host name cannot be parsed by IPAddress.
			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);
		}
    private static void addPrefix (string uriPrefix, HttpListener httpListener)
    {
      var prefix = new ListenerPrefix (uriPrefix);
      if (prefix.Path.IndexOf ('%') != -1)
        throw new HttpListenerException (400, "Invalid path."); // TODO: Code?

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

      // Always listens on all the interfaces, no matter the host name/ip used.
      var epListener = getEndPointListener (
        IPAddress.Any, prefix.Port, httpListener, prefix.Secure);

      epListener.AddPrefix (prefix, httpListener);
    }
        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 System.Net.HttpListenerException(400, "There's another listener for " + prefix);
                    return;
                }
                p2 = (Hashtable)prefs.Clone();
                p2[prefix] = listener;
            } while (Interlocked.CompareExchange(ref prefixes, p2, prefs) != prefs);
        }
 private void init ()
 {
   _chunked = false;
   _context = new HttpListenerContext (this);
   _inputState = InputState.RequestLine;
   _inputStream = null;
   _lineState = LineState.None;
   _outputStream = null;
   _position = 0;
   _prefix = null;
   _requestBuffer = new MemoryStream ();
 }
		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);
				}
			}
		}
		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;
				}
			}
		}
		HttpListener SearchListener (string host, string raw_url, out ListenerPrefix prefix)
		{
			prefix = null;
			if (raw_url == 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;
			Uri raw_uri;
			#if NET_2_0
if (MonoHttp.Utility.MaybeUri (raw_url) && Uri.TryCreate (raw_url, UriKind.Absolute, out raw_uri))
#else
try { raw_uri = new Uri (raw_url); } catch { raw_uri = null; } if (raw_uri != null)
#endif

				path = raw_uri.PathAndQuery;
			else
				path = HttpUtility.UrlDecode (raw_url);
			
			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;
		}
		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;
		}
 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);
     s_timeout     = 90000; // 90k ms for first request, 15k ms from then on
 }
        private static void removePrefix(string uriPrefix, HttpListener httpListener)
        {
            var prefix = new ListenerPrefix (uriPrefix);
            if (prefix.Path.IndexOf ('%') != -1)
                return;

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

            var epListener = getEndPointListener (IPAddress.Any, prefix.Port, httpListener, prefix.Secure);
            epListener.RemovePrefix (prefix, httpListener);
        }
        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       = 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);
        }
Exemple #14
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;
        }
        public void RemovePrefix(ListenerPrefix prefix, HttpListener listener)
        {
            List<ListenerPrefix> current;
            List<ListenerPrefix> future;
            if (prefix.Host == "*") {
                do {
                    current = unhandled;
                    future = (current != null)
                        ? new List<ListenerPrefix> (current)
                        : new List<ListenerPrefix> ();
                    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)
                        ? new List<ListenerPrefix> (current)
                        : new List<ListenerPrefix> ();
                    if (!RemoveSpecial (future, prefix))
                        break; // Prefix not found
                } while (Interlocked.CompareExchange (ref all, future, current) != current);
                CheckIfRemove ();
                return;
            }

            Dictionary<ListenerPrefix, HttpListener> prefs, p2;
            do {
                prefs = prefixes;
                if (!prefs.ContainsKey (prefix))
                    break;

                p2 = new Dictionary<ListenerPrefix, HttpListener> (prefs);
                p2.Remove (prefix);
            } while (Interlocked.CompareExchange (ref prefixes, p2, prefs) != prefs);
            CheckIfRemove ();
        }
        public void AddPrefix(ListenerPrefix prefix, HttpListener listener)
        {
            List<ListenerPrefix> current;
            List<ListenerPrefix> future;
            if (prefix.Host == "*") {
                do {
                    current = unhandled;
                    future = (current != null)
                        ? new List<ListenerPrefix> (current)
                        : new List<ListenerPrefix> ();
                    prefix.Listener = listener;
                    AddSpecial (future, prefix);
                } while (Interlocked.CompareExchange (ref unhandled, future, current) != current);
                return;
            }

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

            Dictionary<ListenerPrefix, HttpListener> prefs, p2;
            do {
                prefs = prefixes;
                if (prefs.ContainsKey (prefix)) {
                    HttpListener other = prefs [prefix];
                    if (other != listener) // TODO: code.
                        throw new HttpListenerException (400, "There's another listener for " + prefix);
                    return;
                }
                p2 = new Dictionary<ListenerPrefix, HttpListener> (prefs);
                p2 [prefix] = listener;
            } while (Interlocked.CompareExchange (ref prefixes, p2, prefs) != prefs);
        }
        void Init()
        {
            if (ssl_stream != null)
            {
                //ssl_stream.AuthenticateAsServer(client_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, _logger);
        }
		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;
		}
Exemple #19
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);
        }
		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 ();
		}
    private static void addSpecial (List<ListenerPrefix> prefixes, ListenerPrefix prefix)
    {
      if (prefixes == null)
        return;

      var path = prefix.Path;
      foreach (var pref in prefixes)
        if (pref.Path == path) // TODO: Code?
          throw new HttpListenerException (400, "Prefix already in use.");

      prefixes.Add (prefix);
    }
		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;
		}
    private static HttpListener matchFromList (
      string host, string path, List<ListenerPrefix> list, out ListenerPrefix prefix)
    {
      prefix = null;
      if (list == null)
        return null;

      HttpListener bestMatch = null;
      var bestLength = -1;
      foreach (var pref in list) {
        var ppath = pref.Path;
        if (ppath.Length < bestLength)
          continue;

        if (path.StartsWith (ppath)) {
          bestLength = ppath.Length;
          bestMatch = pref.Listener;
          prefix = pref;
        }
      }

      return bestMatch;
    }
		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;
			}
		}
    private static bool removeSpecial (List<ListenerPrefix> prefixes, ListenerPrefix prefix)
    {
      if (prefixes == null)
        return false;

      var path = prefix.Path;
      var count = prefixes.Count;
      for (int i = 0; i < count; i++) {
        if (prefixes [i].Path == path) {
          prefixes.RemoveAt (i);
          return true;
        }
      }

      return false;
    }
 private void Init()
 {
     _chunked = false;
     _context = new HttpListenerContext (this);
     _contextWasBound = false;
     _inputState = InputState.RequestLine;
     _inputStream = null;
     _lineState = LineState.None;
     _outputStream = null;
     _position = 0;
     _prefix = null;
     _requestBuffer = new MemoryStream ();
     _timeout = 90000; // 90k ms for first request, 15k ms from then on.
 }
    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 = HttpUtility.UrlDecode (uri.AbsolutePath);
      var pathSlash = path [path.Length - 1] == '/' ? path : path + "/";

      HttpListener bestMatch = null;
      var bestLength = -1;
      if (host != null && host.Length > 0) {
        foreach (var pref in _prefixes.Keys) {
          var ppath = pref.Path;
          if (ppath.Length < bestLength)
            continue;

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

          if (path.StartsWith (ppath) || pathSlash.StartsWith (ppath)) {
            bestLength = ppath.Length;
            bestMatch = _prefixes [pref];
            prefix = pref;
          }
        }

        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);

      if (bestMatch != null)
        return bestMatch;

      return null;
    }
Exemple #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);
        }
		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);
		}