MCPcopy Create free account
hub / github.com/BernardoGiordano/Checkpoint / handleHttpRequest

Function handleHttpRequest

switch/source/server.cpp:222–323  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

220 }
221
222 void handleHttpRequest(s32 clientSocket)
223 {
224 // Read only up to and including the header terminator; the body is either
225 // streamed to disk (upload paths) or read into RAM afterwards (others).
226 std::string data;
227 data.reserve(4096);
228 constexpr size_t RECV_CHUNK = 64 * 1024;
229 std::vector<char> buffer(RECV_CHUNK);
230 size_t headerEnd = std::string::npos;
231 int idleMs = 0;
232
233 while (true) {
234 ssize_t received = pollRecv(clientSocket, buffer.data(), buffer.size(), idleMs, false, HEADER_TIMEOUT_MS);
235 if (received <= 0) {
236 break;
237 }
238 data.append(buffer.data(), received);
239 headerEnd = data.find("\r\n\r\n");
240 if (headerEnd != std::string::npos) {
241 break;
242 }
243 if (data.size() > MAX_HEADER_SIZE) {
244 break; // no terminator within the cap: abandon
245 }
246 }
247 if (headerEnd == std::string::npos) {
248 return; // never got a complete header block
249 }
250
251 std::string headers = data.substr(0, headerEnd);
252 std::string path = extractPath(headers);
253 size_t contentLength = parseContentLength(headers);
254 size_t bodyStart = headerEnd + 4;
255
256 // Streaming upload path.
257 std::string tmpPath;
258 Server::UploadHandler uploadHandler;
259 bool isUpload = false;
260 {
261 std::lock_guard<std::mutex> lock(handlersMutex);
262 auto it = uploadHandlers.find(path);
263 if (it != uploadHandlers.end()) {
264 tmpPath = it->second.first;
265 uploadHandler = it->second.second;
266 isUpload = true;
267 }
268 }
269 if (isUpload) {
270 const char* leftover = data.data() + bodyStart;
271 size_t leftoverLen = data.size() - bodyStart;
272 bool complete = streamBodyToFile(clientSocket, tmpPath, contentLength, leftover, leftoverLen);
273 if (!complete) {
274 // Cancelled, stalled, or dropped mid-upload: drop the request and
275 // clear the transfer UI; the handler never runs on a partial body.
276 std::remove(tmpPath.c_str());
277 TransferStatus::end();
278 Logging::info("Upload to {} abandoned before the full body arrived.", path);
279 return;

Callers 1

networkLoopFunction · 0.70

Calls 14

to_stringFunction · 0.85
appendMethod · 0.80
findMethod · 0.80
pollRecvFunction · 0.70
extractPathFunction · 0.70
parseContentLengthFunction · 0.70
streamBodyToFileFunction · 0.70
endFunction · 0.70
sendResponseFunction · 0.70
sendAllFunction · 0.70
infoFunction · 0.50
dataMethod · 0.45

Tested by

no test coverage detected