ptrd / flupke

Java HTTP3 Client and HTTP3 Server plugin for Kwik
GNU General Public License v3.0
12 stars 5 forks source link

flupke http3 server sample #8

Closed ccc7574 closed 3 months ago

ccc7574 commented 3 months ago

Hello Peter, thx so much for this lib, our company try to use this lib as our http3 server.

I only can find some sample about http3 client with flupke. Could you please provide one sample by using flupke as the http3 server?

I try to write some code but couldn't figure out the detail between flupke connnection, kwik, tls... Thx again!

ptrd commented 3 months ago

Hi,

Yes, sure! It's actually quite simple. There is sample in the Kwik source that might help: https://github.com/ptrd/kwik/blob/master/src/main/java/net/luminis/quic/run/SampleWebServer.java

To summarize, it boils down to the following steps:

In the Kwik source, instantiating the Http3ApplicationProtocolFactory is done via reflection, because Kwik cannot have a compile-time dependency on Flupke. Of course, when using Flupke as library you don't have to use reflection.

(*) I now see that Http3ApplicationProtocolFactory always requires a File argument (the www dir from which to serve files). This is an error, it should at least provide a constructor that just requires a HttpRequestHandler. I will correct this a.s.a.p.

Hope this helps. Regards Peter

ccc7574 commented 3 months ago

Thx so much Peter! You're my hero, my super star!!!

ccc7574 commented 3 months ago

hello Peter, I use the way you told me to write a http3 server, it runs well. and I also write a http3 client, I tried to send a file to http3 server.but got error like below: Could you help on it?

24-03-26T22:07:33.540 Error: Discarding packet (42 bytes) that cannot be decrypted (net.luminis.quic.crypto.MissingKeysException: Missing keys for encryption level Handshake (keys discarded))

Here is my code about http3 client: The cert util load the pem and key file in your test resource folder and return keymanager and trustmanager

    SysOutLogger log = new SysOutLogger();
    CertUtil.initCert();
    SSLContext sslContext = SSLContext.getInstance("TLSv1.3");
    KeyManager[] km = CertUtil.getKM();
    TrustManager[] tm = CertUtil.getTM();
    sslContext.init(km,tm,null);`   

    Http3Client.Builder clientBuilder = new Http3ClientBuilder().sslContext(sslContext);
    HttpClient client = clientBuilder.build();
    HttpRequest.BodyPublisher filePublisher = HttpRequest.BodyPublishers.ofFile(Path.of("D:\\doc\\book\\2021-tesla-impact-report.pdf"));
    HttpRequest request = HttpRequest.newBuilder()
                .uri(new URI("http://localhost:"+port))
                .header("User-Agent", "Flupke http3 library")
                .timeout(Duration.ofSeconds(5))
                .POST(filePublisher)
                .build();
        System.out.println("=====http3 upload " + System.currentTimeMillis());
        HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
ptrd commented 3 months ago

Hi, Thanks for your feedback.

You can ignore that error, it is not fatal w.r.t. the QUIC connection, it's just a handshake ack that is send by the client rather late and the server cannot decrypt it because it has already discarded the handshake keys. But just the fact that it has discarded the handshake keys implies that it has App keys and can communicate.

Calling sslContext on the builder won't work, see https://github.com/ptrd/flupke/issues/5. For using localhost, you can call disableCertificateCheck() on the builder.

Hth, Peter

ccc7574 commented 3 months ago

Hi, 你好 Thanks for your feedback.感谢您的反馈。

You can ignore that error, it is not fatal w.r.t. the QUIC connection, it's just a handshake ack that is send by the client rather late and the server cannot decrypt it because it has already discarded the handshake keys. But just the fact that it has discarded the handshake keys implies that it has App keys and can communicate.您可以忽略该错误,它不是致命的 QUIC 连接,它只是客户端发送的握手确认器相当晚,服务器无法解密它,因为它已经丢弃了握手键。但是,它丢弃了握手键这一事实意味着它拥有App密钥并且可以进行通信。

Calling sslContext on the builder won't work, see #5. For using localhost, you can call disableCertificateCheck() on the builder.在构建器上调用 sslContext 不起作用,请参阅 #5。要使用 localhost,您可以在构建器上调用 disableCertificateCheck()。

Hth, Hth, Peter 彼得

Got it,thx again!