1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include "lib/curl/Global.hxx"
#include "lib/curl/Request.hxx"
#include "lib/curl/Handler.hxx"
#include "event/Loop.hxx"
#include "util/PrintException.hxx"
#include <cassert>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static EventLoop event_loop;
static std::exception_ptr error;
static bool quit;
class MyResponseHandler final : public HttpResponseHandler {
public:
/* virtual methods from class HttpResponseHandler */
void OnHttpResponse(std::string body) noexcept override;
void OnHttpError(std::exception_ptr e) noexcept override;
};
void
MyResponseHandler::OnHttpResponse(std::string body) noexcept
{
write(STDOUT_FILENO, body.data(), body.size());
event_loop.Break();
quit = true;
}
void
MyResponseHandler::OnHttpError(std::exception_ptr _error) noexcept
{
error = _error;
event_loop.Break();
quit = true;
}
int
main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: run_http_client URL\n");
return EXIT_FAILURE;
}
CurlGlobal curl_global(event_loop, nullptr);
const char *url = argv[1];
MyResponseHandler handler;
CurlRequest request(curl_global, url, {}, handler);
if (!quit)
event_loop.Run();
assert(quit);
if (error) {
PrintException(error);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|