httpJson.c 14.1 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
slguan 已提交
16 17 18 19
#define _DEFAULT_SOURCE
#include "os.h"
#include "taosmsg.h"
#include "taoserror.h"
S
TD-1311  
Shengliang Guan 已提交
20
#include "tglobal.h"
H
hzcheng 已提交
21
#include "http.h"
S
slguan 已提交
22
#include "httpLog.h"
H
hzcheng 已提交
23 24
#include "httpJson.h"
#include "httpResp.h"
25
#include "httpUtil.h"
H
hzcheng 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

#define MAX_NUM_STR_SZ 25

char JsonItmTkn = ',';
char JsonObjStt = '{';
char JsonObjEnd = '}';
char JsonArrStt = '[';
char JsonArrEnd = ']';
char JsonStrStt = '\"';
char JsonStrEnd = '\"';
char JsonPairTkn = ':';
char JsonNulTkn[] = "null";
char JsonTrueTkn[] = "true";
char JsonFalseTkn[] = "false";

S
TD-1311  
Shengliang Guan 已提交
41 42 43 44
int32_t httpWriteBufByFd(struct HttpContext* pContext, const char* buf, int32_t sz) {
  int32_t len;
  int32_t countWait = 0;
  int32_t writeLen = 0;
H
hzcheng 已提交
45 46

  do {
47
    if (pContext->fd > 2){
S
TD-1311  
Shengliang Guan 已提交
48
      len = (int32_t)taosSend(pContext->fd, buf + writeLen, (size_t)(sz - writeLen), MSG_NOSIGNAL);
49 50 51 52 53
    }
    else {
      return sz;
    }

H
hzcheng 已提交
54
    if (len < 0) {
S
TD-1311  
Shengliang Guan 已提交
55
      httpDebug("context:%p, fd:%d, socket write errno:%d, times:%d", pContext, pContext->fd, errno, countWait);
56 57 58
      if (++countWait > HTTP_WRITE_RETRY_TIMES) break;
      taosMsleep(HTTP_WRITE_WAIT_TIME_MS);
      continue;
H
hzcheng 已提交
59
    } else if (len == 0) {
S
TD-1311  
Shengliang Guan 已提交
60
      httpDebug("context:%p, fd:%d, socket write errno:%d, connect already closed", pContext, pContext->fd, errno);
61
      break;
H
hzcheng 已提交
62 63
    } else {
      countWait = 0;
64
      writeLen += len;
H
hzcheng 已提交
65
    }
66
  } while (writeLen < sz);
H
hzcheng 已提交
67

68
  return writeLen;
H
hzcheng 已提交
69 70
}

S
TD-1311  
Shengliang Guan 已提交
71 72
int32_t httpWriteBuf(struct HttpContext* pContext, const char* buf, int32_t sz) {
  int32_t writeSz = httpWriteBufByFd(pContext, buf, sz);
73
  if (writeSz != sz) {
S
TD-1311  
Shengliang Guan 已提交
74 75
    httpError("context:%p, fd:%d, dataSize:%d, writeSize:%d, failed to send response:\n%s", pContext, pContext->fd, sz,
              writeSz, buf);
H
hzcheng 已提交
76
  } else {
S
TD-1311  
Shengliang Guan 已提交
77
    httpTrace("context:%p, fd:%d, dataSize:%d, writeSize:%d, response:\n%s", pContext, pContext->fd, sz, writeSz, buf);
H
hzcheng 已提交
78 79 80 81 82
  }

  return writeSz;
}

S
TD-1311  
Shengliang Guan 已提交
83 84
int32_t httpWriteBufNoTrace(struct HttpContext *pContext, const char *buf, int32_t sz) {
  int32_t writeSz = httpWriteBufByFd(pContext, buf, sz);
S
slguan 已提交
85
  if (writeSz != sz) {
S
TD-1311  
Shengliang Guan 已提交
86 87
    httpError("context:%p, fd:%d, dataSize:%d, writeSize:%d, failed to send response", pContext, pContext->fd, sz,
              writeSz);
S
slguan 已提交
88 89 90 91 92
  }

  return writeSz;
}

