MCPcopy Create free account
hub / github.com/bmorcelli/Launcher / streamMultipartUpload

Function streamMultipartUpload

src/webInterface.cpp:387–453  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

385}
386
387bool streamMultipartUpload(httpd_req_t *req) {
388 if (!checkUserWebAuth(req)) return false;
389
390 String boundary = multipartBoundary(headerValue(req, "Content-Type"));
391 if (boundary.isEmpty()) {
392 sendText(req, 400, "text/plain", "Missing multipart boundary");
393 return false;
394 }
395
396 const String delimiter = "\r\n" + boundary;
397 const size_t keep = delimiter.length() + 8;
398 std::vector<uint8_t> pending;
399 pending.reserve(bufSize + keep + 256);
400 File file;
401 bool inFile = false;
402 bool finishedFile = false;
403 size_t written = 0;
404 size_t remaining = req->content_len;
405
406 while (remaining > 0) {
407 int readLen =
408 httpd_req_recv(req, reinterpret_cast<char *>(buff), remaining > bufSize ? bufSize : remaining);
409 if (readLen <= 0) return false;
410 pending.insert(pending.end(), buff, buff + readLen);
411 remaining -= readLen;
412
413 while (!finishedFile) {
414 if (!inFile) {
415 int headerEnd = findBytes(pending, "\r\n\r\n");
416 if (headerEnd < 0) break;
417 String headers;
418 headers.reserve(headerEnd);
419 for (int i = 0; i < headerEnd; ++i) headers += static_cast<char>(pending[i]);
420 String filename = extractDispositionValue(headers, "filename");
421 pending.erase(pending.begin(), pending.begin() + headerEnd + 4);
422 if (filename.isEmpty()) continue;
423 if (!beginUploadTarget(file, filename)) return false;
424 inFile = true;
425 }
426
427 int boundaryAt = findBytes(pending, delimiter);
428 if (boundaryAt >= 0) {
429 if (boundaryAt > 0 && !writeUploadData(file, pending.data(), boundaryAt, written))
430 return false;
431 written += boundaryAt;
432 pending.erase(pending.begin(), pending.begin() + boundaryAt + delimiter.length());
433 finishedFile = finishUploadTarget(file);
434 break;
435 }
436
437 if (pending.size() > keep) {
438 size_t writeLen = pending.size() - keep;
439 if (!writeUploadData(file, pending.data(), writeLen, written)) return false;
440 written += writeLen;
441 pending.erase(pending.begin(), pending.begin() + writeLen);
442 }
443 break;
444 }

Callers 2

otaFileHandlerFunction · 0.85
rootHandlerFunction · 0.85

Calls 11

checkUserWebAuthFunction · 0.85
multipartBoundaryFunction · 0.85
headerValueFunction · 0.85
sendTextFunction · 0.85
findBytesFunction · 0.85
extractDispositionValueFunction · 0.85
beginUploadTargetFunction · 0.85
writeUploadDataFunction · 0.85
finishUploadTargetFunction · 0.85
endMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected