Skip to content

Commit

Permalink
Force port only if not using scheme default
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mildis committed Oct 13, 2018
1 parent 5a7e763 commit 8c74b1b
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,25 @@ public String setReview(String reviewInputAsJson) throws IOException {
// Example
// http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java
private void createHttpContext() {
httpHost = new HttpHost(gerritConfiguration.getHost(), gerritConfiguration.getPort(),
gerritConfiguration.getScheme());
if ((GerritConstants.SCHEME_HTTPS.equals(gerritConfiguration.getScheme())
&& 443 == gerritConfiguration.getPort())
|| (GerritConstants.SCHEME_HTTP.equals(gerritConfiguration.getScheme())
&& 80 == gerritConfiguration.getPort())) {
httpHost = new HttpHost(gerritConfiguration.getHost(), -1, gerritConfiguration.getScheme());
} else {
httpHost = new HttpHost(gerritConfiguration.getHost(), gerritConfiguration.getPort(),
gerritConfiguration.getScheme());
}
httpClientContext = HttpClientContext.create();

if (gerritConfiguration.isAnonymous()) {
httpClient = HttpClients.createDefault();
} else {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
new AuthScope(gerritConfiguration.getHost(), gerritConfiguration.getPort()),
new UsernamePasswordCredentials(gerritConfiguration.getUsername(),
gerritConfiguration.getPassword()));
new AuthScope(gerritConfiguration.getHost(), gerritConfiguration.getPort()),
new UsernamePasswordCredentials(gerritConfiguration.getUsername(),
gerritConfiguration.getPassword()));
httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();

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

} else {
LOG.error("[GERRIT PLUGIN] createHttpContext called with AUTH_SCHEME {} instead of digest or basic",
gerritConfiguration.getHttpAuthScheme());
gerritConfiguration.getHttpAuthScheme());
}
basicAuthCache.put(httpHost, authScheme);
httpClientContext.setAuthCache(basicAuthCache);
Expand Down Expand Up @@ -161,7 +168,7 @@ public String rootUriBuilder() {
uri = uri.concat(URI_AUTH_PREFIX);
}
uri = uri.concat(String.format(URI_CHANGES, encode(gerritConfiguration.getProjectName()),
encode(gerritConfiguration.getBranchName()), encode(gerritConfiguration.getChangeId())));
encode(gerritConfiguration.getBranchName()), encode(gerritConfiguration.getChangeId())));
uri = uri.concat(String.format(URI_REVISIONS, encode(gerritConfiguration.getRevisionId())));

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

0 comments on commit 8c74b1b

Please sign in to comment.