http.c 6.3 KB

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