gawkinet: Primitive Service

1 
1 2.8 A Primitive Web Service
1 ===========================
1 
1 Now we know enough about HTTP to set up a primitive web service that
1 just says '"Hello, world"' when someone connects to it with a browser.
1 Compared to the situation in the preceding node, our program changes the
1 role.  It tries to behave just like the server we have observed.  Since
1 we are setting up a server here, we have to insert the port number in
1 the 'localport' field of the special file name.  The other two fields
1 (HOSTNAME and REMOTEPORT) have to contain a '0' because we do not know
1 in advance which host will connect to our service.
1 
1    In the early 1990s, all a server had to do was send an HTML document
1 and close the connection.  Here, we adhere to the modern syntax of HTTP.
1 The steps are as follows:
1 
1   1. Send a status line telling the web browser that everything is okay.
1 
1   2. Send a line to tell the browser how many bytes follow in the body
1      of the message.  This was not necessary earlier because both
1      parties knew that the document ended when the connection closed.
1      Nowadays it is possible to stay connected after the transmission of
1      one web page.  This is to avoid the network traffic necessary for
1      repeatedly establishing TCP connections for requesting several
1      images.  Thus, there is the need to tell the receiving party how
1      many bytes will be sent.  The header is terminated as usual with an
1      empty line.
1 
1   3. Send the '"Hello, world"' body in HTML. The useless 'while' loop
1      swallows the request of the browser.  We could actually omit the
1      loop, and on most machines the program would still work.  First,
1      start the following program:
1 
1      BEGIN {
1        RS = ORS = "\r\n"
1        HttpService = "/inet/tcp/8080/0/0"
1        Hello = "<HTML><HEAD>" \
1                "<TITLE>A Famous Greeting</TITLE></HEAD>" \
1                "<BODY><H1>Hello, world</H1></BODY></HTML>"
1        Len = length(Hello) + length(ORS)
1        print "HTTP/1.0 200 OK"          |& HttpService
1        print "Content-Length: " Len ORS |& HttpService
1        print Hello                      |& HttpService
1        while ((HttpService |& getline) > 0)
1           continue;
1        close(HttpService)
1      }
1 
1    Now, on the same machine, start your favorite browser and let it
1 point to <http://localhost:8080> (the browser needs to know on which
1 port our server is listening for requests).  If this does not work, the
1 browser probably tries to connect to a proxy server that does not know
1 your machine.  If so, change the browser's configuration so that the
1 browser does not try to use a proxy to connect to your machine.
1