Rewrote state based redirection
[project/luci.git] / libs / web / luasrc / http.lua
index 154f51a..c5a85ea 100644 (file)
@@ -24,14 +24,20 @@ limitations under the License.
 
 ]]--
 
-module("luci.http", package.seeall)
-local ltn12 = require("luci.ltn12")
-require("luci.http.protocol")
-require("luci.util")
+local ltn12 = require "luci.ltn12"
+local proto = require "luci.http.protocol"
+local util  = require "luci.util"
+local string = require "string"
+local coroutine = require "coroutine"
 
-context = luci.util.threadlocal()
+local pairs, tostring, error = pairs, tostring, error
 
-Request = luci.util.class()
+--- LuCI Web Framework high-level HTTP functions.
+module "luci.http"
+
+context = util.threadlocal()
+
+Request = util.class()
 function Request.__init__(self, env, sourcein, sinkerr)
        self.input = sourcein
        self.error = sinkerr
@@ -44,7 +50,7 @@ function Request.__init__(self, env, sourcein, sinkerr)
        self.message = {
                env = env,
                headers = {},
-               params = luci.http.protocol.urldecode_params(env.QUERY_STRING or ""),
+               params = protocol.urldecode_params(env.QUERY_STRING or ""),
        }
        
        self.parsed_input = false
@@ -80,6 +86,14 @@ function Request.formvaluetable(self, prefix)
        return vals
 end
 
+function Request.content(self)
+       if not self.parsed_input then
+               self:_parse_input()
+       end
+       
+       return self.message.content, self.message.content_length
+end
+
 function Request.getcookie(self, name)
   local c = string.gsub(";" .. (self:getenv("HTTP_COOKIE") or "") .. ";", "%s*;%s*", ";")
   local p = ";" .. name .. "=(.-);"
@@ -100,7 +114,7 @@ function Request.setfilehandler(self, callback)
 end
 
 function Request._parse_input(self)
-       luci.http.protocol.parse_message_body(
+       protocol.parse_message_body(
                 self.input,
                 self.message,
                 self.filehandler
@@ -121,6 +135,13 @@ function close()
        end
 end
 
+--- Return the request content if the request was of unknown type.
+-- @return     HTTP request body
+-- @return     HTTP request body length
+function content()
+       return context.request:content()
+end
+
 --- Get a certain HTTP input value or a table of all input values.
 -- @param name         Name of the GET or POST variable to fetch
 -- @param noparse      Don't parse POST data before getting the value
@@ -171,7 +192,22 @@ end
 --- Set the mime type of following content data.
 -- @param mime Mimetype of following content
 function prepare_content(mime)
-       header("Content-Type", mime)
+       if not context.headers or not context.headers["content-type"] then
+               if mime == "application/xhtml+xml" then
+                       if not getenv("HTTP_ACCEPT") or
+                         not getenv("HTTP_ACCEPT"):find("application/xhtml+xml", nil, true) then
+                               mime = "text/html; charset=UTF-8"
+                       end
+                       header("Vary", "Accept")
+               end
+               header("Content-Type", mime)
+       end
+end
+
+--- Get the RAW HTTP input source
+-- @return     HTTP LTN12 source
+function source()
+       return context.request.input
 end
 
 --- Set the HTTP status code and status message.
@@ -210,6 +246,7 @@ function write(content, src_err)
                        end
                        if not context.headers["cache-control"] then
                                header("Cache-Control", "no-cache")
+                               header("Expires", "0")
                        end
                        
                        
@@ -247,10 +284,10 @@ end
 -- @param no_plus      Don't decode + to " "
 -- @return                     URL-decoded string
 -- @see urlencode
-urldecode = luci.http.protocol.urldecode
+urldecode = protocol.urldecode
 
 --- Return the URL-encoded equivalent of a string.
 -- @param str          Source string
 -- @return                     URL-encoded string
 -- @see urldecode
-urlencode = luci.http.protocol.urlencode
+urlencode = protocol.urlencode