* This example shows how to make multiple requests * using curl_multi interface. */
| 16 | * using curl_multi interface. |
| 17 | */ |
| 18 | int main() { |
| 19 | std::vector<std::string> urls; |
| 20 | urls.emplace_back("https://google.com"); |
| 21 | urls.emplace_back("https://facebook.com"); |
| 22 | urls.emplace_back("https://linkedin.com"); |
| 23 | |
| 24 | // Create a vector of curl easy handlers. |
| 25 | std::vector<curl_easy> handlers; |
| 26 | |
| 27 | // Create a vector of curl streams. |
| 28 | std::vector<curl_ios<std::ostringstream>> streams; |
| 29 | |
| 30 | // Create the curl easy handler and associated the streams with it. |
| 31 | for (const auto & url : urls) { |
| 32 | auto *output_stream = new std::ostringstream; |
| 33 | curl_ios<std::ostringstream> curl_stream(*output_stream); |
| 34 | |
| 35 | curl_easy easy(curl_stream); |
| 36 | easy.add<CURLOPT_URL>(url.c_str()); |
| 37 | easy.add<CURLOPT_FOLLOWLOCATION>(1L); |
| 38 | |
| 39 | streams.emplace_back(curl_stream); |
| 40 | handlers.emplace_back(easy); |
| 41 | } |
| 42 | |
| 43 | // Create a map of curl pointers to output streams. |
| 44 | std::unordered_map<CURL*, curl_ios<std::ostringstream>*> easy_streams; |
| 45 | for (int i = 0; i < handlers.size(); ++i) { |
| 46 | easy_streams[handlers.at(i).get_curl()] = (curl_ios<std::ostringstream>*)&streams.at(i); |
| 47 | } |
| 48 | |
| 49 | // Add all the handlers to the curl multi object. |
| 50 | curl_multi multi; |
| 51 | multi.add(handlers); |
| 52 | |
| 53 | try { |
| 54 | // Start the transfers. |
| 55 | multi.perform(); |
| 56 | |
| 57 | // Until there are active transfers, call the perform() API. |
| 58 | while (multi.get_active_transfers()) { |
| 59 | multi.perform(); |
| 60 | |
| 61 | // Extracts the first finished request. |
| 62 | std::unique_ptr<curl_multi::curl_message> message = multi.get_next_finished(); |
| 63 | if (message != nullptr) { |
| 64 | const curl_easy *handler = message->get_handler(); |
| 65 | |
| 66 | // Get the stream associated with the curl easy handler. |
| 67 | curl_ios<std::ostringstream> stream_handler = *easy_streams[handler->get_curl()]; |
| 68 | |
| 69 | auto content = stream_handler.get_stream()->str(); |
| 70 | auto url = handler->get_info<CURLINFO_EFFECTIVE_URL>(); |
| 71 | auto response_code = handler->get_info<CURLINFO_RESPONSE_CODE>(); |
| 72 | auto content_type = handler->get_info<CURLINFO_CONTENT_TYPE>(); |
| 73 | auto http_code = handler->get_info<CURLINFO_HTTP_CODE>(); |
| 74 | |
| 75 | std::cout << "CODE: " << response_code.get() |
nothing calls this directly
no test coverage detected