
        HTTP Servers

This folder contains three versions of a simple HTTP server.

All three HTTP versions only implement the GET method. This
keeps the servers simple, but allows us to investigate the
most important and common features of HTTP.

The first version does not implement persistent connections
(keep-alive). The second version does. See
   https://hpbn.co/http1x/#benefits-of-keepalive-connections

The third version is a multi-threaded server that can handle
multiple tcp connections simultaneously.


Compile the first server at the command-line.
   > javac HttpServer_v1.java
Then run it at the command-line.
   > java  HttpServer_v1
The server runs (by default) on localhost port 8080.
You can change the port number using a command-line
argument.

Open a tab in your browser and type this URL into the
browser's address bar.

   localhost:8080/static.html

You should see a web page appear in the browser and a lot of
logging output appear in the server's console window. Notice
that the server will have made six or seven (depending on
what browser you are using) client connections. The server
makes one TCP connection to the client for each file that
the browser wants to download,
   static.html,
   static.css,
   static.js,
   Anakin1.png,
   Anakin2.png,
   Anakin3.png,
(the seventh connection might be for favicon.png).

Those files all come from the sub-folder public_html. That folder is
the "web site" that the server is serving. (The folder served by the
server is set by the "root" property in the httpserver.properties
file.) Every http server has a specific folder that it serves from.
This is a security feature. It would be a bad idea to let the http
server serve any file from its host computer. So every http server
is restricted to serve just the files in a single folder. But that
restriction is enforced by the code in the http server. It is not
enforced by the computer's operating system. (Try to find the lines
of code in the server that enforces this restriction.) If you want
to see the restriction happen, then compile the first http client,
   > javac HttpServer_v1.java
and run it with the following command-line,
   > java HttpClient_v1 localhost 8080  /../Readme.txt
which is trying to get the Readme.txt file from this folder. That
is, the URL is trying to "escape" from the restricted folder. (Try
commenting out the code that enforces the folder restriction.)
An interesting fact is that the web browser client and the curl
client both refuse to transmit URL's that use the ".." syntax.
Try the following URLs in the browser's address bar.

   localhost:8080/../Readme.txt
   localhost:808/test/../static2.html


Shut down the server (use Control-C) and compile the second server.
   > javac HttpServer_v2.java
Then run it at the command-line.
   > java  HttpServer_v2

Once again, open a tab in your browser and type this URL into
the browser's address bar.

   localhost:8080/static.html

This time, when the page loads the server gives the browser a
persistent TCP connection, so the browser can download several
files on one TCP connection. So the whole web page will download
in one, two, three, or four client connections (depending on the
browser). If the socket connection's time-out is set long enough,
you can even reload the web page on the same tcp connection (but
the downloads will keep getting blocked by the not yet timed-out
socket connection).


Shut down the server (use Control-C) and compile the third server.
   > javac HttpServer_v3.java
Then run it at the command-line.
   > java  HttpServer_v3

Once again, open a tab in your browser and type this URL into
the browser's address bar (or just reload the tab that you
have open for the previous server).

   localhost:8080/static.html

This time, the (multi-threaded) server allows the browser to
open several tcp connections simultaneously and the browser can
download several files from each persistent, open connection.
Since several connections are being used simultaneously, their
logging output gets jumbled together. But you can still figure
out the sequence of requests the browser made and which files
are downloaded on each connection. For this multi-threaded server
you can set the socket connection's time-out value for as long
as you want and then see how the browser will keep reusing the
same open, persistent connections to repeatedly reload the web
site. The browser will not open new connections to the server
until the persistent connections time out.


In this folder there are several Java client program that are
very similar to the client programs in the http_clien.zip file.
The http clients in this folder all default to connecting to
the servers in this folder running on localhost at port 8080.
