2018-02-07 10:37:15 +00:00
|
|
|
#ifndef slic3r_Http_hpp_
|
|
|
|
#define slic3r_Http_hpp_
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
2018-06-12 10:32:03 +00:00
|
|
|
#include <boost/filesystem/path.hpp>
|
2018-02-07 10:37:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
|
|
|
|
/// Represetns a Http request
|
|
|
|
class Http : public std::enable_shared_from_this<Http> {
|
|
|
|
private:
|
|
|
|
struct priv;
|
|
|
|
public:
|
2018-04-04 09:18:22 +00:00
|
|
|
struct Progress
|
|
|
|
{
|
2018-05-23 10:47:39 +00:00
|
|
|
size_t dltotal; // Total bytes to download
|
|
|
|
size_t dlnow; // Bytes downloaded so far
|
|
|
|
size_t ultotal; // Total bytes to upload
|
|
|
|
size_t ulnow; // Bytes uploaded so far
|
|
|
|
|
2018-04-04 09:18:22 +00:00
|
|
|
Progress(size_t dltotal, size_t dlnow, size_t ultotal, size_t ulnow) :
|
|
|
|
dltotal(dltotal), dlnow(dlnow), ultotal(ultotal), ulnow(ulnow)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-02-07 10:37:15 +00:00
|
|
|
typedef std::shared_ptr<Http> Ptr;
|
|
|
|
typedef std::function<void(std::string /* body */, unsigned /* http_status */)> CompleteFn;
|
2018-12-14 14:27:34 +00:00
|
|
|
|
2018-06-27 15:00:20 +00:00
|
|
|
// A HTTP request may fail at various stages of completeness (URL parsing, DNS lookup, TCP connection, ...).
|
|
|
|
// If the HTTP request could not be made or failed before completion, the `error` arg contains a description
|
|
|
|
// of the error and `http_status` is zero.
|
|
|
|
// If the HTTP request was completed but the response HTTP code is >= 400, `error` is empty and `http_status` contains the response code.
|
|
|
|
// In either case there may or may not be a body.
|
2018-02-07 10:37:15 +00:00
|
|
|
typedef std::function<void(std::string /* body */, std::string /* error */, unsigned /* http_status */)> ErrorFn;
|
2018-06-27 15:00:20 +00:00
|
|
|
|
|
|
|
// See the Progress struct above.
|
|
|
|
// Writing true to the `cancel` reference cancels the request in progress.
|
2018-04-04 09:18:22 +00:00
|
|
|
typedef std::function<void(Progress, bool& /* cancel */)> ProgressFn;
|
2018-02-07 10:37:15 +00:00
|
|
|
|
|
|
|
Http(Http &&other);
|
|
|
|
|
2018-06-12 10:32:03 +00:00
|
|
|
// Note: strings are expected to be UTF-8-encoded
|
|
|
|
|
2018-05-23 10:47:39 +00:00
|
|
|
// These are the primary constructors that create a HTTP object
|
|
|
|
// for a GET and a POST request respectively.
|
2018-02-07 10:37:15 +00:00
|
|
|
static Http get(std::string url);
|
|
|
|
static Http post(std::string url);
|
|
|
|
~Http();
|
|
|
|
|
|
|
|
Http(const Http &) = delete;
|
|
|
|
Http& operator=(const Http &) = delete;
|
|
|
|
Http& operator=(Http &&) = delete;
|
|
|
|
|
2018-12-19 14:40:07 +00:00
|
|
|
// Sets a maximum connection timeout in seconds
|
2018-12-21 11:00:54 +00:00
|
|
|
Http& timeout_connect(long timeout);
|
2018-06-27 08:34:21 +00:00
|
|
|
// Sets a maximum size of the data that can be received.
|
|
|
|
// A value of zero sets the default limit, which is is 5MB.
|
2018-02-07 10:37:15 +00:00
|
|
|
Http& size_limit(size_t sizeLimit);
|
2018-05-23 10:47:39 +00:00
|
|
|
// Sets a HTTP header field.
|
2018-02-07 10:37:15 +00:00
|
|
|
Http& header(std::string name, const std::string &value);
|
2018-05-23 10:47:39 +00:00
|
|
|
// Removes a header field.
|
2018-02-07 10:37:15 +00:00
|
|
|
Http& remove_header(std::string name);
|
2018-05-23 10:47:39 +00:00
|
|
|
// Sets a CA certificate file for usage with HTTPS. This is only supported on some backends,
|
|
|
|
// specifically, this is supported with OpenSSL and NOT supported with Windows and OS X native certificate store.
|
|
|
|
// See also ca_file_supported().
|
2018-02-07 10:37:15 +00:00
|
|
|
Http& ca_file(const std::string &filename);
|
2018-05-23 10:47:39 +00:00
|
|
|
// Add a HTTP multipart form field
|
2018-02-07 10:37:15 +00:00
|
|
|
Http& form_add(const std::string &name, const std::string &contents);
|
2018-06-12 10:32:03 +00:00
|
|
|
// Add a HTTP multipart form file data contents, `name` is the name of the part
|
|
|
|
Http& form_add_file(const std::string &name, const boost::filesystem::path &path);
|
2018-05-23 10:47:39 +00:00
|
|
|
// Same as above except also override the file's filename with a custom one
|
2018-06-12 10:32:03 +00:00
|
|
|
Http& form_add_file(const std::string &name, const boost::filesystem::path &path, const std::string &filename);
|
2018-02-07 10:37:15 +00:00
|
|
|
|
2018-08-21 09:10:32 +00:00
|
|
|
// Set the file contents as a POST request body.
|
|
|
|
// The data is used verbatim, it is not additionally encoded in any way.
|
|
|
|
// This can be used for hosts which do not support multipart requests.
|
|
|
|
Http& set_post_body(const boost::filesystem::path &path);
|
2018-07-08 12:32:48 +00:00
|
|
|
|
2018-05-23 10:47:39 +00:00
|
|
|
// Callback called on HTTP request complete
|
2018-02-07 10:37:15 +00:00
|
|
|
Http& on_complete(CompleteFn fn);
|
2018-05-23 10:47:39 +00:00
|
|
|
// Callback called on an error occuring at any stage of the requests: Url parsing, DNS lookup,
|
|
|
|
// TCP connection, HTTP transfer, and finally also when the response indicates an error (status >= 400).
|
|
|
|
// Therefore, a response body may or may not be present.
|
2018-02-07 10:37:15 +00:00
|
|
|
Http& on_error(ErrorFn fn);
|
2018-05-23 10:47:39 +00:00
|
|
|
// Callback called on data download/upload prorgess (called fairly frequently).
|
|
|
|
// See the `Progress` structure for description of the data passed.
|
|
|
|
// Writing a true-ish value into the cancel reference parameter cancels the request.
|
2018-04-04 09:18:22 +00:00
|
|
|
Http& on_progress(ProgressFn fn);
|
2018-02-07 10:37:15 +00:00
|
|
|
|
2018-05-23 10:47:39 +00:00
|
|
|
// Starts performing the request in a background thread
|
2018-02-07 10:37:15 +00:00
|
|
|
Ptr perform();
|
2018-05-23 10:47:39 +00:00
|
|
|
// Starts performing the request on the current thread
|
2018-02-07 10:37:15 +00:00
|
|
|
void perform_sync();
|
2018-05-23 10:47:39 +00:00
|
|
|
// Cancels a request in progress
|
2018-04-04 09:18:22 +00:00
|
|
|
void cancel();
|
2018-02-07 10:37:15 +00:00
|
|
|
|
2018-05-23 10:47:39 +00:00
|
|
|
// Tells whether current backend supports seting up a CA file using ca_file()
|
2018-03-15 17:06:26 +00:00
|
|
|
static bool ca_file_supported();
|
2018-09-04 21:55:37 +00:00
|
|
|
|
|
|
|
// converts the given string to an url_encoded_string
|
|
|
|
static std::string url_encode(const std::string &str);
|
2018-02-07 10:37:15 +00:00
|
|
|
private:
|
|
|
|
Http(const std::string &url);
|
|
|
|
|
|
|
|
std::unique_ptr<priv> p;
|
|
|
|
};
|
|
|
|
|
2018-04-04 09:18:22 +00:00
|
|
|
std::ostream& operator<<(std::ostream &, const Http::Progress &);
|
|
|
|
|
2018-02-07 10:37:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|