7c52dd3db4646dfaa5ec108e5c8426b7c4a2507a
[odoo/odoo.git] / openerp / wsgi.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2011 OpenERP s.a. (<http://openerp.com>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 """ WSGI stuffs (proof of concept for now)
23
24 This module offers an WSGI interface to OpenERP.
25
26 """
27
28 from wsgiref.simple_server import make_server
29 from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
30 import xmlrpclib
31
32 import sys
33
34 import openerp
35 import openerp.tools.config as config
36
37 def wsgi_xmlrpc(environ, start_response):
38     if environ['REQUEST_METHOD'] == 'POST' and environ['PATH_INFO'].startswith('/xmlrpc/'):
39         length = int(environ['CONTENT_LENGTH'])
40         data = environ['wsgi.input'].read(length)
41         path = environ['PATH_INFO'][len('/xmlrpc/'):] # expected to be one of db, object, ... TODO should we expect a possible trailing '/' ?
42
43         # TODO see SimpleXMLRPCDispatcher._marshaled_dispatch() for some necessary handling.
44         # TODO see OpenERPDispatcher for some othe handling (in particular, auth things).
45         params, method = xmlrpclib.loads(data)
46         result = openerp.netsvc.ExportService.getService(path).dispatch(method, None, params)
47         response = xmlrpclib.dumps((result,), methodresponse=1, allow_none=False, encoding=None)
48
49         start_response("200 OK", [('Content-Type','text/xml'), ('Content-Length', str(len(response)))])
50         return [response]
51
52 def wsgi_jsonrpc(environ, start_response):
53     pass
54
55 def application(environ, start_response):
56
57     # Try all handlers until one returns some result (i.e. not None).
58     wsgi_handlers = [wsgi_xmlrpc, wsgi_jsonrpc]
59     for handler in wsgi_handlers:
60         result = handler(environ, start_response)
61         if result is None:
62             continue
63         return result
64
65     # We never returned from the loop.
66     else:
67         response = 'No handler found.\n'
68         start_response('200 OK', [('Content-Type', 'text/plain'), ('Content-Length', str(len(response)))])
69         return [response]
70
71 # Serve XMLRPC via wsgiref's simple_server.
72 # Blocking, should probably be called in its own process.
73 def serve():
74     httpd = make_server('localhost', config['xmlrpc_port'], application)
75     httpd.serve_forever()
76
77 # Application setup be fore we can spawn any worker process.
78 # This is suitable for e.g. gunicorn's on_starting hook.
79 def on_starting(server):
80     config = openerp.tools.config
81     config['addons_path'] = '/home/openerp/repos/addons/trunk/' # need a config file
82     openerp.netsvc.init_logger()
83     openerp.osv.osv.start_object_proxy()
84     openerp.service.web_services.start_web_services()
85
86 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: