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

Function streamBodyToFile

switch/source/server.cpp:181–220  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

179 // honouring a receive cancel. `leftover` is the body bytes already read while
180 // parsing the header. Returns true when the full body was written.
181 bool streamBodyToFile(s32 clientSocket, const std::string& tmpPath, size_t contentLength, const char* leftover, size_t leftoverLen)
182 {
183 FILE* out = fopen(tmpPath.c_str(), "wb");
184 if (out == nullptr) {
185 Logging::error("Failed to open upload temp file {} with errno {}.", tmpPath, errno);
186 return false;
187 }
188
189 TransferStatus::beginNetwork(i18n::t("transfer.downloading"), contentLength);
190
191 size_t written = 0;
192 if (leftoverLen > 0) {
193 size_t toWrite = leftoverLen > contentLength ? contentLength : leftoverLen;
194 fwrite(leftover, 1, toWrite, out);
195 written += toWrite;
196 TransferStatus::setBytesDone(written);
197 }
198
199 constexpr size_t RECV_CHUNK = 64 * 1024;
200 std::vector<char> buffer(RECV_CHUNK);
201 int idleMs = 0;
202 bool ok = true;
203 while (written < contentLength) {
204 ssize_t received = pollRecv(clientSocket, buffer.data(), buffer.size(), idleMs, true);
205 if (received <= 0) {
206 ok = false; // clean close, idle timeout, or cancel: incomplete body
207 break;
208 }
209 size_t toWrite = (size_t)received;
210 if (written + toWrite > contentLength) {
211 toWrite = contentLength - written;
212 }
213 fwrite(buffer.data(), 1, toWrite, out);
214 written += toWrite;
215 TransferStatus::setBytesDone(written);
216 }
217
218 fclose(out);
219 return ok && written == contentLength;
220 }
221
222 void handleHttpRequest(s32 clientSocket)
223 {

Callers 1

handleHttpRequestFunction · 0.70

Calls 6

beginNetworkFunction · 0.70
setBytesDoneFunction · 0.70
pollRecvFunction · 0.70
errorFunction · 0.50
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected