public void Start(IEnumerable<string> urlPrefixes)
        {
            if (_listener == null)
                _listener = new HttpListener(new SocketSharpLogger(_logger));

            foreach (var prefix in urlPrefixes)
            {
                _logger.Info("Adding HttpListener prefix " + prefix);
                _listener.Prefixes.Add(prefix);
            }

            _listener.OnContext = ProcessContext;

            _listener.Start();
        }
Exemplo n.º 2
0
        public void Start(IEnumerable<string> urlPrefixes)
        {
            if (_listener == null)
                _listener = new HttpListener(new PatternsLogger(_logger), _certificatePath);

            foreach (var prefix in urlPrefixes)
            {
                _logger.Info("Adding HttpListener prefix " + prefix);
                _listener.Prefixes.Add(prefix);
            }

            _listener.OnContext = ProcessContext;

            _listener.Start();
        }
Exemplo n.º 3
0
        public bool Start(int port, NAMESettings settings)
        {
            try
            {
                this.settings = settings;

                this.httpListener = new SocketHttpListener.Net.HttpListener();
                this.httpListener.Prefixes.Add($"http://{this.nameConfiguration.AddressToListenOn}:{port}/{this.nameConfiguration.ManifestUriPrefix.TrimStart('/').TrimEnd('/')}/");
                this.httpListener.OnContext = this.OnContext;
                this.httpListener.Start();

                return(true);
            }
            catch (System.Net.HttpListenerException)
            {
                this.httpListener?.Stop();
                return(false);
            }
        }
Exemplo n.º 4
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 != "")
            {
                var 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);
                }
            }

            List <ListenerPrefix> 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);
        }
Exemplo n.º 5
0
        private void OnReadInternal(IAsyncResult ares)
        {
            //_timer.Change(Timeout.Infinite, Timeout.Infinite);
            int nread = -1;

            try
            {
                nread = _stream.EndRead(ares);
                _memoryStream.Write(_buffer, 0, nread);
                if (_memoryStream.Length > 32768)
                {
                    SendError("Bad Request", 400);
                    Close(true);
                    return;
                }
            }
            catch
            {
                if (_memoryStream != null && _memoryStream.Length > 0)
                {
                    SendError();
                }
                if (_socket != null)
                {
                    CloseSocket();
                    Unbind();
                }
                return;
            }

            if (nread == 0)
            {
                CloseSocket();
                Unbind();
                return;
            }

            if (ProcessInput(_memoryStream))
            {
                if (!_context.HaveError)
                {
                    _context.Request.FinishInitialization();
                }

                if (_context.HaveError)
                {
                    SendError();
                    Close(true);
                    return;
                }

                if (!_epl.BindContext(_context))
                {
                    const int NotFoundErrorCode = 404;
                    SendError(HttpStatusDescription.Get(NotFoundErrorCode), NotFoundErrorCode);
                    Close(true);
                    return;
                }
                HttpListener listener = _epl.Listener;
                if (_lastListener != listener)
                {
                    RemoveConnection();
                    listener.AddConnection(this);
                    _lastListener = listener;
                }

                _contextBound = true;
                listener.RegisterContext(_context);
                return;
            }
            _stream.BeginRead(_buffer, 0, BufferSize, s_onreadCallback, this);
        }
 internal HttpListenerPrefixCollection(ILogger logger, HttpListener listener)
 {
     _logger       = logger;
     this.listener = listener;
 }
