|
Introduction
This program implements a simple HTTP file server. It can be used with any web browser (including Lynx) . The server sends to the browser the contents of a named file, in an HTML or other format. The server also creates a log file containing the date, time, and IP address of each visit, as well as some details about which file was requested.
The program creates a TCP service on a given port, which the server uses to wait for an HTTP request. Once the server receives a request, the client's IP address is recorded in the log file ("simpserv.log"). The server then checks the requested file. If the file contains the shutdown command, the server terminates and sends a message to the browser indicating that the server has been shut down.
If the file does not contain the shutdown command, the server checks for the requested file. If no such file exists, an error message is recorded in the log file, and the response to the client is a "404 File Not Found" error message. If the file is found, the contents are loaded into the entity-body of the response, and then sent to the client. This transaction is now complete, and the server waits for the next one.
; Copyright, (c) 1998 by OmniMark Technologies Corporation ; All Rights Reserved ; ; This example illustrates the OmniMark implementation for a simple ; HTTP server. This example may be run with OMLE. All file requests ; to the server are recorded in a log file. The server configuration, ; service port, log file name, file root dir and shutdown command ; can be specified on the command line. ; ex. OMLE -s simpserv.xom -c SvcPort 1080 -d RootDir "/http/files" ; ; OmniMark libraries: The HTTP library makes use of both the I/O ; Protocol library and the TCP library, so both must be included in ; the order they appear here. declare function-library "omioprot" include "omioprot.xin" declare function-library "omtcp" include "omtcp.xin" include "omhttp.xin" ; macro for message display macro DisplayLog is put (#ERROR & OutFile) date "=Y/=M/=D =H:=m:=s " || macro-end ; server config - alternate values can be specified on command line ; if desired. global counter SvcPort initial {80} ; service port number global stream RootDir initial {"."} ; WWW root file dir global stream ExitCmd initial {"shutdown"} ; request to stop server global stream LogFile initial {"simpserv.log"} ; log file ; other global variables global TCPService this-service; identifies the TCP service global stream PathSep initial {mhttp_unixdirdelim}; refers to ; macros in omhttp.xin that specify the path delimiters for various ; platforms global stream OutFile; creates the stream that will write to the log file process ; start recording ; attaches the reopen OutFile as file LogFile ; initialize the service set this-service to TCPServiceOpen at SvcPort DisplayLog "Server started on port %d(SvcPort)%n" ; set the path separator for the platfom. The default platform is UNIX. This action will determine the platform that is being run and choose the appropriate path separator. set PathSep to mhttp_dosdirdelim when #PLATFORM-INFO matches ("ms-dos"|"OS2"|"Windows") ; loop for request: Sets the service to await a request. repeat ; local variables local HttpRequest Request; creates the request shelf local HttpResponse Response; creates the response shelf local TCPConnection this-connection local switch Continue initial {true} ; get a request HttpServiceAwaitRequest this-service receive Request connection this-connection DisplayLog TCPConnectionGetPeerIP this-connection || "%n" ; check the requested file do scan Request key "path" ; if the file is the shutdown command, this will shut down the server match [mhttp_dirdelim] ul(ExitCmd) set Response key "entity-body" to "<html><body><h1>Server Shutdown</h1></body></html>" set Continue to false match [any except "#"]*=> FileName "#"? any*=>FilePos ; sets the path separator to the correct one for the platform in use local stream PlatformFile open Platformfile as buffer using output as PlatformFile repeat scan RootDir||FileName match [any except mhttp_httpdirdelim]+=>nondelim output nondelim match mhttp_httpdirdelim output PathSep again close PlatformFile DisplayLog "Request for file %"%g(PlatformFile)%"%n" ; check for the file existence do when file PlatformFile exists ; if the file exists, put its contents into the HTTP response shelf and write to the log that this has been done DisplayLog "Sending file %"%g(PlatformFile)%"%n" set Response key "entity-body" to file PlatformFile else ; if the file is not found, note this in the log and place an error report on the HTTP response shelf. DisplayLog "File not found%n" set Response key "status-code" to "404" set Response key "reason-phrase" to "File Not Found" set Response key "entity-body" to "<html><body><h1>File not found</h1></body></html>" done done ; send the response HttpConnectionSendResponse this-connection send Response ; exit if request exit when not Continue again DisplayLog "Server stopped%n" close OutFile
---- |