Simple HTTP Server

The situation with the development of an HTTP server is quite different. Building a server to deliver static pages based on HTML files is far from simple, although one of the Indy demos provides a rather good starting point for this. A custom HTTP server, instead, might be interesting when building a totally dynamic site, something I'll focus on in more detail in the next chapter.

To show you how you can start the development of a custom HTTP server, I've built the HttpServ example. This program has a form with a list box used for logging requests and an IdHTTPServer component, with these settings:

object IdHTTPServerl: TIdHTTPServer

Active = True

DefaultPort = 8080

OnCommandGet = IdHTTPServerlCommandGet end

The server uses the port 8080 instead of the standard port 80, so that you can run it alongside another Web server. All of the custom code is in the OnCommandGet event handler, which simply returns a fixed page plus some information about the request itself:

procedure TForml.IdHTTPServerlCommandGet(AThread: TIdPeerThread;

RequestInfo: TIdHTTPRequestInfo; ResponseInfo: TIdHTTPResponseInfo); var

HtmlResult: String; begin

Listboxl.Items.Add (RequestInfo.Document);

// respond

HtmlResult := '<hl>HttpServ Demo</hl>'+

'<p>This is the only page you'll get from this example.</p><hr>' + '<p>Request: ' + Requestlnfo.Document + '</p>' + '<p>Host: ' + Requestlnfo.Host + '</p>' + '<p>Params: ' + Requestlnfo.UnparsedParams + '</p>' + '<p>The headers of the request follow: <br>' + RequestInfo.Headers.Text + '</p>';

Responselnfo.ContentText := HtmlResult; end;

By passing a path and some parameters in the command line of the browser, you'll see them reinterpreted and displayed. For example, Figure 21.7 shows the effect of the command line: http://localhost:8080/test?user=marco

Was this article helpful?

+1 0
Self Publishing a Book The Easy Way

Self Publishing a Book The Easy Way

This comprehensive guide will present you with a variety of self-publishing options and explore their viability. Well take a look at all types of books and book-delivery systems.

Get My Free Ebook


Post a comment