Java URL Normalize normalizeUrl(URL url)

Here you can find the source of normalizeUrl(URL url)

Description

Returns a string containing a properly formed URL with port and scheme information.

License

Open Source License

Parameter

Parameter Description
url the java.net.URL that needs normalization.

Return

a that contains a well-formed URL.

Declaration

public static String normalizeUrl(URL url) 

Method Source Code


//package com.java2s;
/*//from  w ww .  j  a  va 2 s.c  o  m
 * BurpKit - WebKit-based penetration testing plugin for BurpSuite
 * Copyright (C) 2015  Red Canari, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Returns a string containing a properly formed URL with port and scheme information.
     * @param url the {@link java.net.URL} that needs normalization.
     * @return a {@link java.lang.String} that contains a well-formed URL.
     */
    public static String normalizeUrl(URL url) {
        int port = url.getPort();
        String portString = "";
        String protocol = url.getProtocol().toLowerCase();
        String file = url.getFile();
        String query = url.getQuery();

        if (port == -1)
            port = (url.getProtocol().equals("https")) ? 443 : 80;
        if ((protocol.equals("http") && port != 80) || (protocol.equals("https") && port != 443))
            portString = ":" + port;

        if (query != null) {
            List<String> normalizedParameters = new ArrayList<>();
            for (String parameter : query.split("&")) {
                String[] nameValue = parameter.split("=", 2);

                try {
                    nameValue[0] = URLEncoder.encode(URLDecoder.decode(nameValue[0], "UTF-8"), "UTF-8");
                    if (nameValue.length != 2)
                        normalizedParameters.add(nameValue[0]);
                    else {
                        nameValue[1] = URLEncoder.encode(URLDecoder.decode(nameValue[1], "UTF-8"), "UTF-8");
                        normalizedParameters.add(nameValue[0] + "=" + nameValue[1]);
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }
            file = file.replace(query, String.join("&", normalizedParameters));
        }

        return protocol + "://" + url.getHost().toLowerCase() + portString + file;
    }
}

Related

  1. normalizeUrl(String url)
  2. normalizeURL(URL codebase)
  3. normalizeUrl(URL url)
  4. normalizeUrl(URL url)
  5. normalizeURL(URL url)
  6. normalizeUrl(URL url)
  7. normalizeURL(URL url)
  8. normalizeUrlStr(String urlStr)