Javazoid Links

Javazoid December 3, 2000

HttpFileServer

My recent adventures with web servers and IO streams got started with a book called Java Network Programming by Hughes, Shoffner and Hammer. The book spends its first 200 pages discussing every type of IO stream you might image -- all before it opens its first socket.... However, the extra work was definitely worth it all, as I discovered with this experiment.

Solving a problem at work...
The HttpFileServer idea got its birth from a problem I've been facing at work and at home. I have a laptop, with which I can move to either network, request the respective DNS server provide an IP address and -- presto! -- I'm in. The only problem is that I have pretty basic IP services, normally just access to the Internet and absolutely no access to file services or anything else on the local network. Normally, the solution is to ensure that an FTP server is running somewhere on the network and to start using the command line prompt.

HttpFileServer gets beyond all that. As a webserver, it offers up pages, which expose a document root specified by the server. This is great where I need to get files from local server, normally the machine I'm writing code on at work. Since the first (and probably only...) rev of this utility provides only download server, I find I have to open server's on both the laptop and one network machine to get complete bi-directional file movement.

Here's how it works
HttpFileServer is a true web server. After you've studied it a while, you may conclude that it could just as easily be a servlet, running under Apache, but please remember that I wrote it while trying to deal with socket programming. Also remember that the web server version is a lot easier to get up and running . It actually behaves just like any Java console app, except that it open a server socket on a port that you pass it as an argument.

After the main server socket threads off the request, HttpFileServer scans the Http input stream to understand the request. If the URL contains a question mark, it accepts everything between the ? character and the next space as a File parameter.

One of three things can happen:
1) if the request is a directory, HttpFileServer opens that directory, scans it and prepares some HTML that represents the page. Currently, this is pretty rudimentary HTML. But you can click on a link and either navigate a directory or view/download a file.
2) if the request is a file, HttpFileServer reads the file and outputs it to a byte array stream. The byte array is tagged as "latin1" encoding so that the browser will know how to render it. I have been using IE 5.0 and have been able to download these files to my file system by right clicking on one of HttpFileServer's file links.
3) if HttpFileServer can't figure out what to do, it puts up some basic HTML that tells you how to format a request.

Security This application could benefit from some additional security. It is intended as a console app that runs temporarily and then shuts down. Thus leaving the hacking hordes with no ports to open.

The only security feature is that the console requires a document root as a command-line arg. If it isn't passed in, nothing works. If a request doesn't contain the root, nothing works.

Notes
1. HttpFileServer worked OK on my Win 2000 box, but when I moved it to act as a server on my WIN 95 laptop, it kept looking for a fully qualified net name from my DNS server, which could resolve "machinename.workgroup". You may need to modify the serverName code in the main method if you find similar problems.
2. I originally had HttpFileServer reading HTML files as character streams. This made file access painfully slow. Opening files as byte streams and letting the browser figure out the correct character assignment proved to be much faster. For a discussion of this topic see the Brian Goetz article in JavaWorld http://javaworld.com/javaworld/jw-11-2000/jw-1117-performance.html.
3. If you wish to see a complete implementation of a Java web server, check out the Jigsaw web server at the W3 consortium, http://www.w3.org/Jigsaw/ You can download source for this application and understand that there are quite a number of niggly details in creating a web server.
4. You can read Java Network Programming. Check out the Manning web site at http://www.manning.com/Hughes
5. Spaces in command-line args. You need to specify a directory like "my documents" as c:\my&documents. Otherwise, your browser will start modifying it and HttpFileServer will get confused.

 

 

 

Download source

Any questions, please contact me gervasegallant@yahoo.com .


Copyright (c) Gervase Gallant 1999,2000.