From 3fd2efb77e850bc3870b87dd6e5b071cdd2d000e Mon Sep 17 00:00:00 2001 From: Firew0lf Date: Sat, 22 Aug 2015 18:44:56 +0200 Subject: [PATCH] Added the HTTPC services. Should work. --- source/ctr.c | 14 +++--- source/httpc.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 source/httpc.c diff --git a/source/ctr.c b/source/ctr.c index ac04365..b9afb38 100644 --- a/source/ctr.c +++ b/source/ctr.c @@ -11,6 +11,7 @@ int load_ptm_lib(lua_State *L); int load_hid_lib(lua_State *L); int load_ir_lib(lua_State *L); int load_fs_lib(lua_State *L); +int load_httpc_lib(lua_State *L); static int ctr_run(lua_State *L) { lua_pushboolean(L, aptMainLoop()); @@ -33,12 +34,13 @@ static const struct luaL_Reg ctr_lib[] = { // Subtables struct { char *name; int (*load)(lua_State *L); } ctr_libs[] = { - { "gfx", load_gfx_lib }, - { "news", load_news_lib }, - { "ptm", load_ptm_lib }, - { "hid", load_hid_lib }, - { "ir", load_ir_lib }, - { "fs", load_fs_lib }, + { "gfx", load_gfx_lib }, + { "news", load_news_lib }, + { "ptm", load_ptm_lib }, + { "hid", load_hid_lib }, + { "ir", load_ir_lib }, + { "fs", load_fs_lib }, + { "httpc", load_httpc_lib }, { NULL, NULL } }; diff --git a/source/httpc.c b/source/httpc.c new file mode 100644 index 0000000..ae2d6ce --- /dev/null +++ b/source/httpc.c @@ -0,0 +1,127 @@ +#include + +#include <3ds.h> +#include <3ds/types.h> +#include <3ds/services/httpc.h> + +#include +#include + +static int httpc_init(lua_State *L) { + httpcInit(); + return 0; +} + +static int httpc_shutdown(lua_State *L) { + httpcExit(); + return 0; +} + +static int httpc_context(lua_State *L) { + httpcContext *context; + context = (httpcContext*)lua_newuserdata(L, sizeof(*context)); + luaL_getmetatable(L, "LHTTPC"); + lua_setmetatable(L, -2); + + return 1; +} + +static int httpc_open(lua_State *L) { + httpcContext *context = lua_touserdata(L, 1); + char *url = (char*)luaL_checkstring(L, 2); + Result ret = 0; + + ret = httpcOpenContext(context, url, 0); + + lua_pushinteger(L, ret); + return 1; +} + +static int httpc_beginRequest(lua_State *L) { + httpcContext *context = lua_touserdata(L, 1); + Result ret = 0; + + ret = httpcBeginRequest(context); + + lua_pushinteger(L, ret); + return 1; +} + +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; +} + +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; +} + +static int httpc_downloadData(lua_State *L) { + httpcContext *context = lua_touserdata(L, 1); + u32 size = 0; + httpcGetDownloadSizeState(context, NULL, &size); + u8 *buff = (u8*)malloc(size); + + httpcDownloadData(context, buff, size, NULL); + + lua_pushstring(L, (char*)buff); + return 1; +} + +static int httpc_close(lua_State *L) { + httpcContext *context = lua_touserdata(L, 1); + + httpcCloseContext(context); + + return 0; +} + +// object +static const struct luaL_Reg httpc_methods[] = { + {"open", httpc_open}, + {"beginRequest", httpc_beginRequest}, + {"getStatusCode", httpc_getStatusCode}, + {"getDownloadSize", httpc_getDownloadSize}, + {"downloadData", httpc_downloadData}, + {"close", httpc_close}, + {NULL, NULL} +}; + +// module +static const struct luaL_Reg httpc_functions[] = { + {"init", httpc_init }, + {"shutdown", httpc_shutdown}, + {"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) { + luaL_requiref(L, "ctr.httpc", luaopen_httpc_lib, false); +} +