MCPcopy Create free account
hub / github.com/chronoxor/CppServer / HTTPSCacheSession

Class HTTPSCacheSession

tests/test_https.cpp:80–169  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

78};
79
80class HTTPSCacheSession : public HTTPSSession
81{
82public:
83 using HTTPSSession::HTTPSSession;
84
85protected:
86 void onReceivedRequest(const HTTPRequest& request) override
87 {
88 // Process HTTP request methods
89 if (request.method() == "HEAD")
90 SendResponseAsync(response().MakeHeadResponse());
91 else if (request.method() == "GET")
92 {
93 std::string key(request.url());
94 std::string value;
95
96 // Decode the key value
97 key = CppCommon::Encoding::URLDecode(key);
98 CppCommon::StringUtils::ReplaceFirst(key, "/api/cache", "");
99 CppCommon::StringUtils::ReplaceFirst(key, "?key=", "");
100
101 if (key.empty())
102 {
103 // Response with all cache values
104 SendResponseAsync(response().MakeGetResponse(Cache::GetInstance().GetAllCache(), "application/json; charset=UTF-8"));
105 }
106 // Get the cache value by the given key
107 else if (Cache::GetInstance().GetCacheValue(key, value))
108 {
109 // Response with the cache value
110 SendResponseAsync(response().MakeGetResponse(value));
111 }
112 else
113 SendResponseAsync(response().MakeErrorResponse(404, "Required cache value was not found for the key: " + key));
114 }
115 else if ((request.method() == "POST") || (request.method() == "PUT"))
116 {
117 std::string key(request.url());
118 std::string value(request.body());
119
120 // Decode the key value
121 key = CppCommon::Encoding::URLDecode(key);
122 CppCommon::StringUtils::ReplaceFirst(key, "/api/cache", "");
123 CppCommon::StringUtils::ReplaceFirst(key, "?key=", "");
124
125 // Put the cache value
126 Cache::GetInstance().PutCacheValue(key, value);
127
128 // Response with the cache value
129 SendResponseAsync(response().MakeOKResponse());
130 }
131 else if (request.method() == "DELETE")
132 {
133 std::string key(request.url());
134 std::string value;
135
136 // Decode the key value
137 key = CppCommon::Encoding::URLDecode(key);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected