Infering the URL context from the browser's base URL, if any emul
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 01 Feb 2013 19:49:21 +0100
branchemul
changeset 645a947e379f161
parent 644 cfbd4eecb941
child 646 ffdc7659d8ff
Infering the URL context from the browser's base URL, if any
emul/mini/src/main/java/java/net/URL.java
vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java
     1.1 --- a/emul/mini/src/main/java/java/net/URL.java	Fri Feb 01 18:42:07 2013 +0100
     1.2 +++ b/emul/mini/src/main/java/java/net/URL.java	Fri Feb 01 19:49:21 2013 +0100
     1.3 @@ -506,6 +506,17 @@
     1.4      public URL(URL context, String spec, URLStreamHandler handler)
     1.5          throws MalformedURLException
     1.6      {
     1.7 +        this(findContext(context), spec, handler != null);
     1.8 +    }
     1.9 +    
    1.10 +    private URL(URL context, String spec, boolean ishandler)
    1.11 +    throws MalformedURLException {
    1.12 +        // Check for permission to specify a handler
    1.13 +        if (ishandler) {
    1.14 +            throw new SecurityException();
    1.15 +        }
    1.16 +        URLStreamHandler handler = null;
    1.17 +        
    1.18          String original = spec;
    1.19          int i, limit, c;
    1.20          int start = 0;
    1.21 @@ -513,10 +524,6 @@
    1.22          boolean aRef=false;
    1.23          boolean isRelative = false;
    1.24  
    1.25 -        // Check for permission to specify a handler
    1.26 -        if (handler != null) {
    1.27 -            throw new SecurityException();
    1.28 -        }
    1.29  
    1.30          try {
    1.31              limit = spec.length();
    1.32 @@ -1035,6 +1042,23 @@
    1.33          return universal;
    1.34      }
    1.35  
    1.36 +    private static URL findContext(URL context) throws MalformedURLException {
    1.37 +        if (context == null) {
    1.38 +            String base = findBaseURL();
    1.39 +            if (base != null) {
    1.40 +                context = new URL(null, base, false);
    1.41 +            }
    1.42 +        }
    1.43 +        return context;
    1.44 +    }
    1.45 +    
    1.46 +    @JavaScriptBody(args = {}, body = 
    1.47 +          "if (window && window.location && window.location.href) {\n"
    1.48 +        + "  return window.location.href;\n"
    1.49 +        + "}\n"
    1.50 +        + "return null;"
    1.51 +    )
    1.52 +    private static native String findBaseURL();
    1.53  }
    1.54  class Parts {
    1.55      String path, query, ref;
     2.1 --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java	Fri Feb 01 18:42:07 2013 +0100
     2.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java	Fri Feb 01 19:49:21 2013 +0100
     2.3 @@ -62,7 +62,7 @@
     2.4  
     2.5      @HttpResource(path = "/readAnEntry.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip")
     2.6      @BrwsrTest  public void canVmLoadResourceFromZip() throws IOException {
     2.7 -        Object res = loadVMResource("/my/main/file.txt", "http:/readAnEntry.jar");
     2.8 +        Object res = loadVMResource("/my/main/file.txt", "/readAnEntry.jar");
     2.9          assert res instanceof InputStream : "Got array of bytes: " + res;
    2.10          InputStream is = (InputStream)res;
    2.11