Now we know enough about HTTP to set up a primitive web service that just
says "Hello, world"
when someone connects to it with a browser.
Compared
to the situation in the preceding section, our program changes the role. It
tries to behave just like the server we have observed. Since we are setting
up a server here, we have to insert the port number in the `localport'
field of the special file name. The other two fields (hostname and
remoteport) have to contain a `0' because we do not know in
advance which host will connect to our service.
In the early 1990s, all a server had to do was send an HTML document and close the connection. Here, we adhere to the modern syntax of HTTP. The steps are as follows:
"Hello, world"
body
in HTML.
The useless while
loop swallows the request of the browser.
We could actually omit the loop, and on most machines the program would still
work.
First, start the following program:
BEGIN { RS = ORS = "\r\n" HttpService = "/inet/tcp/8080/0/0" Hello = "<HTML><HEAD>" \ "<TITLE>A Famous Greeting</TITLE></HEAD>" \ "<BODY><H1>Hello, world</H1></BODY></HTML>" Len = length(Hello) + length(ORS) print "HTTP/1.0 200 OK" |& HttpService print "Content-Length: " Len ORS |& HttpService print Hello |& HttpService while ((HttpService |& getline) > 0) continue; close(HttpService) }
Now, on the same machine, start your favorite browser and let it point to http://localhost:8080 (the browser needs to know on which port our server is listening for requests). If this does not work, the browser probably tries to connect to a proxy server that does not know your machine. If so, change the browser's configuration so that the browser does not try to use a proxy to connect to your machine.