http.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /******************************************************************************
  2. * HTTP module for NodeMCU
  3. * vowstar@gmail.com
  4. * 2015-12-29
  5. *******************************************************************************/
  6. #include "module.h"
  7. #include "lauxlib.h"
  8. #include "platform.h"
  9. #include "cpu_esp8266.h"
  10. #include "httpclient.h"
  11. static int http_callback_registry = LUA_NOREF;
  12. static void http_callback( char * response, int http_status, char * full_response )
  13. {
  14. #if defined(HTTPCLIENT_DEBUG_ON)
  15. dbg_printf( "http_status=%d\n", http_status );
  16. if ( http_status != HTTP_STATUS_GENERIC_ERROR )
  17. {
  18. if (full_response != NULL) {
  19. dbg_printf( "strlen(full_response)=%d\n", strlen( full_response ) );
  20. }
  21. dbg_printf( "response=%s<EOF>\n", response );
  22. }
  23. #endif
  24. if (http_callback_registry != LUA_NOREF)
  25. {
  26. lua_State *L = lua_getstate();
  27. lua_rawgeti(L, LUA_REGISTRYINDEX, http_callback_registry);
  28. lua_pushnumber(L, http_status);
  29. if ( http_status != HTTP_STATUS_GENERIC_ERROR && response)
  30. {
  31. lua_pushstring(L, response);
  32. }
  33. else
  34. {
  35. lua_pushnil(L);
  36. }
  37. lua_call(L, 2, 0); // With 2 arguments and 0 result
  38. luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
  39. http_callback_registry = LUA_NOREF;
  40. }
  41. }
  42. // Lua: http.request( url, method, header, body, function(status, reponse) end )
  43. static int http_lapi_request( lua_State *L )
  44. {
  45. int length;
  46. const char * url = luaL_checklstring(L, 1, &length);
  47. const char * method = luaL_checklstring(L, 2, &length);
  48. const char * headers = NULL;
  49. const char * body = NULL;
  50. // Check parameter
  51. if ((url == NULL) || (method == NULL))
  52. {
  53. return luaL_error( L, "wrong arg type" );
  54. }
  55. if (lua_isstring(L, 3))
  56. {
  57. headers = luaL_checklstring(L, 3, &length);
  58. }
  59. if (lua_isstring(L, 4))
  60. {
  61. body = luaL_checklstring(L, 4, &length);
  62. }
  63. if (lua_type(L, 5) == LUA_TFUNCTION || lua_type(L, 5) == LUA_TLIGHTFUNCTION) {
  64. lua_pushvalue(L, 5); // copy argument (func) to the top of stack
  65. if (http_callback_registry != LUA_NOREF)
  66. luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
  67. http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
  68. }
  69. http_request(url, method, headers, body, http_callback, 0);
  70. return 0;
  71. }
  72. // Lua: http.post( url, header, body, function(status, reponse) end )
  73. static int http_lapi_post( lua_State *L )
  74. {
  75. int length;
  76. const char * url = luaL_checklstring(L, 1, &length);
  77. const char * headers = NULL;
  78. const char * body = NULL;
  79. // Check parameter
  80. if ((url == NULL))
  81. {
  82. return luaL_error( L, "wrong arg type" );
  83. }
  84. if (lua_isstring(L, 2))
  85. {
  86. headers = luaL_checklstring(L, 2, &length);
  87. }
  88. if (lua_isstring(L, 3))
  89. {
  90. body = luaL_checklstring(L, 3, &length);
  91. }
  92. if (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION) {
  93. lua_pushvalue(L, 4); // copy argument (func) to the top of stack
  94. if (http_callback_registry != LUA_NOREF)
  95. luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
  96. http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
  97. }
  98. http_post(url, headers, body, http_callback);
  99. return 0;
  100. }
  101. // Lua: http.put( url, header, body, function(status, reponse) end )
  102. static int http_lapi_put( lua_State *L )
  103. {
  104. int length;
  105. const char * url = luaL_checklstring(L, 1, &length);
  106. const char * headers = NULL;
  107. const char * body = NULL;
  108. // Check parameter
  109. if ((url == NULL))
  110. {
  111. return luaL_error( L, "wrong arg type" );
  112. }
  113. if (lua_isstring(L, 2))
  114. {
  115. headers = luaL_checklstring(L, 2, &length);
  116. }
  117. if (lua_isstring(L, 3))
  118. {
  119. body = luaL_checklstring(L, 3, &length);
  120. }
  121. if (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION) {
  122. lua_pushvalue(L, 4); // copy argument (func) to the top of stack
  123. if (http_callback_registry != LUA_NOREF)
  124. luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
  125. http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
  126. }
  127. http_put(url, headers, body, http_callback);
  128. return 0;
  129. }
  130. // Lua: http.delete( url, header, body, function(status, reponse) end )
  131. static int http_lapi_delete( lua_State *L )
  132. {
  133. int length;
  134. const char * url = luaL_checklstring(L, 1, &length);
  135. const char * headers = NULL;
  136. const char * body = NULL;
  137. // Check parameter
  138. if ((url == NULL))
  139. {
  140. return luaL_error( L, "wrong arg type" );
  141. }
  142. if (lua_isstring(L, 2))
  143. {
  144. headers = luaL_checklstring(L, 2, &length);
  145. }
  146. if (lua_isstring(L, 3))
  147. {
  148. body = luaL_checklstring(L, 3, &length);
  149. }
  150. if (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION) {
  151. lua_pushvalue(L, 4); // copy argument (func) to the top of stack
  152. if (http_callback_registry != LUA_NOREF)
  153. luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
  154. http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
  155. }
  156. http_delete(url, headers, body, http_callback);
  157. return 0;
  158. }
  159. // Lua: http.get( url, header, function(status, reponse) end )
  160. static int http_lapi_get( lua_State *L )
  161. {
  162. int length;
  163. const char * url = luaL_checklstring(L, 1, &length);
  164. const char * headers = NULL;
  165. // Check parameter
  166. if ((url == NULL))
  167. {
  168. return luaL_error( L, "wrong arg type" );
  169. }
  170. if (lua_isstring(L, 2))
  171. {
  172. headers = luaL_checklstring(L, 2, &length);
  173. }
  174. if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION) {
  175. lua_pushvalue(L, 3); // copy argument (func) to the top of stack
  176. if (http_callback_registry != LUA_NOREF)
  177. luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
  178. http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
  179. }
  180. http_get(url, headers, http_callback);
  181. return 0;
  182. }
  183. // Module function map
  184. static const LUA_REG_TYPE http_map[] = {
  185. { LSTRKEY( "request" ), LFUNCVAL( http_lapi_request ) },
  186. { LSTRKEY( "post" ), LFUNCVAL( http_lapi_post ) },
  187. { LSTRKEY( "put" ), LFUNCVAL( http_lapi_put ) },
  188. { LSTRKEY( "delete" ), LFUNCVAL( http_lapi_delete ) },
  189. { LSTRKEY( "get" ), LFUNCVAL( http_lapi_get ) },
  190. { LSTRKEY( "OK" ), LNUMVAL( 0 ) },
  191. { LSTRKEY( "ERROR" ), LNUMVAL( HTTP_STATUS_GENERIC_ERROR ) },
  192. { LNILKEY, LNILVAL }
  193. };
  194. NODEMCU_MODULE(HTTP, "http", http_map, NULL);