OpenLayers OpenLayers

Ticket #803: mapserver_deterministic_url.patch

File mapserver_deterministic_url.patch, 3.8 kB (added by crschmidt, 1 year ago)
  • tests/Layer/test_MapServer.html

    old new  
    156156    } 
    157157 
    158158    function test_07_Layer_MapServer_getFullRequestString (t) { 
    159  
    160          
    161         t.plan( 1 ); 
     159        t.plan( 3 ); 
    162160        var map = new OpenLayers.Map('map'); 
    163161        tUrl = "http://labs.metacarta.com/cgi-bin/mapserv"; 
    164162        tParams = { layers: 'basic',  
     
    178176         
    179177        t.eq(str, sStr , "getFullRequestString() works"); 
    180178        map.destroy(); 
     179         
     180        tUrl = ["http://octo.metacarta.com/cgi-bin/mapserv","http://labs.metacarta.com/cgi-bin/mapserv"]; 
     181        layer = new OpenLayers.Layer.MapServer(name, tUrl, tParams, null); 
     182        str = layer.getFullRequestString({'c':'d'}); 
     183        t.eq(str, tUrl[1] + '?' + OpenLayers.Util.getParameterString(OpenLayers.Util.extend(tParams,{'c':'d'})), "getFullRequestString() works for list of two urls and is deterministic"); 
     184        layer.destroy(); 
     185        var tParams = { 
     186                 layers: 'basic',  
     187                 format: 'png', 
     188                 mode: 'map', 
     189                 map_imagetype: 'png' 
     190        }; 
     191        tUrl = ["http://octo.metacarta.com/cgi-bin/mapserv","http://labs.metacarta.com/cgi-bin/mapserv"]; 
     192        layer = new OpenLayers.Layer.MapServer(name, tUrl, tParams, null); 
     193        str = layer.getFullRequestString({'a':'b'}); 
     194        t.eq(str, tUrl[0] + '?' + OpenLayers.Util.getParameterString(OpenLayers.Util.extend(tParams,{'a':'b'})), "getFullRequestString() works for list of two urls and is deterministic"); 
     195        layer.destroy(); 
    181196 
    182197    } 
    183198 
  • lib/OpenLayers/Layer/MapServer.js

    old new  
    142142        // use layer's url unless altUrl passed in 
    143143        var url = (altUrl == null) ? this.url : altUrl; 
    144144         
    145         // if url is not a string, it should be an array of strings,  
    146         //  in which case we will randomly select one of them in order 
    147         //  to evenly distribute requests to different urls. 
    148         if (typeof url == "object") { 
    149             url = url[Math.floor(Math.random()*url.length)]; 
    150         }    
    151         // requestString always starts with url 
    152         var requestString = url;         
    153  
    154145        // create a new params hashtable with all the layer params and the  
    155146        // new params together. then convert to string 
    156147        var allParams = OpenLayers.Util.extend({}, this.params); 
    157148        allParams = OpenLayers.Util.extend(allParams, newParams); 
     149        var paramsString = OpenLayers.Util.getParameterString(allParams); 
     150         
     151        // if url is not a string, it should be an array of strings,  
     152        // in which case we will deterministically select one of them in  
     153        // order to evenly distribute requests to different urls. 
     154        if (url instanceof Array) { 
     155            url = this.selectUrl(paramsString, url); 
     156        }    
     157         
    158158        // ignore parameters that are already in the url search string 
    159159        var urlParams = OpenLayers.Util.upperCaseObject( 
    160160                            OpenLayers.Util.getParameters(url)); 
     
    163163                delete allParams[key]; 
    164164            } 
    165165        } 
    166         var paramsString = OpenLayers.Util.getParameterString(allParams); 
     166        paramsString = OpenLayers.Util.getParameterString(allParams); 
    167167         
     168        // requestString always starts with url 
     169        var requestString = url;         
     170 
    168171        // MapServer needs '+' seperating things like bounds/height/width. 
    169172        //   Since typically this is URL encoded, we use a slight hack: we 
    170173        //  depend on the list-like functionality of getParameterString to