Пример #1
0
class Response(object):
    """
        wsgi Response, has a Request object attribute,
    """
    def __init__(self, request, ctype=None):
        self.__request = request
        if ctype:
            self.__ctype = ctype
        else:
            self.__ctype = 'text/html; charset=UTF-8'
        self.__body = []
        from Cookie import SimpleCookie
        self.__cookie = SimpleCookie()
        # add default headers
        self.__header = ('200 OK', [('Content-Type', self.__ctype)])

    @property
    def body(self):
        if self.__request.method == 'HEAD':
            return ""
        else:
            return "".join(self.__body)
            
    @property
    def status(self):
        return self.__header[0]

    @property
    def header(self):
        return self.__header[1]

    def add(self, *args):
        from types import StringTypes, FunctionType
        for response in args:
            if type(response) is FunctionType:
                self.add(response())
            if type(response) is StringTypes:
                self.__body.append(response)
            else:
                self.__body.append(str(response))

    def cookie(self, key, value):
        """
           add Set-Cookie headers
        """
        self.__cookie[key] = value
        self.__cookie[key]['path'] = '/'
        for key in self.__cookie.iterkeys():
            self.__header[1].append(('Set-Cookie', self.__cookie[key].OutputString()))

    def redirect(self, url_append, code=None):
        if not code:
            code = '303 SEE OTHER'
        self.response.header.state(code)
        self.response.header.add('Location', self.__request.base_url + url_append)
Пример #2
0
 def _get_cookies(self):
     if self._cookies is None:
         self._cookies = {}
         cookie = self.META.get('HTTP_COOKIE', '')
         if cookie:
             try:
                 cookie = SimpleCookie(cookie)
             except CookieError:
                 pass
             else:
                 for key in cookie.iterkeys():
                     self._cookies[key] = cookie[key].value
     return self._cookies