[FIX] use absolute url when getting css/js/qweb files + use a static proxy for gettin...
authorChristophe Simonis <chs@openerp.com>
Wed, 9 Nov 2011 08:16:26 +0000 (09:16 +0100)
committerChristophe Simonis <chs@openerp.com>
Wed, 9 Nov 2011 08:16:26 +0000 (09:16 +0100)
bzr revid: chs@openerp.com-20111109081626-l868h7w8glinbxdl

addons/web/common/http.py
addons/web/static/src/js/core.js
addons/web/static/src/js/jq_ajax.js

index ed7a534..ab3c3c4 100644 (file)
@@ -9,6 +9,7 @@ import logging
 import urllib
 import os
 import pprint
+import re
 import sys
 import traceback
 import uuid
@@ -436,6 +437,19 @@ class JSONP(Controller):
         response = werkzeug.wrappers.Response(request_id, headers=headers)
         return response
 
+    @jsonrequest
+    def static_proxy(self, req, path):
+        #req.config.socket_port
+        
+
+        #if not re.match('^/[^/]+/static/.*', path):
+        #    return werkzeug.exceptions.BadRequest()
+
+        env = req.httprequest.environ
+        port = env['SERVER_PORT']
+
+        o = urllib.urlopen('http://127.0.0.1:%s%s' % (port, path))
+        return o.read()
 
 class Root(object):
     """Root WSGI application for the OpenERP Web Client.
index 951e6e2..fac18f1 100644 (file)
@@ -349,7 +349,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.
     init: function(server) {
         this._super();
         var hostname = _('%s//%s').sprintf(location.protocol, location.host);
-        this.server = (server == undefined) ? hostname : server;
+        this.server = _.rtrim((server == undefined) ? hostname : server, '/');
         this.rpc_mode = (this.server == hostname) ? "oe-json" : "oe-jsonp";
         this.debug = ($.deparam($.param.querystring()).debug != undefined);
         this.session_id = false;
@@ -370,6 +370,29 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.
     start: function() {
         this.session_restore();
     },
+
+    get_absolute_url: function(path) {
+        var r_has_protocol = /^https?:\/\//,
+            r_absolute_internal = /^\/[^\/]/;   // starts with / (but not //)
+
+        
+        if (r_has_protocol.test(path)) {
+            if (!_(path).startsWith(this.server + '/')) {
+                $.error('can only contact openerp.server');
+            }
+            absolute_url = path;
+        } else if (r_absolute_internal.test(path)) {
+            absolute_url = this.server + path;
+        } else {    // relative url
+            // XXX is it correct to mix document.location and this.server ??
+            var parts = document.location.pathname.split('/');
+            parts.pop();
+            parts.push(path);
+            absolute_url = this.server + parts.join('/');
+        }
+        return absolute_url
+    },
+
     /**
      * Executes an RPC call, registering the provided callbacks.
      *
@@ -574,11 +597,11 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.
                     self.user_context.lang.replace("_", "-") + ".js"
                 ];
 
+                self.rpc('/web/webclient/qweblist', {"mods": modules}, self.do_load_qweb);
                 self.rpc('/web/webclient/csslist', {"mods": modules}, self.do_load_css);
                 self.rpc('/web/webclient/jslist', {"mods": modules}, function(files) {
                     self.do_load_js(file_list.concat(files));
                 });
-                self.rpc('/web/webclient/qweblist', {"mods": modules}, self.do_load_qweb);
                 openerp._modules_loaded = true;
             });
         });
@@ -587,7 +610,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.
         var self = this;
         _.each(files, function (file) {
             $('head').append($('<link>', {
-                'href': file,
+                'href': self.get_absolute_url(file),
                 'rel': 'stylesheet',
                 'type': 'text/css'
             }));
@@ -599,7 +622,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.
             var file = files.shift();
             var tag = document.createElement('script');
             tag.type = 'text/javascript';
-            tag.src = file;
+            tag.src = self.get_absolute_url(file);
             tag.onload = tag.onreadystatechange = function() {
                 if ( (tag.readyState && tag.readyState != "loaded" && tag.readyState != "complete") || tag.onload_done )
                     return;
@@ -615,7 +638,23 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.
     do_load_qweb: function(files) {
         var self = this;
         _.each(files, function(file) {
-            openerp.web.qweb.add_template(file);
+            self.rpc({
+                //url: self.get_absolute_url(file),
+                url: '/web/jsonp/static_proxy',
+                async: false,
+                //dataType: 'text',
+
+            },
+            {
+                path: file,
+            }, 
+            function(xml) {
+            //function(result) {
+            //    console.log('do_load_qweb', result);
+            //    var xml = result.result;
+            //}).then(function(xml) {
+                openerp.web.qweb.add_template(_(xml).trim());
+            });
         });
     },
     on_modules_loaded: function() {
index 8d347c3..19c3bb3 100644 (file)
@@ -9,16 +9,11 @@
 
         // common preconditions checks
         $.ajaxPrefilter("oe-json oe-jsonp", function(options, originalOptions, jqXHR) {
-            console.log('use', options.dataType);
             if (!$.isPlainObject(options.openerp)) {
                 console.error(options.openerp);
                 $.error('"openerp" option is required.');
             }
             
-            if (_(options.openerp.server).endsWith('/')) {
-                options.openerp.server = options.openerp.server.substr(0, options.openerp.server.length-1);
-            }
-
             if (!$.isPlainObject(options.data)) {
                 $.error('data must not be serialized');
             }
             }
 
 
+            
                 var max_url_length = options.max_url_length || 1000,
                     absolute_url, full_url;
-                 
+            /*     
                 var r_has_protocol = /^https?:\/\//,
                     r_absolute_internal = /^\/[^\/]/;   // starts with / (but not //)
 
@@ -69,7 +65,9 @@
                     parts.push(options.url);
                     absolute_url = options.openerp.server + parts.join('/');
                 }
-                
+            // */
+
+            var absolute_url = options.openerp.get_absolute_url(options.url);
 
                 /// now, made the same url changes that jQuery will do...
                    var rquery = /\?/,
@@ -89,9 +87,6 @@
                     // if nothing was replaced, add timestamp to the end
                     full_url = ret + ((ret === full_url) ? (rquery.test(full_url) ? "&" : "?") + "_=" + ts : "");
                 }
-
-                console.log('absolute_url', absolute_url);
-                console.log('full_url', full_url);
                 
                 options.url = absolute_url;
 
 
         $.ajaxTransport("oe-jsonp", function(options, originalOptions, jqXHR) {
 
-            console.log('real oe-jsonp', options);
                 var $iframe = null;
                 var $form = $('<form>')
                                 .attr('method', 'POST')
                                 .appendTo($('body'))
                                 ;
 
-                console.log($form);
-                
                 function cleanUp() {
                     if ($iframe) {
                         $iframe.unbind("load").attr("src", "javascript:false;").remove();
 
                         // the first bind is fired up when the iframe is added to the DOM
                         $iframe.bind('load', function() {
-                            //console.log('bind1', this);
                             // the second bind is fired up when the result of the form submission is received
                             $iframe.unbind('load').bind('load', function() {
-                                //console.log('bind2', this);
-                                
                                 // we cannot access the content of remote iframe.
                                 // but we don't care, we try to get the result in any cases