Exemple #1
0
    def listHeaders(self):
        result = []
        if self._server_version:
            result.append(('Server', self._server_version))

        result.append(('Date', build_http_date(_now())))
        result.extend(HTTPResponse.listHeaders(self))
        return result
Exemple #2
0
    def listHeaders(self):
        result = []
        if self._server_version:
            result.append(('Server', self._server_version))

        result.append(('Date', build_http_date(_now())))
        result.extend(HTTPResponse.listHeaders(self))
        return result
    def __call__(self):
        """Delete the cookie if there's already one or create a new one if no
           cookie is present.
        """
        context = aq_inner(self.context)
        if self.request.get(COOKIE_NAME, '') == '1':
            self.request.response.expireCookie(COOKIE_NAME, path='/')
        else:
            self.request.response.setCookie(
                COOKIE_NAME, '1', path='/',
                expires=build_http_date(time.time() + COOKIE_LIFETIME))

        self.request.response.redirect(context.absolute_url(), status=302)
Exemple #4
0
    def __str__(self):
        if self._wrote:
            if self._chunking:
                return '0\r\n\r\n'
            else:
                return ''

        headers = self.headers
        body = self.body

        # set 204 (no content) status if 200 and response is empty
        # and not streaming
        if ('content-type' not in headers and 'content-length' not in headers
                and not self._streaming and self.status == 200):
            self.setStatus('nocontent')

        if self.status in (100, 101, 102, 204, 304):
            # These responses should not have any body or Content-Length.
            # See RFC 2616 4.4 "Message Length".
            body = ''
            if 'content-length' in headers:
                del headers['content-length']
            if 'content-type' in headers:
                del headers['content-type']
        elif not headers.has_key('content-length') and not self._streaming:
            self.setHeader('content-length', len(body))

        chunks = []
        append = chunks.append

        # status header must come first.
        append("HTTP/%s %d %s" %
               (self._http_version or '1.0', self.status, self.errmsg))

        # add zserver headers
        append('Server: %s' % self._server_version)
        append('Date: %s' % build_http_date(time.time()))

        if self._http_version == '1.0':
            if self._http_connection == 'keep-alive':
                self.setHeader('Connection', 'Keep-Alive')
            else:
                self.setHeader('Connection', 'close')

        # Close the connection if we have been asked to.
        # Use chunking if streaming output.
        if self._http_version == '1.1':
            if self._http_connection == 'close':
                self.setHeader('Connection', 'close')
            elif (not self.headers.has_key('content-length')
                  and self.http_chunk and self._streaming):
                self.setHeader('Transfer-Encoding', 'chunked')
                self._chunking = 1

        headers = headers.items()
        headers.extend(self.accumulated_headers)

        for key, val in headers:
            if key.lower() == key:
                # only change non-literal header names
                key = "%s%s" % (key[:1].upper(), key[1:])
                start = 0
                l = key.find('-', start)
                while l >= start:
                    key = "%s-%s%s" % (key[:l], key[l + 1:l + 2].upper(),
                                       key[l + 2:])
                    start = l + 1
                    l = key.find('-', start)
                val = val.replace('\n\t', '\r\n\t')
            append("%s: %s" % (key, val))
        if self.cookies:
            chunks.extend(['%s: %s' % x for x in self._cookie_list()])

        append('')
        append(body)
        return "\r\n".join(chunks)
    def __str__(self):
        if self._wrote:
            if self._chunking:
                return '0\r\n\r\n'
            else:
                return ''

        headers = self.headers
        body = self.body

        # set 204 (no content) status if 200 and response is empty
        # and not streaming
        if ('content-type' not in headers and
                'content-length' not in headers and
                not self._streaming and self.status == 200):
            self.setStatus('nocontent')

        if self.status in (100, 101, 102, 204, 304):
            # These responses should not have any body or Content-Length.
            # See RFC 2616 4.4 "Message Length".
            body = ''
            if 'content-length' in headers:
                del headers['content-length']
            if 'content-type' in headers:
                del headers['content-type']
        elif 'content-length' not in headers and not self._streaming:
            self.setHeader('content-length', len(body))

        chunks = []
        append = chunks.append

        # status header must come first.
        append("HTTP/%s %d %s" % (self._http_version or '1.0',
                                  self.status, self.errmsg))

        # add zserver headers
        append('Server: %s' % self._server_version)
        append('Date: %s' % build_http_date(time.time()))

        if self._http_version == '1.0':
            if self._http_connection == 'keep-alive':
                self.setHeader('Connection', 'Keep-Alive')
            else:
                self.setHeader('Connection', 'close')

        # Close the connection if we have been asked to.
        # Use chunking if streaming output.
        if self._http_version == '1.1':
            if self._http_connection == 'close':
                self.setHeader('Connection', 'close')
            elif ('content-length' not in self.headers and
                  self.http_chunk and self._streaming):
                self.setHeader('Transfer-Encoding', 'chunked')
                self._chunking = 1

        headers = headers.items()
        headers.extend(self.accumulated_headers)

        for key, val in headers:
            if key.lower() == key:
                # only change non-literal header names
                key = "%s%s" % (key[:1].upper(), key[1:])
                start = 0
                l = key.find('-', start)
                while l >= start:
                    key = "%s-%s%s" % (key[:l],
                                       key[l + 1:l + 2].upper(),
                                       key[l + 2:])
                    start = l + 1
                    l = key.find('-', start)
                val = val.replace('\n\t', '\r\n\t')
            append("%s: %s" % (key, val))

        if self.cookies:
            chunks.extend(['%s: %s' % x for x in self._cookie_list()])

        append('')
        append(body)
        return "\r\n".join(chunks)
