Browse Source

Add application/x-www-form-urlencoded at higher-level.

Miku AuahDark 2 years ago
parent
commit
34035b6d51
3 changed files with 4 additions and 15 deletions
  1. 0 3
      src/common/HTTPRequest.cpp
  2. 3 12
      src/generic/CurlClient.cpp
  3. 1 0
      src/lua/main.cpp

+ 0 - 3
src/common/HTTPRequest.cpp

@@ -50,9 +50,6 @@ HTTPSClient::Reply HTTPRequest::request(const HTTPSClient::Request &req)
 
 		request << "Host: " << info.hostname << "\r\n";
 
-		if (hasData && req.headers.count("Content-Type") == 0)
-			request << "Content-Type: application/x-www-form-urlencoded\r\n";
-
 		if (hasData)
 			request << "Content-Length: " << req.postdata.size() << "\r\n";
 

+ 3 - 12
src/generic/CurlClient.cpp

@@ -155,17 +155,11 @@ HTTPSClient::Reply CurlClient::request(const HTTPSClient::Request &req)
 
 	curl.easy_setopt(handle, CURLOPT_URL, req.url.c_str());
 	curl.easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
-
-	std::string method = req.method;
-	if (method.empty())
-		method = req.postdata.size() > 0 ? "POST" : "GET";
-	else
-		std::transform(method.begin(), method.end(), method.begin(), toUppercase);
-	curl.easy_setopt(handle, CURLOPT_CUSTOMREQUEST, method.c_str());
+	curl.easy_setopt(handle, CURLOPT_CUSTOMREQUEST, req.method.c_str());
 
 	StringReader reader {};
 
-	if (req.postdata.size() > 0 && (method != "GET" && method != "HEAD"))
+	if (req.postdata.size() > 0 && (req.method != "GET" && req.method != "HEAD"))
 	{
 		reader.str = &req.postdata;
 		reader.pos = 0;
@@ -173,12 +167,9 @@ HTTPSClient::Reply CurlClient::request(const HTTPSClient::Request &req)
 		curl.easy_setopt(handle, CURLOPT_READFUNCTION, stringReader);
 		curl.easy_setopt(handle, CURLOPT_READDATA, &reader);
 		curl.easy_setopt(handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) req.postdata.length());
-
-		if (newHeaders.count("Content-Type") == 0)
-			newHeaders["Content-Type"] = "application/x-www-form-urlencoded";
 	}
 
-	if (method == "HEAD")
+	if (req.method == "HEAD")
 		curl.easy_setopt(handle, CURLOPT_NOBODY, 1L);
 
 	// Curl doesn't copy memory, keep the strings around

+ 1 - 0
src/lua/main.cpp

@@ -78,6 +78,7 @@ static int w_request(lua_State *L)
 		if (!lua_isnoneornil(L, -1))
 		{
 			req.postdata = w_checkstring(L, -1);
+			req.headers["Content-Type"] = "application/x-www-form-urlencoded";
 			defaultMethod = "POST";
 		}
 		lua_pop(L, 1);