From 404e54536590e56fc681e3b6f4d8f5e963bc6740 Mon Sep 17 00:00:00 2001
From: Antony Lesuisse <al@openerp.com>
Date: Sun, 25 Sep 2011 03:20:39 +0200
Subject: [PATCH] [FIX] revert 3651
 al@openerp.com-20110925003322-yioqd1cdlffvr6uc http static is used by webdav

bzr revid: al@openerp.com-20110925012039-4hmqj2v5aaxxtxwy
---
 openerp/service/http_server.py | 45 ++++++++++++++++++++++++++++++++++
 openerp/tools/config.py        |  8 ++++++
 2 files changed, 53 insertions(+)

diff --git a/openerp/service/http_server.py b/openerp/service/http_server.py
index 57fe21d8fea2..9bfb9397d190 100644
--- a/openerp/service/http_server.py
+++ b/openerp/service/http_server.py
@@ -246,6 +246,7 @@ class XMLRPCRequestHandler(FixSendError,HttpLogHandler,SimpleXMLRPCServer.Simple
         self.connection = dummyconn()
         self.rpc_paths = map(lambda s: '/%s' % s, netsvc.ExportService._services.keys())
 
+
 def init_xmlrpc():
     if tools.config.get('xmlrpc', False):
         # Example of http file serving:
@@ -259,6 +260,50 @@ def init_xmlrpc():
         reg_http_service('/xmlrpc/', XMLRPCRequestHandler, secure_only=True)
         logging.getLogger("web-services").info("Registered XML-RPC over HTTPS only")
 
+class StaticHTTPHandler(HttpLogHandler, FixSendError, HttpOptions, HTTPHandler):
+    _logger = logging.getLogger('httpd')
+    _HTTP_OPTIONS = { 'Allow': ['OPTIONS', 'GET', 'HEAD'] }
+
+    def __init__(self,request, client_address, server):
+        HTTPHandler.__init__(self,request,client_address,server)
+        document_root = tools.config.get('static_http_document_root', False)
+        assert document_root, "Please specify static_http_document_root in configuration, or disable static-httpd!"
+        self.__basepath = document_root
+
+    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('static_http_enable', False):
+        return
+    
+    document_root = tools.config.get('static_http_document_root', False)
+    assert document_root, "Document root must be specified explicitly to enable static HTTP service (option --static-http-document-root)"
+    
+    base_path = tools.config.get('static_http_url_prefix', '/')
+    
+    reg_http_service(base_path, StaticHTTPHandler)
+    
+    logging.getLogger("web-services").info("Registered HTTP dir %s for %s" % \
+                        (document_root, base_path))
+
 import security
 
 class OpenERPAuthProvider(AuthProvider):
diff --git a/openerp/tools/config.py b/openerp/tools/config.py
index 2d9d0b4f587f..3304a4f4a992 100644
--- a/openerp/tools/config.py
+++ b/openerp/tools/config.py
@@ -150,6 +150,13 @@ class configmanager(object):
                          help="Filter listed database", metavar="REGEXP")
         parser.add_option_group(group)
 
+        # Static HTTP
+        group = optparse.OptionGroup(parser, "Static HTTP service")
+        group.add_option("--static-http-enable", dest="static_http_enable", action="store_true", my_default=False, help="enable static HTTP service for serving plain HTML files")
+        group.add_option("--static-http-document-root", dest="static_http_document_root", help="specify the directory containing your static HTML files (e.g '/var/www/')")
+        group.add_option("--static-http-url-prefix", dest="static_http_url_prefix", help="specify the URL root prefix where you want web browsers to access your static HTML files (e.g '/')")
+        parser.add_option_group(group)
+
         # Testing Group
         group = optparse.OptionGroup(parser, "Testing Configuration")
         group.add_option("--test-file", dest="test_file", my_default=False,
@@ -340,6 +347,7 @@ class configmanager(object):
                 'netrpc_interface', 'netrpc_port', 'db_maxconn', 'import_partial', 'addons_path',
                 'netrpc', 'xmlrpc', 'syslog', 'without_demo', 'timezone',
                 'xmlrpcs_interface', 'xmlrpcs_port', 'xmlrpcs',
+                'static_http_enable', 'static_http_document_root', 'static_http_url_prefix',
                 'secure_cert_file', 'secure_pkey_file', 'dbfilter'
                 ]
 
-- 
GitLab