Exemple #6
0
    def __str__(self,
                html_search=re.compile('<html>',re.I).search,
                ):
        if self._wrote:
            if self._chunking:
                return '0\r\n\r\n'
            else:
                return ''

        headers=self.headers
        body=self.body

        # set 204 (no content) status if 200 and response is empty
        # and not streaming
        if not headers.has_key('content-type') and \
                not headers.has_key('content-length') and \
                not self._streaming and \
                self.status == 200:
            self.setStatus('nocontent')

        # add content length if not streaming
        if not headers.has_key('content-length') and \
                not self._streaming:
            self.setHeader('content-length',len(body))


        content_length= headers.get('content-length', None)
        if content_length>0 :
            self.setHeader('content-length', content_length)

        headersl=[]
        append=headersl.append

        status=headers.get('status', '200 OK')

        # status header must come first.
        append("HTTP/%s %s" % (self._http_version or '1.0' , status))
        if headers.has_key('status'):
            del headers['status']

        # add zserver headers
        append('Server: %s' % self._server_version)
        append('Date: %s' % build_http_date(time.time()))

        if self._http_version=='1.0':
            if self._http_connection=='keep-alive' and \
                    self.headers.has_key('content-length'):
                self.setHeader('Connection','Keep-Alive')
            else:
                self.setHeader('Connection','close')

        # Close the connection if we have been asked to.
        # Use chunking if streaming output.
        if self._http_version=='1.1':
            if self._http_connection=='close':
                self.setHeader('Connection','close')
            elif not self.headers.has_key('content-length'):
                if self.http_chunk and self._streaming:
                    self.setHeader('Transfer-Encoding','chunked')
                    self._chunking=1
                else:
                    self.setHeader('Connection','close')

        for key, val in headers.items():
            if key.lower()==key:
                # only change non-literal header names
                key="%s%s" % (key[:1].upper(), key[1:])
                start=0
                l=key.find('-',start)
                while l >= start:
                    key="%s-%s%s" % (key[:l],key[l+1:l+2].upper(),key[l+2:])
                    start=l+1
                    l=key.find('-',start)
            append("%s: %s" % (key, val))
        if self.cookies:
            headersl=headersl+self._cookie_list()
        headersl[len(headersl):]=[self.accumulated_headers, body]
        return "\r\n".join(headersl)