* Added support for CGI SGI
[project/luci.git] / libs / web / luasrc / http / protocol.lua
index 2c83257c953ad2fbbc68c528913147e5dde72512..d9259f660419df777f6123914e1411d336650c97 100644 (file)
@@ -327,6 +327,20 @@ end
 -- Parse a http message
 function parse_message( data, filecb )
 
+       local reader  = _linereader( data )
+       local message = parse_message_header( reader )
+
+       if message then
+               parse_message_body( reader, message, filecb )
+       end
+
+       return message
+end
+
+
+-- Parse a http message header
+function parse_message_header( data )
+
        -- Create a line reader
        local reader  = _linereader( data )
        local message = { }
@@ -356,9 +370,6 @@ function parse_message( data, filecb )
 
                        message.headers = hdrs
 
-                       -- Get content
-                       local clen = ( hdrs['Content-Length'] or HTTP_MAX_CONTENT ) + 0
-
                        -- Process get parameters
                        if ( method == "get" or method == "post" ) and
                           message.request_uri:match("?")
@@ -368,71 +379,6 @@ function parse_message( data, filecb )
                                message.params = { }
                        end
 
-                       -- Process post method
-                       if method == "post" and hdrs['Content-Type'] then
-
-                               -- Is it multipart/form-data ?
-                               if hdrs['Content-Type']:match("^multipart/form%-data") then
-                                       for k, v in pairs( mimedecode(
-                                               reader,
-                                               hdrs['Content-Type']:match("boundary=(.+)"),
-                                               filecb
-                                       ) ) do
-                                               message.params[k] = v
-                                       end
-
-                               -- Is it x-www-urlencoded?
-                               elseif hdrs['Content-Type'] == 'application/x-www-urlencoded' then
-
-                                       -- XXX: readline isn't the best solution here
-                                       for chunk in reader do
-                                               for k, v in pairs( urldecode_params( chunk ) ) do
-                                                       message.params[k] = v
-                                               end
-
-                                               -- XXX: unreliable (undefined line length)
-                                               if clen + chunk:len() >= HTTP_MAX_CONTENT then
-                                                       break
-                                               end
-
-                                               clen = clen + chunk:len()
-                                       end
-
-                               -- Unhandled encoding
-                               -- If a file callback is given then feed it line by line, else
-                               -- store whole buffer in message.content
-                               else
-
-                                       for chunk in reader do
-
-                                               -- We have a callback, feed it.
-                                               if type(filecb) == "function" then
-
-                                                       filecb( "_post", nil, chunk, false )
-
-                                               -- Append to .content buffer.
-                                               else
-                                                       message.content = 
-                                                               type(message.content) == "string"
-                                                                       and message.content .. chunk
-                                                                       or chunk
-                                               end
-
-                                               -- XXX: unreliable
-                                               if clen + chunk:len() >= HTTP_MAX_CONTENT then
-                                                       break
-                                               end
-
-                                               clen = clen + chunk:len()
-                                       end
-
-                                       -- Send eof to callback
-                                       if type(filecb) == "function" then
-                                               filecb( "_post", nil, "", true )
-                                       end
-                               end
-                       end
-
                        -- Populate common environment variables
                        message.env = {
                                CONTENT_LENGTH    = hdrs['Content-Length'];
@@ -467,6 +413,80 @@ function parse_message( data, filecb )
        end
 end
 
+
+-- Parse a http message body
+function parse_message_body( reader, message, filecb )
+
+       if type(message) == "table" then
+               local env = message.env
+
+               local clen = ( env.CONTENT_LENGTH or HTTP_MAX_CONTENT ) + 0
+               
+               -- Process post method
+               if env.REQUEST_METHOD:lower() == "post" and env.CONTENT_TYPE then
+                       -- Is it multipart/form-data ?
+                       if env.CONTENT_TYPE:match("^multipart/form%-data") then
+                               for k, v in pairs( mimedecode(
+                                       reader,
+                                       env.CONTENT_TYPE:match("boundary=(.+)"),
+                                       filecb
+                               ) ) do
+                                       message.params[k] = v
+                               end
+
+                       -- Is it x-www-form-urlencoded?
+                       elseif env.CONTENT_TYPE:match('^application/x%-www%-form%-urlencoded') then
+                               -- XXX: readline isn't the best solution here
+                               for chunk in reader do
+                                       for k, v in pairs( urldecode_params( chunk ) ) do
+                                               message.params[k] = v
+                                       end
+
+                                       -- XXX: unreliable (undefined line length)
+                                       if clen + chunk:len() >= HTTP_MAX_CONTENT then
+                                               break
+                                       end
+
+                                       clen = clen + chunk:len()
+                               end
+
+                       -- Unhandled encoding
+                       -- If a file callback is given then feed it line by line, else
+                       -- store whole buffer in message.content
+                       else
+                               for chunk in reader do
+
+                                       -- We have a callback, feed it.
+                                       if type(filecb) == "function" then
+
+                                               filecb( "_post", nil, chunk, false )
+
+                                       -- Append to .content buffer.
+                                       else
+                                               message.content = 
+                                                       type(message.content) == "string"
+                                                               and message.content .. chunk
+                                                               or chunk
+                                       end
+
+                                       -- XXX: unreliable
+                                       if clen + chunk:len() >= HTTP_MAX_CONTENT then
+                                               break
+                                       end
+
+                                       clen = clen + chunk:len()
+                               end
+
+                               -- Send eof to callback
+                               if type(filecb) == "function" then
+                                       filecb( "_post", nil, "", true )
+                               end
+                       end
+               end
+       end
+end
+
+
 function _linereader( obj )
 
        -- object is string