Skip to content

Commit

Permalink
Clean routes upon startup. Added some static route examples. Allow se…
Browse files Browse the repository at this point in the history
…nding of content with any response types. Moved the validation of supported methods (except for HTTP/0.9, which allow only GET) into the route handlers. Support content in request. fixes lparenteau#6
  • Loading branch information
lparenteau committed May 3, 2012
1 parent 6cd7fac commit c846a5a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
7 changes: 7 additions & 0 deletions conf/httpm.conf
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export httpm_errorlog="$gtm_log/httpm_error.log"

# Routing
gtm >>$log 2>&1 << EOF
; Ensure clean routing
kill ^httpm("routing")
; Default document root, with static file serving.
set ^httpm("routing","*","/")="do handle^static(""/var/www/localhost/htdocs"")"
; Example: Disabling default host
; set ^httpm("routing","*","/")="do handle^static(""/dev/null"")"
; Example: Adding a virtual host
; set ^httpm("routing","EXAMPLE.COM","/")="do handle^static(""/var/www/localhost/example"")"
; set ^httpm("routing","WWW.EXAMPLE.COM","/")=^httpm("routing","EXAMPLE.COM","/")
EOF
17 changes: 11 additions & 6 deletions r/httpm.m
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,11 @@ set request("uri")=$$geturi^request(line)
;
; Serve a single HTTP/1.0 or HTTP/1.1 request.
;
new request,response
new request,response,length

; Extract method
set request("method")=$$getmethod^request(line)

; Currently only support GET and HEAD methods
if request("method")'="GET",request("method")'="HEAD" do senderr^response("501") quit

; Extract the Request-URI
set request("uri")=$$geturi^request(line)

Expand All @@ -212,14 +209,22 @@ set request("uri")=$$geturi^request(line)
quit:'$test
quit:$zeof

; If the request advertised a body, read it.
if $data(request("CONTENT-LENGTH")) do
. set request("content")=""
. set length=request("CONTENT-LENGTH")
. use $io:(nodelim)
. for read line#length:timeout quit:'$test quit:$zeof set request("content")=request("content")_line set length=length-$zlength(line) quit:length<1
. use $io:(delim=delim)

; Route the request to the correct handler. This will populate the response variable.
do route^routing()

; Send response headers
do sendresphdr^response()

; Send the content only if it isn't a HEAD request and it is a 200 OK.
if request("method")'="HEAD",response("status")="200" do send^response()
; Send response content
do send^response()

quit

Expand Down
5 changes: 4 additions & 1 deletion r/static.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
; is used if a directory is requested.
;

; Support GET and HEAD methods
if request("method")'="GET",request("method")'="HEAD" set response("status")="501" quit

; Ensure that the requested file exists and sits inside the document root.
new dontcare,file
set file=$zparse(rule_request("uri"))
Expand Down Expand Up @@ -60,7 +63,7 @@ set response("headers","Date")=$zdate(curdate,"DAY, DD MON YEAR 24:60:SS ")
. ; Notice that in case the below condition is false, the else on the next line will be executed.
. if lastmod'>ifmod set response("status")="304"
else if $data(request("IF-NONE-MATCH")),md5sum=request("IF-NONE-MATCH") set response("status")="304"
else set response("status")="200" set response("file")=file
else set response("status")="200" set:request("method")'="HEAD" response("file")=file

; Get and send content-type
set ext=$zparse(file,"TYPE")
Expand Down

0 comments on commit c846a5a

Please sign in to comment.