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

Function streamBodyToFile

3ds/source/server.cpp:153–191  ·  view source on GitHub ↗

Streams the body of an upload request to `tmpPath`, tracking progress and honouring a receive cancel. `leftover` is the body already read while parsing the header. Returns true when the full body was written.

Source from the content-addressed store, hash-verified

151 // honouring a receive cancel. `leftover` is the body already read while
152 // parsing the header. Returns true when the full body was written.
153 static bool streamBodyToFile(s32 clientSocket, const std::u16string& tmpPath, size_t contentLength, const char* leftover, size_t leftoverLen)
154 {
155 FSStream out(Archive::sdmc(), tmpPath, FS_OPEN_WRITE, (u32)contentLength);
156 if (!out.good()) {
157 Logging::error("Failed to open upload temp file (0x{:08X}).", (u32)out.result());
158 return false;
159 }
160
161 TransferStatus::beginNetwork(i18n::t("transfer.downloading"), contentLength);
162
163 size_t written = 0;
164 if (leftoverLen > 0) {
165 size_t toWrite = leftoverLen > contentLength ? contentLength : leftoverLen;
166 out.write(leftover, (u32)toWrite);
167 written += toWrite;
168 TransferStatus::setBytesDone(written);
169 }
170
171 constexpr size_t RECV_CHUNK = 32 * 1024;
172 std::unique_ptr<char[]> buffer(new char[RECV_CHUNK]);
173 int idleMs = 0;
174 bool ok = true;
175 while (written < contentLength) {
176 ssize_t received = pollRecv(clientSocket, buffer.get(), RECV_CHUNK, idleMs, true);
177 if (received <= 0) {
178 ok = false; // clean close, idle timeout, or cancel: incomplete body
179 break;
180 }
181 size_t toWrite = (size_t)received;
182 if (written + toWrite > contentLength) {
183 toWrite = contentLength - written;
184 }
185 out.write(buffer.get(), (u32)toWrite);
186 written += toWrite;
187 TransferStatus::setBytesDone(written);
188 }
189 out.close();
190 return ok && written == contentLength;
191 }
192
193 static void handleHttpRequest(s32 clientSocket)
194 {

Callers 1

handleHttpRequestFunction · 0.70

Calls 9

resultMethod · 0.80
beginNetworkFunction · 0.70
setBytesDoneFunction · 0.70
pollRecvFunction · 0.70
errorFunction · 0.50
goodMethod · 0.45
writeMethod · 0.45
getMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected