Merge pull request #1195 from mloidl/duet_whitespace

duet upload: allow whitespaces in filenames
This commit is contained in:
Vojtech Kral 2018-09-07 13:05:37 +02:00 committed by GitHub
commit 00c51e06ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 3 deletions

View File

@ -197,7 +197,7 @@ std::string Duet::get_upload_url(const std::string &filename) const
{ {
return (boost::format("%1%rr_upload?name=0:/gcodes/%2%&%3%") return (boost::format("%1%rr_upload?name=0:/gcodes/%2%&%3%")
% get_base_url() % get_base_url()
% filename % Http::url_encode(filename)
% timestamp_str()).str(); % timestamp_str()).str();
} }
@ -248,9 +248,10 @@ wxString Duet::format_error(const std::string &body, const std::string &error, u
bool Duet::start_print(wxString &msg, const std::string &filename) const bool Duet::start_print(wxString &msg, const std::string &filename) const
{ {
bool res = false; bool res = false;
auto url = (boost::format("%1%rr_gcode?gcode=M32%%20\"%2%\"") auto url = (boost::format("%1%rr_gcode?gcode=M32%%20\"%2%\"")
% get_base_url() % get_base_url()
% filename).str(); % Http::url_encode(filename)).str();
auto http = Http::get(std::move(url)); auto http = Http::get(std::move(url));
http.on_error([&](std::string body, std::string error, unsigned status) { http.on_error([&](std::string body, std::string error, unsigned status) {
@ -275,5 +276,4 @@ int Duet::get_err_code_from_body(const std::string &body) const
return root.get<int>("err", 0); return root.get<int>("err", 0);
} }
} }

View File

@ -421,6 +421,21 @@ bool Http::ca_file_supported()
return res; return res;
} }
std::string Http::url_encode(const std::string &str)
{
::CURL *curl = ::curl_easy_init();
if (curl == nullptr) {
return str;
}
char *ce = ::curl_easy_escape(curl, str.c_str(), str.length());
std::string encoded = std::string(ce);
::curl_free(ce);
::curl_easy_cleanup(curl);
return encoded;
}
std::ostream& operator<<(std::ostream &os, const Http::Progress &progress) std::ostream& operator<<(std::ostream &os, const Http::Progress &progress)
{ {
os << "Http::Progress(" os << "Http::Progress("

View File

@ -98,6 +98,9 @@ public:
// Tells whether current backend supports seting up a CA file using ca_file() // Tells whether current backend supports seting up a CA file using ca_file()
static bool ca_file_supported(); static bool ca_file_supported();
// converts the given string to an url_encoded_string
static std::string url_encode(const std::string &str);
private: private:
Http(const std::string &url); Http(const std::string &url);