thttp.c 6.3 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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
#include "thttp.h"
#include "taoserror.h"
#include "tlog.h"
S
Shengliang Guan 已提交
20
#include "zlib.h"
S
Shengliang Guan 已提交
21

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

  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;
  }

  free(pDest);
  return code;
}

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

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

  char ipv4Buf[128] = {0};
138 139
  tinet_ntoa(ipv4Buf, ipv4);

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

S
Shengliang Guan 已提交
143 144
  uv_tcp_t   socket_tcp = {0};
  uv_loop_t* loop = uv_default_loop();
145 146 147
  uv_tcp_init(loop, &socket_tcp);
  uv_connect_t* connect = (uv_connect_t*)malloc(sizeof(uv_connect_t));

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

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

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

  connect->data = wb;
  uv_tcp_connect(connect, &socket_tcp, (const struct sockaddr*)&dest, clientConnCb);
  terrno = 0;
S
Shengliang Guan 已提交
167
  uv_run(loop, UV_RUN_DEFAULT);
168 169 170 171 172 173
  uv_loop_close(loop);
  free(connect);
  return terrno;
}

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

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

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

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

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

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

  code = 0;

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

  return code;
}
231 232

#endif