Http.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #ifndef slic3r_Http_hpp_
  2. #define slic3r_Http_hpp_
  3. #include <memory>
  4. #include <string>
  5. #include <functional>
  6. #include <boost/filesystem/path.hpp>
  7. namespace Slic3r {
  8. /// Represetns a Http request
  9. class Http : public std::enable_shared_from_this<Http> {
  10. private:
  11. struct priv;
  12. public:
  13. struct Progress
  14. {
  15. size_t dltotal; // Total bytes to download
  16. size_t dlnow; // Bytes downloaded so far
  17. size_t ultotal; // Total bytes to upload
  18. size_t ulnow; // Bytes uploaded so far
  19. Progress(size_t dltotal, size_t dlnow, size_t ultotal, size_t ulnow) :
  20. dltotal(dltotal), dlnow(dlnow), ultotal(ultotal), ulnow(ulnow)
  21. {}
  22. };
  23. typedef std::shared_ptr<Http> Ptr;
  24. typedef std::function<void(std::string /* body */, unsigned /* http_status */)> CompleteFn;
  25. // A HTTP request may fail at various stages of completeness (URL parsing, DNS lookup, TCP connection, ...).
  26. // If the HTTP request could not be made or failed before completion, the `error` arg contains a description
  27. // of the error and `http_status` is zero.
  28. // If the HTTP request was completed but the response HTTP code is >= 400, `error` is empty and `http_status` contains the response code.
  29. // In either case there may or may not be a body.
  30. typedef std::function<void(std::string /* body */, std::string /* error */, unsigned /* http_status */)> ErrorFn;
  31. // See the Progress struct above.
  32. // Writing true to the `cancel` reference cancels the request in progress.
  33. typedef std::function<void(Progress, bool& /* cancel */)> ProgressFn;
  34. Http(Http &&other);
  35. // Note: strings are expected to be UTF-8-encoded
  36. // These are the primary constructors that create a HTTP object
  37. // for a GET and a POST request respectively.
  38. static Http get(std::string url);
  39. static Http post(std::string url);
  40. static Http put(std::string url);
  41. ~Http();
  42. Http(const Http &) = delete;
  43. Http& operator=(const Http &) = delete;
  44. Http& operator=(Http &&) = delete;
  45. // Sets a maximum connection timeout in seconds
  46. Http& timeout_connect(long timeout);
  47. // Sets a maximum size of the data that can be received.
  48. // A value of zero sets the default limit, which is is 5MB.
  49. Http& size_limit(size_t sizeLimit);
  50. // Sets a HTTP header field.
  51. Http& header(std::string name, const std::string &value);
  52. // Removes a header field.
  53. Http& remove_header(std::string name);
  54. // Authorization by HTTP digest, based on RFC2617.
  55. Http& auth_digest(const std::string &user, const std::string &password);
  56. // Basic HTTP authorization
  57. Http& auth_basic(const std::string &user, const std::string &password);
  58. // Sets a CA certificate file for usage with HTTPS. This is only supported on some backends,
  59. // specifically, this is supported with OpenSSL and NOT supported with Windows and OS X native certificate store.
  60. // See also ca_file_supported().
  61. Http& ca_file(const std::string &filename);
  62. // Add a HTTP multipart form field
  63. Http& form_add(const std::string &name, const std::string &contents);
  64. // Add a HTTP multipart form file data contents, `name` is the name of the part
  65. Http& form_add_file(const std::string &name, const boost::filesystem::path &path);
  66. // Same as above except also override the file's filename with a custom one
  67. Http& form_add_file(const std::string &name, const boost::filesystem::path &path, const std::string &filename);
  68. // Set the file contents as a POST request body.
  69. // The data is used verbatim, it is not additionally encoded in any way.
  70. // This can be used for hosts which do not support multipart requests.
  71. Http& set_post_body(const boost::filesystem::path &path);
  72. // Set the POST request body.
  73. // The data is used verbatim, it is not additionally encoded in any way.
  74. // This can be used for hosts which do not support multipart requests.
  75. Http& set_post_body(const std::string &body);
  76. // Set the file contents as a PUT request body.
  77. // The data is used verbatim, it is not additionally encoded in any way.
  78. // This can be used for hosts which do not support multipart requests.
  79. Http& set_put_body(const boost::filesystem::path &path);
  80. // Callback called on HTTP request complete
  81. Http& on_complete(CompleteFn fn);
  82. // Callback called on an error occuring at any stage of the requests: Url parsing, DNS lookup,
  83. // TCP connection, HTTP transfer, and finally also when the response indicates an error (status >= 400).
  84. // Therefore, a response body may or may not be present.
  85. Http& on_error(ErrorFn fn);
  86. // Callback called on data download/upload prorgess (called fairly frequently).
  87. // See the `Progress` structure for description of the data passed.
  88. // Writing a true-ish value into the cancel reference parameter cancels the request.
  89. Http& on_progress(ProgressFn fn);
  90. // Starts performing the request in a background thread
  91. Ptr perform();
  92. // Starts performing the request on the current thread
  93. void perform_sync();
  94. // Cancels a request in progress
  95. void cancel();
  96. // Tells whether current backend supports seting up a CA file using ca_file()
  97. static bool ca_file_supported();
  98. // Return empty string on success or error message on fail.
  99. static std::string tls_global_init();
  100. static std::string tls_system_cert_store();
  101. // converts the given string to an url_encoded_string
  102. static std::string url_encode(const std::string &str);
  103. private:
  104. Http(const std::string &url);
  105. std::unique_ptr<priv> p;
  106. };
  107. std::ostream& operator<<(std::ostream &, const Http::Progress &);
  108. }
  109. #endif