• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / Zope / 3956162881

pending completion
3956162881

push

github

Michael Howitz
Update to deprecation warning free releases.

4401 of 7036 branches covered (62.55%)

Branch coverage included in aggregate %.

27161 of 31488 relevant lines covered (86.26%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

41.67
/src/ZPublisher/BaseResponse.py
1
##############################################################################
2
#
3
# Copyright (c) 2002 Zope Foundation and Contributors.
4
#
5
# This software is subject to the provisions of the Zope Public License,
6
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
# FOR A PARTICULAR PURPOSE
11
#
12
##############################################################################
13
"""CGI Response Output formatter"""
1✔
14

15
from zExceptions import BadRequest
1✔
16
from zExceptions import Forbidden
1✔
17
from zExceptions import NotFound
1✔
18
from zExceptions import Unauthorized
1✔
19

20

21
class BaseResponse:
1✔
22
    """Base Response Class
23
    """
24
    body = b''
1✔
25
    debug_mode = None
1✔
26
    _auth = None
1✔
27
    _error_format = 'text/plain'
1✔
28

29
    # Allow (reluctantly) access to unprotected attributes
30
    __allow_access_to_unprotected_subobjects__ = 1
1✔
31

32
    def __init__(self, stdout, stderr,
1✔
33
                 body=b'', headers=None, status=None, cookies=None):
34
        self.stdout = stdout
×
35
        self.stderr = stderr
×
36
        self.body = body
×
37
        if headers is None:
×
38
            headers = {}
×
39
        self.headers = headers
×
40
        self.status = status
×
41
        if cookies is None:
×
42
            cookies = {}
×
43
        self.cookies = cookies
×
44

45
    def setStatus(self, status, reason=None):
1✔
46
        self.status = status
×
47

48
    def setHeader(self, name, value):
1✔
49
        self.headers[name] = value
×
50

51
    __setitem__ = setHeader
1✔
52

53
    def outputBody(self):
1✔
54
        """Output the response body."""
55
        self.stdout.write(bytes(self))
1✔
56

57
    def setBody(self, body):
1✔
58
        if isinstance(body, str):
×
59
            raise ValueError('Body must be binary.')
×
60
        self.body = body
×
61

62
    def getStatus(self):
1✔
63
        """Returns the current HTTP status code as an integer."""
64
        return self.status
1✔
65

66
    def setCookie(self, name, value, **kw):
1✔
67
        """Set an HTTP cookie on the browser.
68

69
        The response will include an HTTP header that sets a cookie on
70
        cookie-enabled browsers with a key "name" and value
71
        "value". This overwrites any previously set value for the
72
        cookie in the Response object.
73
        """
74
        cookies = self.cookies
×
75
        if name in cookies:
×
76
            cookie = cookies[name]
×
77
        else:
78
            cookie = cookies[name] = {}
×
79
        for k, v in kw.items():
×
80
            cookie[k] = v
×
81
        cookie['value'] = value
×
82

83
    def appendBody(self, body):
1✔
84
        self.setBody(self.getBody() + body)
×
85

86
    def getHeader(self, name):
1✔
87
        """Get a header value.
88

89
        Returns the value associated with a HTTP return header, or
90
        "None" if no such header has been set in the response
91
        yet.
92
        """
93
        return self.headers.get(name, None)
×
94

95
    def __getitem__(self, name):
1✔
96
        """Get the value of an output header."""
97
        return self.headers[name]
×
98

99
    def getBody(self):
1✔
100
        """Returns bytes representing the currently set body."""
101
        return self.body
×
102

103
    def __bytes__(self):
1✔
104
        return bytes(self.body)
×
105

106
    def __repr__(self):
1✔
107
        return f'{self.__class__.__name__}({self.body!r})'
×
108

109
    def flush(self):
1✔
110
        pass
×
111

112
    def write(self, data):
1✔
113
        """Return data as a stream.
114

115
        HTML data may be returned using a stream-oriented interface.
116
        This allows the browser to display partial results while
117
        computation of a response to proceed.
118

119
        The published object should first set any output headers or
120
        cookies on the response object.
121

122
        Note that published objects must not generate any errors
123
        after beginning stream-oriented output.
124
        """
125
        if isinstance(data, str):
×
126
            raise ValueError('Data must be binary.')
×
127
        self.body = self.body + data
×
128

129
    def exception(self, fatal=0, info=None):
1✔
130
        """Handle an exception.
131

132
        The fatal argument indicates whether the error is fatal.
133

134
        The info argument, if given should be a tuple with an
135
        error type, value, and traceback.
136
        """
137

138
    def notFoundError(self, v=''):
1✔
139
        """Generate an error indicating that an object was not found."""
140
        raise NotFound(v)
×
141

142
    def debugError(self, v=''):
1✔
143
        """Raise an error with debugging info and in debugging mode."""
144
        raise NotFound("Debugging notice: %s" % v)
×
145

146
    def badRequestError(self, v=''):
1✔
147
        """Raise an error indicating something wrong with the request."""
148
        raise BadRequest(v)
×
149

150
    def forbiddenError(self, v=''):
1✔
151
        """Raise an error indicating that the request cannot be done."""
152
        raise Forbidden(v)
×
153

154
    def unauthorized(self):
1✔
155
        """Raise an error indicating that the user was not authorized.
156

157
        Make sure to generate an appropriate challenge, as appropriate.
158
        """
159
        raise Unauthorized()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc