This is my rudimentary HTTP server, written in Java. While it will never compare to something like Apache, JWS or IIS, it has the advantage that it's free, easy to install and easy to configure. Historically, I wrote this program simply to serve files over HTTP (I needed something to transfer a file...don't ask); later I added directory browsing, and most recently added CGI support.
Among the more prominent features: Prior to the major rewrite in June of 1999, the web server created a new thread to handle each connection. However, this method is very inefficient; because creating threads is a rather expensive operation (at least on Windows), I reworked the program to create a pool of request handling threads; now the threads could be recycled. This lead to a dramatic increase in performance, as I no longer had thread-creation latencies.
The next feature I added was virtual hosting support, a la Apache. Under the new program design, the life of a request handler involved many modifications to the handler's request data. Therefore, the logical progression was to create virtual hosts and serve data differently depending on the value of the Host: header.
The last major modification of the code came in July of 1999; I reworked the request handler to delegate the handling of different types of HTTP commands to different classes, thereby separating common HTTP functions from functions that are specific to, say, GET and HEAD commands. It also allowed me to easily integrate CGI support.
To run this program, type java httpd -c configfile
. Unlike
previous versions, all options must be placed in a configuration file.
Configuration files are meant to resemble Java source code...but I don't implement every single detail of the syntax. A sample configuration file is below:
01: // thibs.cfg -- configuration file for thibs 02: // Copyright (C) 1999 Darrick Wong 03: 04: httpdconf { 05: DebugLevel=-1; 06: NormalLogFile=thibs.log; 07: ErrorLogFile=thibs.log; 08: } 09: 10: VHostConf { 11: WebAdminEmail=dork@thibs.menloschool.org; 12: DocumentRoot=/home/djwong/public_html/; 13: CgiRoot=/home/djwong/public_html/; 14: HostName=djwong; 15: } 16: 17: VHostConf { 18: WebAdminEmail=dork@thibs.menloschool.org; 19: DocumentRoot=/home/djwong/public_html/kubiak/; 20: HostName=kubiak; 21: } 22: 23: VHostConf { 24: WebAdminEmail=dork@thibs.menloschool.org; 25: DocumentRoot=/home/sweaglesw/public_html/whale/; 26: CgiRoot=/home/sweaglesw/public_html/random/; 27: HostName=whale; 28: } 29: 30: VHostConf { 31: WebAdminEmail=dork@thibs.menloschool.org; 32: DocumentRoot=/; 33: HostName=default; 34: } 35: 36: ServerConf { 37: Port=2000; 38: DebugLevel=9; 39: SysAdminEmail=dork@thibs.menloschool.org; 40: VirtualHosts=default,djwong,kubiak; 41: } 42: 43: ServerConf { 44: Port=80; 45: DebugLevel=9; 46: SysAdminEmail=dork@thibs.menloschool.org; 47: VirtualHosts=default,kubiak,whale; 48: }
Let's dissect this configuration file step by step. The first two lines are
comments describing the configuration file. To initialize the web server, we
must first create a configuration object for the entire HTTP server as a whole.
Therefore, in line 4, we create a httpdconf
"object" and set some
of its properties. Here, we set the debug level to -1, indicating that we don't
want any error messages. Then, we set up log files for standard error and
standard output. To see a table of possible properties for httpdconf
,
follow this link.
Next, we must create virtual hosts. To do so, we create a bunch of
VHostConf
objects and set their properties. Every virtual host must have
a name; if none is provided, then "default" becomes the host name. No two
virtual hosts can have the same name; if you try, then the server will complain.
In the first virtual host configuration objec,t we set the webmaster's email
address for that host, we specify a document root and finally a root for CGI
support. The others are very similar, only they have slightly different
parameters. To see a table of possible properties for VHostConf
,
follow this link.
Finally, we must create servers to listen on different ports. For that, we use
the ServerConf
object. You can create as many ServerConf
objects as you like. Here, we create two servers. The first one runs
on port 2000. Notice the line that starts with VirtualHosts
. This
line lists the virtual hosts associated with that particular server; host names
are separated with commas. Hosts can be associated with multiple servers; note
that the first entry specifies the virtual host that is used if the Host: header
is invalid or nonexistant. For a list of possible properties for
ServerConf
objects, follow this link.
httpdconf
Property Name | Description |
---|---|
DebugLevel | Debugging level. Varying numbers of debug messages appear based on this value; if you specify -1, then nothing appears; if you specify 9, then everything appears. Default: -1. |
NormalLogFile | Standard output is redirected to this file. Default: no redirection. |
ErrorLogFile | Standard output is redirected to this file. Default: no redirection. |
VHostConf
Property Name | Description |
---|---|
BufferSize | Memory buffer size for file transfer. Default: 131072 bytes. |
ShowIcons | Toggles the showing of icons in directory listings. Default: true. |
ListFolderUsingTables | Toggles the use of tables in directory listings. Tables result in a cleaner appearance but increase the amount of data that must be sent. Default: true. |
ListFolders | Toggles directory browsing. If you disable this option, then users get a 403 error if there is no default file. Default: true. |
LookForDefaultFiles | Toggles the searching for default files in a folder. If you have this option enabled, then the web server will look for files to send to the browser when the user references a directory. If this option is disabled, then the user gets a directory listing. If directory listings are disabled, then the user gets a 403 error. Default: true. |
BrowserCachingEnabled | Toggles the use of the Last-Modified HTTP header.
If that header is not sent, then browsers will generally not cache anything sent
by the web server and will therefore have to re-query the server. Disabling this
option is not recommended because it is counter HTTP 1.1 policies and increases
network traffic and server load. Default: true. |
FileSystemCaseSensitive | Toggles whether or not to pay attention to the case of file names when sorting names. If disabled, AUTOEXEC.BAT is the same as autoexec.bat. Default: false. |
UseJavaScriptInTableListings | Toggles the use of JavaScript in table listings. If enabled, then the standard folder selector at the top is replaced by a drop-down list (if the drop-down list is enabled), and table headings (if tables in directory listings are enabled) become a bit more interactive. Default: true. |
UseComboBoxForDirectoryNavigation | Toggles the use of a drop-down list for directory navigation. You must have JavaScript in folder listings enabled. Default: true. |
CGITimeoutAfterDeathTimeout | Amount of time, in milliseconds, that the web server continues to send output to the client after termination of the CGI. Default: 1000ms. |
VFSRoot | The folder containing the Virtual File System files. These files are used for serving images and stylesheets for directory listings, and is also used as a hack to test error handling code and get server status information. Default: "./VFS/". |
WebAdminEmail | The e-mail address of the administrator for this virtual host. Default: webmaster@localhost. |
DocumentRoot | The folder containing the files that you want to serve over HTTP. This is equivalent of \inetpub\wwwroot in IIS, and htdocs in Apache. Default: "./htdocs/". |
CGIRoot | The folder containing CGI scripts. Any files referenced here
(through the virtual folder cgi-bin ) are executed, rather than simply
copied. Default: "./VFS/". |
HostName | This is the name of this virtual host. When accepting a
connection, the request handler reads the Host header, if any, and
tries to match it with a VHostConf object. If it doesn't find one, then it
uses the first VHostConf for configuration data. This value MUST be provided;
there is no default. |
DefaultFiles | A comma separated list of files to look for when a URI refers to a folder. The files are search for as they are listed in this line; the first file in the list is looked for first. Default: index.html,index.htm,default.html,default.htm. |
MimeDatabase | This is the name of a file containing
MIME file type information. The format of this file should be extension
=mimetype ; the extension should NOT have a leading period. The default
for this setting is "./mimetype.txt"; please change this setting so that the path
settings are correct. |
ServerConf
Property Name | Description |
---|---|
Port | The port that this particular server listens to. Default: 80. |
DebugLevel | Debugging level. Varying numbers of debug messages appear based on this value; if you specify -1, then nothing appears; if you specify 9, then everything appears. Default: -1. |
SysAdminEmail | The e-mail address of the system administrator. Default: root@localhost. |
TimeoutMillis | If the client does not send data for the specified length of time (in milliseconds), the server disconnects the connection. Default: 30000ms (30 seconds) |
VirtualHosts | This is a comma-separated list of virtual hosts that are
associated with this server. The first host in this lists is used as the default
host if the client does not specify a Host header or the value of the
Host does not match any other host name. |
Download the entire package.
Copyright ©1996-2024, Darrick Wong. All Rights Reserved. Send feedback.