Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfbbb2bf

Browse files
committed
Merge pull requestAsyncHttpClient#1158 from gsfernandes/1152
proxy-authorization not working when using an http proxy while requesting a https url,closeAsyncHttpClient#1152
2 parents89ff65b +f8fa536 commitfbbb2bf

File tree

7 files changed

+390
-1
lines changed

7 files changed

+390
-1
lines changed

‎src/main/java/com/ning/http/util/AuthenticatorUtils.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static String perRequestProxyAuthorizationHeader(Request request,
123123

124124
StringproxyAuthorization =null;
125125

126-
if (!connect &&proxyServer !=null &&proxyServer.getPrincipal() !=null
126+
if (connect &&proxyServer !=null &&proxyServer.getPrincipal() !=null
127127
&&proxyServer.getScheme() ==Realm.AuthScheme.BASIC) {
128128
proxyAuthorization =computeBasicAuthentication(proxyServer);
129129

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright (c) 2016 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
packagecom.ning.http.client.async;
15+
16+
importcom.ning.http.client.AsyncHttpClient;
17+
importcom.ning.http.client.AsyncHttpClientConfig;
18+
importcom.ning.http.client.ProxyServer;
19+
importcom.ning.http.client.Realm;
20+
importcom.ning.http.client.Realm.AuthScheme;
21+
importcom.ning.http.client.Request;
22+
importcom.ning.http.client.RequestBuilder;
23+
importcom.ning.http.client.Response;
24+
25+
importjava.io.IOException;
26+
importjava.net.UnknownHostException;
27+
importjava.util.concurrent.ExecutionException;
28+
importjava.util.concurrent.Future;
29+
30+
importjavax.servlet.ServletException;
31+
importjavax.servlet.http.HttpServletRequest;
32+
importjavax.servlet.http.HttpServletResponse;
33+
34+
importorg.eclipse.jetty.server.Connector;
35+
importorg.eclipse.jetty.server.Server;
36+
importorg.eclipse.jetty.server.handler.AbstractHandler;
37+
importorg.eclipse.jetty.server.nio.SelectChannelConnector;
38+
importorg.testng.Assert;
39+
importorg.testng.annotations.AfterClass;
40+
importorg.testng.annotations.BeforeClass;
41+
importorg.testng.annotations.Test;
42+
43+
/**
44+
* Test that validates that when having an HTTP proxy and trying to access an HTTP through the proxy the
45+
* proxy credentials should be passed after it gets a 407 response.
46+
*/
47+
publicabstractclassBasicHttpProxyToHttpTestextendsAbstractBasicTest {
48+
49+
privateServerserver2;
50+
51+
publicstaticclassProxyHTTPHandlerextendsAbstractHandler {
52+
53+
@Override
54+
publicvoidhandle(StringpathInContext,org.eclipse.jetty.server.Requestrequest,HttpServletRequesthttpRequest,
55+
HttpServletResponsehttpResponse)throwsIOException,ServletException {
56+
57+
Stringauthorization =httpRequest.getHeader("Proxy-Authorization");
58+
if (authorization ==null) {
59+
httpResponse.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
60+
httpResponse.setHeader("Proxy-Authenticate","Basic realm=\"Fake Realm\"");
61+
}elseif (authorization
62+
.equals("Basic am9obmRvZTpwYXNz")) {
63+
httpResponse.addHeader("target",request.getUri().toString());
64+
httpResponse.setStatus(HttpServletResponse.SC_OK);
65+
}else {
66+
httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
67+
}
68+
httpResponse.getOutputStream().flush();
69+
httpResponse.getOutputStream().close();
70+
request.setHandled(true);
71+
}
72+
}
73+
74+
@AfterClass(alwaysRun =true)
75+
publicvoidtearDownGlobal()throwsException {
76+
server.stop();
77+
server2.stop();
78+
}
79+
80+
@BeforeClass(alwaysRun =true)
81+
publicvoidsetUpGlobal()throwsException {
82+
// HTTP Server
83+
server =newServer();
84+
// HTTP Proxy Server
85+
server2 =newServer();
86+
87+
port1 =findFreePort();
88+
port2 =findFreePort();
89+
90+
// HTTP Server
91+
Connectorlistener =newSelectChannelConnector();
92+
93+
listener.setHost("127.0.0.1");
94+
listener.setPort(port1);
95+
server.addConnector(listener);
96+
server.setHandler(newEchoHandler());
97+
server.start();
98+
99+
listener =newSelectChannelConnector();
100+
101+
// Proxy Server configuration
102+
listener.setHost("127.0.0.1");
103+
listener.setPort(port2);
104+
server2.addConnector(listener);
105+
server2.setHandler(configureHandler());
106+
server2.start();
107+
log.info("Local HTTP Server (" +port1 +"), HTTPS Server (" +port2 +") started successfully");
108+
}
109+
110+
111+
@Override
112+
publicAbstractHandlerconfigureHandler()throwsException {
113+
returnnewProxyHTTPHandler();
114+
}
115+
116+
@Test
117+
publicvoidhttpProxyToHttpTargetTest()throwsIOException,InterruptedException,ExecutionException {
118+
try (AsyncHttpClientclient =getAsyncHttpClient(newAsyncHttpClientConfig.Builder().build())) {
119+
Requestrequest =newRequestBuilder("GET").setProxyServer(basicProxy()).setUrl(getTargetUrl()).setRealm(newRealm.RealmBuilder().setPrincipal("user").setPassword("passwd").build()).build();
120+
Future<Response>responseFuture =client.executeRequest(request);
121+
Responseresponse =responseFuture.get();
122+
Assert.assertEquals(response.getStatusCode(),HttpServletResponse.SC_OK);
123+
Assert.assertEquals(getTargetUrl(),response.getHeader("target"));
124+
}
125+
}
126+
127+
privateProxyServerbasicProxy()throwsUnknownHostException {
128+
ProxyServerproxyServer =newProxyServer("127.0.0.1",port2,"johndoe","pass");
129+
proxyServer.setScheme(AuthScheme.BASIC);
130+
returnproxyServer;
131+
}
132+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright (c) 2016 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
packagecom.ning.http.client.async;
15+
16+
importcom.ning.http.client.AsyncHttpClient;
17+
importcom.ning.http.client.AsyncHttpClientConfig;
18+
importcom.ning.http.client.ProxyServer;
19+
importcom.ning.http.client.Realm;
20+
importcom.ning.http.client.Realm.AuthScheme;
21+
importcom.ning.http.client.Request;
22+
importcom.ning.http.client.RequestBuilder;
23+
importcom.ning.http.client.Response;
24+
25+
importjava.io.File;
26+
importjava.io.IOException;
27+
importjava.net.URL;
28+
importjava.net.UnknownHostException;
29+
importjava.security.NoSuchAlgorithmException;
30+
importjava.util.concurrent.ExecutionException;
31+
importjava.util.concurrent.Future;
32+
33+
importjavax.servlet.ServletException;
34+
importjavax.servlet.http.HttpServletRequest;
35+
importjavax.servlet.http.HttpServletResponse;
36+
37+
importorg.eclipse.jetty.server.Connector;
38+
importorg.eclipse.jetty.server.Server;
39+
importorg.eclipse.jetty.server.handler.AbstractHandler;
40+
importorg.eclipse.jetty.server.handler.ConnectHandler;
41+
importorg.eclipse.jetty.server.nio.SelectChannelConnector;
42+
importorg.eclipse.jetty.server.ssl.SslSocketConnector;
43+
importorg.testng.Assert;
44+
importorg.testng.annotations.AfterClass;
45+
importorg.testng.annotations.BeforeClass;
46+
importorg.testng.annotations.Test;
47+
48+
/**
49+
* Test that validates that when having an HTTP proxy and trying to access an HTTPS through the proxy the
50+
* proxy credentials should be passed during the CONNECT request.
51+
*/
52+
publicabstractclassBasicHttpProxyToHttpsTestextendsAbstractBasicTest {
53+
54+
privateServerserver2;
55+
56+
@AfterClass(alwaysRun =true)
57+
publicvoidtearDownGlobal()throwsException {
58+
server.stop();
59+
server2.stop();
60+
}
61+
62+
@BeforeClass(alwaysRun =true)
63+
publicvoidsetUpGlobal()throwsException {
64+
// HTTP Proxy Server
65+
server =newServer();
66+
// HTTPS Server
67+
server2 =newServer();
68+
69+
port1 =findFreePort();
70+
port2 =findFreePort();
71+
72+
// Proxy Server configuration
73+
Connectorlistener =newSelectChannelConnector();
74+
listener.setHost("127.0.0.1");
75+
listener.setPort(port1);
76+
server.addConnector(listener);
77+
server.setHandler(configureHandler());
78+
server.start();
79+
80+
// HTTPS Server
81+
SslSocketConnectorconnector =newSslSocketConnector();
82+
connector.setHost("127.0.0.1");
83+
connector.setPort(port2);
84+
85+
ClassLoadercl =getClass().getClassLoader();
86+
// override system properties
87+
URLkeystoreUrl =cl.getResource("ssltest-keystore.jks");
88+
StringkeyStoreFile =newFile(keystoreUrl.toURI()).getAbsolutePath();
89+
connector.setKeystore(keyStoreFile);
90+
connector.setKeyPassword("changeit");
91+
connector.setKeystoreType("JKS");
92+
93+
log.info("SSL keystore path: {}",keyStoreFile);
94+
95+
server2.addConnector(connector);
96+
server2.setHandler(newEchoHandler());
97+
server2.start();
98+
log.info("Local Proxy Server (" +port1 +"), HTTPS Server (" +port2 +") started successfully");
99+
}
100+
101+
@Override
102+
publicAbstractHandlerconfigureHandler()throwsException {
103+
returnnewConnectHandler(newEchoHandler()) {
104+
@Override
105+
protectedbooleanhandleAuthentication(HttpServletRequestrequest,HttpServletResponseresponse,Stringaddress)throwsServletException,IOException {
106+
Stringauthorization =request.getHeader("Proxy-Authorization");
107+
if (authorization ==null) {
108+
response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
109+
response.setHeader("Proxy-Authenticate","Basic realm=\"Fake Realm\"");
110+
returnfalse;
111+
}elseif (authorization
112+
.equals("Basic am9obmRvZTpwYXNz")) {
113+
returntrue;
114+
}
115+
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
116+
returnfalse;
117+
}
118+
};
119+
}
120+
121+
@Test
122+
publicvoidhttpProxyToHttpsTargetTest()throwsIOException,InterruptedException,ExecutionException,NoSuchAlgorithmException {
123+
try (AsyncHttpClientclient =getAsyncHttpClient(newAsyncHttpClientConfig.Builder().setAcceptAnyCertificate(true).build())) {
124+
Requestrequest =newRequestBuilder("GET").setProxyServer(basicProxy()).setUrl(getTargetUrl2()).setRealm(newRealm.RealmBuilder().setPrincipal("user").setPassword("passwd").build()).build();
125+
Future<Response>responseFuture =client.executeRequest(request);
126+
Responseresponse =responseFuture.get();
127+
Assert.assertEquals(response.getStatusCode(),HttpServletResponse.SC_OK);
128+
Assert.assertEquals("127.0.0.1:" +port2,response.getHeader("x-host"));
129+
}
130+
}
131+
132+
privateProxyServerbasicProxy()throwsUnknownHostException {
133+
ProxyServerproxyServer =newProxyServer("127.0.0.1",port1,"johndoe","pass");
134+
proxyServer.setScheme(AuthScheme.BASIC);
135+
returnproxyServer;
136+
}
137+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2016 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
packagecom.ning.http.client.async.grizzly;
15+
16+
importcom.ning.http.client.AsyncHttpClient;
17+
importcom.ning.http.client.AsyncHttpClientConfig;
18+
importcom.ning.http.client.async.BasicHttpProxyToHttpTest;
19+
importcom.ning.http.client.async.ProviderUtil;
20+
21+
importorg.testng.annotations.Test;
22+
23+
@Test
24+
publicclassGrizzlyBasicHttpProxyToHttpTestextendsBasicHttpProxyToHttpTest {
25+
26+
@Override
27+
publicAsyncHttpClientgetAsyncHttpClient(AsyncHttpClientConfigconfig) {
28+
returnProviderUtil.grizzlyProvider(config);
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2016 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
packagecom.ning.http.client.async.grizzly;
15+
16+
importcom.ning.http.client.AsyncHttpClient;
17+
importcom.ning.http.client.AsyncHttpClientConfig;
18+
importcom.ning.http.client.async.BasicHttpProxyToHttpsTest;
19+
importcom.ning.http.client.async.ProviderUtil;
20+
21+
importorg.testng.annotations.Test;
22+
23+
@Test
24+
publicclassGrizzlyBasicHttpProxyToHttpsTestextendsBasicHttpProxyToHttpsTest {
25+
26+
@Override
27+
publicAsyncHttpClientgetAsyncHttpClient(AsyncHttpClientConfigconfig) {
28+
returnProviderUtil.grizzlyProvider(config);
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2016 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
packagecom.ning.http.client.async.netty;
15+
16+
importcom.ning.http.client.AsyncHttpClient;
17+
importcom.ning.http.client.AsyncHttpClientConfig;
18+
importcom.ning.http.client.async.BasicHttpProxyToHttpTest;
19+
importcom.ning.http.client.async.ProviderUtil;
20+
21+
importorg.testng.annotations.Test;
22+
23+
@Test
24+
publicclassNettyBasicHttpProxyToHttpTestextendsBasicHttpProxyToHttpTest {
25+
26+
@Override
27+
publicAsyncHttpClientgetAsyncHttpClient(AsyncHttpClientConfigconfig) {
28+
returnProviderUtil.nettyProvider(config);
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp