UPnP: Fix construction of absolute URLs

where the service relative URL is an absolute path
and the location absolute URL is below the top level.
Fixes communication with "Freebox" UPnP routers.
ref: http://zzz.i2p/topics/3157
This commit is contained in:
zzz
2021-08-07 08:39:05 -04:00
parent 2cb0650980
commit 7d1656c20f
3 changed files with 41 additions and 28 deletions

View File

@ -1,3 +1,13 @@
2021-08-07 zzz
* UPnP: Fix URL handling
2021-08-02 zzz
* Console: Show restart button if there's an update
to be handled by a post-processor
2021-07-30 zzz
* Util: Update DoH server list
2021-07-28 zzz
* Tunnels: Fixes for proposal 157

View File

@ -18,10 +18,10 @@ public class RouterVersion {
/** deprecated */
public final static String ID = "Git";
public final static String VERSION = CoreVersion.VERSION;
public final static long BUILD = 7;
public final static long BUILD = 8;
/** for example "-test" */
public final static String EXTRA = "";
public final static String EXTRA = "-rc";
public final static String FULL_VERSION = VERSION + "-" + BUILD + EXTRA;
public static void main(String args[]) {
System.out.println("I2P Router version: " + FULL_VERSION);

View File

@ -268,16 +268,11 @@ public class Device implements org.cybergarage.http.HTTPRequestListener,
if ((baseURLStr == null) || (baseURLStr.length() <= 0)) {
if ((locationURLStr != null) && (0 < locationURLStr.length())) {
if (!locationURLStr.endsWith("/") || !urlString.startsWith("/")) {
if (!urlString.startsWith("/")) {
String absUrl;
// I2P - getAbsoluteURL("/WANIPCn.xml", "", "http://192.168.1.1:5555/rootDesc.xml")
// returns here as "http://192.168.1.1:5555/rootDesc.xml/WANIPCn.xml" which is horribly wrong
// So back up to last slash
// Relative to location, so back up to last slash
if (!locationURLStr.endsWith("/")) {
if (urlString.startsWith("/"))
absUrl = locationURLStr.substring(0, locationURLStr.lastIndexOf('/')) + urlString;
else
absUrl = locationURLStr.substring(0, locationURLStr.lastIndexOf('/') + 1) + urlString;
absUrl = locationURLStr.substring(0, locationURLStr.lastIndexOf('/') + 1) + urlString;
} else {
absUrl = locationURLStr + urlString;
}
@ -287,14 +282,6 @@ public class Device implements org.cybergarage.http.HTTPRequestListener,
return url.toString();
} catch (Exception e) {
}
} else {
String absUrl = locationURLStr + urlString.substring(1);
try {
URL url = new URL(absUrl);
//Debug.warning("Return 2: " + url);
return url.toString();
} catch (Exception e) {
}
}
String absUrl = HTTP.getAbsoluteURL(locationURLStr, urlString);
@ -318,22 +305,20 @@ public class Device implements org.cybergarage.http.HTTPRequestListener,
}
if ((baseURLStr != null) && (0 < baseURLStr.length())) {
if (!baseURLStr.endsWith("/") || !urlString.startsWith("/")) {
String absUrl = baseURLStr + urlString;
if (!urlString.startsWith("/")) {
String absUrl;
// Relative to base, so back up to last slash
if (!baseURLStr.endsWith("/")) {
absUrl = baseURLStr.substring(0, baseURLStr.lastIndexOf('/') + 1) + urlString;
} else {
absUrl = baseURLStr + urlString;
}
try {
URL url = new URL(absUrl);
//Debug.warning("Return 4: " + url);
return url.toString();
} catch (Exception e) {
}
} else {
String absUrl = baseURLStr + urlString.substring(1);
try {
URL url = new URL(absUrl);
//Debug.warning("Return 5: " + url);
return url.toString();
} catch (Exception e) {
}
}
String absUrl = HTTP.getAbsoluteURL(baseURLStr, urlString);
@ -2326,4 +2311,22 @@ public class Device implements org.cybergarage.http.HTTPRequestListener,
* output(pr); pr.flush(); }
*/
/*
public static void main(String[] args) {
test("/foo/x", "http://aa:123/");
test("/foo/x", "http://aa:123/bar/");
test("/foo/x", "http://aa:123/bar/baz");
test("foo/x", "http://aa:123/");
test("foo/x", "http://aa:123/bar/");
test("foo/x", "http://aa:123/bar/baz");
}
private static void test(String a, String b) {
Device d = new Device();
String c = d.getAbsoluteURL(a, "", b);
System.out.println(b + ' ' + a + ' ' + c);
c = d.getAbsoluteURL(a, b, "");
System.out.println(b + ' ' + a + ' ' + c);
}
*/
}