Permalink
Browse files

Fixed all cases of 'i' not being new'ed. Refactored resonse content h…

…andling so it can be split on multiple records.
  • Loading branch information...
1 parent 3ed5168 commit fddbb9f72d0cbb7686b96d3ce80d5d33245c7d21 @lparenteau committed Jul 4, 2012
Showing with 86 additions and 70 deletions.
  1. +28 −16 r/auth.m
  2. +14 −37 r/news.m
  3. +1 −1 r/request.m
  4. +39 −9 r/response.m
  5. +1 −1 r/routing.m
  6. +1 −2 r/template.m
  7. +2 −4 r/url.m
View
@@ -58,18 +58,13 @@ set response("headers","Cache-Control")="no-cache"
if $$methodis^request("PUT,POST") do postlogin(docroot,urlroot) quit
new localvar,lastmod
- set response("content")=""
; Use template engine to load the header of the page. Convention is to use '<docroot>/start.html', which can include a <title>{%}title{%}</title> line in there
; to make use of @AUTH@("logintitle")
set localvar("title")=$get(@AUTH@("logintitle"))
set response("lastmod")=$$loadcontent^template(docroot,docroot_"/start.html")
; Set login form
- set response("content")=response("content")_"<h2>Login</h2><form action="""_urlroot_"login/"" method=""post"">"
- set response("content")=response("content")_"<p><label for=""username"">Username: </label><input type=""text"" name=""username"" /></p>"
- set response("content")=response("content")_"<p><label for=""password"">Password: </label><input type=""password"" name=""password"" /></p>"
- set response("content")=response("content")_"<p><input type=""submit"" value=""Login"" /></p>"
- set response("content")=response("content")_"</form>"
+ do addcontent^response("<h2>Login</h2><form action="""_urlroot_"login/"" method=""post""><p><label for=""username"">Username: </label><input type=""text"" name=""username"" /></p><p><label for=""password"">Password: </label><input type=""password"" name=""password"" /></p><p><input type=""submit"" value=""Login"" /></p></form>")
; Use template engine to load the footer of the page. Convention is to use '<docroot>/end.html'.
set lastmod=$$loadcontent^template(docroot,docroot_"/end.html")
@@ -85,27 +80,26 @@ set response("headers","Content-Type")="text/html"
;
; Handle the login form data received
;
- new content
+ new content,i
for i=1:1:2 do
. set value=$zpiece(request("content"),"&",i)
. set content($zpiece(value,"=",1))=$zpiece(value,"=",2,$zlength(line))
new localvar,lastmod,session
- set response("content")=""
; Use template engine to load the header of the page. Convention is to use '<docroot>/start.html', which can include a <title>{%}title{%}</title> line in there
; to make use of @AUTH@("logintitle")
set localvar("title")=$get(@AUTH@("logintitle"))
set response("lastmod")=$$loadcontent^template(docroot,docroot_"/start.html")
if $data(@AUTH@("accounts",content("username"))) do if 1
- . if @AUTH@("accounts",content("username"),"password")=$$hash(content("password"),@AUTH@("accounts",content("username"),"salt")) do
+ . if @AUTH@("accounts",content("username"),"password")=$$hash(content("password"),@AUTH@("accounts",content("username"),"salt")) do if 1
. . set session=$$encode^base64($$salt(16))
- . . set response("content")=response("content")_"Welcome back "_content("username")_"!"
+ . . do addcontent^response("Welcome back "_content("username")_"!")
. . set response("headers","Set-Cookie")="session="_session_"; Path=/; HttpOnly"
. . set @SESSION@(session)=content("username")
- . else set response("content")=response("content")_"Wrong username or password..."
- else set response("content")=response("content")_"Wrong username or password..."
+ . else do addcontent^response("Wrong username or password...") if 1
+ else do addcontent^response("Wrong username or password...") if 1
; Use template engine to load the footer of the page. Convention is to use '<docroot>/end.html'.
set lastmod=$$loadcontent^template(docroot,docroot_"/end.html")
@@ -123,14 +117,12 @@ set response("headers","Content-Type")="text/html"
;
new localvar,lastmod
- set response("content")=""
; Use template engine to load the header of the page. Convention is to use '<docroot>/start.html', which can include a <title>{%}title{%}</title> line in there
; to make use of @AUTH@("logintitle")
set localvar("title")=$get(@AUTH@("logintitle"))
set response("lastmod")=$$loadcontent^template(docroot,docroot_"/start.html")
- ; Set login form
- set response("content")=response("content")_"<h2>Logged out!</h2>"
+ do addcontent^response("<h2>Logged out!</h2>")
set response("headers","Set-Cookie")="session=deleted; Path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT"
kill @SESSION@($get(request("headers","COOKIE","session")," "))
@@ -155,7 +147,7 @@ set response("headers","Content-Type")="text/html"
;
; Return a pseudo-random value consisting of count digits.
;
- new salt
+ new salt,i
set salt=""
for i=1:1:count set salt=salt_$random(10)
@@ -173,3 +165,23 @@ set response("headers","Content-Type")="text/html"
; Return 1 if the request is from an authenticated user, 0 otherwise.
;
quit $data(@SESSION@($get(request("headers","COOKIE","session")," ")))
+
+adduser(username,password,AUTH)
+ ;
+ ; Add a new user
+ ;
+ new salt,saltedpass
+
+ ; Default AUTH
+ if '$data(AUTH) new AUTH set AUTH="^AUTH"
+
+ set salt=$$salt(64)
+ set saltedpass=$$hash(password,salt)
+
+ tstart ():serial
+ if '$data(@AUTH@("accounts",username)) do
+ . set @AUTH@("accounts",username,"password")=saltedpass
+ . set @AUTH@("accounts",username,"salt")=salt
+ tcommit
+
+ quit
View
@@ -102,7 +102,7 @@ set response("lastmod")="0,0"
; Footer
set footer="</feed>"
- set response("content")=header_author_posts_footer
+ do addcontent^response(header_author_posts_footer)
set response("headers","Content-Type")="application/atom+xml"
@@ -114,7 +114,6 @@ set response("headers","Content-Type")="application/atom+xml"
;
new lastmod
- set response("content")=""
; Use template engine to load the header of the page. Convention is to use '<docroot>/start.html', which can include a <title>{%}title{%}</title> line in there
; to make use of @NEWS@("title"), and {%}auth{%} to insert login/logout/admin links
set localvar("title")=$get(@NEWS@("title"))
@@ -124,10 +123,9 @@ set response("lastmod")=$$loadcontent^template(docroot,docroot_"/start.html")
new postid,cnt
set postid=$order(@NEWS@("post",""),-1)
set cnt=0
- set response("content")=response("content")_"<h2>"_$get(@NEWS@("title"))_"</h2>"
+ do addcontent^response("<h2>"_$get(@NEWS@("title"))_"</h2>")
for quit:postid="" quit:cnt=5 do
- . set response("content")=response("content")_"<h3>"_@NEWS@("post",postid,"title")_"</h3><h4>"_$zdate(@NEWS@("post",postid,"published"),"DAY, DD MON YEAR")_"</h4>"
- . set response("content")=response("content")_"<p>"_@NEWS@("post",postid,"content")_"</p>"
+ . do addcontent^response("<h3>"_@NEWS@("post",postid,"title")_"</h3><h4>"_$zdate(@NEWS@("post",postid,"published"),"DAY, DD MON YEAR")_"</h4><p>"_@NEWS@("post",postid,"content")_"</p>")
. ; Update last modified date if needed.
. set:$$isnewer^date(@NEWS@("post",postid,"updated"),response("lastmod")) response("lastmod")=@NEWS@("post",postid,"updated")
. set response("glolist","@NEWS@(""post"","_postid_",""updated"")")=""
@@ -136,7 +134,7 @@ set response("content")=response("content")_"<h2>"_$get(@NEWS@("title"))_"</h2>"
. set cnt=cnt+1
; Add link to archives (all posts)
- set response("content")=response("content")_"<p>Browse <a href="""_urlroot_"archive/"">older posts</a>.</p>"
+ do addcontent^response("<p>Browse <a href="""_urlroot_"archive/"">older posts</a>.</p>")
; Use template engine to load the footer of the page. Convention is to use '<docroot>/end.html'.
set lastmod=$$loadcontent^template(docroot,docroot_"/end.html")
@@ -154,7 +152,6 @@ set response("headers","Content-Type")="text/html"
;
new lastmod
- set response("content")=""
; Use template engine to load the header of the page. Convention is to use '<docroot>/start.html', which can include a <title>{%}title{%}</title> line in there
; to make use of @NEWS@("title")
set localvar("title")=$get(@NEWS@("title"))_" | Archive"
@@ -164,9 +161,9 @@ set response("lastmod")=$$loadcontent^template(docroot,docroot_"/start.html")
new postid,cnt
set postid=$order(@NEWS@("post",""),-1)
set cnt=0
- set response("content")=response("content")_"<h2>"_$get(@NEWS@("title"))_"</h2><h3>Archive</h3>"
+ do addcontent^response("<h2>"_$get(@NEWS@("title"))_"</h2><h3>Archive</h3>")
for quit:postid="" do
- . set response("content")=response("content")_"<a href="""_urlroot_"posts/"_postid_""">"_@NEWS@("post",postid,"title")_"</a><br>"
+ . do addcontent^response("<a href="""_urlroot_"posts/"_postid_""">"_@NEWS@("post",postid,"title")_"</a><br>")
. ; Update last modified date if needed.
. set:$$isnewer^date(@NEWS@("post",postid,"updated"),response("lastmod")) response("lastmod")=@NEWS@("post",postid,"updated")
. set response("glolist","@NEWS@(""post"","_postid_",""updated"")")=""
@@ -191,16 +188,13 @@ set response("headers","Content-Type")="text/html"
set postid=$zpiece(request("uri"),"/",4,4)
if '$data(@NEWS@("post",postid)) do set^response(405) quit
- set response("content")=""
; Use template engine to load the header of the page. Convention is to use '<docroot>/start.html', which can include a <title>{%}title{%}</title> line in there
; to make use of @NEWS@("title")
set localvar("title")=$get(@NEWS@("title"))_" | "_@NEWS@("post",postid,"title")
set response("lastmod")=$$loadcontent^template(docroot,docroot_"/start.html")
; A post from database
- set response("content")=response("content")_"<h2>"_$get(@NEWS@("title"))_"</h2>"
- set response("content")=response("content")_"<h3>"_@NEWS@("post",postid,"title")_"</h3><h4>"_$zdate(@NEWS@("post",postid,"published"),"DAY, DD MON YEAR")_"</h4>"
- set response("content")=response("content")_"<p>"_@NEWS@("post",postid,"content")_"</p>"
+ do addcontent^response("<h2>"_$get(@NEWS@("title"))_"</h2><h3>"_@NEWS@("post",postid,"title")_"</h3><h4>"_$zdate(@NEWS@("post",postid,"published"),"DAY, DD MON YEAR")_"</h4><p>"_@NEWS@("post",postid,"content")_"</p>")
; Update last modified date if needed.
set:$$isnewer^date(@NEWS@("post",postid,"updated"),response("lastmod")) response("lastmod")=@NEWS@("post",postid,"updated")
set response("glolist","@NEWS@(""post"","_postid_",""updated"")")=""
@@ -224,7 +218,6 @@ set response("headers","Content-Type")="text/html"
; This page is only accessible to logged in user
if '$$isauthenticated^auth() do set^response(403) quit
- set response("content")=""
; Use template engine to load the header of the page. Convention is to use '<docroot>/start.html', which can include a <title>{%}title{%}</title> line in there
; to make use of @NEWS@("title")
set localvar("title")=$get(@NEWS@("title"))_" | Admin"
@@ -234,13 +227,9 @@ set response("lastmod")=$$loadcontent^template(docroot,docroot_"/start.html")
new postid,cnt
set postid=$order(@NEWS@("post",""),-1)
set cnt=0
- set response("content")=response("content")_"<h2>"_$get(@NEWS@("title"))_"</h2>"
- set response("content")=response("content")_"<p>Create a <a href="""_urlroot_"admin/add/"">New Post</a></p>"
- set response("content")=response("content")_"<h3>All Posts</h3>"
+ do addcontent^response("<h2>"_$get(@NEWS@("title"))_"</h2><p>Create a <a href="""_urlroot_"admin/add/"">New Post</a></p><h3>All Posts</h3>")
for quit:postid="" do
- . set response("content")=response("content")_"<a href="""_urlroot_"posts/"_postid_""">"_@NEWS@("post",postid,"title")_"</a>"
- . set response("content")=response("content")_" <a href="""_urlroot_"admin/delete/"_postid_""">Delete</a>"
- . set response("content")=response("content")_" <a href="""_urlroot_"admin/edit/"_postid_""">Edit</a><br>"
+ . do addcontent^response("<a href="""_urlroot_"posts/"_postid_""">"_@NEWS@("post",postid,"title")_"</a> <a href="""_urlroot_"admin/delete/"_postid_""">Delete</a> <a href="""_urlroot_"admin/edit/"_postid_""">Edit</a><br>")
. ; Update last modified date if needed.
. set:$$isnewer^date(@NEWS@("post",postid,"updated"),response("lastmod")) response("lastmod")=@NEWS@("post",postid,"updated")
. set response("glolist","@NEWS@(""post"","_postid_",""updated"")")=""
@@ -261,15 +250,14 @@ set response("headers","Content-Type")="text/html"
;
; Handle various admin actions
;
- new lastmod,action,postid,content,value
+ new lastmod,action,postid,content,value,i
; This page is only accessible to logged in user
if '$$isauthenticated^auth() do set^response(404) quit
set action=$zpiece(request("uri"),"/",4,4)
set postid=$zpiece(request("uri"),"/",5,5)
- set response("content")=""
; Use template engine to load the header of the page. Convention is to use '<docroot>/start.html', which can include a <title>{%}title{%}</title> line in there
; to make use of @NEWS@("title")
set localvar("title")=$get(@NEWS@("title"))_" | Admin"
@@ -278,7 +266,7 @@ set response("lastmod")=$$loadcontent^template(docroot,docroot_"/start.html")
;
if action="add" do
. if $$methodis^request("PUT,POST") do if 1
- . . set response("content")=response("content")_"<h2>Post published!</h2>"
+ . . do addcontent^response("<h2>Post published!</h2>")
. . for i=1:1:3 do
. . . set value=$zpiece(request("content"),"&",i)
. . . set content($zpiece(value,"=",1))=$$paragraph($$decode^url($zpiece(value,"=",2,$zlength(line))))
@@ -291,20 +279,9 @@ set response("lastmod")=$$loadcontent^template(docroot,docroot_"/start.html")
. . set:$get(@NEWS@("post",postid,"published"))="" @NEWS@("post",postid,"published")=@NEWS@("post",postid,"updated")
. . tcommit
. else do
- . . set response("content")=response("content")_"<h2>New post</h2><form enctype=""application/x-www-form-urlencoded"" accept-charset=""UTF-8"" action="""_urlroot_"admin/add/"" method=""post"">"
- . . set response("content")=response("content")_"<p><label for=""title"">Title: </label><input type=""text"" name=""title"" /></p>"
- . . set response("content")=response("content")_"<p><label for=""summary"">Summary: </label><input type=""text"" name=""summary"" /></p>"
- . . set response("content")=response("content")_"<p><label for=""content"">Content: </label><textarea wrap=""virtual"" name=""content"" ></textarea></p>"
- . . set response("content")=response("content")_"<p><input type=""submit"" value=""Publish"" /></p>"
- . . set response("content")=response("content")_"</form>"
- else if action="delete" kill @NEWS@("post",postid) set response("content")=response("content")_"<h2>Post deleted</h2>"
- else if action="edit" do
- . set response("content")=response("content")_"<h2>New post</h2><form enctype=""application/x-www-form-urlencoded"" accept-charset=""UTF-8"" action="""_urlroot_"admin/add/"_postid_""" method=""post"">"
- . set response("content")=response("content")_"<p><label for=""title"">Title: </label><input type=""text"" name=""title"" value="""_@NEWS@("post",postid,"title")_"""/></p>"
- . set response("content")=response("content")_"<p><label for=""summary"">Summary: </label><input type=""text"" name=""summary"" value="""_@NEWS@("post",postid,"summary")_"""/></p>"
- . set response("content")=response("content")_"<p><label for=""content"">Content: </label><textarea wrap=""virtual"" name=""content"" >"_@NEWS@("post",postid,"content")_"</textarea></p>"
- . set response("content")=response("content")_"<p><input type=""submit"" value=""Publish"" /></p>"
- . set response("content")=response("content")_"</form>"
+ . . do addcontent^response("<h2>New post</h2><form enctype=""application/x-www-form-urlencoded"" accept-charset=""UTF-8"" action="""_urlroot_"admin/add/"" method=""post""><p><label for=""title"">Title: </label><input type=""text"" name=""title"" /></p><p><label for=""summary"">Summary: </label><input type=""text"" name=""summary"" /></p><p><label for=""content"">Content: </label><textarea wrap=""virtual"" name=""content"" ></textarea></p><p><input type=""submit"" value=""Publish"" /></p></form>") if 1
+ else if action="delete" kill @NEWS@("post",postid) do addcontent^response("<h2>Post deleted</h2>") if 1
+ else if action="edit" do addcontent^response("<h2>New post</h2><form enctype=""application/x-www-form-urlencoded"" accept-charset=""UTF-8"" action="""_urlroot_"admin/add/"_postid_""" method=""post""><p><label for=""title"">Title: </label><input type=""text"" name=""title"" value="""_@NEWS@("post",postid,"title")_"""/></p><p><label for=""summary"">Summary: </label><input type=""text"" name=""summary"" value="""_@NEWS@("post",postid,"summary")_"""/></p><p><label for=""content"">Content: </label><textarea wrap=""virtual"" name=""content"" >"_@NEWS@("post",postid,"content")_"</textarea></p><p><input type=""submit"" value=""Publish"" /></p></form>") if 1
else kill response("content"),response("lastmod") do set^response(404) quit
; Use template engine to load the footer of the page. Convention is to use '<docroot>/end.html'.
View
@@ -57,7 +57,7 @@
;
; Compare current request's method with the comma seperated list of methods.
;
- new p
+ new p,i
for i=1:1 set p=$zpiece(methods,",",i,i) quit:(p="")!(p=request("method"))
; Method is in the supplied list
quit:p'="" 1
View
@@ -25,7 +25,7 @@ set response("status")=status
if $data(conf("status",status,"data")) do
. set response("headers","Content-Type")=conf("status",status,"ct")
. set response("headers","Content-Length")=conf("status",status,"cl")
- . set response("content")=conf("status",status,"data")
+ . do addcontent(conf("status",status,"data"))
quit
@@ -35,7 +35,7 @@ set response("status")=status
;
; Populate the response
- new response
+ new response,i
do set^response(status)
@@ -46,7 +46,10 @@ set response("status")=status
write eol
; Send error data, if any
- write:$data(response("content")) response("content")
+ set i=$order(response("content",""))
+ for quit:i="" do
+ . write response("content",i)
+ . set i=$order(response("content",i))
; Log request/response
set:'$data(response("date")) response("date")=$horolog
@@ -110,7 +113,7 @@ set response("status")=status
. . . close cmd
. . . set length=$zpiece(length,$char(9),1)
. . . if 1
- . . else set:$data(response("content")) length=$zlength(response("content"))
+ . . else set:$data(response("content")) length=response("content-length")
. . set:$data(length) response("headers","Content-Length")=length
set:$data(response("lastmod")) response("headers","Last-Modified")=$zdate(response("lastmod"),"DAY, DD MON YEAR 24:60:SS ")_"GMT"
@@ -137,7 +140,7 @@ write connection("HTTPVER")_" "_response("status")_" "_conf("status",response("s
; If a file has been specified, send it.
if request("method")'="HEAD" do
. if $data(response("file")) do sendfile(response("file")) if 1
- . else do:$data(response("content")) sendcontent(response("content"))
+ . else do:$data(response("content")) sendcontent()
; Log request/response
do common^log()
@@ -170,10 +173,17 @@ write connection("HTTPVER")_" "_response("status")_" "_conf("status",response("s
set:'$data(response("headers","Content-Length")) response("headers","Content-Length")=length
quit
-sendcontent(data)
+sendcontent()
;
- ; Send supplied data as content
+ ; Send response("content",...) as content
;
+ new data,i
+
+ set i=$order(response("content",""))
+ set data=""
+ for quit:i="" do
+ . set data=data_response("content",i)
+ . set i=$order(response("content",i))
if $data(response("encoding")) do
. new cmd,old,arg
@@ -216,14 +226,17 @@ set response("headers","Cache-Control")="max-age = 86400"
;
; Calculate the MD5SUM of the response's content (or file)
;
- new old,cmd,file
+ new old,cmd,file,i
set old=$io
set cmd="md5sum"
set file=$get(response("file"),"-")
open cmd:(command="md5sum "_file)::"PIPE"
use cmd
if '$data(response("file")) do
- . write response("content")
+ . set i=$order(response("content",""))
+ . for quit:i="" do
+ . . write response("content",i)
+ . . set i=$order(response("content",i))
. write /eof
read response("headers","Content-MD5")#32
close cmd
@@ -233,3 +246,20 @@ read response("headers","Content-MD5")#32
set response("headers","ETag")=response("headers","Content-MD5")
quit
+
+addcontent(data)
+ ;
+ ; Add supplied data to the response's content
+ ;
+ new last,max,i,length
+
+ ; Default to 4080 bytes, since the default record size is 4096.
+ set max=$get(conf("cacherecsize"),4080)
+ set length=$zlength(data)
+
+ if $data(response("content"))=0 set last=0,response("content-length")=0
+ else set last=$order(response("content",""),-1)+1
+
+ for i=1:max:length set response("content",last)=$zextract(data,i,i+max-1),response("content-length")=response("content-length")+$zlength(response("content",last)),last=last+1
+
+ quit
Oops, something went wrong.

0 comments on commit fddbb9f

Please sign in to comment.