luat_http_rtt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "luat_base.h"
  2. #include "luat_http.h"
  3. #include "luat_malloc.h"
  4. #define LUAT_LOG_TAG "http"
  5. #include "luat_log.h"
  6. #include "rtthread.h"
  7. #ifdef SAL_USING_POSIX
  8. #include "webclient.h"
  9. #include "fcntl.h"
  10. #undef WEBCLIENT_HEADER_BUFSZ
  11. #undef WEBCLIENT_RESPONSE_BUFSZ
  12. #define WEBCLIENT_HEADER_BUFSZ (512*3)
  13. #define WEBCLIENT_RESPONSE_BUFSZ (512*3)
  14. static void webclient_req(luat_lib_http_req_t *req)
  15. {
  16. int fd = -1, rc = WEBCLIENT_OK;
  17. size_t offset;
  18. int length = 0;
  19. size_t total_length = 0;
  20. unsigned char *ptr = RT_NULL;
  21. struct webclient_session* session = RT_NULL;
  22. int resp_status = 0;
  23. const char* filename = req->dwpath;
  24. const char* URI = req->url;
  25. session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
  26. if(session == RT_NULL)
  27. {
  28. rc = -WEBCLIENT_NOMEM;
  29. goto __exit;
  30. }
  31. else {
  32. LLOGD("webclient_session_create ok");
  33. }
  34. rc = webclient_connect(session, URI);
  35. if (rc != WEBCLIENT_OK)
  36. {
  37. /* connect to webclient server failed. */
  38. LLOGE("http connect fail");
  39. goto __exit;
  40. }
  41. else {
  42. LLOGD("http connect ok");
  43. }
  44. // TODO 把DELETE和PUT支持一下
  45. rc = webclient_send_header(session, req->body.size == 0 ? WEBCLIENT_GET : WEBCLIENT_POST);
  46. if (rc != WEBCLIENT_OK)
  47. {
  48. /* send header to webclient server failed. */
  49. LLOGE("http send header fail");
  50. goto __exit;
  51. }
  52. else {
  53. LLOGD("http send header ok");
  54. }
  55. if (req->body.size)
  56. {
  57. webclient_write(session, req->body.ptr, req->body.size);
  58. }
  59. /* resolve response data, get http status code */
  60. resp_status = webclient_handle_response(session);
  61. LLOGD("post handle response(%d).", resp_status);
  62. if (resp_status < 200)
  63. {
  64. LLOGW("get file failed, wrong response: %d (-0x%X).", resp_status, resp_status);
  65. rc = -WEBCLIENT_ERROR;
  66. goto __exit;
  67. }
  68. fd = open(req->dwpath, O_WRONLY | O_CREAT | O_TRUNC, 0);
  69. if (fd < 0)
  70. {
  71. LLOGW("get file failed, open file(%s) error.", filename);
  72. rc = -WEBCLIENT_ERROR;
  73. goto __exit;
  74. }
  75. ptr = (unsigned char *) web_malloc(WEBCLIENT_RESPONSE_BUFSZ);
  76. if (ptr == RT_NULL)
  77. {
  78. LLOGW("get file failed, no memory for response buffer.");
  79. rc = -WEBCLIENT_NOMEM;
  80. goto __exit;
  81. }
  82. if (session->content_length < 0)
  83. {
  84. while (total_length < 64*1024)
  85. {
  86. length = webclient_read(session, ptr, WEBCLIENT_RESPONSE_BUFSZ);
  87. if (length > 0)
  88. {
  89. write(fd, ptr, length);
  90. total_length += length;
  91. //LOG_RAW(">");
  92. }
  93. else
  94. {
  95. break;
  96. }
  97. }
  98. }
  99. else
  100. {
  101. for (offset = 0; offset < (size_t) session->content_length;)
  102. {
  103. length = webclient_read(session, ptr,
  104. session->content_length - offset > WEBCLIENT_RESPONSE_BUFSZ ?
  105. WEBCLIENT_RESPONSE_BUFSZ : session->content_length - offset);
  106. if (length > 0)
  107. {
  108. write(fd, ptr, length);
  109. total_length += length;
  110. //LOG_RAW(">");
  111. }
  112. else
  113. {
  114. break;
  115. }
  116. offset += length;
  117. }
  118. }
  119. if (total_length)
  120. {
  121. LLOGW("save %d bytes.", total_length);
  122. }
  123. __exit:
  124. if (fd >= 0)
  125. {
  126. close(fd);
  127. }
  128. if (session != RT_NULL)
  129. {
  130. webclient_close(session);
  131. }
  132. if (ptr != RT_NULL)
  133. {
  134. web_free(ptr);
  135. }
  136. luat_lib_http_resp_t *resp = luat_heap_malloc(sizeof(luat_lib_http_resp_t));
  137. if (resp == NULL) {
  138. LLOGE("sys out of memory! malloc for luat_lib_http_resp_t return NULL");
  139. luat_http_req_gc(req);
  140. // TODO 重启?
  141. return;
  142. }
  143. memset(resp, 0, sizeof(luat_lib_http_resp_t));
  144. resp->luacb = req->luacb;
  145. resp->code = resp_status > 0 ? resp_status : rc;
  146. if (resp->code < 0) {
  147. LLOGD("http req error %d %p %p", rc, resp, req->httpcb);
  148. req->httpcb(resp);
  149. }
  150. else {
  151. if (resp_status >= 200) {
  152. resp->body.type = 1;
  153. if (total_length > 0 && total_length < WEBCLIENT_RESPONSE_BUFSZ) {
  154. resp->body.ptr = luat_heap_malloc(total_length);
  155. if (resp->body.ptr != NULL) {
  156. fd = open(req->dwpath, O_RDONLY, 0);
  157. if (fd) {
  158. read(fd, resp->body.ptr, total_length);
  159. close(fd);
  160. resp->body.size = total_length;
  161. }
  162. else {
  163. resp->body.size = 0;
  164. luat_heap_free(resp->body.ptr);
  165. LLOGW("resp file is fail to open");
  166. }
  167. }
  168. else {
  169. LLOGW("resp body malloc fail!!! size=%d", total_length);
  170. }
  171. }
  172. else {
  173. LLOGI("resp is too big, only save at file");
  174. }
  175. }
  176. else {
  177. LLOGI("resp code < 200, skip body");
  178. }
  179. req->httpcb(resp);
  180. }
  181. LLOGD("http every done, clean req");
  182. luat_http_req_gc(req);
  183. }
  184. static void luat_http_thread_entry(void* arg) {
  185. luat_lib_http_req_t *req = (luat_lib_http_req_t *)arg;
  186. // 默认下载到到文件里
  187. if (req->dwpath[0] == 0x00) {
  188. strcpy(req->dwpath, "/httpdw.bin");
  189. }
  190. webclient_req(req);
  191. }
  192. int luat_http_req(luat_lib_http_req_t *req) {
  193. rt_thread_t tid;
  194. int ret = -1;
  195. tid = rt_thread_create("http",
  196. luat_http_thread_entry,
  197. req,
  198. 4*1024,
  199. 22,
  200. 20);
  201. if (tid)
  202. {
  203. ret = rt_thread_startup(tid);
  204. }
  205. else {
  206. LLOGE("http thread fail to start");
  207. }
  208. return ret;
  209. }
  210. #endif