http.c 7.3 KB

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