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

Class HTTPSCacheSession

examples/https_server.cpp:80–170  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected