浏览代码

Support PATCH, PUT, HEAD, and DELETE methods.

Miku AuahDark 3 年之前
父节点
当前提交
e0a90ecac0
共有 4 个文件被更改,包括 26 次插入22 次删除
  1. 10 4
      src/common/HTTPRequest.cpp
  2. 1 1
      src/common/HTTPSClient.cpp
  3. 1 6
      src/common/HTTPSClient.h
  4. 14 11
      src/lua/main.cpp

+ 10 - 4
src/common/HTTPRequest.cpp

@@ -35,7 +35,13 @@ HTTPSClient::Reply HTTPRequest::request(const HTTPSClient::Request &req)
 	// Build the request
 	{
 		std::stringstream request;
-		request << (req.method == HTTPSClient::Request::GET ? "GET " : "POST ") << info.query << " HTTP/1.1\r\n";
+		std::string method = req.method;
+		bool hasData = req.postdata.length() > 0;
+
+		if (method.length() == 0)
+			method = hasData ? "POST" : "GET";
+
+		request << method << " " << info.query << " HTTP/1.1\r\n";
 
 		for (auto &header : req.headers)
 			request << header.first << ": " << header.second << "\r\n";
@@ -44,15 +50,15 @@ HTTPSClient::Reply HTTPRequest::request(const HTTPSClient::Request &req)
 
 		request << "Host: " << info.hostname << "\r\n";
 
-		if (req.method == HTTPSClient::Request::POST && req.headers.count("Content-Type") == 0)
+		if (hasData && req.headers.count("Content-Type") == 0)
 			request << "Content-Type: application/x-www-form-urlencoded\r\n";
 
-		if (req.method == HTTPSClient::Request::POST)
+		if (hasData)
 			request << "Content-Length: " << req.postdata.size() << "\r\n";
 
 		request << "\r\n";
 
-		if (req.method == HTTPSClient::Request::POST)
+		if (hasData)
 			request << req.postdata;
 
 		// Send it

+ 1 - 1
src/common/HTTPSClient.cpp

@@ -31,7 +31,7 @@ bool HTTPSClient::ci_string_less::operator()(const std::string &lhs, const std::
 
 HTTPSClient::Request::Request(const std::string &url)
 	: url(url)
-	, method(GET)
+	, method("")
 {
 }
 

+ 1 - 6
src/common/HTTPSClient.h

@@ -20,12 +20,7 @@ public:
 		header_map headers;
 		std::string url;
 		std::string postdata;
-
-		enum Method
-		{
-			GET,
-			POST,
-		} method;
+		std::string method;
 	};
 
 	struct Reply

+ 14 - 11
src/lua/main.cpp

@@ -1,3 +1,6 @@
+#include <algorithm>
+#include <set>
+
 extern "C"
 {
 #include <lua.h>
@@ -7,6 +10,8 @@ extern "C"
 #include "../common/HTTPS.h"
 #include "../common/config.h"
 
+static std::set<std::string> validMethod = {"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"};
+
 static std::string w_checkstring(lua_State *L, int idx)
 {
 	size_t len;
@@ -34,20 +39,18 @@ static void w_readheaders(lua_State *L, int idx, HTTPSClient::header_map &header
 	lua_pop(L, 1);
 }
 
-static HTTPSClient::Request::Method w_optmethod(lua_State *L, int idx, HTTPSClient::Request::Method defaultMethod)
+static std::string w_optmethod(lua_State *L, int idx, const std::string &defaultMethod)
 {
 	if (lua_isnoneornil(L, idx))
 		return defaultMethod;
 
-	auto str = w_checkstring(L, idx);
-	if (str == "get")
-		return HTTPSClient::Request::GET;
-	else if (str == "post")
-		return HTTPSClient::Request::POST;
-	else
-		luaL_argerror(L, idx, "expected one of \"get\" or \"set\"");
+	std::string str = w_checkstring(L, idx);
+	std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) { return toupper(c); });
+
+	if (validMethod.find(str) == validMethod.end())
+		luaL_argerror(L, idx, "expected one of \"get\", \"head\", \"post\", \"put\", \"delete\", or \"patch\"");
 
-	return defaultMethod;
+	return str;
 }
 
 static int w_request(lua_State *L)
@@ -61,13 +64,13 @@ static int w_request(lua_State *L)
 	{
 		advanced = true;
 
-		HTTPSClient::Request::Method defaultMethod = HTTPSClient::Request::GET;
+		std::string defaultMethod = "GET";
 
 		lua_getfield(L, 2, "data");
 		if (!lua_isnoneornil(L, -1))
 		{
 			req.postdata = w_checkstring(L, -1);
-			defaultMethod = HTTPSClient::Request::POST;
+			defaultMethod = "POST";
 		}
 		lua_pop(L, 1);