| Javazoid Links |
Javazoid December 3, 2000 |
HttpFileServerMy 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...
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 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:
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. Notes1. 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.
Any questions, please contact me gervasegallant@yahoo.com . Copyright (c) Gervase Gallant 1999,2000. |