28
28
import org .apache .http .client .config .RequestConfig ;
29
29
import org .apache .http .client .methods .AbortableHttpRequest ;
30
30
import org .apache .http .client .utils .URIUtils ;
31
+ import org .apache .http .config .SocketConfig ;
31
32
import org .apache .http .entity .InputStreamEntity ;
32
33
import org .apache .http .impl .client .HttpClientBuilder ;
33
34
import org .apache .http .message .BasicHeader ;
49
50
import java .util .BitSet ;
50
51
import java .util .Enumeration ;
51
52
import java .util .Formatter ;
52
- import java .util .List ;
53
53
54
54
/**
55
55
* An HTTP reverse proxy/gateway servlet. It is designed to be extended for customization
@@ -91,10 +91,10 @@ public class ProxyServlet extends HttpServlet {
91
91
92
92
/** A integer parameter name to set the socket read timeout (millis) */
93
93
public static final String P_READTIMEOUT = "http.read.timeout" ;
94
-
94
+
95
95
/** A boolean parameter whether to use JVM-defined system properties to configure various networking aspects. */
96
96
public static final String P_USESYSTEMPROPERTIES = "useSystemProperties" ;
97
-
97
+
98
98
/** The parameter name for the target (destination) URI to proxy to. */
99
99
protected static final String P_TARGET_URI = "targetUri" ;
100
100
protected static final String ATTR_TARGET_URI =
@@ -177,7 +177,7 @@ public void init() throws ServletException {
177
177
if (connectTimeoutString != null ) {
178
178
this .connectTimeout = Integer .parseInt (connectTimeoutString );
179
179
}
180
-
180
+
181
181
String readTimeoutString = getConfigParam (P_READTIMEOUT );
182
182
if (readTimeoutString != null ) {
183
183
this .readTimeout = Integer .parseInt (readTimeoutString );
@@ -190,7 +190,7 @@ public void init() throws ServletException {
190
190
191
191
initTarget ();//sets target*
192
192
193
- proxyClient = createHttpClient (buildRequestConfig () );
193
+ proxyClient = createHttpClient ();
194
194
}
195
195
196
196
/**
@@ -205,6 +205,20 @@ protected RequestConfig buildRequestConfig() {
205
205
.build ();
206
206
}
207
207
208
+ /**
209
+ * Sub-classes can override specific behaviour of {@link org.apache.http.config.SocketConfig}.
210
+ */
211
+ protected SocketConfig buildSocketConfig () {
212
+
213
+ if (readTimeout < 1 ) {
214
+ return null ;
215
+ }
216
+
217
+ return SocketConfig .custom ()
218
+ .setSoTimeout (readTimeout )
219
+ .build ();
220
+ }
221
+
208
222
protected void initTarget () throws ServletException {
209
223
targetUri = getConfigParam (P_TARGET_URI );
210
224
if (targetUri == null )
@@ -223,16 +237,18 @@ protected void initTarget() throws ServletException {
223
237
* HttpClient offers many opportunities for customization.
224
238
* In any case, it should be thread-safe.
225
239
*/
226
- protected HttpClient createHttpClient (final RequestConfig requestConfig ) {
227
- HttpClientBuilder clientBuilder = HttpClientBuilder .create ().setDefaultRequestConfig (requestConfig );
240
+ protected HttpClient createHttpClient () {
241
+ HttpClientBuilder clientBuilder = HttpClientBuilder .create ()
242
+ .setDefaultRequestConfig (buildRequestConfig ())
243
+ .setDefaultSocketConfig (buildSocketConfig ());
228
244
if (useSystemProperties )
229
245
clientBuilder = clientBuilder .useSystemProperties ();
230
246
return clientBuilder .build ();
231
247
}
232
248
233
249
/**
234
250
* The http client used.
235
- * @see #createHttpClient(RequestConfig )
251
+ * @see #createHttpClient()
236
252
*/
237
253
protected HttpClient getProxyClient () {
238
254
return proxyClient ;
@@ -392,8 +408,8 @@ protected void closeQuietly(Closeable closeable) {
392
408
}
393
409
}
394
410
395
- /**
396
- * Copy request headers from the servlet client to the proxy request.
411
+ /**
412
+ * Copy request headers from the servlet client to the proxy request.
397
413
* This is easily overridden to add your own.
398
414
*/
399
415
protected void copyRequestHeaders (HttpServletRequest servletRequest , HttpRequest proxyRequest ) {
0 commit comments