handle POST data
authorTero Marttila <terom@fixme.fi>
Mon, 09 Feb 2009 06:55:51 +0200
changeset 55 d36efeb64650
parent 54 3fa90f00c0c4
child 56 dfb1c755e972
handle POST data
http.py
--- a/http.py	Mon Feb 09 06:11:51 2009 +0200
+++ b/http.py	Mon Feb 09 06:55:51 2009 +0200
@@ -29,6 +29,28 @@
 
         # parse query args
         self.arg_dict = cgi.parse_qs(self.arg_str, True)
+
+        # load post data?
+        if self.is_post() :
+            # content-type of post data
+            content_type = self.env.get('CONTENT_TYPE', 'application/x-www-form-urlencoded')
+            
+            # valid content-type?
+            # XXX: how to handle errors?
+            assert any(content_type.startswith(x) for x in (
+                'application/x-www-form-urlencoded',
+                'multipart/form-data'
+            ))
+
+            # input stream
+            input = self.env['wsgi.input']
+
+            # use cgi.FieldStorage to parse this
+            self.post_data = cgi.FieldStorage(fp=input, environ=self.env, keep_blank_values=1)
+        
+        else :
+            # no post data
+            self.post_data = None
  
     @property
     def site_host (self) :
@@ -116,6 +138,25 @@
         """
 
         return cgi.parse_qsl(self.arg_str)
+    
+    def is_post (self) :
+        """
+            Is this a POST request?
+        """
+
+        # just check REQUEST_METHOD
+        return (self.env['REQUEST_METHOD'].upper() == 'POST')
+    
+    def get_post (self, name) :
+        """
+            Get the value of the given POST field
+        """
+        
+        # sanity-check
+        assert self.post_data
+        
+        # return the FieldStorage value
+        return self.post_data[name].value
 
 class Response (object) :
     """