S
TD-1311  
Shengliang Guan 已提交
93 94
int32_t httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) {
  int32_t remain = 0;
S
slguan 已提交
95 96 97
  char sLen[24];
  uint64_t srcLen = (uint64_t) (buf->lst - buf->buf);

H
hzcheng 已提交
98
  if (buf->pContext->fd <= 0) {
S
TD-1311  
Shengliang Guan 已提交
99
    httpTrace("context:%p, fd:%d, write json body error", buf->pContext, buf->pContext->fd);
H
hzcheng 已提交
100 101 102
    buf->pContext->fd = -1;
  }

S
slguan 已提交
103 104 105 106 107 108 109 110
  /*
   * HTTP servers often use compression to optimize transmission, for example
   * with Content-Encoding: gzip or Content-Encoding: deflate.
   * If both compression and chunked encoding are enabled, then the content stream is first compressed, then chunked;
   * so the chunk encoding itself is not compressed, and the data in each chunk is not compressed individually.
   * The remote endpoint then decodes the stream by concatenating the chunks and uncompressing the result.
   */

S
TD-1311  
Shengliang Guan 已提交
111
  if (buf->pContext->parser->acceptEncodingGzip == 0 || !tsHttpEnableCompress) {
S
slguan 已提交
112
    if (buf->lst == buf->buf) {
S
TD-1311  
Shengliang Guan 已提交
113
      httpTrace("context:%p, fd:%d, no data need dump", buf->pContext, buf->pContext->fd);
S
slguan 已提交
114 115
      return 0;  // there is no data to dump.
    } else {
S
TD-1311  
Shengliang Guan 已提交
116
      int32_t len = sprintf(sLen, "%lx\r\n", srcLen);
S
TD-1311  
Shengliang Guan 已提交
117 118
      httpTrace("context:%p, fd:%d, write body, chunkSize:%" PRIu64 ", response:\n%s", buf->pContext, buf->pContext->fd,
                srcLen, buf->buf);
S
slguan 已提交
119
      httpWriteBufNoTrace(buf->pContext, sLen, len);
S
TD-1311  
Shengliang Guan 已提交
120
      remain = httpWriteBufNoTrace(buf->pContext, buf->buf, (int32_t)srcLen);
S
slguan 已提交
121
    }
H
hzcheng 已提交
122
  } else {
S
slguan 已提交
123 124
    char compressBuf[JSON_BUFFER_SIZE] = {0};
    int32_t compressBufLen = JSON_BUFFER_SIZE;
S
TD-1311  
Shengliang Guan 已提交
125
    int32_t ret = httpGzipCompress(buf->pContext, buf->buf, srcLen, compressBuf, &compressBufLen, isTheLast);
S
slguan 已提交
126 127
    if (ret == 0) {
      if (compressBufLen > 0) {
S
TD-1311  
Shengliang Guan 已提交
128
        int32_t len = sprintf(sLen, "%x\r\n", compressBufLen);
S
TD-1311  
Shengliang Guan 已提交
129 130
        httpTrace("context:%p, fd:%d, write body, chunkSize:%" PRIu64 ", compressSize:%d, last:%d, response:\n%s",
                  buf->pContext, buf->pContext->fd, srcLen, compressBufLen, isTheLast, buf->buf);
S
slguan 已提交
131
        httpWriteBufNoTrace(buf->pContext, sLen, len);
S
TD-1311  
Shengliang Guan 已提交
132
        remain = httpWriteBufNoTrace(buf->pContext, (const char*)compressBuf, compressBufLen);
S
slguan 已提交
133
      } else {
S
TD-1311  
Shengliang Guan 已提交
134 135
        httpTrace("context:%p, fd:%d, last:%d, compress already dumped, response:\n%s", buf->pContext,
                  buf->pContext->fd, isTheLast, buf->buf);
S
slguan 已提交
136 137 138
        return 0;  // there is no data to dump.
      }
    } else {
S
TD-1311  
Shengliang Guan 已提交
139 140
      httpError("context:%p, fd:%d, failed to compress data, chunkSize:%" PRIu64 ", last:%d, error:%d, response:\n%s",
                buf->pContext, buf->pContext->fd, srcLen, isTheLast, ret, buf->buf);
S
slguan 已提交
141 142
      return 0;
    }
H
hzcheng 已提交
143 144
  }

S
slguan 已提交
145
  httpWriteBufNoTrace(buf->pContext, "\r\n", 2);
S
TD-1311  
Shengliang Guan 已提交
146
  buf->total += (int32_t)(buf->lst - buf->buf);
H
hzcheng 已提交
147
  buf->lst = buf->buf;
S
TD-1311  
Shengliang Guan 已提交
148
  memset(buf->buf, 0, (size_t)buf->size);
S
slguan 已提交
149
  return remain;
H
hzcheng 已提交
150 151 152 153 154 155 156 157
}

