http.c 6.2 KB

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