# HG changeset patch # User Tero Marttila # Date 1234155351 -7200 # Node ID d36efeb64650c2069f3c00f18fc47f38897c72ff # Parent 3fa90f00c0c435b5110900724bf18809651d97a3 handle POST data diff -r 3fa90f00c0c4 -r d36efeb64650 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) : """