Pārlūkot izejas kodu

Fix Attachment POST bug

JDierkse 5 gadi atpakaļ
vecāks
revīzija
ce3b357c4b
1 mainītis faili ar 53 papildinājumiem un 25 dzēšanām
  1. 53 25
      Http/HttpClientImpl.cpp

+ 53 - 25
Http/HttpClientImpl.cpp

@@ -95,7 +95,7 @@ std::string HttpClientImpl::PerformOperation(const HttpRequest& request) const
 		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);
 		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
-		debug << "ReturnType::Content" << std::endl;
+		debug << "ReturnType::Redirect" << std::endl;
 	}
 	if (m_debugLogging)
 		Logging::Log(Logging::Severity::Debug, debug.str());
@@ -144,7 +144,6 @@ std::string HttpClientImpl::PerformOperation(const HttpRequest& request) const
 	    method != HttpRequest::Method::HEAD &&
 	    !data.empty())
 	{
-
 		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
 		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::stringstream debug;
+
 	auto method = request.Method();
 	auto returnType = request.ReturnType();
 	auto url = request.URL();
@@ -289,6 +290,16 @@ std::string HttpClientImpl::ProcessFileUpload(const HttpRequest& request) const
 	std::string contents;
 	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)
 	{
 		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_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, '&');
-
-	for (std::vector<std::string>::iterator it = postTokens.begin(); it != postTokens.end(); ++it)
+	for (const auto& postToken : postTokens)
 	{
-		std::vector<std::string> tokens = StringAlgorithm::split(*it, '=');
+		std::vector<std::string> postItem = StringAlgorithm::split(postToken, '=');
 
 		curl_formadd(&formpost, &lastptr,
-			CURLFORM_COPYNAME, tokens[0].c_str(),
-			CURLFORM_COPYCONTENTS, tokens[1].c_str(),
+			CURLFORM_COPYNAME, postItem[0].c_str(),
+			CURLFORM_COPYCONTENTS, postItem[1].c_str(),
 			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,
 		CURLFORM_COPYNAME, fileFieldname.c_str(),
@@ -332,14 +349,10 @@ std::string HttpClientImpl::ProcessFileUpload(const HttpRequest& request) const
 		CURLFORM_BUFFERLENGTH, contents.size(),
 		CURLFORM_END);
 
-	if (!headers.empty())
-	{
-		struct curl_slist *list = nullptr;
-		for (const auto& header : headers)
-			list = curl_slist_append(list, header.c_str());
-
-		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
-	}
+	debug << "Data: " << data << std::endl;
+	if (m_debugLogging)
+		Logging::Log(Logging::Severity::Debug, debug.str());
+	debug.str("");
 
 	curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
 	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_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 ||
 	    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_WRITEFUNCTION, &WriteCallback);
 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
+		debug << "ReturnType::None || ReturnType::Code" << std::endl;
 	}
 	else if (returnType == HttpRequest::ReturnType::Content)
 	{
 		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
 		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
+		debug << "ReturnType::Content" << std::endl;
 	}
 	else if (returnType == HttpRequest::ReturnType::Redirect)
 	{
 		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);
 		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
 		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;
 	curl_easy_perform(curl);
@@ -452,6 +475,11 @@ std::string HttpClientImpl::ProcessFileUpload(const HttpRequest& request) const
 		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_formfree(formpost);