Skip to content

Commit 7da868a

Browse files
committed
More h3 wrap up
1 parent 9f9ca69 commit 7da868a

6 files changed

Lines changed: 73 additions & 32 deletions

File tree

examples/Http3Server.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ int main() {
1010
.key_file_name = "../misc/key.pem",
1111
.cert_file_name = "../misc/cert.pem",
1212
.passphrase = "1234"
13-
}).get("/*", [](auto *res, auto */*req*/) {
13+
}).get("/*", [](auto *res, auto *req) {
14+
15+
std::cout << req->getHeader(":path") << std::endl;
16+
1417
res->end("Hello quic!");
1518
}).listen(3000, [](auto *listen_socket) {
1619
if (listen_socket) {

src/Http3Context.h

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,44 @@ extern "C" {
33
#include "quic.h"
44
}
55

6+
#include "Http3ContextData.h"
7+
#include "Http3ResponseData.h"
68

79
/* Let's just have this one here for now */
810
us_quic_socket_context_t *context;
911

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-
2112
/* This would be a request */
2213
void on_stream_headers(us_quic_stream_t *s) {
2314

24-
printf("==== HTTP/3 request ====\n");
15+
Http3ContextData *contextData = (Http3ContextData *) us_quic_socket_context_ext(us_quic_socket_context(us_quic_stream_socket(s)));
2516

26-
print_current_headers();
17+
contextData->router.route();
2718

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);
3319

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);
3920
}
4021

4122
/* And this would be the body of the request */
4223
void on_stream_data(us_quic_stream_t *s, char *data, int length) {
24+
25+
// Http3ResponseData *responseData = us_quic_stream_ext(s);
26+
// responseData->onData(data, length);
27+
4328
printf("Body length is: %d\n", length);
4429
}
4530

4631
void on_stream_writable(us_quic_stream_t *s) {
47-
32+
// Http3ResponseData *responseData = us_quic_stream_ext(s);
33+
// responseData->onWritable();
4834
}
4935

5036
void on_stream_close(us_quic_stream_t *s) {
51-
printf("Stream closed\n");
37+
38+
// Http3ResponseData *responseData = us_quic_stream_ext(s);
39+
// responseData->onAborted();
40+
41+
//printf("Stream closed\n");
42+
43+
// inplace destruct Http3ResponseData here
5244
}
5345

5446
/* On new connection */
@@ -58,7 +50,9 @@ void on_open(us_quic_socket_t *s, int is_client) {
5850

5951
/* On new stream */
6052
void on_stream_open(us_quic_stream_t *s, int is_client) {
61-
printf("Stream opened!\n");
53+
//printf("Stream opened!\n");
54+
55+
// inplace initialize Http3ResponseData here
6256
}
6357

6458
void on_close(us_quic_socket_t *s) {
@@ -73,7 +67,7 @@ namespace uWS {
7367
printf("Creating context now\n");
7468

7569
/* Create quic socket context (assumes h3 for now) */
76-
context = us_create_quic_socket_context(loop, options);
70+
context = us_create_quic_socket_context(loop, options); // sizeof(Http3ContextData)
7771

7872
/* Specify application callbacks */
7973
us_quic_socket_context_on_stream_data(context, on_stream_data);
@@ -85,7 +79,7 @@ namespace uWS {
8579
us_quic_socket_context_on_close(context, on_close);
8680

8781
/* 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);
82+
us_quic_listen_socket_t *listen_socket = us_quic_socket_context_listen(context, "::1", 9004); // sizeof(Http3ResponseData)
8983

9084

9185
return nullptr;

src/Http3ContextData.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "HttpRouter.h"
2+
3+
namespace uWS {
4+
5+
struct Http3ContextData {
6+
HttpRouter<int> router;
7+
};
8+
9+
}

src/Http3Request.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
extern "C" {
2+
#include "quic.h"
3+
}
4+
15
namespace uWS {
26

37
struct Http3Request {
4-
// really the same thing as HttpRequest with same rules
8+
9+
std::string_view getHeader(std::string_view key) {
10+
for (int i = 0, more = 1; more; i++) {
11+
char *name, *value;
12+
int name_length, value_length;
13+
if (more = us_quic_socket_context_get_header(nullptr, i, &name, &name_length, &value, &value_length)) {
14+
if (name_length == key.length() && !memcmp(name, key.data(), key.length())) {
15+
return {value, value_length};
16+
}
17+
}
18+
return {nullptr, 0};
19+
}
20+
}
521
};
622
}

src/Http3Response.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
namespace uWS {
22

3+
/* Is a quic stream */
34
struct Http3Response {
4-
// has a quic stream
5+
6+
void writeStatus() {
7+
8+
}
59

610
void end(std::string_view data) {
7-
11+
12+
// Http3ResponseData *responseData = us_quic_stream_ext(this);
13+
14+
// if not already written status then write status
15+
16+
/* Write headers */
17+
us_quic_socket_context_set_header(context, 0, ":status", 7, "200", 3);
18+
//us_quic_socket_context_set_header(context, 1, "content-length", 14, "11", 2);
19+
//us_quic_socket_context_set_header(context, 2, "content-type", 12, "text/html", 9);
20+
us_quic_socket_context_send_headers(context, this, 1, 1);
21+
22+
/* Write body and shutdown (unknown if content-length must be present?) */
23+
us_quic_stream_write(this, "Hello quic!", 11);
24+
25+
/* Every request has its own stream, so we conceptually serve requests like in HTTP 1.0 */
26+
us_quic_stream_shutdown(this);
827
}
928
};
1029

src/Http3ResponseData.h

Whitespace-only changes.

0 commit comments

Comments
 (0)