void httpWriteJsonBufHead(JsonBuf* buf) {
  if (buf->pContext->fd <= 0) {
    buf->pContext->fd = -1;
  }

  char msg[1024] = {0};
S
TD-1311  
Shengliang Guan 已提交
158
  int32_t  len = -1;
H
hzcheng 已提交
159

S
TD-1311  
Shengliang Guan 已提交
160 161 162
  if (buf->pContext->parser->acceptEncodingGzip == 0 || !tsHttpEnableCompress) {
    len = sprintf(msg, httpRespTemplate[HTTP_RESPONSE_CHUNKED_UN_COMPRESS], httpVersionStr[buf->pContext->parser->httpVersion],
                  httpKeepAliveStr[buf->pContext->parser->keepAlive]);
H
hzcheng 已提交
163
  } else {
S
TD-1311  
Shengliang Guan 已提交
164 165
    len = sprintf(msg, httpRespTemplate[HTTP_RESPONSE_CHUNKED_COMPRESS], httpVersionStr[buf->pContext->parser->httpVersion],
                  httpKeepAliveStr[buf->pContext->parser->keepAlive]);
H
hzcheng 已提交
166 167 168 169 170 171 172
  }

  httpWriteBuf(buf->pContext, (const char*)msg, len);
}

void httpWriteJsonBufEnd(JsonBuf* buf) {
  if (buf->pContext->fd <= 0) {
S
TD-1311  
Shengliang Guan 已提交
173
    httpTrace("context:%p, fd:%d, json buf fd is 0", buf->pContext, buf->pContext->fd);
H
hzcheng 已提交
174 175 176
    buf->pContext->fd = -1;
  }

S
slguan 已提交
177 178
  httpWriteJsonBufBody(buf, true);
  httpWriteBufNoTrace(buf->pContext, "0\r\n\r\n", 5);  // end of chunked resp
H
hzcheng 已提交
179 180 181 182 183 184 185 186 187
}

void httpInitJsonBuf(JsonBuf* buf, struct HttpContext* pContext) {
  buf->lst = buf->buf;
  buf->total = 0;
  buf->size = JSON_BUFFER_SIZE;  // option setting
  buf->pContext = pContext;
  memset(buf->lst, 0, JSON_BUFFER_SIZE);

S
TD-1311  
Shengliang Guan 已提交
188
  if (pContext->parser->acceptEncodingGzip == 1 && tsHttpEnableCompress) {
S
slguan 已提交
189 190 191
    httpGzipCompressInit(buf->pContext);
  }

R
root 已提交
192
  httpTrace("context:%p, fd:%d, json buffer initialized", buf->pContext, buf->pContext->fd);
H
hzcheng 已提交
193 194 195 196 197 198 199 200 201 202
}

void httpJsonItemToken(JsonBuf* buf) {
  char c = *(buf->lst - 1);
  if (c == JsonArrStt || c == JsonObjStt || c == JsonPairTkn || c == JsonItmTkn) {
    return;
  }
  if (buf->lst > buf->buf) httpJsonToken(buf, JsonItmTkn);
}

S
TD-1311  
Shengliang Guan 已提交
203
void httpJsonString(JsonBuf* buf, char* sVal, int32_t len) {
H
hzcheng 已提交
204 205 206 207 208 209
  httpJsonItemToken(buf);
  httpJsonToken(buf, JsonStrStt);
  httpJsonPrint(buf, sVal, len);
  httpJsonToken(buf, JsonStrEnd);
}

S
TD-1311  
Shengliang Guan 已提交
210
void httpJsonOriginString(JsonBuf* buf, char* sVal, int32_t len) {
H
hzcheng 已提交
211 212 213 214
  httpJsonItemToken(buf);
  httpJsonPrint(buf, sVal, len);
}

