Skip to content

Commit b5d5f42

Browse files
committed
Cosmetic improvements for netty client example (dealing with URL argument, readme)
1 parent 2cf5480 commit b5d5f42

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ Note that `poll` would either execute callbacks and returns immediately. If ther
305305

306306
Have a look at the [quiche4j-examples](quiche4j-examples/src/main/java/io/quiche4j/examples/) folder for more complete examples on how to use the Quiche4j API to work with HTTP/3 protocol.
307307

308+
Examples package has [`Http3NettyClient`](quiche4j-examples/src/main/java/io/quiche4j/examples/Http3NettyClient.java) with a toy implementation of HTTP/3 client to show case the idea of how `quiche4j` connection state management could be integrated with [Netty](https://netty.io/) I/O primitives.
309+
308310
### Errors Hanlding
309311

310312
Native JNI code propagates errors using return codes (typically the return code < 0 means either DONE or failed). For example, [`quiche::Error`](https://github.com/cloudflare/quiche/blob/204d693bb543e12a605073181ae605eacb743039/src/lib.rs#L320-L365) enum. `Quiche4j` follows the same convention instead of throwing Java exceptions to ensure good perfomance and compatibility with async runtimes (catching exception in async environemnt might be somewhat problematic). See [`Quiche.ErrorCode`](src/main/java/io/quiche4j/Quiche.java) and [`Http3.ErrorCode`](src/main/java/io/quiche4j/http3/Http3.java) for more details.

http3-client.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
set -eu -o pipefail
33

4-
URL=${1:-"http://quic.tech:8443"}
4+
URL=${1:-"https://quic.tech:8443"}
55

66
java \
77
-cp quiche4j-examples/target/quiche4j-examples-*.jar \

quiche4j-examples/src/main/java/io/quiche4j/examples/Http3NettyClient.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import java.io.IOException;
77
import java.net.InetSocketAddress;
8+
import java.net.URI;
9+
import java.net.URISyntaxException;
810
import java.nio.channels.ClosedChannelException;
911
import java.nio.charset.StandardCharsets;
1012
import java.util.ArrayList;
@@ -356,9 +358,23 @@ public HttpOverQuicHandler httpHandler() {
356358
}
357359

358360
public static void main(String[] args) throws IOException {
361+
if (0 == args.length) {
362+
System.out.println("Usage: ./http3-netty-client.sh <URL>");
363+
System.exit(1);
364+
}
365+
366+
final String url = args[0];
367+
final URI uri;
368+
try {
369+
uri = new URI(url);
370+
} catch (URISyntaxException e) {
371+
System.out.println("Failed to parse URL " + url);
372+
System.exit(1);
373+
return;
374+
}
375+
376+
final InetSocketAddress address = new InetSocketAddress(uri.getHost(), uri.getPort());
359377

360-
final String hostname = "quic.tech";
361-
final InetSocketAddress address = new InetSocketAddress(hostname, 8443);
362378
final EventLoopGroup workerGroup = new NioEventLoopGroup();
363379
final Config config = new ConfigBuilder(Quiche.PROTOCOL_VERSION)
364380
.withApplicationProtos(Http3.APPLICATION_PROTOCOL)
@@ -378,7 +394,7 @@ public static void main(String[] args) throws IOException {
378394
final Http3Config http3config = new Http3ConfigBuilder().build();
379395

380396
final Http3ClientInitializer initializer =
381-
new Http3ClientInitializer(address, hostname, config, http3config);
397+
new Http3ClientInitializer(address, uri.getHost(), config, http3config);
382398

383399
try {
384400
final Bootstrap b = new Bootstrap();
@@ -392,8 +408,8 @@ public static void main(String[] args) throws IOException {
392408
// connection is now ready
393409
System.out.println("Connection was succesfully established");
394410

395-
final FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/", Unpooled.EMPTY_BUFFER);
396-
request.headers().add(HttpHeaderNames.HOST, "quic.tech");
411+
final FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, uri.getPath(), Unpooled.EMPTY_BUFFER);
412+
request.headers().add(HttpHeaderNames.HOST, uri.getHost());
397413
request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), "https");
398414

399415
long streamId = initializer.httpHandler().sendRequest(request);

0 commit comments

Comments
 (0)