|
| 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 | + |
1 | 68 | namespace uWS { |
2 | 69 | 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 | + |
4 | 91 | return nullptr; |
5 | 92 |
|
6 | 93 | // call init here after setting the ext to Http3ContextData |
|
0 commit comments