S
TD-1311  
Shengliang Guan 已提交
215
void httpJsonStringForTransMean(JsonBuf* buf, char* sVal, int32_t maxLen) {
H
hzcheng 已提交
216 217 218 219 220 221 222 223
  httpJsonItemToken(buf);
  httpJsonToken(buf, JsonStrStt);

  if (sVal != NULL) {
    // dispose transferred meaning byte
    char* lastPos = sVal;
    char* curPos = sVal;

S
TD-1311  
Shengliang Guan 已提交
224
    for (int32_t i = 0; i < maxLen; ++i) {
H
hzcheng 已提交
225 226 227 228 229
      if (*curPos == 0) {
        break;
      }

      if (*curPos == '\"') {
S
TD-1311  
Shengliang Guan 已提交
230
        httpJsonPrint(buf, lastPos, (int32_t)(curPos - lastPos));
H
hzcheng 已提交
231 232 233 234
        curPos++;
        lastPos = curPos;
        httpJsonPrint(buf, "\\\"", 2);
      } else if (*curPos == '\\') {
S
TD-1311  
Shengliang Guan 已提交
235
        httpJsonPrint(buf, lastPos, (int32_t)(curPos - lastPos));
H
hzcheng 已提交
236 237 238 239 240 241 242 243 244
        curPos++;
        lastPos = curPos;
        httpJsonPrint(buf, "\\\\", 2);
      } else {
        curPos++;
      }
    }

    if (*lastPos) {
S
TD-1311  
Shengliang Guan 已提交
245
      httpJsonPrint(buf, lastPos, (int32_t)(curPos - lastPos));
H
hzcheng 已提交
246 247 248 249 250 251 252 253 254
    }
  }

  httpJsonToken(buf, JsonStrEnd);
}

void httpJsonInt64(JsonBuf* buf, int64_t num) {
  httpJsonItemToken(buf);
  httpJsonTestBuf(buf, MAX_NUM_STR_SZ);
L
lihui 已提交
255
  buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%" PRId64, num);
H
hzcheng 已提交
256 257
}

S
slguan 已提交
258 259 260
void httpJsonTimestamp(JsonBuf* buf, int64_t t, bool us) {
  char ts[35] = {0};
  struct tm *ptm;
S
TD-1311  
Shengliang Guan 已提交
261
  int32_t precision = 1000;
S
slguan 已提交
262 263 264
  if (us) {
    precision = 1000000;
  }
H
hzcheng 已提交
265

S
slguan 已提交
266
  time_t tt = t / precision;
H
hzcheng 已提交
267
  ptm = localtime(&tt);
S
TD-1311  
Shengliang Guan 已提交
268
  int32_t length = (int32_t) strftime(ts, 35, "%Y-%m-%d %H:%M:%S", ptm);
S
slguan 已提交
269 270 271 272 273
  if (us) {
    length += snprintf(ts + length, 8, ".%06ld", t % precision);
  } else {
    length += snprintf(ts + length, 5, ".%03ld", t % precision);
  }
H
hzcheng 已提交
274

S
slguan 已提交
275
  httpJsonString(buf, ts, length);
S
slguan 已提交
276 277
}

S
slguan 已提交
278 279
void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, bool us) {
  char ts[40] = {0};
S
slguan 已提交
280
  struct tm *ptm;
S
TD-1311  
Shengliang Guan 已提交
281
  int32_t precision = 1000;
S
slguan 已提交
282 283 284 285 286
  if (us) {
    precision = 1000000;
  }

  time_t tt = t / precision;
S
slguan 已提交
287
  ptm = localtime(&tt);
S
TD-1311  
Shengliang Guan 已提交
288
  int32_t length = (int32_t)strftime(ts, 40, "%Y-%m-%dT%H:%M:%S", ptm);
S
slguan 已提交
289 290 291 292 293
  if (us) {
    length += snprintf(ts + length, 8, ".%06ld", t % precision);
  } else {
    length += snprintf(ts + length, 5, ".%03ld", t % precision);
  }
S
TD-1311  
Shengliang Guan 已提交
294
  length += (int32_t)strftime(ts + length, 40 - length, "%z", ptm);
S
slguan 已提交
295

S
slguan 已提交
296
  httpJsonString(buf, ts, length);
H
hzcheng 已提交
297 298
}

