thttp.c 8.4 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#define _DEFAULT_SOURCE
dengyihao's avatar
opt rpc  
dengyihao 已提交
17
// clang-format off
dengyihao's avatar
dengyihao 已提交
18
#include <uv.h>
dengyihao's avatar
dengyihao 已提交
19
#include "zlib.h"
dengyihao's avatar
opt rpc  
dengyihao 已提交
20
#include "thttp.h"
dengyihao's avatar
opt rpc  
dengyihao 已提交
21
#include "taoserror.h"
S
Shengliang Guan 已提交
22
#include "tlog.h"
dengyihao's avatar
dengyihao 已提交
23

dengyihao's avatar
dengyihao 已提交
24
// clang-format on
dengyihao's avatar
dengyihao 已提交
25 26

#define HTTP_RECV_BUF_SIZE 1024
dengyihao's avatar
dengyihao 已提交
27

dengyihao's avatar
dengyihao 已提交
28 29 30 31
typedef struct SHttpClient {
  uv_connect_t conn;
  uv_tcp_t     tcp;
  uv_write_t   req;
dengyihao's avatar
dengyihao 已提交
32
  uv_buf_t*    wbuf;
dengyihao's avatar
dengyihao 已提交
33
  char*        rbuf;
dengyihao's avatar
dengyihao 已提交
34 35 36
  char*        addr;
  uint16_t     port;
} SHttpClient;
S
Shengliang Guan 已提交
37

S
Shengliang Guan 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
static int32_t taosBuildHttpHeader(const char* server, int32_t contLen, char* pHead, int32_t headLen,
                                   EHttpCompFlag flag) {
  if (flag == HTTP_FLAT) {
    return snprintf(pHead, headLen,
                    "POST /report HTTP/1.1\n"
                    "Host: %s\n"
                    "Content-Type: application/json\n"
                    "Content-Length: %d\n\n",
                    server, contLen);
  } else if (flag == HTTP_GZIP) {
    return snprintf(pHead, headLen,
                    "POST /report HTTP/1.1\n"
                    "Host: %s\n"
                    "Content-Type: application/json\n"
                    "Content-Encoding: gzip\n"
                    "Content-Length: %d\n\n",
                    server, contLen);
  } else {
    return -1;
  }
}