Exemplo n.º 7
0
        static async Task <EndPointListener> GetEPListener(ILogger logger, string host, int port, HttpListener listener, bool secure)
        {
            var networkManager = listener.NetworkManager;

            IPAddress addr;

            if (host == "*" || host == "+")
            {
                addr = GetIpAnyAddress(listener);
            }
            else if (IPAddress.TryParse(host, out addr) == false)
            {
                try
                {
                    var all = (await networkManager.GetHostAddressesAsync(host).ConfigureAwait(false));

                    addr = (all.Length == 0 ? null : IPAddress.Parse(all[0].Address)) ??
                           GetIpAnyAddress(listener);
                }
                catch
                {
                    addr = GetIpAnyAddress(listener);
                }
            }

            Dictionary <int, EndPointListener> p = null;  // Dictionary<int, EndPointListener>

            if (!ip_to_endpoints.TryGetValue(addr.ToString(), out p))
            {
                p = new Dictionary <int, EndPointListener>();
                ip_to_endpoints[addr.ToString()] = p;
            }

            EndPointListener epl = null;

            if (p.ContainsKey(port))
            {
                epl = (EndPointListener)p[port];
            }
            else
            {
                epl     = new EndPointListener(listener, addr, port, secure, listener.Certificate, logger, listener.CryptoProvider, listener.SocketFactory, listener.MemoryStreamFactory, listener.TextEncoding, listener.FileSystem, listener.EnvironmentInfo);
                p[port] = epl;
            }

            return(epl);
        }
Exemplo n.º 8
0
 private static IPAddress GetIpAnyAddress(HttpListener listener)
 {
     return(listener.EnableDualMode ? IPAddress.IPv6Any : IPAddress.Any);
 }
Exemplo n.º 9
0
        static async Task <EndPointListener> GetEPListener(ILogger logger, string host, int port, HttpListener listener, bool secure)
        {
            var networkManager = listener.NetworkManager;

            IpAddressInfo addr;

            if (host == "*" || host == "+")
            {
                addr = GetIpAnyAddress(listener);
            }
            else if (networkManager.TryParseIpAddress(host, out addr) == false)
            {
                try
                {
                    addr = (await networkManager.GetHostAddressesAsync(host).ConfigureAwait(false)).FirstOrDefault() ??
                           GetIpAnyAddress(listener);
                }
                catch
                {
                    addr = GetIpAnyAddress(listener);
                }
            }

            Dictionary <int, EndPointListener> p = null;  // Dictionary<int, EndPointListener>

            if (!ip_to_endpoints.TryGetValue(addr.Address, out p))
            {
                p = new Dictionary <int, EndPointListener>();
                ip_to_endpoints[addr.Address] = p;
            }

            EndPointListener epl = null;

            if (p.ContainsKey(port))
            {
                epl = (EndPointListener)p[port];
            }
            else
            {
                epl     = new EndPointListener(listener, addr, port, secure, listener.Certificate, logger, listener.CryptoProvider, listener.StreamFactory, listener.SocketFactory, listener.MemoryStreamFactory, listener.TextEncoding);
                p[port] = epl;
            }

            return(epl);
        }
Exemplo n.º 10
0
 private static IpAddressInfo GetIpAnyAddress(HttpListener listener)
 {
     return(listener.EnableDualMode ? IpAddressInfo.IPv6Any : IpAddressInfo.Any);
 }
Exemplo n.º 11
0
        void OnReadInternal(IAsyncResult ares)
        {
            timer.Stop();
            int nread = -1;

            try
            {
                nread = stream.EndRead(ares);
                ms.Write(buffer, 0, nread);
                if (ms.Length > 32768)
                {
                    SendError("Bad request", 400);
                    Close(true);
                    return;
                }
            }
            catch (Exception ex)
            {
                OnReadInternalException(ms, ex);
                return;
            }

            if (nread == 0)
            {
                //if (ms.Length > 0)
                //	SendError (); // Why bother?
                CloseSocket();
                Unbind();
                return;
            }

            if (ProcessInput(ms))
            {
                if (!context.HaveError)
                {
                    context.Request.FinishInitialization();
                }

                if (context.HaveError)
                {
                    SendError();
                    Close(true);
                    return;
                }

                if (!epl.BindContext(context))
                {
                    SendError("Invalid host", 400);
                    Close(true);
                    return;
                }
                HttpListener listener = context.Listener;
                if (last_listener != listener)
                {
                    RemoveConnection();
                    listener.AddConnection(this);
                    last_listener = listener;
                }

                context_bound = true;
                listener.RegisterContext(context);
                return;
            }
            try
            {
                timer.Start(this.epl.Listener.TimeoutManager.HeaderWait);
                stream.BeginRead(buffer, 0, BufferSize, onread_cb, this);
            }
            catch (IOException ex)
            {
                OnReadInternalException(ms, ex);
            }
        }