S
TD-1311  
Shengliang Guan 已提交
299
void httpJsonInt(JsonBuf* buf, int32_t num) {
H
hzcheng 已提交
300 301 302 303 304 305 306 307
  httpJsonItemToken(buf);
  httpJsonTestBuf(buf, MAX_NUM_STR_SZ);
  buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%d", num);
}

void httpJsonFloat(JsonBuf* buf, float num) {
  httpJsonItemToken(buf);
  httpJsonTestBuf(buf, MAX_NUM_STR_SZ);
S
slguan 已提交
308 309 310
  if (isinf(num) || isnan(num)) {
    buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "null");
  } else if (num > 1E10 || num < -1E10) {
H
hzcheng 已提交
311 312 313 314 315 316 317 318 319
    buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%.5e", num);
  } else {
    buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%.5f", num);
  }
}

void httpJsonDouble(JsonBuf* buf, double num) {
  httpJsonItemToken(buf);
  httpJsonTestBuf(buf, MAX_NUM_STR_SZ);
S
slguan 已提交
320 321 322
  if (isinf(num) || isnan(num)) {
    buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "null");
  } else if (num > 1E10 || num < -1E10) {
H
hzcheng 已提交
323 324 325 326 327 328 329 330
    buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%.9e", num);
  } else {
    buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%.9f", num);
  }
}

void httpJsonNull(JsonBuf* buf) { httpJsonString(buf, "null", 4); }

S
TD-1311  
Shengliang Guan 已提交
331
void httpJsonBool(JsonBuf* buf, int32_t val) {
H
hzcheng 已提交
332 333 334 335 336 337
  if (val == 0)
    httpJsonPrint(buf, JsonFalseTkn, sizeof(JsonFalseTkn));
  else
    httpJsonPrint(buf, JsonTrueTkn, sizeof(JsonTrueTkn));
}

S
TD-1311  
Shengliang Guan 已提交
338
void httpJsonPairHead(JsonBuf* buf, char* name, int32_t len) {
H
hzcheng 已提交
339 340 341 342 343
  httpJsonItemToken(buf);
  httpJsonString(buf, name, len);
  httpJsonToken(buf, JsonPairTkn);
}

S
TD-1311  
Shengliang Guan 已提交
344
void httpJsonPair(JsonBuf* buf, char* name, int32_t nameLen, char* sVal, int32_t valLen) {
H
hzcheng 已提交
345 346 347 348
  httpJsonPairHead(buf, name, nameLen);
  httpJsonString(buf, sVal, valLen);
}

S
TD-1311  
Shengliang Guan 已提交
349
void httpJsonPairOriginString(JsonBuf* buf, char* name, int32_t nameLen, char* sVal, int32_t valLen) {
H
hzcheng 已提交
350 351 352 353
  httpJsonPairHead(buf, name, nameLen);
  httpJsonOriginString(buf, sVal, valLen);
}

S
TD-1311  
Shengliang Guan 已提交
354
void httpJsonPairIntVal(JsonBuf* buf, char* name, int32_t nNameLen, int32_t num) {
H
hzcheng 已提交
355 356 357 358
  httpJsonPairHead(buf, name, nNameLen);
  httpJsonInt(buf, num);
}

S
TD-1311  
Shengliang Guan 已提交
359
void httpJsonPairInt64Val(JsonBuf* buf, char* name, int32_t nNameLen, int64_t num) {
H
hzcheng 已提交
360 361 362 363
  httpJsonPairHead(buf, name, nNameLen);
  httpJsonInt64(buf, num);
}

S
TD-1311  
Shengliang Guan 已提交
364
void httpJsonPairBoolVal(JsonBuf* buf, char* name, int32_t nNameLen, int32_t num) {
H
hzcheng 已提交
365 366 367 368
  httpJsonPairHead(buf, name, nNameLen);
  httpJsonBool(buf, num);
}

S
TD-1311  
Shengliang Guan 已提交
369
void httpJsonPairFloatVal(JsonBuf* buf, char* name, int32_t nNameLen, float num) {
H
hzcheng 已提交
370 371 372 373
  httpJsonPairHead(buf, name, nNameLen);
  httpJsonFloat(buf, num);
}

S
TD-1311  
Shengliang Guan 已提交
374
void httpJsonPairDoubleVal(JsonBuf* buf, char* name, int32_t nNameLen, double num) {
H
hzcheng 已提交
375 376 377 378
  httpJsonPairHead(buf, name, nNameLen);
  httpJsonDouble(buf, num);
}

