private static bool removeSpecial(List <HttpListenerPrefix> prefixes, HttpListenerPrefix prefix)
        {
            bool   flag;
            string path  = prefix.Path;
            int    count = prefixes.Count;
            int    num   = 0;

            while (true)
            {
                if (num >= count)
                {
                    flag = false;
                    break;
                }
                else if (prefixes[num].Path != path)
                {
                    num++;
                }
                else
                {
                    prefixes.RemoveAt(num);
                    flag = true;
                    break;
                }
            }
            return(flag);
        }
Exemplo n.º 2
0
        private static void removePrefix(string uriPrefix, WebSocketSharp.Net.HttpListener listener)
        {
            int num;
            EndPointListener   endPointListener;
            HttpListenerPrefix httpListenerPrefix = new HttpListenerPrefix(uriPrefix);
            IPAddress          pAddress           = EndPointManager.convertToIPAddress(httpListenerPrefix.Host);

            if (pAddress.IsLocal())
            {
                if (int.TryParse(httpListenerPrefix.Port, out num))
                {
                    if (num.IsPortNumber())
                    {
                        string path = httpListenerPrefix.Path;
                        if (path.IndexOf('%') == -1)
                        {
                            if (path.IndexOf("//", StringComparison.Ordinal) == -1)
                            {
                                IPEndPoint pEndPoint = new IPEndPoint(pAddress, num);
                                if (EndPointManager._endpoints.TryGetValue(pEndPoint, out endPointListener))
                                {
                                    if (!(endPointListener.IsSecure ^ httpListenerPrefix.IsSecure))
                                    {
                                        endPointListener.RemovePrefix(httpListenerPrefix, listener);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        private static EndPointListener getEndPointListener(HttpListenerPrefix prefix, HttpListener listener)
        {
            IPAddress key = convertToIPAddress(prefix.Host);
            Dictionary <int, EndPointListener> dictionary = null;

            if (_addressToEndpoints.ContainsKey(key))
            {
                dictionary = _addressToEndpoints[key];
            }
            else
            {
                dictionary = new Dictionary <int, EndPointListener>();
                _addressToEndpoints[key] = dictionary;
            }
            int port = prefix.Port;
            EndPointListener listener2 = null;

            if (dictionary.ContainsKey(port))
            {
                listener2 = dictionary[port];
            }
            else
            {
                listener2        = new EndPointListener(key, port, listener.ReuseAddress, prefix.IsSecure, listener.CertificateFolderPath, listener.SslConfiguration);
                dictionary[port] = listener2;
            }
            return(listener2);
        }
        private static HttpListener matchFromList(
            string host, string path, List <HttpListenerPrefix> list, out HttpListenerPrefix prefix)
        {
            prefix = null;
            if (list == null)
            {
                return(null);
            }

            HttpListener bestMatch = null;
            var          bestLen   = -1;

            foreach (var pref in list)
            {
                var ppath = pref.Path;
                if (ppath.Length < bestLen)
                {
                    continue;
                }

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

            return(bestMatch);
        }
        private static HttpListener searchHttpListenerFromSpecial(
            string path, List <HttpListenerPrefix> prefixes, out HttpListenerPrefix prefix
            )
        {
            prefix = null;

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

            HttpListener bestMatch = null;

            var bestLen = -1;

            foreach (var pref in prefixes)
            {
                var prefPath = pref.Path;

                var len = prefPath.Length;
                if (len < bestLen)
                {
                    continue;
                }

                if (path.StartsWith(prefPath))
                {
                    bestLen   = len;
                    bestMatch = pref.Listener;
                    prefix    = pref;
                }
            }

            return(bestMatch);
        }
Exemplo n.º 6
0
        public void RemovePrefix(HttpListenerPrefix prefix, HttpListener httpListener)
        {
            List <HttpListenerPrefix> current, future;

            if (prefix.Host == "*")
            {
                do
                {
                    current = _unhandled;
                    future  = current != null
                   ? new List <HttpListenerPrefix> (current)
                   : new List <HttpListenerPrefix> ();

                    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 <HttpListenerPrefix> (current)
                   : new List <HttpListenerPrefix> ();

                    if (!removeSpecial(future, prefix))
                    {
                        break; // Prefix not found.
                    }
                }while (Interlocked.CompareExchange(ref _all, future, current) != current);

                checkIfRemove();
                return;
            }

            Dictionary <HttpListenerPrefix, HttpListener> prefs, prefs2;

            do
            {
                prefs = _prefixes;
                if (!prefs.ContainsKey(prefix))
                {
                    break;
                }

                prefs2 = new Dictionary <HttpListenerPrefix, HttpListener> (prefs);
                prefs2.Remove(prefix);
            }while (Interlocked.CompareExchange(ref _prefixes, prefs2, prefs) != prefs);

            checkIfRemove();
        }
Exemplo n.º 7
0
        public void AddPrefix(HttpListenerPrefix prefix, HttpListener listener)
        {
            List <HttpListenerPrefix> current, future;

            if (prefix.Host == "*")
            {
                do
                {
                    current = _unhandled;
                    future  = current != null
                   ? new List <HttpListenerPrefix> (current)
                   : new List <HttpListenerPrefix> ();

                    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 <HttpListenerPrefix> (current)
                   : new List <HttpListenerPrefix> ();

                    prefix.Listener = listener;
                    addSpecial(future, prefix);
                }while (Interlocked.CompareExchange(ref _all, future, current) != current);

                return;
            }

            Dictionary <HttpListenerPrefix, HttpListener> prefs, prefs2;

            do
            {
                prefs = _prefixes;
                if (prefs.ContainsKey(prefix))
                {
                    if (prefs[prefix] != listener)
                    {
                        throw new HttpListenerException(
                                  87, String.Format("There's another listener for {0}.", prefix)
                                  );
                    }

                    return;
                }

                prefs2         = new Dictionary <HttpListenerPrefix, HttpListener> (prefs);
                prefs2[prefix] = listener;
            }while (Interlocked.CompareExchange(ref _prefixes, prefs2, prefs) != prefs);
        }
Exemplo n.º 8
0
        private static void removePrefix(string uriPrefix, HttpListener listener)
        {
            var pref = new HttpListenerPrefix(uriPrefix, listener);

            var addr = convertToIPAddress(pref.Host);

            if (addr == null)
            {
                return;
            }

            if (!addr.IsLocal())
            {
                return;
            }

            int port;

            if (!Int32.TryParse(pref.Port, out port))
            {
                return;
            }

            if (!port.IsPortNumber())
            {
                return;
            }

            var path = pref.Path;

            if (path.IndexOf('%') != -1)
            {
                return;
            }

            if (path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                return;
            }

            var endpoint = new IPEndPoint(addr, port);

            EndPointListener lsnr;

            if (!_endpoints.TryGetValue(endpoint, out lsnr))
            {
                return;
            }

            if (lsnr.IsSecure ^ pref.IsSecure)
            {
                return;
            }

            lsnr.RemovePrefix(pref);
        }
Exemplo n.º 9
0
        private static void removePrefix(string uriPrefix, HttpListener listener)
        {
            HttpListenerPrefix httpListenerPrefix = new HttpListenerPrefix(uriPrefix);

            if (httpListenerPrefix.Path.IndexOf('%') == -1 && httpListenerPrefix.Path.IndexOf("//", StringComparison.Ordinal) == -1)
            {
                EndPointListener endPointListener = getEndPointListener(httpListenerPrefix.Host, httpListenerPrefix.Port, listener, httpListenerPrefix.IsSecure);
                endPointListener.RemovePrefix(httpListenerPrefix, listener);
            }
        }
Exemplo n.º 10
0
        private static void removePrefix(string uriPrefix, HttpListener listener)
        {
            HttpListenerPrefix prefix = new HttpListenerPrefix(uriPrefix);
            string             path   = prefix.Path;

            if ((path.IndexOf('%') == -1) && (path.IndexOf("//", StringComparison.Ordinal) == -1))
            {
                getEndPointListener(prefix, listener).RemovePrefix(prefix, listener);
            }
        }
Exemplo n.º 11
0
        private HttpListener searchListener(Uri uri, out HttpListenerPrefix prefix)
        {
            prefix = null;
            if (uri == null)
            {
                return(null);
            }
            string       host   = uri.Host;
            int          port   = uri.Port;
            string       text   = HttpUtility.UrlDecode(uri.AbsolutePath);
            string       text2  = ((text[text.Length - 1] != '/') ? (text + "/") : text);
            HttpListener result = null;
            int          num    = -1;

            if (host != null && host.Length > 0)
            {
                foreach (HttpListenerPrefix key in _prefixes.Keys)
                {
                    string path = key.Path;
                    if (path.Length >= num && !(key.Host != host) && key.Port == port && (text.StartsWith(path) || text2.StartsWith(path)))
                    {
                        num    = path.Length;
                        result = _prefixes[key];
                        prefix = key;
                    }
                }
                if (num != -1)
                {
                    return(result);
                }
            }
            List <HttpListenerPrefix> unhandled = _unhandled;

            result = matchFromList(host, text, unhandled, out prefix);
            if (text != text2 && result == null)
            {
                result = matchFromList(host, text2, unhandled, out prefix);
            }
            if (result != null)
            {
                return(result);
            }
            unhandled = _all;
            result    = matchFromList(host, text, unhandled, out prefix);
            if (text != text2 && result == null)
            {
                result = matchFromList(host, text2, unhandled, out prefix);
            }
            if (result != null)
            {
                return(result);
            }
            return(null);
        }
Exemplo n.º 12
0
 private void init()
 {
     _context       = new HttpListenerContext(this);
     _inputState    = InputState.RequestLine;
     _inputStream   = null;
     _lineState     = LineState.None;
     _outputStream  = null;
     _position      = 0;
     _prefix        = null;
     _requestBuffer = new MemoryStream();
 }
Exemplo n.º 13
0
        private static void addSpecial(List <HttpListenerPrefix> prefixes, HttpListenerPrefix prefix)
        {
            var path = prefix.Path;

            foreach (var pref in prefixes)
            {
                if (pref.Path == path)
                {
                    throw new HttpListenerException(400, "The prefix is already in use."); // TODO: Code?
                }
            }
            prefixes.Add(prefix);
        }
Exemplo n.º 14
0
    private static void addPrefix (string uriPrefix, HttpListener httpListener)
    {
      var pref = new HttpListenerPrefix (uriPrefix);
      if (pref.Path.IndexOf ('%') != -1)
        throw new HttpListenerException (400, "Invalid path."); // TODO: Code?

      if (pref.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 epl = getEndPointListener (IPAddress.Any, pref.Port, pref.IsSecure, httpListener);
      epl.AddPrefix (pref, httpListener);
    }
Exemplo n.º 15
0
    private static void addPrefix (string uriPrefix, HttpListener listener)
    {
      var pref = new HttpListenerPrefix (uriPrefix);
      if (pref.Path.IndexOf ('%') != -1)
        throw new HttpListenerException (400, "Invalid path."); // TODO: Code?

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

      // Listens on all the interfaces if host name cannot be parsed by IPAddress.
      var lsnr = getEndPointListener (pref.Host, pref.Port, listener, pref.IsSecure);
      lsnr.AddPrefix (pref, listener);
    }
        private static void addSpecial(List <HttpListenerPrefix> prefixes, HttpListenerPrefix prefix)
        {
            string path = prefix.Path;

            foreach (HttpListenerPrefix httpListenerPrefix in prefixes)
            {
                if (httpListenerPrefix.Path == path)
                {
                    throw new WebSocketSharp.Net.HttpListenerException(87, "The prefix is already in use.");
                }
            }
            prefixes.Add(prefix);
        }
 public void Add(string uriPrefix)
 {
     this._listener.CheckDisposed();
     HttpListenerPrefix.CheckPrefix(uriPrefix);
     if (!this._prefixes.Contains(uriPrefix))
     {
         this._prefixes.Add(uriPrefix);
         if (this._listener.IsListening)
         {
             EndPointManager.AddPrefix(uriPrefix, this._listener);
         }
     }
 }
Exemplo n.º 18
0
        private static void addSpecial(List <HttpListenerPrefix> prefixes, HttpListenerPrefix prefix)
        {
            string path = prefix.Path;

            foreach (HttpListenerPrefix prefix2 in prefixes)
            {
                if (prefix2.Path == path)
                {
                    throw new HttpListenerException(400, "The prefix is already in use.");
                }
            }
            prefixes.Add(prefix);
        }
Exemplo n.º 19
0
    private static void addPrefix (string uriPrefix, HttpListener listener)
    {
      var pref = new HttpListenerPrefix (uriPrefix);

      var path = pref.Path;
      if (path.IndexOf ('%') != -1)
        throw new HttpListenerException (87, "Includes an invalid path.");

      if (path.IndexOf ("//", StringComparison.Ordinal) != -1)
        throw new HttpListenerException (87, "Includes an invalid path.");

      // Listens on all the interfaces if host name cannot be parsed by IPAddress.
      getEndPointListener (pref, listener).AddPrefix (pref, listener);
    }
Exemplo n.º 20
0
        public void RemovePrefix(HttpListenerPrefix prefix, HttpListener listener)
        {
            if (prefix.Host == "*")
            {
                List <HttpListenerPrefix> unhandled;
                List <HttpListenerPrefix> list;
                do
                {
                    unhandled = _unhandled;
                    if (unhandled == null)
                    {
                        break;
                    }
                    list = new List <HttpListenerPrefix>(unhandled);
                }while (removeSpecial(list, prefix) && Interlocked.CompareExchange(ref _unhandled, list, unhandled) != unhandled);
                checkIfRemove();
                return;
            }
            if (prefix.Host == "+")
            {
                List <HttpListenerPrefix> unhandled;
                List <HttpListenerPrefix> list;
                do
                {
                    unhandled = _all;
                    if (unhandled == null)
                    {
                        break;
                    }
                    list = new List <HttpListenerPrefix>(unhandled);
                }while (removeSpecial(list, prefix) && Interlocked.CompareExchange(ref _all, list, unhandled) != unhandled);
                checkIfRemove();
                return;
            }
            Dictionary <HttpListenerPrefix, HttpListener> prefixes;
            Dictionary <HttpListenerPrefix, HttpListener> dictionary;

            do
            {
                prefixes = _prefixes;
                if (!prefixes.ContainsKey(prefix))
                {
                    break;
                }
                dictionary = new Dictionary <HttpListenerPrefix, HttpListener>(prefixes);
                dictionary.Remove(prefix);
            }while (Interlocked.CompareExchange(ref _prefixes, dictionary, prefixes) != prefixes);
            checkIfRemove();
        }
Exemplo n.º 21
0
        /// <summary>
        /// Adds the specified <paramref name="uriPrefix"/> to the collection.
        /// </summary>
        /// <param name="uriPrefix">
        /// A <see cref="string"/> that represents the URI prefix to add. The prefix must be
        /// a well-formed URI prefix with http or https scheme, and must end with a <c>'/'</c>.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="uriPrefix"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="uriPrefix"/> is invalid.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// The <see cref="HttpListener"/> associated with this collection is closed.
        /// </exception>
        public void Add(string uriPrefix)
        {
            _listener.CheckDisposed();
            HttpListenerPrefix.CheckPrefix(uriPrefix);
            if (_prefixes.Contains(uriPrefix))
            {
                return;
            }

            _prefixes.Add(uriPrefix);
            if (_listener.IsListening)
            {
                EndPointManager.AddPrefix(uriPrefix, _listener);
            }
        }
Exemplo n.º 22
0
        private static void addPrefix(string uriPrefix, HttpListener listener)
        {
            HttpListenerPrefix prefix = new HttpListenerPrefix(uriPrefix);
            string             path   = prefix.Path;

            if (path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(0x57, "Includes an invalid path.");
            }
            if (path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                throw new HttpListenerException(0x57, "Includes an invalid path.");
            }
            getEndPointListener(prefix, listener).AddPrefix(prefix, listener);
        }
Exemplo n.º 23
0
        private static bool removeSpecial(List <HttpListenerPrefix> prefixes, HttpListenerPrefix prefix)
        {
            string path  = prefix.Path;
            int    count = prefixes.Count;

            for (int i = 0; i < count; i++)
            {
                if (prefixes[i].Path == path)
                {
                    prefixes.RemoveAt(i);
                    return(true);
                }
            }
            return(false);
        }
        public void AddPrefix(HttpListenerPrefix prefix, WebSocketSharp.Net.HttpListener listener)
        {
            List <HttpListenerPrefix> httpListenerPrefixes;
            List <HttpListenerPrefix> httpListenerPrefixes1;
            Dictionary <HttpListenerPrefix, WebSocketSharp.Net.HttpListener> httpListenerPrefixes2;
            Dictionary <HttpListenerPrefix, WebSocketSharp.Net.HttpListener> httpListenerPrefixes3;

            if (prefix.Host == "*")
            {
                do
                {
                    httpListenerPrefixes  = this._unhandled;
                    httpListenerPrefixes1 = (httpListenerPrefixes != null ? new List <HttpListenerPrefix>(httpListenerPrefixes) : new List <HttpListenerPrefix>());
                    prefix.Listener       = listener;
                    EndPointListener.addSpecial(httpListenerPrefixes1, prefix);
                }while (Interlocked.CompareExchange <List <HttpListenerPrefix> >(ref this._unhandled, httpListenerPrefixes1, httpListenerPrefixes) != httpListenerPrefixes);
            }
            else if (prefix.Host != "+")
            {
                do
                {
                    httpListenerPrefixes2 = this._prefixes;
                    if (!httpListenerPrefixes2.ContainsKey(prefix))
                    {
                        httpListenerPrefixes3         = new Dictionary <HttpListenerPrefix, WebSocketSharp.Net.HttpListener>(httpListenerPrefixes2);
                        httpListenerPrefixes3[prefix] = listener;
                    }
                    else
                    {
                        if (httpListenerPrefixes2[prefix] != listener)
                        {
                            throw new WebSocketSharp.Net.HttpListenerException(87, string.Format("There's another listener for {0}.", prefix));
                        }
                        break;
                    }
                }while (Interlocked.CompareExchange <Dictionary <HttpListenerPrefix, WebSocketSharp.Net.HttpListener> >(ref this._prefixes, httpListenerPrefixes3, httpListenerPrefixes2) != httpListenerPrefixes2);
            }
            else
            {
                do
                {
                    httpListenerPrefixes  = this._all;
                    httpListenerPrefixes1 = (httpListenerPrefixes != null ? new List <HttpListenerPrefix>(httpListenerPrefixes) : new List <HttpListenerPrefix>());
                    prefix.Listener       = listener;
                    EndPointListener.addSpecial(httpListenerPrefixes1, prefix);
                }while (Interlocked.CompareExchange <List <HttpListenerPrefix> >(ref this._all, httpListenerPrefixes1, httpListenerPrefixes) != httpListenerPrefixes);
            }
        }
Exemplo n.º 25
0
        private static void addPrefix(string uriPrefix, HttpListener listener)
        {
            HttpListenerPrefix httpListenerPrefix = new HttpListenerPrefix(uriPrefix);

            if (httpListenerPrefix.Path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(400, "Invalid path.");
            }
            if (httpListenerPrefix.Path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                throw new HttpListenerException(400, "Invalid path.");
            }
            EndPointListener endPointListener = getEndPointListener(httpListenerPrefix.Host, httpListenerPrefix.Port, listener, httpListenerPrefix.IsSecure);

            endPointListener.AddPrefix(httpListenerPrefix, listener);
        }
Exemplo n.º 26
0
        public void AddPrefix(HttpListenerPrefix prefix, HttpListener listener)
        {
            if (prefix.Host == "*")
            {
                List <HttpListenerPrefix> unhandled;
                List <HttpListenerPrefix> list;
                do
                {
                    unhandled       = _unhandled;
                    list            = ((unhandled == null) ? new List <HttpListenerPrefix>() : new List <HttpListenerPrefix>(unhandled));
                    prefix.Listener = listener;
                    addSpecial(list, prefix);
                }while (Interlocked.CompareExchange(ref _unhandled, list, unhandled) != unhandled);
                return;
            }
            if (prefix.Host == "+")
            {
                List <HttpListenerPrefix> unhandled;
                List <HttpListenerPrefix> list;
                do
                {
                    unhandled       = _all;
                    list            = ((unhandled == null) ? new List <HttpListenerPrefix>() : new List <HttpListenerPrefix>(unhandled));
                    prefix.Listener = listener;
                    addSpecial(list, prefix);
                }while (Interlocked.CompareExchange(ref _all, list, unhandled) != unhandled);
                return;
            }
            Dictionary <HttpListenerPrefix, HttpListener> prefixes;
            Dictionary <HttpListenerPrefix, HttpListener> dictionary;

            do
            {
                prefixes = _prefixes;
                if (prefixes.ContainsKey(prefix))
                {
                    if (prefixes[prefix] != listener)
                    {
                        throw new HttpListenerException(400, $"There's another listener for {prefix}.");
                    }
                    break;
                }
                dictionary         = new Dictionary <HttpListenerPrefix, HttpListener>(prefixes);
                dictionary[prefix] = listener;
            }while (Interlocked.CompareExchange(ref _prefixes, dictionary, prefixes) != prefixes);
        }
        private static void addPrefix(string uriPrefix, HttpListener listener)
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            var path = pref.Path;

            if (path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(400, "Invalid path."); // TODO: Code?
            }
            if (path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                throw new HttpListenerException(400, "Invalid path."); // TODO: Code?
            }
            // Listens on all the interfaces if host name cannot be parsed by IPAddress.
            getEndPointListener(pref, listener).AddPrefix(pref, listener);
        }
Exemplo n.º 28
0
        private static void addPrefix(string uriPrefix, HttpListener httpListener)
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            if (pref.Path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(400, "Invalid path."); // TODO: Code?
            }
            if (pref.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 epl = getEndPointListener(IPAddress.Any, pref.Port, pref.IsSecure, httpListener);

            epl.AddPrefix(pref, httpListener);
        }
Exemplo n.º 29
0
        private static void addSpecial(
            List <HttpListenerPrefix> prefixes, HttpListenerPrefix prefix
            )
        {
            var path = prefix.Path;

            foreach (var pref in prefixes)
            {
                if (pref.Path == path)
                {
                    var msg = "The prefix is already in use.";

                    throw new HttpListenerException(87, msg);
                }
            }

            prefixes.Add(prefix);
        }
Exemplo n.º 30
0
        private static void removePrefix(string uriPrefix, HttpListener httpListener)
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            if (pref.Path.IndexOf('%') != -1)
            {
                return;
            }

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

            var epl = getEndPointListener(pref.Host, pref.Port, httpListener, pref.IsSecure);

            epl.RemovePrefix(pref, httpListener);
        }
        private static void removePrefix(string uriPrefix, HttpListener listener)
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            var path = pref.Path;

            if (path.IndexOf('%') != -1)
            {
                return;
            }

            if (path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                return;
            }

            getEndPointListener(pref, listener).RemovePrefix(pref, listener);
        }
Exemplo n.º 32
0
        private static EndPointListener getEndPointListener(
            HttpListenerPrefix prefix, HttpListener listener
            )
        {
            var addr = convertToIPAddress(prefix.Host);

            Dictionary <int, EndPointListener> eps = null;

            if (_addressToEndpoints.ContainsKey(addr))
            {
                eps = _addressToEndpoints[addr];
            }
            else
            {
                eps = new Dictionary <int, EndPointListener> ();
                _addressToEndpoints[addr] = eps;
            }

            var port = prefix.Port;

            EndPointListener lsnr = null;

            if (eps.ContainsKey(port))
            {
                lsnr = eps[port];
            }
            else
            {
                lsnr =
                    new EndPointListener(
                        addr,
                        port,
                        listener.ReuseAddress,
                        prefix.IsSecure,
                        listener.CertificateFolderPath,
                        listener.SslConfiguration
                        );

                eps[port] = lsnr;
            }

            return(lsnr);
        }
Exemplo n.º 33
0
        private static void addPrefix(string uriPrefix, WebSocketSharp.Net.HttpListener listener)
        {
            int num;
            EndPointListener   endPointListener;
            HttpListenerPrefix httpListenerPrefix = new HttpListenerPrefix(uriPrefix);
            IPAddress          pAddress           = EndPointManager.convertToIPAddress(httpListenerPrefix.Host);

            if (!pAddress.IsLocal())
            {
                throw new WebSocketSharp.Net.HttpListenerException(87, "Includes an invalid host.");
            }
            if (!int.TryParse(httpListenerPrefix.Port, out num))
            {
                throw new WebSocketSharp.Net.HttpListenerException(87, "Includes an invalid port.");
            }
            if (!num.IsPortNumber())
            {
                throw new WebSocketSharp.Net.HttpListenerException(87, "Includes an invalid port.");
            }
            string path = httpListenerPrefix.Path;

            if (path.IndexOf('%') != -1)
            {
                throw new WebSocketSharp.Net.HttpListenerException(87, "Includes an invalid path.");
            }
            if (path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                throw new WebSocketSharp.Net.HttpListenerException(87, "Includes an invalid path.");
            }
            IPEndPoint pEndPoint = new IPEndPoint(pAddress, num);

            if (!EndPointManager._endpoints.TryGetValue(pEndPoint, out endPointListener))
            {
                endPointListener = new EndPointListener(pEndPoint, httpListenerPrefix.IsSecure, listener.CertificateFolderPath, listener.SslConfiguration, listener.ReuseAddress);
                EndPointManager._endpoints.Add(pEndPoint, endPointListener);
            }
            else if (endPointListener.IsSecure ^ httpListenerPrefix.IsSecure)
            {
                throw new WebSocketSharp.Net.HttpListenerException(87, "Includes an invalid scheme.");
            }
            endPointListener.AddPrefix(httpListenerPrefix, listener);
        }
Exemplo n.º 34
0
 private void init ()
 {
   _context = new HttpListenerContext (this);
   _inputState = InputState.RequestLine;
   _inputStream = null;
   _lineState = LineState.None;
   _outputStream = null;
   _position = 0;
   _prefix = null;
   _requestBuffer = new MemoryStream ();
 }
Exemplo n.º 35
0
    private static void removePrefix (string uriPrefix, HttpListener listener)
    {
      var pref = new HttpListenerPrefix (uriPrefix);

      var path = pref.Path;
      if (path.IndexOf ('%') != -1)
        return;

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

      getEndPointListener (pref, listener).RemovePrefix (pref, listener);
    }
Exemplo n.º 36
0
    private static void removePrefix (string uriPrefix, HttpListener listener)
    {
      var pref = new HttpListenerPrefix (uriPrefix);
      if (pref.Path.IndexOf ('%') != -1)
        return;

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

      var lsnr = getEndPointListener (pref.Host, pref.Port, listener, pref.IsSecure);
      lsnr.RemovePrefix (pref, listener);
    }
Exemplo n.º 37
0
        private static void removePrefix(string uriPrefix, HttpListener httpListener)
        {
            var pref = new HttpListenerPrefix (uriPrefix);
              if (pref.Path.IndexOf ('%') != -1)
            return;

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

              var epl = getEndPointListener (IPAddress.Any, pref.Port, pref.IsSecure, httpListener);
              epl.RemovePrefix (pref, httpListener);
        }
Exemplo n.º 38
0
    private static EndPointListener getEndPointListener (
      HttpListenerPrefix prefix, HttpListener listener)
    {
      var addr = convertToIPAddress (prefix.Host);

      Dictionary<int, EndPointListener> eps = null;
      if (_addressToEndpoints.ContainsKey (addr)) {
        eps = _addressToEndpoints[addr];
      }
      else {
        eps = new Dictionary<int, EndPointListener> ();
        _addressToEndpoints[addr] = eps;
      }

      var port = prefix.Port;

      EndPointListener lsnr = null;
      if (eps.ContainsKey (port)) {
        lsnr = eps[port];
      }
      else {
        lsnr = new EndPointListener (
          addr,
          port,
          listener.ReuseAddress,
          prefix.IsSecure,
          listener.CertificateFolderPath,
          listener.SslConfiguration);

        eps[port] = lsnr;
      }

      return lsnr;
    }
Exemplo n.º 39
0
        private HttpListener searchListener(Uri uri, out HttpListenerPrefix prefix)
        {
            prefix = null;
              if (uri == null)
            return null;

              var host = uri.Host;
              var dns = Uri.CheckHostName (host) == UriHostNameType.Dns;
              var port = uri.Port;
              var path = HttpUtility.UrlDecode (uri.AbsolutePath);
              var pathSlash = path[path.Length - 1] == '/' ? path : path + "/";

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

              if (pref.Port != port)
            continue;

              if (dns) {
            var phost = pref.Host;
            if (Uri.CheckHostName (phost) == UriHostNameType.Dns && phost != host)
              continue;
              }

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

            if (bestLen != -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;
        }
Exemplo n.º 40
0
        private static bool removeSpecial(List<HttpListenerPrefix> prefixes, HttpListenerPrefix prefix)
        {
            var path = prefix.Path;
              var cnt = prefixes.Count;
              for (var i = 0; i < cnt; i++) {
            if (prefixes[i].Path == path) {
              prefixes.RemoveAt (i);
              return true;
            }
              }

              return false;
        }
Exemplo n.º 41
0
        private static HttpListener matchFromList(
      string host, string path, List<HttpListenerPrefix> list, out HttpListenerPrefix prefix)
        {
            prefix = null;
              if (list == null)
            return null;

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

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

              return bestMatch;
        }
Exemplo n.º 42
0
        private static void addSpecial(List<HttpListenerPrefix> prefixes, HttpListenerPrefix prefix)
        {
            var path = prefix.Path;
              foreach (var pref in prefixes)
            if (pref.Path == path)
              throw new HttpListenerException (400, "The prefix is already in use."); // TODO: Code?

              prefixes.Add (prefix);
        }
Exemplo n.º 43
0
        public void RemovePrefix(HttpListenerPrefix prefix, HttpListener listener)
        {
            List<HttpListenerPrefix> current, future;
              if (prefix.Host == "*") {
            do {
              current = _unhandled;
              if (current == null)
            break;

              future = new List<HttpListenerPrefix> (current);
              if (!removeSpecial (future, prefix))
            break; // The prefix wasn't found.
            }
            while (Interlocked.CompareExchange (ref _unhandled, future, current) != current);

            checkIfRemove ();
            return;
              }

              if (prefix.Host == "+") {
            do {
              current = _all;
              if (current == null)
            break;

              future = new List<HttpListenerPrefix> (current);
              if (!removeSpecial (future, prefix))
            break; // The prefix wasn't found.
            }
            while (Interlocked.CompareExchange (ref _all, future, current) != current);

            checkIfRemove ();
            return;
              }

              Dictionary<HttpListenerPrefix, HttpListener> prefs, prefs2;
              do {
            prefs = _prefixes;
            if (!prefs.ContainsKey (prefix))
              break;

            prefs2 = new Dictionary<HttpListenerPrefix, HttpListener> (prefs);
            prefs2.Remove (prefix);
              }
              while (Interlocked.CompareExchange (ref _prefixes, prefs2, prefs) != prefs);

              checkIfRemove ();
        }
Exemplo n.º 44
0
        public void AddPrefix(HttpListenerPrefix prefix, HttpListener listener)
        {
            List<HttpListenerPrefix> current, future;
              if (prefix.Host == "*") {
            do {
              current = _unhandled;
              future = current != null
                   ? new List<HttpListenerPrefix> (current)
                   : new List<HttpListenerPrefix> ();

              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<HttpListenerPrefix> (current)
                   : new List<HttpListenerPrefix> ();

              prefix.Listener = listener;
              addSpecial (future, prefix);
            }
            while (Interlocked.CompareExchange (ref _all, future, current) != current);

            return;
              }

              Dictionary<HttpListenerPrefix, HttpListener> prefs, prefs2;
              do {
            prefs = _prefixes;
            if (prefs.ContainsKey (prefix)) {
              if (prefs[prefix] != listener)
            throw new HttpListenerException (
              400, String.Format ("There's another listener for {0}.", prefix)); // TODO: Code?

              return;
            }

            prefs2 = new Dictionary<HttpListenerPrefix, HttpListener> (prefs);
            prefs2[prefix] = listener;
              }
              while (Interlocked.CompareExchange (ref _prefixes, prefs2, prefs) != prefs);
        }