Skip to content

Commit

Permalink
fixed bugs in url class:
Browse files Browse the repository at this point in the history
- correct backpath ('..') handling
- correct absolute path handling
- included https


git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@2428 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
orbiter committed Aug 19, 2006
1 parent 1ce3c22 commit b7dc251
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions source/de/anomic/net/URL.java
Expand Up @@ -72,6 +72,7 @@ public void parseURLString(String url) throws MalformedURLException {
path = url.substring(q);
}

resolveBackpath();
identPort(url);
identRef();
identQuest();
Expand Down Expand Up @@ -105,23 +106,33 @@ public URL(URL baseURL, String relPath) throws MalformedURLException {
if (baseURL == null) throw new MalformedURLException("base URL is null");
int p = relPath.indexOf(':');
String relprotocol = (p < 0) ? null : relPath.substring(0, p).toLowerCase();
if ((relprotocol != null) && ("http.ftp.mailto".indexOf(relprotocol) >= 0)) {
if ((relprotocol != null) && ("http.https.ftp.mailto".indexOf(relprotocol) >= 0)) {
parseURLString(relPath);
} else {
this.protocol = baseURL.protocol;
this.host = baseURL.host;
this.port = baseURL.port;
if (relPath.toLowerCase().startsWith("javascript:")) this.path = baseURL.path;
else if (relPath.startsWith("/")) this.path = relPath;
else {
if (relPath.startsWith("#") ||
relPath.startsWith("?") ||
baseURL.path.endsWith("/")) this.path = baseURL.path + relPath;
else this.path = baseURL.path + "/" + relPath;
if (relPath.toLowerCase().startsWith("javascript:")) {
this.path = baseURL.path;
} else if (relPath.startsWith("/")) {
this.path = relPath;
} else if (!(baseURL.path.endsWith("/"))) {
if (relPath.startsWith("#") || relPath.startsWith("?")) {
this.path = baseURL.path + relPath;
} else {
this.path = relPath;
}
} else {
if (relPath.startsWith("#") || relPath.startsWith("?")) {
this.path = baseURL.path + relPath;
} else {
this.path = baseURL.path + "/" + relPath;
}
}
this.quest = baseURL.quest;
this.ref = baseURL.ref;

resolveBackpath();
identRef();
identQuest();
}
Expand All @@ -137,6 +148,16 @@ public URL(String protocol, String host, int port, String path) throws Malformed
identQuest();
}

private void resolveBackpath() throws MalformedURLException {
// resolve '..'
int p;
while ((p = path.indexOf("/..")) >= 0) {
String head = path.substring(0, p);
int q = head.lastIndexOf('/');
if (q < 0) throw new MalformedURLException("backpath cannot be resolved in path = " + path);
path = head.substring(0, q) + path.substring(p + 3);
}
}
private void identPort(String inputURL) throws MalformedURLException {
// identify ref in file
int r = host.indexOf(':');
Expand Down Expand Up @@ -180,9 +201,8 @@ public String getFile() {

public String getFile(boolean includeReference) {
// this is the path plus quest plus ref
if (quest != null) return path + "?" + quest;
if ((ref != null) && (includeReference)) return path + "#" + ref;
return path;
if (quest != null) return ((includeReference) && (ref != null)) ? path + "?" + quest + "#" + ref : path + "?" + quest;
return ((includeReference) && (ref != null)) ? path + "#" + ref : path;
}

public String getPath() {
Expand Down Expand Up @@ -274,6 +294,7 @@ public int compareTo(Object h) {
public static void main(String[] args) {
URL u, v;
try {

u = new URL("http://www.anomic.de/home/test?x=1#home");
System.out.println("toString=" + u.toString() + "\ntoNormalform=" + u.toNormalform() + "\n");

Expand All @@ -286,19 +307,33 @@ public static void main(String[] args) {
u = new URL("ftp://ftp.anomic.de/home/test#home");
System.out.println("toString=" + u.toString() + "\ntoNormalform=" + u.toNormalform() + "\n");

u = new URL("http://www.anomic.de/home/../abc/");
System.out.println("toString=" + u.toString() + "\ntoNormalform=" + u.toNormalform() + "\n");

u = new URL("mailto:abcdefg@nomailnomail.com");
System.out.println("toString=" + u.toString() + "\ntoNormalform=" + u.toNormalform() + "\n");

v = new URL("http://www.anomic.de/home");
u = new URL(v, "test");
System.out.println("toString u=" + u.toString() + "\ntoNormalform=" + u.toNormalform() + "\n");
u = new URL(v, "test/");
System.out.println("toString u=" + u.toString() + "\ntoNormalform=" + u.toNormalform() + "\n");

v = new URL("http://www.anomic.de/home/");
u = new URL(v, "test");
System.out.println("toString u=" + u.toString() + "\ntoNormalform=" + u.toNormalform() + "\n");
u = new URL(v, "test/");
System.out.println("toString u=" + u.toString() + "\ntoNormalform=" + u.toNormalform() + "\n");

u = new URL(v, "http://www.yacy.net/test");
System.out.println("toString u=" + u.toString() + "\ntoNormalform=" + u.toNormalform() + "\n");

u = new URL(v, "ftp://ftp.yacy.net/test");
System.out.println("toString u=" + u.toString() + "\ntoNormalform=" + u.toNormalform() + "\n");

u = new URL(v, "../test");
System.out.println("toString u=" + u.toString() + "\ntoNormalform=" + u.toNormalform() + "\n");

u = new URL(v, "mailto:abcdefg@nomailnomail.com");
System.out.println("toString u=" + u.toString() + "\ntoNormalform=" + u.toNormalform() + "\n");

Expand Down

0 comments on commit b7dc251

Please sign in to comment.