Skip to content

Commit 88b15f3

Browse files
committed
Add base for a http cache
1 parent 8028beb commit 88b15f3

File tree

10 files changed

+174
-47
lines changed

10 files changed

+174
-47
lines changed

build.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ int main(int argc, char **argv) {
99
char *CXX = strcpy(calloc(1024, 1), or_else(getenv("CXX"), "g++"));
1010
char *EXEC_SUFFIX = strcpy(calloc(1024, 1), maybe(getenv("EXEC_SUFFIX")));
1111

12-
char *EXAMPLE_FILES[] = {"HelloWorldThreaded", "Http3Server", "Broadcast", "HelloWorld", "Crc32", "ServerName",
12+
char *EXAMPLE_FILES[] = {"CachingApp", "HelloWorldThreaded", "Http3Server", "Broadcast", "HelloWorld", "Crc32", "ServerName",
1313
"EchoServer", "BroadcastingEchoServer", "UpgradeSync", "UpgradeAsync", "ParameterRoutes"};
1414

1515
strcat(CXXFLAGS, " -march=native -O3 -Wpedantic -Wall -Wextra -Wsign-conversion -Wconversion -std=c++20 -Isrc -IuSockets/src");

examples/CachingApp.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "App.h"
2+
#include <iostream>
3+
4+
int main() {
5+
uWS::App app;
6+
7+
/* Regular, non-cached response */
8+
app.get("/*", [](auto *res, auto */*req*/) {
9+
res->end("Responding without a cache");
10+
}).get("/cached", [](auto *res, auto */*req*/) {
11+
/* A cached response with 13 seconds of lifetime */
12+
std::cout << "Filling cache now" << std::endl;
13+
res->end("This is a response");
14+
}, 13).listen(8080, [](bool success) {
15+
if (success) {
16+
std::cout << "Listening on port 8080" << std::endl;
17+
} else {
18+
std::cerr << "Failed to listen on port 8080" << std::endl;
19+
}
20+
});
21+
22+
app.run();
23+
}

src/App.h

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ namespace uWS {
7979

8080
static_assert(sizeof(struct us_socket_context_options_t) == sizeof(SocketContextOptions), "Mismatching uSockets/uWebSockets ABI");
8181

82-
template <bool SSL>
82+
template <bool SSL, typename BuilderPatternReturnType>
8383
struct TemplatedApp {
8484
private:
8585
/* The app always owns at least one http context, but creates websocket contexts on demand */
@@ -94,7 +94,7 @@ struct TemplatedApp {
9494
TopicTree<TopicTreeMessage, TopicTreeBigMessage> *topicTree = nullptr;
9595

9696
/* Server name */
97-
TemplatedApp &&addServerName(std::string hostname_pattern, SocketContextOptions options = {}) {
97+
BuilderPatternReturnType &&addServerName(std::string hostname_pattern, SocketContextOptions options = {}) {
9898

9999
/* Do nothing if not even on SSL */
100100
if constexpr (SSL) {
@@ -104,10 +104,10 @@ struct TemplatedApp {
104104
us_socket_context_add_server_name(SSL, (struct us_socket_context_t *) httpContext, hostname_pattern.c_str(), options, domainRouter);
105105
}
106106

107-
return std::move(*this);
107+
return std::move(static_cast<BuilderPatternReturnType &&>(*this));
108108
}
109109

110-
TemplatedApp &&removeServerName(std::string hostname_pattern) {
110+
BuilderPatternReturnType &&removeServerName(std::string hostname_pattern) {
111111

112112
/* This will do for now, would be better if us_socket_context_remove_server_name returned the user data */
113113
auto *domainRouter = us_socket_context_find_server_name_userdata(SSL, (struct us_socket_context_t *) httpContext, hostname_pattern.c_str());
@@ -119,7 +119,7 @@ struct TemplatedApp {
119119
return std::move(*this);
120120
}
121121

122-
TemplatedApp &&missingServerName(MoveOnlyFunction<void(const char *hostname)> handler) {
122+
BuilderPatternReturnType &&missingServerName(MoveOnlyFunction<void(const char *hostname)> handler) {
123123

124124
if (!constructorFailed()) {
125125
httpContext->getSocketContextData()->missingServerNameHandler = std::move(handler);
@@ -141,7 +141,7 @@ struct TemplatedApp {
141141
}
142142

143143
/* Attaches a "filter" function to track socket connections/disconnections */
144-
TemplatedApp &&filter(MoveOnlyFunction<void(HttpResponse<SSL> *, int)> &&filterHandler) {
144+
BuilderPatternReturnType &&filter(MoveOnlyFunction<void(HttpResponse<SSL> *, int)> &&filterHandler) {
145145
httpContext->filter(std::move(filterHandler));
146146

147147
return std::move(*this);
@@ -259,7 +259,7 @@ struct TemplatedApp {
259259
};
260260

261261
/* Closes all sockets including listen sockets. */
262-
TemplatedApp &&close() {
262+
BuilderPatternReturnType &&close() {
263263
us_socket_context_close(SSL, (struct us_socket_context_t *) httpContext);
264264
for (void *webSocketContext : webSocketContexts) {
265265
us_socket_context_close(SSL, (struct us_socket_context_t *) webSocketContext);
@@ -269,13 +269,13 @@ struct TemplatedApp {
269269
}
270270

271271
template <typename UserData>
272-
TemplatedApp &&ws(std::string pattern, WebSocketBehavior<UserData> &&behavior) {
272+
BuilderPatternReturnType &&ws(std::string pattern, WebSocketBehavior<UserData> &&behavior) {
273273
/* Don't compile if alignment rules cannot be satisfied */
274274
static_assert(alignof(UserData) <= LIBUS_EXT_ALIGNMENT,
275275
"µWebSockets cannot satisfy UserData alignment requirements. You need to recompile µSockets with LIBUS_EXT_ALIGNMENT adjusted accordingly.");
276276

277277
if (!httpContext) {
278-
return std::move(*this);
278+
return std::move(static_cast<BuilderPatternReturnType &&>(*this));
279279
}
280280

281281
/* Terminate on misleading idleTimeout values */
@@ -438,11 +438,11 @@ struct TemplatedApp {
438438
req->setYield(true);
439439
}
440440
}, true);
441-
return std::move(*this);
441+
return std::move(static_cast<BuilderPatternReturnType &&>(*this));
442442
}
443443

444444
/* Browse to a server name, changing the router to this domain */
445-
TemplatedApp &&domain(std::string serverName) {
445+
BuilderPatternReturnType &&domain(std::string serverName) {
446446
HttpContextData<SSL> *httpContextData = httpContext->getSocketContextData();
447447

448448
void *domainRouter = us_socket_context_find_server_name_userdata(SSL, (struct us_socket_context_t *) httpContext, serverName.c_str());
@@ -454,82 +454,82 @@ struct TemplatedApp {
454454
httpContextData->currentRouter = &httpContextData->router;
455455
}
456456

457-
return std::move(*this);
457+
return std::move(static_cast<BuilderPatternReturnType &&>(*this));
458458
}
459459

460-
TemplatedApp &&get(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
460+
BuilderPatternReturnType &&get(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
461461
if (httpContext) {
462462
httpContext->onHttp("GET", pattern, std::move(handler));
463463
}
464-
return std::move(*this);
464+
return std::move(static_cast<BuilderPatternReturnType &&>(*this));
465465
}
466466

467-
TemplatedApp &&post(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
467+
BuilderPatternReturnType &&post(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
468468
if (httpContext) {
469469
httpContext->onHttp("POST", pattern, std::move(handler));
470470
}
471-
return std::move(*this);
471+
return std::move(static_cast<BuilderPatternReturnType &&>(*this));
472472
}
473473

474-
TemplatedApp &&options(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
474+
BuilderPatternReturnType &&options(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
475475
if (httpContext) {
476476
httpContext->onHttp("OPTIONS", pattern, std::move(handler));
477477
}
478478
return std::move(*this);
479479
}
480480

481-
TemplatedApp &&del(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
481+
BuilderPatternReturnType &&del(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
482482
if (httpContext) {
483483
httpContext->onHttp("DELETE", pattern, std::move(handler));
484484
}
485485
return std::move(*this);
486486
}
487487

488-
TemplatedApp &&patch(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
488+
BuilderPatternReturnType &&patch(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
489489
if (httpContext) {
490490
httpContext->onHttp("PATCH", pattern, std::move(handler));
491491
}
492492
return std::move(*this);
493493
}
494494

495-
TemplatedApp &&put(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
495+
BuilderPatternReturnType &&put(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
496496
if (httpContext) {
497497
httpContext->onHttp("PUT", pattern, std::move(handler));
498498
}
499499
return std::move(*this);
500500
}
501501

502-
TemplatedApp &&head(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
502+
BuilderPatternReturnType &&head(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
503503
if (httpContext) {
504504
httpContext->onHttp("HEAD", pattern, std::move(handler));
505505
}
506506
return std::move(*this);
507507
}
508508

509-
TemplatedApp &&connect(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
509+
BuilderPatternReturnType &&connect(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
510510
if (httpContext) {
511511
httpContext->onHttp("CONNECT", pattern, std::move(handler));
512512
}
513513
return std::move(*this);
514514
}
515515

516-
TemplatedApp &&trace(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
516+
BuilderPatternReturnType &&trace(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
517517
if (httpContext) {
518518
httpContext->onHttp("TRACE", pattern, std::move(handler));
519519
}
520520
return std::move(*this);
521521
}
522522

523523
/* This one catches any method */
524-
TemplatedApp &&any(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
524+
BuilderPatternReturnType &&any(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
525525
if (httpContext) {
526526
httpContext->onHttp("*", pattern, std::move(handler));
527527
}
528-
return std::move(*this);
528+
return std::move(static_cast<BuilderPatternReturnType &&>(*this));
529529
}
530530

531531
/* Host, port, callback */
532-
TemplatedApp &&listen(std::string host, int port, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
532+
BuilderPatternReturnType &&listen(std::string host, int port, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
533533
if (!host.length()) {
534534
return listen(port, std::move(handler));
535535
}
@@ -538,7 +538,7 @@ struct TemplatedApp {
538538
}
539539

540540
/* Host, port, options, callback */
541-
TemplatedApp &&listen(std::string host, int port, int options, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
541+
BuilderPatternReturnType &&listen(std::string host, int port, int options, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
542542
if (!host.length()) {
543543
return listen(port, options, std::move(handler));
544544
}
@@ -547,44 +547,44 @@ struct TemplatedApp {
547547
}
548548

549549
/* Port, callback */
550-
TemplatedApp &&listen(int port, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
550+
BuilderPatternReturnType &&listen(int port, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
551551
handler(httpContext ? httpContext->listen(nullptr, port, 0) : nullptr);
552-
return std::move(*this);
552+
return std::move(static_cast<BuilderPatternReturnType &&>(*this));
553553
}
554554

555555
/* Port, options, callback */
556-
TemplatedApp &&listen(int port, int options, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
556+
BuilderPatternReturnType &&listen(int port, int options, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
557557
handler(httpContext ? httpContext->listen(nullptr, port, options) : nullptr);
558558
return std::move(*this);
559559
}
560560

561561
/* options, callback, path to unix domain socket */
562-
TemplatedApp &&listen(int options, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler, std::string path) {
562+
BuilderPatternReturnType &&listen(int options, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler, std::string path) {
563563
handler(httpContext ? httpContext->listen(path.c_str(), options) : nullptr);
564564
return std::move(*this);
565565
}
566566

567567
/* callback, path to unix domain socket */
568-
TemplatedApp &&listen(MoveOnlyFunction<void(us_listen_socket_t *)> &&handler, std::string path) {
568+
BuilderPatternReturnType &&listen(MoveOnlyFunction<void(us_listen_socket_t *)> &&handler, std::string path) {
569569
handler(httpContext ? httpContext->listen(path.c_str(), 0) : nullptr);
570570
return std::move(*this);
571571
}
572572

573573
/* Register event handler for accepted FD. Can be used together with adoptSocket. */
574-
TemplatedApp &&preOpen(LIBUS_SOCKET_DESCRIPTOR (*handler)(LIBUS_SOCKET_DESCRIPTOR)) {
574+
BuilderPatternReturnType &&preOpen(LIBUS_SOCKET_DESCRIPTOR (*handler)(LIBUS_SOCKET_DESCRIPTOR)) {
575575
httpContext->onPreOpen(handler);
576-
return std::move(*this);
576+
return std::move(static_cast<BuilderPatternReturnType &&>(*this));
577577
}
578578

579579
/* adopt an externally accepted socket */
580-
TemplatedApp &&adoptSocket(LIBUS_SOCKET_DESCRIPTOR accepted_fd) {
580+
BuilderPatternReturnType &&adoptSocket(LIBUS_SOCKET_DESCRIPTOR accepted_fd) {
581581
httpContext->adoptAcceptedSocket(accepted_fd);
582-
return std::move(*this);
582+
return std::move(static_cast<BuilderPatternReturnType &&>(*this));
583583
}
584584

585-
TemplatedApp &&run() {
585+
BuilderPatternReturnType &&run() {
586586
uWS::run();
587-
return std::move(*this);
587+
return std::move(static_cast<BuilderPatternReturnType &&>(*this));
588588
}
589589

590590
Loop *getLoop() {
@@ -593,9 +593,13 @@ struct TemplatedApp {
593593

594594
};
595595

596-
typedef TemplatedApp<false> App;
597-
typedef TemplatedApp<true> SSLApp;
596+
}
597+
598+
#include "CachingApp.h"
598599

600+
namespace uWS {
601+
typedef uWS::CachingApp App;
602+
typedef uWS::CachingApp SSLApp;
599603
}
600604

601605
#endif // UWS_APP_H

src/AsyncSocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct AsyncSocket {
4747
/* This guy is promiscuous */
4848
template <bool> friend struct HttpContext;
4949
template <bool, bool, typename> friend struct WebSocketContext;
50-
template <bool> friend struct TemplatedApp;
50+
template <bool, typename> friend struct TemplatedApp;
5151
template <bool, typename> friend struct WebSocketContextData;
5252
template <typename, typename> friend struct TopicTree;
5353
template <bool> friend struct HttpResponse;

0 commit comments

Comments
 (0)