forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[java] A draft of a Netty-based HTTP client
The default is still OkHttp. Pass -Dwebdriver.http.factory=netty to use the new one. Its stability is below my expectations yet. Everything works well if I run tests from IDEA in a single process, but there are random TimeoutException-s during a Bazel run.
- Loading branch information
Showing
23 changed files
with
451 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
java/client/src/org/openqa/selenium/remote/http/netty/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
java_library( | ||
name = "netty", | ||
srcs = glob(["*.java"]), | ||
visibility = [ | ||
"//java/client/src/org/openqa/selenium/remote:__pkg__", | ||
"//java/client/test/org/openqa/selenium/remote/http/netty:__pkg__", | ||
], | ||
deps = [ | ||
"//java/client/src/org/openqa/selenium/remote/http", | ||
"//third_party/java/guava", | ||
"//third_party/java/netty:netty-all", | ||
"//third_party/java/async-http-client", | ||
"//third_party/java/reactive-streams", | ||
], | ||
) |
36 changes: 36 additions & 0 deletions
36
java/client/src/org/openqa/selenium/remote/http/netty/CreateNettyClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.remote.http.netty; | ||
|
||
import static org.asynchttpclient.Dsl.asyncHttpClient; | ||
|
||
import org.asynchttpclient.AsyncHttpClient; | ||
import org.openqa.selenium.remote.http.ClientConfig; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
class CreateNettyClient implements Function<ClientConfig, AsyncHttpClient> { | ||
|
||
@Override | ||
public AsyncHttpClient apply(ClientConfig config) { | ||
Objects.requireNonNull(config, "Client config to use must be set."); | ||
|
||
return asyncHttpClient(); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
java/client/src/org/openqa/selenium/remote/http/netty/NettyClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.remote.http.netty; | ||
|
||
import org.openqa.selenium.remote.http.ClientConfig; | ||
import org.openqa.selenium.remote.http.Filter; | ||
import org.openqa.selenium.remote.http.HttpClient; | ||
import org.openqa.selenium.remote.http.HttpHandler; | ||
import org.openqa.selenium.remote.http.HttpRequest; | ||
import org.openqa.selenium.remote.http.HttpResponse; | ||
import org.openqa.selenium.remote.http.WebSocket; | ||
|
||
import java.io.UncheckedIOException; | ||
import java.util.Objects; | ||
import java.util.function.BiFunction; | ||
|
||
public class NettyClient implements HttpClient { | ||
|
||
private final HttpHandler handler; | ||
private BiFunction<HttpRequest, WebSocket.Listener, WebSocket> toWebSocket; | ||
|
||
private NettyClient(HttpHandler handler, BiFunction<HttpRequest, WebSocket.Listener, WebSocket> toWebSocket) { | ||
this.handler = Objects.requireNonNull(handler); | ||
this.toWebSocket = Objects.requireNonNull(toWebSocket); | ||
} | ||
|
||
@Override | ||
public HttpResponse execute(HttpRequest request) throws UncheckedIOException { | ||
return handler.execute(request); | ||
} | ||
|
||
@Override | ||
public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) { | ||
Objects.requireNonNull(request, "Request to send must be set."); | ||
Objects.requireNonNull(listener, "WebSocket listener must be set."); | ||
|
||
return toWebSocket.apply(request, listener); | ||
} | ||
|
||
@Override | ||
public HttpClient with(Filter filter) { | ||
Objects.requireNonNull(filter, "Filter to use must be set."); | ||
|
||
// TODO: We should probably ensure that websocket requests are run through the filter. | ||
return new NettyClient(handler.with(filter), toWebSocket); | ||
} | ||
|
||
public static class Factory implements HttpClient.Factory { | ||
|
||
@Override | ||
public HttpClient createClient(ClientConfig config) { | ||
Objects.requireNonNull(config, "Client config to use must be set."); | ||
|
||
return new NettyClient(new NettyHttpHandler(config).with(config.filter()), NettyWebSocket.create(config)); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
java/client/src/org/openqa/selenium/remote/http/netty/NettyHttpHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.remote.http.netty; | ||
|
||
import org.asynchttpclient.AsyncHttpClient; | ||
import org.asynchttpclient.Response; | ||
import org.openqa.selenium.remote.http.ClientConfig; | ||
import org.openqa.selenium.remote.http.HttpHandler; | ||
import org.openqa.selenium.remote.http.HttpRequest; | ||
import org.openqa.selenium.remote.http.HttpResponse; | ||
import org.openqa.selenium.remote.http.RemoteCall; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
|
||
public class NettyHttpHandler extends RemoteCall { | ||
|
||
private final AsyncHttpClient client; | ||
private final HttpHandler handler; | ||
|
||
public NettyHttpHandler(ClientConfig config) { | ||
super(config); | ||
this.client = new CreateNettyClient().apply(config); | ||
this.handler = config.filter().andFinally(this::makeCall); | ||
} | ||
|
||
@Override | ||
public HttpResponse execute(HttpRequest request) { | ||
return handler.execute(request); | ||
} | ||
|
||
private HttpResponse makeCall(HttpRequest request) { | ||
Objects.requireNonNull(request, "Request must be set."); | ||
|
||
Future<Response> whenResponse = client.executeRequest( | ||
NettyMessages.toNettyRequest(getConfig().baseUri(), request)); | ||
|
||
try { | ||
Response response = whenResponse.get(); | ||
return NettyMessages.toSeleniumResponse(response); | ||
} catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
java/client/src/org/openqa/selenium/remote/http/netty/NettyMessages.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.remote.http.netty; | ||
|
||
import static org.openqa.selenium.remote.http.Contents.empty; | ||
|
||
import org.asynchttpclient.Request; | ||
import org.asynchttpclient.RequestBuilder; | ||
import org.asynchttpclient.Response; | ||
import org.openqa.selenium.remote.http.Contents; | ||
import org.openqa.selenium.remote.http.HttpMethod; | ||
import org.openqa.selenium.remote.http.HttpRequest; | ||
import org.openqa.selenium.remote.http.HttpResponse; | ||
|
||
import java.net.URI; | ||
|
||
import static org.asynchttpclient.Dsl.request; | ||
|
||
class NettyMessages { | ||
|
||
private NettyMessages() { | ||
// Utility classes. | ||
} | ||
|
||
protected static Request toNettyRequest(URI baseUrl, HttpRequest request) { | ||
String rawUrl; | ||
|
||
if (request.getUri().startsWith("ws://")) { | ||
rawUrl = "http://" + request.getUri().substring("ws://".length()); | ||
} else if (request.getUri().startsWith("wss://")) { | ||
rawUrl = "https://" + request.getUri().substring("wss://".length()); | ||
} else if (request.getUri().startsWith("http://") || request.getUri().startsWith("https://")) { | ||
rawUrl = request.getUri(); | ||
} else { | ||
rawUrl = baseUrl.toString().replaceAll("/$", "") + request.getUri(); | ||
} | ||
|
||
RequestBuilder builder = request(request.getMethod().toString(), rawUrl); | ||
|
||
for (String name : request.getQueryParameterNames()) { | ||
for (String value : request.getQueryParameters(name)) { | ||
builder.addQueryParam(name, value); | ||
} | ||
} | ||
|
||
for (String name : request.getHeaderNames()) { | ||
for (String value : request.getHeaders(name)) { | ||
builder.addHeader(name, value); | ||
} | ||
} | ||
|
||
if (request.getMethod().equals(HttpMethod.POST)) { | ||
builder.setBody(request.getContent().get()); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
|
||
public static HttpResponse toSeleniumResponse(Response response) { | ||
HttpResponse toReturn = new HttpResponse(); | ||
|
||
toReturn.setStatus(response.getStatusCode()); | ||
|
||
toReturn.setContent(! response.hasResponseBody() | ||
? empty() | ||
: Contents.memoize(response::getResponseBodyAsStream)); | ||
|
||
response.getHeaders().names().forEach( | ||
name -> response.getHeaders(name).forEach(value -> toReturn.addHeader(name, value))); | ||
|
||
return toReturn; | ||
} | ||
} |
Oops, something went wrong.