Skip to content

Commit

Permalink
Configuration refactoring. Should allow easier virtual host support l…
Browse files Browse the repository at this point in the history
…ater on.
  • Loading branch information
lparenteau committed Apr 10, 2012
1 parent 4ad9d39 commit c047b60
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 53 deletions.
18 changes: 15 additions & 3 deletions conf/httpm.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,28 @@

# Running path, containing M source code.
# Default value : Path where ./script/httpm.sh is located.
gtmdir="$PWD/`dirname $0`/.."
export gtmdir="$PWD/`dirname $0`/.."

# Setup GT.M default.
# Default value : What is provided as default by the GT.M release.
source /usr/lib/fis-gtm/V5.5-000_x86_64/gtmprofile

# PID file location
# Default value : With GT.M's log files.
pid="$gtm_log/httpm.pid"
export pid="$gtm_log/httpm.pid"

# SCRIPT log file location
# Default value : With GT.M's log files.
log="$gtm_log/httpm_script.log"
export log="$gtm_log/httpm_script.log"

# HTTPM configuration
# Port to listen on
export httpm_port="8080"
# Server string
export httpm_server="httpm"
# Document root
export httpm_docroot="/var/www/localhost/htdocs"
# Default document name
export httpm_index="index.html"
# Error log file
export httpm_errorlog="$gtm_log/httpm_error.log"
28 changes: 20 additions & 8 deletions r/httpm.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
;

; User configuration.
do conf^userconf
do envconf

; HTTP status codes
set ^httpm("status","100")="Continue"
Expand Down Expand Up @@ -85,7 +85,18 @@
set ^httpm("ct",".jpeg")="image/jpeg"
set ^httpm("ct",".gif")="image/gif"
set ^httpm("ct",".png")="image/png"
set ^httpm("serverstring")="httpm"

quit

envconf()
;
; Configuration from environment variable
;
set conf("serverstring")=$ztrnlnm("httpm_server","","","","","VALUE")
set conf("listenon")=$ztrnlnm("httpm_port","","","","","VALUE")
set conf("docroot")=$ztrnlnm("httpm_docroot","","","","","VALUE")
set conf("index")=$ztrnlnm("httpm_index","","","","","VALUE")
set conf("errorlog")=$ztrnlnm("httpm_errorlog","","","","","VALUE")

quit

Expand All @@ -97,7 +108,7 @@
do conf
new socket,key,handle,p,socketfd
set socket="httpm"
open socket:(ZLISTEN=^httpm("conf","listen")_":TCP":znoff:zdelay:zbfsize=2048:zibfsize=2048:attach="httpm"):30:"SOCKET"
open socket:(ZLISTEN=conf("listenon")_":TCP":znoff:zdelay:zbfsize=2048:zibfsize=2048:attach="httpm"):30:"SOCKET"
use socket
write /listen(5)
; When a connection will be made and the connected socket created, it will use the next number, so we can use that to
Expand All @@ -118,7 +129,7 @@
. . set key=$key
. set handle=$piece(key,"|",2)
. ; Spawn a new process to handle the connection then close the connected socket as we won't use it from here.
. zsystem "$gtm_dist/mumps -run serve^httpm <&"_socketfd_" >&"_socketfd_" 2>>"_^httpm("conf","log")_" &"
. zsystem "$gtm_dist/mumps -run serve^httpm <&"_socketfd_" >&"_socketfd_" 2>>"_conf("errorlog")_" &"
. close socket:(socket=handle)
. use socket
close socket
Expand All @@ -129,6 +140,7 @@
; Server web page(s) to a connected client.
;
set $ZTRAP="do errhandler^httpm"
do envconf
new line,eol,delim,connection
set eol=$char(13)_$char(10)
set delim=$char(10)
Expand Down Expand Up @@ -163,7 +175,7 @@
; Ensure that the requested file exists and sits inside the document root.
set dontcare=$zsearch("")
quit:$zsearch(file)=""
quit:$zextract(file,0,$zlength(^httpm("conf","root")))'=^httpm("conf","root")
quit:$zextract(file,0,$zlength(conf("docroot")))'=conf("docroot")

do sendfile^response(file)

Expand All @@ -187,15 +199,15 @@ set request("file")=$$geturi^request(line)
; Ensure that the requested file exists and sits inside the document root.
set dontcare=$zsearch("")
if $zsearch(request("file"))="" do senderr^response("404") quit
if $zextract(request("file"),0,$zlength(^httpm("conf","root")))'=^httpm("conf","root") do senderr^response("404") quit
if $zextract(request("file"),0,$zlength(conf("docroot")))'=conf("docroot") do senderr^response("404") quit

