|
28 | 28 | import org.openqa.selenium.internal.Either;
|
29 | 29 | import org.openqa.selenium.internal.Require;
|
30 | 30 | import org.openqa.selenium.remote.http.ClientConfig;
|
| 31 | +import org.openqa.selenium.remote.http.Filter; |
31 | 32 | import org.openqa.selenium.remote.http.HttpClient;
|
32 | 33 | import org.openqa.selenium.remote.http.HttpHandler;
|
33 | 34 | import org.openqa.selenium.remote.http.HttpRequest;
|
34 | 35 | import org.openqa.selenium.remote.http.HttpResponse;
|
35 | 36 | import org.openqa.selenium.remote.service.DriverService;
|
36 | 37 |
|
37 | 38 | import java.io.ByteArrayInputStream;
|
| 39 | +import java.io.Closeable; |
38 | 40 | import java.io.IOException;
|
39 | 41 | import java.io.UncheckedIOException;
|
40 | 42 | import java.net.URI;
|
|
55 | 57 | import java.util.stream.StreamSupport;
|
56 | 58 |
|
57 | 59 | import static java.nio.charset.StandardCharsets.UTF_8;
|
| 60 | +import static java.util.logging.Level.WARNING; |
58 | 61 | import static org.openqa.selenium.internal.Debug.getDebugLogLevel;
|
59 | 62 | import static org.openqa.selenium.remote.DriverCommand.QUIT;
|
| 63 | +import static org.openqa.selenium.remote.http.HttpMethod.DELETE; |
60 | 64 |
|
61 | 65 | /**
|
62 | 66 | * Create a new Selenium session using the W3C WebDriver protocol. This class will not generate any
|
@@ -325,8 +329,11 @@ private WebDriver getRemoteDriver() {
|
325 | 329 | driverClientConfig = driverClientConfig.baseUri(baseUri);
|
326 | 330 | }
|
327 | 331 |
|
328 |
| - HttpHandler handler = Require.nonNull("Http handler", handlerFactory.apply(driverClientConfig)) |
329 |
| - .with(new AddWebDriverSpecHeaders().andThen(new ErrorFilter())); |
| 332 | + HttpHandler client = handlerFactory.apply(driverClientConfig); |
| 333 | + HttpHandler handler = Require.nonNull("Http handler", client) |
| 334 | + .with(new CloseHttpClientFilter(client) |
| 335 | + .andThen(new AddWebDriverSpecHeaders()) |
| 336 | + .andThen(new ErrorFilter())); |
330 | 337 |
|
331 | 338 | byte[] payload = getPayloadUtf8Bytes();
|
332 | 339 | Either<ProtocolHandshake.Result, String> result = new ProtocolHandshake().createSession(
|
@@ -442,4 +449,34 @@ private byte[] getPayloadUtf8Bytes() {
|
442 | 449 | throw new SessionNotCreatedException("Unable to create session roughPayload", e);
|
443 | 450 | }
|
444 | 451 | }
|
| 452 | + |
| 453 | + private static class CloseHttpClientFilter implements Filter { |
| 454 | + |
| 455 | + private final HttpHandler client; |
| 456 | + |
| 457 | + CloseHttpClientFilter(HttpHandler client) { |
| 458 | + this.client = Require.nonNull("Http client", client); |
| 459 | + } |
| 460 | + |
| 461 | + @Override |
| 462 | + public HttpHandler apply(HttpHandler next) { |
| 463 | + return req -> { |
| 464 | + try { |
| 465 | + return next.execute(req); |
| 466 | + } finally { |
| 467 | + if (req.getMethod() == DELETE && client instanceof Closeable) { |
| 468 | + HttpSessionId.getSessionId(req.getUri()).ifPresent(id -> { |
| 469 | + if (("/session/" + id).equals(req.getUri())) { |
| 470 | + try { |
| 471 | + ((Closeable) client).close(); |
| 472 | + } catch (IOException e) { |
| 473 | + LOG.log(WARNING, "Exception swallowed while closing http client", e); |
| 474 | + } |
| 475 | + } |
| 476 | + }); |
| 477 | + } |
| 478 | + } |
| 479 | + }; |
| 480 | + } |
| 481 | + } |
445 | 482 | }
|
0 commit comments