Changeset 4384


Ignore:
Timestamp:
Aug 8, 2008, 12:12:25 PM (14 years ago)
Author:
Nicklas Nordborg
Message:

References #1032: Apache server complains "Request-URI Too Large"

Main functionality is in place. The "New experiment" function is fixed and probably all other places where a new window is opened with the Main.openPopup script. Other functions that use 'location.href', 'location.replace' or 'window.open' are not fixed.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/config/dist/web.xml

    r4319 r4384  
    3737    <location>/exception/exception.jsp</location>
    3838  </error-page>
     39 
     40  <!--
     41    If BASE Javascript encounters an URL that is longer than
     42    specified by this setting when assigning it to for example
     43    location.href = url, BASE will instead try to rewrite the document
     44    to generate a POST request instead. This is to avoid problem with
     45    web servers that doesn't accept URL:s longer than a specified size
     46    For example, Apache has a default max length of 8190. If this setting
     47    is 0, the rewrite functionality is disabled.
     48  -->
     49  <context-param>
     50    <param-name>max-url-length</param-name>
     51    <param-value>8000</param-value>
     52  </context-param>
    3953 
    4054  <!--
  • trunk/src/clients/web/net/sf/basedb/clients/web/taglib/Head.java

    r4187 r4384  
    171171    sb.append("\t\t\treturn '").append(sc == null ? "" : sc.getId()).append("';\n");
    172172    sb.append("\t\t}\n");
     173    if (page.getMaxUrlLength() != null)
     174    {
     175      sb.append("\t\tfunction getMaxUrlLength()\n");
     176      sb.append("\t\t{\n");
     177      sb.append("\t\t\treturn '").append(page.getMaxUrlLength()).append("';\n");
     178      sb.append("\t\t}\n");
     179    }
    173180    sb.append("\t</script>\n");
    174181  }
  • trunk/src/clients/web/net/sf/basedb/clients/web/taglib/Page.java

    r4095 r4384  
    2828import net.sf.basedb.core.SessionControl;
    2929import net.sf.basedb.clients.web.Base;
     30import net.sf.basedb.util.Values;
    3031
    3132import javax.servlet.jsp.PageContext;
     
    163164  */
    164165  private static String BASE_VERSION = null;
     166 
     167  private static String MAX_URL_LENGTH = null;
    165168
    166169  /**
     
    221224  {
    222225    return ROOT;
     226  }
     227  /**
     228    @since 2.8
     229  */
     230  public String getMaxUrlLength()
     231  {
     232    return MAX_URL_LENGTH;
    223233  }
    224234  public String getServerName()
     
    246256  {
    247257    super.setPageContext(pageContext);
    248     if (ROOT == null) ROOT = ((HttpServletRequest)pageContext.getRequest()).getContextPath()+"/";
     258    if (ROOT == null)
     259    {
     260      ROOT = ((HttpServletRequest)pageContext.getRequest()).getContextPath()+"/";
     261      int maxUrlLength = Values.getInt(pageContext.getServletContext().getInitParameter("max-url-length"));
     262      MAX_URL_LENGTH = maxUrlLength > 0 ? String.valueOf(maxUrlLength) : null;
     263    }
    249264    try
    250265    {
  • trunk/www/include/scripts/main.js

    r4314 r4384  
    9191      url += '&warnIfOpen=0';
    9292    }
    93     window.newWindow = window.open(url, name, options);
    94     window.newWindow.isOld = true;
     93    newWin.isOld = true;
     94    window.newWindow = newWin;
     95    Main.safeSetLocation(url, newWin, true);
    9596    setTimeout("window.newWindow.focus()", 100);
    9697  }
     
    510511        i++;
    511512      }
    512       theWindow.location.href = url;
     513      Main.safeSetLocation(url, theWindow);
    513514    }
    514515  }
     
    540541        i++;
    541542      }
    542       theWindow.location.href = url;
     543      Main.safeSetLocation(url, theWindow);
    543544    }
    544545  }
     
    581582    url += '&item_type='+itemType+'&item_id='+itemId;
    582583    if (extraUrl) url += extraUrl;
    583     location.href = url;
    584   }
    585  
    586   this.getIdFromLocation = function()
     584    Main.safeSetLocation(url);
     585  }
     586 
     587  this.getIdFromLocation = function(url)
    587588  {
    588589    var findId = /ID=([0-9a-f]*)/;
    589     var ID = location.search.match(findId)[1];
    590     return ID;
     590    if (!url) url = location.search;
     591    var matches = url.match(findId);
     592    return matches && matches.length > 1 ? matches[1] : null;
    591593  }
    592594 
     
    625627  }
    626628 
    627  
    628   this.viewSource = function()
    629   {
     629  /*
     630    Safely set the location of a document using as if
     631    win.document.location.href = url or win.document.replace(url)
     632    had been used. If the url is longer
     633    than specified by getMaxUrlLength() function the url will
     634    be re-written to a <form> using hidden fields to submit
     635    query values.
     636    @param url The url to set
     637    @param win The window to set the location on, if not given,
     638      the current window is assumed
     639    @param replace If given, use location.replace() instead of location.href
     640      (only if url is not too long)
     641  */
     642  this.safeSetLocation = function(url, win, replace)
     643  {
     644    if (!url) return;
     645    if (!win) win = window;
     646    var postform;
     647    if (window.getMaxUrlLength && url.length > window.getMaxUrlLength())
     648    {
     649      postform = this.convertToPostForm(url, win);
     650    }
     651    if (postform)
     652    {
     653      document.body.appendChild(postform);
     654      postform.submit();
     655    }
     656    else if (replace)
     657    {
     658      win.document.location.replace(url);
     659    }
     660    else
     661    {
     662      win.document.location.href = url;
     663    }
     664  }
     665 
     666  /*
     667    Convert an URL to a HTML form using POST and write it to
     668    a document. This method extract all query parameters from the
     669    URL (except ID) and generate a <form> tag with hidden input fields.
     670    The method returns an unattached Form object. To post it,
     671    do: document.body.appendChild(form); form.submit();
     672    @param url The url to convert
     673    @param win The window the form should post to, if no window is given
     674      or if it doesn't have a name the form is posted to the current
     675      window.
     676  */
     677  this.convertToPostForm = function(url, win)
     678  {
     679    var i = url.indexOf('?');
     680    if (i < 0) return null;
     681   
     682    var query = url.substring(i+1);
     683    if (!query) return null;
     684   
     685    url = url.substring(0, i);
     686    var ID = Main.getIdFromLocation(query);
     687    if (ID) url += '?ID=' + ID;
     688
     689    var frm = document.createElement('form');
     690    frm.setAttribute('action', url);
     691    frm.setAttribute('method', 'post');
     692    if (win && win.name) frm.setAttribute('target', win.name);
     693   
     694    var options = query.split('&');
     695    for (var i = 0; i < options.length; i++)
     696    {
     697      var pair = options[i].split('=');
     698      var name = pair[0];
     699      var value = decodeURIComponent(pair[1]);
     700      if (name && name != 'ID')
     701      {
     702        Forms.createHidden(frm, name, value, document);
     703      }
     704    }
     705    return frm;
     706  }
     707 
     708  this.viewSource = function(doc)
     709  {
     710    if (!doc) doc = document;
    630711    this.openPopup('', 'ViewSource', 800, 800);
    631     var html = document.body.parentNode.innerHTML;
     712    var html = doc.body.parentNode.innerHTML;
    632713    html = html.replace(/\</g, '&lt;');
    633714    html = html.replace(/\>/g, '&gt;');
     
    16921773    @param name The name of the hidden element
    16931774    @param value The value of the hidden element
    1694   */
    1695   this.createHidden = function(frm, name, value)
    1696   {
    1697     var hidden = document.createElement('input');
     1775    @param doc The document to create the element in, if not given
     1776      uses the global 'document' object
     1777  */
     1778  this.createHidden = function(frm, name, value, doc)
     1779  {
     1780    if (!doc) doc = document;
     1781    var hidden = doc.createElement('input');
    16981782    hidden.setAttribute('type', 'hidden');
    16991783    hidden.setAttribute('name', name);
Note: See TracChangeset for help on using the changeset viewer.