; Read all request
for read line:timeout quit:'$test quit:line=$char(13) quit:$zeof do parsehdrs^request(line)
quit:'$test
quit:$zeof

; Send response headers
do sendresphdr^response(request("file"))
do sendresphdr^response()

; Send the content only if it isn't a HEAD request
do:request("method")'="HEAD" sendfile^response(request("file"))
Expand All @@ -218,7 +230,7 @@ set request("file")=$$geturi^request(line)
set $ztrap="halt"
new file,old
set old=$io
set file=^httpm("conf","log")
set file=conf("errorlog")
open file:(append:nofixed:wrap:noreadonly:chset="M")
use file
write "Error at "_$horolog,!,$zstatus,!
Expand Down
4 changes: 2 additions & 2 deletions r/request.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
; The resulting file is returned to the caller.
;
new file
set file=$zparse(^httpm("conf","root")_$ztranslate($zpiece(line," ",2),$char(13)))
if $zparse(file,"DIRECTORY")=file set file=file_^httpm("conf","index")
set file=$zparse(conf("docroot")_$ztranslate($zpiece(line," ",2),$char(13)))
if $zparse(file,"DIRECTORY")=file set file=file_conf("index")
quit file

parsehdrs(line)
Expand Down
20 changes: 10 additions & 10 deletions r/response.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@
. write eol_^httpm("status",status,"data")
quit

sendresphdr(file)
sendresphdr()
;
; Send the response header for the supplied file
; Send the response header for the current request.
;
new ext,ct,old,cmd,length,curdate,expdate,lastmod,buf
set curdate=$horolog
do sendstatus("200")

; Get and send content-type
set ext=$zparse(file,"TYPE")
set ext=$zparse(request("file"),"TYPE")
if $zlength(ext),$data(^httpm("ct",ext)) set ct=^httpm("ct",ext)
else set ct="text/plain"
write "Content-Type: "_ct_eol

; Get and send content-length
set old=$io
set cmd="cmd"
open cmd:(command="du -b "_file:readonly)::"PIPE"
open cmd:(command="du -b "_request("file"):readonly)::"PIPE"
use cmd
read length
close cmd
Expand All @@ -57,23 +57,23 @@
write "Expires: "_$zdate(expdate,"DAY, DD MON YEAR 24:60:SS ")_"GMT"_eol

; Send Last-Modified header
open cmd:(command="stat -c %y "_file:readonly)::"PIPE"
open cmd:(command="stat -c %y "_request("file"):readonly)::"PIPE"
use cmd
read buf
close cmd
use old
set lastmod=$$CDN^%H($zextract(buf,6,7)_"/"_$zextract(buf,9,10)_"/"_$zextract(buf,1,4))_","_$$CTN^%H($zextract(buf,12,19))
write "Last-Modified: "_$zdate(lastmod,"DAY, DD MON YEAR 24:60:SS ")_"GMT"_eol

do:connection("httpver")="HTTP/1.1" sendresphdr11(file)
do:connection("httpver")="HTTP/1.1" sendresphdr11()

; HTTP mandate a blank line between headers and content.
write eol
quit

sendresphdr11(file)
sendresphdr11()
;
; Send HTTP/1.1 specific response headers for the supplied file
; Send HTTP/1.1 specific response headers for the current request.
;
new old,cmd,md5sum

Expand All @@ -86,7 +86,7 @@
; Get and send Content-MD5
set old=$io
set cmd="cmd"
open cmd:(command="md5sum "_file:readonly)::"PIPE"
open cmd:(command="md5sum "_request("file"):readonly)::"PIPE"
use cmd
read md5sum#32
close cmd
Expand All @@ -105,7 +105,7 @@
if '$data(curdate) new curdate set curdate=$horolog
write connection("httpver")_" "_status_" "_^httpm("status",status)_eol
write "Date: "_$zdate(curdate,"DAY, DD MON YEAR 24:60:SS ")_"GMT"_eol
write:$data(^httpm("conf","server")) "Server: "_^httpm("conf","server")_eol
write "Server: "_conf("serverstring")_eol
quit

sendfile(file)
Expand Down
30 changes: 0 additions & 30 deletions r/userconf.m

This file was deleted.

0 comments on commit c047b60

Please sign in to comment.