1
0

AsyncHttpClient.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "AsyncHttpClient.h"
  2. namespace hv {
  3. int AsyncHttpClient::doTask(const HttpClientTaskPtr& task) {
  4. const HttpRequestPtr& req = task->req;
  5. // queueInLoop timeout?
  6. uint64_t now_hrtime = hloop_now_hrtime(loop_thread.hloop());
  7. int elapsed_ms = (now_hrtime - task->start_time) / 1000;
  8. int timeout_ms = req->timeout * 1000;
  9. if (timeout_ms > 0 && elapsed_ms >= timeout_ms) {
  10. hlogw("%s queueInLoop timeout!", req->url.c_str());
  11. return -10;
  12. }
  13. req->ParseUrl();
  14. sockaddr_u peeraddr;
  15. memset(&peeraddr, 0, sizeof(peeraddr));
  16. int ret = sockaddr_set_ipport(&peeraddr, req->host.c_str(), req->port);
  17. if (ret != 0) {
  18. hloge("unknown host %s", req->host.c_str());
  19. return -20;
  20. }
  21. int connfd = -1;
  22. hio_t* connio = NULL;
  23. HttpClientContextPtr ctx = NULL;
  24. // first get from conn_pools
  25. char strAddr[SOCKADDR_STRLEN] = {0};
  26. SOCKADDR_STR(&peeraddr, strAddr);
  27. auto iter = conn_pools.find(strAddr);
  28. if (iter != conn_pools.end()) {
  29. if (iter->second.get(connfd)) {
  30. // hlogd("get from conn_pools");
  31. ctx = getContext(connfd);
  32. }
  33. }
  34. if (connfd < 0) {
  35. // create socket
  36. connfd = socket(peeraddr.sa.sa_family, SOCK_STREAM, 0);
  37. if (connfd < 0) {
  38. perror("socket");
  39. return -30;
  40. }
  41. connio = hio_get(loop_thread.hloop(), connfd);
  42. assert(connio != NULL);
  43. hio_set_peeraddr(connio, &peeraddr.sa, sockaddr_len(&peeraddr));
  44. // https
  45. if (req->https) {
  46. hio_enable_ssl(connio);
  47. }
  48. }
  49. if (ctx == NULL) {
  50. // new HttpClientContext
  51. ctx.reset(new HttpClientContext);
  52. ctx->channel.reset(new SocketChannel(connio));
  53. addContext(ctx);
  54. }
  55. ctx->req = req;
  56. ctx->cb = task->cb;
  57. ctx->channel->onread = [this, ctx](Buffer* buf) {
  58. const char* data = (const char*)buf->data();
  59. int len = buf->size();
  60. int nparse = ctx->parser->FeedRecvData(data, len);
  61. if (nparse != len) {
  62. ctx->errorCallback();
  63. ctx->channel->close();
  64. return;
  65. }
  66. if (ctx->parser->IsComplete()) {
  67. std::string req_connection = ctx->req->GetHeader("Connection");
  68. std::string resp_connection = ctx->resp->GetHeader("Connection");
  69. ctx->successCallback();
  70. if (stricmp(req_connection.c_str(), "keep-alive") == 0 &&
  71. stricmp(resp_connection.c_str(), "keep-alive") == 0) {
  72. // add into conn_pools to reuse
  73. // hlogd("add into conn_pools");
  74. conn_pools[ctx->channel->peeraddr()].add(ctx->channel->fd());
  75. } else {
  76. ctx->channel->close();
  77. }
  78. }
  79. };
  80. ctx->channel->onclose = [this, ctx, task]() {
  81. ctx->channel->status = SocketChannel::CLOSED;
  82. removeContext(ctx);
  83. if (task->retry_cnt-- > 0) {
  84. // try again
  85. send(task);
  86. } else {
  87. ctx->errorCallback();
  88. }
  89. };
  90. // timer
  91. if (timeout_ms > 0) {
  92. ctx->timerID = setTimeout(timeout_ms - elapsed_ms, [ctx](TimerID timerID){
  93. hlogw("%s timeout!", ctx->req->url.c_str());
  94. if (ctx->channel) {
  95. ctx->channel->close();
  96. }
  97. });
  98. }
  99. if (ctx->channel->isConnected()) {
  100. // sendRequest
  101. sendRequest(ctx);
  102. } else {
  103. // startConnect
  104. hevent_set_userdata(connio, this);
  105. hio_setcb_connect(connio, onconnect);
  106. hio_connect(connio);
  107. }
  108. return 0;
  109. }
  110. void AsyncHttpClient::onconnect(hio_t* io) {
  111. AsyncHttpClient* client = (AsyncHttpClient*)hevent_userdata(io);
  112. HttpClientContextPtr ctx = client->getContext(hio_fd(io));
  113. assert(ctx != NULL && ctx->req != NULL && ctx->channel != NULL);
  114. ctx->channel->status = SocketChannel::CONNECTED;
  115. client->sendRequest(ctx);
  116. ctx->channel->startRead();
  117. }
  118. int AsyncHttpClient::sendRequest(const HttpClientContextPtr ctx) {
  119. assert(ctx != NULL && ctx->req != NULL && ctx->channel != NULL);
  120. SocketChannelPtr channel = ctx->channel;
  121. if (ctx->parser == NULL) {
  122. ctx->parser.reset(HttpParser::New(HTTP_CLIENT, (http_version)ctx->req->http_major));
  123. }
  124. if (ctx->resp == NULL) {
  125. ctx->resp.reset(new HttpResponse);
  126. }
  127. ctx->parser->InitResponse(ctx->resp.get());
  128. ctx->parser->SubmitRequest(ctx->req.get());
  129. char* data = NULL;
  130. size_t len = 0;
  131. while (ctx->parser->GetSendData(&data, &len)) {
  132. Buffer buf(data, len);
  133. channel->write(&buf);
  134. }
  135. return 0;
  136. }
  137. }