Skip to content

Commit 8c74b1b

Browse files
author
mildis
committed
Force port only if not using scheme default
HttpClient issue the Host header with the port whenever the port is defined. This patch only set the port when not using the default scheme port (80 for http and 443 for https) scheme=http, host=example.com, port=80 => Host: example.com scheme=https, host=example.com, port=443 => Host: example.com scheme=http, host=example.com, port=8080 => Host: example.com:8080 scheme=https, host=example.com, port=8443 => Host: example.com:8443 Change-Id: I19d238c1abd871d2f641a2643770ab8c78594d6f
1 parent 5a7e763 commit 8c74b1b

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/main/java/fr/techad/sonar/gerrit/network/rest/GerritRestConnector.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,25 @@ public String setReview(String reviewInputAsJson) throws IOException {
8282
// Example
8383
// http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java
8484
private void createHttpContext() {
85-
httpHost = new HttpHost(gerritConfiguration.getHost(), gerritConfiguration.getPort(),
86-
gerritConfiguration.getScheme());
85+
if ((GerritConstants.SCHEME_HTTPS.equals(gerritConfiguration.getScheme())
86+
&& 443 == gerritConfiguration.getPort())
87+
|| (GerritConstants.SCHEME_HTTP.equals(gerritConfiguration.getScheme())
88+
&& 80 == gerritConfiguration.getPort())) {
89+
httpHost = new HttpHost(gerritConfiguration.getHost(), -1, gerritConfiguration.getScheme());
90+
} else {
91+
httpHost = new HttpHost(gerritConfiguration.getHost(), gerritConfiguration.getPort(),
92+
gerritConfiguration.getScheme());
93+
}
8794
httpClientContext = HttpClientContext.create();
8895

8996
if (gerritConfiguration.isAnonymous()) {
9097
httpClient = HttpClients.createDefault();
9198
} else {
9299
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
93100
credentialsProvider.setCredentials(
94-
new AuthScope(gerritConfiguration.getHost(), gerritConfiguration.getPort()),
95-
new UsernamePasswordCredentials(gerritConfiguration.getUsername(),
96-
gerritConfiguration.getPassword()));
101+
new AuthScope(gerritConfiguration.getHost(), gerritConfiguration.getPort()),
102+
new UsernamePasswordCredentials(gerritConfiguration.getUsername(),
103+
gerritConfiguration.getPassword()));
97104
httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
98105

99106
BasicAuthCache basicAuthCache = new BasicAuthCache();
@@ -106,7 +113,7 @@ private void createHttpContext() {
106113

107114
} else {
108115
LOG.error("[GERRIT PLUGIN] createHttpContext called with AUTH_SCHEME {} instead of digest or basic",
109-
gerritConfiguration.getHttpAuthScheme());
116+
gerritConfiguration.getHttpAuthScheme());
110117
}
111118
basicAuthCache.put(httpHost, authScheme);
112119
httpClientContext.setAuthCache(basicAuthCache);
@@ -161,7 +168,7 @@ public String rootUriBuilder() {
161168
uri = uri.concat(URI_AUTH_PREFIX);
162169
}
163170
uri = uri.concat(String.format(URI_CHANGES, encode(gerritConfiguration.getProjectName()),
164-
encode(gerritConfiguration.getBranchName()), encode(gerritConfiguration.getChangeId())));
171+
encode(gerritConfiguration.getBranchName()), encode(gerritConfiguration.getChangeId())));
165172
uri = uri.concat(String.format(URI_REVISIONS, encode(gerritConfiguration.getRevisionId())));
166173

167174
LOG.debug("[GERRIT PLUGIN] Built URI : {}", uri);

0 commit comments

Comments
 (0)