![]() ![]() ![]() ![]() ![]() ![]() ![]() |
|||||||||||
|
Introduction
This sample program implements a simple HTTP server that uses cookies to interact with the client browser. When the sample program is run, an OmniMark program records hits on the server machine, while passing cookies to the client. The OmniMark program records the date, time, and the client's IP address for each visit. The client will see a dynamically created HTML page displaying an HTTP header, with repeated hits incrementing the "cookies-visitcount" field.
The sample program creates a TCP service on a given port, which the server uses to wait for an HTTP request. When the server has received a request, the client's IP address and the date will be recorded in the OmniMark program. The sample program first checks the request for the shutdown command. If no shutdown command is received, the server generates an HTTP response that sends a dynamically created HTML page to the client. The cookie counter will be sent with the response, and will increment each time the page is reloaded.
If the client sends a shutdown command, the server will report a shutdown message to both the OmniMark program and the client, then shut down. This will end the TCP service and disconnect the server.
; Copyright 1999 OmniMark Technologies Corporation. All rights reserved.
;
; Sample OmniMark implementation of an HTTP server that
; communicates
; with the client browser using cookies. The client makes the
; request to the server. If no user cookie is specified, the server
; generates a new ID for the user and transfers the cookie back to
; the user. If the client already has a user-id cookie, a visit-count
; cookie is incremented and returned to the client. The entire client
; request is returned for display on the client browser for testing
; and verification. The server configuration can be specified
; on the command line.
; sample command line:
; omnimark -s cookserv.xom -i ../../xin/ -x ../../lib -c svcport 1080 -d ExitCmd "^QUIT^"
; To connect to the server, the URL entered in the browser should be "http://<address>:<port number>
process
; OmniMark libraries - full path specifications must be included on the command line
declare function-library "omioprot"
include "omioprot.xin"
declare function-library "omtcp"
include "omtcp.xin"
declare function-library "omutil"
include "omutil.xin"
include "omhttp.xin"
include "omdate.xin"
; macro for message display - sets the date and time an entry is made in the message log
macro DisplayLog is
put (#ERROR & OutFile) date "=Y/=M/=D =H:=m:=s " ||
macro-end
; server configuration - specify on command line
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 {"cookserv.log"} ; log file
; other global variables
global TCPService this-service
global stream PathSep initial {mhttp_unixdirdelim}
global stream OutFile
; request handling routine
; the function reads the contents of the request shelf into Resp
define function RequestProcess
read-only stream Req
into modifiable stream Resp
as
; local variables
local stream Cookies variable
local counter UserID
local counter VisitCount
local stream Expires
; displays the contents of the request and response
; puts the output onto the Response shelf's "entity-body" item
open Resp key "entity-body" as buffer
using output as Resp key "entity-body"
do
output "<html><head><title>cookserv.xom</title></head><body>"
output "<h1>http Request</h1>"
repeat over Req
; examine each item on the Request shelf
output "<br>" || key of Req || " = %g(Req)"
again
output "<br></body></html>"
done
close Resp key "entity-body"
; get the request cookies
HttpObjectGetCookieValues Req into Cookies
do when Cookies has key "userid"
set UserID to Cookies^"userid"
else
set UserID to UTIL_UniformRand(1,100)
done
do when Cookies has key "visitcount"
set VisitCount to Cookies^"visitcount" + 1
else
set VisitCount to 1
done
set Expires to ymdhms-to-arpadate ( add-to-ymdhms now-as-ymdhms months 1 )
; set some cookies
HttpObjectSetCookieValue Resp for 'UserID' to "%d(UserID)"
HttpObjectSetCookieAttribute Resp
for 'UserID' attribute 'Expires' to Expires
HttpObjectSetCookieAttribute Resp
for 'UserID' attribute 'Path' to '/'
HttpObjectSetCookieAttribute Resp
for 'UserID' attribute 'Version' to '1'
HttpObjectSetCookieValue Resp for 'VisitCount' to "%d(VisitCount)"
HttpObjectSetCookieAttribute Resp
for 'VisitCount' attribute 'Expires' to Expires
HttpObjectSetCookieAttribute Resp
for 'VisitCount' attribute 'Path' to '/'
HttpObjectSetCookieAttribute Resp
for 'VisitCount' attribute 'Version' to '1'
;return
process
; display version
put #ERROR HTTPLibraryVersion || "%n"
; start recording
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 platform
set PathSep to mhttp_dosdirdelim
when #PLATFORM-INFO matches ("ms-dos"|"OS2"|"Windows")
; loop for request
repeat
; local variables
: HttpRequest and HttpResponse are http shelves defined in the omhttp.xin file
local HttpRequest Request
local HttpResponse Response
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 when Request^"path" matches ([mhttp_dirdelim] ul(ExitCmd))
set Response^"entity-body" to
"<html><head></head><body><h1>Server shutdown</h1></body></html>"
set Continue to false
else
RequestProcess Request into Response
done
; send the response
HttpConnectionSendResponse this-connection send Response
; exit if request
exit when not Continue
again
DisplayLog "Server stopped%n"
close OutFile
| ---- |