Merge branch '1.5.x'

This commit is contained in:
Andy Wilkinson 2017-06-12 14:25:26 +01:00
commit 700af8ed00
4 changed files with 43 additions and 12 deletions

View File

@ -187,6 +187,15 @@ final class JarURLConnection extends java.net.JarURLConnection {
@Override
public int getContentLength() {
long longContentLength = getContentLengthLong();
if (longContentLength > Integer.MAX_VALUE) {
return -1;
}
return (int) longContentLength;
}
@Override
public long getContentLengthLong() {
if (this.jarFile == null) {
return -1;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -136,6 +136,20 @@ public class JarURLConnectionTests {
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
}
@Test
public void getContentLengthReturnsLengthOfUnderlyingEntry() throws Exception {
URL url = new URL(new URL("jar", null, -1,
"file:" + getAbsolutePath() + "!/nested.jar!/", new Handler()), "/3.dat");
assertThat(url.openConnection().getContentLength()).isEqualTo(1);
}
@Test
public void getContentLengthLongReturnsLengthOfUnderlyingEntry() throws Exception {
URL url = new URL(new URL("jar", null, -1,
"file:" + getAbsolutePath() + "!/nested.jar!/", new Handler()), "/3.dat");
assertThat(url.openConnection().getContentLengthLong()).isEqualTo(1);
}
private String getAbsolutePath() {
return this.rootJarFile.getAbsolutePath().replace('\\', '/');
}

View File

@ -48,7 +48,7 @@ class JarResourceManager implements ResourceManager {
public Resource getResource(String path) throws IOException {
URL url = new URL("jar:file:" + this.jarPath + "!"
+ (path.startsWith("/") ? path : "/" + path));
URLResource resource = new URLResource(url, url.openConnection(), path);
URLResource resource = new URLResource(url, path);
if (resource.getContentLength() < 0) {
return null;
}

View File

@ -21,7 +21,6 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStore;
@ -679,15 +678,9 @@ public class UndertowServletWebServerFactory extends AbstractServletWebServerFac
@Override
public Resource getResource(String path) {
for (URL url : this.metaInfResourceJarUrls) {
try {
URL resourceUrl = new URL(url + "META-INF/resources" + path);
URLConnection connection = resourceUrl.openConnection();
if (connection.getContentLength() >= 0) {
return new URLResource(resourceUrl, connection, path);
}
}
catch (IOException ex) {
// Continue
URLResource resource = getMetaInfResource(url, path);
if (resource != null) {
return resource;
}
}
return null;
@ -704,6 +697,21 @@ public class UndertowServletWebServerFactory extends AbstractServletWebServerFac
@Override
public void removeResourceChangeListener(ResourceChangeListener listener) {
}
private URLResource getMetaInfResource(URL resourceJar, String path) {
try {
URL resourceUrl = new URL(resourceJar + "META-INF/resources" + path);
URLResource resource = new URLResource(resourceUrl, path);
if (resource.getContentLength() < 0) {
return null;
}
return resource;
}
catch (MalformedURLException ex) {
return null;
}
}
}