Пример #1
0
class HttpResponse:
    def __init__(self, code: int, content: Union[bytes, str] = b''):
        self.content = to_bytes(content)
        self.response_code = code
        self.headers = IOrderedDict()
        self._standard_headers()

    def _standard_headers(self):
        if 'Content-Length' not in self.headers:
            if not self.content:
                self.content = '%03d %s' % (self.response_code,
                                            status_reasons.get(
                                                self.response_code, 'Unknown'))
            self.headers['Content-Length'] = len(self.content)
        if 'Content-Type' not in self.headers:
            self.headers['Content-Type'] = b'text/html'
        if 'Date' not in self.headers:
            self.headers['Date'] = formatdate(time.time(), False, True)

    def __bytes__(self):
        self._standard_headers()
        reason = status_reasons.get(self.response_code, 'Unknown').encode()
        response = (b'HTTP/1.1 %03d %s\r\n' % (self.response_code, reason) +
                    b''.join(b'%s: %s\r\n' % (to_bytes(name), to_bytes(val))
                             for name, val in self.headers.items()) + b'\r\n' +
                    to_bytes(self.content))
        return response
 def inner(status, headers):
     headers_obj = IOrderedDict(headers)
     if 'Set-Cookie' in headers_obj and ', ' in headers_obj['Set-Cookie']:
         cookie = SimpleCookie()
         cookie.load(headers_obj['Set-Cookie'])
         del headers_obj['Set-Cookie']
         headers_list = headers_obj.items()
         for key in ("auctions_loggedin", "auction_session"):
             if key in cookie:
                 headers_list += [
                     ('Set-Cookie', cookie[key].output(header="").lstrip().rstrip(','))
                 ]
         headers = headers_list
     return start_response_decorated(status, headers)
Пример #3
0
 def inner(status, headers):
     headers_obj = IOrderedDict(headers)
     if 'Set-Cookie' in headers_obj and ', ' in headers_obj['Set-Cookie']:
         cookie = SimpleCookie()
         cookie.load(headers_obj['Set-Cookie'])
         del headers_obj['Set-Cookie']
         headers_list = headers_obj.items()
         for key in ("auctions_loggedin", "auction_session"):
             if key in cookie:
                 headers_list += [
                     ('Set-Cookie',
                      cookie[key].output(header="").lstrip().rstrip(','))
                 ]
         headers = headers_list
     return start_response_decorated(status, headers)