Fix Attachment POST bug

This commit is contained in:
2020-06-14 21:59:24 +02:00
parent b22c2b2f4d
commit ce3b357c4b
+53 -25
View File
@@ -95,7 +95,7 @@ std::string HttpClientImpl::PerformOperation(const HttpRequest& request) const
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr); curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
debug << "ReturnType::Content" << std::endl; debug << "ReturnType::Redirect" << std::endl;
} }
if (m_debugLogging) if (m_debugLogging)
Logging::Log(Logging::Severity::Debug, debug.str()); Logging::Log(Logging::Severity::Debug, debug.str());
@@ -144,7 +144,6 @@ std::string HttpClientImpl::PerformOperation(const HttpRequest& request) const
method != HttpRequest::Method::HEAD && method != HttpRequest::Method::HEAD &&
!data.empty()) !data.empty())
{ {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
debug << "Data: " << data << std::endl; debug << "Data: " << data << std::endl;
} }
@@ -273,6 +272,8 @@ std::string HttpClientImpl::PerformOperation(const HttpRequest& request) const
std::string HttpClientImpl::ProcessFileUpload(const HttpRequest& request) const std::string HttpClientImpl::ProcessFileUpload(const HttpRequest& request) const
{ {
std::stringstream debug;
auto method = request.Method(); auto method = request.Method();
auto returnType = request.ReturnType(); auto returnType = request.ReturnType();
auto url = request.URL(); auto url = request.URL();
@@ -289,6 +290,16 @@ std::string HttpClientImpl::ProcessFileUpload(const HttpRequest& request) const
std::string contents; std::string contents;
std::ifstream fileStream(filename, std::ios::in | std::ios::binary); std::ifstream fileStream(filename, std::ios::in | std::ios::binary);
debug << "Filename: " << filename << std::endl;
if (m_debugLogging)
Logging::Log(Logging::Severity::Debug, debug.str());
debug.str("");
debug << "FileFieldname: " << fileFieldname << std::endl;
if (m_debugLogging)
Logging::Log(Logging::Severity::Debug, debug.str());
debug.str("");
if (fileStream) if (fileStream)
{ {
fileStream.seekg(0, std::ios::end); fileStream.seekg(0, std::ios::end);
@@ -303,27 +314,33 @@ std::string HttpClientImpl::ProcessFileUpload(const HttpRequest& request) const
curl_global_init(CURL_GLOBAL_ALL); curl_global_init(CURL_GLOBAL_ALL);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "cache-control:",
CURLFORM_COPYCONTENTS, "no-cache",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "content-type:",
CURLFORM_COPYCONTENTS, "multipart/form-data",
CURLFORM_END);
std::vector<std::string> postTokens = StringAlgorithm::split(data, '&'); std::vector<std::string> postTokens = StringAlgorithm::split(data, '&');
for (const auto& postToken : postTokens)
for (std::vector<std::string>::iterator it = postTokens.begin(); it != postTokens.end(); ++it)
{ {
std::vector<std::string> tokens = StringAlgorithm::split(*it, '='); std::vector<std::string> postItem = StringAlgorithm::split(postToken, '=');
curl_formadd(&formpost, &lastptr, curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, tokens[0].c_str(), CURLFORM_COPYNAME, postItem[0].c_str(),
CURLFORM_COPYCONTENTS, tokens[1].c_str(), CURLFORM_COPYCONTENTS, postItem[1].c_str(),
CURLFORM_END); CURLFORM_END);
debug << "PostToken: " << postItem[0] << " = " << postItem[1] << std::endl;
} }
if (m_debugLogging)
Logging::Log(Logging::Severity::Debug, debug.str());
debug.str("");
curl_easy_setopt(curl, CURLOPT_POST, 1);
if (method != HttpRequest::Method::GET &&
method != HttpRequest::Method::HEAD &&
!data.empty())
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
debug << "Data: " << data << std::endl;
}
if (m_debugLogging)
Logging::Log(Logging::Severity::Debug, debug.str());
debug.str("");
curl_formadd(&formpost, &lastptr, curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, fileFieldname.c_str(), CURLFORM_COPYNAME, fileFieldname.c_str(),
@@ -332,14 +349,10 @@ std::string HttpClientImpl::ProcessFileUpload(const HttpRequest& request) const
CURLFORM_BUFFERLENGTH, contents.size(), CURLFORM_BUFFERLENGTH, contents.size(),
CURLFORM_END); CURLFORM_END);
if (!headers.empty()) debug << "Data: " << data << std::endl;
{ if (m_debugLogging)
struct curl_slist *list = nullptr; Logging::Log(Logging::Severity::Debug, debug.str());
for (const auto& header : headers) debug.str("");
list = curl_slist_append(list, header.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_timeout); curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_timeout);
@@ -349,7 +362,11 @@ std::string HttpClientImpl::ProcessFileUpload(const HttpRequest& request) const
{ {
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str()); curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str()); curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
debug << "CookieFile: " << cookieFile << std::endl;
} }
if (m_debugLogging)
Logging::Log(Logging::Severity::Debug, debug.str());
debug.str("");
if (returnType == HttpRequest::ReturnType::None || if (returnType == HttpRequest::ReturnType::None ||
returnType == HttpRequest::ReturnType::Code) returnType == HttpRequest::ReturnType::Code)
@@ -357,19 +374,25 @@ std::string HttpClientImpl::ProcessFileUpload(const HttpRequest& request) const
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr); curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
debug << "ReturnType::None || ReturnType::Code" << std::endl;
} }
else if (returnType == HttpRequest::ReturnType::Content) else if (returnType == HttpRequest::ReturnType::Content)
{ {
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
debug << "ReturnType::Content" << std::endl;
} }
else if (returnType == HttpRequest::ReturnType::Redirect) else if (returnType == HttpRequest::ReturnType::Redirect)
{ {
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr); curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
debug << "ReturnType::Redirect" << std::endl;
} }
if (m_debugLogging)
Logging::Log(Logging::Severity::Debug, debug.str());
debug.str("");
long code = 0; long code = 0;
curl_easy_perform(curl); curl_easy_perform(curl);
@@ -452,6 +475,11 @@ std::string HttpClientImpl::ProcessFileUpload(const HttpRequest& request) const
buffer = std::to_string(code); buffer = std::to_string(code);
} }
debug << "Code: " << code << std::endl;
if (m_debugLogging)
Logging::Log(Logging::Severity::Debug, debug.str());
debug.str("");
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
curl_formfree(formpost); curl_formfree(formpost);