| 235 | return Result::Error(HttpResultCategory, HttpError::MultipartWriterBoundaryMissing); |
| 236 | if (fieldName.isEmpty()) |
| 237 | return Result::Error(HttpResultCategory, HttpError::MultipartWriterFieldNameEmpty); |
| 238 | if (not scHttpMultipartQuotedParameterIsSafe(fieldName)) |
| 239 | return Result::Error(HttpResultCategory, HttpError::MultipartWriterFieldNameUnsafe); |
| 240 | if (not fileName.isEmpty() and not scHttpMultipartQuotedParameterIsSafe(fileName)) |
| 241 | return Result::Error(HttpResultCategory, HttpError::MultipartWriterFileNameUnsafe); |
| 242 | if (not contentType.isEmpty() and not scHttpHeaderValueIsSafe(contentType)) |
| 243 | return Result::Error(HttpResultCategory, HttpError::MultipartWriterContentTypeUnsafe); |
| 244 | if (numParts >= MaxParts) |
| 245 | return Result::Error(HttpResultCategory, HttpError::MultipartWriterPartLimitExceeded); |
| 246 | Part& part = parts[numParts++]; |
| 247 | part.partName = fieldName; |
| 248 | part.fileName = fileName; |
| 249 | part.contentType = contentType; |
| 250 | part.body = body; |
| 251 | return Result(true); |
| 252 | } |
| 253 | |
| 254 | size_t HttpMultipartWriter::getContentLength() const |
| 255 | { |
| 256 | size_t total = 0; |
| 257 | for (size_t idx = 0; idx < numParts; ++idx) |
| 258 | { |
| 259 | const Part& part = parts[idx]; |
| 260 | total += 2 + boundary.sizeInBytes() + 2; // --boundary\r\n |
| 261 | total += sizeof("Content-Disposition: form-data; name=\"") - 1; |
| 262 | total += part.partName.sizeInBytes(); |
| 263 | total += 1; |
| 264 | if (part.fileName.sizeInBytes() > 0) |
| 265 | { |
| 266 | total += sizeof("; filename=\"") - 1; |
| 267 | total += part.fileName.sizeInBytes(); |
| 268 | total += 1; |
| 269 | } |
| 270 | total += 2; |