@@ -226,58 +226,60 @@ public String forward(SeleniumBasedRequest request, HttpServletResponse response
226
226
227
227
HttpResponse proxyResponse = sendRequestToNode (proxyRequest );
228
228
lastActivity = timeSource .currentTimeInMillis ();
229
-
230
- final int statusCode = proxyResponse .getStatusLine ().getStatusCode ();
231
- response .setStatus (statusCode );
232
- processResponseHeaders (request , response , slot .getRemoteURL (), proxyResponse );
233
-
234
- byte [] consumedNewWebDriverSessionBody = null ;
235
- if (statusCode != HttpServletResponse .SC_INTERNAL_SERVER_ERROR &&
236
- statusCode != HttpServletResponse .SC_NOT_FOUND ) {
237
- consumedNewWebDriverSessionBody = updateHubIfNewWebDriverSession (request , proxyResponse );
238
- }
239
- if (newSessionRequest && statusCode == HttpServletResponse .SC_INTERNAL_SERVER_ERROR ) {
240
- removeIncompleteNewSessionRequest ();
241
- }
242
- if (statusCode == HttpServletResponse .SC_NOT_FOUND ) {
243
- removeSessionBrowserTimeout ();
244
- }
245
-
246
229
HttpEntity responseBody = proxyResponse .getEntity ();
247
- byte [] contentBeingForwarded = null ;
248
- if (responseBody != null ) {
249
- try {
250
- InputStream in ;
251
- if (consumedNewWebDriverSessionBody == null ) {
252
- in = responseBody .getContent ();
253
- if (request .getRequestType () == RequestType .START_SESSION
254
- && request instanceof LegacySeleniumRequest ) {
255
- res = getResponseUtf8Content (in );
256
-
257
- updateHubNewSeleniumSession (res );
230
+ try {
231
+ final int statusCode = proxyResponse .getStatusLine ().getStatusCode ();
232
+ response .setStatus (statusCode );
233
+ processResponseHeaders (request , response , slot .getRemoteURL (), proxyResponse );
234
+
235
+ byte [] consumedNewWebDriverSessionBody = null ;
236
+ if (statusCode != HttpServletResponse .SC_INTERNAL_SERVER_ERROR &&
237
+ statusCode != HttpServletResponse .SC_NOT_FOUND ) {
238
+ consumedNewWebDriverSessionBody = updateHubIfNewWebDriverSession (request , proxyResponse );
239
+ }
240
+ if (newSessionRequest && statusCode == HttpServletResponse .SC_INTERNAL_SERVER_ERROR ) {
241
+ removeIncompleteNewSessionRequest ();
242
+ }
243
+ if (statusCode == HttpServletResponse .SC_NOT_FOUND ) {
244
+ removeSessionBrowserTimeout ();
245
+ }
258
246
259
- in = new ByteArrayInputStream (res .getBytes ("UTF-8" ));
247
+ byte [] contentBeingForwarded = null ;
248
+ if (responseBody != null ) {
249
+ try {
250
+ InputStream in ;
251
+ if (consumedNewWebDriverSessionBody == null ) {
252
+ in = responseBody .getContent ();
253
+ if (request .getRequestType () == RequestType .START_SESSION
254
+ && request instanceof LegacySeleniumRequest ) {
255
+ res = getResponseUtf8Content (in );
256
+
257
+ updateHubNewSeleniumSession (res );
258
+
259
+ in = new ByteArrayInputStream (res .getBytes ("UTF-8" ));
260
+ }
261
+ } else {
262
+ in = new ByteArrayInputStream (consumedNewWebDriverSessionBody );
260
263
}
261
- } else {
262
- in = new ByteArrayInputStream (consumedNewWebDriverSessionBody );
263
- }
264
264
265
- final byte [] bytes = drainInputStream (in );
266
- writeRawBody (response , bytes );
265
+ final byte [] bytes = drainInputStream (in );
266
+ writeRawBody (response , bytes );
267
267
268
- contentBeingForwarded = bytes ;
269
- } finally {
270
- EntityUtils .consume (responseBody );
271
- }
268
+ } finally {
269
+ EntityUtils .consume (responseBody );
270
+ }
272
271
273
- }
272
+ }
274
273
275
- if (slot .getProxy () instanceof CommandListener ) {
276
- SeleniumBasedResponse wrappedResponse = new SeleniumBasedResponse (response );
277
- wrappedResponse .setForwardedContent (contentBeingForwarded );
278
- ((CommandListener ) slot .getProxy ()).afterCommand (this , request , wrappedResponse );
274
+ if (slot .getProxy () instanceof CommandListener ) {
275
+ SeleniumBasedResponse wrappedResponse = new SeleniumBasedResponse (response );
276
+ wrappedResponse .setForwardedContent (contentBeingForwarded );
277
+ ((CommandListener ) slot .getProxy ()).afterCommand (this , request , wrappedResponse );
278
+ }
279
+ response .flushBuffer ();
280
+ } finally {
281
+ EntityUtils .consume (responseBody );
279
282
}
280
-
281
283
response .flushBuffer ();
282
284
283
285
return res ;
@@ -547,17 +549,24 @@ public boolean sendDeleteSessionRequest() {
547
549
}
548
550
549
551
HttpHost host = new HttpHost (remoteURL .getHost (), remoteURL .getPort ());
550
-
552
+ HttpEntity responseBody = null ;
551
553
boolean ok ;
552
554
try {
553
555
HttpClient client = getClient ();
554
556
HttpResponse response = client .execute (host , request );
557
+ responseBody = response .getEntity ();
555
558
int code = response .getStatusLine ().getStatusCode ();
556
559
ok = (code >= 200 ) && (code <= 299 );
557
560
} catch (Throwable e ) {
558
561
ok = false ;
559
562
// corrupted or the something else already sent the DELETE.
560
563
log .severe ("Error releasing. Server corrupted ?" );
564
+ }finally {
565
+ try {
566
+ EntityUtils .consume (responseBody );
567
+ } catch (IOException e ) {
568
+ log .warning ("Consuming the response body when DELETE to the node" + e .getMessage ());
569
+ }
561
570
}
562
571
return ok ;
563
572
}
0 commit comments