Form.py :  » Web-Frameworks » Python-Publishing-Accessories » PPA-0.2.7 » PPA » HTTP » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Python Publishing Accessories 
Python Publishing Accessories » PPA 0.2.7 » PPA » HTTP » Form.py
# $Id: Form.py,v 1.4 2006/06/09 10:16:26 ods Exp $

from weakref import WeakKeyDictionary
from Errors import ClientError
import urllib


class Form(object):

    maxContentLength = 1048576
    requireContentLength = 1
    keepBlankValues = 0
    charset = None
    charsetErrors = 'replace'
    
    __cache = WeakKeyDictionary()
    
    def __new__(cls, request):
        if not cls.__cache.has_key(request):
            cls.__cache[request] = self = object.__new__(cls)
            self._init(request)
        return cls.__cache[request]

    def _init(self, request):
        self._dict = {}
        method = request.method()
        if method in ('GET', 'HEAD'):
            self.parseURLEncodedString(request.query())
        elif method=='POST':
            # XXX Header may contain parameters, parse it.
            # XXX Add method to Headers class returning value and dictionary of
            # parameters?
            content_type = request.headers['Content-Type'] or \
                                'application/x-www-form-urlencoded'
            try:
                content_length = int(request.headers['Content-Length'])
            except (KeyError, ValueError):
                content_length = None
            else:
                if content_length>self.maxContentLength:
                    raise ClientError(413, '')
            if content_length is None and self.requireContentLength:
                raise ClientError(411, '')
            if content_type=='application/x-www-form-urlencoded':
                self.parseURLEncoded(request, content_length)
            elif content_type=='multipart/form-data':
                # XXX Use boundary parameter from header.
                self.parseMultipart(request, content_length)
    
    def parseURLEncoded(self, request, content_length=None):
        if content_length is None:
            body = request.read(self.maxContentLength+1)
            if len(body)>self.maxContentLength:
                raise ClientError(413, '')
        else:
            body = request.read(content_length)
        self.parseURLEncodedString(body)

    def parseURLEncodedString(self, body):
        pairs = [(ps.split('=', 1)+[''])[:2] for ps0 in body.split(';')
                                              for ps in ps0.split('&')]
        unquote = urllib.unquote_plus
        setdefault = self._dict.setdefault
        for qfn, qfv in pairs:
            if qfv or self.keepBlankValues:
                setdefault(unquote(qfn), []).append(unquote(qfv))

    def _decode(self, value):
        if self.charset is not None:
            return value.decode(self.charset, self.charsetErrors)
        else:
            return value

    def getString(self, name, default=''):
        return self._decode(self._dict.get(name, [default])[0])

    def getStringList(self, name):
        return [self._decode(value) for value in self._dict.get(name, [])]
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.