예제 #1
0
        private void AcceptClient(IAsyncResult ar)
        {
            try
            {
                using (HTTPServerSession session = new HTTPServerSession(_socket.EndAccept(ar), _params))
                {
                    ManualResetEvent evt = ar.AsyncState as ManualResetEvent;
                    evt.Set();

                    while (session.HasMoreRequests)
                    {
                        try
                        {
                            HTTPServerResponse response = new HTTPServerResponse(session);
                            HTTPServerRequest  request  = new HTTPServerRequest(session);

                            response.Version   = request.Version;
                            response.KeepAlive = session.CanKeepAlive && request.KeepAlive && _params.KeepAlive;

                            try
                            {
                                IHTTPRequestHandler handler = _factory.CreateRequestHandler(request);
                                if (handler != null)
                                {
                                    if (request.ExpectsContinue)
                                    {
                                        response.SendContinue();
                                    }

                                    handler.HandleRequest(request, response);
                                    session.KeepAlive = response.KeepAlive && session.CanKeepAlive && _params.KeepAlive;
                                }
                                else
                                {
                                    SendErrorResponse(session, HTTPServerResponse.HTTPStatus.HTTP_NOT_IMPLEMENTED);
                                }
                            }
                            catch (Exception ex)
                            {
                                if (!response.Sent)
                                {
                                    SendErrorResponse(session, HTTPServerResponse.HTTPStatus.HTTP_INTERNAL_SERVER_ERROR);
                                }

                                OnServerException(ex);
                                break;
                            }
                        }
                        catch (HTTPNoMessageException) { break; }
                        catch (HTTPMessageException)
                        {
                            SendErrorResponse(session, HTTPServerResponse.HTTPStatus.HTTP_BAD_REQUEST);
                            break;
                        }
                    }
                }
            }
            catch (Exception)
            { }
        }
예제 #2
0
        public void HandleRequest( HTTPServerRequest request, HTTPServerResponse response )
        {
            var matcher = UrlMatcher.Match( request.URI );

            if ( matcher.Success )
            {
                WebServedFile file = WebServedFile.ActiveFiles.Find( find => find.UniqueId == matcher.Groups[1].Captures[0].Value );

                if ( matcher.Groups[2].Captures.Count > 0 && matcher.Groups[2].Captures[0].Value != "" )
                    sendFile( file, response );
                else
                    sendFileInfoPage( file, response );
            }
        }
예제 #3
0
            public void HandleRequest( HTTPServerRequest request, HTTPServerResponse response )
            {
                var root = Path.GetFullPath( RootPath );
                var path = Path.Combine( root, request.URI.Substring( 1 ) );

                if ( !path.StartsWith( root ) )
                {
                    response.StatusAndReason = HTTPServerResponse.HTTPStatus.HTTP_FORBIDDEN;
                    response.Send( );
                    return;
                }

                if ( request.URI == "/" )
                    response.SendFile( Path.Combine( RootPath, "index.html" ), "text/html" );
                else if ( File.Exists( path ) )
                    response.SendFile( path, MimeTypeResolver.DetermineMimeType( path ) );
                else
                {
                    response.StatusAndReason = HTTPServerResponse.HTTPStatus.HTTP_NOT_FOUND;
                    response.Send( );
                }
            }
예제 #4
0
 public IHTTPRequestHandler CreateRequestHandler( HTTPServerRequest request )
 {
     return new MongoHandler( );
 }
예제 #5
0
 public IHTTPRequestHandler CreateRequestHandler( HTTPServerRequest request )
 {
     return new HostedFileHandler( );
 }
예제 #6
0
        private void AcceptClient(IAsyncResult ar)
        {
            try
            {
                using (HTTPServerSession session = new HTTPServerSession(_socket.EndAccept(ar), _params))
                {
                    ManualResetEvent evt = ar.AsyncState as ManualResetEvent;
                    evt.Set();

                    while (session.HasMoreRequests)
                    {
                        try
                        {
                            HTTPServerResponse response = new HTTPServerResponse(session);
                            HTTPServerRequest request = new HTTPServerRequest(session);

                            response.Version = request.Version;
                            response.KeepAlive = session.CanKeepAlive && request.KeepAlive && _params.KeepAlive;

                            try
                            {
                                IHTTPRequestHandler handler = _factory.CreateRequestHandler(request);
                                if (handler != null)
                                {
                                    if (request.ExpectsContinue)
                                        response.SendContinue();

                                    handler.HandleRequest(request, response);
                                    if (handler.DetectInvalidPackageHeader()) break;

                                    session.KeepAlive = response.KeepAlive && session.CanKeepAlive && _params.KeepAlive;

                                    while (session.HasMoreRequests)
                                    {
                                        System.Threading.Thread.Sleep(1000);
                                    }

                                }
                                else
                                    SendErrorResponse(session, HTTPServerResponse.HTTPStatus.HTTP_NOT_IMPLEMENTED);
                            }
                            catch (Exception ex)
                            {
                                if (!response.Sent)
                                    SendErrorResponse(session, HTTPServerResponse.HTTPStatus.HTTP_INTERNAL_SERVER_ERROR);

                                OnServerException(ex);
                                break;
                            }
                        }
                        catch (HTTPNoMessageException) { break; }
                        catch (HTTPMessageException)
                        {
                            SendErrorResponse(session, HTTPServerResponse.HTTPStatus.HTTP_BAD_REQUEST);
                            break;
                        }
                    }
                }
            }
            catch (Exception)
            { }
        }