maph 1.0.0 High-Performance Memory-Mapped Key-Value Database with O(1) Lookups

Loading...
Searching...
No Matches
http3_server.c
Go to the documentation of this file.
1#ifdef LIBUS_USE_QUIC
2
3#define _GNU_SOURCE
4#include <unistd.h>
5#include <stdlib.h>
6
7/* Experimental HTTP/3 server. You can use quiche-client --no-verify https://[::1]:9004/ */
8#include <libusockets.h>
9
10/* Quic interface is not exposed under libusockets.h yet */
11#include "quic.h"
12
13/* Let's just have this one here for now */
14us_quic_socket_context_t *context;
15
16#include <stdio.h>
17
18/* Loop callbacks not used in this example */
19 void on_wakeup(struct us_loop_t *loop) {}
20 void on_pre(struct us_loop_t *loop) {}
21 void on_post(struct us_loop_t *loop) {}
22
23 void print_current_headers() {
24 /* Iterate the headers and print them */
25 for (int i = 0, more = 1; more; i++) {
26 char *name, *value;
27 int name_length, value_length;
28 if (more = us_quic_socket_context_get_header(context, i, &name, &name_length, &value, &value_length)) {
29 printf("header %.*s = %.*s\n", name_length, name, value_length, value);
30 }
31 }
32}
33
34/* This would be a request */
35 void on_stream_headers(us_quic_stream_t *s) {
36
37 //printf("==== HTTP/3 request ====\n");
38
39 //print_current_headers();
40
41 /* Write headers */
42 us_quic_socket_context_set_header(context, 0, ":status", 7, "200", 3);
43 //us_quic_socket_context_set_header(context, 1, "content-length", 14, "11", 2);
44 //us_quic_socket_context_set_header(context, 2, "content-type", 12, "text/html", 9);
45 us_quic_socket_context_send_headers(context, s, 1, 1);
46
47 /* Write body and shutdown (unknown if content-length must be present?) */
48 us_quic_stream_write(s, "Hello quic!", 11);
49
50 /* Every request has its own stream, so we conceptually serve requests like in HTTP 1.0 */
51 us_quic_stream_shutdown(s);
52}
53
54/* And this would be the body of the request */
55 void on_stream_data(us_quic_stream_t *s, char *data, int length) {
56 printf("Body length is: %d\n", length);
57}
58
59 void on_stream_writable(us_quic_stream_t *s) {
60
61}
62
63 void on_stream_close(us_quic_stream_t *s) {
64 //printf("Stream closed\n");
65}
66
67/* On new connection */
68 void on_open(us_quic_socket_t *s, int is_client) {
69 printf("Connection established!\n");
70}
71
72/* On new stream */
73 void on_stream_open(us_quic_stream_t *s, int is_client) {
74 //printf("Stream opened!\n");
75}
76
77 void on_close(us_quic_socket_t *s) {
78 printf("Disconnected!\n");
79}
80
81 int main() {
82 /* Create the event loop */
83 struct us_loop_t *loop = us_create_loop(0, on_wakeup, on_pre, on_post, 0);
84
85 /* SSL cert is always needed for quic */
86 us_quic_socket_context_options_t options = {
87 .cert_file_name = "",
88 .key_file_name = ""
89 };
90
91 /* Create quic socket context (assumes h3 for now) */
92 context = us_create_quic_socket_context(loop, options);
93
94 /* Specify application callbacks */
95 us_quic_socket_context_on_stream_data(context, on_stream_data);
96 us_quic_socket_context_on_stream_open(context, on_stream_open);
97 us_quic_socket_context_on_stream_close(context, on_stream_close);
98 us_quic_socket_context_on_stream_writable(context, on_stream_writable);
99 us_quic_socket_context_on_stream_headers(context, on_stream_headers);
100 us_quic_socket_context_on_open(context, on_open);
101 us_quic_socket_context_on_close(context, on_close);
102
103 /* The listening socket is the actual UDP socket used */
104 us_quic_listen_socket_t *listen_socket = us_quic_socket_context_listen(context, "::1", 9004);
105
106 /* Run the event loop */
107 us_loop_run(loop);
108
109 printf("Falling through!\n");
110}
111#else
112
113#include <stdio.h>
114
115 int main() {
116 printf("Compile with WITH_QUIC=1 make examples in order to build this example\n");
117}
118
119#endif
on_pre
void on_pre(struct us_loop_t *loop)
Definition echo_server.c:27
on_wakeup
void on_wakeup(struct us_loop_t *loop)
Definition echo_server.c:22
on_post
void on_post(struct us_loop_t *loop)
Definition echo_server.c:32
main
int main()
Definition http3_server.c:115
libusockets.h
us_loop_run
void us_loop_run(struct us_loop_t *loop)
us_create_loop
struct us_loop_t * us_create_loop(void *hint, void(*wakeup_cb)(struct us_loop_t *loop), void(*pre_cb)(struct us_loop_t *loop), void(*post_cb)(struct us_loop_t *loop), unsigned int ext_size)
us_loop_t
Definition asio.h:26
quic.h