2015-12-29 12:25:37 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* HTTP module for NodeMCU
|
|
|
|
* vowstar@gmail.com
|
|
|
|
* 2015-12-29
|
|
|
|
*******************************************************************************/
|
2019-07-23 22:22:59 +02:00
|
|
|
#include <string.h>
|
2019-07-21 23:58:21 +02:00
|
|
|
#include <stdlib.h>
|
2015-12-29 12:25:37 +01:00
|
|
|
#include "module.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "cpu_esp8266.h"
|
2019-07-23 17:47:18 +02:00
|
|
|
#include "http/httpclient.h"
|
2020-04-27 02:13:38 +02:00
|
|
|
#include <ctype.h>
|
2015-12-29 12:25:37 +01:00
|
|
|
static int http_callback_registry = LUA_NOREF;
|
|
|
|
|
2016-12-01 22:13:33 +01:00
|
|
|
static void http_callback( char * response, int http_status, char ** full_response_p )
|
2015-12-29 12:25:37 +01:00
|
|
|
{
|
2016-12-01 22:13:33 +01:00
|
|
|
const char *full_response = full_response_p ? *full_response_p : NULL;
|
|
|
|
|
2015-12-29 12:25:37 +01:00
|
|
|
#if defined(HTTPCLIENT_DEBUG_ON)
|
2016-10-27 08:38:47 +02:00
|
|
|
dbg_printf( "http_status=%d\n", http_status );
|
2015-12-29 12:25:37 +01:00
|
|
|
if ( http_status != HTTP_STATUS_GENERIC_ERROR )
|
|
|
|
{
|
2016-04-09 13:54:10 +02:00
|
|
|
if (full_response != NULL) {
|
2016-10-27 08:38:47 +02:00
|
|
|
dbg_printf( "strlen(full_response)=%d\n", strlen( full_response ) );
|
2016-04-09 13:54:10 +02:00
|
|
|
}
|
2016-10-27 08:38:47 +02:00
|
|
|
dbg_printf( "response=%s<EOF>\n", response );
|
2015-12-29 12:25:37 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (http_callback_registry != LUA_NOREF)
|
|
|
|
{
|
2016-04-09 13:54:10 +02:00
|
|
|
lua_State *L = lua_getstate();
|
|
|
|
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, http_callback_registry);
|
2015-12-29 12:25:37 +01:00
|
|
|
|
2016-04-09 13:54:10 +02:00
|
|
|
lua_pushnumber(L, http_status);
|
|
|
|
if ( http_status != HTTP_STATUS_GENERIC_ERROR && response)
|
2015-12-29 12:25:37 +01:00
|
|
|
{
|
2016-04-09 13:54:10 +02:00
|
|
|
lua_pushstring(L, response);
|
2016-12-01 22:13:33 +01:00
|
|
|
lua_newtable(L);
|
|
|
|
|
|
|
|
const char *p = full_response;
|
|
|
|
|
|
|
|
// Need to skip the HTTP/1.1 header line
|
|
|
|
while (*p && *p != '\n') {
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (*p == '\n') {
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*p && *p != '\r' && *p != '\n') {
|
|
|
|
const char *eol = p;
|
|
|
|
while (*eol && *eol != '\r') {
|
|
|
|
eol++;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *colon = p;
|
|
|
|
while (*colon != ':' && colon < eol) {
|
|
|
|
colon++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*colon != ':') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *value = colon + 1;
|
|
|
|
while (*value == ' ') {
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
|
|
|
|
luaL_Buffer b;
|
|
|
|
luaL_buffinit(L, &b);
|
|
|
|
while (p < colon) {
|
|
|
|
luaL_addchar(&b, tolower((unsigned char) *p));
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
luaL_pushresult(&b);
|
|
|
|
|
|
|
|
lua_pushlstring(L, value, eol - value);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
|
|
|
|
p = eol + 1;
|
|
|
|
if (*p == '\n') {
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
2015-12-29 12:25:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-09 13:54:10 +02:00
|
|
|
lua_pushnil(L);
|
2016-12-01 22:13:33 +01:00
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (full_response_p && *full_response_p) {
|
2019-07-21 23:58:21 +02:00
|
|
|
free(*full_response_p);
|
2016-12-01 22:13:33 +01:00
|
|
|
*full_response_p = NULL;
|
2015-12-29 12:25:37 +01:00
|
|
|
}
|
|
|
|
|
2016-04-09 13:54:10 +02:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
|
2015-12-29 12:25:37 +01:00
|
|
|
http_callback_registry = LUA_NOREF;
|
2016-12-01 22:13:33 +01:00
|
|
|
|
|
|
|
lua_call(L, 3, 0); // With 3 arguments and 0 result
|
2015-12-29 12:25:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: http.request( url, method, header, body, function(status, reponse) end )
|
|
|
|
static int http_lapi_request( lua_State *L )
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
const char * url = luaL_checklstring(L, 1, &length);
|
|
|
|
const char * method = luaL_checklstring(L, 2, &length);
|
|
|
|
const char * headers = NULL;
|
|
|
|
const char * body = NULL;
|
|
|
|
|
|
|
|
// Check parameter
|
|
|
|
if ((url == NULL) || (method == NULL))
|
|
|
|
{
|
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lua_isstring(L, 3))
|
|
|
|
{
|
|
|
|
headers = luaL_checklstring(L, 3, &length);
|
|
|
|
}
|
|
|
|
if (lua_isstring(L, 4))
|
|
|
|
{
|
|
|
|
body = luaL_checklstring(L, 4, &length);
|
|
|
|
}
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
if (lua_isfunction(L, 5)) {
|
2016-04-09 13:54:10 +02:00
|
|
|
lua_pushvalue(L, 5); // copy argument (func) to the top of stack
|
2016-12-01 22:13:33 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
|
2015-12-29 12:25:37 +01:00
|
|
|
http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
|
|
|
|
2016-08-14 00:48:13 +02:00
|
|
|
http_request(url, method, headers, body, http_callback, 0);
|
2015-12-29 12:25:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: http.post( url, header, body, function(status, reponse) end )
|
|
|
|
static int http_lapi_post( lua_State *L )
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
const char * url = luaL_checklstring(L, 1, &length);
|
|
|
|
const char * headers = NULL;
|
|
|
|
const char * body = NULL;
|
|
|
|
|
|
|
|
// Check parameter
|
|
|
|
if ((url == NULL))
|
|
|
|
{
|
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lua_isstring(L, 2))
|
|
|
|
{
|
|
|
|
headers = luaL_checklstring(L, 2, &length);
|
|
|
|
}
|
|
|
|
if (lua_isstring(L, 3))
|
|
|
|
{
|
|
|
|
body = luaL_checklstring(L, 3, &length);
|
|
|
|
}
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
if (lua_isfunction(L, 4)) {
|
2016-04-09 13:54:10 +02:00
|
|
|
lua_pushvalue(L, 4); // copy argument (func) to the top of stack
|
2015-12-29 12:25:37 +01:00
|
|
|
if (http_callback_registry != LUA_NOREF)
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
|
|
|
|
http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
|
|
|
|
|
|
|
http_post(url, headers, body, http_callback);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: http.put( url, header, body, function(status, reponse) end )
|
|
|
|
static int http_lapi_put( lua_State *L )
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
const char * url = luaL_checklstring(L, 1, &length);
|
|
|
|
const char * headers = NULL;
|
|
|
|
const char * body = NULL;
|
|
|
|
|
|
|
|
// Check parameter
|
|
|
|
if ((url == NULL))
|
|
|
|
{
|
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lua_isstring(L, 2))
|
|
|
|
{
|
|
|
|
headers = luaL_checklstring(L, 2, &length);
|
|
|
|
}
|
|
|
|
if (lua_isstring(L, 3))
|
|
|
|
{
|
|
|
|
body = luaL_checklstring(L, 3, &length);
|
|
|
|
}
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
if (lua_isfunction(L, 4)) {
|
2016-04-09 13:54:10 +02:00
|
|
|
lua_pushvalue(L, 4); // copy argument (func) to the top of stack
|
2015-12-29 12:25:37 +01:00
|
|
|
if (http_callback_registry != LUA_NOREF)
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
|
|
|
|
http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
|
|
|
|
|
|
|
http_put(url, headers, body, http_callback);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: http.delete( url, header, body, function(status, reponse) end )
|
|
|
|
static int http_lapi_delete( lua_State *L )
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
const char * url = luaL_checklstring(L, 1, &length);
|
|
|
|
const char * headers = NULL;
|
|
|
|
const char * body = NULL;
|
|
|
|
|
|
|
|
// Check parameter
|
|
|
|
if ((url == NULL))
|
|
|
|
{
|
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lua_isstring(L, 2))
|
|
|
|
{
|
|
|
|
headers = luaL_checklstring(L, 2, &length);
|
|
|
|
}
|
|
|
|
if (lua_isstring(L, 3))
|
|
|
|
{
|
|
|
|
body = luaL_checklstring(L, 3, &length);
|
|
|
|
}
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
if (lua_isfunction(L, 4)) {
|
2016-04-09 13:54:10 +02:00
|
|
|
lua_pushvalue(L, 4); // copy argument (func) to the top of stack
|
2015-12-29 12:25:37 +01:00
|
|
|
if (http_callback_registry != LUA_NOREF)
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
|
|
|
|
http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
|
|
|
|
|
|
|
http_delete(url, headers, body, http_callback);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: http.get( url, header, function(status, reponse) end )
|
|
|
|
static int http_lapi_get( lua_State *L )
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
const char * url = luaL_checklstring(L, 1, &length);
|
|
|
|
const char * headers = NULL;
|
|
|
|
|
|
|
|
// Check parameter
|
|
|
|
if ((url == NULL))
|
|
|
|
{
|
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lua_isstring(L, 2))
|
|
|
|
{
|
|
|
|
headers = luaL_checklstring(L, 2, &length);
|
|
|
|
}
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
if (lua_isfunction(L, 3)) {
|
2016-04-09 13:54:10 +02:00
|
|
|
lua_pushvalue(L, 3); // copy argument (func) to the top of stack
|
2015-12-29 12:25:37 +01:00
|
|
|
if (http_callback_registry != LUA_NOREF)
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
|
|
|
|
http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
|
|
|
|
|
|
|
http_get(url, headers, http_callback);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Module function map
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_BEGIN(http, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( request, http_lapi_request )
|
|
|
|
LROT_FUNCENTRY( post, http_lapi_post )
|
|
|
|
LROT_FUNCENTRY( put, http_lapi_put )
|
|
|
|
LROT_FUNCENTRY( delete, http_lapi_delete )
|
|
|
|
LROT_FUNCENTRY( get, http_lapi_get )
|
2015-12-29 12:25:37 +01:00
|
|
|
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_NUMENTRY( OK, 0 )
|
|
|
|
LROT_NUMENTRY( ERROR, HTTP_STATUS_GENERIC_ERROR )
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(http, NULL, 0)
|
2015-12-29 12:25:37 +01:00
|
|
|
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
NODEMCU_MODULE(HTTP, "http", http, NULL);
|