From ea4bb981402542f9b00eb69a26c90e810d33bae6 Mon Sep 17 00:00:00 2001 From: "P. Christeas" Date: Mon, 26 Jul 2010 12:33:34 +0300 Subject: [PATCH] Feature: serve static http, from a dir Now, it is possible to serve static http from the files of one dir. Just configure like: [static-http] enable = True dir_path = /home/panos/build/openerp/doc/build/html and the contents of that directory will be served as a default at our http port ;) Conflicts: bin/service/http_server.py bzr revid: p_christ@hol.gr-20100726093334-2xr5mg0c9xvm68ej --- bin/openerp-server.py | 1 + bin/service/http_server.py | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/bin/openerp-server.py b/bin/openerp-server.py index c56977fe1f0..5a68440af27 100755 --- a/bin/openerp-server.py +++ b/bin/openerp-server.py @@ -108,6 +108,7 @@ if not ( tools.config["stop_after_init"] or \ tools.config["translate_out"] ): service.http_server.init_servers() service.http_server.init_xmlrpc() + service.http_server.init_static_http() import service.netrpc_server service.netrpc_server.init_servers() diff --git a/bin/service/http_server.py b/bin/service/http_server.py index a61877b20e6..f185549db25 100644 --- a/bin/service/http_server.py +++ b/bin/service/http_server.py @@ -34,6 +34,8 @@ import netsvc import errno import threading import tools +import posixpath +import urllib import os import select import socket @@ -273,6 +275,47 @@ def init_xmlrpc(): netsvc.Logger().notifyChannel('web-services', netsvc.LOG_INFO, "Registered XML-RPC over HTTPS") +class StaticHTTPHandler(HTTPHandler): + def __init__(self,request, client_address, server): + HTTPHandler.__init__(self,request,client_address,server) + dir_path = tools.config.get_misc('static-http', 'dir_path', False) + assert dir_path + self.__basepath = dir_path + + def translate_path(self, path): + """Translate a /-separated PATH to the local filename syntax. + + Components that mean special things to the local file system + (e.g. drive or directory names) are ignored. (XXX They should + probably be diagnosed.) + + """ + # abandon query parameters + path = path.split('?',1)[0] + path = path.split('#',1)[0] + path = posixpath.normpath(urllib.unquote(path)) + words = path.split('/') + words = filter(None, words) + path = self.__basepath + for word in words: + if word in (os.curdir, os.pardir): continue + path = os.path.join(path, word) + return path + +def init_static_http(): + if not tools.config.get_misc('static-http','enable', True): + return + + dir_path = tools.config.get_misc('static-http', 'dir_path', False) + assert dir_path + + base_path = tools.config.get_misc('static-http', 'base_path', '/') + + reg_http_service(HTTPDir(base_path,StaticHTTPHandler)) + + netsvc.Logger().notifyChannel("web-services", netsvc.LOG_INFO, + "Registered HTTP dir %s for %s" % (dir_path, base_path)) + class OerpAuthProxy(AuthProxy): """ Require basic authentication..