thttp.c 6.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
wafwerar's avatar
wafwerar 已提交
17 18 19
#ifdef USE_UV
#include <uv.h>
#endif
dengyihao's avatar
opt rpc  
dengyihao 已提交
20 21
// clang-format off
#include "zlib.h"
dengyihao's avatar
opt rpc  
dengyihao 已提交
22
#include "thttp.h"
dengyihao's avatar
opt rpc  
dengyihao 已提交
23
#include "taoserror.h"
S
Shengliang Guan 已提交
24 25
#include "tlog.h"

S
Shengliang Guan 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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;
  }
}

S
Shengliang Guan 已提交
48 49 50
int32_t taosCompressHttpRport(char* pSrc, int32_t srcLen) {
  int32_t code = -1;
  int32_t destLen = srcLen;
wafwerar's avatar
wafwerar 已提交
51
  void*   pDest = taosMemoryMalloc(destLen);
S
Shengliang Guan 已提交
52 53 54 55 56 57 58 59 60 61 62 63 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

  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 已提交
113
  taosMemoryFree(pDest);
S
Shengliang Guan 已提交
114 115 116
  return code;
}

117
#ifdef USE_UV
S
Shengliang Guan 已提交
118
static void clientConnCb(uv_connect_t* req, int32_t status) {
S
Shengliang Guan 已提交
119
  if (status < 0) {
120
    terrno = TAOS_SYSTEM_ERROR(status);
121
    uError("connection error %s", uv_strerror(status));
122
    uv_close((uv_handle_t*)req->handle, NULL);
123 124 125
    return;
  }
  uv_buf_t* wb = req->data;
126
  assert(wb != NULL);
127 128
  uv_write_t write_req;
  uv_write(&write_req, req->handle, wb, 2, NULL);
S
Shengliang Guan 已提交
129
  uv_close((uv_handle_t*)req->handle, NULL);
130 131
}

132
int32_t taosSendHttpReport(const char* server, uint16_t port, char* pCont, int32_t contLen, EHttpCompFlag flag) {
133 134 135 136 137 138
  uint32_t ipv4 = taosGetIpv4FromFqdn(server);
  if (ipv4 == 0xffffffff) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to get http server:%s ip since %s", server, terrstr());
    return -1;
  }
S
Shengliang Guan 已提交
139 140

  char ipv4Buf[128] = {0};
141 142
  tinet_ntoa(ipv4Buf, ipv4);

S
Shengliang Guan 已提交
143
  struct sockaddr_in dest = {0};
144 145
  uv_ip4_addr(ipv4Buf, port, &dest);

S
Shengliang Guan 已提交
146 147
  uv_tcp_t   socket_tcp = {0};
  uv_loop_t* loop = uv_default_loop();
148
  uv_tcp_init(loop, &socket_tcp);
wafwerar's avatar
wafwerar 已提交
149
  uv_connect_t* connect = (uv_connect_t*)taosMemoryMalloc(sizeof(uv_connect_t));
150

S
Shengliang Guan 已提交
151 152 153 154 155 156 157 158 159
  if (flag == HTTP_GZIP) {
    int32_t dstLen = taosCompressHttpRport(pCont, contLen);
    if (dstLen > 0) {
      contLen = dstLen;
    } else {
      flag = HTTP_FLAT;
    }
  }

S
Shengliang Guan 已提交
160
  char    header[1024] = {0};
S
Shengliang Guan 已提交
161 162
  int32_t headLen = taosBuildHttpHeader(server, contLen, header, sizeof(header), flag);

163 164 165 166 167 168
  uv_buf_t wb[2];
  wb[0] = uv_buf_init((char*)header, headLen);
  wb[1] = uv_buf_init((char*)pCont, contLen);

  connect->data = wb;
  terrno = 0;
169
  uv_tcp_connect(connect, &socket_tcp, (const struct sockaddr*)&dest, clientConnCb);
S
Shengliang Guan 已提交
170
  uv_run(loop, UV_RUN_DEFAULT);
171
  uv_loop_close(loop);
wafwerar's avatar
wafwerar 已提交
172
  taosMemoryFree(connect);
173 174 175 176
  return terrno;
}

#else
S
Shengliang Guan 已提交
177
int32_t taosSendHttpReport(const char* server, uint16_t port, char* pCont, int32_t contLen, EHttpCompFlag flag) {
dengyihao's avatar
opt rpc  
dengyihao 已提交
178
  int32_t     code = -1;
wafwerar's avatar
wafwerar 已提交
179
  TdSocketPtr pSocket = NULL;
S
Shengliang Guan 已提交
180 181 182 183 184 185 186 187

  uint32_t ip = taosGetIpv4FromFqdn(server);
  if (ip == 0xffffffff) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to get http server:%s ip since %s", server, terrstr());
    goto SEND_OVER;
  }

wafwerar's avatar
wafwerar 已提交
188 189
  pSocket = taosOpenTcpClientSocket(ip, port, 0);
  if (pSocket == NULL) {
S
Shengliang Guan 已提交
190
    terrno = TAOS_SYSTEM_ERROR(errno);
S
monitor  
Shengliang Guan 已提交
191
    uError("failed to create http socket to %s:%u since %s", server, port, terrstr());
S
Shengliang Guan 已提交
192 193 194
    goto SEND_OVER;
  }

S
Shengliang Guan 已提交
195 196 197 198 199 200 201 202 203
  if (flag == HTTP_GZIP) {
    int32_t dstLen = taosCompressHttpRport(pCont, contLen);
    if (dstLen > 0) {
      contLen = dstLen;
    } else {
      flag = HTTP_FLAT;
    }
  }

S
Shengliang Guan 已提交
204 205
  char    header[1024] = {0};
  int32_t headLen = taosBuildHttpHeader(server, contLen, header, sizeof(header), flag);
wafwerar's avatar
wafwerar 已提交
206
  if (taosWriteMsg(pSocket, header, headLen) < 0) {
S
Shengliang Guan 已提交
207
    terrno = TAOS_SYSTEM_ERROR(errno);
208
    uError("failed to send http header to %s:%u since %s", server, port, terrstr());
S
Shengliang Guan 已提交
209 210 211
    goto SEND_OVER;
  }

wafwerar's avatar
wafwerar 已提交
212
  if (taosWriteMsg(pSocket, (void*)pCont, contLen) < 0) {
S
Shengliang Guan 已提交
213
    terrno = TAOS_SYSTEM_ERROR(errno);
214
    uError("failed to send http content to %s:%u since %s", server, port, terrstr());
S
Shengliang Guan 已提交
215 216 217 218
    goto SEND_OVER;
  }

  // read something to avoid nginx error 499
wafwerar's avatar
wafwerar 已提交
219
  if (taosWriteMsg(pSocket, header, 10) < 0) {
S
Shengliang Guan 已提交
220
    terrno = TAOS_SYSTEM_ERROR(errno);
221
    uError("failed to receive response from %s:%u since %s", server, port, terrstr());
S
Shengliang Guan 已提交
222 223 224 225 226
    goto SEND_OVER;
  }
  code = 0;

SEND_OVER:
wafwerar's avatar
wafwerar 已提交
227 228
  if (pSocket != NULL) {
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
229 230 231 232
  }

  return code;
}
233

dengyihao's avatar
opt rpc  
dengyihao 已提交
234
// clang-format on
dengyihao's avatar
opt rpc  
dengyihao 已提交
235
#endif