Skip to content

Commit 4ebe334

Browse files
authored
add user-agent header to template downloader request (#12791)
1 parent 71bd26f commit 4ebe334

File tree

8 files changed

+63
-0
lines changed

8 files changed

+63
-0
lines changed

core/src/main/java/com/cloud/storage/template/HttpTemplateDownloader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.cloud.utils.Pair;
5353
import com.cloud.utils.UriUtils;
5454
import com.cloud.utils.exception.CloudRuntimeException;
55+
import com.cloud.utils.net.HttpClientCloudStackUserAgent;
5556
import com.cloud.utils.net.Proxy;
5657

5758
/**
@@ -125,6 +126,7 @@ private GetMethod createRequest(String downloadUrl) {
125126
GetMethod request = new GetMethod(downloadUrl);
126127
request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
127128
request.setFollowRedirects(followRedirects);
129+
request.getParams().setParameter(HttpMethodParams.USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
128130
return request;
129131
}
130132

core/src/main/java/com/cloud/storage/template/MetalinkTemplateDownloader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
//
1919
package com.cloud.storage.template;
2020

21+
2122
import com.cloud.storage.StorageLayer;
2223
import com.cloud.utils.UriUtils;
24+
import com.cloud.utils.net.HttpClientCloudStackUserAgent;
25+
2326
import org.apache.commons.httpclient.HttpClient;
2427
import org.apache.commons.httpclient.HttpMethod;
2528
import org.apache.commons.httpclient.HttpMethodRetryHandler;
@@ -59,6 +62,7 @@ protected GetMethod createRequest(String downloadUrl) {
5962
GetMethod request = new GetMethod(downloadUrl);
6063
request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
6164
request.setFollowRedirects(followRedirects);
65+
request.getParams().setParameter(HttpMethodParams.USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
6266
if (!toFileSet) {
6367
String[] parts = downloadUrl.split("/");
6468
String filename = parts[parts.length - 1];

core/src/main/java/com/cloud/storage/template/SimpleHttpMultiFileDownloader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.apache.commons.lang3.StringUtils;
4545

4646
import com.cloud.storage.StorageLayer;
47+
import com.cloud.utils.net.HttpClientCloudStackUserAgent;
4748

4849
public class SimpleHttpMultiFileDownloader extends ManagedContextRunnable implements TemplateDownloader {
4950
private static final MultiThreadedHttpConnectionManager s_httpClientManager = new MultiThreadedHttpConnectionManager();
@@ -95,6 +96,7 @@ private GetMethod createRequest(String downloadUrl) {
9596
GetMethod request = new GetMethod(downloadUrl);
9697
request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);
9798
request.setFollowRedirects(followRedirects);
99+
request.getParams().setParameter(HttpMethodParams.USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
98100
return request;
99101
}
100102

@@ -141,6 +143,7 @@ private void tryAndGetTotalRemoteSize() {
141143
continue;
142144
}
143145
HeadMethod headMethod = new HeadMethod(downloadUrl);
146+
headMethod.getParams().setParameter(HttpMethodParams.USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
144147
try {
145148
if (client.executeMethod(headMethod) != HttpStatus.SC_OK) {
146149
continue;

core/src/main/java/org/apache/cloudstack/direct/download/HttpDirectTemplateDownloader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.apache.cloudstack.direct.download;
2121

22+
2223
import java.io.File;
2324
import java.io.FileOutputStream;
2425
import java.io.IOException;
@@ -32,13 +33,15 @@
3233
import com.cloud.utils.Pair;
3334
import com.cloud.utils.UriUtils;
3435
import com.cloud.utils.exception.CloudRuntimeException;
36+
import com.cloud.utils.net.HttpClientCloudStackUserAgent;
3537
import com.cloud.utils.storage.QCOW2Utils;
3638
import org.apache.commons.collections.MapUtils;
3739
import org.apache.commons.httpclient.HttpClient;
3840
import org.apache.commons.httpclient.HttpStatus;
3941
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
4042
import org.apache.commons.httpclient.methods.GetMethod;
4143
import org.apache.commons.httpclient.methods.HeadMethod;
44+
import org.apache.commons.httpclient.params.HttpMethodParams;
4245
import org.apache.commons.io.IOUtils;
4346

4447
public class HttpDirectTemplateDownloader extends DirectTemplateDownloaderImpl {
@@ -68,6 +71,7 @@ public HttpDirectTemplateDownloader(String url, Long templateId, String destPool
6871
protected GetMethod createRequest(String downloadUrl, Map<String, String> headers) {
6972
GetMethod request = new GetMethod(downloadUrl);
7073
request.setFollowRedirects(this.isFollowRedirects());
74+
request.getParams().setParameter(HttpMethodParams.USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
7175
if (MapUtils.isNotEmpty(headers)) {
7276
for (String key : headers.keySet()) {
7377
request.setRequestHeader(key, headers.get(key));
@@ -111,6 +115,7 @@ protected Pair<Boolean, String> performDownload() {
111115
public boolean checkUrl(String url) {
112116
HeadMethod httpHead = new HeadMethod(url);
113117
httpHead.setFollowRedirects(this.isFollowRedirects());
118+
httpHead.getParams().setParameter(HttpMethodParams.USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
114119
try {
115120
int responseCode = client.executeMethod(httpHead);
116121
if (responseCode != HttpStatus.SC_OK) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package com.cloud.utils;
2121

22+
import static com.cloud.utils.UriUtils.USER_AGENT;
23+
2224
import org.apache.logging.log4j.Logger;
2325
import org.apache.logging.log4j.LogManager;
2426

@@ -33,6 +35,8 @@
3335
import java.net.URL;
3436
import java.util.Map;
3537

38+
import com.cloud.utils.net.HttpClientCloudStackUserAgent;
39+
3640
public class HttpUtils {
3741

3842
protected static Logger LOGGER = LogManager.getLogger(HttpUtils.class);
@@ -161,6 +165,7 @@ public static boolean downloadFileWithProgress(final String fileURL, final Strin
161165
try {
162166
URL url = new URL(fileURL);
163167
httpConn = (HttpURLConnection) url.openConnection();
168+
httpConn.setRequestProperty(USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
164169
int responseCode = httpConn.getResponseCode();
165170
if (responseCode == HttpURLConnection.HTTP_OK) {
166171
int contentLength = httpConn.getContentLength();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@
6767

6868
import com.cloud.utils.crypt.DBEncryptionUtil;
6969
import com.cloud.utils.exception.CloudRuntimeException;
70+
import com.cloud.utils.net.HttpClientCloudStackUserAgent;
7071
import com.google.common.collect.ImmutableMap;
7172
import com.google.common.collect.ImmutableSet;
7273

7374
public class UriUtils {
7475

7576
protected static Logger LOGGER = LogManager.getLogger(UriUtils.class);
77+
public static final String USER_AGENT = "User-Agent";
7678

7779
public static String formNfsUri(String host, String path) {
7880
try {
@@ -227,6 +229,7 @@ public static long getRemoteSize(String url, Boolean followRedirect) {
227229
URI uri = new URI(url);
228230
httpConn = (HttpURLConnection)uri.toURL().openConnection();
229231
httpConn.setRequestMethod(method);
232+
httpConn.setRequestProperty(USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
230233
httpConn.setConnectTimeout(2000);
231234
httpConn.setReadTimeout(5000);
232235
httpConn.setInstanceFollowRedirects(Boolean.TRUE.equals(followRedirect));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.utils.net;
21+
22+
import org.apache.logging.log4j.util.Strings;
23+
24+
public class HttpClientCloudStackUserAgent {
25+
public static final String CLOUDSTACK_USER_AGENT = buildUserAgent();
26+
27+
private static String buildUserAgent() {
28+
String version = HttpClientCloudStackUserAgent.class
29+
.getPackage()
30+
.getImplementationVersion();
31+
32+
if (Strings.isBlank(version)) {
33+
version = "unknown";
34+
}
35+
return "CloudStack-Agent/" + version + " (Apache CloudStack)";
36+
}
37+
38+
private HttpClientCloudStackUserAgent() {}
39+
}

utils/src/main/java/com/cloud/utils/storage/QCOW2Utils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import com.cloud.utils.NumbersUtil;
3737
import com.cloud.utils.UriUtils;
38+
import com.cloud.utils.net.HttpClientCloudStackUserAgent;
3839

3940
public final class QCOW2Utils {
4041
protected static Logger LOGGER = LogManager.getLogger(QCOW2Utils.class);
@@ -119,6 +120,7 @@ public static long getVirtualSizeFromUrl(String urlStr, boolean followRedirects)
119120
try {
120121
URI url = new URI(urlStr);
121122
httpConn = (HttpURLConnection)url.toURL().openConnection();
123+
httpConn.setRequestProperty(UriUtils.USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
122124
httpConn.setInstanceFollowRedirects(followRedirects);
123125
return getVirtualSize(httpConn.getInputStream(), UriUtils.isUrlForCompressedFile(urlStr));
124126
} catch (URISyntaxException e) {

0 commit comments

Comments
 (0)