From 221522cf995cbcd39c956f66423a26bbccae8f72 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 16 Oct 2020 04:49:14 +0200 Subject: Matrix: stream download to rapidjson parser --- src/DownloadUtils.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src/DownloadUtils.cpp') diff --git a/src/DownloadUtils.cpp b/src/DownloadUtils.cpp index c44bed5..4f8b5b9 100644 --- a/src/DownloadUtils.cpp +++ b/src/DownloadUtils.cpp @@ -3,6 +3,8 @@ #include "../include/Storage.hpp" #include "../include/base64_url.hpp" #include +#include +#include static const bool debug_download = false; @@ -77,4 +79,47 @@ namespace QuickMedia { return download_result; } } + + // TODO: Add timeout + DownloadResult download_to_json(const std::string &url, rapidjson::Document &result, const std::vector &additional_args, bool use_tor, bool use_browser_useragent, bool fail_on_error) { + sf::Clock timer; + std::vector args; + if(use_tor) + args.push_back("torsocks"); + args.insert(args.end(), { "curl", "-H", "Accept-Language: en-US,en;q=0.5", "-H", "Connection: keep-alive", "--compressed", "-s", "-L" }); + if(fail_on_error) + args.push_back("-f"); + for(const CommandArg &arg : additional_args) { + args.push_back(arg.option.c_str()); + args.push_back(arg.value.c_str()); + } + if(use_browser_useragent) { + args.push_back("-H"); + args.push_back(useragent_str); + } + args.push_back("--"); + args.push_back(url.c_str()); + args.push_back(nullptr); + if(debug_download) { + for(const char *arg : args) { + if(arg) + fprintf(stderr, "'%s' ", arg); + } + fprintf(stderr, "\n"); + } + ReadProgram read_program; + if(exec_program_pipe(args.data(), &read_program) != 0) + return DownloadResult::NET_ERR; + + FILE *file = fdopen(read_program.read_fd, "rb"); + char read_buffer[8192]; + rapidjson::FileReadStream is(file, read_buffer, sizeof(read_buffer)); + rapidjson::ParseResult parse_result = result.ParseStream(is); + + wait_program(read_program.pid); + fclose(file); + fprintf(stderr, "Download duration for %s: %d ms\n", url.c_str(), timer.getElapsedTime().asMilliseconds()); + + return parse_result.IsError() ? DownloadResult::ERR : DownloadResult::OK; + } } \ No newline at end of file -- cgit v1.2.3