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 20
#ifdef USE_UV
#include <uv.h>
#endif
#include "zlib.h"
S
Shengliang Guan 已提交
21 22 23 24
#include "thttp.h"
#include "taoserror.h"
#include "tlog.h"

S
Shengliang Guan 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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 已提交
47 48 49
int32_t taosCompressHttpRport(char* pSrc, int32_t srcLen) {
  int32_t code = -1;
  int32_t destLen = srcLen;
wafwerar's avatar
wafwerar 已提交
50
  void*   pDest = taosMemoryMalloc(destLen);
S
Shengliang Guan 已提交
51 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

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

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

131
int32_t taosSendHttpReport(const char* server, uint16_t port, char* pCont, int32_t contLen, EHttpCompFlag flag) {
132 133 134 135 136 137
  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 已提交
138 139

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

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

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

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

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

162 163 164 165 166 167
  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;
168
  uv_tcp_connect(connect, &socket_tcp, (const struct sockaddr*)&dest, clientConnCb);
S
Shengliang Guan 已提交
169
  uv_run(loop, UV_RUN_DEFAULT);
170
  uv_loop_close(loop);
wafwerar's avatar
wafwerar 已提交
171
  taosMemoryFree(connect);
172 173 174 175
  return terrno;
}

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

  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 已提交
187 188
  pSocket = taosOpenTcpClientSocket(ip, port, 0);
  if (pSocket == NULL) {
S
Shengliang Guan 已提交
189
    terrno = TAOS_SYSTEM_ERROR(errno);
S
monitor  
Shengliang Guan 已提交
190
    uError("failed to create http socket to %s:%u since %s", server, port, terrstr());
S
Shengliang Guan 已提交
191 192 193
    goto SEND_OVER;
  }

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

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

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

  // read something to avoid nginx error 499
wafwerar's avatar
wafwerar 已提交
218
  if (taosWriteMsg(pSocket, header, 10) < 0) {
S
Shengliang Guan 已提交
219
    terrno = TAOS_SYSTEM_ERROR(errno);
220
    uError("failed to receive response from %s:%u since %s", server, port, terrstr());
S
Shengliang Guan 已提交
221 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 234

#endif