2011-08-19 13:02:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
2012-02-01 10:28:38 +00:00
|
|
|
# Copyright (C) 2011-2012 OpenERP s.a. (<http://openerp.com>).
|
2011-08-19 13:02:48 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero 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 Affero General Public License 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/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2012-02-10 14:36:09 +00:00
|
|
|
"""
|
2011-08-19 13:02:48 +00:00
|
|
|
|
2012-02-10 14:36:09 +00:00
|
|
|
WSGI stack, common code.
|
2011-08-19 13:02:48 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2011-09-07 14:13:18 +00:00
|
|
|
import httplib
|
|
|
|
import urllib
|
2011-08-19 13:02:48 +00:00
|
|
|
import xmlrpclib
|
2011-09-07 14:13:18 +00:00
|
|
|
import StringIO
|
2011-08-19 13:02:48 +00:00
|
|
|
|
2012-01-15 23:52:15 +00:00
|
|
|
import errno
|
2011-09-14 10:43:00 +00:00
|
|
|
import logging
|
2013-03-13 16:18:15 +00:00
|
|
|
import platform
|
2013-01-31 13:10:51 +00:00
|
|
|
import socket
|
2011-08-19 13:02:48 +00:00
|
|
|
import sys
|
2011-09-14 15:06:03 +00:00
|
|
|
import threading
|
2011-09-27 10:22:46 +00:00
|
|
|
import traceback
|
2011-08-19 13:02:48 +00:00
|
|
|
|
2012-09-22 10:51:07 +00:00
|
|
|
import werkzeug.serving
|
|
|
|
import werkzeug.contrib.fixers
|
|
|
|
|
2011-08-19 13:02:48 +00:00
|
|
|
import openerp
|
|
|
|
import openerp.tools.config as config
|
2012-09-22 10:51:07 +00:00
|
|
|
import websrv_lib
|
2011-08-19 13:02:48 +00:00
|
|
|
|
2012-01-24 11:14:38 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2011-09-27 08:19:02 +00:00
|
|
|
# XML-RPC fault codes. Some care must be taken when changing these: the
|
|
|
|
# constants are also defined client-side and must remain in sync.
|
|
|
|
# User code must use the exceptions defined in ``openerp.exceptions`` (not
|
|
|
|
# create directly ``xmlrpclib.Fault`` objects).
|
2011-10-01 01:28:01 +00:00
|
|
|
RPC_FAULT_CODE_CLIENT_ERROR = 1 # indistinguishable from app. error.
|
|
|
|
RPC_FAULT_CODE_APPLICATION_ERROR = 1
|
|
|
|
RPC_FAULT_CODE_WARNING = 2
|
|
|
|
RPC_FAULT_CODE_ACCESS_DENIED = 3
|
|
|
|
RPC_FAULT_CODE_ACCESS_ERROR = 4
|
2011-09-27 08:19:02 +00:00
|
|
|
|
2013-10-06 10:21:31 +00:00
|
|
|
def xmlrpc_return(start_response, service, method, params, string_faultcode=False):
|
2011-09-27 08:19:02 +00:00
|
|
|
"""
|
|
|
|
Helper to call a service's method with some params, using a wsgi-supplied
|
|
|
|
``start_response`` callback.
|
|
|
|
|
|
|
|
This is the place to look at to see the mapping between core exceptions
|
|
|
|
and XML-RPC fault codes.
|
|
|
|
"""
|
|
|
|
# Map OpenERP core exceptions to XML-RPC fault codes. Specific exceptions
|
|
|
|
# defined in ``openerp.exceptions`` are mapped to specific fault codes;
|
|
|
|
# all the other exceptions are mapped to the generic
|
2011-10-01 01:28:01 +00:00
|
|
|
# RPC_FAULT_CODE_APPLICATION_ERROR value.
|
2011-09-27 08:19:02 +00:00
|
|
|
# This also mimics SimpleXMLRPCDispatcher._marshaled_dispatch() for
|
|
|
|
# exception handling.
|
2011-09-02 15:02:26 +00:00
|
|
|
try:
|
2014-02-27 12:56:48 +00:00
|
|
|
result = openerp.http.dispatch_rpc(service, method, params)
|
2011-09-02 15:02:26 +00:00
|
|
|
response = xmlrpclib.dumps((result,), methodresponse=1, allow_none=False, encoding=None)
|
2011-09-30 08:50:12 +00:00
|
|
|
except Exception, e:
|
2013-10-06 10:21:31 +00:00
|
|
|
if string_faultcode:
|
|
|
|
response = xmlrpc_handle_exception_string(e)
|
2011-09-30 08:50:12 +00:00
|
|
|
else:
|
2013-10-06 10:21:31 +00:00
|
|
|
response = xmlrpc_handle_exception_int(e)
|
2011-09-30 08:50:12 +00:00
|
|
|
start_response("200 OK", [('Content-Type','text/xml'), ('Content-Length', str(len(response)))])
|
|
|
|
return [response]
|
|
|
|
|
2013-10-06 10:21:31 +00:00
|
|
|
def xmlrpc_handle_exception_int(e):
|
2013-01-30 13:44:51 +00:00
|
|
|
if isinstance(e, openerp.osv.orm.except_orm): # legacy
|
2011-10-01 01:28:01 +00:00
|
|
|
fault = xmlrpclib.Fault(RPC_FAULT_CODE_WARNING, openerp.tools.ustr(e.value))
|
2011-09-30 08:50:12 +00:00
|
|
|
response = xmlrpclib.dumps(fault, allow_none=False, encoding=None)
|
2013-02-15 14:35:03 +00:00
|
|
|
elif isinstance(e, openerp.exceptions.Warning) or isinstance(e, openerp.exceptions.RedirectWarning):
|
2011-10-01 01:28:01 +00:00
|
|
|
fault = xmlrpclib.Fault(RPC_FAULT_CODE_WARNING, str(e))
|
2013-02-15 14:35:03 +00:00
|
|
|
response = xmlrpclib.dumps(fault, allow_none=False, encoding=None)
|
2011-09-30 12:55:38 +00:00
|
|
|
elif isinstance (e, openerp.exceptions.AccessError):
|
2011-10-01 01:28:01 +00:00
|
|
|
fault = xmlrpclib.Fault(RPC_FAULT_CODE_ACCESS_ERROR, str(e))
|
2011-09-28 22:05:39 +00:00
|
|
|
response = xmlrpclib.dumps(fault, allow_none=False, encoding=None)
|
2011-09-30 12:55:38 +00:00
|
|
|
elif isinstance(e, openerp.exceptions.AccessDenied):
|
2011-10-01 01:28:01 +00:00
|
|
|
fault = xmlrpclib.Fault(RPC_FAULT_CODE_ACCESS_DENIED, str(e))
|
2011-09-02 15:02:26 +00:00
|
|
|
response = xmlrpclib.dumps(fault, allow_none=False, encoding=None)
|
2011-09-30 12:55:38 +00:00
|
|
|
elif isinstance(e, openerp.exceptions.DeferredException):
|
2011-09-27 10:22:46 +00:00
|
|
|
info = e.traceback
|
|
|
|
# Which one is the best ?
|
|
|
|
formatted_info = "".join(traceback.format_exception(*info))
|
|
|
|
#formatted_info = openerp.tools.exception_to_unicode(e) + '\n' + info
|
2011-10-01 01:28:01 +00:00
|
|
|
fault = xmlrpclib.Fault(RPC_FAULT_CODE_APPLICATION_ERROR, formatted_info)
|
2011-09-30 08:50:12 +00:00
|
|
|
response = xmlrpclib.dumps(fault, allow_none=False, encoding=None)
|
2011-09-30 12:55:38 +00:00
|
|
|
else:
|
2011-09-30 08:50:12 +00:00
|
|
|
if hasattr(e, 'message') and e.message == 'AccessDenied': # legacy
|
2011-10-01 01:28:01 +00:00
|
|
|
fault = xmlrpclib.Fault(RPC_FAULT_CODE_ACCESS_DENIED, str(e))
|
2011-09-30 08:50:12 +00:00
|
|
|
response = xmlrpclib.dumps(fault, allow_none=False, encoding=None)
|
|
|
|
else:
|
|
|
|
info = sys.exc_info()
|
|
|
|
# Which one is the best ?
|
|
|
|
formatted_info = "".join(traceback.format_exception(*info))
|
|
|
|
#formatted_info = openerp.tools.exception_to_unicode(e) + '\n' + info
|
2011-10-01 01:28:01 +00:00
|
|
|
fault = xmlrpclib.Fault(RPC_FAULT_CODE_APPLICATION_ERROR, formatted_info)
|
2011-09-30 08:50:12 +00:00
|
|
|
response = xmlrpclib.dumps(fault, allow_none=None, encoding=None)
|
|
|
|
return response
|
|
|
|
|
2013-10-06 10:21:31 +00:00
|
|
|
def xmlrpc_handle_exception_string(e):
|
2013-01-30 13:44:51 +00:00
|
|
|
if isinstance(e, openerp.osv.orm.except_orm):
|
2011-09-30 08:50:12 +00:00
|
|
|
fault = xmlrpclib.Fault('warning -- ' + e.name + '\n\n' + e.value, '')
|
|
|
|
response = xmlrpclib.dumps(fault, allow_none=False, encoding=None)
|
2011-09-30 12:55:38 +00:00
|
|
|
elif isinstance(e, openerp.exceptions.Warning):
|
2011-09-30 08:50:12 +00:00
|
|
|
fault = xmlrpclib.Fault('warning -- Warning\n\n' + str(e), '')
|
|
|
|
response = xmlrpclib.dumps(fault, allow_none=False, encoding=None)
|
2011-09-30 12:55:38 +00:00
|
|
|
elif isinstance(e, openerp.exceptions.AccessError):
|
2011-09-30 08:50:12 +00:00
|
|
|
fault = xmlrpclib.Fault('warning -- AccessError\n\n' + str(e), '')
|
|
|
|
response = xmlrpclib.dumps(fault, allow_none=False, encoding=None)
|
2011-09-30 12:55:38 +00:00
|
|
|
elif isinstance(e, openerp.exceptions.AccessDenied):
|
2011-09-30 08:50:12 +00:00
|
|
|
fault = xmlrpclib.Fault('AccessDenied', str(e))
|
|
|
|
response = xmlrpclib.dumps(fault, allow_none=False, encoding=None)
|
2011-09-30 12:55:38 +00:00
|
|
|
elif isinstance(e, openerp.exceptions.DeferredException):
|
2011-09-30 08:50:12 +00:00
|
|
|
info = e.traceback
|
|
|
|
formatted_info = "".join(traceback.format_exception(*info))
|
|
|
|
fault = xmlrpclib.Fault(openerp.tools.ustr(e.message), formatted_info)
|
2011-09-02 15:02:26 +00:00
|
|
|
response = xmlrpclib.dumps(fault, allow_none=False, encoding=None)
|
2011-09-30 12:55:38 +00:00
|
|
|
else:
|
2011-09-27 10:22:46 +00:00
|
|
|
info = sys.exc_info()
|
|
|
|
formatted_info = "".join(traceback.format_exception(*info))
|
2011-09-30 08:50:12 +00:00
|
|
|
fault = xmlrpclib.Fault(openerp.tools.exception_to_unicode(e), formatted_info)
|
2011-09-02 15:02:26 +00:00
|
|
|
response = xmlrpclib.dumps(fault, allow_none=None, encoding=None)
|
2011-09-30 08:50:12 +00:00
|
|
|
return response
|
2011-09-01 13:04:50 +00:00
|
|
|
|
2011-09-30 13:38:21 +00:00
|
|
|
def wsgi_xmlrpc(environ, start_response):
|
2013-10-06 10:21:31 +00:00
|
|
|
""" Two routes are available for XML-RPC
|
2011-09-30 13:38:21 +00:00
|
|
|
|
2013-10-06 10:21:31 +00:00
|
|
|
/xmlrpc/<service> route returns faultCode as strings. This is a historic
|
|
|
|
violation of the protocol kept for compatibility.
|
2011-09-30 13:38:21 +00:00
|
|
|
|
2013-10-06 10:21:31 +00:00
|
|
|
/xmlrpc/2/<service> is a new route that returns faultCode as int and is
|
|
|
|
therefore fully compliant.
|
|
|
|
"""
|
2011-09-01 13:04:50 +00:00
|
|
|
if environ['REQUEST_METHOD'] == 'POST' and environ['PATH_INFO'].startswith('/xmlrpc/'):
|
|
|
|
length = int(environ['CONTENT_LENGTH'])
|
|
|
|
data = environ['wsgi.input'].read(length)
|
2013-10-06 10:21:31 +00:00
|
|
|
|
|
|
|
# Distinguish betweed the 2 faultCode modes
|
|
|
|
string_faultcode = True
|
|
|
|
if environ['PATH_INFO'].startswith('/xmlrpc/2/'):
|
|
|
|
service = environ['PATH_INFO'][len('/xmlrpc/2/'):]
|
|
|
|
string_faultcode = False
|
|
|
|
else:
|
|
|
|
service = environ['PATH_INFO'][len('/xmlrpc/'):]
|
2011-09-01 13:04:50 +00:00
|
|
|
|
|
|
|
params, method = xmlrpclib.loads(data)
|
2013-10-06 10:21:31 +00:00
|
|
|
return xmlrpc_return(start_response, service, method, params, string_faultcode)
|
2011-08-19 13:02:48 +00:00
|
|
|
|
2011-09-14 10:43:00 +00:00
|
|
|
# WSGI handlers registered through the register_wsgi_handler() function below.
|
2011-09-02 13:31:36 +00:00
|
|
|
module_handlers = []
|
2013-01-31 13:10:51 +00:00
|
|
|
# RPC endpoints registered through the register_rpc_endpoint() function below.
|
|
|
|
rpc_handlers = {}
|
2011-09-02 13:31:36 +00:00
|
|
|
|
|
|
|
def register_wsgi_handler(handler):
|
|
|
|
""" Register a WSGI handler.
|
|
|
|
|
|
|
|
Handlers are tried in the order they are added. We might provide a way to
|
|
|
|
register a handler for specific routes later.
|
|
|
|
"""
|
|
|
|
module_handlers.append(handler)
|
|
|
|
|
2013-01-31 13:10:51 +00:00
|
|
|
def register_rpc_endpoint(endpoint, handler):
|
|
|
|
""" Register a handler for a given RPC enpoint.
|
|
|
|
"""
|
|
|
|
rpc_handlers[endpoint] = handler
|
|
|
|
|
2012-09-22 10:51:07 +00:00
|
|
|
def application_unproxied(environ, start_response):
|
2011-09-01 13:04:50 +00:00
|
|
|
""" WSGI entry point."""
|
2013-03-01 12:07:44 +00:00
|
|
|
# cleanup db/uid trackers - they're set at HTTP dispatch in
|
|
|
|
# web.session.OpenERPSession.send() and at RPC dispatch in
|
|
|
|
# openerp.service.web_services.objects_proxy.dispatch().
|
|
|
|
# /!\ The cleanup cannot be done at the end of this `application`
|
|
|
|
# method because werkzeug still produces relevant logging afterwards
|
|
|
|
if hasattr(threading.current_thread(), 'uid'):
|
|
|
|
del threading.current_thread().uid
|
|
|
|
if hasattr(threading.current_thread(), 'dbname'):
|
|
|
|
del threading.current_thread().dbname
|
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
with openerp.api.Environment.manage():
|
|
|
|
# Try all handlers until one returns some result (i.e. not None).
|
|
|
|
wsgi_handlers = [wsgi_xmlrpc]
|
|
|
|
wsgi_handlers += module_handlers
|
|
|
|
for handler in wsgi_handlers:
|
|
|
|
result = handler(environ, start_response)
|
|
|
|
if result is None:
|
|
|
|
continue
|
|
|
|
return result
|
2011-08-19 13:02:48 +00:00
|
|
|
|
2011-09-14 10:43:00 +00:00
|
|
|
# We never returned from the loop.
|
2011-08-24 13:22:20 +00:00
|
|
|
response = 'No handler found.\n'
|
2011-09-14 10:43:00 +00:00
|
|
|
start_response('404 Not Found', [('Content-Type', 'text/plain'), ('Content-Length', str(len(response)))])
|
2011-08-24 13:22:20 +00:00
|
|
|
return [response]
|
2011-08-19 13:02:48 +00:00
|
|
|
|
2012-09-22 10:51:07 +00:00
|
|
|
def application(environ, start_response):
|
2012-09-24 10:56:22 +00:00
|
|
|
if config['proxy_mode'] and 'HTTP_X_FORWARDED_HOST' in environ:
|
2012-09-22 10:51:07 +00:00
|
|
|
return werkzeug.contrib.fixers.ProxyFix(application_unproxied)(environ, start_response)
|
|
|
|
else:
|
|
|
|
return application_unproxied(environ, start_response)
|
|
|
|
|
2011-09-14 15:06:03 +00:00
|
|
|
|
2011-08-19 13:02:48 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|