S
TD-1311  
Shengliang Guan 已提交
379
void httpJsonPairNullVal(JsonBuf* buf, char* name, int32_t nNameLen) {
H
hzcheng 已提交
380 381 382 383
  httpJsonPairHead(buf, name, nNameLen);
  httpJsonNull(buf);
}

S
TD-1311  
Shengliang Guan 已提交
384
void httpJsonPairArray(JsonBuf* buf, char* name, int32_t len, httpJsonBuilder fnBuilder, void* dsHandle) {
H
hzcheng 已提交
385 386 387 388
  httpJsonPairHead(buf, name, len);
  httpJsonArray(buf, fnBuilder, dsHandle);
}

S
TD-1311  
Shengliang Guan 已提交
389
void httpJsonPairObject(JsonBuf* buf, char* name, int32_t len, httpJsonBuilder fnBuilder, void* dsHandle) {
H
hzcheng 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  httpJsonPairHead(buf, name, len);
  httpJsonObject(buf, fnBuilder, dsHandle);
}

void httpJsonObject(JsonBuf* buf, httpJsonBuilder fnBuilder, void* dsHandle) {
  httpJsonItemToken(buf);
  httpJsonToken(buf, JsonObjStt);
  (*fnBuilder)(buf, dsHandle);
  httpJsonToken(buf, JsonObjEnd);
}

void httpJsonArray(JsonBuf* buf, httpJsonBuilder fnBuilder, void* jsonHandle) {
  httpJsonItemToken(buf);
  httpJsonToken(buf, JsonArrStt);
  (*fnBuilder)(buf, jsonHandle);
  httpJsonToken(buf, JsonArrEnd);
}

S
TD-1311  
Shengliang Guan 已提交
408
void httpJsonTestBuf(JsonBuf* buf, int32_t safety) {
H
hzcheng 已提交
409 410
  if ((buf->lst - buf->buf + safety) < buf->size) return;
  // buf->slot = *buf->lst;
S
slguan 已提交
411
  httpWriteJsonBufBody(buf, false);
H
hzcheng 已提交
412 413 414 415 416 417 418
}

void httpJsonToken(JsonBuf* buf, char c) {
  httpJsonTestBuf(buf, MAX_NUM_STR_SZ);  // maybe object stack
  *buf->lst++ = c;
}

S
TD-1311  
Shengliang Guan 已提交
419
void httpJsonPrint(JsonBuf* buf, const char* json, int32_t len) {
H
hzcheng 已提交
420 421 422 423 424
  if (len == 0 || len >= JSON_BUFFER_SIZE) {
    return;
  }

  if (len > buf->size) {
S
slguan 已提交
425
    httpWriteJsonBufBody(buf, false);
H
hzcheng 已提交
426 427 428 429 430 431 432 433 434
    httpJsonPrint(buf, json, len);
    // buf->slot = json[len - 1];
    return;
  }
  httpJsonTestBuf(buf, len + 2);
  memcpy(buf->lst, json, (size_t)len);
  buf->lst += len;
}

S
TD-1311  
Shengliang Guan 已提交
435
void httpJsonPairStatus(JsonBuf* buf, int32_t code) {
H
hzcheng 已提交
436 437 438 439 440
  if (code == 0) {
    httpJsonPair(buf, "status", 6, "succ", 4);
  } else {
    httpJsonPair(buf, "status", 6, "error", 5);
    httpJsonItemToken(buf);
S
Shengliang Guan 已提交
441
    httpJsonPairIntVal(buf, "code", 4, code & 0XFFFF);
S
Shengliang Guan 已提交
442 443 444 445 446 447
    httpJsonItemToken(buf);
    if (code == TSDB_CODE_MND_DB_NOT_SELECTED) {
      httpJsonPair(buf, "desc", 4, "failed to create database", 23);
    } else if (code == TSDB_CODE_MND_INVALID_TABLE_NAME) {
      httpJsonPair(buf, "desc", 4, "failed to create table", 22);
    } else {
S
TD-1311  
Shengliang Guan 已提交
448
      httpJsonPair(buf, "desc", 4, (char*)tstrerror(code), (int32_t)strlen(tstrerror(code)));
H
hzcheng 已提交
449 450
    }
  }
451
}