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