| 275 | } |
| 276 | |
| 277 | bool parseMultipart(const std::string& head, uint64_t bodyLen, const std::string& boundary, std::string& outMeta, uint64_t& outFileOffset, |
| 278 | uint64_t& outFileLen, std::string& outError) |
| 279 | { |
| 280 | outMeta.clear(); |
| 281 | outFileOffset = 0; |
| 282 | outFileLen = 0; |
| 283 | |
| 284 | std::string boundaryMarker = "--" + boundary; |
| 285 | std::string nextBoundaryMarker = "\r\n" + boundaryMarker; |
| 286 | |
| 287 | // Meta part. |
| 288 | size_t metaPos = head.find(boundaryMarker); |
| 289 | if (metaPos == std::string::npos) { |
| 290 | outError = "Missing boundary."; |
| 291 | return false; |
| 292 | } |
| 293 | // File part header. |
| 294 | size_t filePos = head.find("name=\"file\""); |
| 295 | if (filePos == std::string::npos) { |
| 296 | outError = "Incomplete form data."; |
| 297 | return false; |
| 298 | } |
| 299 | // Meta content: between the meta part's header terminator and the boundary |
| 300 | // that precedes the file part. |
| 301 | size_t metaHeaderEnd = head.find("\r\n\r\n", metaPos); |
| 302 | if (metaHeaderEnd == std::string::npos) { |
| 303 | outError = "Incomplete form data."; |
| 304 | return false; |
| 305 | } |
| 306 | size_t metaDataStart = metaHeaderEnd + 4; |
| 307 | size_t metaDataEnd = head.find(nextBoundaryMarker, metaDataStart); |
| 308 | if (metaDataEnd == std::string::npos || metaDataEnd > filePos) { |
| 309 | outError = "Incomplete form data."; |
| 310 | return false; |
| 311 | } |
| 312 | outMeta = head.substr(metaDataStart, metaDataEnd - metaDataStart); |
| 313 | |
| 314 | // File data begins right after the file part's header terminator. |
| 315 | size_t fileHeaderEnd = head.find("\r\n\r\n", filePos); |
| 316 | if (fileHeaderEnd == std::string::npos) { |
| 317 | outError = "Incomplete form data."; |
| 318 | return false; |
| 319 | } |
| 320 | uint64_t fileDataStart = (uint64_t)fileHeaderEnd + 4; |
| 321 | |
| 322 | // The body ends with "\r\n--boundary--\r\n"; the file data is everything |
| 323 | // between fileDataStart and that trailing delimiter. |
| 324 | std::string trailer = "\r\n" + boundaryMarker + "--\r\n"; |
| 325 | if (bodyLen < fileDataStart + trailer.size()) { |
| 326 | outError = "Incomplete form data."; |
| 327 | return false; |
| 328 | } |
| 329 | outFileOffset = fileDataStart; |
| 330 | outFileLen = bodyLen - fileDataStart - trailer.size(); |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | bool extractZip(ByteReader& in, uint64_t limit, ExtractSink& sink, const CancelFn& cancelled, const ProgressFn& onBytes, std::string& outError) |
no test coverage detected