| 292 | } |
| 293 | |
| 294 | bool http_mime::save_file(ostream &out, const std::string& bound, |
| 295 | const std::pair<std::string, pair_value>& file) { |
| 296 | string buf; |
| 297 | buf.copy(bound.c_str(), bound.size()); |
| 298 | buf.append("\r\n"); |
| 299 | |
| 300 | const char disposition[] = "Content-Disposition: form-data; name=\""; |
| 301 | buf.append(disposition, sizeof(disposition) - 1); |
| 302 | if (!file.first.empty()) { |
| 303 | buf.append(file.first.c_str(), file.first.size()); |
| 304 | } |
| 305 | buf.append("\"; filename=\""); |
| 306 | if (!file.second.first.empty()) { |
| 307 | buf.append(file.second.first.c_str(), file.second.first.size()); |
| 308 | } |
| 309 | buf.append("\"\r\n"); |
| 310 | |
| 311 | buf.append("Content-Type: "); |
| 312 | std::string ctype; |
| 313 | get_ctype(file.second.first.c_str(), ctype); |
| 314 | buf.append(ctype.c_str(), ctype.size()); |
| 315 | buf.append("\r\n\r\n"); |
| 316 | |
| 317 | if (file.second.second.empty()) { |
| 318 | buf.append("\r\n"); |
| 319 | if (out.write(buf) == false) { |
| 320 | logger_error("write to file error: %s", last_serror()); |
| 321 | return false; |
| 322 | } |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | if (out.write(buf) == false) { |
| 327 | logger_error("write to file error: %s", last_serror()); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | const char* filepath = file.second.second.c_str(); |
| 332 | ifstream in; |
| 333 | if (!in.open_read(filepath)) { |
| 334 | logger_error("open %s failed(%s)", filepath, last_serror()); |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | char buffer[8192]; |
| 339 | while (!in.eof()) { |
| 340 | const int ret = in.read(buffer, sizeof(buffer), false); |
| 341 | if (ret == -1) { |
| 342 | break; |
| 343 | } |
| 344 | if (out.write(buffer, ret) == -1) { |
| 345 | logger_error("write to file error: %s", last_serror()); |
| 346 | return false; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | const char crln[] = "\r\n"; |
| 351 | if (out.write(crln, sizeof(crln) - 1) == -1) { |