-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestInspector.cpp
More file actions
296 lines (257 loc) · 9.51 KB
/
Copy pathRequestInspector.cpp
File metadata and controls
296 lines (257 loc) · 9.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "RequestInspector.h"
#include "Logger.h"
#include <iomanip>
#include <sstream>
#include <cmath>
using namespace std;
RequestInspector& RequestInspector::current()
{
thread_local RequestInspector instance;
return instance;
}
void RequestInspector::reset()
{
*this = RequestInspector();
}
void RequestInspector::start(uint64_t reqId, const string& clientIP, const string& method, const string& url, const string& httpVersion, const string& threadId)
{
reset();
this->reqId = reqId;
this->clientIP = clientIP;
this->method = method;
this->url = url;
this->httpVersion = httpVersion;
this->threadId = threadId;
this->startTime = chrono::high_resolution_clock::now();
}
void RequestInspector::addHeader(const string& key, const string& value)
{
trackedHeaders.push_back({key, value});
}
void RequestInspector::addPipelineStep(const string& name)
{
pipelineSteps.push_back(name);
}
void RequestInspector::setStaticFile(const string& resolvedPath, bool exists, const string& mime, long long fileSize)
{
this->staticFileAttempted = true;
this->resolvedPath = resolvedPath;
this->fileExists = exists;
this->mimeType = mime;
this->fileSize = fileSize;
}
void RequestInspector::setMetadataCache(const string& status)
{
this->metadataCacheStatus = status;
}
void RequestInspector::setFileCache(const string& status, const string& reason)
{
this->cacheAttempted = true;
this->cacheStatus = status;
this->cacheReason = reason;
}
void RequestInspector::setCompression(bool supported, bool used, const string& algorithm, size_t originalSize, size_t compressedSize)
{
this->compressionAttempted = true;
this->compressionSupported = supported;
this->compressionUsed = used;
this->compressionAlgorithm = algorithm;
this->origCompSize = originalSize;
this->compSize = compressedSize;
}
void RequestInspector::setCachingInfo(const string& etag, const string& lastModified, bool etagMatch, bool modifiedMatch)
{
this->cachingCheckAttempted = true;
this->etag = etag;
this->lastModified = lastModified;
this->etagMatch = etagMatch;
this->modifiedMatch = modifiedMatch;
}
void RequestInspector::setRangeInfo(bool requested, long long start, long long end, long long totalSize)
{
this->rangeAttempted = true;
this->rangeStart = start;
this->rangeEnd = end;
this->rangeTotalSize = totalSize;
}
void RequestInspector::setRouting(bool matched, const string& routePattern, const map<string, string>& params)
{
this->routeAttempted = true;
this->routeMatched = matched;
this->matchedRoute = routePattern;
this->routeParams = params;
}
void RequestInspector::setDecision(const string& decision)
{
this->decision = decision;
}
void RequestInspector::setResponse(const string& status, const string& contentType, size_t bodySize, const string& connection)
{
this->responseStatus = status;
this->responseContentType = contentType;
this->responseBodySize = bodySize;
this->responseConnection = connection;
}
string RequestInspector::generateReport() const
{
auto endTime = chrono::high_resolution_clock::now();
double durationMs = chrono::duration<double, milli>(endTime - startTime).count();
stringstream ss;
ss << "\n==========================================================\n";
ss << "REQUEST #" << reqId << "\n";
ss << "==========================================================\n\n";
ss << "Client\n";
ss << "------\n";
ss << left << setw(14) << "IP" << ": " << clientIP << "\n";
ss << left << setw(14) << "Method" << ": " << method << "\n";
ss << left << setw(14) << "URL" << ": " << url << "\n";
ss << left << setw(14) << "HTTP" << ": " << httpVersion << "\n";
if(!threadId.empty()) {
ss << left << setw(14) << "Worker ID" << ": " << threadId << "\n";
}
ss << "\n";
if(!trackedHeaders.empty())
{
ss << "Headers\n";
ss << "-------\n";
for(const auto& h : trackedHeaders)
{
ss << left << setw(16) << h.first << ": " << h.second << "\n";
}
ss << "\n";
}
if(!pipelineSteps.empty())
{
ss << "Pipeline\n";
ss << "--------\n";
for(const auto& step : pipelineSteps)
{
ss << "✓ " << step << "\n";
}
ss << "\n";
}
if(staticFileAttempted)
{
ss << "----------------------------------------------------------\n";
ss << "Static File Middleware\n";
ss << "----------------------------------------------------------\n\n";
ss << "File\n";
ss << "-----\n";
ss << left << setw(14) << "Resolved Path" << ": " << resolvedPath << "\n";
ss << left << setw(14) << "Exists" << ": " << (fileExists ? "YES" : "NO") << "\n";
if(fileExists)
{
if(!mimeType.empty()) ss << left << setw(14) << "Mime" << ": " << mimeType << "\n";
ss << left << setw(14) << "Size" << ": " << fileSize << " bytes\n";
}
ss << "\n";
}
if(!metadataCacheStatus.empty())
{
ss << "Metadata Cache\n";
ss << "--------------\n";
ss << left << setw(14) << "Status" << ": " << metadataCacheStatus << "\n\n";
}
if(cacheAttempted)
{
ss << "File Cache\n";
ss << "----------\n";
ss << left << setw(14) << "Status" << ": " << cacheStatus << "\n";
if(!cacheReason.empty())
{
ss << left << setw(14) << "Reason" << ": " << cacheReason << "\n";
}
ss << "\n";
}
if(compressionAttempted)
{
ss << "Compression\n";
ss << "-----------\n";
ss << left << setw(14) << "Supported" << ": " << (compressionSupported ? "YES" : "NO") << "\n";
ss << left << setw(14) << "Used" << ": " << (compressionUsed ? "YES" : "NO") << "\n";
if(compressionUsed)
{
ss << left << setw(14) << "Algorithm" << ": " << compressionAlgorithm << "\n";
ss << left << setw(14) << "Original" << ": " << origCompSize << " bytes\n";
ss << left << setw(14) << "Compressed" << ": " << compSize << " bytes\n";
if(origCompSize > 0)
{
int savedPct = static_cast<int>(round((1.0 - (double)compSize / origCompSize) * 100.0));
ss << left << setw(14) << "Saved" << ": " << savedPct << "%\n";
}
}
ss << "\n";
}
if(cachingCheckAttempted)
{
ss << "Caching\n";
ss << "-------\n";
if(!etag.empty()) ss << left << setw(14) << "ETag" << ": " << etag << "\n";
if(!lastModified.empty()) ss << left << setw(14) << "LastModified" << ": " << lastModified << "\n";
ss << "\n";
ss << "Client Cache\n";
ss << "------------\n";
ss << left << setw(14) << "ETag Match" << ": " << (etagMatch ? "YES" : "NO") << "\n";
if(!etagMatch)
{
ss << left << setw(14) << "Modified Match" << ": " << (modifiedMatch ? "YES" : "NO") << "\n";
}
ss << "\n";
}
if(rangeAttempted)
{
ss << "Range\n";
ss << "-----\n";
ss << left << setw(14) << "Start" << ": " << rangeStart << "\n";
ss << left << setw(14) << "End" << ": " << rangeEnd << "\n";
if(rangeTotalSize > 0) ss << left << setw(14) << "Total Size" << ": " << rangeTotalSize << " bytes\n";
ss << "\n";
}
if(routeAttempted)
{
ss << "Router\n";
ss << "------\n";
ss << left << setw(16) << "Matched Route" << ": " << (routeMatched ? matchedRoute : "NONE") << "\n\n";
if(!routeParams.empty())
{
ss << "Parameters\n";
ss << "----------\n";
for(const auto& p : routeParams)
{
ss << left << setw(14) << p.first << "= " << p.second << "\n";
}
ss << "\n";
}
}
if(!decision.empty())
{
ss << "Decision\n";
ss << "--------\n";
ss << decision << "\n\n";
}
ss << "Response\n";
ss << "--------\n";
ss << left << setw(14) << "Status" << ": " << responseStatus << "\n";
if(!responseContentType.empty()) ss << left << setw(14) << "Content-Type" << ": " << responseContentType << "\n";
ss << left << setw(14) << "Body" << ": " << responseBodySize << " bytes\n";
if(!responseConnection.empty()) ss << left << setw(14) << "Connection" << ": " << responseConnection << "\n";
ss << "\n";
ss << fixed << setprecision(2);
ss << "Finished in " << durationMs << " ms\n";
ss << "==========================================================\n\n";
ss << "SUMMARY\n\n";
ss << left << setw(14) << "Client" << ": " << clientIP << "\n";
ss << left << setw(14) << "Request" << ": " << method << " " << url << "\n";
ss << left << setw(14) << "Result" << ": " << responseStatus << "\n\n";
if(cacheAttempted) ss << left << setw(14) << "Cache" << ": " << cacheStatus << "\n";
if(!metadataCacheStatus.empty()) ss << left << setw(14) << "Metadata" << ": " << metadataCacheStatus << "\n";
if(compressionAttempted) ss << left << setw(14) << "Compression" << ": " << (compressionUsed ? "YES" : "NO") << "\n";
if(!responseConnection.empty()) ss << left << setw(14) << "KeepAlive" << ": " << (responseConnection == "keep-alive" ? "YES" : "NO") << "\n";
ss << "\n" << left << setw(14) << "Total Time" << ": " << durationMs << " ms\n";
ss << "==========================================================\n";
return ss.str();
}
void RequestInspector::finishAndLog()
{
Logger::logRaw(generateReport());
}