| 390 | // ---------------------------------------------------------------------------------------- |
| 391 | |
| 392 | std::string http_client::build_post(std::string& content_type, const string_to_stringmap& postvars, const string_to_stringmap& filenames_in) const |
| 393 | { |
| 394 | if ( postvars.empty() && filenames_in.empty() ) |
| 395 | return std::string(); |
| 396 | |
| 397 | string_to_stringmap filenames = filenames_in; |
| 398 | |
| 399 | // sanitize the files |
| 400 | if ( !filenames.empty() ) |
| 401 | { |
| 402 | string_to_stringmap::iterator var_names = filenames.begin(); |
| 403 | while (var_names != filenames.end()) |
| 404 | { |
| 405 | stringmap::iterator fnames = var_names->second.begin(); |
| 406 | |
| 407 | while( fnames != var_names->second.end() ) |
| 408 | { |
| 409 | FILE *fp = ::fopen(fnames->second.c_str(), "rb"); |
| 410 | if ( fp == NULL ) |
| 411 | { |
| 412 | stringmap::iterator old_one = fnames++; |
| 413 | var_names->second.erase(old_one); |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | fclose(fp); |
| 418 | ++fnames; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | if ( fnames->second.empty() ) |
| 423 | { |
| 424 | string_to_stringmap::iterator old_one = var_names++; |
| 425 | filenames.erase(old_one); |
| 426 | } |
| 427 | else |
| 428 | { |
| 429 | ++var_names; |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | content_type = !filenames.empty() ? "multipart/form-data" : "application/x-www-form-urlencoded"; |
| 435 | std::stringstream postBody; |
| 436 | if ( !filenames.empty() ) |
| 437 | { |
| 438 | std::string mime_boundary = get_random_string(32); |
| 439 | |
| 440 | // First add the form vars |
| 441 | for (string_to_stringmap::const_iterator ci = postvars.begin(); ci != postvars.end(); ++ci) |
| 442 | { |
| 443 | for (stringmap::const_iterator si = ci->second.begin(); si != ci->second.end(); ++si) |
| 444 | { |
| 445 | postBody << "--" << mime_boundary << "\r\n" |
| 446 | "Content-Disposition: form-data; name=\"" << ci->first << "\"\r\n\r\n" |
| 447 | << si->second << "\r\n"; |
| 448 | } |
| 449 | } |