static int lua_download(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 2) return luaL_error(L, "wrong number of arguments");
#endif
const char* url = luaL_checkstring(L,1);
const char* file = luaL_checkstring(L,2);
httpcContext context;
Result ret = httpcOpenContext(&context, (char*)url , 0);
#ifndef SKIP_ERROR_HANDLING
if(ret==0){
#endif
httpcBeginRequest(&context);
HTTPC_RequestStatus loading;
httpcGetRequestState(&context, &loading);
while (loading == HTTPC_STATUS_REQUEST_IN_PROGRESS){
httpcGetRequestState(&context, &loading);
}
u32 statuscode=0;
u32 contentsize=0;
httpcGetResponseStatusCode(&context, &statuscode, 0);
#ifndef SKIP_ERROR_HANDLING
if (statuscode != 200) luaL_error(L, "download request error");
#endif
httpcGetDownloadSizeState(&context, NULL, &contentsize);
u8* buf = (u8*)malloc(contentsize);
memset(buf, 0, contentsize);
httpcDownloadData(&context, buf, contentsize, NULL);
Handle fileHandle;
u32 bytesWritten;
FS_Archive sdmcArchive=(FS_Archive){ARCHIVE_SDMC, (FS_Path){PATH_EMPTY, 1, (u8*)""}};
FS_Path filePath=fsMakePath(PATH_ASCII, file);
FSUSER_OpenFileDirectly( &fileHandle, sdmcArchive, filePath, FS_OPEN_CREATE|FS_OPEN_WRITE, 0x00000000);
FSFILE_Write(fileHandle, &bytesWritten, 0, buf, contentsize,0x10001);
FSFILE_Close(fileHandle);
svcCloseHandle(fileHandle);
free(buf);
#ifndef SKIP_ERROR_HANDLING
}else luaL_error(L, "error opening url");
#endif
httpcCloseContext(&context);
return 0;
}