Exemple #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)
            { }
        }
Exemple #2
0
        /// <summary>
        /// Writes the file directly to the http client.
        /// </summary>
        private void sendFile( WebServedFile file, HTTPServerResponse response )
        {
            response.ContentLength = file.File.Length;
            response.Set( "Content-Disposition", "attachment; filename=\"" + file.File.Name + "\"" );

            try
            {
                using ( Stream outStream = response.Send( ) )
                using ( FileStream fileInStream = new FileStream( file.File.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ) )
                {
                    // Iterate through the file in chunk-sized increments.
                    for ( long i = 0; i < file.File.Length; i += Protocol.TransferChunkSize )
                    {
                        // Calculate the number of bytes we're about to send.
                        int numBytes = (int) Math.Min( Protocol.TransferChunkSize, file.File.Length - i );

                        // Read in the chunk from a file, write it to the network.
                        byte[] chunk = new byte[numBytes];
                        fileInStream.Read( chunk, 0, numBytes );
                        outStream.Write( chunk, 0, chunk.Length );
                    }
                }
            }
            catch ( IOException ) { }
            catch ( SocketException ) { } // User hung up (closed their browser).
        }
Exemple #3
0
        private void SendErrorResponse(HTTPServerSession session, HTTPServerResponse.HTTPStatus status)
        {
            HTTPServerResponse response = new HTTPServerResponse(session);

            response.Version         = HTTPMessage.HTTP_1_1;
            response.StatusAndReason = status;
            response.KeepAlive       = false;
            response.Send();
            session.KeepAlive = false;
        }
Exemple #4
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 );
            }
        }
Exemple #5
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( );
                }
            }
Exemple #6
0
 /// <summary>
 /// Writes an xhtml page with information about the given file, including a link to download it.
 /// (The file should download automatically using META REFRESH).
 /// </summary>
 private void sendFileInfoPage( WebServedFile file, HTTPServerResponse response )
 {
     response.ContentType = "text/html";
     try
     {
         using ( Stream ostr = response.Send( ) )
         using ( TextWriter tw = new StreamWriter( ostr ) )
             tw.WriteLine( Resources.sendFilePage.Replace( "${fileUrl}", file.GetDownloadPath( ) )
                 .Replace( "${fileName}", file.File.Name ) // I should be shot for chaining these together...but with sendFile.html under 500 bytes,
                 .Replace( "${sender}", Configuration.CurrentSettings.Username ) // the performance hit is pretty negligible.
                 .Replace( "${fileSize}", Util.FormatFileSize( file.File.Length ) ) );
     }
     catch ( IOException ) { }
     catch ( SocketException ) { } // User hung up (closed their browser).
 }
Exemple #7
0
 private void SendErrorResponse(HTTPServerSession session, HTTPServerResponse.HTTPStatus status)
 {
     HTTPServerResponse response = new HTTPServerResponse(session);
     response.Version = HTTPMessage.HTTP_1_1;
     response.StatusAndReason = status;
     response.KeepAlive = false;
     response.Send();
     session.KeepAlive = false;
 }
Exemple #8
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)
            { }
        }