diff --git a/bin/jmeter.properties b/bin/jmeter.properties
index dd4dd7eaacd..8820915bb25 100644
--- a/bin/jmeter.properties
+++ b/bin/jmeter.properties
@@ -378,8 +378,9 @@ remote_hosts=127.0.0.1
#httpclient.timeout=0
# 0 == no timeout
-# Set the http version (defaults to 1.1)
-#httpclient.version=1.1 (or use the parameter http.protocol.version)
+# Set the default HTTP version for HttpClient5 and Java samplers when HTTP Version is empty
+# Valid values are HTTP/1.1 and HTTP/2 (defaults to HTTP/1.1)
+#httpclient.version=HTTP/1.1
# Define characters per second > 0 to emulate slow connections
#httpclient.socket.http.cps=0
diff --git a/src/bom-thirdparty/build.gradle.kts b/src/bom-thirdparty/build.gradle.kts
index ffd0b4073f5..c31f64f9711 100644
--- a/src/bom-thirdparty/build.gradle.kts
+++ b/src/bom-thirdparty/build.gradle.kts
@@ -107,6 +107,7 @@ dependencies {
because("User might still rely on commons-text")
}
api("org.apache.httpcomponents.client5:httpclient5:5.5.1")
+ api("org.apache.httpcomponents.core5:httpcore5:5.3.4")
api("org.apache.httpcomponents:httpasyncclient:4.1.5")
api("org.apache.httpcomponents:httpclient:4.5.14")
api("org.apache.httpcomponents:httpcore-nio:4.4.16")
diff --git a/src/core/src/main/resources/org/apache/jmeter/resources/messages.properties b/src/core/src/main/resources/org/apache/jmeter/resources/messages.properties
index 3137d879e74..3ba59ea9f1f 100644
--- a/src/core/src/main/resources/org/apache/jmeter/resources/messages.properties
+++ b/src/core/src/main/resources/org/apache/jmeter/resources/messages.properties
@@ -473,6 +473,7 @@ html_assertion_title=HTML Assertion
html_extractor_title=CSS Selector Extractor
html_extractor_type=CSS Selector Extractor Implementation
http_implementation=Implementation:
+http_version=HTTP Version:
html_report=Generate HTML report
http_response_code=HTTP response code
http_url_rewriting_modifier_title=HTTP URL Re-writing Modifier
diff --git a/src/core/src/main/resources/org/apache/jmeter/resources/messages_de.properties b/src/core/src/main/resources/org/apache/jmeter/resources/messages_de.properties
index f1278670119..17ff43ab91f 100644
--- a/src/core/src/main/resources/org/apache/jmeter/resources/messages_de.properties
+++ b/src/core/src/main/resources/org/apache/jmeter/resources/messages_de.properties
@@ -16,6 +16,7 @@
#
about=Über Apache JMeter
+http_version=HTTP-Version\:
add=Hinzufügen
add_as_child=Als ein Kind hinzufügen
add_parameter=Variable hinzufügen
diff --git a/src/core/src/main/resources/org/apache/jmeter/resources/messages_es.properties b/src/core/src/main/resources/org/apache/jmeter/resources/messages_es.properties
index 8fe4fb5a53d..dcb50db6a35 100644
--- a/src/core/src/main/resources/org/apache/jmeter/resources/messages_es.properties
+++ b/src/core/src/main/resources/org/apache/jmeter/resources/messages_es.properties
@@ -273,6 +273,7 @@ html_assertion_file=Escribir el reporte JTidy en fichero
html_assertion_label=Aserción HTML
html_assertion_title=Aserción HTML
http_implementation=Implementación HTTP\:
+http_version=Versión HTTP\:
http_response_code=código de respuesta HTTP
http_url_rewriting_modifier_title=Modificador de re-escritura HTTP URL
http_user_parameter_modifier=Modificador de Parámetro de Usuario HTTP
diff --git a/src/core/src/main/resources/org/apache/jmeter/resources/messages_fr.properties b/src/core/src/main/resources/org/apache/jmeter/resources/messages_fr.properties
index 044d7ba8351..209f0a84645 100644
--- a/src/core/src/main/resources/org/apache/jmeter/resources/messages_fr.properties
+++ b/src/core/src/main/resources/org/apache/jmeter/resources/messages_fr.properties
@@ -467,6 +467,7 @@ html_assertion_title=Assertion HTML
html_extractor_title=Extracteur par Sélecteur CSS
html_extractor_type=Implémentation de l'extracteur Sélecteur CSS
http_implementation=Implémentation \:
+http_version=Version HTTP \:
html_report=Générer le rapport HTML
http_response_code=Code de réponse HTTP
http_url_rewriting_modifier_title=Transcripteur d'URL HTTP
diff --git a/src/core/src/main/resources/org/apache/jmeter/resources/messages_ja.properties b/src/core/src/main/resources/org/apache/jmeter/resources/messages_ja.properties
index 561489d9e29..6fe16f42798 100644
--- a/src/core/src/main/resources/org/apache/jmeter/resources/messages_ja.properties
+++ b/src/core/src/main/resources/org/apache/jmeter/resources/messages_ja.properties
@@ -176,6 +176,7 @@ grouping_store_first_only=各グループの最初のサンプラーだけ保存
header_manager_title=HTTP ヘッダマネージャ
headers_stored=ヘッダーマネージャに保存されているヘッダ
help=ヘルプ
+http_version=HTTP バージョン\:
http_response_code=HTTP応答コード
http_url_rewriting_modifier_title=HTTP URL-Rewriting 修飾子
http_user_parameter_modifier=HTTPユーザーパラメータの変更
diff --git a/src/core/src/main/resources/org/apache/jmeter/resources/messages_ko.properties b/src/core/src/main/resources/org/apache/jmeter/resources/messages_ko.properties
index 8b963980d24..1de63f00cd6 100644
--- a/src/core/src/main/resources/org/apache/jmeter/resources/messages_ko.properties
+++ b/src/core/src/main/resources/org/apache/jmeter/resources/messages_ko.properties
@@ -471,6 +471,7 @@ html_assertion_title=HTML Assertion
html_extractor_title=CSS Selector 추출기
html_extractor_type=CSS Selector 추출기 구현
http_implementation=구현\:
+http_version=HTTP 버전\:
html_report=HTML 보고서 생성
http_response_code=HTTP 응답 코드
http_url_rewriting_modifier_title=HTTP URL Re-writing Modifier
diff --git a/src/core/src/main/resources/org/apache/jmeter/resources/messages_no.properties b/src/core/src/main/resources/org/apache/jmeter/resources/messages_no.properties
index a6daacc3059..160b0288aa1 100644
--- a/src/core/src/main/resources/org/apache/jmeter/resources/messages_no.properties
+++ b/src/core/src/main/resources/org/apache/jmeter/resources/messages_no.properties
@@ -71,6 +71,7 @@ graph_results_deviation=Avvik
graph_results_title=Graf resultater
headers_stored=Headere lagret hos headermanager
help=Hjelp
+http_version=HTTP-versjon\:
infinite=Uendelig
interleave_control_title=Vekslende kontroller
iterator_num=Løkketeller\:
diff --git a/src/core/src/main/resources/org/apache/jmeter/resources/messages_pl.properties b/src/core/src/main/resources/org/apache/jmeter/resources/messages_pl.properties
index fb5aedd2f24..9d81a10d112 100644
--- a/src/core/src/main/resources/org/apache/jmeter/resources/messages_pl.properties
+++ b/src/core/src/main/resources/org/apache/jmeter/resources/messages_pl.properties
@@ -16,6 +16,7 @@
#
about=O programie Apache JMeter
+http_version=Wersja HTTP\:
action_check_message=Aktualnie jest przeprowadzany test, zatrzymaj albo wyłącz test, aby uruchomić to polecenie
action_check_title=Test uruchomiony
active_total_threads_tooltip=Uruchamianie wątków / całkowita liczba wątków do uruchomienia
diff --git a/src/core/src/main/resources/org/apache/jmeter/resources/messages_pt_BR.properties b/src/core/src/main/resources/org/apache/jmeter/resources/messages_pt_BR.properties
index e9960bf8719..e278975db23 100644
--- a/src/core/src/main/resources/org/apache/jmeter/resources/messages_pt_BR.properties
+++ b/src/core/src/main/resources/org/apache/jmeter/resources/messages_pt_BR.properties
@@ -261,6 +261,7 @@ html_assertion_file=Escrever relatório do JTidy em arquivo
html_assertion_label=Asserção HTML
html_assertion_title=Asserção HTML
http_implementation=Implementação\:
+http_version=Versão HTTP\:
http_response_code=Código da Resposta HTTP
http_url_rewriting_modifier_title=Modificador de Re-escrita de URL HTTP
http_user_parameter_modifier=Modificador de Parâmetros HTTP do Usuário
diff --git a/src/core/src/main/resources/org/apache/jmeter/resources/messages_tr.properties b/src/core/src/main/resources/org/apache/jmeter/resources/messages_tr.properties
index 352f8bd7e7a..a18b1fffbf4 100644
--- a/src/core/src/main/resources/org/apache/jmeter/resources/messages_tr.properties
+++ b/src/core/src/main/resources/org/apache/jmeter/resources/messages_tr.properties
@@ -246,6 +246,7 @@ html_assertion_file=JTidy raporunu dosyaya yaz
html_assertion_label=HTML Doğrulama
html_assertion_title=HTML Doğrulama
http_implementation=Uygulaması\:
+http_version=HTTP sürümü\:
http_response_code=HTTP cevap kodu
http_url_rewriting_modifier_title=HTTP URL Yeniden Yazma Niteleyicisi
http_user_parameter_modifier=HTTP Kullanıcı Parametresi Niteleyicisi
diff --git a/src/core/src/main/resources/org/apache/jmeter/resources/messages_zh_CN.properties b/src/core/src/main/resources/org/apache/jmeter/resources/messages_zh_CN.properties
index 1763f2be645..253e35f83ed 100644
--- a/src/core/src/main/resources/org/apache/jmeter/resources/messages_zh_CN.properties
+++ b/src/core/src/main/resources/org/apache/jmeter/resources/messages_zh_CN.properties
@@ -433,6 +433,7 @@ html_assertion_title=HTML断言
html_extractor_title=CSS/JQuery提取器
html_extractor_type=CSS 选择器提取器实现
http_implementation=实现:
+http_version=HTTP 版本:
http_response_code=HTTP响应代码
http_url_rewriting_modifier_title=HTTP URL 重写修饰符
http_user_parameter_modifier=HTTP 用户参数修饰符
diff --git a/src/core/src/main/resources/org/apache/jmeter/resources/messages_zh_TW.properties b/src/core/src/main/resources/org/apache/jmeter/resources/messages_zh_TW.properties
index 93107f3298c..49012192320 100644
--- a/src/core/src/main/resources/org/apache/jmeter/resources/messages_zh_TW.properties
+++ b/src/core/src/main/resources/org/apache/jmeter/resources/messages_zh_TW.properties
@@ -206,6 +206,7 @@ headers_stored=標頭管理員中儲存的標頭資料
help=輔助說明
html_assertion_label=HTML 驗證
html_assertion_title=HTML 驗證
+http_version=HTTP 版本:
http_response_code=HTTP 回應代碼
http_url_rewriting_modifier_title=HTTP URL 重導修飾詞
http_user_parameter_modifier=HTTP 使用者參數修飾詞
diff --git a/src/dist-check/src/test/java/org/apache/jmeter/junit/JMeterTest.java b/src/dist-check/src/test/java/org/apache/jmeter/junit/JMeterTest.java
index 2aca34f3e1e..0aff1b1a678 100644
--- a/src/dist-check/src/test/java/org/apache/jmeter/junit/JMeterTest.java
+++ b/src/dist-check/src/test/java/org/apache/jmeter/junit/JMeterTest.java
@@ -385,6 +385,7 @@ public void GUIComponents2(GuiComponentHolder componentHolder) throws Exception
// TODO: support expressions?
IGNORED_PROPERTIES.add(HTTPSamplerBaseSchema.INSTANCE.getIpSourceType());
IGNORED_PROPERTIES.add(HTTPSamplerBaseSchema.INSTANCE.getImplementation());
+ IGNORED_PROPERTIES.add(HTTPSamplerBaseSchema.INSTANCE.getHttpVersion());
// TODO: support expressions in UrlConfigGui
IGNORED_PROPERTIES.add(HTTPSamplerBaseSchema.INSTANCE.getFollowRedirects());
IGNORED_PROPERTIES.add(HTTPSamplerBaseSchema.INSTANCE.getAutoRedirects());
diff --git a/src/protocol/http/build.gradle.kts b/src/protocol/http/build.gradle.kts
index af6b3482f5d..800054537e7 100644
--- a/src/protocol/http/build.gradle.kts
+++ b/src/protocol/http/build.gradle.kts
@@ -61,6 +61,8 @@ dependencies {
exclude("com.google.code.findbugs", "jsr305")
}
implementation("dnsjava:dnsjava")
+ implementation("org.apache.httpcomponents.client5:httpclient5")
+ implementation("org.apache.httpcomponents.core5:httpcore5")
implementation("org.apache.httpcomponents:httpmime")
implementation("org.apache.httpcomponents:httpcore")
implementation("org.brotli:dec")
diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/config/gui/HttpDefaultsGui.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/config/gui/HttpDefaultsGui.java
index 8b49bcbb4ec..4fdd406acf4 100644
--- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/config/gui/HttpDefaultsGui.java
+++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/config/gui/HttpDefaultsGui.java
@@ -79,6 +79,7 @@ public class HttpDefaultsGui extends AbstractConfigGui {
private JTextField proxyUser;
private JPasswordField proxyPass;
private final JComboBox httpImplementation = new JComboBox<>(HTTPSamplerFactory.getImplementations());
+ private final JComboBox httpVersion = new JComboBox<>(new String[] {"HTTP/1.1", "HTTP/2", ""});
private JTextField connectTimeOut;
private JTextField responseTimeOut;
@@ -152,6 +153,7 @@ public void modifyTestElement(TestElement config) {
}
config.set(httpSchema.getImplementation(), String.valueOf(httpImplementation.getSelectedItem()));
+ config.set(httpSchema.getHttpVersion(), String.valueOf(httpVersion.getSelectedItem()));
}
@Override
@@ -169,6 +171,7 @@ public void configure(TestElement el) {
HTTPSamplerBaseSchema httpSchema = HTTPSamplerBaseSchema.INSTANCE;
sourceIpType.setSelectedIndex(samplerBase.get(httpSchema.getIpSourceType()));
httpImplementation.setSelectedItem(samplerBase.getString(httpSchema.getImplementation()));
+ httpVersion.setSelectedItem(samplerBase.getString(httpSchema.getHttpVersion()));
}
private void init() { // WARNING: called from ctor so must not be overridden (i.e. must be private or final)
@@ -329,6 +332,8 @@ protected final JPanel getImplementationPanel(){
implPanel.add(new JLabel(JMeterUtils.getResString("http_implementation"))); // $NON-NLS-1$
httpImplementation.addItem("");// $NON-NLS-1$
implPanel.add(httpImplementation);
+ implPanel.add(new JLabel(JMeterUtils.getResString("http_version"))); // $NON-NLS-1$
+ implPanel.add(httpVersion);
return implPanel;
}
diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/CacheManager.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/CacheManager.java
index 8852f6332e4..32509ab81f8 100644
--- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/CacheManager.java
+++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/CacheManager.java
@@ -245,6 +245,55 @@ public void saveDetails(HttpResponse method, HTTPSampleResult res) {
}
}
+ /**
+ * Save the Last-Modified, Etag, and Expires headers if the result is
+ * cacheable. Version for Apache HttpClient 5 implementation.
+ *
+ * @param response response to extract header information from
+ * @param res result to decide if result is cacheable
+ */
+ public void saveDetails(org.apache.hc.core5.http.ClassicHttpResponse response, HTTPSampleResult res) {
+ final String varyHeader = getHeader(response, HTTPConstants.VARY);
+ if (isCacheable(res, varyHeader)) {
+ String lastModified = getHeader(response, HTTPConstants.LAST_MODIFIED);
+ String expires = getHeader(response, HTTPConstants.EXPIRES);
+ String etag = getHeader(response, HTTPConstants.ETAG);
+ String cacheControl = getHeader(response, HTTPConstants.CACHE_CONTROL);
+ String date = getHeader(response, HTTPConstants.DATE);
+ if (anyNotBlank(lastModified, expires, etag, cacheControl)) {
+ setCache(lastModified, cacheControl, expires, etag,
+ res.getUrlAsString(), date, getVaryHeader(varyHeader, asHeaders(res.getRequestHeaders())));
+ }
+ }
+ }
+
+ /**
+ * Save the Last-Modified, Etag, and Expires headers if the result is
+ * cacheable. Version for Java HttpClient implementation.
+ *
+ * @param response response to extract header information from
+ * @param res result to decide if result is cacheable
+ */
+ public void saveDetails(java.net.http.HttpResponse> response, HTTPSampleResult res) {
+ final String varyHeader = response.headers().firstValue(HTTPConstants.VARY).orElse(null);
+ if (isCacheable(res, varyHeader)) {
+ String lastModified = response.headers().firstValue(HTTPConstants.LAST_MODIFIED).orElse(null);
+ String expires = response.headers().firstValue(HTTPConstants.EXPIRES).orElse(null);
+ String etag = response.headers().firstValue(HTTPConstants.ETAG).orElse(null);
+ String cacheControl = response.headers().firstValue(HTTPConstants.CACHE_CONTROL).orElse(null);
+ String date = response.headers().firstValue(HTTPConstants.DATE).orElse(null);
+ if (anyNotBlank(lastModified, expires, etag, cacheControl)) {
+ setCache(lastModified, cacheControl, expires, etag,
+ res.getUrlAsString(), date, getVaryHeader(varyHeader, asHeaders(res.getRequestHeaders())));
+ }
+ }
+ }
+
+ private static String getHeader(org.apache.hc.core5.http.HttpMessage message, String name) {
+ org.apache.hc.core5.http.Header header = message.getFirstHeader(name);
+ return header == null ? null : header.getValue();
+ }
+
// helper method to save the cache entry
private void setCache(String lastModified, String cacheControl, String expires,
String etag, String url, String date, Map.Entry varyHeader) {
@@ -410,6 +459,29 @@ public void setHeaders(URL url, HttpRequestBase request) {
}
}
+ /**
+ * Check the cache, and if there is a match, set conditional request headers.
+ *
+ * @param url URL to look up in cache
+ * @param request request where to set the headers
+ */
+ public void setHeaders(URL url, org.apache.hc.core5.http.ClassicHttpRequest request) {
+ CacheEntry entry = getEntry(url.toString(), asHeaders(request.getHeaders()));
+ if (log.isDebugEnabled()) {
+ log.debug("setHeaders for HTTP Method:{}(HC5) URL:{} Entry:{}", request.getMethod(), url, entry);
+ }
+ if (entry != null) {
+ String lastModified = entry.getLastModified();
+ if (lastModified != null) {
+ request.setHeader(HTTPConstants.IF_MODIFIED_SINCE, lastModified);
+ }
+ String etag = entry.getEtag();
+ if (etag != null) {
+ request.setHeader(HTTPConstants.IF_NONE_MATCH, etag);
+ }
+ }
+ }
+
/**
* Check the cache, and if there is a match, set the headers:
*
@@ -460,6 +532,17 @@ public boolean inCache(URL url, Header[] allHeaders) {
return entryStillValid(url, getEntry(url.toString(), allHeaders));
}
+ /**
+ * Check whether the URL has a valid entry for the supplied HttpClient 5 request headers.
+ *
+ * @param url URL to look up in cache
+ * @param allHeaders request headers
+ * @return {@code true} if the matching entry has not expired
+ */
+ public boolean inCache(URL url, org.apache.hc.core5.http.Header[] allHeaders) {
+ return entryStillValid(url, getEntry(url.toString(), asHeaders(allHeaders)));
+ }
+
public boolean inCache(URL url, org.apache.jmeter.protocol.http.control.Header[] allHeaders) {
return entryStillValid(url, getEntry(url.toString(), asHeaders(allHeaders)));
}
@@ -484,6 +567,14 @@ private static Header[] asHeaders(String allHeaders) {
return result.toArray(new Header[result.size()]);
}
+ private static Header[] asHeaders(org.apache.hc.core5.http.Header[] allHeaders) {
+ Header[] result = new Header[allHeaders.length];
+ for (int i = 0; i < allHeaders.length; i++) {
+ result[i] = new BasicHeader(allHeaders[i].getName(), allHeaders[i].getValue());
+ }
+ return result;
+ }
+
private static class HeaderAdapter implements Header {
private final org.apache.jmeter.protocol.http.control.Header delegate;
diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/gui/HttpTestSampleGui.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/gui/HttpTestSampleGui.java
index 77863a286bd..7b6150e3b37 100644
--- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/gui/HttpTestSampleGui.java
+++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/gui/HttpTestSampleGui.java
@@ -81,6 +81,7 @@ public class HttpTestSampleGui extends AbstractSamplerGui {
private JTextField proxyUser;
private JPasswordField proxyPass;
private final JComboBox httpImplementation = new JComboBox<>(HTTPSamplerFactory.getImplementations());
+ private final JComboBox httpVersion = new JComboBox<>(new String[] {"HTTP/1.1", "HTTP/2", ""});
private JTextField connectTimeOut;
private JTextField responseTimeOut;
@@ -135,6 +136,7 @@ public void configure(TestElement element) {
if (!isAJP) {
sourceIpType.setSelectedIndex(samplerBase.getIpSourceType());
httpImplementation.setSelectedItem(samplerBase.getString(httpSchema.getImplementation()));
+ httpVersion.setSelectedItem(samplerBase.getHttpVersion());
}
}
@@ -179,6 +181,9 @@ public void modifyTestElement(TestElement sampler) {
String selectedImplementation = String.valueOf(httpImplementation.getSelectedItem());
samplerBase.set(httpSchema.getImplementation(),
StringUtilities.isBlank(selectedImplementation) ? null : selectedImplementation);
+ String selectedHttpVersion = String.valueOf(httpVersion.getSelectedItem());
+ samplerBase.set(httpSchema.getHttpVersion(),
+ StringUtilities.isBlank(selectedHttpVersion) ? null : selectedHttpVersion);
}
}
@@ -349,6 +354,8 @@ protected final JPanel getImplementationPanel(){
implPanel.add(new JLabel(JMeterUtils.getResString("http_implementation"))); // $NON-NLS-1$
httpImplementation.addItem("");// $NON-NLS-1$
implPanel.add(httpImplementation);
+ implPanel.add(new JLabel(JMeterUtils.getResString("http_version"))); // $NON-NLS-1$
+ implPanel.add(httpVersion);
return implPanel;
}
diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC5Impl.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC5Impl.java
new file mode 100644
index 00000000000..afcb4642ba9
--- /dev/null
+++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC5Impl.java
@@ -0,0 +1,972 @@
+/*
+ * 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 org.apache.jmeter.protocol.http.sampler;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.URI;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.net.UnknownHostException;
+import java.nio.charset.Charset;
+import java.security.GeneralSecurityException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeoutException;
+
+import javax.net.ssl.SSLContext;
+
+import org.apache.hc.client5.http.HttpRoute;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
+import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
+import org.apache.hc.client5.http.auth.AuthScope;
+import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
+import org.apache.hc.client5.http.classic.ExecChainHandler;
+import org.apache.hc.client5.http.config.ConnectionConfig;
+import org.apache.hc.client5.http.config.RequestConfig;
+import org.apache.hc.client5.http.config.TlsConfig;
+import org.apache.hc.client5.http.entity.BrotliInputStreamFactory;
+import org.apache.hc.client5.http.entity.DecompressingEntity;
+import org.apache.hc.client5.http.entity.DeflateInputStreamFactory;
+import org.apache.hc.client5.http.entity.GZIPInputStreamFactory;
+import org.apache.hc.client5.http.entity.InputStreamFactory;
+import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
+import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
+import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
+import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
+import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
+import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
+import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
+import org.apache.hc.client5.http.impl.routing.DefaultRoutePlanner;
+import org.apache.hc.client5.http.io.ConnectionEndpoint;
+import org.apache.hc.client5.http.io.HttpClientConnectionManager;
+import org.apache.hc.client5.http.io.LeaseRequest;
+import org.apache.hc.client5.http.nio.AsyncClientConnectionManager;
+import org.apache.hc.client5.http.nio.AsyncConnectionEndpoint;
+import org.apache.hc.client5.http.protocol.HttpClientContext;
+import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
+import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
+import org.apache.hc.client5.http.ssl.TrustAllStrategy;
+import org.apache.hc.core5.concurrent.FutureCallback;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.HeaderElement;
+import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.HttpVersion;
+import org.apache.hc.core5.http.NameValuePair;
+import org.apache.hc.core5.http.config.Lookup;
+import org.apache.hc.core5.http.config.RegistryBuilder;
+import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.FileEntity;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
+import org.apache.hc.core5.http.message.BasicHeaderValueParser;
+import org.apache.hc.core5.http.message.BasicNameValuePair;
+import org.apache.hc.core5.http.message.ParserCursor;
+import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.http2.HttpVersionPolicy;
+import org.apache.hc.core5.io.CloseMode;
+import org.apache.hc.core5.reactor.ConnectionInitiator;
+import org.apache.hc.core5.ssl.SSLContexts;
+import org.apache.hc.core5.util.TimeValue;
+import org.apache.hc.core5.util.Timeout;
+import org.apache.jmeter.protocol.http.control.AuthManager;
+import org.apache.jmeter.protocol.http.control.Authorization;
+import org.apache.jmeter.protocol.http.control.CacheManager;
+import org.apache.jmeter.protocol.http.control.CookieManager;
+import org.apache.jmeter.protocol.http.control.DNSCacheManager;
+import org.apache.jmeter.protocol.http.control.HeaderManager;
+import org.apache.jmeter.protocol.http.util.HTTPArgument;
+import org.apache.jmeter.protocol.http.util.HTTPConstants;
+import org.apache.jmeter.protocol.http.util.HTTPFileArg;
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.services.FileServer;
+import org.apache.jmeter.testelement.property.CollectionProperty;
+import org.apache.jmeter.testelement.property.JMeterProperty;
+import org.apache.jmeter.threads.JMeterContextService;
+import org.apache.jmeter.threads.JMeterVariables;
+import org.apache.jmeter.util.JsseSSLManager;
+import org.apache.jmeter.util.SSLManager;
+import org.apache.jorphan.util.JOrphanUtils;
+import org.apache.jorphan.util.StringUtilities;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * HTTP Sampler using Apache HttpClient 5.x.
+ */
+public class HTTPHC5Impl extends HTTPHCAbstractImpl {
+
+ private static final Logger log = LoggerFactory.getLogger(HTTPHC5Impl.class);
+
+ /** Key used to store the current {@link SampleResult} in the {@link HttpClientContext}. */
+ static final String CONTEXT_ATTRIBUTE_SAMPLER_RESULT = "__jmeter.S_R__"; //$NON-NLS-1$
+
+ private static final ThreadLocal> HTTP_CLIENTS =
+ ThreadLocal.withInitial(HashMap::new);
+
+ private static final ThreadLocal> HTTP_2_CLIENTS =
+ ThreadLocal.withInitial(HashMap::new);
+
+ private static final String[] HEADERS_TO_SAVE = {HttpHeaders.CONTENT_LENGTH, HttpHeaders.CONTENT_ENCODING,
+ HttpHeaders.CONTENT_MD5};
+
+ private static final Lookup CONTENT_DECODERS = RegistryBuilder.create()
+ .register("br", BrotliInputStreamFactory.getInstance())
+ .register("gzip", GZIPInputStreamFactory.getInstance())
+ .register("x-gzip", GZIPInputStreamFactory.getInstance())
+ .register("deflate", DeflateInputStreamFactory.getInstance())
+ .build();
+
+ private static final TlsStrategy HTTP_2_TLS_STRATEGY = createHttp2TlsStrategy();
+
+ private static final ExecChainHandler RESPONSE_CONTENT_ENCODING = (request, scope, chain) -> {
+ HttpClientContext context = scope.clientContext;
+ RequestConfig requestConfig = context.getRequestConfig();
+ if (requestConfig == null) {
+ requestConfig = RequestConfig.DEFAULT;
+ }
+ ClassicHttpResponse response = chain.proceed(request, scope);
+ HttpEntity entity = response.getEntity();
+ if (!requestConfig.isContentCompressionEnabled() || entity == null || entity.getContentLength() == 0
+ || entity.getContentEncoding() == null) {
+ return response;
+ }
+
+ Header[][] headersToSave = new Header[HEADERS_TO_SAVE.length][];
+ for (int i = 0; i < HEADERS_TO_SAVE.length; i++) {
+ headersToSave[i] = response.getHeaders(HEADERS_TO_SAVE[i]);
+ }
+ String contentEncoding = entity.getContentEncoding();
+ HeaderElement[] codecs = BasicHeaderValueParser.INSTANCE.parseElements(contentEncoding,
+ new ParserCursor(0, contentEncoding.length()));
+ for (HeaderElement codec : codecs) {
+ InputStreamFactory decoderFactory = CONTENT_DECODERS.lookup(codec.getName().toLowerCase(Locale.ROOT));
+ if (decoderFactory != null) {
+ response.setEntity(new DecompressingEntity(response.getEntity(), decoderFactory));
+ response.removeHeaders(HttpHeaders.CONTENT_LENGTH);
+ response.removeHeaders(HttpHeaders.CONTENT_ENCODING);
+ response.removeHeaders(HttpHeaders.CONTENT_MD5);
+ }
+ }
+ for (Header[] headers : headersToSave) {
+ for (Header header : headers) {
+ if (!response.containsHeader(header.getName())) {
+ response.addHeader(header);
+ }
+ }
+ }
+ return response;
+ };
+
+ private volatile org.apache.hc.client5.http.classic.methods.HttpUriRequestBase currentRequest;
+
+ @SuppressWarnings("deprecation") // buildAsync is unavailable before HttpClient 5.5
+ private static TlsStrategy createHttp2TlsStrategy() {
+ try {
+ SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build();
+ return ClientTlsStrategyBuilder.create()
+ .setSslContext(sslContext)
+ .setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
+ .build();
+ } catch (GeneralSecurityException e) {
+ throw new IllegalStateException("Could not create HTTP/2 TLS strategy", e);
+ }
+ }
+
+ protected HTTPHC5Impl(HTTPSamplerBase testElement) {
+ super(testElement);
+ }
+
+ @Override
+ protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRedirect, int frameDepth) {
+ HTTPSampleResult result = createSampleResult(url, method);
+ org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request = null;
+ ClassicHttpResponse response = null;
+ try {
+ resetStateIfNeeded();
+ request = createRequest(url.toURI(), method);
+ setupRequest(url, request, result, areFollowingRedirect);
+ result.sampleStart();
+
+ CacheManager cacheManager = getCacheManager();
+ if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method)) {
+ if (cacheManager.inCache(url, request.getHeaders())) {
+ return updateSampleResultForResourceInCache(result);
+ }
+ }
+
+ currentRequest = request;
+ HttpClientKey clientKey = createHttpClientKey(url);
+ HttpClientContext context = createHttpClientContext(url, clientKey, request);
+ context.setAttribute(CONTEXT_ATTRIBUTE_SAMPLER_RESULT, result);
+ response = clientKey.httpVersionPolicy != HttpVersionPolicy.FORCE_HTTP_1
+ ? executeHttp2(getHttp2Client(clientKey), request, context)
+ : getClient(clientKey).executeOpen(null, request, context);
+ result.sampleEnd();
+ currentRequest = null;
+
+ updateResult(response, request, result);
+ if (cacheManager != null) {
+ cacheManager.saveDetails(response, result);
+ }
+ saveConnectionCookies(response, result.getURL(), getCookieManager());
+ return resultProcessing(areFollowingRedirect, frameDepth, result);
+ } catch (Exception e) {
+ if (result.getEndTime() == 0) {
+ result.sampleEnd();
+ }
+ if (request != null) {
+ result.setRequestHeaders(getRequestHeaders(request));
+ result.setSentBytes(calculateSentBytes(request));
+ }
+ return errorResult(e, result);
+ } finally {
+ JOrphanUtils.closeQuietly(response);
+ currentRequest = null;
+ }
+ }
+
+ private HTTPSampleResult createSampleResult(URL url, String method) {
+ HTTPSampleResult result = new HTTPSampleResult();
+ configureSampleLabel(result, url);
+ result.setHTTPMethod(method);
+ result.setURL(url);
+ return result;
+ }
+
+ private static org.apache.hc.client5.http.classic.methods.HttpUriRequestBase createRequest(URI uri, String method) {
+ return new org.apache.hc.client5.http.classic.methods.HttpUriRequestBase(method, uri);
+ }
+
+ private void setupRequest(URL url, org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request,
+ HTTPSampleResult result, boolean areFollowingRedirect) throws IOException {
+ HttpVersionPolicy httpVersionPolicy = getHttpVersionPolicy(testElement.getHttpVersion(), HTTP_VERSION);
+ RequestConfig.Builder config = RequestConfig.custom()
+ .setRedirectsEnabled(getAutoRedirects() && !areFollowingRedirect);
+ int responseTimeout = getResponseTimeout();
+ if (responseTimeout > 0) {
+ config.setResponseTimeout(Timeout.ofMilliseconds(responseTimeout));
+ }
+ request.setConfig(config.build());
+ if (httpVersionPolicy == HttpVersionPolicy.FORCE_HTTP_1) {
+ request.setHeader(HTTPConstants.HEADER_CONNECTION,
+ getUseKeepAlive() ? HTTPConstants.KEEP_ALIVE : HTTPConstants.CONNECTION_CLOSE);
+ } else if (httpVersionPolicy == HttpVersionPolicy.FORCE_HTTP_2) {
+ request.setVersion(HttpVersion.HTTP_2);
+ }
+ setConnectionHeaders(request, getHeaderManager(), httpVersionPolicy);
+ CacheManager cacheManager = getCacheManager();
+ if (cacheManager != null) {
+ cacheManager.setHeaders(url, request);
+ }
+
+ String cookies = setConnectionCookie(request, url, getCookieManager());
+ if (StringUtilities.isNotEmpty(cookies)) {
+ result.setCookies(cookies);
+ } else {
+ result.setCookies(getOnlyCookieFromHeaders(request));
+ }
+
+ if (canHaveBody(request.getMethod())) {
+ result.setQueryString(setupRequestEntity(request));
+ }
+ }
+
+ private static boolean canHaveBody(String method) {
+ return !HTTPConstants.HEAD.equals(method) && !HTTPConstants.TRACE.equals(method);
+ }
+
+ private String setupRequestEntity(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request) throws IOException {
+ HTTPFileArg[] files = getHTTPFiles();
+ String contentEncoding = getContentEncoding();
+ Charset charset = Charset.forName(contentEncoding);
+ HttpEntity entity;
+ String requestData;
+ if (getUseMultipart()) {
+ MultipartEntityBuilder builder = MultipartEntityBuilder.create().setCharset(charset);
+ for (JMeterProperty property : getArguments().getEnabledArguments()) {
+ HTTPArgument argument = (HTTPArgument) property.getObjectValue();
+ if (!argument.isSkippable(argument.getName())) {
+ ContentType contentType = StringUtilities.isNotEmpty(argument.getContentType())
+ ? ContentType.parse(argument.getContentType())
+ : ContentType.TEXT_PLAIN.withCharset(charset);
+ builder.addTextBody(argument.getName(), argument.getValue(), contentType);
+ }
+ }
+ for (HTTPFileArg file : files) {
+ File resolvedFile = FileServer.getFileServer().getResolvedFile(file.getPath());
+ ContentType contentType = StringUtilities.isNotEmpty(file.getMimeType())
+ ? ContentType.parse(file.getMimeType()) : ContentType.DEFAULT_BINARY;
+ builder.addBinaryBody(file.getParamName(), resolvedFile, contentType, file.getName());
+ }
+ entity = builder.build();
+ requestData = getEntityPreview(entity, contentEncoding);
+ } else if (!hasArguments() && getSendFileAsPostBody()) {
+ HTTPFileArg file = files[0];
+ if (request.getFirstHeader(HTTPConstants.HEADER_CONTENT_TYPE) == null && StringUtilities.isNotEmpty(file.getMimeType())) {
+ request.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, file.getMimeType());
+ }
+ entity = new FileEntity(FileServer.getFileServer().getResolvedFile(file.getPath()), null);
+ requestData = "";
+ } else if (getSendParameterValuesAsPostBody()) {
+ StringBuilder body = new StringBuilder();
+ for (JMeterProperty property : getArguments().getEnabledArguments()) {
+ body.append(((HTTPArgument) property.getObjectValue()).getEncodedValue(contentEncoding));
+ }
+ entity = new StringEntity(body.toString(), charset);
+ requestData = body.toString();
+ } else if (hasArguments()) {
+ entity = new UrlEncodedFormEntity(createNameValuePairs(contentEncoding), charset);
+ requestData = getEntityPreview(entity, contentEncoding);
+ } else {
+ return "";
+ }
+ request.setEntity(entity);
+ return requestData;
+ }
+
+ private List createNameValuePairs(String contentEncoding) throws IOException {
+ List pairs = new ArrayList<>();
+ for (JMeterProperty property : getArguments().getEnabledArguments()) {
+ HTTPArgument argument = (HTTPArgument) property.getObjectValue();
+ String name = argument.getName();
+ if (argument.isSkippable(name)) {
+ continue;
+ }
+ String value = argument.getValue();
+ if (!argument.isAlwaysEncoded()) {
+ name = URLDecoder.decode(name, contentEncoding);
+ value = URLDecoder.decode(value, contentEncoding);
+ }
+ pairs.add(new BasicNameValuePair(name, value));
+ }
+ return pairs;
+ }
+
+ private static String getEntityPreview(HttpEntity entity, String contentEncoding) throws IOException {
+ if (!entity.isRepeatable()) {
+ return "";
+ }
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ entity.writeTo(output);
+ return output.toString(contentEncoding);
+ }
+
+ private static void setConnectionHeaders(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request,
+ HeaderManager headerManager, HttpVersionPolicy httpVersionPolicy) {
+ if (headerManager == null) {
+ return;
+ }
+ CollectionProperty headers = headerManager.getHeaders();
+ if (headers == null) {
+ return;
+ }
+ for (JMeterProperty property : headers) {
+ org.apache.jmeter.protocol.http.control.Header header =
+ (org.apache.jmeter.protocol.http.control.Header) property.getObjectValue();
+ if (!HTTPConstants.HEADER_CONTENT_LENGTH.equalsIgnoreCase(header.getName())
+ && (httpVersionPolicy == HttpVersionPolicy.FORCE_HTTP_1
+ || !HTTPConstants.HEADER_CONNECTION.equalsIgnoreCase(header.getName()))) {
+ request.addHeader(header.getName(), header.getValue());
+ }
+ }
+ }
+
+ private static String setConnectionCookie(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request,
+ URL url, CookieManager cookieManager) {
+ if (cookieManager == null) {
+ return null;
+ }
+ String cookies = cookieManager.getCookieHeaderForURL(url);
+ if (cookies != null) {
+ request.setHeader(HTTPConstants.HEADER_COOKIE, cookies);
+ }
+ return cookies;
+ }
+
+ private HttpClientContext createHttpClientContext(URL url, HttpClientKey key,
+ org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request) {
+ HttpClientContext context = HttpClientContext.create();
+ BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+ configureTargetCredentials(url, request, credentialsProvider);
+ configureProxyCredentials(key, credentialsProvider);
+ context.setCredentialsProvider(credentialsProvider);
+ return context;
+ }
+
+ private void configureTargetCredentials(URL url,
+ org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request,
+ BasicCredentialsProvider credentialsProvider) {
+ AuthManager authManager = getAuthManager();
+ Authorization authorization = authManager == null ? null : authManager.getAuthForURL(url);
+ if (authorization == null) {
+ return;
+ }
+ credentialsProvider.setCredentials(new AuthScope(url.getHost(), getPort(url)),
+ new UsernamePasswordCredentials(authorization.getUser(), authorization.getPass().toCharArray()));
+ if (AuthManager.Mechanism.BASIC.equals(authorization.getMechanism())) {
+ request.setHeader(HttpHeaders.AUTHORIZATION, authorization.toBasicHeader());
+ }
+ }
+
+ private static void configureProxyCredentials(HttpClientKey key, BasicCredentialsProvider credentialsProvider) {
+ if (key.hasProxy && StringUtilities.isNotEmpty(key.proxyUser)) {
+ credentialsProvider.setCredentials(new AuthScope(key.proxyHost, key.proxyPort),
+ new UsernamePasswordCredentials(key.proxyUser, key.proxyPass.toCharArray()));
+ }
+ }
+
+ private static int getPort(URL url) {
+ return url.getPort() == -1 ? url.getDefaultPort() : url.getPort();
+ }
+
+ private void updateResult(ClassicHttpResponse response,
+ org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request, HTTPSampleResult result) throws IOException {
+ result.setRequestHeaders(getRequestHeaders(request));
+ result.setSentBytes(calculateSentBytes(request));
+ Header contentType = response.getFirstHeader(HTTPConstants.HEADER_CONTENT_TYPE);
+ if (contentType != null) {
+ result.setContentType(contentType.getValue());
+ result.setEncodingAndType(contentType.getValue());
+ }
+ HttpEntity entity = response.getEntity();
+ long bodySize = 0;
+ if (entity != null) {
+ byte[] body = readResponse(result, entity.getContent(), entity.getContentLength());
+ result.setResponseData(body);
+ bodySize = body.length;
+ }
+ int statusCode = response.getCode();
+ result.setResponseCode(Integer.toString(statusCode));
+ result.setResponseMessage(response.getReasonPhrase());
+ result.setSuccessful(isSuccessCode(statusCode));
+ result.setResponseHeaders(getResponseHeaders(response));
+ result.setHeadersSize(result.getResponseHeaders().length());
+ result.setBodySize(bodySize);
+ if (result.isRedirect()) {
+ Header location = response.getFirstHeader(HTTPConstants.HEADER_LOCATION);
+ if (location != null) {
+ result.setRedirectLocation(location.getValue());
+ }
+ }
+ }
+
+ private static String getResponseHeaders(ClassicHttpResponse response) {
+ StringBuilder headers = new StringBuilder();
+ headers.append(response.getVersion()).append(' ').append(response.getCode()).append(' ')
+ .append(response.getReasonPhrase()).append('\n');
+ for (Header header : response.getHeaders()) {
+ headers.append(header.getName()).append(": ").append(header.getValue()).append('\n');
+ }
+ return headers.toString();
+ }
+
+ private static String getRequestHeaders(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request) {
+ StringBuilder headers = new StringBuilder();
+ for (Header header : request.getHeaders()) {
+ if (ALL_EXCEPT_COOKIE.test(header.getName())) {
+ headers.append(header.getName()).append(": ").append(header.getValue()).append('\n');
+ }
+ }
+ return headers.toString();
+ }
+
+ private static long calculateSentBytes(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request) {
+ if (request == null) {
+ return 0;
+ }
+ long sentBytes = 0;
+
+ String method = request.getMethod();
+ String uri = request.getRequestUri();
+ if (uri == null) {
+ uri = "";
+ }
+ org.apache.hc.core5.http.ProtocolVersion version = request.getVersion();
+ String versionStr = version != null ? version.toString() : "HTTP/1.1";
+
+ sentBytes += method.getBytes(Charset.defaultCharset()).length;
+ sentBytes += 1;
+ sentBytes += uri.getBytes(Charset.defaultCharset()).length;
+ sentBytes += 1;
+ sentBytes += versionStr.getBytes(Charset.defaultCharset()).length;
+ sentBytes += 2;
+
+ for (Header header : request.getHeaders()) {
+ String name = header.getName();
+ String value = header.getValue();
+ if (name != null) {
+ sentBytes += name.getBytes(Charset.defaultCharset()).length;
+ sentBytes += 2;
+ }
+ if (value != null) {
+ sentBytes += value.getBytes(Charset.defaultCharset()).length;
+ }
+ sentBytes += 2;
+ }
+ sentBytes += 2;
+
+ HttpEntity entity = request.getEntity();
+ if (entity != null) {
+ long contentLength = entity.getContentLength();
+ if (contentLength >= 0) {
+ sentBytes += contentLength;
+ } else if (entity.isRepeatable()) {
+ CountingOutputStream counter = new CountingOutputStream();
+ try {
+ entity.writeTo(counter);
+ sentBytes += counter.getCount();
+ } catch (IOException e) {
+ log.debug("Exception measuring entity length", e);
+ }
+ }
+ }
+
+ return sentBytes;
+ }
+
+ private static class CountingOutputStream extends java.io.OutputStream {
+ private long count = 0;
+
+ @Override
+ public void write(int b) {
+ count++;
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len) {
+ count += len;
+ }
+
+ long getCount() {
+ return count;
+ }
+ }
+
+ private static String getOnlyCookieFromHeaders(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request) {
+ Header cookie = request.getFirstHeader(HTTPConstants.HEADER_COOKIE);
+ return cookie == null ? "" : cookie.getValue();
+ }
+
+ private static void saveConnectionCookies(ClassicHttpResponse response, URL url, CookieManager cookieManager) {
+ if (cookieManager == null) {
+ return;
+ }
+ for (Header header : response.getHeaders(HTTPConstants.HEADER_SET_COOKIE)) {
+ cookieManager.addCookieFromHeader(header.getValue(), url);
+ }
+ }
+
+ private static CloseableHttpClient getClient(HttpClientKey key) {
+ Map clients = HTTP_CLIENTS.get();
+ return clients.computeIfAbsent(key, HTTPHC5Impl::createClient);
+ }
+
+ private static CloseableHttpAsyncClient getHttp2Client(HttpClientKey key) {
+ Map clients = HTTP_2_CLIENTS.get();
+ return clients.computeIfAbsent(key, HTTPHC5Impl::createHttp2Client);
+ }
+
+ private static CloseableHttpClient createClient(HttpClientKey key) {
+ org.apache.hc.client5.http.impl.classic.HttpClientBuilder builder = HttpClients.custom().disableAutomaticRetries();
+ PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
+ connectionManagerBuilder.setDefaultTlsConfig(TlsConfig.custom()
+ .setVersionPolicy(key.httpVersionPolicy)
+ .build());
+ if (key.dnsCacheManager != null) {
+ connectionManagerBuilder.setDnsResolver(createDnsResolver(key.dnsCacheManager));
+ }
+ if (key.connectTimeout > 0) {
+ connectionManagerBuilder
+ .setDefaultConnectionConfig(ConnectionConfig.custom()
+ .setConnectTimeout(Timeout.ofMilliseconds(key.connectTimeout))
+ .build());
+ }
+ builder.setConnectionManager(new ConnectTimeMeasuringConnectionManager(connectionManagerBuilder.build()));
+ builder.setRoutePlanner(createRoutePlanner(key));
+ return builder.disableContentCompression()
+ .addExecInterceptorFirst("response-content-encoding", RESPONSE_CONTENT_ENCODING)
+ .build();
+ }
+
+ private static CloseableHttpAsyncClient createHttp2Client(HttpClientKey key) {
+ HttpAsyncClientBuilder builder = HttpAsyncClients.custom()
+ .disableAutomaticRetries()
+ .setRoutePlanner(createRoutePlanner(key));
+ PoolingAsyncClientConnectionManagerBuilder connectionManagerBuilder = PoolingAsyncClientConnectionManagerBuilder.create();
+ connectionManagerBuilder.setTlsStrategy(HTTP_2_TLS_STRATEGY);
+ connectionManagerBuilder.setDefaultTlsConfig(TlsConfig.custom()
+ .setVersionPolicy(key.httpVersionPolicy)
+ .build());
+ if (key.dnsCacheManager != null) {
+ connectionManagerBuilder.setDnsResolver(createDnsResolver(key.dnsCacheManager));
+ }
+ if (key.connectTimeout > 0) {
+ connectionManagerBuilder.setDefaultConnectionConfig(ConnectionConfig.custom()
+ .setConnectTimeout(Timeout.ofMilliseconds(key.connectTimeout))
+ .build());
+ }
+ builder.setConnectionManager(new ConnectTimeMeasuringAsyncConnectionManager(connectionManagerBuilder.build()));
+ CloseableHttpAsyncClient asyncClient = builder.build();
+ asyncClient.start();
+ return asyncClient;
+ }
+
+ @SuppressWarnings("deprecation") // SimpleHttpRequest.copy is required for HttpClient 5.3 compatibility
+ private static ClassicHttpResponse executeHttp2(CloseableHttpAsyncClient client,
+ org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request, HttpClientContext context)
+ throws IOException {
+ SimpleHttpRequest asyncRequest = SimpleHttpRequest.copy(request);
+ asyncRequest.setConfig(request.getConfig());
+ HttpEntity requestEntity = request.getEntity();
+ if (requestEntity != null) {
+ asyncRequest.setBody(EntityUtils.toByteArray(requestEntity),
+ requestEntity.getContentType() == null ? ContentType.DEFAULT_BINARY
+ : ContentType.parse(requestEntity.getContentType()));
+ }
+ Future responseFuture = client.execute(asyncRequest, context, null);
+ try {
+ return createClassicResponse(responseFuture.get(1, java.util.concurrent.TimeUnit.MINUTES));
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new IOException("Interrupted while executing HTTP/2 request", e);
+ } catch (TimeoutException e) {
+ responseFuture.cancel(true);
+ throw new IOException("Timed out while executing HTTP/2 request", e);
+ } catch (ExecutionException e) {
+ throw new IOException("Could not execute HTTP/2 request", e.getCause());
+ }
+ }
+
+ private static ClassicHttpResponse createClassicResponse(SimpleHttpResponse asyncResponse) {
+ BasicClassicHttpResponse response = new BasicClassicHttpResponse(asyncResponse.getCode(),
+ asyncResponse.getReasonPhrase());
+ response.setVersion(asyncResponse.getVersion());
+ for (Header header : asyncResponse.getHeaders()) {
+ response.addHeader(header);
+ }
+ byte[] responseBody = asyncResponse.getBodyBytes();
+ if (responseBody != null) {
+ response.setEntity(new ByteArrayEntity(responseBody, asyncResponse.getContentType()));
+ }
+ return response;
+ }
+
+ private static DefaultRoutePlanner createRoutePlanner(HttpClientKey key) {
+ return new DefaultRoutePlanner(null) {
+ @Override
+ protected HttpHost determineProxy(HttpHost target, org.apache.hc.core5.http.protocol.HttpContext context) {
+ return key.hasProxy ? new HttpHost(key.proxyScheme, key.proxyHost, key.proxyPort) : null;
+ }
+
+ @Override
+ protected InetAddress determineLocalAddress(HttpHost firstHop,
+ org.apache.hc.core5.http.protocol.HttpContext context) {
+ return key.localAddress;
+ }
+ };
+ }
+
+ private static org.apache.hc.client5.http.DnsResolver createDnsResolver(DNSCacheManager dnsCacheManager) {
+ return new org.apache.hc.client5.http.DnsResolver() {
+ @Override
+ public InetAddress[] resolve(String host) throws UnknownHostException {
+ return dnsCacheManager.resolve(host);
+ }
+
+ @Override
+ public String resolveCanonicalHostname(String host) throws UnknownHostException {
+ InetAddress[] addresses = resolve(host);
+ return addresses == null || addresses.length == 0 ? host : addresses[0].getCanonicalHostName();
+ }
+ };
+ }
+
+ private HttpClientKey createHttpClientKey(URL url) throws IOException {
+ String proxyScheme = getProxyScheme();
+ String proxyHost = getProxyHost();
+ int proxyPort = getProxyPortInt();
+ int connectTimeout = getConnectTimeout();
+ String proxyUser = getProxyUser();
+ String proxyPass = getProxyPass();
+ DNSCacheManager dnsCacheManager = testElement.getDNSResolver();
+ InetAddress localAddress = getIpSourceAddress();
+ boolean useDynamicProxy = isDynamicProxy(proxyHost, proxyPort);
+ boolean useStaticProxy = isStaticProxy(url.getHost());
+ HttpVersionPolicy httpVersionPolicy = getHttpVersionPolicy(testElement.getHttpVersion(), HTTP_VERSION);
+ if (!useDynamicProxy) {
+ proxyScheme = PROXY_SCHEME;
+ proxyHost = PROXY_HOST;
+ proxyPort = PROXY_PORT;
+ }
+ return new HttpClientKey(url.getProtocol(), url.getAuthority(), useDynamicProxy || useStaticProxy,
+ proxyScheme, proxyHost, proxyPort, proxyUser, proxyPass, connectTimeout, dnsCacheManager, localAddress,
+ httpVersionPolicy);
+ }
+
+ static HttpVersionPolicy getHttpVersionPolicy(String samplerHttpVersion, String defaultHttpVersion) {
+ String httpVersion = StringUtilities.isBlank(samplerHttpVersion) ? defaultHttpVersion : samplerHttpVersion;
+ return "HTTP/2".equals(httpVersion) || "2".equals(httpVersion)
+ ? HttpVersionPolicy.NEGOTIATE : HttpVersionPolicy.FORCE_HTTP_1;
+ }
+
+ @Override
+ protected void notifyFirstSampleAfterLoopRestart() {
+ JMeterVariables variables = JMeterContextService.getContext().getVariables();
+ resetStateOnThreadGroupIteration.set(variables != null && !variables.isSameUserOnNextIteration()
+ && RESET_STATE_ON_THREAD_GROUP_ITERATION);
+ }
+
+ private static void resetStateIfNeeded() {
+ if (resetStateOnThreadGroupIteration.get()) {
+ closeThreadLocalClients();
+ ((JsseSSLManager) SSLManager.getInstance()).resetContext();
+ resetStateOnThreadGroupIteration.set(false);
+ }
+ }
+
+ @Override
+ protected void threadFinished() {
+ closeThreadLocalClients();
+ }
+
+ private static void closeThreadLocalClients() {
+ Map clients = HTTP_CLIENTS.get();
+ for (CloseableHttpClient client : clients.values()) {
+ JOrphanUtils.closeQuietly(client);
+ }
+ clients.clear();
+ Map http2Clients = HTTP_2_CLIENTS.get();
+ for (CloseableHttpAsyncClient client : http2Clients.values()) {
+ JOrphanUtils.closeQuietly(client);
+ }
+ http2Clients.clear();
+ }
+
+ @Override
+ public boolean interrupt() {
+ org.apache.hc.client5.http.classic.methods.HttpUriRequestBase request = currentRequest;
+ if (request != null) {
+ currentRequest = null;
+ request.cancel();
+ }
+ return request != null;
+ }
+
+ private static void recordConnectEnd(HttpContext context) {
+ SampleResult sample = (SampleResult) context.getAttribute(CONTEXT_ATTRIBUTE_SAMPLER_RESULT);
+ if (sample != null) {
+ sample.connectEnd();
+ }
+ }
+
+ /**
+ * Delegating connection manager that records the connect time of the current sample
+ * as soon as the connection to the first hop has been established.
+ */
+ static final class ConnectTimeMeasuringConnectionManager implements HttpClientConnectionManager {
+
+ private final HttpClientConnectionManager delegate;
+
+ ConnectTimeMeasuringConnectionManager(HttpClientConnectionManager delegate) {
+ this.delegate = delegate;
+ }
+
+ @Override
+ public LeaseRequest lease(String id, HttpRoute route, Timeout requestTimeout, Object state) {
+ return delegate.lease(id, route, requestTimeout, state);
+ }
+
+ @Override
+ public void release(ConnectionEndpoint endpoint, Object newState, TimeValue validDuration) {
+ delegate.release(endpoint, newState, validDuration);
+ }
+
+ @Override
+ public void connect(ConnectionEndpoint endpoint, TimeValue connectTimeout, HttpContext context) throws IOException {
+ try {
+ delegate.connect(endpoint, connectTimeout, context);
+ } finally {
+ recordConnectEnd(context);
+ }
+ }
+
+ @Override
+ public void upgrade(ConnectionEndpoint endpoint, HttpContext context) throws IOException {
+ delegate.upgrade(endpoint, context);
+ }
+
+ @Override
+ public void close(CloseMode closeMode) {
+ delegate.close(closeMode);
+ }
+
+ @Override
+ public void close() throws IOException {
+ delegate.close();
+ }
+ }
+
+ /**
+ * Delegating asynchronous connection manager that records the connect time of the current
+ * sample as soon as the connection to the first hop has been established.
+ */
+ private static final class ConnectTimeMeasuringAsyncConnectionManager implements AsyncClientConnectionManager {
+
+ private final AsyncClientConnectionManager delegate;
+
+ private ConnectTimeMeasuringAsyncConnectionManager(AsyncClientConnectionManager delegate) {
+ this.delegate = delegate;
+ }
+
+ @Override
+ public Future lease(String id, HttpRoute route, Object state, Timeout requestTimeout,
+ FutureCallback callback) {
+ return delegate.lease(id, route, state, requestTimeout, callback);
+ }
+
+ @Override
+ public void release(AsyncConnectionEndpoint endpoint, Object newState, TimeValue validDuration) {
+ delegate.release(endpoint, newState, validDuration);
+ }
+
+ @Override
+ public Future connect(AsyncConnectionEndpoint endpoint,
+ ConnectionInitiator connectionInitiator, Timeout connectTimeout, Object attachment,
+ HttpContext context, FutureCallback callback) {
+ return delegate.connect(endpoint, connectionInitiator, connectTimeout, attachment, context,
+ new FutureCallback<>() {
+ @Override
+ public void completed(AsyncConnectionEndpoint result) {
+ recordConnectEnd(context);
+ if (callback != null) {
+ callback.completed(result);
+ }
+ }
+
+ @Override
+ public void failed(Exception ex) {
+ recordConnectEnd(context);
+ if (callback != null) {
+ callback.failed(ex);
+ }
+ }
+
+ @Override
+ public void cancelled() {
+ recordConnectEnd(context);
+ if (callback != null) {
+ callback.cancelled();
+ }
+ }
+ });
+ }
+
+ @Override
+ public void upgrade(AsyncConnectionEndpoint endpoint, Object attachment, HttpContext context) {
+ delegate.upgrade(endpoint, attachment, context);
+ }
+
+ @Override
+ public void upgrade(AsyncConnectionEndpoint endpoint, Object attachment, HttpContext context,
+ FutureCallback callback) {
+ delegate.upgrade(endpoint, attachment, context, callback);
+ }
+
+ @Override
+ public void close(CloseMode closeMode) {
+ delegate.close(closeMode);
+ }
+
+ @Override
+ public void close() throws IOException {
+ delegate.close();
+ }
+ }
+
+ private static final class HttpClientKey {
+ private final String protocol;
+ private final String authority;
+ private final boolean hasProxy;
+ private final String proxyScheme;
+ private final String proxyHost;
+ private final int proxyPort;
+ private final String proxyUser;
+ private final String proxyPass;
+ private final int connectTimeout;
+ private final DNSCacheManager dnsCacheManager;
+ private final InetAddress localAddress;
+ private final HttpVersionPolicy httpVersionPolicy;
+
+ private HttpClientKey(String protocol, String authority, boolean hasProxy, String proxyScheme,
+ String proxyHost, int proxyPort, String proxyUser, String proxyPass, int connectTimeout, DNSCacheManager dnsCacheManager,
+ InetAddress localAddress, HttpVersionPolicy httpVersionPolicy) {
+ this.protocol = protocol;
+ this.authority = authority;
+ this.hasProxy = hasProxy;
+ this.proxyScheme = proxyScheme;
+ this.proxyHost = proxyHost;
+ this.proxyPort = proxyPort;
+ this.proxyUser = proxyUser;
+ this.proxyPass = proxyPass;
+ this.connectTimeout = connectTimeout;
+ this.dnsCacheManager = dnsCacheManager;
+ this.localAddress = localAddress;
+ this.httpVersionPolicy = httpVersionPolicy;
+ }
+
+ @Override
+ public boolean equals(Object object) {
+ if (this == object) {
+ return true;
+ }
+ if (!(object instanceof HttpClientKey other)) {
+ return false;
+ }
+ return hasProxy == other.hasProxy && proxyPort == other.proxyPort && connectTimeout == other.connectTimeout
+ && Objects.equals(protocol, other.protocol) && Objects.equals(authority, other.authority)
+ && Objects.equals(proxyScheme, other.proxyScheme) && Objects.equals(proxyHost, other.proxyHost)
+ && Objects.equals(proxyUser, other.proxyUser) && Objects.equals(proxyPass, other.proxyPass)
+ && Objects.equals(dnsCacheManager, other.dnsCacheManager) && Objects.equals(localAddress, other.localAddress)
+ && httpVersionPolicy == other.httpVersionPolicy;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(protocol, authority, hasProxy, proxyScheme, proxyHost, proxyPort, proxyUser, proxyPass, connectTimeout, dnsCacheManager,
+ localAddress, httpVersionPolicy);
+ }
+ }
+}
diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPJavaImpl.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPJavaImpl.java
index 527ed485aad..c60bf4c1fec 100644
--- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPJavaImpl.java
+++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPJavaImpl.java
@@ -17,22 +17,58 @@
package org.apache.jmeter.protocol.http.sampler;
+import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
+import java.net.Authenticator;
import java.net.BindException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
+import java.net.PasswordAuthentication;
import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.zip.GZIPInputStream;
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLContextSpi;
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLEngineResult;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLParameters;
+import javax.net.ssl.SSLServerSocketFactory;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.SSLSessionContext;
+import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.TrustManager;
+
import org.apache.jmeter.protocol.http.control.AuthManager;
import org.apache.jmeter.protocol.http.control.Authorization;
import org.apache.jmeter.protocol.http.control.CacheManager;
@@ -44,6 +80,7 @@
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.util.JMeterUtils;
+import org.apache.jmeter.util.JsseSSLManager;
import org.apache.jmeter.util.SSLManager;
import org.apache.jorphan.io.CountingInputStream;
import org.apache.jorphan.util.StringUtilities;
@@ -59,8 +96,112 @@ public class HTTPJavaImpl extends HTTPAbstractImpl {
private static final boolean OBEY_CONTENT_LENGTH =
JMeterUtils.getPropDefault("httpsampler.obey_contentlength", false); // $NON-NLS-1$
+ private static final String DEFAULT_HTTP_VERSION =
+ JMeterUtils.getPropDefault("httpclient.version", HTTPConstants.HTTP_1_1); // $NON-NLS-1$
+
+ private static final ThreadLocal> HTTP_2_CLIENTS =
+ ThreadLocal.withInitial(HashMap::new);
+
+ /**
+ * Shared daemon thread pool used by the HTTP/2 {@link HttpClient} instances. It allows JMeter to observe
+ * when a connection has been established, since the client only submits tasks once the TCP connection is up.
+ */
+ private static final ExecutorService HTTP_2_EXECUTOR =
+ Executors.newCachedThreadPool(new Http2ThreadFactory());
+
private static final Logger log = LoggerFactory.getLogger(HTTPJavaImpl.class);
+ /**
+ * HTTP/2 does not transmit a reason phrase (see RFC 9113, section 8.3.2), so the response message is
+ * derived from the status code. The phrases are the ones registered in the IANA HTTP status code registry.
+ */
+ private static final Map HTTP_REASON_PHRASES = createReasonPhrases();
+
+ private static Map createReasonPhrases() {
+ Map phrases = new HashMap<>();
+ phrases.put(100, "Continue"); // $NON-NLS-1$
+ phrases.put(101, "Switching Protocols"); // $NON-NLS-1$
+ phrases.put(102, "Processing"); // $NON-NLS-1$
+ phrases.put(103, "Early Hints"); // $NON-NLS-1$
+ phrases.put(200, "OK"); // $NON-NLS-1$
+ phrases.put(201, "Created"); // $NON-NLS-1$
+ phrases.put(202, "Accepted"); // $NON-NLS-1$
+ phrases.put(203, "Non-Authoritative Information"); // $NON-NLS-1$
+ phrases.put(204, "No Content"); // $NON-NLS-1$
+ phrases.put(205, "Reset Content"); // $NON-NLS-1$
+ phrases.put(206, "Partial Content"); // $NON-NLS-1$
+ phrases.put(207, "Multi-Status"); // $NON-NLS-1$
+ phrases.put(208, "Already Reported"); // $NON-NLS-1$
+ phrases.put(226, "IM Used"); // $NON-NLS-1$
+ phrases.put(300, "Multiple Choices"); // $NON-NLS-1$
+ phrases.put(301, "Moved Permanently"); // $NON-NLS-1$
+ phrases.put(302, "Found"); // $NON-NLS-1$
+ phrases.put(303, "See Other"); // $NON-NLS-1$
+ phrases.put(304, "Not Modified"); // $NON-NLS-1$
+ phrases.put(305, "Use Proxy"); // $NON-NLS-1$
+ phrases.put(307, "Temporary Redirect"); // $NON-NLS-1$
+ phrases.put(308, "Permanent Redirect"); // $NON-NLS-1$
+ phrases.put(400, "Bad Request"); // $NON-NLS-1$
+ phrases.put(401, "Unauthorized"); // $NON-NLS-1$
+ phrases.put(402, "Payment Required"); // $NON-NLS-1$
+ phrases.put(403, "Forbidden"); // $NON-NLS-1$
+ phrases.put(404, "Not Found"); // $NON-NLS-1$
+ phrases.put(405, "Method Not Allowed"); // $NON-NLS-1$
+ phrases.put(406, "Not Acceptable"); // $NON-NLS-1$
+ phrases.put(407, "Proxy Authentication Required"); // $NON-NLS-1$
+ phrases.put(408, "Request Timeout"); // $NON-NLS-1$
+ phrases.put(409, "Conflict"); // $NON-NLS-1$
+ phrases.put(410, "Gone"); // $NON-NLS-1$
+ phrases.put(411, "Length Required"); // $NON-NLS-1$
+ phrases.put(412, "Precondition Failed"); // $NON-NLS-1$
+ phrases.put(413, "Content Too Large"); // $NON-NLS-1$
+ phrases.put(414, "URI Too Long"); // $NON-NLS-1$
+ phrases.put(415, "Unsupported Media Type"); // $NON-NLS-1$
+ phrases.put(416, "Range Not Satisfiable"); // $NON-NLS-1$
+ phrases.put(417, "Expectation Failed"); // $NON-NLS-1$
+ phrases.put(421, "Misdirected Request"); // $NON-NLS-1$
+ phrases.put(422, "Unprocessable Content"); // $NON-NLS-1$
+ phrases.put(423, "Locked"); // $NON-NLS-1$
+ phrases.put(424, "Failed Dependency"); // $NON-NLS-1$
+ phrases.put(425, "Too Early"); // $NON-NLS-1$
+ phrases.put(426, "Upgrade Required"); // $NON-NLS-1$
+ phrases.put(428, "Precondition Required"); // $NON-NLS-1$
+ phrases.put(429, "Too Many Requests"); // $NON-NLS-1$
+ phrases.put(431, "Request Header Fields Too Large"); // $NON-NLS-1$
+ phrases.put(451, "Unavailable For Legal Reasons"); // $NON-NLS-1$
+ phrases.put(500, "Internal Server Error"); // $NON-NLS-1$
+ phrases.put(501, "Not Implemented"); // $NON-NLS-1$
+ phrases.put(502, "Bad Gateway"); // $NON-NLS-1$
+ phrases.put(503, "Service Unavailable"); // $NON-NLS-1$
+ phrases.put(504, "Gateway Timeout"); // $NON-NLS-1$
+ phrases.put(505, "HTTP Version Not Supported"); // $NON-NLS-1$
+ phrases.put(506, "Variant Also Negotiates"); // $NON-NLS-1$
+ phrases.put(507, "Insufficient Storage"); // $NON-NLS-1$
+ phrases.put(508, "Loop Detected"); // $NON-NLS-1$
+ phrases.put(510, "Not Extended"); // $NON-NLS-1$
+ phrases.put(511, "Network Authentication Required"); // $NON-NLS-1$
+ return Collections.unmodifiableMap(phrases);
+ }
+
+ /**
+ * Returns the reason phrase belonging to the given HTTP status code.
+ *
+ * @param statusCode the HTTP status code
+ * @return the registered reason phrase, or an empty string if the status code is unknown
+ */
+ static String getReasonPhrase(int statusCode) {
+ return HTTP_REASON_PHRASES.getOrDefault(statusCode, ""); // $NON-NLS-1$
+ }
+
+ static boolean isHttp2(String samplerHttpVersion, String defaultHttpVersion) {
+ String httpVersion = StringUtilities.isBlank(samplerHttpVersion) ? defaultHttpVersion : samplerHttpVersion;
+ return "HTTP/2".equalsIgnoreCase(httpVersion) || "2".equalsIgnoreCase(httpVersion); // $NON-NLS-1$ $NON-NLS-2$
+ }
+
+ private boolean isHttp2() {
+ return isHttp2(testElement.getHttpVersion(), DEFAULT_HTTP_VERSION);
+ }
+
private static final int MAX_CONN_RETRIES =
JMeterUtils.getPropDefault("http.java.sampler.retries" // $NON-NLS-1$
,0); // Maximum connection retries
@@ -191,10 +332,10 @@ protected HttpURLConnection setupConnection(URL u, String method, HTTPSampleResu
}
conn.setRequestMethod(method);
- setConnectionHeaders(conn, u, getHeaderManager(), getCacheManager());
+ Map securityHeaders = setConnectionHeaders(conn, u, getHeaderManager(), getCacheManager());
String cookies = setConnectionCookie(conn, u, getCookieManager());
- Map securityHeaders = setConnectionAuthorization(conn, u, getAuthManager());
+ setConnectionAuthorization(conn, u, getAuthManager(), securityHeaders);
if (method.equals(HTTPConstants.POST)) {
setPostHeaders(conn);
@@ -348,6 +489,11 @@ private static String setConnectionCookie(HttpURLConnection conn, URL u, CookieM
return cookieHeader;
}
+ private static boolean isSecurityHeader(String name) {
+ return name != null && (HTTPConstants.HEADER_AUTHORIZATION.equalsIgnoreCase(name)
+ || "Proxy-Authorization".equalsIgnoreCase(name));
+ }
+
/**
* Extracts all the required headers for that particular URL request and
* sets them in the HttpURLConnection passed in
@@ -361,11 +507,13 @@ private static String setConnectionCookie(HttpURLConnection conn, URL u, CookieM
* the HeaderManager containing all the cookies
* for this UrlConfig
* @param cacheManager the CacheManager (may be null)
+ * @return Map of security headers set from HeaderManager
*/
- private static void setConnectionHeaders(HttpURLConnection conn, URL u,
+ private static Map setConnectionHeaders(HttpURLConnection conn, URL u,
HeaderManager headerManager, CacheManager cacheManager) {
// Add all the headers from the HeaderManager
Header[] arrayOfHeaders = null;
+ Map securityHeaders = new LinkedHashMap<>();
if (headerManager != null) {
CollectionProperty headers = headerManager.getHeaders();
if (headers != null) {
@@ -377,12 +525,16 @@ private static void setConnectionHeaders(HttpURLConnection conn, URL u,
String v = header.getValue();
arrayOfHeaders[i++] = header;
conn.addRequestProperty(n, v);
+ if (isSecurityHeader(n)) {
+ securityHeaders.put(n, v);
+ }
}
}
}
if (cacheManager != null){
cacheManager.setHeaders(conn, arrayOfHeaders, u);
}
+ return securityHeaders;
}
/**
@@ -445,8 +597,10 @@ private static String getFromConnectionHeaders(HttpURLConnection conn, Map entry : securityHeaders.entrySet()) {
- hdrs.append(entry.getKey()).append(": ") // $NON-NLS-1$
- .append(entry.getValue()).append("\n"); // $NON-NLS-1$
+ if (!hasHeader(requestHeaders, entry.getKey())) {
+ hdrs.append(entry.getKey()).append(": ") // $NON-NLS-1$
+ .append(entry.getValue()).append("\n"); // $NON-NLS-1$
+ }
}
}
return hdrs.toString();
@@ -464,9 +618,10 @@ private static String getFromConnectionHeaders(HttpURLConnection conn, MapAuthManager containing all the cookies for
* this UrlConfig
- * @return String Authorization header value or null if not set
+ * @param securityHeaders
+ * Map to collect security headers
*/
- private static Map setConnectionAuthorization(HttpURLConnection conn, URL u, AuthManager authManager) {
+ private static void setConnectionAuthorization(HttpURLConnection conn, URL u, AuthManager authManager, Map securityHeaders) {
if (authManager != null) {
Authorization auth = authManager.getAuthForURL(u);
if (auth != null) {
@@ -474,10 +629,9 @@ private static Map setConnectionAuthorization(HttpURLConnection
conn.setRequestProperty(HTTPConstants.HEADER_AUTHORIZATION, headerValue);
// Java hides request properties so we have to
// keep trace of it
- return Collections.singletonMap(HTTPConstants.HEADER_AUTHORIZATION, headerValue);
+ securityHeaders.put(HTTPConstants.HEADER_AUTHORIZATION, headerValue);
}
}
- return Collections.emptyMap();
}
/**
@@ -501,6 +655,9 @@ private static Map setConnectionAuthorization(HttpURLConnection
*/
@Override
protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRedirect, int frameDepth) {
+ if (isHttp2()) {
+ return sampleHttp2(url, method, areFollowingRedirect, frameDepth);
+ }
HttpURLConnection conn = null;
String urlStr = url.toString();
@@ -524,6 +681,10 @@ protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRe
}
}
+ Map> requestHeaders = null;
+ Map securityHeaders = Collections.emptyMap();
+ byte[] postBodyBytes = null;
+
try {
// Sampling proper - establish the connection and read the response:
// Repeatedly try to connect:
@@ -532,9 +693,13 @@ protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRe
for (; retry < MAX_CONN_RETRIES; retry++) {
try {
conn = setupConnection(url, method, res);
+ requestHeaders = new LinkedHashMap<>(conn.getRequestProperties());
+ securityHeaders = setConnectionHeaders(conn, url, getHeaderManager(), getCacheManager());
+ setConnectionAuthorization(conn, url, getAuthManager(), securityHeaders);
// Attempt the connection:
savedConn = conn;
conn.connect();
+ res.connectEnd();
break;
} catch (BindException e) {
if (retry >= MAX_CONN_RETRIES) {
@@ -560,9 +725,15 @@ protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRe
if (method.equals(HTTPConstants.POST)) {
String postBody = sendPostData(conn);
res.setQueryString(postBody);
+ if (postBody != null) {
+ postBodyBytes = getBytes(postBody);
+ }
} else if (method.equals(HTTPConstants.PUT)) {
String putBody = sendPutData(conn);
res.setQueryString(putBody);
+ if (putBody != null) {
+ postBodyBytes = getBytes(putBody);
+ }
}
// Request sent. Now get the response:
byte[] responseData = readResponse(conn, res);
@@ -644,6 +815,8 @@ protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRe
cacheManager.saveDetails(conn, res);
}
+ res.setSentBytes(calculateSentBytes(url, method, testElement.getHttpVersion(), requestHeaders, securityHeaders, postBodyBytes));
+
res = resultProcessing(areFollowingRedirect, frameDepth, res);
log.debug("End : sample");
@@ -652,6 +825,7 @@ protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRe
if (res.getEndTime() == 0) {
res.sampleEnd();
}
+ res.setSentBytes(calculateSentBytes(url, method, testElement.getHttpVersion(), requestHeaders, securityHeaders, postBodyBytes));
savedConn = null; // we don't want interrupt to try disconnection again
// We don't want to continue using this connection, even if KeepAlive is set
if (conn != null) { // May not exist
@@ -726,4 +900,877 @@ public boolean interrupt() {
}
return conn != null;
}
+
+ private HTTPSampleResult sampleHttp2(URL url, String method, boolean areFollowingRedirect, int frameDepth) {
+ if (log.isDebugEnabled()) {
+ log.debug("Start : sampleHttp2 {}, method {}, followingRedirect {}, depth {}",
+ url, method, areFollowingRedirect, frameDepth);
+ }
+
+ HTTPSampleResult res = new HTTPSampleResult();
+ configureSampleLabel(res, url);
+ res.setURL(url);
+ res.setHTTPMethod(method);
+
+ res.sampleStart();
+
+ final CacheManager cacheManager = getCacheManager();
+ if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method)) {
+ if (cacheManager.inCache(url, getHeaders(getHeaderManager()))) {
+ return updateSampleResultForResourceInCache(res);
+ }
+ }
+
+ CapturingHttpURLConnection capturingConn = null;
+ byte[] requestBodyBytes = new byte[0];
+ Map securityHeaders = Collections.emptyMap();
+
+ try {
+ capturingConn = new CapturingHttpURLConnection(url, method);
+
+ securityHeaders = setConnectionHeaders(capturingConn, url, getHeaderManager(), getCacheManager());
+ String cookies = setConnectionCookie(capturingConn, url, getCookieManager());
+ setConnectionAuthorization(capturingConn, url, getAuthManager(), securityHeaders);
+
+ if (method.equals(HTTPConstants.POST)) {
+ setPostHeaders(capturingConn);
+ String postBody = sendPostData(capturingConn);
+ res.setQueryString(postBody);
+ requestBodyBytes = capturingConn.getCapturedBytes();
+ } else if (method.equals(HTTPConstants.PUT)) {
+ setPutHeaders(capturingConn);
+ String putBody = sendPutData(capturingConn);
+ res.setQueryString(putBody);
+ requestBodyBytes = capturingConn.getCapturedBytes();
+ }
+
+ res.setRequestHeaders(getAllHeadersExceptCookie(capturingConn, securityHeaders));
+ if (StringUtilities.isNotEmpty(cookies)) {
+ res.setCookies(cookies);
+ } else {
+ res.setCookies(getOnlyCookieFromHeaders(capturingConn, securityHeaders));
+ }
+
+ URI uri = url.toURI();
+ HttpRequest.Builder reqBuilder = HttpRequest.newBuilder(uri);
+
+ if (method.equalsIgnoreCase(HTTPConstants.POST) || method.equalsIgnoreCase(HTTPConstants.PUT)
+ || method.equalsIgnoreCase(HTTPConstants.PATCH)) {
+ reqBuilder.method(method, HttpRequest.BodyPublishers.ofByteArray(requestBodyBytes));
+ } else if (method.equalsIgnoreCase(HTTPConstants.GET)) {
+ reqBuilder.GET();
+ } else if (method.equalsIgnoreCase(HTTPConstants.DELETE)) {
+ reqBuilder.DELETE();
+ } else {
+ reqBuilder.method(method, HttpRequest.BodyPublishers.noBody());
+ }
+
+ int rto = getResponseTimeout();
+ if (rto > 0) {
+ reqBuilder.timeout(Duration.ofMillis(rto));
+ }
+
+ Map> props = capturingConn.getRequestProperties();
+ for (Map.Entry> entry : props.entrySet()) {
+ String headerName = entry.getKey();
+ if (headerName == null || isRestrictedHeader(headerName)) {
+ continue;
+ }
+ for (String value : entry.getValue()) {
+ reqBuilder.header(headerName, value);
+ }
+ }
+
+ Http2Client client = getHttpClient(url);
+ HttpRequest httpRequest = reqBuilder.build();
+
+ HttpResponse response;
+ ConnectTimeTracker connectTimeTracker = client.connectTimeTracker;
+ connectTimeTracker.sampleStarted(res);
+ try {
+ response = client.httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofInputStream());
+ } finally {
+ connectTimeTracker.sampleFinished();
+ }
+ res.latencyEnd();
+
+ byte[] responseData = readResponse(response, res);
+
+ res.sampleEnd();
+
+ res.setResponseData(responseData);
+
+ int statusCode = response.statusCode();
+ res.setResponseCode(Integer.toString(statusCode));
+ res.setSuccessful(isSuccessCode(statusCode));
+ res.setResponseMessage(getReasonPhrase(statusCode));
+
+ String responseHeaders = getResponseHeaders(response);
+ res.setResponseHeaders(responseHeaders);
+
+ String ct = response.headers().firstValue(HTTPConstants.HEADER_CONTENT_TYPE).orElse(null);
+ if (ct != null) {
+ res.setContentType(ct);
+ res.setEncodingAndType(ct);
+ }
+
+ if (res.isRedirect()) {
+ String location = response.headers().firstValue(HTTPConstants.HEADER_LOCATION).orElse(null);
+ if (location != null) {
+ res.setRedirectLocation(location);
+ }
+ }
+
+ res.setHeadersSize(
+ responseHeaders.length()
+ + StringUtilities.count(responseHeaders, '\n')
+ + 2);
+
+ if (getAutoRedirects()) {
+ res.setURL(response.uri().toURL());
+ }
+
+ saveConnectionCookies(response, url, getCookieManager());
+
+ if (cacheManager != null) {
+ cacheManager.saveDetails(response, res);
+ }
+
+ res.setSentBytes(calculateSentBytes(url, method, "HTTP/2",
+ capturingConn != null ? capturingConn.getRequestProperties() : null,
+ securityHeaders, requestBodyBytes));
+
+ res = resultProcessing(areFollowingRedirect, frameDepth, res);
+
+ log.debug("End : sampleHttp2");
+ return res;
+ } catch (Exception e) {
+ if (res.getEndTime() == 0) {
+ res.sampleEnd();
+ }
+ res.setSentBytes(calculateSentBytes(url, method, "HTTP/2",
+ capturingConn != null ? capturingConn.getRequestProperties() : null,
+ securityHeaders, requestBodyBytes));
+ return errorResult(e, res);
+ }
+ }
+
+ private byte[] readResponse(HttpResponse response, SampleResult res) throws IOException {
+ InputStream in = response.body();
+ if (in == null) {
+ return NULL_BA;
+ }
+
+ boolean gzipped = response.headers().firstValue(HTTPConstants.HEADER_CONTENT_ENCODING)
+ .map(HTTPConstants.ENCODING_GZIP::equalsIgnoreCase)
+ .orElse(false);
+
+ long contentLength = response.headers().firstValueAsLong(HTTPConstants.HEADER_CONTENT_LENGTH).orElse(-1L);
+
+ if (contentLength == 0 && OBEY_CONTENT_LENGTH) {
+ log.info("Content-Length: 0, not reading http-body");
+ res.setResponseHeaders(getResponseHeaders(response));
+ res.latencyEnd();
+ return NULL_BA;
+ }
+
+ CountingInputStream instream = new CountingInputStream(in);
+ InputStream stream = gzipped ? new GZIPInputStream(instream) : instream;
+
+ try {
+ byte[] responseData = readResponse(res, stream, contentLength);
+ res.setBodySize(instream.getBytesRead());
+ return responseData;
+ } finally {
+ instream.close();
+ }
+ }
+
+ private static String getResponseHeaders(HttpResponse> response) {
+ StringBuilder headerBuf = new StringBuilder();
+ String versionStr = (response.version() == HttpClient.Version.HTTP_2) ? "HTTP/2" : "HTTP/1.1"; // $NON-NLS-1$ $NON-NLS-2$
+ headerBuf.append(versionStr).append(" ").append(response.statusCode()).append("\n"); // $NON-NLS-1$ $NON-NLS-2$
+
+ response.headers().map().forEach((key, values) -> {
+ if (key != null) {
+ for (String val : values) {
+ headerBuf.append(key).append(": ").append(val).append("\n"); // $NON-NLS-1$ $NON-NLS-2$
+ }
+ }
+ });
+ return headerBuf.toString();
+ }
+
+ private static void saveConnectionCookies(HttpResponse> response, URL u, CookieManager cookieManager) {
+ if (cookieManager != null) {
+ List setCookies = response.headers().allValues(HTTPConstants.HEADER_SET_COOKIE);
+ for (String setCookie : setCookies) {
+ cookieManager.addCookieFromHeader(setCookie, u);
+ }
+ }
+ }
+
+ private static boolean isRestrictedHeader(String name) {
+ return HTTPConstants.HEADER_CONNECTION.equalsIgnoreCase(name)
+ || HTTPConstants.HEADER_CONTENT_LENGTH.equalsIgnoreCase(name)
+ || "Host".equalsIgnoreCase(name) // $NON-NLS-1$
+ || "Expect".equalsIgnoreCase(name) // $NON-NLS-1$
+ || "Upgrade".equalsIgnoreCase(name); // $NON-NLS-1$
+ }
+
+ private static long calculateSentBytes(
+ URL u,
+ String method,
+ String version,
+ Map> requestHeaders,
+ Map securityHeaders,
+ byte[] postBodyBytes) {
+ long sentBytes = 0;
+
+ if (StringUtilities.isBlank(method)) {
+ method = HTTPConstants.GET;
+ }
+
+ String uri = u != null ? u.getFile() : "";
+ if (StringUtilities.isBlank(uri)) {
+ uri = "/"; // $NON-NLS-1$
+ }
+
+ if (StringUtilities.isBlank(version)) {
+ version = HTTPConstants.HTTP_1_1;
+ }
+
+ // Request line: METHOD URI VERSION\r\n
+ sentBytes += method.getBytes(StandardCharsets.UTF_8).length;
+ sentBytes += 1;
+ sentBytes += uri.getBytes(StandardCharsets.UTF_8).length;
+ sentBytes += 1;
+ sentBytes += version.getBytes(StandardCharsets.UTF_8).length;
+ sentBytes += 2;
+
+ // Request headers
+ if (requestHeaders != null) {
+ for (Map.Entry> entry : requestHeaders.entrySet()) {
+ String key = entry.getKey();
+ if (key != null) {
+ List values = entry.getValue();
+ if (values != null) {
+ for (String val : values) {
+ sentBytes += key.getBytes(StandardCharsets.UTF_8).length;
+ sentBytes += 2; // ": "
+ if (val != null) {
+ sentBytes += val.getBytes(StandardCharsets.UTF_8).length;
+ }
+ sentBytes += 2; // "\r\n"
+ }
+ }
+ }
+ }
+ }
+
+ if (securityHeaders != null && !securityHeaders.isEmpty()) {
+ for (Map.Entry secEntry : securityHeaders.entrySet()) {
+ String secKey = secEntry.getKey();
+ String secVal = secEntry.getValue();
+ if (secKey != null && !hasHeader(requestHeaders, secKey)) {
+ sentBytes += secKey.getBytes(StandardCharsets.UTF_8).length;
+ sentBytes += 2; // ": "
+ if (secVal != null) {
+ sentBytes += secVal.getBytes(StandardCharsets.UTF_8).length;
+ }
+ sentBytes += 2; // "\r\n"
+ }
+ }
+ }
+
+ // Header/Body separator \r\n
+ sentBytes += 2;
+
+ // Request body
+ if (postBodyBytes != null && postBodyBytes.length > 0) {
+ sentBytes += postBodyBytes.length;
+ } else if (requestHeaders != null) {
+ String contentLengthStr = getHeaderValue(requestHeaders, HTTPConstants.HEADER_CONTENT_LENGTH);
+ if (StringUtilities.isNotEmpty(contentLengthStr)) {
+ try {
+ sentBytes += Long.parseLong(contentLengthStr);
+ } catch (NumberFormatException e) {
+ log.debug("Could not parse Content-Length header: {}", contentLengthStr, e);
+ }
+ }
+ }
+
+ return sentBytes;
+ }
+
+ private static boolean hasHeader(Map> headers, String headerName) {
+ if (headers == null || headerName == null) {
+ return false;
+ }
+ for (String key : headers.keySet()) {
+ if (headerName.equalsIgnoreCase(key)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static String getHeaderValue(Map> headers, String headerName) {
+ if (headers == null || headerName == null) {
+ return null;
+ }
+ for (Map.Entry> entry : headers.entrySet()) {
+ if (headerName.equalsIgnoreCase(entry.getKey())) {
+ List values = entry.getValue();
+ if (values != null && !values.isEmpty()) {
+ return values.get(0);
+ }
+ }
+ }
+ return null;
+ }
+
+ private byte[] getBytes(String postBody) {
+ String enc = testElement != null ? testElement.getContentEncoding() : null;
+ if (StringUtilities.isBlank(enc)) {
+ enc = StandardCharsets.UTF_8.name();
+ }
+ try {
+ return postBody.getBytes(enc);
+ } catch (Exception e) {
+ return postBody.getBytes(StandardCharsets.UTF_8);
+ }
+ }
+
+ private Http2Client getHttpClient(URL url) {
+ int connectTimeout = getConnectTimeout();
+ String proxyHost = getProxyHost();
+ int proxyPort = getProxyPortInt();
+ String proxyUser = getProxyUser();
+ String proxyPass = getProxyPass();
+ boolean autoRedirects = getAutoRedirects();
+ SSLContext sslContext = null;
+
+ if (HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(url.getProtocol())) {
+ try {
+ SSLManager sslmgr = SSLManager.getInstance();
+ if (sslmgr instanceof JsseSSLManager jsseSSLManager) {
+ sslContext = jsseSSLManager.getContext();
+ }
+ } catch (Exception e) {
+ log.warn("Problem getting SSLContext for HTTP/2 HttpClient: ", e); // $NON-NLS-1$
+ }
+ if (sslContext == null) {
+ // Use the default context explicitly, so the connect time of the TLS handshake can be measured
+ try {
+ sslContext = SSLContext.getDefault();
+ } catch (NoSuchAlgorithmException e) {
+ log.warn("Problem getting default SSLContext for HTTP/2 HttpClient: ", e); // $NON-NLS-1$
+ }
+ }
+ }
+
+ HttpClientKey key = new HttpClientKey(connectTimeout, proxyHost, proxyPort,
+ proxyUser, proxyPass, autoRedirects, sslContext);
+
+ return HTTP_2_CLIENTS.get().computeIfAbsent(key, HTTPJavaImpl::createHttpClient);
+ }
+
+ private static Http2Client createHttpClient(HttpClientKey key) {
+ ConnectTimeTracker connectTimeTracker = new ConnectTimeTracker();
+ HttpClient.Builder builder = HttpClient.newBuilder()
+ .version(HttpClient.Version.HTTP_2)
+ .executor(new ConnectTimeMeasuringExecutor(connectTimeTracker))
+ .followRedirects(key.autoRedirects ? HttpClient.Redirect.NORMAL : HttpClient.Redirect.NEVER);
+
+ if (key.connectTimeout > 0) {
+ builder.connectTimeout(Duration.ofMillis(key.connectTimeout));
+ }
+
+ if (StringUtilities.isNotEmpty(key.proxyHost) && key.proxyPort > 0) {
+ builder.proxy(ProxySelector.of(new InetSocketAddress(key.proxyHost, key.proxyPort)));
+ if (StringUtilities.isNotEmpty(key.proxyUser)) {
+ builder.authenticator(new Authenticator() {
+ @Override
+ protected PasswordAuthentication getPasswordAuthentication() {
+ if (getRequestorType() == RequestorType.PROXY) {
+ return new PasswordAuthentication(key.proxyUser,
+ key.proxyPass != null ? key.proxyPass.toCharArray() : new char[0]);
+ }
+ return super.getPasswordAuthentication();
+ }
+ });
+ }
+ }
+
+ if (key.sslContext != null) {
+ builder.sslContext(new ConnectTimeMeasuringSSLContext(key.sslContext, connectTimeTracker));
+ }
+
+ return new Http2Client(builder.build(), connectTimeTracker);
+ }
+
+ /**
+ * Holder for an HTTP/2 {@link HttpClient} and the tracker which measures the connect time of its connections.
+ */
+ private static final class Http2Client {
+ private final HttpClient httpClient;
+ private final ConnectTimeTracker connectTimeTracker;
+
+ Http2Client(HttpClient httpClient, ConnectTimeTracker connectTimeTracker) {
+ this.httpClient = httpClient;
+ this.connectTimeTracker = connectTimeTracker;
+ }
+ }
+
+ /**
+ * Records the connect time of the JDK {@link HttpClient} in the current {@link SampleResult}.
+ *
+ * The JDK client does not expose a hook for connection establishment, so two indicators are used:
+ * the client submits its first task to the executor once the TCP connection has been established, and
+ * the wrapped {@code SSLEngine} reports when the TLS handshake has been finished. The TLS handshake
+ * always wins, since it is the more precise and the later event.
+ */
+ static final class ConnectTimeTracker {
+ private volatile SampleResult sampleResult;
+ private volatile boolean connectRecorded;
+ private volatile boolean handshakeRecorded;
+
+ void sampleStarted(SampleResult result) {
+ this.connectRecorded = false;
+ this.handshakeRecorded = false;
+ this.sampleResult = result;
+ }
+
+ void sampleFinished() {
+ this.sampleResult = null;
+ }
+
+ /** Invoked when the TCP connection has been established. */
+ void connectionEstablished() {
+ SampleResult result = sampleResult;
+ if (result != null && !connectRecorded && !handshakeRecorded) {
+ connectRecorded = true;
+ result.connectEnd();
+ }
+ }
+
+ /** Invoked when the TLS handshake has been finished, it overrides the plain TCP connect time. */
+ void handshakeFinished() {
+ SampleResult result = sampleResult;
+ if (result != null && !handshakeRecorded) {
+ handshakeRecorded = true;
+ connectRecorded = true;
+ result.connectEnd();
+ }
+ }
+ }
+
+ /**
+ * Executor which notifies the {@link ConnectTimeTracker} before delegating to the shared thread pool.
+ */
+ private static final class ConnectTimeMeasuringExecutor implements Executor {
+ private final ConnectTimeTracker tracker;
+
+ ConnectTimeMeasuringExecutor(ConnectTimeTracker tracker) {
+ this.tracker = tracker;
+ }
+
+ @Override
+ public void execute(Runnable command) {
+ tracker.connectionEstablished();
+ HTTP_2_EXECUTOR.execute(command);
+ }
+ }
+
+ private static final class Http2ThreadFactory implements ThreadFactory {
+ private final AtomicInteger counter = new AtomicInteger();
+
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread thread = new Thread(r, "JMeter-HTTP2-" + counter.incrementAndGet()); // $NON-NLS-1$
+ thread.setDaemon(true);
+ return thread;
+ }
+ }
+
+ /**
+ * {@link SSLContext} which creates {@link SSLEngine}s that report the end of the TLS handshake.
+ */
+ static final class ConnectTimeMeasuringSSLContext extends SSLContext {
+ ConnectTimeMeasuringSSLContext(SSLContext delegate, ConnectTimeTracker tracker) {
+ super(new ConnectTimeMeasuringSSLContextSpi(delegate, tracker), delegate.getProvider(),
+ delegate.getProtocol());
+ }
+ }
+
+ private static final class ConnectTimeMeasuringSSLContextSpi extends SSLContextSpi {
+ private final SSLContext delegate;
+ private final ConnectTimeTracker tracker;
+
+ ConnectTimeMeasuringSSLContextSpi(SSLContext delegate, ConnectTimeTracker tracker) {
+ this.delegate = delegate;
+ this.tracker = tracker;
+ }
+
+ @Override
+ protected void engineInit(KeyManager[] km, TrustManager[] tm, SecureRandom sr) throws KeyManagementException {
+ delegate.init(km, tm, sr);
+ }
+
+ @Override
+ protected SSLSocketFactory engineGetSocketFactory() {
+ return delegate.getSocketFactory();
+ }
+
+ @Override
+ protected SSLServerSocketFactory engineGetServerSocketFactory() {
+ return delegate.getServerSocketFactory();
+ }
+
+ @Override
+ protected SSLEngine engineCreateSSLEngine() {
+ return new ConnectTimeMeasuringSSLEngine(delegate.createSSLEngine(), tracker);
+ }
+
+ @Override
+ protected SSLEngine engineCreateSSLEngine(String host, int port) {
+ return new ConnectTimeMeasuringSSLEngine(delegate.createSSLEngine(host, port), tracker);
+ }
+
+ @Override
+ protected SSLSessionContext engineGetServerSessionContext() {
+ return delegate.getServerSessionContext();
+ }
+
+ @Override
+ protected SSLSessionContext engineGetClientSessionContext() {
+ return delegate.getClientSessionContext();
+ }
+
+ @Override
+ protected SSLParameters engineGetDefaultSSLParameters() {
+ return delegate.getDefaultSSLParameters();
+ }
+
+ @Override
+ protected SSLParameters engineGetSupportedSSLParameters() {
+ return delegate.getSupportedSSLParameters();
+ }
+ }
+
+ /**
+ * {@link SSLEngine} which delegates all calls and notifies the {@link ConnectTimeTracker}
+ * as soon as the TLS handshake has been finished.
+ */
+ static final class ConnectTimeMeasuringSSLEngine extends SSLEngine {
+ private final SSLEngine delegate;
+ private final ConnectTimeTracker tracker;
+
+ ConnectTimeMeasuringSSLEngine(SSLEngine delegate, ConnectTimeTracker tracker) {
+ super(delegate.getPeerHost(), delegate.getPeerPort());
+ this.delegate = delegate;
+ this.tracker = tracker;
+ }
+
+ private void checkHandshakeFinished(SSLEngineResult result) {
+ if (result.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.FINISHED) {
+ tracker.handshakeFinished();
+ }
+ }
+
+ @Override
+ public SSLEngineResult wrap(ByteBuffer[] srcs, int offset, int length, ByteBuffer dst) throws SSLException {
+ SSLEngineResult result = delegate.wrap(srcs, offset, length, dst);
+ checkHandshakeFinished(result);
+ return result;
+ }
+
+ @Override
+ public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts, int offset, int length) throws SSLException {
+ SSLEngineResult result = delegate.unwrap(src, dsts, offset, length);
+ checkHandshakeFinished(result);
+ return result;
+ }
+
+ @Override
+ public Runnable getDelegatedTask() {
+ return delegate.getDelegatedTask();
+ }
+
+ @Override
+ public void closeInbound() throws SSLException {
+ delegate.closeInbound();
+ }
+
+ @Override
+ public boolean isInboundDone() {
+ return delegate.isInboundDone();
+ }
+
+ @Override
+ public void closeOutbound() {
+ delegate.closeOutbound();
+ }
+
+ @Override
+ public boolean isOutboundDone() {
+ return delegate.isOutboundDone();
+ }
+
+ @Override
+ public String[] getSupportedCipherSuites() {
+ return delegate.getSupportedCipherSuites();
+ }
+
+ @Override
+ public String[] getEnabledCipherSuites() {
+ return delegate.getEnabledCipherSuites();
+ }
+
+ @Override
+ public void setEnabledCipherSuites(String[] suites) {
+ delegate.setEnabledCipherSuites(suites);
+ }
+
+ @Override
+ public String[] getSupportedProtocols() {
+ return delegate.getSupportedProtocols();
+ }
+
+ @Override
+ public String[] getEnabledProtocols() {
+ return delegate.getEnabledProtocols();
+ }
+
+ @Override
+ public void setEnabledProtocols(String[] protocols) {
+ delegate.setEnabledProtocols(protocols);
+ }
+
+ @Override
+ public SSLSession getSession() {
+ return delegate.getSession();
+ }
+
+ @Override
+ public SSLSession getHandshakeSession() {
+ return delegate.getHandshakeSession();
+ }
+
+ @Override
+ public void beginHandshake() throws SSLException {
+ delegate.beginHandshake();
+ }
+
+ @Override
+ public SSLEngineResult.HandshakeStatus getHandshakeStatus() {
+ return delegate.getHandshakeStatus();
+ }
+
+ @Override
+ public void setUseClientMode(boolean mode) {
+ delegate.setUseClientMode(mode);
+ }
+
+ @Override
+ public boolean getUseClientMode() {
+ return delegate.getUseClientMode();
+ }
+
+ @Override
+ public void setNeedClientAuth(boolean need) {
+ delegate.setNeedClientAuth(need);
+ }
+
+ @Override
+ public boolean getNeedClientAuth() {
+ return delegate.getNeedClientAuth();
+ }
+
+ @Override
+ public void setWantClientAuth(boolean want) {
+ delegate.setWantClientAuth(want);
+ }
+
+ @Override
+ public boolean getWantClientAuth() {
+ return delegate.getWantClientAuth();
+ }
+
+ @Override
+ public void setEnableSessionCreation(boolean flag) {
+ delegate.setEnableSessionCreation(flag);
+ }
+
+ @Override
+ public boolean getEnableSessionCreation() {
+ return delegate.getEnableSessionCreation();
+ }
+
+ @Override
+ public SSLParameters getSSLParameters() {
+ return delegate.getSSLParameters();
+ }
+
+ @Override
+ public void setSSLParameters(SSLParameters params) {
+ delegate.setSSLParameters(params);
+ }
+
+ @Override
+ public String getApplicationProtocol() {
+ return delegate.getApplicationProtocol();
+ }
+
+ @Override
+ public String getHandshakeApplicationProtocol() {
+ return delegate.getHandshakeApplicationProtocol();
+ }
+
+ @Override
+ public void setHandshakeApplicationProtocolSelector(BiFunction, String> selector) {
+ if (selector == null) {
+ delegate.setHandshakeApplicationProtocolSelector(null);
+ } else {
+ delegate.setHandshakeApplicationProtocolSelector((engine, protocols) -> selector.apply(this, protocols));
+ }
+ }
+
+ @Override
+ public BiFunction, String> getHandshakeApplicationProtocolSelector() {
+ return delegate.getHandshakeApplicationProtocolSelector();
+ }
+ }
+
+ private static class HttpClientKey {
+ private final int connectTimeout;
+ private final String proxyHost;
+ private final int proxyPort;
+ private final String proxyUser;
+ private final String proxyPass;
+ private final boolean autoRedirects;
+ private final SSLContext sslContext;
+
+ HttpClientKey(int connectTimeout, String proxyHost, int proxyPort,
+ String proxyUser, String proxyPass, boolean autoRedirects,
+ SSLContext sslContext) {
+ this.connectTimeout = connectTimeout;
+ this.proxyHost = proxyHost;
+ this.proxyPort = proxyPort;
+ this.proxyUser = proxyUser;
+ this.proxyPass = proxyPass;
+ this.autoRedirects = autoRedirects;
+ this.sslContext = sslContext;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (!(o instanceof HttpClientKey that)) {
+ return false;
+ }
+ return connectTimeout == that.connectTimeout
+ && proxyPort == that.proxyPort
+ && autoRedirects == that.autoRedirects
+ && Objects.equals(proxyHost, that.proxyHost)
+ && Objects.equals(proxyUser, that.proxyUser)
+ && Objects.equals(proxyPass, that.proxyPass)
+ && Objects.equals(sslContext, that.sslContext);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(connectTimeout, proxyHost, proxyPort, proxyUser, proxyPass, autoRedirects, sslContext);
+ }
+ }
+
+ private static class CapturingHttpURLConnection extends HttpURLConnection {
+ private final Map> requestProperties = new LinkedHashMap<>();
+ private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+
+ CapturingHttpURLConnection(URL url, String method) {
+ super(url);
+ this.method = method;
+ }
+
+ @Override
+ public void setRequestProperty(String key, String value) {
+ if (key == null) {
+ return;
+ }
+ List list = new ArrayList<>();
+ list.add(value);
+ requestProperties.put(key, list);
+ }
+
+ @Override
+ public void addRequestProperty(String key, String value) {
+ if (key == null) {
+ return;
+ }
+ requestProperties.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
+ }
+
+ @Override
+ public String getRequestProperty(String key) {
+ if (key == null) {
+ return null;
+ }
+ List values = requestProperties.get(key);
+ if (values == null || values.isEmpty()) {
+ for (Map.Entry> entry : requestProperties.entrySet()) {
+ if (key.equalsIgnoreCase(entry.getKey())) {
+ values = entry.getValue();
+ break;
+ }
+ }
+ }
+ return (values != null && !values.isEmpty()) ? values.get(0) : null;
+ }
+
+ @Override
+ public Map> getRequestProperties() {
+ return Collections.unmodifiableMap(requestProperties);
+ }
+
+ @Override
+ public java.io.OutputStream getOutputStream() throws IOException {
+ return outputStream;
+ }
+
+ byte[] getCapturedBytes() {
+ return outputStream.toByteArray();
+ }
+
+ @Override
+ public void connect() throws IOException {
+ }
+
+ @Override
+ public void disconnect() {
+ }
+
+ @Override
+ public boolean usingProxy() {
+ return false;
+ }
+
+ @Override
+ public String getHeaderField(int n) {
+ return null;
+ }
+
+ @Override
+ public String getHeaderFieldKey(int n) {
+ return null;
+ }
+
+ @Override
+ public String getHeaderField(String name) {
+ return null;
+ }
+ }
}
diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java
index 3105d0cf1ec..ee307623677 100644
--- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java
+++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java
@@ -160,6 +160,8 @@ public abstract class HTTPSamplerBase extends AbstractSampler
public static final String IMPLEMENTATION = "HTTPSampler.implementation"; // $NON-NLS-1$
+ public static final String HTTP_VERSION = "HTTPSampler.httpVersion"; // $NON-NLS-1$
+
public static final String PATH = "HTTPSampler.path"; // $NON-NLS-1$
public static final String FOLLOW_REDIRECTS = HTTPSamplerBaseSchema.INSTANCE.getFollowRedirects().getName();
@@ -640,6 +642,14 @@ public String getImplementation() {
return get(getSchema().getImplementation());
}
+ public void setHttpVersion(String value) {
+ set(getSchema().getHttpVersion(), value);
+ }
+
+ public String getHttpVersion() {
+ return get(getSchema().getHttpVersion());
+ }
+
public boolean useMD5() {
return get(getSchema().getStoreAsMD5());
}
diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerFactory.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerFactory.java
index 1b9798bad4f..c7781156dc4 100644
--- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerFactory.java
+++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerFactory.java
@@ -38,6 +38,8 @@ public final class HTTPSamplerFactory {
//+ JMX implementation attribute values (also displayed in GUI) - do not change
public static final String IMPL_HTTP_CLIENT4 = "HttpClient4"; // $NON-NLS-1$
+ public static final String IMPL_HTTP_CLIENT5 = "HttpClient5"; // $NON-NLS-1$
+
public static final String IMPL_HTTP_CLIENT3_1 = "HttpClient3.1"; // $NON-NLS-1$
public static final String IMPL_JAVA = "Java"; // $NON-NLS-1$
@@ -62,7 +64,7 @@ public static HTTPSamplerBase newInstance() {
/**
* Create a new instance of the required sampler type
*
- * @param alias HTTP_SAMPLER or HTTP_SAMPLER_APACHE or IMPL_HTTP_CLIENT3_1 or IMPL_HTTP_CLIENT4
+ * @param alias HTTP_SAMPLER or HTTP_SAMPLER_APACHE or IMPL_HTTP_CLIENT3_1, IMPL_HTTP_CLIENT4 or IMPL_HTTP_CLIENT5
* @return the appropriate sampler
* @throws UnsupportedOperationException if alias is not recognised
*/
@@ -76,11 +78,14 @@ public static HTTPSamplerBase newInstance(String alias) {
if (alias.equals(IMPL_HTTP_CLIENT4) || alias.equals(HTTP_SAMPLER_APACHE) || alias.equals(IMPL_HTTP_CLIENT3_1)) {
return new HTTPSamplerProxy(IMPL_HTTP_CLIENT4);
}
+ if (alias.equals(IMPL_HTTP_CLIENT5)) {
+ return new HTTPSamplerProxy(IMPL_HTTP_CLIENT5);
+ }
throw new IllegalArgumentException("Unknown sampler type: '" + alias+"'");
}
public static String[] getImplementations(){
- return new String[]{IMPL_HTTP_CLIENT4,IMPL_JAVA};
+ return new String[]{IMPL_HTTP_CLIENT4, IMPL_HTTP_CLIENT5, IMPL_JAVA};
}
public static HTTPAbstractImpl getImplementation(String impl, HTTPSamplerBase base){
@@ -94,6 +99,8 @@ public static HTTPAbstractImpl getImplementation(String impl, HTTPSamplerBase ba
return new HTTPJavaImpl(base);
} else if (IMPL_HTTP_CLIENT4.equals(impl) || IMPL_HTTP_CLIENT3_1.equals(impl)) {
return new HTTPHC4Impl(base);
+ } else if (IMPL_HTTP_CLIENT5.equals(impl)) {
+ return new HTTPHC5Impl(base);
} else {
throw new IllegalArgumentException("Unknown implementation type: '"+impl+"'");
}
diff --git a/src/protocol/http/src/main/kotlin/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBaseSchema.kt b/src/protocol/http/src/main/kotlin/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBaseSchema.kt
index 89bb2f58e28..84698fff881 100644
--- a/src/protocol/http/src/main/kotlin/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBaseSchema.kt
+++ b/src/protocol/http/src/main/kotlin/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBaseSchema.kt
@@ -87,6 +87,9 @@ public abstract class HTTPSamplerBaseSchema : TestElementSchema() {
public val implementation: StringPropertyDescriptor
by string("HTTPSampler.implementation")
+ public val httpVersion: StringPropertyDescriptor
+ by string("HTTPSampler.httpVersion")
+
public val connectTimeout: IntegerPropertyDescriptor
by int("HTTPSampler.connect_timeout")
diff --git a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPHC5Features.java b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPHC5Features.java
new file mode 100644
index 00000000000..7abf55a6bd7
--- /dev/null
+++ b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPHC5Features.java
@@ -0,0 +1,437 @@
+/*
+ * 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 org.apache.jmeter.protocol.http.sampler;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
+import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.post;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.hc.client5.http.HttpRoute;
+import org.apache.hc.client5.http.io.ConnectionEndpoint;
+import org.apache.hc.client5.http.io.HttpClientConnectionManager;
+import org.apache.hc.client5.http.io.LeaseRequest;
+import org.apache.hc.client5.http.protocol.HttpClientContext;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.http2.HttpVersionPolicy;
+import org.apache.hc.core5.io.CloseMode;
+import org.apache.hc.core5.util.TimeValue;
+import org.apache.hc.core5.util.Timeout;
+import org.apache.jmeter.protocol.http.control.AuthManager;
+import org.apache.jmeter.protocol.http.control.CacheManager;
+import org.apache.jmeter.protocol.http.util.HTTPConstants;
+import org.apache.jmeter.samplers.SampleResult;
+import org.junit.jupiter.api.Test;
+
+import com.github.tomakehurst.wiremock.WireMockServer;
+import com.github.tomakehurst.wiremock.client.WireMock;
+import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
+
+class TestHTTPHC5Features {
+
+ @Test
+ void usesHttpClientVersionWhenSamplerVersionIsEmpty() {
+ assertEquals(HttpVersionPolicy.NEGOTIATE, HTTPHC5Impl.getHttpVersionPolicy("", "HTTP/2"));
+ }
+
+ @Test
+ void usesSamplerHttpVersionWhenSpecified() {
+ assertEquals(HttpVersionPolicy.FORCE_HTTP_1, HTTPHC5Impl.getHttpVersionPolicy("HTTP/1.1", "HTTP/2"));
+ assertEquals(HttpVersionPolicy.NEGOTIATE, HTTPHC5Impl.getHttpVersionPolicy("HTTP/2", "HTTP/1.1"));
+ }
+
+ @Test
+ void defaultsToHttp11ForUnsupportedHttpVersion() {
+ assertEquals(HttpVersionPolicy.FORCE_HTTP_1, HTTPHC5Impl.getHttpVersionPolicy("HTTP/3", "HTTP/2"));
+ }
+
+ @Test
+ void doesNotRequireProtocolUpgradeConfiguration() throws Exception {
+ try (InputStream classFile = HTTPHC5Impl.class.getResourceAsStream("HTTPHC5Impl.class")) {
+ assertFalse(new String(classFile.readAllBytes(), StandardCharsets.ISO_8859_1)
+ .contains("setProtocolUpgradeEnabled"));
+ }
+ }
+
+ @Test
+ void doesNotRequireHttpAsyncClassicAdapter() throws Exception {
+ try (InputStream classFile = HTTPHC5Impl.class.getResourceAsStream("HTTPHC5Impl.class")) {
+ assertFalse(hasMethodReference(classFile.readAllBytes(),
+ "org/apache/hc/client5/http/impl/async/HttpAsyncClients", "classic"));
+ }
+ }
+
+ @Test
+ void usesHttp2WhenSelected() throws Exception {
+ WireMockServer server = new WireMockServer(WireMockConfiguration.wireMockConfig()
+ .dynamicHttpsPort()
+ .http2TlsDisabled(false));
+ try {
+ server.start();
+ server.stubFor(get(urlEqualTo("/http2")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/2");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL("https://localhost:" + server.httpsPort() + "/http2"), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertEquals("HTTP/2", result.getResponseHeaders().substring(0, "HTTP/2".length()));
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void fallsBackToHttp11WhenServerDoesNotSupportHttp2() throws Exception {
+ WireMockServer server = new WireMockServer(WireMockConfiguration.wireMockConfig()
+ .dynamicHttpsPort()
+ .http2TlsDisabled(true));
+ try {
+ server.start();
+ server.stubFor(get(urlEqualTo("/fallback")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/2");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL("https://localhost:" + server.httpsPort() + "/fallback"), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertEquals("HTTP/1.1", result.getResponseHeaders().substring(0, "HTTP/1.1".length()));
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void usesHttp2WithProxy() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/http2proxy")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/2");
+ sampler.setProxyHost("localhost");
+ sampler.setProxyPortInt(Integer.toString(server.port()));
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/http2proxy")), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void usesHttp11WhenSelected() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/http11")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/1.1");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/http11")), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertEquals("HTTP/1.1", result.getResponseHeaders().substring(0, "HTTP/1.1".length()));
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void setsSentBytesCorrectlyForGetRequest() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/sentBytesGet")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/1.1");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/sentBytesGet")), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertTrue(result.getSentBytes() > 0, "sentBytes should be greater than 0");
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void setsSentBytesCorrectlyForPostRequest() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(post(urlEqualTo("/sentBytesPost")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/1.1");
+ sampler.setPostBodyRaw(true);
+ sampler.addNonEncodedArgument("", "hello world", "");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/sentBytesPost")), HTTPConstants.POST, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertTrue(result.getSentBytes() > "hello world".length(), "sentBytes should include request line, headers, and body");
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void sendsConditionalRequestForCachedResource() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/cache"))
+ .willReturn(aResponse().withHeader("ETag", "cache-tag").withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setCacheManager(new CacheManager());
+ URL url = new URL(server.url("/cache"));
+
+ assertEquals("200", sampler.sample(url, HTTPConstants.GET, false, 1).getResponseCode());
+ assertEquals("200", sampler.sample(url, HTTPConstants.GET, false, 1).getResponseCode());
+
+ server.verify(1, getRequestedFor(urlEqualTo("/cache"))
+ .withHeader("If-None-Match", WireMock.equalTo("cache-tag")));
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void sendsBasicCredentialsFromAuthorizationManager() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/auth"))
+ .withHeader("Authorization", WireMock.equalTo("Basic dXNlcjpwYXNz"))
+ .willReturn(aResponse().withStatus(200)));
+ server.stubFor(get(urlEqualTo("/auth")).atPriority(10)
+ .willReturn(aResponse().withHeader("WWW-Authenticate", "Basic realm=\"test\"").withStatus(401)));
+ AuthManager authManager = new AuthManager();
+ authManager.set(-1, server.url("/"), "user", "pass", "", "", AuthManager.Mechanism.BASIC);
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setAuthManager(authManager);
+
+ assertEquals("200", sampler.sample(new URL(server.url("/auth")), HTTPConstants.GET, false, 1).getResponseCode());
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void authenticatesWithConfiguredProxyCredentials() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/proxy"))
+ .withHeader("Proxy-Authorization", WireMock.equalTo("Basic dXNlcjpwYXNz"))
+ .willReturn(aResponse().withStatus(200)));
+ server.stubFor(get(urlEqualTo("/proxy")).atPriority(10)
+ .willReturn(aResponse().withHeader("Proxy-Authenticate", "Basic realm=\"proxy\"").withStatus(407)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setProxyHost("localhost");
+ sampler.setProxyPortInt(Integer.toString(server.port()));
+ sampler.setProxyUser("user");
+ sampler.setProxyPass("pass");
+
+ assertEquals("200", sampler.sample(new URL(server.url("/proxy")), HTTPConstants.GET, false, 1).getResponseCode());
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void setsConnectTimeForHttp11() throws Exception {
+ SampleResult result = new SampleResult();
+ result.sampleStart();
+ HttpClientContext context = HttpClientContext.create();
+ context.setAttribute(HTTPHC5Impl.CONTEXT_ATTRIBUTE_SAMPLER_RESULT, result);
+ HttpClientConnectionManager connectionManager =
+ new HTTPHC5Impl.ConnectTimeMeasuringConnectionManager(new SlowConnectingConnectionManager(50));
+
+ connectionManager.connect(null, null, context);
+ Thread.sleep(50);
+ result.sampleEnd();
+
+ assertTrue(result.getConnectTime() >= 50,
+ "connectTime should cover the time spent connecting, but was " + result.getConnectTime());
+ assertTrue(result.getConnectTime() <= result.getTime(),
+ "connectTime should not exceed the elapsed time");
+ }
+
+ /** Connection manager that only supports {@code connect} and takes a well-known amount of time for it. */
+ private static final class SlowConnectingConnectionManager implements HttpClientConnectionManager {
+
+ private final long connectDurationMillis;
+
+ SlowConnectingConnectionManager(long connectDurationMillis) {
+ this.connectDurationMillis = connectDurationMillis;
+ }
+
+ @Override
+ public LeaseRequest lease(String id, HttpRoute route, Timeout requestTimeout, Object state) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void release(ConnectionEndpoint endpoint, Object newState, TimeValue validDuration) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void connect(ConnectionEndpoint endpoint, TimeValue connectTimeout, HttpContext context) throws IOException {
+ try {
+ Thread.sleep(connectDurationMillis);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new IOException(e);
+ }
+ }
+
+ @Override
+ public void upgrade(ConnectionEndpoint endpoint, HttpContext context) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void close(CloseMode closeMode) {
+ // nothing to close
+ }
+
+ @Override
+ public void close() {
+ // nothing to close
+ }
+ }
+
+ @Test
+ void setsConnectTimeForHttp2() throws Exception {
+ WireMockServer server = new WireMockServer(WireMockConfiguration.wireMockConfig()
+ .dynamicHttpsPort()
+ .http2TlsDisabled(false));
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/connectTime2")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/2");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL("https://localhost:" + server.httpsPort() + "/connectTime2"), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertTrue(result.getConnectTime() > 0,
+ "connectTime should be greater than 0, but was " + result.getConnectTime());
+ assertTrue(result.getConnectTime() <= result.getTime(),
+ "connectTime should not exceed the elapsed time");
+ } finally {
+ server.stop();
+ }
+ }
+
+ private static HTTPSamplerBase newSampler() {
+ return HTTPSamplerFactory.newInstance("HttpClient5");
+ }
+
+ private static WireMockServer createServer() {
+ return new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
+ }
+
+ private static boolean hasMethodReference(byte[] classBytes, String className, String methodName) throws IOException {
+ try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(classBytes))) {
+ input.readInt();
+ input.readUnsignedShort();
+ input.readUnsignedShort();
+ int constantPoolCount = input.readUnsignedShort();
+ int[] tags = new int[constantPoolCount];
+ int[] firstReferences = new int[constantPoolCount];
+ int[] secondReferences = new int[constantPoolCount];
+ String[] utf8Values = new String[constantPoolCount];
+ int i = 1;
+ while (i < constantPoolCount) {
+ tags[i] = input.readUnsignedByte();
+ switch (tags[i]) {
+ case 1:
+ utf8Values[i] = input.readUTF();
+ break;
+ case 3:
+ case 4:
+ input.readInt();
+ break;
+ case 5:
+ case 6:
+ input.readLong();
+ i++;
+ break;
+ case 7:
+ case 8:
+ case 16:
+ case 19:
+ case 20:
+ firstReferences[i] = input.readUnsignedShort();
+ break;
+ case 9:
+ case 10:
+ case 11:
+ case 12:
+ case 17:
+ case 18:
+ firstReferences[i] = input.readUnsignedShort();
+ secondReferences[i] = input.readUnsignedShort();
+ break;
+ case 15:
+ input.readUnsignedByte();
+ firstReferences[i] = input.readUnsignedShort();
+ break;
+ default:
+ throw new IOException("Unknown class-file constant-pool tag " + tags[i]);
+ }
+ i++;
+ }
+ for (int methodReferenceIndex = 1; methodReferenceIndex < constantPoolCount; methodReferenceIndex++) {
+ if (tags[methodReferenceIndex] != 10) {
+ continue;
+ }
+ int classIndex = firstReferences[methodReferenceIndex];
+ int nameAndTypeIndex = secondReferences[methodReferenceIndex];
+ String referencedClass = utf8Values[firstReferences[classIndex]];
+ String referencedMethod = utf8Values[firstReferences[nameAndTypeIndex]];
+ if (className.equals(referencedClass) && methodName.equals(referencedMethod)) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+}
diff --git a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPJavaFeatures.java b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPJavaFeatures.java
new file mode 100644
index 00000000000..d008a94431b
--- /dev/null
+++ b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPJavaFeatures.java
@@ -0,0 +1,392 @@
+/*
+ * 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 org.apache.jmeter.protocol.http.sampler;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
+import static com.github.tomakehurst.wiremock.client.WireMock.post;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.net.Socket;
+import java.net.URI;
+import java.net.URL;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.security.cert.X509Certificate;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509ExtendedTrustManager;
+
+import org.apache.jmeter.protocol.http.control.Header;
+import org.apache.jmeter.protocol.http.control.HeaderManager;
+import org.apache.jmeter.protocol.http.util.HTTPConstants;
+import org.apache.jmeter.samplers.SampleResult;
+import org.junit.jupiter.api.Test;
+
+import com.github.tomakehurst.wiremock.WireMockServer;
+import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
+
+class TestHTTPJavaFeatures {
+
+ @Test
+ void usesHttpClientVersionWhenSamplerVersionIsEmpty() {
+ assertTrue(HTTPJavaImpl.isHttp2("", "HTTP/2"));
+ assertFalse(HTTPJavaImpl.isHttp2("", "HTTP/1.1"));
+ }
+
+ @Test
+ void usesSamplerHttpVersionWhenSpecified() {
+ assertFalse(HTTPJavaImpl.isHttp2("HTTP/1.1", "HTTP/2"));
+ assertTrue(HTTPJavaImpl.isHttp2("HTTP/2", "HTTP/1.1"));
+ }
+
+ @Test
+ void defaultsToHttp11ForUnsupportedHttpVersion() {
+ assertFalse(HTTPJavaImpl.isHttp2("HTTP/3", "HTTP/2"));
+ }
+
+ @Test
+ void usesHttp2WhenSelected() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/http2")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/2");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/http2")), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertEquals("HTTP/2", result.getResponseHeaders().substring(0, "HTTP/2".length()));
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void setsResponseMessageForHttp2() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/http2ResponseMessage")).willReturn(aResponse().withStatus(404)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/2");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/http2ResponseMessage")), HTTPConstants.GET, false, 1);
+
+ assertEquals("404", result.getResponseCode());
+ assertEquals("Not Found", result.getResponseMessage());
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void returnsEmptyReasonPhraseForUnknownStatusCode() {
+ assertEquals("OK", HTTPJavaImpl.getReasonPhrase(200));
+ assertEquals("", HTTPJavaImpl.getReasonPhrase(599));
+ }
+
+ @Test
+ void usesHttp2WithProxy() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/http2proxy")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/2");
+ sampler.setProxyHost("localhost");
+ sampler.setProxyPortInt(Integer.toString(server.port()));
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/http2proxy")), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void setsSentBytesCorrectlyForGetRequest() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/sentBytesGet")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/1.1");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/sentBytesGet")), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertTrue(result.getSentBytes() > 0, "sentBytes should be greater than 0");
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void setsSentBytesCorrectlyForPostRequest() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(post(urlEqualTo("/sentBytesPost")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/1.1");
+ sampler.setPostBodyRaw(true);
+ sampler.addNonEncodedArgument("", "hello world", "");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/sentBytesPost")), HTTPConstants.POST, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertTrue(result.getSentBytes() > "hello world".length(), "sentBytes should include request line, headers, and body");
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void setsSentBytesCorrectlyForHttp2GetRequest() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/http2SentBytesGet")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/2");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/http2SentBytesGet")), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertTrue(result.getSentBytes() > 0, "sentBytes should be greater than 0");
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void setsSentBytesCorrectlyForHttp2PostRequest() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(post(urlEqualTo("/http2SentBytesPost")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/2");
+ sampler.setPostBodyRaw(true);
+ sampler.addNonEncodedArgument("", "hello world http2", "");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/http2SentBytesPost")), HTTPConstants.POST, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertTrue(result.getSentBytes() > "hello world http2".length(), "sentBytes should include request line, headers, and body");
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void displaysAuthorizationHeaderFromHeaderManagerInHttp11() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/authHttp11")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/1.1");
+ HeaderManager headerManager = new HeaderManager();
+ headerManager.add(new Header("Authorization", "Bearer my-secret-token"));
+ sampler.setHeaderManager(headerManager);
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/authHttp11")), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertTrue(result.getRequestHeaders().contains("Authorization: Bearer my-secret-token"),
+ "Request headers should contain Authorization header set in HeaderManager");
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void displaysAuthorizationHeaderFromHeaderManagerInHttp2() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/authHttp2")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/2");
+ HeaderManager headerManager = new HeaderManager();
+ headerManager.add(new Header("Authorization", "Bearer my-secret-token-http2"));
+ sampler.setHeaderManager(headerManager);
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/authHttp2")), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertTrue(result.getRequestHeaders().contains("Authorization: Bearer my-secret-token-http2"),
+ "Request headers should contain Authorization header set in HeaderManager");
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void setsConnectTimeForHttp11() throws Exception {
+ WireMockServer server = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicHttpsPort());
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/connectTime")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/1.1");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL("https://localhost:" + server.httpsPort() + "/connectTime"), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertTrue(result.getConnectTime() > 0,
+ "connectTime should be greater than 0, but was " + result.getConnectTime());
+ assertTrue(result.getConnectTime() <= result.getTime(),
+ "connectTime should not exceed the elapsed time");
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void setsConnectTimeForHttp2OverPlainConnection() throws Exception {
+ WireMockServer server = createServer();
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/http2ConnectTime")).willReturn(aResponse().withStatus(200)));
+ HTTPSamplerBase sampler = newSampler();
+ sampler.setHttpVersion("HTTP/2");
+
+ HTTPSampleResult result = sampler.sample(
+ new URL(server.url("/http2ConnectTime")), HTTPConstants.GET, false, 1);
+
+ assertEquals("200", result.getResponseCode());
+ assertTrue(result.getConnectTime() > 0,
+ "connectTime should be greater than 0, but was " + result.getConnectTime());
+ assertTrue(result.getConnectTime() <= result.getTime(),
+ "connectTime should not exceed the elapsed time");
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ void setsConnectTimeForHttp2OverTls() throws Exception {
+ WireMockServer server = new WireMockServer(
+ WireMockConfiguration.wireMockConfig().dynamicHttpsPort().http2TlsDisabled(false));
+ server.start();
+ try {
+ server.stubFor(get(urlEqualTo("/http2TlsConnectTime")).willReturn(aResponse().withStatus(200)));
+
+ HTTPJavaImpl.ConnectTimeTracker tracker = new HTTPJavaImpl.ConnectTimeTracker();
+ SSLContext sslContext = trustAllContext();
+ HttpClient client = HttpClient.newBuilder()
+ .version(HttpClient.Version.HTTP_2)
+ .sslContext(new HTTPJavaImpl.ConnectTimeMeasuringSSLContext(sslContext, tracker))
+ .build();
+
+ SampleResult result = new SampleResult();
+ result.sampleStart();
+ tracker.sampleStarted(result);
+ HttpResponse response;
+ try {
+ response = client.send(
+ HttpRequest.newBuilder(URI.create(
+ "https://localhost:" + server.httpsPort() + "/http2TlsConnectTime")).build(),
+ HttpResponse.BodyHandlers.ofString());
+ } finally {
+ tracker.sampleFinished();
+ }
+ result.sampleEnd();
+
+ assertEquals(200, response.statusCode());
+ assertEquals(HttpClient.Version.HTTP_2, response.version());
+ assertTrue(result.getConnectTime() > 0,
+ "connectTime should be greater than 0, but was " + result.getConnectTime());
+ assertTrue(result.getConnectTime() <= result.getTime(),
+ "connectTime should not exceed the elapsed time");
+ } finally {
+ server.stop();
+ }
+ }
+
+ private static SSLContext trustAllContext() throws Exception {
+ TrustManager trustAll = new X509ExtendedTrustManager() {
+ @Override
+ public void checkClientTrusted(X509Certificate[] chain, String authType) {
+ // trust everything in the test
+ }
+
+ @Override
+ public void checkServerTrusted(X509Certificate[] chain, String authType) {
+ // trust everything in the test
+ }
+
+ @Override
+ public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) {
+ // trust everything in the test
+ }
+
+ @Override
+ public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) {
+ // trust everything in the test
+ }
+
+ @Override
+ public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
+ // trust everything in the test
+ }
+
+ @Override
+ public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
+ // trust everything in the test
+ }
+
+ @Override
+ public X509Certificate[] getAcceptedIssuers() {
+ return new X509Certificate[0];
+ }
+ };
+ SSLContext context = SSLContext.getInstance("TLS");
+ context.init(null, new TrustManager[] { trustAll }, null);
+ return context;
+ }
+
+ private static HTTPSamplerBase newSampler() {
+ return HTTPSamplerFactory.newInstance("Java");
+ }
+
+ private static WireMockServer createServer() {
+ return new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
+ }
+}
diff --git a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPSamplerFactory.java b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPSamplerFactory.java
new file mode 100644
index 00000000000..a617f13df1d
--- /dev/null
+++ b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPSamplerFactory.java
@@ -0,0 +1,45 @@
+/*
+ * 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 org.apache.jmeter.protocol.http.sampler;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Arrays;
+
+import org.junit.jupiter.api.Test;
+
+public class TestHTTPSamplerFactory {
+
+ @Test
+ void httpClient5IsSelectable() {
+ assertTrue(Arrays.asList(HTTPSamplerFactory.getImplementations()).contains("HttpClient5"));
+
+ HTTPSamplerBase sampler = HTTPSamplerFactory.newInstance("HttpClient5");
+
+ assertEquals("HttpClient5", sampler.getImplementation());
+ assertInstanceOf(HTTPHC5Impl.class, HTTPSamplerFactory.getImplementation(sampler.getImplementation(), sampler));
+ }
+
+ @Test
+ void unknownImplementationIsRejected() {
+ assertThrows(IllegalArgumentException.class, () -> HTTPSamplerFactory.newInstance("HttpClient6"));
+ }
+}
diff --git a/xdocs/usermanual/component_reference.xml b/xdocs/usermanual/component_reference.xml
index 663b24fe6e4..a05c368e344 100644
--- a/xdocs/usermanual/component_reference.xml
+++ b/xdocs/usermanual/component_reference.xml
@@ -142,6 +142,7 @@ Latency is set to the time it takes to login.
Javauses the HTTP implementation provided by the JVM.
This has some limitations in comparison with the HttpClient implementations - see below.
HTTPClient4uses Apache HttpComponents HttpClient 4.x.
+ HTTPClient5uses Apache HttpComponents HttpClient 5.x.
Blank Value does not set implementation on HTTP Samplers, so relies on HTTP Request Defaults if present or on jmeter.httpsampler property defined in jmeter.properties
@@ -241,13 +242,16 @@ https.default.protocol=SSLv3
Port the proxy server is listening to.
(Optional) username for proxy server.
(Optional) password for proxy server. (N.B. this is stored unencrypted in the test plan)
- Java, HttpClient4.
+ Java, HttpClient4, HttpClient5.
If not specified (and not defined by HTTP Request Defaults), the default depends on the value of the JMeter property
jmeter.httpsampler, failing that, the HttpClient4 implementation is used.
+ HTTP/1.1 or HTTP/2. Applies to the
+ HttpClient5 and Java implementations. An empty value uses the httpclient.version property, which
+ defaults to HTTP/1.1.
HTTP, HTTPS or FILE. Default: HTTP
GET, POST, HEAD, TRACE,
OPTIONS, PUT, DELETE, PATCH (not supported for
- JAVA implementation). With HttpClient4, the following methods related to WebDav are
+ JAVA implementation). With HttpClient4 or HttpClient5, the following methods related to WebDav are
also allowed: COPY, LOCK, MKCOL, MOVE,
PROPFIND, PROPPATCH, UNLOCK, REPORT, MKCALENDAR,
SEARCH.
@@ -484,7 +488,7 @@ so the value may be greater than the number of bytes in the response content.
Retry handling
-By default retry has been set to 0 for both HttpClient4 and Java implementations, meaning no retry is attempted.
+By default retry has been set to 0 for HttpClient4, HttpClient5 and Java implementations, meaning no retry is attempted.
For HttpClient4, the retry count can be overridden by setting the relevant JMeter property, for example:
@@ -3623,7 +3627,7 @@ By default, a Graphite implementation is provided.
DNS Cache Manager is designed for using in the root of Thread Group or Test Plan. Do not place it as child element of particular HTTP Sampler
- DNS Cache Manager works only with HTTP requests using HTTPClient4 implementation.
+ DNS Cache Manager works only with HTTP requests using HTTPClient4 or HTTPClient5 implementation.
The DNS Cache Manager element allows to test applications, which have several servers behind load balancers (CDN, etc.),
when user receives content from different IP's. By default JMeter uses JVM DNS cache. That's why
only one server from the cluster receives load. DNS Cache Manager resolves names for each thread separately each iteration and
@@ -3649,7 +3653,7 @@ By default, a Graphite implementation is provided.
The IP address for the test server will be looked up by using the custom DNS resolver. When none is given, the
system DNS resolver will be used.
- Now you can use www.example.com in your HTTPClient4 samplers and the requests will be made against
+
Now you can use www.example.com in your HTTPClient4 or HTTPClient5 samplers and the requests will be made against
a123.another.example.org with all headers set to www.example.com.
@@ -3683,14 +3687,14 @@ transmits the login information when it encounters this type of page.
The Authorization headers may not be shown in the Tree View Listener "Request" tab.
The Java implementation does pre-emptive authentication, but it does not
return the Authorization header when JMeter fetches the headers.
-The HttpComponents (HC 4.5.X) implementation defaults to pre-emptive since 3.2 and the header will be shown.
-To disable this, set the values as below, in which case authentication will only be performed in response to a challenge.
+The HttpComponents HC 4.5.X and HC 5.X implementations use pre-emptive Basic authentication and the header will be shown.
+To disable this for HttpClient4, set the value below, in which case authentication will only be performed in response to a challenge.
In the file jmeter.properties set httpclient4.auth.preemptive=false
-Note: the above settings only apply to the HttpClient sampler.
+Note: the above setting applies only to HttpClient4.
When looking for a match against a URL, JMeter checks each entry in turn, and stops when it finds the first match.
@@ -3719,6 +3723,7 @@ information for the user named, "jmeter".
Java BASIC
HttpClient 4 BASIC, DIGEST and Kerberos
+HttpClient 5 BASIC and DIGEST
@@ -3912,7 +3917,7 @@ All port values are treated equally; a sampler that does not specify a port will
Port the web server is listening to.
Connection Timeout. Number of milliseconds to wait for a connection to open.
Response Timeout. Number of milliseconds to wait for a response.
- Java, HttpClient4.
+ Java, HttpClient4, HttpClient5.
If not specified the default depends on the value of the JMeter property
jmeter.httpsampler, failing that, the Java implementation is used.
HTTP or HTTPS.
diff --git a/xdocs/usermanual/properties_reference.xml b/xdocs/usermanual/properties_reference.xml
index a40ae5027a6..721a41d46bf 100644
--- a/xdocs/usermanual/properties_reference.xml
+++ b/xdocs/usermanual/properties_reference.xml
@@ -442,8 +442,9 @@ JMETER-SERVER
Defaults to: 0
- Set the http version.
- Defaults to: 1.1 (or use the parameter http.protocol.version)
+ Set the default HTTP version for HttpClient5 and Java samplers with an empty HTTP Version value.
+ Valid values are HTTP/1.1 and HTTP/2.
+ Defaults to: HTTP/1.1
Set characters per second to a value greater then zero to emulate slow connections.
@@ -796,6 +797,8 @@ JMETER-SERVER
HTTPSampler2
HttpClient4
Use Apache HTTPClient version 4
+ HttpClient5
+ Use Apache HTTPClient version 5
Defaults to: HttpClient4