lib/include/class_knjappserver_web.rb in knjappserver-0.0.28 vs lib/include/class_knjappserver_web.rb in knjappserver-0.0.29
- old
+ new
@@ -1,16 +1,21 @@
class Knjappserver
#Imports a .rhtml-file and executes it.
+ #===Examples
+ # _kas.import("/some/path/page.rhtml")
def import(filepath)
if filepath.to_s.index("../proc/self") != nil
raise Errno::EACCES, "Possible attempt to hack the appserver."
end
_httpsession.eruby.import(filepath)
end
#Redirects to another URL.
+ #===Examples
+ # _kas.redirect("someotherpage.rhtml")
+ # _kas.redirect("newpage.rhtml", :perm => true)
def redirect(url, args = {})
#Header way
if !_httpsession.alert_sent and !self.headers_sent?
if args[:perm]
_httpsession.resp.status = 301 if !self.headers_sent?
@@ -24,25 +29,31 @@
print "<script type=\"text/javascript\">location.href=\"#{url}\";</script>"
exit
end
#Sends a javascript-alert to the HTML.
+ #===Examples
+ # _kas.alert("Hello world!")
def alert(msg)
_httpsession.alert_sent = true
Knj::Web.alert(msg)
return self
end
#Define a cookies in the clients browser.
+ #===Examples
+ # _kas.cookie(:name => "MyCookie", :value => "Trala")
def cookie(cookie)
raise "No HTTP-session attached to this thread." if !_httpsession
raise "HTTP-session not active." if !_httpsession.resp
raise "Not a hash: '#{cookie.class.name}', '#{cookie}'." unless cookie.is_a?(Hash)
_httpsession.resp.cookie(cookie)
end
#Sends a header to the clients browser.
+ #===Examples
+ # _kas.header("Content-Type", "text/javascript")
def header(key, val)
raise "No HTTP-session attached to this thread." if !_httpsession
raise "HTTP-session not active." if !_httpsession.resp
_httpsession.resp.header(key, val)
end
@@ -52,21 +63,60 @@
raise "No HTTP-session attached to this thread." if !_httpsession
raise "HTTP-session not active." if !_httpsession.resp
Knj::Php.header(str)
end
+ #Returns true if the headers are already sent.
+ #===Examples
+ # _kas.headers_sent? #=> true
def headers_sent?
return true if _httpsession.resp.headers_sent
return false
end
+ #Define the size for when to automatically send headers. If you want to send hundres of kilobytes and then a header, you can use this method to do so.
+ #===Examples
+ #Set the size to 200 kb.
+ # _kas.headers_send_size = (1024 * 200)
def headers_send_size=(newsize)
- if self.headers_sent?
- raise "The headers are already sent and you cannot modify the send-size any more."
+ raise "The headers are already sent and you cannot modify the send-size any more." if self.headers_sent?
+ _httpsession.size_send = newsize.to_i
+ end
+
+ #Serves the given filepath and enables caching for it. No other content should be written to the page when using this method.
+ #===Examples
+ # _kas.header("Content-Type", "text/javascript")
+ # _kas.serve_file("somefile.js")
+ def serve_file(filepath)
+ raise "File doesnt exist: '#{filepath}'." if !File.exists?(filepath)
+ httpsess = _httpsession
+ headers = httpsess.headers
+ resp = httpsess.resp
+
+ if headers["cache-control"] and headers["cache-control"][0]
+ cache_control = {}
+ headers["cache-control"][0].scan(/(.+)=(.+)/) do |match|
+ cache_control[match[1]] = match[2]
+ end
end
- _httpsession.size_send = newsize.to_i
+ cache_dont = true if cache_control and cache_control.key?("max-age") and cache_control["max-age"].to_i <= 0
+ lastmod = File.mtime(filepath)
+
+ self.header("Last-Modified", lastmod.httpdate)
+ self.header("Expires", (Time.now + 86400).httpdate) #next day.
+
+ if !cache_dont and headers["if-modified-since"] and headers["if-modified-since"][0]
+ request_mod = Datet.in(headers["if-modified-since"].first).time
+
+ if request_mod == lastmod
+ resp.status = 304
+ return nil
+ end
+ end
+
+ httpsess.force_content(:type => :file, :path => filepath)
end
#Sends a javascript back to the browser and exits.
def back
Knj::Web.back
@@ -76,9 +126,11 @@
def inputs(*args)
return Knj::Web.inputs(args)
end
#Urlencodes a string.
+ #===Examples
+ # _kas.redirect("mypage.rhtml?arg=#{_kas.urlenc(value_variable)}")
def urlenc(str)
return Knj::Web.urlenc(str)
end
#Urldecodes a string.
\ No newline at end of file