EternalLines.com: Components: HTTP Server: Documentation 


How to install the component:
  1. Uncompress the .zip file to a directory

  2. Install the elHTTPServer component
    From Delphi, open and install the UnitsDx\elHTTPServer.dpk package;
    or add elHTTPServerReg.pas to your own package.


How the component works:

The HTTP Server component works by 'listening' on a 'port' for
incoming connections.
The default port for HTTP (Web) requests is 80.

When a new connection is made to the server, the server creates a new
thread to service the connection (the client).

Once the connection is established, the server will wait for the HTTP
request from the client and decode it into a Request object.

At this stage an event handler is called (OnGetResponse) where
the user can inspect the properties of the Request object and set the
response using the properties of a Response object.


How to use the component:
  1. Drop an instance of TelHTTPServer on a form.
    Change propery values if required.

  2. Write an event handler for the OnGetResponse event handler.
    This event handler is called from within the client's thread. If you
    wish to handle the event in the main thread (for example if you access
    any VCL components) you must rather use the OnSyncGetResponse event.

    The OnGetResponse event handler is called with a Client parameter.
    The incoming request can be examined as properties of the Client.Request class.
    The response must be set to Client.Response.

    For example:
                procedure TForm1.elHTTPServer1GetResponse(Sender: AhttpServer; Client: AhttpServerClient);
                begin
                  if StrEqualNoCase(Client.Request.Method, 'GET') then
                    begin
                      Client.Response := ThttpResponse.Create;
                      Client.Response.StatusCode := http_OK;
                      Client.Response.ContentType := 'text/html';
                      Client.Response.ContentAsString := '<HTML><BODY>' +
                         'Hello world.<BR>' +
                         'You requested: ' + Client.Request.URI +
                         '</BODY></HTML>';
                    end else
                    Client.Response := httpErrorResponse(http_MethodNotFound,
                        'Method not supported',
                        httpErrorPage('Method not supported',
                        'Method ' + Client.Request.Method + ' not supported.',
                        'Your client made a request using a method that is not recognised by ' +
                        'the web server.'));
    
                end;
                
  3. Enable the server in your initialization code:
                procedure TForm1.FormCreate(Sender: TObject);
                begin
                  elHTTPServer1.Active := True;
                end;
                
  4. Run the application.

    To test your application, point your browser at the local machine (http://127.0.0.1/).


The response object - ThttpResponse:

Important properties:

            StatusCode: Integer
                This property must be set to the value of an HTTP response code.
                A full list is defined in cHTTP unit.
                The most important ones are:

                      http_OK                   = 200;
                      http_PartialContent       = 206;
                      http_MovedPermanently     = 301;
                      http_NotModified          = 304;
                      http_TemporaryRedirect    = 307;
                      http_BadRequest           = 400;
                      http_Unauthorized         = 401;
                      http_Forbidden            = 403;
                      http_NotFound             = 404;
                      http_MethodNotFound       = 405;
                      http_InternalServerError  = 500;
                      http_NotImplemented       = 501;
                      http_ServiceUnavailable   = 503;

            ContentType: String
                This property must be set to a valid MIME content type,
                for example text/html or text/ascii.

            ContentAsString: String
            ContentAsStream: AReaderEx
                Either of these properties must be set to the response content
                (eg the HTML file).
                The cReaders unit contains Reader classes that can be passed
                to ContentAsStream, for example:
                  ContentAsStream := TFileReader.Create('c:\inetpub\index.html');
                  
        
The request object - ThttpRequest:

Important properties:

            Method: String
                Usually GET, PUT, POST or HEAD.

            URI: String
                The path.

            Host: String
	        Value of the Host field.

            Referer: String
                The referer field. Page from which this link was refered.