This is an automated email from the ASF dual-hosted git repository.

dahn pushed a commit to branch 4.22
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/4.22 by this push:
     new 4ebe3349b77 add user-agent header to template downloader request 
(#12791)
4ebe3349b77 is described below

commit 4ebe3349b77b0cfb11c21a1c25cc0fb331f4018b
Author: Manoj Kumar <[email protected]>
AuthorDate: Mon Mar 30 15:32:12 2026 +0530

    add user-agent header to template downloader request (#12791)
---
 .../storage/template/HttpTemplateDownloader.java   |  2 ++
 .../template/MetalinkTemplateDownloader.java       |  4 +++
 .../template/SimpleHttpMultiFileDownloader.java    |  3 ++
 .../download/HttpDirectTemplateDownloader.java     |  5 +++
 utils/src/main/java/com/cloud/utils/HttpUtils.java |  5 +++
 utils/src/main/java/com/cloud/utils/UriUtils.java  |  3 ++
 .../utils/net/HttpClientCloudStackUserAgent.java   | 39 ++++++++++++++++++++++
 .../java/com/cloud/utils/storage/QCOW2Utils.java   |  2 ++
 8 files changed, 63 insertions(+)

diff --git 
a/core/src/main/java/com/cloud/storage/template/HttpTemplateDownloader.java 
b/core/src/main/java/com/cloud/storage/template/HttpTemplateDownloader.java
index 6fe001de72c..71c329796d1 100755
--- a/core/src/main/java/com/cloud/storage/template/HttpTemplateDownloader.java
+++ b/core/src/main/java/com/cloud/storage/template/HttpTemplateDownloader.java
@@ -52,6 +52,7 @@ import com.cloud.storage.StorageLayer;
 import com.cloud.utils.Pair;
 import com.cloud.utils.UriUtils;
 import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.utils.net.HttpClientCloudStackUserAgent;
 import com.cloud.utils.net.Proxy;
 
 /**
@@ -125,6 +126,7 @@ public class HttpTemplateDownloader extends 
ManagedContextRunnable implements Te
         GetMethod request = new GetMethod(downloadUrl);
         request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
myretryhandler);
         request.setFollowRedirects(followRedirects);
+        request.getParams().setParameter(HttpMethodParams.USER_AGENT, 
HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
         return request;
     }
 
diff --git 
a/core/src/main/java/com/cloud/storage/template/MetalinkTemplateDownloader.java 
b/core/src/main/java/com/cloud/storage/template/MetalinkTemplateDownloader.java
index 95ed0d1e76d..0dad2564779 100644
--- 
a/core/src/main/java/com/cloud/storage/template/MetalinkTemplateDownloader.java
+++ 
b/core/src/main/java/com/cloud/storage/template/MetalinkTemplateDownloader.java
@@ -18,8 +18,11 @@
 //
 package com.cloud.storage.template;
 
+
 import com.cloud.storage.StorageLayer;
 import com.cloud.utils.UriUtils;
+import com.cloud.utils.net.HttpClientCloudStackUserAgent;
+
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpMethod;
 import org.apache.commons.httpclient.HttpMethodRetryHandler;
@@ -59,6 +62,7 @@ public class MetalinkTemplateDownloader extends 
TemplateDownloaderBase implement
         GetMethod request = new GetMethod(downloadUrl);
         request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
myretryhandler);
         request.setFollowRedirects(followRedirects);
+        request.getParams().setParameter(HttpMethodParams.USER_AGENT, 
HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
         if (!toFileSet) {
             String[] parts = downloadUrl.split("/");
             String filename = parts[parts.length - 1];
diff --git 
a/core/src/main/java/com/cloud/storage/template/SimpleHttpMultiFileDownloader.java
 
b/core/src/main/java/com/cloud/storage/template/SimpleHttpMultiFileDownloader.java
index 8719947cb4f..6608754073a 100644
--- 
a/core/src/main/java/com/cloud/storage/template/SimpleHttpMultiFileDownloader.java
+++ 
b/core/src/main/java/com/cloud/storage/template/SimpleHttpMultiFileDownloader.java
@@ -44,6 +44,7 @@ import org.apache.commons.httpclient.params.HttpMethodParams;
 import org.apache.commons.lang3.StringUtils;
 
 import com.cloud.storage.StorageLayer;
+import com.cloud.utils.net.HttpClientCloudStackUserAgent;
 
 public class SimpleHttpMultiFileDownloader extends ManagedContextRunnable 
implements TemplateDownloader {
     private static final MultiThreadedHttpConnectionManager 
s_httpClientManager = new MultiThreadedHttpConnectionManager();
@@ -95,6 +96,7 @@ public class SimpleHttpMultiFileDownloader extends 
ManagedContextRunnable implem
         GetMethod request = new GetMethod(downloadUrl);
         request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
retryHandler);
         request.setFollowRedirects(followRedirects);
+        request.getParams().setParameter(HttpMethodParams.USER_AGENT, 
HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
         return request;
     }
 
@@ -141,6 +143,7 @@ public class SimpleHttpMultiFileDownloader extends 
ManagedContextRunnable implem
                 continue;
             }
             HeadMethod headMethod = new HeadMethod(downloadUrl);
+            headMethod.getParams().setParameter(HttpMethodParams.USER_AGENT, 
HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
             try {
                 if (client.executeMethod(headMethod) != HttpStatus.SC_OK) {
                     continue;
diff --git 
a/core/src/main/java/org/apache/cloudstack/direct/download/HttpDirectTemplateDownloader.java
 
b/core/src/main/java/org/apache/cloudstack/direct/download/HttpDirectTemplateDownloader.java
index c4a802ecdbc..99b84bb645c 100644
--- 
a/core/src/main/java/org/apache/cloudstack/direct/download/HttpDirectTemplateDownloader.java
+++ 
b/core/src/main/java/org/apache/cloudstack/direct/download/HttpDirectTemplateDownloader.java
@@ -19,6 +19,7 @@
 
 package org.apache.cloudstack.direct.download;
 
+
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -32,6 +33,7 @@ import java.util.Map;
 import com.cloud.utils.Pair;
 import com.cloud.utils.UriUtils;
 import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.utils.net.HttpClientCloudStackUserAgent;
 import com.cloud.utils.storage.QCOW2Utils;
 import org.apache.commons.collections.MapUtils;
 import org.apache.commons.httpclient.HttpClient;
@@ -39,6 +41,7 @@ import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.HeadMethod;
+import org.apache.commons.httpclient.params.HttpMethodParams;
 import org.apache.commons.io.IOUtils;
 
 public class HttpDirectTemplateDownloader extends DirectTemplateDownloaderImpl 
{
@@ -68,6 +71,7 @@ public class HttpDirectTemplateDownloader extends 
DirectTemplateDownloaderImpl {
     protected GetMethod createRequest(String downloadUrl, Map<String, String> 
headers) {
         GetMethod request = new GetMethod(downloadUrl);
         request.setFollowRedirects(this.isFollowRedirects());
+        request.getParams().setParameter(HttpMethodParams.USER_AGENT, 
HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
         if (MapUtils.isNotEmpty(headers)) {
             for (String key : headers.keySet()) {
                 request.setRequestHeader(key, headers.get(key));
@@ -111,6 +115,7 @@ public class HttpDirectTemplateDownloader extends 
DirectTemplateDownloaderImpl {
     public boolean checkUrl(String url) {
         HeadMethod httpHead = new HeadMethod(url);
         httpHead.setFollowRedirects(this.isFollowRedirects());
+        httpHead.getParams().setParameter(HttpMethodParams.USER_AGENT, 
HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
         try {
             int responseCode = client.executeMethod(httpHead);
             if (responseCode != HttpStatus.SC_OK) {
diff --git a/utils/src/main/java/com/cloud/utils/HttpUtils.java 
b/utils/src/main/java/com/cloud/utils/HttpUtils.java
index 9f998efe099..b7d95f8133b 100644
--- a/utils/src/main/java/com/cloud/utils/HttpUtils.java
+++ b/utils/src/main/java/com/cloud/utils/HttpUtils.java
@@ -19,6 +19,8 @@
 
 package com.cloud.utils;
 
+import static com.cloud.utils.UriUtils.USER_AGENT;
+
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.LogManager;
 
@@ -33,6 +35,8 @@ import java.net.HttpURLConnection;
 import java.net.URL;
 import java.util.Map;
 
+import com.cloud.utils.net.HttpClientCloudStackUserAgent;
+
 public class HttpUtils {
 
     protected static Logger LOGGER = LogManager.getLogger(HttpUtils.class);
@@ -161,6 +165,7 @@ public class HttpUtils {
         try {
             URL url = new URL(fileURL);
             httpConn = (HttpURLConnection) url.openConnection();
+            httpConn.setRequestProperty(USER_AGENT, 
HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
             int responseCode = httpConn.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK) {
                 int contentLength = httpConn.getContentLength();
diff --git a/utils/src/main/java/com/cloud/utils/UriUtils.java 
b/utils/src/main/java/com/cloud/utils/UriUtils.java
index eba6f88201b..a4654091e0c 100644
--- a/utils/src/main/java/com/cloud/utils/UriUtils.java
+++ b/utils/src/main/java/com/cloud/utils/UriUtils.java
@@ -67,12 +67,14 @@ import org.w3c.dom.NodeList;
 
 import com.cloud.utils.crypt.DBEncryptionUtil;
 import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.utils.net.HttpClientCloudStackUserAgent;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 
 public class UriUtils {
 
     protected static Logger LOGGER = LogManager.getLogger(UriUtils.class);
+    public static final String USER_AGENT = "User-Agent";
 
     public static String formNfsUri(String host, String path) {
         try {
@@ -227,6 +229,7 @@ public class UriUtils {
                 URI uri = new URI(url);
                 httpConn = (HttpURLConnection)uri.toURL().openConnection();
                 httpConn.setRequestMethod(method);
+                httpConn.setRequestProperty(USER_AGENT, 
HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
                 httpConn.setConnectTimeout(2000);
                 httpConn.setReadTimeout(5000);
                 
httpConn.setInstanceFollowRedirects(Boolean.TRUE.equals(followRedirect));
diff --git 
a/utils/src/main/java/com/cloud/utils/net/HttpClientCloudStackUserAgent.java 
b/utils/src/main/java/com/cloud/utils/net/HttpClientCloudStackUserAgent.java
new file mode 100644
index 00000000000..991aa296380
--- /dev/null
+++ b/utils/src/main/java/com/cloud/utils/net/HttpClientCloudStackUserAgent.java
@@ -0,0 +1,39 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.utils.net;
+
+import org.apache.logging.log4j.util.Strings;
+
+public class HttpClientCloudStackUserAgent {
+    public static final String CLOUDSTACK_USER_AGENT = buildUserAgent();
+
+    private static String buildUserAgent() {
+        String version = HttpClientCloudStackUserAgent.class
+                .getPackage()
+                .getImplementationVersion();
+
+        if (Strings.isBlank(version)) {
+            version = "unknown";
+        }
+        return "CloudStack-Agent/" + version + " (Apache CloudStack)";
+    }
+
+    private HttpClientCloudStackUserAgent() {}
+}
diff --git a/utils/src/main/java/com/cloud/utils/storage/QCOW2Utils.java 
b/utils/src/main/java/com/cloud/utils/storage/QCOW2Utils.java
index 34d748b0708..a4a49825d1f 100644
--- a/utils/src/main/java/com/cloud/utils/storage/QCOW2Utils.java
+++ b/utils/src/main/java/com/cloud/utils/storage/QCOW2Utils.java
@@ -35,6 +35,7 @@ import org.apache.logging.log4j.LogManager;
 
 import com.cloud.utils.NumbersUtil;
 import com.cloud.utils.UriUtils;
+import com.cloud.utils.net.HttpClientCloudStackUserAgent;
 
 public final class QCOW2Utils {
     protected static Logger LOGGER = LogManager.getLogger(QCOW2Utils.class);
@@ -119,6 +120,7 @@ public final class QCOW2Utils {
         try {
             URI url = new URI(urlStr);
             httpConn = (HttpURLConnection)url.toURL().openConnection();
+            httpConn.setRequestProperty(UriUtils.USER_AGENT, 
HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
             httpConn.setInstanceFollowRedirects(followRedirects);
             return getVirtualSize(httpConn.getInputStream(), 
UriUtils.isUrlForCompressedFile(urlStr));
         } catch (URISyntaxException e) {

Reply via email to