示例#1
0
        private void OnConnectionReceived(IAsyncResult asyncResult)
        {
            ServerLog.Write(TraceEventType.Verbose, "Enter HttpServer.OnConnectionReceived");
            try
            {
                TcpClient   client  = _listener.EndAcceptTcpClient(asyncResult);
                HttpRequest request = null;
                try
                {
                    request = new HttpRequest(client);
                }
                catch (NotSupportedException)
                {
                    // this can happen when a bad request comes in
                    // leave request as null, close the client
                    // and accept another request.
                    NetworkStream stream = client.GetStream();
                    stream.Close();
                    client.Close();
                }

                // start accepting next one before raising event in case the event handlers take too long
                _listener.BeginAcceptTcpClient(OnConnectionReceived, null);
                if (request != null)
                {
                    OnHttpRequestReceived(new HttpRequestReceivedArgs(request));
                }
            }
            catch (SocketException ex)
            {
                // ignore socket exceptions.  Just don't try to accept another connection
                ServerLog.Write(ex);
            }
            ServerLog.Write(TraceEventType.Verbose, "Exit HttpServer.OnConnectionReceived");
        }
        public void ProcessRequest(Uri baseUri, HttpListenerContext context)
        {
            var requestData = new HttpRequestData
            {
                HttpVerb         = context.Request.HttpMethod,
                HttpVersion      = context.Request.ProtocolVersion.ToString(),
                RequestUrl       = context.Request.Url,
                RemoteEndPoint   = context.Request.RemoteEndPoint,
                VirtualDirectory = baseUri.AbsolutePath,
                RelativePagePath = context.Request.Url.LocalPath.Substring(baseUri.AbsolutePath.Length)
            };

            try
            {
                if (!CheckHostOk())
                {
                    Start();
                }
            }
            catch (Exception ex)
            {
                // We can't recover from this
                ServerLog.Write(ex);
                throw;
            }
            _aspHost.ProcessRequest(requestData, new HttpResponseWrapper(context.Response));
        }
示例#3
0
        protected virtual void OnHttpRequestReceived(HttpRequestReceivedArgs e)
        {
            ServerLog.Write(TraceEventType.Verbose, "Enter HttpServer.OnHttpRequestReceived " + e.HttpRequest.RequestUri.OriginalString);
            EventHandler <HttpRequestReceivedArgs> handler = HttpRequestReceived;

            if (handler != null)
            {
                handler(this, e);
            }
            ServerLog.Write(TraceEventType.Verbose, "Exit HttpServer.OnHttpRequestReceived");
        }
 private bool Start()
 {
     try
     {
         _aspHost = (SimpleAspNetHost)ApplicationHost.CreateApplicationHost(
             typeof(SimpleAspNetHost), "/", AppPath);
     }
     catch (Exception ex)
     {
         ServerLog.Write(ex);
         return(false);
     }
     return(true);
 }
 private bool CheckHostOk()
 {
     try
     {
         // If the _aspHost hasn't been destroyed this is always true
         // otherwise it throws an exception
         if (_aspHost.StillAlive)
         {
         }
     }
     catch (Exception ex)
     {
         ServerLog.Write(ex);
         return(false);
     }
     return(true);
 }