Skip to content

Commit 3ab07a7

Browse files
committed
Add minimal http3 impl.
1 parent dfa9214 commit 3ab07a7

3 files changed

Lines changed: 91 additions & 3 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ endif
1414
# WITH_QUIC enables experimental Http3 examples
1515
ifeq ($(WITH_QUIC),1)
1616
override CXXFLAGS += -DLIBUS_USE_QUIC
17+
override LDFLAGS += -pthread -lz -lm uSockets/lsquic/src/liblsquic/liblsquic.a
1718
endif
1819

1920
# WITH_LIBDEFLATE=1 enables fast paths for SHARED_COMPRESSOR and inflation

src/Http3App.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace uWS {
1111

1212
QuicApp(SocketContextOptions options = {}) {
1313
/* Create the http3 context */
14-
http3Context = Http3Context::create((us_loop_t *)Loop::get(), options);
14+
http3Context = Http3Context::create((us_loop_t *)Loop::get(), {});
1515
}
1616

1717
QuicApp &listen(int port, std::function<void(void *)> cb) {
@@ -25,7 +25,7 @@ namespace uWS {
2525
}
2626

2727
void run() {
28-
28+
uWS::Loop::get()->run();
2929
}
3030
};
3131

src/Http3Context.h

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,93 @@
1+
2+
extern "C" {
3+
#include "quic.h"
4+
}
5+
6+
7+
/* Let's just have this one here for now */
8+
us_quic_socket_context_t *context;
9+
10+
void print_current_headers() {
11+
/* Iterate the headers and print them */
12+
for (int i = 0, more = 1; more; i++) {
13+
char *name, *value;
14+
int name_length, value_length;
15+
if (more = us_quic_socket_context_get_header(context, i, &name, &name_length, &value, &value_length)) {
16+
printf("header %.*s = %.*s\n", name_length, name, value_length, value);
17+
}
18+
}
19+
}
20+
21+
/* This would be a request */
22+
void on_stream_headers(us_quic_stream_t *s) {
23+
24+
printf("==== HTTP/3 request ====\n");
25+
26+
print_current_headers();
27+
28+
/* Write headers */
29+
us_quic_socket_context_set_header(context, 0, ":status", 7, "200", 3);
30+
//us_quic_socket_context_set_header(context, 1, "content-length", 14, "11", 2);
31+
//us_quic_socket_context_set_header(context, 2, "content-type", 12, "text/html", 9);
32+
us_quic_socket_context_send_headers(context, s, 1, 1);
33+
34+
/* Write body and shutdown (unknown if content-length must be present?) */
35+
us_quic_stream_write(s, "Hello quic!", 11);
36+
37+
/* Every request has its own stream, so we conceptually serve requests like in HTTP 1.0 */
38+
us_quic_stream_shutdown(s);
39+
}
40+
41+
/* And this would be the body of the request */
42+
void on_stream_data(us_quic_stream_t *s, char *data, int length) {
43+
printf("Body length is: %d\n", length);
44+
}
45+
46+
void on_stream_writable(us_quic_stream_t *s) {
47+
48+
}
49+
50+
void on_stream_close(us_quic_stream_t *s) {
51+
printf("Stream closed\n");
52+
}
53+
54+
/* On new connection */
55+
void on_open(us_quic_socket_t *s, int is_client) {
56+
printf("Connection established!\n");
57+
}
58+
59+
/* On new stream */
60+
void on_stream_open(us_quic_stream_t *s, int is_client) {
61+
printf("Stream opened!\n");
62+
}
63+
64+
void on_close(us_quic_socket_t *s) {
65+
printf("Disconnected!\n");
66+
}
67+
168
namespace uWS {
269
struct Http3Context {
3-
static Http3Context *create(us_loop_t *loop, us_socket_context_options_t options) {
70+
static Http3Context *create(us_loop_t *loop, us_quic_socket_context_options_t options) {
71+
//return nullptr;
72+
73+
printf("Creating context now\n");
74+
75+
/* Create quic socket context (assumes h3 for now) */
76+
context = us_create_quic_socket_context(loop, options);
77+
78+
/* Specify application callbacks */
79+
us_quic_socket_context_on_stream_data(context, on_stream_data);
80+
us_quic_socket_context_on_stream_open(context, on_stream_open);
81+
us_quic_socket_context_on_stream_close(context, on_stream_close);
82+
us_quic_socket_context_on_stream_writable(context, on_stream_writable);
83+
us_quic_socket_context_on_stream_headers(context, on_stream_headers);
84+
us_quic_socket_context_on_open(context, on_open);
85+
us_quic_socket_context_on_close(context, on_close);
86+
87+
/* The listening socket is the actual UDP socket used */
88+
us_quic_listen_socket_t *listen_socket = us_quic_socket_context_listen(context, "::1", 9004);
89+
90+
491
return nullptr;
592

693
// call init here after setting the ext to Http3ContextData

0 commit comments

Comments
 (0)