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

Function handleHttpRequest

3ds/source/server.cpp:193–294  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

191 }
192
193 static void handleHttpRequest(s32 clientSocket)
194 {
195 // Read only up to and including the header terminator; the body is either
196 // streamed to disk (upload paths) or read into RAM afterwards (others).
197 std::string data;
198 data.reserve(4096);
199 constexpr size_t RECV_CHUNK = 32 * 1024;
200 std::unique_ptr<char[]> buffer(new char[RECV_CHUNK]);
201 size_t headerEnd = std::string::npos;
202 int idleMs = 0;
203
204 while (true) {
205 ssize_t received = pollRecv(clientSocket, buffer.get(), RECV_CHUNK, idleMs, false);
206 if (received <= 0) {
207 break;
208 }
209 data.append(buffer.get(), received);
210 headerEnd = data.find("\r\n\r\n");
211 if (headerEnd != std::string::npos) {
212 break;
213 }
214 if (data.size() > MAX_HEADER_SIZE) {
215 break;
216 }
217 }
218 if (headerEnd == std::string::npos) {
219 return;
220 }
221
222 std::string headers = data.substr(0, headerEnd);
223 std::string path = extractPath(headers);
224 size_t contentLength = parseContentLength(headers);
225 size_t bodyStart = headerEnd + 4;
226
227 // Streaming upload path.
228 std::string tmpPath;
229 Server::UploadHandler uploadHandler;
230 bool isUpload = false;
231 {
232 std::lock_guard<std::mutex> lock(handlersMutex);
233 auto it = uploadHandlers.find(path);
234 if (it != uploadHandlers.end()) {
235 tmpPath = it->second.first;
236 uploadHandler = it->second.second;
237 isUpload = true;
238 }
239 }
240 if (isUpload) {
241 std::u16string tmpU16 = StringUtils::UTF8toUTF16(tmpPath.c_str());
242 const char* leftover = data.data() + bodyStart;
243 size_t leftoverLen = data.size() - bodyStart;
244 bool complete = streamBodyToFile(clientSocket, tmpU16, contentLength, leftover, leftoverLen);
245 if (!complete) {
246 // Cancelled, stalled, or dropped mid-upload: drop the request and
247 // clear the transfer UI; the handler never runs on a partial body.
248 FSUSER_DeleteFile(Archive::sdmc(), fsMakePath(PATH_UTF16, tmpU16.data()));
249 TransferStatus::end();
250 Logging::info("Upload abandoned before the full body arrived.");

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
infoFunction · 0.50
getMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected