Skip to content

Commit

Permalink
Set Http protocol version
Browse files Browse the repository at this point in the history
Change allows for setting the highest http version protocol. Defaults to version 1.1
  • Loading branch information
jamesloveday committed May 11, 2021
1 parent f7f4dce commit 2299c2f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<property name="responseName" value="HTTP_RESPONSE" />
<property name="responseStatusName" value="HTTP_STATUS" />
<property name="contentTypeName" value="HTTP_CONTENT_TYPE" />
<property name="httpVersion" value="HTTP_VERSION"/>
</participant>

</http-client-txnmgr>
</http-client-txnmgr>
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import javax.net.ssl.X509TrustManager;
import javax.security.cert.CertificateExpiredException;

import org.apache.commons.codec.binary.StringUtils;
import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
Expand Down Expand Up @@ -101,6 +102,7 @@ public class HttpQuery extends Log implements AbortParticipant, Configurable, De
private String basicAuthenticationName;
private RedirectStrategy redirectStrategy;
private boolean ignoreNullRequest;
private String httpVersion;

// Shared clients for the instance.
// Created at configuration time; destroyed when this participant is destroyed.
Expand All @@ -121,6 +123,16 @@ public int prepare (long id, Serializable o) {

addHeaders(ctx, httpRequest);

//set the http protocol version, default to version 1.1
//config example <property name="httpVersion" value="1,1"/>
HttpVersion definedVersion = null;
httpVersion = getVersion(ctx);
if(httpVersion != null && httpVersion.length() > 0) {
String[] versions = httpVersion.split(",");
definedVersion = new HttpVersion(Integer.parseInt(versions[0]), Integer.parseInt(versions[1]));
}
httpRequest.setProtocolVersion((definedVersion != null) ? definedVersion : HttpVersion.HTTP_1_1);

httpRequest.setConfig(RequestConfig.custom().
setConnectTimeout(connectTimeout).
setSocketTimeout(timeout).
Expand Down Expand Up @@ -208,6 +220,7 @@ public void setConfiguration (Configuration cfg) throws ConfigurationException {
timeout= cfg.getInt("timeout", DEFAULT_TIMEOUT);

urlName = cfg.get("urlName", "HTTP_URL");
httpVersion = cfg.get("httpVersion", "HTTP_VERSION");
methodName = cfg.get("methodName", "HTTP_METHOD");
paramsName = cfg.get ("paramsName", "HTTP_PARAMS");
requestName = cfg.get ("requestName", "HTTP_REQUEST");
Expand Down Expand Up @@ -342,6 +355,10 @@ private HttpRequestBase getHttpRequest(Context ctx) {
return null;
}

private String getVersion(Context ctx) {
return ctx.getString(httpVersion);
}

private ContentType getContentType (Context ctx) {
return cfg.getBoolean("no-charset") ?
ContentType.create(ctx.get(contentTypeName, contentType)) :
Expand Down Expand Up @@ -406,4 +423,4 @@ private CloseableHttpAsyncClient destroyClient (CloseableHttpAsyncClient client)
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,27 @@ public void testTrustWrongHostCertificate() {
Integer sc = ctx.get ("HTTP_STATUS", 10000L);
assertEquals (Integer.valueOf(HttpStatus.SC_OK), sc, "Status code should be 200");
}
}

@Test
public void testHttpVersionDefault() {
Context ctx = new Context();
ctx.put("HTTP_URL", "https://untrusted-root.badssl.com/");
ctx.put("HTTP_METHOD", "GET");
ctx.put("HTTP_TRUST_ALL_CERTS", "true");
mgr.queue(ctx);
Integer sc = ctx.get ("HTTP_STATUS", 10000L);
assertEquals (Integer.valueOf(HttpStatus.SC_OK), sc, "Status code should be 200");
}

@Test
public void testHttpVersionSetValue() {
Context ctx = new Context();
ctx.put("HTTP_URL", "https://untrusted-root.badssl.com/");
ctx.put("HTTP_VERSION", "2,0");
ctx.put("HTTP_METHOD", "GET");
ctx.put("HTTP_TRUST_ALL_CERTS", "true");
mgr.queue(ctx);
Integer sc = ctx.get ("HTTP_STATUS", 10000L);
assertEquals (Integer.valueOf(HttpStatus.SC_OK), sc, "Status code should be 200");
}
}

0 comments on commit 2299c2f

Please sign in to comment.