dengyihao's avatar
dengyihao 已提交
60
static int32_t taosCompressHttpRport(char* pSrc, int32_t srcLen) {
S
Shengliang Guan 已提交
61 62
  int32_t code = -1;
  int32_t destLen = srcLen;
wafwerar's avatar
wafwerar 已提交
63
  void*   pDest = taosMemoryMalloc(destLen);
S
Shengliang Guan 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

  if (pDest == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  z_stream gzipStream = {0};
  gzipStream.zalloc = (alloc_func)0;
  gzipStream.zfree = (free_func)0;
  gzipStream.opaque = (voidpf)0;
  if (deflateInit2(&gzipStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  gzipStream.next_in = (Bytef*)pSrc;
  gzipStream.avail_in = (uLong)srcLen;
  gzipStream.next_out = (Bytef*)pDest;
  gzipStream.avail_out = (uLong)(destLen);

  while (gzipStream.avail_in != 0 && gzipStream.total_out < (uLong)(destLen)) {
    if (deflate(&gzipStream, Z_FULL_FLUSH) != Z_OK) {
      terrno = TSDB_CODE_COMPRESS_ERROR;
      goto _OVER;
    }
  }

  if (gzipStream.avail_in != 0) {
    terrno = TSDB_CODE_COMPRESS_ERROR;
    goto _OVER;
  }

  int32_t err = 0;
  while (1) {
    if ((err = deflate(&gzipStream, Z_FINISH)) == Z_STREAM_END) {
      break;
    }
    if (err != Z_OK) {
      terrno = TSDB_CODE_COMPRESS_ERROR;
      goto _OVER;
    }
  }

  if (deflateEnd(&gzipStream) != Z_OK) {
    terrno = TSDB_CODE_COMPRESS_ERROR;
    goto _OVER;
  }

  if (gzipStream.total_out >= srcLen) {
    terrno = TSDB_CODE_COMPRESS_ERROR;
    goto _OVER;
  }

  code = 0;

_OVER:
  if (code == 0) {
    memcpy(pSrc, pDest, gzipStream.total_out);
    code = gzipStream.total_out;
  }

wafwerar's avatar
wafwerar 已提交
125
  taosMemoryFree(pDest);
S
Shengliang Guan 已提交
126 127 128
  return code;
}

dengyihao's avatar
dengyihao 已提交
129
static FORCE_INLINE void destroyHttpClient(SHttpClient* cli) {
dengyihao's avatar
dengyihao 已提交
130 131
  taosMemoryFree(cli->wbuf);
  taosMemoryFree(cli->rbuf);
dengyihao's avatar
dengyihao 已提交
132 133 134
  taosMemoryFree(cli->addr);
  taosMemoryFree(cli);
}
dengyihao's avatar
dengyihao 已提交
135
static FORCE_INLINE void clientCloseCb(uv_handle_t* handle) {
dengyihao's avatar
dengyihao 已提交
136 137 138
  SHttpClient* cli = handle->data;
  destroyHttpClient(cli);
}
dengyihao's avatar
dengyihao 已提交
139
static FORCE_INLINE void clientAllocBuffCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
140 141 142
  SHttpClient* cli = handle->data;
  buf->base = cli->rbuf;
  buf->len = HTTP_RECV_BUF_SIZE;
dengyihao's avatar
dengyihao 已提交
143
}
dengyihao's avatar
dengyihao 已提交
144
static FORCE_INLINE void clientRecvCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
145
  SHttpClient* cli = handle->data;
dengyihao's avatar
dengyihao 已提交
146
  if (nread < 0) {
dengyihao's avatar
dengyihao 已提交
147
    uError("http-report recv error:%s", uv_err_name(nread));
dengyihao's avatar
dengyihao 已提交
148
  } else {
dengyihao's avatar
dengyihao 已提交
149
    uTrace("http-report succ to recv %d bytes", (int32_t)nread);
dengyihao's avatar
dengyihao 已提交
150
  }
dengyihao's avatar
dengyihao 已提交
151 152 153 154 155
  if (!uv_is_closing((uv_handle_t*)&cli->tcp)) {
    uv_close((uv_handle_t*)&cli->tcp, clientCloseCb);
  } else {
    destroyHttpClient(cli);
  }
dengyihao's avatar
dengyihao 已提交
156
}
dengyihao's avatar
dengyihao 已提交
157 158 159 160 161
static void clientSentCb(uv_write_t* req, int32_t status) {
  SHttpClient* cli = req->data;
  if (status != 0) {
    terrno = TAOS_SYSTEM_ERROR(status);
    uError("http-report failed to send data %s", uv_strerror(status));
dengyihao's avatar
dengyihao 已提交
162 163 164 165 166
    if (!uv_is_closing((uv_handle_t*)&cli->tcp)) {
      uv_close((uv_handle_t*)&cli->tcp, clientCloseCb);
    } else {
      destroyHttpClient(cli);
    }
dengyihao's avatar
dengyihao 已提交
167
    return;
dengyihao's avatar
dengyihao 已提交
168
  } else {
169
    uTrace("http-report succ to send data");
dengyihao's avatar
dengyihao 已提交
170
  }
dengyihao's avatar
dengyihao 已提交
171 172 173 174 175 176 177 178 179 180
  status = uv_read_start((uv_stream_t*)&cli->tcp, clientAllocBuffCb, clientRecvCb);
  if (status != 0) {
    terrno = TAOS_SYSTEM_ERROR(status);
    uError("http-report failed to recv data,reason:%s, dst:%s:%d", uv_strerror(status), cli->addr, cli->port);
    if (!uv_is_closing((uv_handle_t*)&cli->tcp)) {
      uv_close((uv_handle_t*)&cli->tcp, clientCloseCb);
    } else {
      destroyHttpClient(cli);
    }
  }
dengyihao's avatar
dengyihao 已提交
181
}
S
Shengliang Guan 已提交
182
static void clientConnCb(uv_connect_t* req, int32_t status) {
dengyihao's avatar
dengyihao 已提交
183 184
  SHttpClient* cli = req->data;
  if (status != 0) {
185
    terrno = TAOS_SYSTEM_ERROR(status);
dengyihao's avatar
dengyihao 已提交
186
    uError("http-report failed to conn to server, reason:%s, dst:%s:%d", uv_strerror(status), cli->addr, cli->port);
dengyihao's avatar
dengyihao 已提交
187 188 189 190 191
    if (!uv_is_closing((uv_handle_t*)&cli->tcp)) {
      uv_close((uv_handle_t*)&cli->tcp, clientCloseCb);
    } else {
      destroyHttpClient(cli);
    }
192 193
    return;
  }
dengyihao's avatar
dengyihao 已提交
194 195 196 197 198 199 200 201 202 203
  status = uv_write(&cli->req, (uv_stream_t*)&cli->tcp, cli->wbuf, 2, clientSentCb);
  if (0 != status) {
    terrno = TAOS_SYSTEM_ERROR(status);
    uError("http-report failed to send data,reason:%s, dst:%s:%d", uv_strerror(status), cli->addr, cli->port);
    if (!uv_is_closing((uv_handle_t*)&cli->tcp)) {
      uv_close((uv_handle_t*)&cli->tcp, clientCloseCb);
    } else {
      destroyHttpClient(cli);
    }
  }
204 205
}

dengyihao's avatar
dengyihao 已提交
206
static FORCE_INLINE int32_t taosBuildDstAddr(const char* server, uint16_t port, struct sockaddr_in* dest) {
dengyihao's avatar
dengyihao 已提交
207 208
  uint32_t ip = taosGetIpv4FromFqdn(server);
  if (ip == 0xffffffff) {
209
    terrno = TAOS_SYSTEM_ERROR(errno);
dengyihao's avatar
dengyihao 已提交
210
    uError("http-report failed to get http server:%s since %s", server, errno == 0 ? "invalid http server" : terrstr());
211 212
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
213 214 215
  char buf[128] = {0};
  tinet_ntoa(buf, ip);
  uv_ip4_addr(buf, port, dest);
dengyihao's avatar
dengyihao 已提交
216
  return 0;
217
}
S
Shengliang Guan 已提交
218
int32_t taosSendHttpReport(const char* server, uint16_t port, char* pCont, int32_t contLen, EHttpCompFlag flag) {
dengyihao's avatar
dengyihao 已提交
219 220 221
  struct sockaddr_in dest = {0};
  if (taosBuildDstAddr(server, port, &dest) < 0) {
    return -1;
S
Shengliang Guan 已提交
222
  }
S
Shengliang Guan 已提交
223 224 225 226 227 228 229 230
  if (flag == HTTP_GZIP) {
    int32_t dstLen = taosCompressHttpRport(pCont, contLen);
    if (dstLen > 0) {
      contLen = dstLen;
    } else {
      flag = HTTP_FLAT;
    }
  }
dengyihao's avatar
dengyihao 已提交
231
  terrno = 0;
S
Shengliang Guan 已提交
232

dengyihao's avatar
dengyihao 已提交
233
  char    header[2048] = {0};
S
Shengliang Guan 已提交
234
  int32_t headLen = taosBuildHttpHeader(server, contLen, header, sizeof(header), flag);
S
Shengliang Guan 已提交
235

dengyihao's avatar
dengyihao 已提交
236 237 238
  uv_buf_t* wb = taosMemoryCalloc(2, sizeof(uv_buf_t));
  wb[0] = uv_buf_init((char*)header, headLen);  // stack var
  wb[1] = uv_buf_init((char*)pCont, contLen);   //  heap var
S
Shengliang Guan 已提交
239

dengyihao's avatar
dengyihao 已提交
240 241 242 243
  SHttpClient* cli = taosMemoryCalloc(1, sizeof(SHttpClient));
  cli->conn.data = cli;
  cli->tcp.data = cli;
  cli->req.data = cli;
dengyihao's avatar
dengyihao 已提交
244
  cli->wbuf = wb;
dengyihao's avatar
dengyihao 已提交
245
  cli->rbuf = taosMemoryCalloc(1, HTTP_RECV_BUF_SIZE);
dengyihao's avatar
dengyihao 已提交
246 247
  cli->addr = tstrdup(server);
  cli->port = port;
S
Shengliang Guan 已提交
248

dengyihao's avatar
dengyihao 已提交
249 250 251 252 253 254 255
  uv_loop_t* loop = taosMemoryMalloc(sizeof(uv_loop_t));
  int        err = uv_loop_init(loop);
  if (err != 0) {
    uError("http-report failed to init uv_loop, reason: %s", uv_strerror(err));
    taosMemoryFree(loop);
    terrno = TAOS_SYSTEM_ERROR(err);
    destroyHttpClient(cli);
dengyihao's avatar
dengyihao 已提交
256
    return terrno;
dengyihao's avatar
dengyihao 已提交
257
  }
dengyihao's avatar
dengyihao 已提交
258 259
  uv_tcp_init(loop, &cli->tcp);
  // set up timeout to avoid stuck;
dengyihao's avatar
dengyihao 已提交
260
  int32_t fd = taosCreateSocketWithTimeout(5);
S
Shengliang Guan 已提交
261

dengyihao's avatar
dengyihao 已提交
262
  int ret = uv_tcp_open((uv_tcp_t*)&cli->tcp, fd);
dengyihao's avatar
dengyihao 已提交
263
  if (ret != 0) {
dengyihao's avatar
dengyihao 已提交
264
    uError("http-report failed to open socket, reason:%s, dst:%s:%d", uv_strerror(ret), cli->addr, cli->port);
dengyihao's avatar
dengyihao 已提交
265
    destroyHttpClient(cli);
dengyihao's avatar
dengyihao 已提交
266
    uv_stop(loop);
dengyihao's avatar
dengyihao 已提交
267 268 269 270 271 272 273 274 275 276
    terrno = TAOS_SYSTEM_ERROR(ret);
  } else {
    ret = uv_tcp_connect(&cli->conn, &cli->tcp, (const struct sockaddr*)&dest, clientConnCb);
    if (ret != 0) {
      uError("http-report failed to connect to http-server, reason:%s, dst:%s:%d", uv_strerror(ret), cli->addr,
             cli->port);
      destroyHttpClient(cli);
      uv_stop(loop);
      terrno = TAOS_SYSTEM_ERROR(ret);
    }
S
Shengliang Guan 已提交
277 278
  }

dengyihao's avatar
dengyihao 已提交
279 280
  uv_run(loop, UV_RUN_DEFAULT);
  uv_loop_close(loop);
dengyihao's avatar
dengyihao 已提交
281
  taosMemoryFree(loop);
dengyihao's avatar
dengyihao 已提交
282
  return terrno;
S
Shengliang Guan 已提交
283
}