|
1 | 1 | HTTP Server |
| 2 | +=========== |
| 3 | + |
| 4 | +Usage |
| 5 | +----- |
| 6 | + |
| 7 | +To start the httpserver app, run the jar file: |
| 8 | + |
| 9 | + java -jar httpserver.jar |
| 10 | + |
| 11 | +The server runs on port 8080, serving files from the current directory. These |
| 12 | +values can be changed via command line parameters. |
| 13 | + |
| 14 | + java -jar httpserver.jar /path/to/docroot 7070 |
| 15 | + |
| 16 | +Features |
| 17 | +-------- |
| 18 | + |
| 19 | +The server will return directoy listings as HTML pages with links to the |
| 20 | +contained files and directories. Clicking on the link for a directory will |
| 21 | +open the listing for this directory, clicking on the link for a file will |
| 22 | +initiate a download for the file. |
| 23 | + |
| 24 | +File download is aided by a "Content-Disposition" header that includes the |
| 25 | +file name as well as a "Content-Length" header to enable progress monitoring. |
| 26 | + |
| 27 | +Invalid paths will result in "404 Not Found" responses. |
| 28 | + |
| 29 | +The server will return "Last-Modified" and "Etag" headers to fascilitate |
| 30 | +effective caching and therefore supports conditional requests via the |
| 31 | +"If-Match", "If-None-Match", and "If-Modified-Since" request headers. |
| 32 | + |
| 33 | +The server supports HEAD requests. |
| 34 | + |
| 35 | +Limitations |
| 36 | +----------- |
| 37 | + |
| 38 | +The server only supports GET and HEAD requests. All other requests are |
| 39 | +answered with "501 Not Implemented". |
| 40 | + |
| 41 | +Persistent (keep-alive) connections are not enabled, each request will contain |
| 42 | +the "Connection: close" header. (1) |
| 43 | + |
| 44 | + |
| 45 | +Implementation Notes |
| 46 | +-------------------- |
| 47 | + |
| 48 | +The high-level processing of HTTP requests goes something like this. |
| 49 | + |
| 50 | + 1. Open a listening TCP socket (HttpServer). |
| 51 | + 2. Accept a connection and hand it to a thread for processing (HttpServerThread). |
| 52 | + 3. Read and parse the request from the connection (HttpRequest). |
| 53 | + 4. Select the type of response to send (HttpHandler). |
| 54 | + 5. Build and send the response (HttpResponse). |
| 55 | + 6. If the connection is persistent (1), repeat from step 3. |
| 56 | + 7. Close the connection and end the thread. |
| 57 | + |
| 58 | +A classical HTTP framework would define different handlers (views) for |
| 59 | +different URLs, called "Routing" or "Traversal". These handlers would then |
| 60 | +build the appropriate response. I used a simplified version of this due to the |
| 61 | +simple nature of this server. This implementation only uses one handler |
| 62 | +and defines different types of responses which contain the respective logic. |
| 63 | +It is the handler's responsibilty to select the correct response. |
| 64 | + |
| 65 | +The HttpRequest object encapsulates all information about the request (method, |
| 66 | +path, headers). The HttpResponse objects encapsulate all information needed |
| 67 | +to send the reponse (status, headers) and implements a method to write the |
| 68 | +response to the socket. |
| 69 | + |
| 70 | +The specific reponses (DirectoryResponse, FileResponse, StatusResponse) are |
| 71 | +sub-classes of HttpResponse. Since all information needed to build the |
| 72 | +response is found in the request and the filesystem, most of the logic is found |
| 73 | +in the constructors, setting response-specific headers. The subclasses |
| 74 | +implement writeBody which writes the response-specific body to the socket. |
| 75 | +The base class will suppress the body if needed (HEAD requests, NotModified |
| 76 | +responses). |
| 77 | + |
| 78 | + |
| 79 | +Developer Notes |
| 80 | +--------------- |
| 81 | + |
| 82 | +The chosen implemenation is aimed to be closely aligned to the HTTP protocol |
| 83 | +so that the different elements (requests & responses, status & headers) are |
| 84 | +clear to anyone famliar with HTTP. |
| 85 | + |
| 86 | +I took the basic structure of how to use ServerSocket, Socket and Thread |
| 87 | +from the respective [JDK tutorial sample](https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html). |
| 88 | +All other code was conceived and written by me, sometimes taking hints from |
| 89 | +code snippets found in the JDK reference. |
| 90 | + |
| 91 | +This was my first Java project in over ten years so a lot of my time was spent |
| 92 | +reading the JDK reference and the ocasional google search, as well as the HTTP |
| 93 | +specification. Due to that I decided to do without unit tests and proper |
| 94 | +javadoc which I would usually include. |
| 95 | + |
| 96 | +(1) I had tried the implementation for persistent connections for file |
| 97 | +downloads but had to disable it again. The HTTP clients (curl, Chrome) were |
| 98 | +not able to detect the end of the response unless the connection was closed. |
| 99 | +I could not find if this is a bug in the implementation or if my understaning |
| 100 | +of how persistent connections are supposed to work is wrong. Also, I am aware |
| 101 | +that the current implementation is not complete with respect to the |
| 102 | +"Keep-Alive" header's parameters (timeout, max). |
0 commit comments