Skip to content

Commit e0a90ec

Browse files
committed
Support PATCH, PUT, HEAD, and DELETE methods.
1 parent dff11a4 commit e0a90ec

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

src/common/HTTPRequest.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ HTTPSClient::Reply HTTPRequest::request(const HTTPSClient::Request &req)
3535
// Build the request
3636
{
3737
std::stringstream request;
38-
request << (req.method == HTTPSClient::Request::GET ? "GET " : "POST ") << info.query << " HTTP/1.1\r\n";
38+
std::string method = req.method;
39+
bool hasData = req.postdata.length() > 0;
40+
41+
if (method.length() == 0)
42+
method = hasData ? "POST" : "GET";
43+
44+
request << method << " " << info.query << " HTTP/1.1\r\n";
3945

4046
for (auto &header : req.headers)
4147
request << header.first << ": " << header.second << "\r\n";
@@ -44,15 +50,15 @@ HTTPSClient::Reply HTTPRequest::request(const HTTPSClient::Request &req)
4450

4551
request << "Host: " << info.hostname << "\r\n";
4652

47-
if (req.method == HTTPSClient::Request::POST && req.headers.count("Content-Type") == 0)
53+
if (hasData && req.headers.count("Content-Type") == 0)
4854
request << "Content-Type: application/x-www-form-urlencoded\r\n";
4955

50-
if (req.method == HTTPSClient::Request::POST)
56+
if (hasData)
5157
request << "Content-Length: " << req.postdata.size() << "\r\n";
5258

5359
request << "\r\n";
5460

55-
if (req.method == HTTPSClient::Request::POST)
61+
if (hasData)
5662
request << req.postdata;
5763

5864
// Send it

src/common/HTTPSClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ bool HTTPSClient::ci_string_less::operator()(const std::string &lhs, const std::
3131

3232
HTTPSClient::Request::Request(const std::string &url)
3333
: url(url)
34-
, method(GET)
34+
, method("")
3535
{
3636
}
3737

src/common/HTTPSClient.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ class HTTPSClient
2020
header_map headers;
2121
std::string url;
2222
std::string postdata;
23-
24-
enum Method
25-
{
26-
GET,
27-
POST,
28-
} method;
23+
std::string method;
2924
};
3025

3126
struct Reply

src/lua/main.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include <algorithm>
2+
#include <set>
3+
14
extern "C"
25
{
36
#include <lua.h>
@@ -7,6 +10,8 @@ extern "C"
710
#include "../common/HTTPS.h"
811
#include "../common/config.h"
912

13+
static std::set<std::string> validMethod = {"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"};
14+
1015
static std::string w_checkstring(lua_State *L, int idx)
1116
{
1217
size_t len;
@@ -34,20 +39,18 @@ static void w_readheaders(lua_State *L, int idx, HTTPSClient::header_map &header
3439
lua_pop(L, 1);
3540
}
3641

37-
static HTTPSClient::Request::Method w_optmethod(lua_State *L, int idx, HTTPSClient::Request::Method defaultMethod)
42+
static std::string w_optmethod(lua_State *L, int idx, const std::string &defaultMethod)
3843
{
3944
if (lua_isnoneornil(L, idx))
4045
return defaultMethod;
4146

42-
auto str = w_checkstring(L, idx);
43-
if (str == "get")
44-
return HTTPSClient::Request::GET;
45-
else if (str == "post")
46-
return HTTPSClient::Request::POST;
47-
else
48-
luaL_argerror(L, idx, "expected one of \"get\" or \"set\"");
47+
std::string str = w_checkstring(L, idx);
48+
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) { return toupper(c); });
49+
50+
if (validMethod.find(str) == validMethod.end())
51+
luaL_argerror(L, idx, "expected one of \"get\", \"head\", \"post\", \"put\", \"delete\", or \"patch\"");
4952

50-
return defaultMethod;
53+
return str;
5154
}
5255

5356
static int w_request(lua_State *L)
@@ -61,13 +64,13 @@ static int w_request(lua_State *L)
6164
{
6265
advanced = true;
6366

64-
HTTPSClient::Request::Method defaultMethod = HTTPSClient::Request::GET;
67+
std::string defaultMethod = "GET";
6568

6669
lua_getfield(L, 2, "data");
6770
if (!lua_isnoneornil(L, -1))
6871
{
6972
req.postdata = w_checkstring(L, -1);
70-
defaultMethod = HTTPSClient::Request::POST;
73+
defaultMethod = "POST";
7174
}
7275
lua_pop(L, 1);
7376

0 commit comments

Comments
 (0)