Changeset 6972 in josm


Ignore:
Timestamp:
2014-04-12T11:26:11+02:00 (10 years ago)
Author:
bastiK
Message:

see #9906 - remotecontrol import handler: add workaround when URLs with multibyte
characters are only one time percent encoded

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImportHandler.java

    r6897 r6972  
    1313import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
    1414import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
     15import org.openstreetmap.josm.tools.Utils;
    1516
    1617/**
     
    113114    @Override
    114115    protected void validateRequest() throws RequestHandlerBadRequestException {
    115         final String urlString = args.get("url");
     116        String urlString = args.get("url");
     117        if (Main.pref.getBoolean("remotecontrol.importhandler.fix_url_query", true)) {
     118            urlString = Utils.fixURLQuery(urlString);
     119        }
    116120        try {
    117121            // Ensure the URL is valid
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r6962 r6972  
    2323import java.io.InputStreamReader;
    2424import java.io.OutputStream;
     25import java.io.UnsupportedEncodingException;
    2526import java.net.HttpURLConnection;
    2627import java.net.MalformedURLException;
    2728import java.net.URL;
    2829import java.net.URLConnection;
     30import java.net.URLEncoder;
    2931import java.nio.channels.FileChannel;
    3032import java.nio.charset.Charset;
     
    7375    private static final int MILLIS_OF_DAY = 86400000;
    7476
     77    public static String URL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=";
     78   
    7579    /**
    7680     * Tests whether {@code predicate} applies to at least one elements from {@code collection}.
     
    982986        }
    983987    }
     988   
     989    /**
     990     * Fixes URL with illegal characters in the query (and fragment) part by
     991     * percent encoding those characters.
     992     *
     993     * special characters like & and # are not encoded
     994     *
     995     * @param url the URL that should be fixed
     996     * @return the repaired URL
     997     */
     998    public static String fixURLQuery(String url) {
     999        if (url.indexOf('?') == -1)
     1000            return url;
     1001       
     1002        String query = url.substring(url.indexOf('?') + 1);
     1003       
     1004        StringBuilder sb = new StringBuilder(url.substring(0, url.indexOf('?') + 1));
     1005       
     1006        for (int i=0; i<query.length(); i++) {
     1007            String c = query.substring(i, i+1);
     1008            if (URL_CHARS.contains(c)) {
     1009                sb.append(c);
     1010            } else {
     1011                try {
     1012                    sb.append(URLEncoder.encode(c, "UTF-8"));
     1013                } catch (UnsupportedEncodingException ex) {
     1014                    throw new RuntimeException(ex);
     1015                }
     1016            }
     1017        }
     1018        return sb.toString();
     1019    }
     1020   
    9841021}
Note: See TracChangeset for help on using the changeset viewer.