forked from love2d/lua-https
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
135 lines (107 loc) · 2.7 KB
/
main.cpp
File metadata and controls
135 lines (107 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <algorithm>
#include <set>
extern "C"
{
#include <lua.h>
#include <lauxlib.h>
}
#include "../common/HTTPS.h"
#include "../common/config.h"
static std::string validMethod[] = {"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"};
static int str_toupper(char c)
{
unsigned char uc = (unsigned char) c;
return toupper(uc);
}
static std::string w_checkstring(lua_State *L, int idx)
{
size_t len;
const char *str = luaL_checklstring(L, idx, &len);
return std::string(str, len);
}
static void w_pushstring(lua_State *L, const std::string &str)
{
lua_pushlstring(L, str.data(), str.size());
}
static void w_readheaders(lua_State *L, int idx, HTTPSClient::header_map &headers)
{
if (idx < 0)
idx += lua_gettop(L) + 1;
lua_pushnil(L);
while (lua_next(L, idx))
{
auto header = w_checkstring(L, -2);
headers[header] = w_checkstring(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 1);
}
static std::string w_optmethod(lua_State *L, int idx, const std::string &defaultMethod)
{
std::string *const validMethodEnd = validMethod + sizeof(validMethod) / sizeof(std::string);
if (lua_isnoneornil(L, idx))
return defaultMethod;
std::string str = w_checkstring(L, idx);
std::transform(str.begin(), str.end(), str.begin(), str_toupper);
if (std::find(validMethod, validMethodEnd, str) == validMethodEnd)
luaL_argerror(L, idx, "expected one of \"get\", \"head\", \"post\", \"put\", \"delete\", or \"patch\"");
return str;
}
static int w_request(lua_State *L)
{
auto url = w_checkstring(L, 1);
HTTPSClient::Request req(url);
bool advanced = false;
if (lua_istable(L, 2))
{
advanced = true;
std::string defaultMethod = "GET";
lua_getfield(L, 2, "data");
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);
lua_getfield(L, 2, "method");
req.method = w_optmethod(L, -1, defaultMethod);
lua_pop(L, 1);
lua_getfield(L, 2, "headers");
if (!lua_isnoneornil(L, -1))
w_readheaders(L, -1, req.headers);
lua_pop(L, 1);
}
HTTPSClient::Reply reply;
try
{
reply = request(req);
}
catch (const std::exception& e)
{
std::string errorMessage = e.what();
lua_pushnil(L);
lua_pushstring(L, errorMessage.c_str());
return 2;
}
lua_pushinteger(L, reply.responseCode);
w_pushstring(L, reply.body);
if (advanced)
{
lua_newtable(L);
for (const auto &header : reply.headers)
{
w_pushstring(L, header.first);
w_pushstring(L, header.second);
lua_settable(L, -3);
}
}
return advanced ? 3 : 2;
}
extern "C" int HTTPS_DLLEXPORT luaopen_https(lua_State *L)
{
lua_newtable(L);
lua_pushcfunction(L, w_request);
lua_setfield(L, -2, "request");
return 1;
}