/
httpc.c
280 lines (238 loc) · 6.3 KB
/
httpc.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/***
The `httpc` module.
@module ctr.httpc
@usage local httpc = require("ctr.httpc")
*/
#include <stdlib.h>
#include <string.h>
#include <3ds.h>
#include <3ds/types.h>
#include <3ds/services/httpc.h>
#include <lapi.h>
#include <lauxlib.h>
bool isHttpcInitialized = false;
/***
Create a HTTP Context.
@function context
@treturn context a new http context
*/
static int httpc_context(lua_State *L) {
httpcContext context;
lua_newuserdata(L, sizeof(&context));
luaL_getmetatable(L, "LHTTPC");
lua_setmetatable(L, -2);
return 1;
}
/***
context object
@section Methods
*/
/***
Open an url in the context.
@function :open
@tparam string url the url to open
@tparam[opt="GET"] string method method to use; can be `"GET"`, `"POST"`, `"HEAD"`, `"PUT"` or `"DELETE"`
*/
static int httpc_open(lua_State *L) {
httpcContext *context = lua_touserdata(L, 1);
char *url = (char*)luaL_checkstring(L, 2);
char *smethod = (char*)luaL_optstring(L, 3, "GET");
HTTPC_RequestMethod method = HTTPC_METHOD_GET; // default to GET
if (strcmp(smethod, "POST")) {
method = HTTPC_METHOD_POST;
} else if (strcmp(smethod, "HEAD")) {
method = HTTPC_METHOD_HEAD;
} else if (strcmp(smethod, "PUT")) {
method = HTTPC_METHOD_PUT;
} else if (strcmp(smethod, "DELETE")) {
method = HTTPC_METHOD_DELETE;
}
Result ret = 0;
ret = httpcOpenContext(context, method, url, 0);
if (ret != 0) {
lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
lua_pushboolean(L, true);
return 1;
}
/***
Add a field in the request header.
@function :addRequestHeaderField
@tparam string name Name of the field
@tparam string value Value of the field
*/
static int httpc_addRequestHeaderField(lua_State *L) {
httpcContext *context = lua_touserdata(L, 1);
char *name = (char*)luaL_checkstring(L, 2);
char *value = (char*)luaL_checkstring(L, 3);
Result ret = httpcAddRequestHeaderField(context, name ,value);
if (ret != 0) {
lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
lua_pushboolean(L, true);
return 1;
}
/***
Begin a request to get the content at the URL.
@function :beginRequest
*/
static int httpc_beginRequest(lua_State *L) {
httpcContext *context = lua_touserdata(L, 1);
Result ret = 0;
ret = httpcBeginRequest(context);
if (ret != 0) {
lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
lua_pushboolean(L, true);
return 1;
}
/***
Return the status code returned by the request.
@function :getStatusCode
@treturn number the status code
*/
static int httpc_getStatusCode(lua_State *L) {
httpcContext *context = lua_touserdata(L, 1);
u32 statusCode = 0;
Result ret = httpcGetResponseStatusCode(context, &statusCode, 0);
if (ret != 0) {
lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
lua_pushinteger(L, statusCode);
return 1;
}
/***
Return the amount of data to download.
@function :getDownloadSize
@treturn number size in (bytes)
*/
static int httpc_getDownloadSize(lua_State *L) {
httpcContext *context = lua_touserdata(L, 1);
u32 contentSize = 0;
httpcGetDownloadSizeState(context, NULL, &contentSize);
lua_pushinteger(L, contentSize);
return 1;
}
/***
Download and return the data of the context.
@function :downloadData
@treturn string data
*/
static int httpc_downloadData(lua_State *L) {
httpcContext *context = lua_touserdata(L, 1);
u32 status = 0;
Result ret = httpcGetResponseStatusCode(context, &status, 0);
if (ret != 0) {
lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
u32 size = 0;
httpcGetDownloadSizeState(context, NULL, &size);
u8 *buff = (u8*)malloc(size);
ret = httpcDownloadData(context, buff, size, NULL);
if (ret != 0) {
lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
lua_pushstring(L, (char*)buff);
//free(buff);
lua_pushinteger(L, size); // only for test purposes.
return 2;
}
/***
Close the context.
@function :close
*/
static int httpc_close(lua_State *L) {
httpcContext *context = lua_touserdata(L, 1);
httpcCloseContext(context);
return 0;
}
/***
Add a POST form field to a HTTP context.
@function :addPostData
@tparam string name name of the field
@tparam string value value of the field
*/
static int httpc_addPostData(lua_State *L) {
httpcContext *context = lua_touserdata(L, 1);
char *name = (char*)luaL_checkstring(L, 2);
char *value = (char*)luaL_checkstring(L, 3);
httpcAddPostDataAscii(context, name, value);
return 0;
}
/***
Get a header field from a response.
@function :getResponseHeader
@tparam string name name of the header field to get
@tparam[opt=2048] number maximum size of the value to get
@treturn string field value
*/
static int httpc_getResponseHeader(lua_State *L) {
httpcContext *context = lua_touserdata(L, 1);
char *name = (char*)luaL_checkstring(L, 2);
u32 maxSize = luaL_checkinteger(L, 3);
char* value = 0;
httpcGetResponseHeader(context, name, value, maxSize);
lua_pushstring(L, value);
return 1;
}
/***
Add a trusted RootCA cert to a context.
@function :addTrustedRootCA
@tparam string DER certificate
*/
static int httpc_addTrustedRootCA(lua_State *L) {
httpcContext *context = lua_touserdata(L, 1);
u32 certsize;
u8* cert = (u8*)luaL_checklstring(L, 2, (size_t*)&certsize);
httpcAddTrustedRootCA(context, cert, certsize);
return 0;
}
// object
static const struct luaL_Reg httpc_methods[] = {
{"open", httpc_open },
{"addRequestHeaderField", httpc_addRequestHeaderField},
{"beginRequest", httpc_beginRequest },
{"getStatusCode", httpc_getStatusCode },
{"getDownloadSize", httpc_getDownloadSize },
{"downloadData", httpc_downloadData },
{"close", httpc_close },
{"addPostData", httpc_addPostData },
{"getResponseHeader", httpc_getResponseHeader },
{"addTrustedRootCA", httpc_addTrustedRootCA },
{NULL, NULL}
};
// module
static const struct luaL_Reg httpc_functions[] = {
{"context", httpc_context },
{NULL, NULL}
};
int luaopen_httpc_lib(lua_State *L) {
luaL_newmetatable(L, "LHTTPC");
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_setfuncs(L, httpc_methods, 0);
luaL_newlib(L, httpc_functions);
return 1;
}
void load_httpc_lib(lua_State *L) {
if (!isHttpcInitialized) {
httpcInit(0x1000);
isHttpcInitialized = true;
}
luaL_requiref(L, "ctr.httpc", luaopen_httpc_lib, false);
}
void unload_httpc_lib(lua_State *L) {
httpcExit();
}