1
|
|
|
# Copyright (c) 2006-2007 Open Source Applications Foundation |
2
|
|
|
# |
3
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
4
|
|
|
# you may not use this file except in compliance with the License. |
5
|
|
|
# You may obtain a copy of the License at |
6
|
|
|
# |
7
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
8
|
|
|
# |
9
|
|
|
# Unless required by applicable law or agreed to in writing, software |
10
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
11
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12
|
|
|
# See the License for the specific language governing permissions and |
13
|
|
|
# limitations under the License. |
14
|
|
|
# |
15
|
|
|
# Origin: https://code.google.com/p/wsgi-xmlrpc/ |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
from six.moves.xmlrpc_server import SimpleXMLRPCDispatcher |
19
|
|
|
import logging |
20
|
|
|
|
21
|
|
|
logger = logging.getLogger(__name__) |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class WSGIXMLRPCApplication(object): |
25
|
|
|
"""Application to handle requests to the XMLRPC service""" |
26
|
|
|
|
27
|
|
|
def __init__(self, instance=None, methods=None): |
28
|
|
|
"""Create windmill xmlrpc dispatcher""" |
29
|
|
|
if methods is None: |
30
|
|
|
methods = [] |
31
|
|
|
try: |
32
|
|
|
self.dispatcher = SimpleXMLRPCDispatcher(allow_none=True, encoding=None) |
33
|
|
|
except TypeError: |
34
|
|
|
# python 2.4 |
35
|
|
|
self.dispatcher = SimpleXMLRPCDispatcher() |
36
|
|
|
if instance is not None: |
37
|
|
|
self.dispatcher.register_instance(instance) |
38
|
|
|
for method in methods: |
39
|
|
|
self.dispatcher.register_function(method) |
40
|
|
|
self.dispatcher.register_introspection_functions() |
41
|
|
|
|
42
|
|
|
def register_instance(self, instance): |
43
|
|
|
return self.dispatcher.register_instance(instance) |
44
|
|
|
|
45
|
|
|
def register_function(self, function, name=None): |
46
|
|
|
return self.dispatcher.register_function(function, name) |
47
|
|
|
|
48
|
|
|
def handler(self, environ, start_response): |
49
|
|
|
"""XMLRPC service for windmill browser core to communicate with""" |
50
|
|
|
|
51
|
|
|
if environ['REQUEST_METHOD'] == 'POST': |
52
|
|
|
return self.handle_POST(environ, start_response) |
53
|
|
|
else: |
54
|
|
|
start_response("400 Bad request", [('Content-Type', 'text/plain')]) |
55
|
|
|
return [''] |
56
|
|
|
|
57
|
|
|
def handle_POST(self, environ, start_response): |
58
|
|
|
"""Handles the HTTP POST request. |
59
|
|
|
|
60
|
|
|
Attempts to interpret all HTTP POST requests as XML-RPC calls, |
61
|
|
|
which are forwarded to the server's _dispatch method for handling. |
62
|
|
|
|
63
|
|
|
Most code taken from SimpleXMLRPCServer with modifications for wsgi and my custom dispatcher. |
64
|
|
|
""" |
65
|
|
|
|
66
|
|
|
try: |
67
|
|
|
# Get arguments by reading body of request. |
68
|
|
|
# We read this in chunks to avoid straining |
69
|
|
|
# socket.read(); around the 10 or 15Mb mark, some platforms |
70
|
|
|
# begin to have problems (bug #792570). |
71
|
|
|
|
72
|
|
|
length = int(environ['CONTENT_LENGTH']) |
73
|
|
|
data = environ['wsgi.input'].read(length) |
74
|
|
|
|
75
|
|
|
# In previous versions of SimpleXMLRPCServer, _dispatch |
76
|
|
|
# could be overridden in this class, instead of in |
77
|
|
|
# SimpleXMLRPCDispatcher. To maintain backwards compatibility, |
78
|
|
|
# check to see if a subclass implements _dispatch and |
79
|
|
|
# using that method if present. |
80
|
|
|
response = self.dispatcher._marshaled_dispatch( |
81
|
|
|
data, getattr(self.dispatcher, '_dispatch', None) |
82
|
|
|
) |
83
|
|
|
response += b'\n' |
84
|
|
|
except Exception as e: # This should only happen if the module is buggy |
85
|
|
|
# internal error, report as HTTP server error |
86
|
|
|
logger.exception(e) |
87
|
|
|
start_response("500 Server error", [('Content-Type', 'text/plain')]) |
88
|
|
|
return [] |
89
|
|
|
else: |
90
|
|
|
# got a valid XML RPC response |
91
|
|
|
start_response("200 OK", [('Content-Type', 'text/xml'), ('Content-Length', str(len(response)),)]) |
92
|
|
|
return [response] |
93
|
|
|
|
94
|
|
|
def __call__(self, environ, start_response): |
95
|
|
|
return self.handler(environ, start_response) |
96
|
|
|
|