| 4280 | bool is_valid() const { return is_valid_; } |
| 4281 | |
| 4282 | bool parse(const char *buf, size_t n, const ContentReceiver &content_callback, |
| 4283 | const MultipartContentHeader &header_callback) { |
| 4284 | |
| 4285 | buf_append(buf, n); |
| 4286 | |
| 4287 | while (buf_size() > 0) { |
| 4288 | switch (state_) { |
| 4289 | case 0: { // Initial boundary |
| 4290 | buf_erase(buf_find(dash_boundary_crlf_)); |
| 4291 | if (dash_boundary_crlf_.size() > buf_size()) { return true; } |
| 4292 | if (!buf_start_with(dash_boundary_crlf_)) { return false; } |
| 4293 | buf_erase(dash_boundary_crlf_.size()); |
| 4294 | state_ = 1; |
| 4295 | break; |
| 4296 | } |
| 4297 | case 1: { // New entry |
| 4298 | clear_file_info(); |
| 4299 | state_ = 2; |
| 4300 | break; |
| 4301 | } |
| 4302 | case 2: { // Headers |
| 4303 | auto pos = buf_find(crlf_); |
| 4304 | if (pos > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } |
| 4305 | while (pos < buf_size()) { |
| 4306 | // Empty line |
| 4307 | if (pos == 0) { |
| 4308 | if (!header_callback(file_)) { |
| 4309 | is_valid_ = false; |
| 4310 | return false; |
| 4311 | } |
| 4312 | buf_erase(crlf_.size()); |
| 4313 | state_ = 3; |
| 4314 | break; |
| 4315 | } |
| 4316 | |
| 4317 | static const std::string header_name = "content-type:"; |
| 4318 | const auto header = buf_head(pos); |
| 4319 | if (start_with_case_ignore(header, header_name)) { |
| 4320 | file_.content_type = trim_copy(header.substr(header_name.size())); |
| 4321 | } else { |
| 4322 | static const std::regex re_content_disposition( |
| 4323 | R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~", |
| 4324 | std::regex_constants::icase); |
| 4325 | |
| 4326 | std::smatch m; |
| 4327 | if (std::regex_match(header, m, re_content_disposition)) { |
| 4328 | Params params; |
| 4329 | parse_disposition_params(m[1], params); |
| 4330 | |
| 4331 | auto it = params.find("name"); |
| 4332 | if (it != params.end()) { |
| 4333 | file_.name = it->second; |
| 4334 | } else { |
| 4335 | is_valid_ = false; |
| 4336 | return false; |
| 4337 | } |
| 4338 | |
| 4339 | it = params.find("filename"); |
no test coverage detected