Skip to content

Commit 493f2f6

Browse files
committed
Template: perform a HEAD request to check file size from a URL
Signed-off-by: Marc-Aurèle Brothier <m@brothier.org>
1 parent bd56044 commit 493f2f6

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

utils/src/main/java/com/cloud/utils/UriUtils.java

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
import java.util.ListIterator;
3636
import java.util.StringTokenizer;
3737

38-
import javax.net.ssl.HttpsURLConnection;
39-
4038
import org.apache.commons.httpclient.Credentials;
4139
import org.apache.commons.httpclient.HttpClient;
4240
import org.apache.commons.httpclient.HttpException;
@@ -201,40 +199,53 @@ private static List<NameValuePair> getUserDetails(String query) {
201199
return details;
202200
}
203201

202+
public static void main(String[] args) {
203+
long size = getRemoteSize("http://localhost:8000/systemvm/target/systemvm.zip");
204+
System.out.println(size);
205+
}
206+
204207
// Get the size of a file from URL response header.
205-
public static Long getRemoteSize(String url) {
206-
Long remoteSize = (long)0;
207-
HttpURLConnection httpConn = null;
208-
HttpsURLConnection httpsConn = null;
209-
try {
210-
URI uri = new URI(url);
211-
if (uri.getScheme().equalsIgnoreCase("http")) {
208+
public static long getRemoteSize(String url) {
209+
long remoteSize = 0L;
210+
final String[] methods = new String[]{"HEAD", "GET"};
211+
IllegalArgumentException exception = null;
212+
// Attempting first a HEAD request to avoid downloading the whole file. If
213+
// it fails (for example with S3 presigned URL), fallback on a standard GET
214+
// request.
215+
for (String method : methods) {
216+
HttpURLConnection httpConn = null;
217+
try {
218+
URI uri = new URI(url);
212219
httpConn = (HttpURLConnection)uri.toURL().openConnection();
213-
if (httpConn != null) {
214-
httpConn.setConnectTimeout(2000);
215-
httpConn.setReadTimeout(5000);
216-
String contentLength = httpConn.getHeaderField("content-length");
217-
if (contentLength != null) {
218-
remoteSize = Long.parseLong(contentLength);
220+
httpConn.setRequestMethod(method);
221+
httpConn.setConnectTimeout(2000);
222+
httpConn.setReadTimeout(5000);
223+
String contentLength = httpConn.getHeaderField("Content-Length");
224+
if (contentLength != null) {
225+
remoteSize = Long.parseLong(contentLength);
226+
} else if (method.equals("GET") && httpConn.getResponseCode() < 300) {
227+
// Calculate the content size based on the input stream content
228+
byte[] buf = new byte[1024];
229+
int length;
230+
while ((length = httpConn.getInputStream().read(buf, 0, buf.length)) != -1) {
231+
remoteSize += length;
219232
}
220-
httpConn.disconnect();
221233
}
222-
} else if (uri.getScheme().equalsIgnoreCase("https")) {
223-
httpsConn = (HttpsURLConnection)uri.toURL().openConnection();
224-
if (httpsConn != null) {
225-
String contentLength = httpsConn.getHeaderField("content-length");
226-
if (contentLength != null) {
227-
remoteSize = Long.parseLong(contentLength);
228-
}
229-
httpsConn.disconnect();
234+
return remoteSize;
235+
} catch (URISyntaxException e) {
236+
throw new IllegalArgumentException("Invalid URL " + url);
237+
} catch (IOException e) {
238+
exception = new IllegalArgumentException("Unable to establish connection with URL " + url);
239+
} finally {
240+
if (httpConn != null) {
241+
httpConn.disconnect();
230242
}
231243
}
232-
} catch (URISyntaxException e) {
233-
throw new IllegalArgumentException("Invalid URL " + url);
234-
} catch (IOException e) {
235-
throw new IllegalArgumentException("Unable to establish connection with URL " + url);
236244
}
237-
return remoteSize;
245+
if (exception != null) {
246+
throw exception;
247+
}
248+
return 0L;
238249
}
239250

240251
public static Pair<String, Integer> validateUrl(String url) throws IllegalArgumentException {

0 commit comments

Comments
 (0)