From 61377c582e0f2675baa5f5e6e30a35d1a4164b33 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Mon, 1 May 2017 16:08:43 +0300 Subject: Add hxx extension for headers and lib prefix for library dir --- libbutl/curl.txx | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 libbutl/curl.txx (limited to 'libbutl/curl.txx') diff --git a/libbutl/curl.txx b/libbutl/curl.txx new file mode 100644 index 0000000..5fd81dc --- /dev/null +++ b/libbutl/curl.txx @@ -0,0 +1,99 @@ +// file : libbutl/curl.txx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include // move(), forward() +#include // invalid_argument + +namespace butl +{ + template + typename std::enable_if::value, I>::type curl:: + map_in (I&& in, method_proto mp, io_data& d) + { + switch (mp) + { + case ftp_put: + { + d.options.push_back ("--upload-file"); + d.options.push_back ("-"); + break; + } + case http_post: + { + d.options.push_back ("--data-binary"); + d.options.push_back ("@-"); + break; + } + case ftp_get: + case http_get: + { + throw std::invalid_argument ("input specified for GET method"); + } + } + + return std::forward (in); + } + + template + typename std::enable_if::value, O>::type curl:: + map_out (O&& out, method_proto mp, io_data&) + { + switch (mp) + { + case ftp_get: + case http_get: + case http_post: + { + // Note: no need for any options, curl writes to stdout by default. + // + break; + } + case ftp_put: + { + throw std::invalid_argument ("output specified for PUT method"); + } + } + + return std::forward (out); + } + + template + curl:: + curl (const C& cmdc, + I&& in, + O&& out, + E&& err, + method_type m, + const std::string& url, + A&&... options) + { + method_proto_options mpo; + method_proto mp (translate (m, url, mpo)); + + io_data in_data; + io_data out_data; + + process& p (*this); + p = process_start ( + cmdc, + map_in (std::forward (in), mp, in_data), + map_out (std::forward (out), mp, out_data), + std::forward (err), + dir_path (), + "curl", + "-s", // Silent. + "-S", // But do show diagnostics. + mpo, + in_data.options, + out_data.options, + std::forward (options)..., + url); + + // Note: leaving this scope closes any open ends of the pipes in io_data. + } +} -- cgit v1.1