/
dispatch.py
139 lines (112 loc) · 4.14 KB
/
dispatch.py
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
# -*- coding: utf-8 -*-
# rdiffweb, A web interface to rdiff-backup repositories
# Copyright (C) 2012-2021 rdiffweb contributors
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
"""
Default page handler
@author: Patrik Dufresne <patrik@ikus-soft.com>
"""
import os
import cherrypy
from cherrypy.lib.static import mimetypes, serve_file
import rdiffweb.tools.auth_form # noqa
import rdiffweb.tools.auth_mfa # noqa
import rdiffweb.tools.ratelimit # noqa
from rdiffweb.core.rdw_helpers import unquote_url
def empty():
@cherrypy.expose
def handler():
return None
return handler
def poppath(*args, **kwargs):
"""
A decorator for _cp_dispatch
(cherrypy.dispatch.Dispatcher.dispatch_method_name).
Used to merge the segment of URI into a single parameter denoting the
repository path.
"""
# Since keyword arg comes after *args, we have to process it ourselves
# for lower versions of python.
handler = None
handler_call = False
for k, v in kwargs.items():
if k == 'handler':
handler = v
else:
raise TypeError("cherrypy.popargs() got an unexpected keyword argument '{0}'".format(k))
import inspect
if handler is not None and (hasattr(handler, '__call__') or inspect.isclass(handler)):
handler_call = True
def decorated(cls_or_self=None, vpath=None):
if inspect.isclass(cls_or_self):
# cherrypy.poppath is a class decorator
cls = cls_or_self
setattr(cls, cherrypy.dispatch.Dispatcher.dispatch_method_name, decorated)
return cls
# We're in the actual function
self = cls_or_self
parms = {}
for arg in args:
if not vpath:
break
parms[arg] = unquote_url(vpath.pop(0))
# Build repo path.
path = []
while len(vpath) > 0:
path.append(unquote_url(vpath.pop(0)))
parms['path'] = b"/".join(path)
if handler is not None:
if handler_call:
return handler(**parms)
else:
cherrypy.request.params.update(parms) # @UndefinedVariable
return handler
cherrypy.request.params.update(parms) # @UndefinedVariable
# If we are the ultimate handler, then to prevent our _cp_dispatch
# from being called again, we will resolve remaining elements through
# getattr() directly.
if vpath:
return getattr(self, vpath.pop(0), None)
else:
return self
return decorated
def static(path):
"""
Create a page handler to serve static files. Disable authentication.
"""
assert isinstance(path, str)
assert os.path.exists(path), "%r doesn't exists" % path
content_type = None
if os.path.isfile(path):
# Set content-type based on filename extension
ext = ""
i = path.rfind('.')
if i != -1:
ext = path[i:].lower()
content_type = mimetypes.types_map.get(ext, None) # @UndefinedVariable
@cherrypy.expose
@cherrypy.tools.auth_form(on=False)
@cherrypy.tools.auth_mfa(on=False)
@cherrypy.tools.ratelimit(on=False)
@cherrypy.tools.sessions(on=False)
def handler(*args, **kwargs):
if cherrypy.request.method not in ('GET', 'HEAD'):
raise cherrypy.HTTPError(405)
filename = os.path.join(path, *args)
assert filename.startswith(path)
if not os.path.isfile(filename):
raise cherrypy.HTTPError(404)
return serve_file(filename, content_type)
return handler