-
Notifications
You must be signed in to change notification settings - Fork 273
/
misc.py
213 lines (163 loc) · 7.18 KB
/
misc.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
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
#####################################################################################
#
# Copyright (c) Crossbar.io Technologies GmbH
#
# Unless a separate license agreement exists between you and Crossbar.io GmbH (e.g.
# you have purchased a commercial license), the license terms below apply.
#
# Should you enter into a separate license agreement after having received a copy of
# this software, then the terms of such license agreement replace the terms below at
# the time at which such license agreement becomes effective.
#
# In case a separate license agreement ends, and such agreement ends without being
# replaced by another separate license agreement, the license terms below apply
# from the time at which said agreement ends.
#
# LICENSE TERMS
#
# This program is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License, version 3, as published by the
# Free Software Foundation. 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 Affero General Public License Version 3 for more details.
#
# You should have received a copy of the GNU Affero General Public license along
# with this program. If not, see <http://www.gnu.org/licenses/agpl-3.0.en.html>.
#
#####################################################################################
import json
import os
from twisted.python.filepath import FilePath
from twisted.internet import reactor
from twisted.web import server
from twisted.web.resource import Resource, NoResource
from twisted.web.static import File
from twisted.web.twcgi import CGIScript, CGIProcessProtocol
import crossbar
from crossbar.webservice.base import RouterWebService, Resource404, set_cross_origin_headers
class NodeInfoResource(Resource):
"""
Node information page.
"""
isLeaf = True
def __init__(self, templates, controller_session):
Resource.__init__(self)
self._page = templates.get_template('cb_node_info.html')
self._pid = '{}'.format(os.getpid())
self._controller_session = controller_session
def _delayedRender(self, node_info, request):
try:
peer = request.transport.getPeer()
peer = '{}:{}'.format(peer.host, peer.port)
except:
peer = '?:?'
s = self._page.render(cbVersion=crossbar.__version__,
workerPid=self._pid,
peer=peer,
**node_info)
request.write(s.encode('utf8'))
request.finish()
def render_GET(self, request):
# http://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous-deferred.html
d = self._controller_session.call('crossbar.get_status')
d.addCallback(self._delayedRender, request)
return server.NOT_DONE_YET
class RouterWebServiceNodeInfo(RouterWebService):
"""
Node information page service.
"""
@staticmethod
def create(transport, path, config):
personality = transport.worker.personality
personality.WEB_SERVICE_CHECKERS['nodeinfo'](personality, config)
resource = NodeInfoResource(transport.templates, transport.worker)
return RouterWebServiceNodeInfo(transport, path, config, resource)
class JsonResource(Resource):
"""
Static Twisted Web resource that renders to a JSON document.
"""
def __init__(self, value, options=None):
Resource.__init__(self)
options = options or {}
if options.get('prettify', False):
self._data = json.dumps(value, sort_keys=True, indent=3, ensure_ascii=False)
else:
self._data = json.dumps(value, separators=(',', ':'), ensure_ascii=False)
# Twisted Web render_METHOD methods are expected to return a byte string
self._data = self._data.encode('utf8')
self._allow_cross_origin = options.get('allow_cross_origin', True)
self._discourage_caching = options.get('discourage_caching', False)
# number of HTTP/GET requests we served from this resource
#
self._requests_served = 0
def render_GET(self, request):
# we produce JSON: set correct response content type
#
# note: both args to request.setHeader are supposed to be byte strings
# https://twistedmatrix.com/documents/current/api/twisted.web.http.Request.html#setHeader
#
request.setHeader(b'content-type', b'application/json; charset=utf8-8')
# set response headers for cross-origin requests
#
if self._allow_cross_origin:
set_cross_origin_headers(request)
# set response headers to disallow caching
#
if self._discourage_caching:
request.setHeader(b'cache-control', b'no-store, no-cache, must-revalidate, max-age=0')
self._requests_served += 1
return self._data
class RouterWebServiceJson(RouterWebService):
"""
JSON static value Web service.
"""
@staticmethod
def create(transport, path, config):
personality = transport.worker.personality
personality.WEB_SERVICE_CHECKERS['json'](personality, config)
value = config['value']
resource = JsonResource(value)
return RouterWebServiceJson(transport, path, config, resource)
class CgiScript(CGIScript):
def __init__(self, filename, filter):
CGIScript.__init__(self, filename)
self.filter = filter
def runProcess(self, env, request, qargs=[]):
p = CGIProcessProtocol(request)
reactor.spawnProcess(p, self.filter, [self.filter, self.filename], env, os.path.dirname(self.filename))
class CgiDirectory(Resource, FilePath):
cgiscript = CgiScript
def __init__(self, pathname, filter, childNotFound=None):
Resource.__init__(self)
FilePath.__init__(self, pathname)
self.filter = filter
if childNotFound:
self.childNotFound = childNotFound
else:
self.childNotFound = NoResource("CGI directories do not support directory listing.")
def getChild(self, path, request):
fnp = self.child(path)
if not fnp.exists():
return File.childNotFound
elif fnp.isdir():
return CgiDirectory(fnp.path, self.filter, self.childNotFound)
else:
return self.cgiscript(fnp.path, self.filter)
def render(self, request):
return self.childNotFound.render(request)
class RouterWebServiceCgi(RouterWebService):
"""
CGI script Web service.
"""
@staticmethod
def create(transport, path, config):
personality = transport.worker.personality
personality.WEB_SERVICE_CHECKERS['cgi'](personality, config)
cgi_processor = config['processor']
cgi_directory = os.path.abspath(os.path.join(transport.cbdir, config['directory']))
# http://stackoverflow.com/a/20433918/884770
cgi_directory = cgi_directory.encode('ascii', 'ignore')
resource = CgiDirectory(cgi_directory, cgi_processor, Resource404(transport.templates, cgi_directory))
return RouterWebServiceCgi(transport, path, config, resource)