etwist/http.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 26 Sep 2011 19:08:44 +0200
branchstable
changeset 7866 45b8ab1b7c63
parent 7855 54283a5b7afc
child 8316 d5b1b75805dd
permissions -rw-r--r--
Added tag cubicweb-debian-version-3.13.6-1 for changeset 8a8949ca5351
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     1
"""twisted server for CubicWeb web instances
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     2
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     3
:organization: Logilab
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5513
diff changeset
     4
:copyright: 2001-2011 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     6
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     7
"""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     8
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     9
__docformat__ = "restructuredtext en"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    10
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    11
from cubicweb.web.http_headers import Headers
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    12
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    13
class HTTPResponse(object):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    14
    """An object representing an HTTP Response to be sent to the client.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    15
    """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    16
    def __init__(self, twisted_request, code=None, headers=None, stream=None):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    17
        self._headers_out = headers
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    18
        self._twreq = twisted_request
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    19
        self._stream = stream
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    20
        self._code = code
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    21
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    22
        self._init_headers()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    23
        self._finalize()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    24
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    25
    def _init_headers(self):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    26
        if self._headers_out is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    27
            return
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5513
diff changeset
    28
        # initialize headers
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5513
diff changeset
    29
        for k, values in self._headers_out.getAllRawHeaders():
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5513
diff changeset
    30
            self._twreq.responseHeaders.setRawHeaders(k, values)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    31
        # add content-length if not present
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    32
        if (self._headers_out.getHeader('content-length') is None
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    33
            and self._stream is not None):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    34
           self._twreq.setHeader('content-length', len(self._stream))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    35
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    36
    def _finalize(self):
5513
07b32d9d8804 [twisted] we must set response code before starting to write the answer, else it's ignored
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
    37
        # we must set code before writing anything, else it's too late
07b32d9d8804 [twisted] we must set response code before starting to write the answer, else it's ignored
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
    38
        if self._code is not None:
07b32d9d8804 [twisted] we must set response code before starting to write the answer, else it's ignored
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
    39
            self._twreq.setResponseCode(self._code)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    40
        if self._stream is not None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    41
            self._twreq.write(str(self._stream))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    42
        self._twreq.finish()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    43
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    44
    def __repr__(self):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    45
        return "<%s.%s code=%d>" % (self.__module__, self.__class__.__name__, self._code)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    46
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    47
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    48
def not_modified_response(twisted_request, headers_in):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    49
    headers_out = Headers()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    50
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    51
    for header in (
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    52
        # Required from sec 10.3.5:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    53
        'date', 'etag', 'content-location', 'expires',
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    54
        'cache-control', 'vary',
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    55
        # Others:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    56
        'server', 'proxy-authenticate', 'www-authenticate', 'warning'):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    57
        value = headers_in.getRawHeaders(header)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    58
        if value is not None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    59
            headers_out.setRawHeaders(header, value)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    60
    return HTTPResponse(twisted_request=twisted_request,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    61
                        headers=headers_out)