summaryrefslogtreecommitdiff
path: root/jasper_reports/JasperReports/websrv_lib.py
blob: 1c0d3c469615bb332bad3074dfbc7c0a0ee8f5e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# -*- coding: utf-8 -*-
#
# Copyright P. Christeas <p_christ@hol.gr> 2008-2010
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly advised to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
###############################################################################


""" Framework for generic http servers

    This library contains *no* Odoo-specific functionality. It should be
    usable in other projects, too.
"""

import logging
from http.server import SimpleHTTPRequestHandler

_logger = logging.getLogger(__name__)

# A list of HTTPDir.
handlers = []


class AuthRequiredExc(Exception):
    def __init__(self, atype, realm):
        Exception.__init__(self)
        self.atype = atype
        self.realm = realm


class AuthRejectedExc(Exception):
    pass


class AuthProvider:
    def __init__(self, realm):
        self.realm = realm

    def authenticate(self, user, passwd, client_address):
        return False

    def log(self, msg):
        _logger.info(msg)

    def check_request(self, handler, path='/'):
        """ Check if we are allowed to process that request
        """
        pass


class HTTPHandler(SimpleHTTPRequestHandler):
    def __init__(self, request, client_address, server):
        SimpleHTTPRequestHandler.__init__(
            self, request, client_address, server)
        self.protocol_version = 'HTTP/1.1'
        self.connection = DummyConn()

    def handle(self):
        """ Classes here should NOT handle inside their constructor
        """
        pass

    def finish(self):
        pass

    def setup(self):
        pass


class HTTPDir:
    """ A dispatcher class, like a virtual folder in httpd
    """

    def __init__(self, path, handler, auth_provider=None, secure_only=False):
        self.path = path
        self.handler = handler
        self.auth_provider = auth_provider
        self.secure_only = secure_only

    def matches(self, request):
        """ Test if some request matches us. If so, return
            the matched path. """
        if request.startswith(self.path):
            return self.path
        return False

    def instanciate_handler(self, request, client_address, server):
        handler = self.handler(NoConnection(request), client_address, server)
        if self.auth_provider:
            handler.auth_provider = self.auth_provider()
        return handler


def reg_http_service(path, handler, auth_provider=None, secure_only=False):
    """ Register a HTTP handler at a given path.

    The auth_provider will be instanciated and set on the handler instances.
    """
    global handlers
    service = HTTPDir(path, handler, auth_provider, secure_only)
    pos = len(handlers)
    lastpos = pos
    while pos > 0:
        pos -= 1
        if handlers[pos].matches(service.path):
            lastpos = pos
            # we won't break here, but search all way to the top, to
            # ensure there is no lesser entry that will shadow the one
            # we are inserting.
    handlers.insert(lastpos, service)


def list_http_services(protocol=None):
    global handlers
    ret = []
    for svc in handlers:
        if protocol is None or protocol == 'http' or svc.secure_only:
            ret.append((svc.path, str(svc.handler)))

    return ret


def find_http_service(path, secure=False):
    global handlers
    for vdir in handlers:
        p = vdir.matches(path)
        if p is False or (vdir.secure_only and not secure):
            continue
        return vdir
    return None


class NoConnection(object):
    """ a class to use instead of the real connection
    """

    def __init__(self, realsocket=None):
        self.__hidden_socket = realsocket

    def makefile(self, mode, bufsize):
        return None

    def close(self):
        pass

    def getsockname(self):
        """ We need to return info about the real socket that is used for the request
        """
        if not self.__hidden_socket:
            raise AttributeError("No-connection class cannot tell real socket")
        return self.__hidden_socket.getsockname()


class DummyConn:
    def shutdown(self, tru):
        pass


def _quote_html(html):
    return html.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")


class FixSendError:
    def send_error(self, code, message=None):

        try:
            short, long = self.responses[code]
        except KeyError:
            short, long = '???', '???'
        if message is None:
            message = short
        explain = long
        _logger.error("code %d, message %s", code, message)
        content = (self.error_message_format % {
            'code': code,
            'message': _quote_html(message),
            'explain': explain
        })
        self.send_response(code, message)
        self.send_header("Content-Type", self.error_content_type)
        self.send_header('Connection', 'close')
        self.send_header('Content-Length', len(content) or 0)
        self.end_headers()
        if hasattr(self, '_flush'):
            self._flush()
        if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
            self.wfile.write(content)


class HttpOptions:

    _HTTP_OPTIONS = {'Allow': ['OPTIONS']}

    def do_OPTIONS(self):
        """return the list of capabilities """

        opts = self._HTTP_OPTIONS
        nopts = self._prep_OPTIONS(opts)
        if nopts:
            opts = nopts

        self.send_response(200)
        self.send_header("Content-Length", 0)
        if 'Microsoft' in self.headers.get('User-Agent', ''):
            self.send_header('MS-Author-Via', 'DAV')
            # Microsoft's webdav lib ass-umes that the server would
            # be a FrontPage(tm) one, unless we send a non-standard
            # header that we are not an elephant.
            # http://www.ibm.com/developerworks/rational/library/2089.html

        for key, value in opts.items():
            if isinstance(value, str):
                self.send_header(key, value)
            elif isinstance(value, (tuple, list)):
                self.send_header(key, ', '.join(value))
        self.end_headers()

    def _prep_OPTIONS(self, opts):
        """ Prepare the OPTIONS response, if needed.
            Sometimes, like in special DAV folders, the OPTIONS may
            contain extra keywords, perhaps also dependant on the
            request url.
        :param opts: MUST be copied before being altered
        :returns: the updated options.
        """
        return opts