TD-1207
This commit is contained in:
parent
2079533b50
commit
fa7e9bd6d2
|
@ -26,7 +26,7 @@
|
|||
int32_t httpParseBasicAuthToken(HttpContext *pContext, char *token, int32_t len) {
|
||||
token[len] = '\0';
|
||||
int32_t outlen = 0;
|
||||
char *base64 = (char *)base64_decode(token, len, &outlen);
|
||||
char * base64 = (char *)base64_decode(token, len, &outlen);
|
||||
if (base64 == NULL || outlen == 0) {
|
||||
httpError("context:%p, fd:%d, basic token:%s parsed error", pContext, pContext->fd, token);
|
||||
free(base64);
|
||||
|
@ -49,7 +49,7 @@ int32_t httpParseBasicAuthToken(HttpContext *pContext, char *token, int32_t len)
|
|||
strncpy(pContext->user, base64, (size_t)user_len);
|
||||
pContext->user[user_len] = 0;
|
||||
|
||||
char *password = user + 1;
|
||||
char * password = user + 1;
|
||||
int32_t pass_len = (int32_t)((base64 + outlen) - password);
|
||||
if (pass_len < 1 || pass_len >= HTTP_PASSWORD_LEN) {
|
||||
httpError("context:%p, fd:%d, basic token:%s parse password error", pContext, pContext->fd, token);
|
||||
|
|
|
@ -100,9 +100,7 @@ const char *httpContextStateStr(HttpContextState state) {
|
|||
}
|
||||
}
|
||||
|
||||
void httpNotifyContextClose(HttpContext *pContext) {
|
||||
shutdown(pContext->fd, SHUT_WR);
|
||||
}
|
||||
void httpNotifyContextClose(HttpContext *pContext) { shutdown(pContext->fd, SHUT_WR); }
|
||||
|
||||
bool httpAlterContextState(HttpContext *pContext, HttpContextState srcState, HttpContextState destState) {
|
||||
return (atomic_val_compare_exchange_32(&pContext->state, srcState, destState) == srcState);
|
||||
|
@ -124,7 +122,7 @@ HttpContext *httpCreateContext(int32_t fd) {
|
|||
httpDebug("context:%p, fd:%d, is created, data:%p", pContext, fd, ppContext);
|
||||
|
||||
// set the ref to 0
|
||||
taosCacheRelease(tsHttpServer.contextCache, (void**)&ppContext, false);
|
||||
taosCacheRelease(tsHttpServer.contextCache, (void **)&ppContext, false);
|
||||
|
||||
return pContext;
|
||||
}
|
||||
|
@ -174,7 +172,6 @@ bool httpInitContext(HttpContext *pContext) {
|
|||
pContext->encodeMethod = NULL;
|
||||
memset(&pContext->singleCmd, 0, sizeof(HttpSqlCmd));
|
||||
|
||||
|
||||
httpTrace("context:%p, fd:%d, parsed:%d", pContext, pContext->fd, pContext->parsed);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -125,14 +125,14 @@ bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
|
|||
cmd->numOfRows--;
|
||||
continue;
|
||||
}
|
||||
int32_t* length = taos_fetch_lengths(result);
|
||||
int32_t *length = taos_fetch_lengths(result);
|
||||
|
||||
// for group by
|
||||
if (groupFields != -1) {
|
||||
char target[HTTP_GC_TARGET_SIZE] = {0};
|
||||
int32_t len;
|
||||
len = snprintf(target,HTTP_GC_TARGET_SIZE,"%s{",aliasBuffer);
|
||||
for (int32_t i = dataFields + 1; i<num_fields; i++){
|
||||
len = snprintf(target, HTTP_GC_TARGET_SIZE, "%s{", aliasBuffer);
|
||||
for (int32_t i = dataFields + 1; i < num_fields; i++) {
|
||||
switch (fields[i].type) {
|
||||
case TSDB_DATA_TYPE_BOOL:
|
||||
case TSDB_DATA_TYPE_TINYINT:
|
||||
|
@ -155,9 +155,9 @@ bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
|
|||
break;
|
||||
case TSDB_DATA_TYPE_BINARY:
|
||||
case TSDB_DATA_TYPE_NCHAR:
|
||||
if (row[i]!= NULL){
|
||||
if (row[i] != NULL) {
|
||||
len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "%s:", fields[i].name);
|
||||
memcpy(target + len, (char *) row[i], length[i]);
|
||||
memcpy(target + len, (char *)row[i], length[i]);
|
||||
len = (int32_t)strlen(target);
|
||||
}
|
||||
break;
|
||||
|
@ -165,10 +165,9 @@ bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
|
|||
len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "%s:%s", fields[i].name, "-");
|
||||
break;
|
||||
}
|
||||
if(i < num_fields - 1 ){
|
||||
if (i < num_fields - 1) {
|
||||
len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, ", ");
|
||||
}
|
||||
|
||||
}
|
||||
len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "}");
|
||||
|
||||
|
@ -217,10 +216,10 @@ bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
|
|||
break;
|
||||
case TSDB_DATA_TYPE_BINARY:
|
||||
case TSDB_DATA_TYPE_NCHAR:
|
||||
httpJsonStringForTransMean(jsonBuf, (char*)row[i], fields[i].bytes);
|
||||
httpJsonStringForTransMean(jsonBuf, (char *)row[i], fields[i].bytes);
|
||||
break;
|
||||
case TSDB_DATA_TYPE_TIMESTAMP:
|
||||
if (precision == TSDB_TIME_PRECISION_MILLI) { //ms
|
||||
if (precision == TSDB_TIME_PRECISION_MILLI) { // ms
|
||||
httpJsonInt64(jsonBuf, *((int64_t *)row[i]));
|
||||
} else {
|
||||
httpJsonInt64(jsonBuf, *((int64_t *)row[i]) / 1000);
|
||||
|
|
|
@ -27,23 +27,22 @@ typedef enum {
|
|||
struct ehttp_gzip_s {
|
||||
ehttp_gzip_conf_t conf;
|
||||
ehttp_gzip_callbacks_t callbacks;
|
||||
void *arg;
|
||||
z_stream *gzip;
|
||||
gz_header *header;
|
||||
char *chunk;
|
||||
|
||||
void * arg;
|
||||
z_stream * gzip;
|
||||
gz_header * header;
|
||||
char * chunk;
|
||||
int32_t state;
|
||||
};
|
||||
|
||||
static void dummy_on_data(ehttp_gzip_t *gzip, void *arg, const char *buf, int32_t len) {
|
||||
}
|
||||
static void dummy_on_data(ehttp_gzip_t *gzip, void *arg, const char *buf, int32_t len) {}
|
||||
|
||||
static void ehttp_gzip_cleanup(ehttp_gzip_t *gzip) {
|
||||
switch(gzip->state) {
|
||||
switch (gzip->state) {
|
||||
case EHTTP_GZIP_READY: {
|
||||
inflateEnd(gzip->gzip);
|
||||
} break;
|
||||
default: break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (gzip->gzip) {
|
||||
free(gzip->gzip);
|
||||
|
@ -60,8 +59,8 @@ static void ehttp_gzip_cleanup(ehttp_gzip_t *gzip) {
|
|||
gzip->state = EHTTP_GZIP_CLOSED;
|
||||
}
|
||||
|
||||
ehttp_gzip_t* ehttp_gzip_create_decompressor(ehttp_gzip_conf_t conf, ehttp_gzip_callbacks_t callbacks, void *arg) {
|
||||
ehttp_gzip_t *gzip = (ehttp_gzip_t*)calloc(1, sizeof(*gzip));
|
||||
ehttp_gzip_t *ehttp_gzip_create_decompressor(ehttp_gzip_conf_t conf, ehttp_gzip_callbacks_t callbacks, void *arg) {
|
||||
ehttp_gzip_t *gzip = (ehttp_gzip_t *)calloc(1, sizeof(*gzip));
|
||||
if (!gzip) return NULL;
|
||||
|
||||
do {
|
||||
|
@ -69,12 +68,12 @@ ehttp_gzip_t* ehttp_gzip_create_decompressor(ehttp_gzip_conf_t conf, ehttp_gzip_
|
|||
gzip->callbacks = callbacks;
|
||||
gzip->arg = arg;
|
||||
if (gzip->callbacks.on_data == NULL) gzip->callbacks.on_data = dummy_on_data;
|
||||
gzip->gzip = (z_stream*)calloc(1, sizeof(*gzip->gzip));
|
||||
gzip->gzip = (z_stream *)calloc(1, sizeof(*gzip->gzip));
|
||||
if (gzip->conf.get_header) {
|
||||
gzip->header = (gz_header*)calloc(1, sizeof(*gzip->header));
|
||||
gzip->header = (gz_header *)calloc(1, sizeof(*gzip->header));
|
||||
}
|
||||
if (gzip->conf.chunk_size<=0) gzip->conf.chunk_size = EHTTP_GZIP_CHUNK_SIZE_DEFAULT;
|
||||
gzip->chunk = (char*)malloc(gzip->conf.chunk_size);
|
||||
if (gzip->conf.chunk_size <= 0) gzip->conf.chunk_size = EHTTP_GZIP_CHUNK_SIZE_DEFAULT;
|
||||
gzip->chunk = (char *)malloc(gzip->conf.chunk_size);
|
||||
if (!gzip->gzip || (gzip->conf.get_header && !gzip->header) || !gzip->chunk) break;
|
||||
gzip->gzip->zalloc = Z_NULL;
|
||||
gzip->gzip->zfree = Z_NULL;
|
||||
|
@ -95,7 +94,7 @@ ehttp_gzip_t* ehttp_gzip_create_decompressor(ehttp_gzip_conf_t conf, ehttp_gzip_
|
|||
}
|
||||
if (ret != Z_OK) break;
|
||||
|
||||
gzip->gzip->next_out = (z_const Bytef*)gzip->chunk;
|
||||
gzip->gzip->next_out = (z_const Bytef *)gzip->chunk;
|
||||
gzip->gzip->avail_out = gzip->conf.chunk_size;
|
||||
gzip->state = EHTTP_GZIP_READY;
|
||||
return gzip;
|
||||
|
@ -105,7 +104,7 @@ ehttp_gzip_t* ehttp_gzip_create_decompressor(ehttp_gzip_conf_t conf, ehttp_gzip_
|
|||
return NULL;
|
||||
}
|
||||
|
||||
ehttp_gzip_t* ehttp_gzip_create_compressor(ehttp_gzip_conf_t conf, ehttp_gzip_callbacks_t callbacks, void *arg);
|
||||
ehttp_gzip_t *ehttp_gzip_create_compressor(ehttp_gzip_conf_t conf, ehttp_gzip_callbacks_t callbacks, void *arg);
|
||||
|
||||
void ehttp_gzip_destroy(ehttp_gzip_t *gzip) {
|
||||
ehttp_gzip_cleanup(gzip);
|
||||
|
@ -129,15 +128,15 @@ int32_t ehttp_gzip_write(ehttp_gzip_t *gzip, const char *buf, int32_t len) {
|
|||
}
|
||||
if (ret != Z_OK && ret != Z_STREAM_END) return -1;
|
||||
|
||||
if (gzip->gzip->avail_out>0) {
|
||||
if (ret!=Z_STREAM_END) continue;
|
||||
if (gzip->gzip->avail_out > 0) {
|
||||
if (ret != Z_STREAM_END) continue;
|
||||
}
|
||||
|
||||
int32_t len = (int32_t)(gzip->gzip->next_out - (z_const Bytef*)gzip->chunk);
|
||||
int32_t len = (int32_t)(gzip->gzip->next_out - (z_const Bytef *)gzip->chunk);
|
||||
|
||||
gzip->gzip->next_out[0] = '\0';
|
||||
gzip->callbacks.on_data(gzip, gzip->arg, gzip->chunk, len);
|
||||
gzip->gzip->next_out = (z_const Bytef*)gzip->chunk;
|
||||
gzip->gzip->next_out = (z_const Bytef *)gzip->chunk;
|
||||
gzip->gzip->avail_out = gzip->conf.chunk_size;
|
||||
}
|
||||
|
||||
|
@ -155,7 +154,7 @@ int32_t ehttp_gzip_finish(ehttp_gzip_t *gzip) {
|
|||
|
||||
if (ret != Z_STREAM_END) return -1;
|
||||
|
||||
int32_t len = (int32_t)(gzip->gzip->next_out - (z_const Bytef*)gzip->chunk);
|
||||
int32_t len = (int32_t)(gzip->gzip->next_out - (z_const Bytef *)gzip->chunk);
|
||||
|
||||
gzip->gzip->next_out[0] = '\0';
|
||||
gzip->callbacks.on_data(gzip, gzip->arg, gzip->chunk, len);
|
||||
|
@ -164,4 +163,3 @@ int32_t ehttp_gzip_finish(ehttp_gzip_t *gzip) {
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ bool httpProcessData(HttpContext* pContext) {
|
|||
/*
|
||||
* httpCloseContextByApp has been called when parsing the error
|
||||
*/
|
||||
//httpCloseContextByApp(pContext);
|
||||
// httpCloseContextByApp(pContext);
|
||||
} else {
|
||||
httpProcessRequest(pContext);
|
||||
}
|
||||
|
|
|
@ -44,20 +44,21 @@ int32_t httpWriteBufByFd(struct HttpContext* pContext, const char* buf, int32_t
|
|||
int32_t writeLen = 0;
|
||||
|
||||
do {
|
||||
if (pContext->fd > 2){
|
||||
if (pContext->fd > 2) {
|
||||
len = (int32_t)taosSend(pContext->fd, buf + writeLen, (size_t)(sz - writeLen), MSG_NOSIGNAL);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return sz;
|
||||
}
|
||||
|
||||
if (len < 0) {
|
||||
httpDebug("context:%p, fd:%d, socket write errno:%d:%s, times:%d", pContext, pContext->fd, errno, strerror(errno), countWait);
|
||||
httpDebug("context:%p, fd:%d, socket write errno:%d:%s, times:%d", pContext, pContext->fd, errno, strerror(errno),
|
||||
countWait);
|
||||
if (++countWait > HTTP_WRITE_RETRY_TIMES) break;
|
||||
taosMsleep(HTTP_WRITE_WAIT_TIME_MS);
|
||||
continue;
|
||||
} else if (len == 0) {
|
||||
httpDebug("context:%p, fd:%d, socket write errno:%d:%s, connect already closed", pContext, pContext->fd, errno, strerror(errno));
|
||||
httpDebug("context:%p, fd:%d, socket write errno:%d:%s, connect already closed", pContext, pContext->fd, errno,
|
||||
strerror(errno));
|
||||
break;
|
||||
} else {
|
||||
countWait = 0;
|
||||
|
@ -80,7 +81,7 @@ int32_t httpWriteBuf(struct HttpContext* pContext, const char* buf, int32_t sz)
|
|||
return writeSz;
|
||||
}
|
||||
|
||||
int32_t httpWriteBufNoTrace(struct HttpContext *pContext, const char *buf, int32_t sz) {
|
||||
int32_t httpWriteBufNoTrace(struct HttpContext* pContext, const char* buf, int32_t sz) {
|
||||
int32_t writeSz = httpWriteBufByFd(pContext, buf, sz);
|
||||
if (writeSz != sz) {
|
||||
httpError("context:%p, fd:%d, dataSize:%d, writeSize:%d, failed to send response", pContext, pContext->fd, sz,
|
||||
|
@ -93,7 +94,7 @@ int32_t httpWriteBufNoTrace(struct HttpContext *pContext, const char *buf, int32
|
|||
int32_t httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) {
|
||||
int32_t remain = 0;
|
||||
char sLen[24];
|
||||
int32_t srcLen = (int32_t) (buf->lst - buf->buf);
|
||||
int32_t srcLen = (int32_t)(buf->lst - buf->buf);
|
||||
|
||||
if (buf->pContext->fd <= 0) {
|
||||
httpTrace("context:%p, fd:%d, write json body error", buf->pContext, buf->pContext->fd);
|
||||
|
@ -113,9 +114,9 @@ int32_t httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) {
|
|||
httpTrace("context:%p, fd:%d, no data need dump", buf->pContext, buf->pContext->fd);
|
||||
return 0; // there is no data to dump.
|
||||
} else {
|
||||
int32_t len = sprintf(sLen, "%d\r\n", srcLen);
|
||||
httpTrace("context:%p, fd:%d, write body, chunkSize:%d, response:\n%s", buf->pContext, buf->pContext->fd,
|
||||
srcLen, buf->buf);
|
||||
int32_t len = sprintf(sLen, "%x\r\n", srcLen);
|
||||
httpTrace("context:%p, fd:%d, write body, chunkSize:%d, response:\n%s", buf->pContext, buf->pContext->fd, srcLen,
|
||||
buf->buf);
|
||||
httpWriteBufNoTrace(buf->pContext, sLen, len);
|
||||
remain = httpWriteBufNoTrace(buf->pContext, buf->buf, srcLen);
|
||||
}
|
||||
|
@ -126,8 +127,8 @@ int32_t httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) {
|
|||
if (ret == 0) {
|
||||
if (compressBufLen > 0) {
|
||||
int32_t len = sprintf(sLen, "%x\r\n", compressBufLen);
|
||||
httpTrace("context:%p, fd:%d, write body, chunkSize:%d, compressSize:%d, last:%d, response:\n%s",
|
||||
buf->pContext, buf->pContext->fd, srcLen, compressBufLen, isTheLast, buf->buf);
|
||||
httpTrace("context:%p, fd:%d, write body, chunkSize:%d, compressSize:%d, last:%d, response:\n%s", buf->pContext,
|
||||
buf->pContext->fd, srcLen, compressBufLen, isTheLast, buf->buf);
|
||||
httpWriteBufNoTrace(buf->pContext, sLen, len);
|
||||
remain = httpWriteBufNoTrace(buf->pContext, (const char*)compressBuf, compressBufLen);
|
||||
} else {
|
||||
|
@ -257,7 +258,7 @@ void httpJsonInt64(JsonBuf* buf, int64_t num) {
|
|||
|
||||
void httpJsonTimestamp(JsonBuf* buf, int64_t t, bool us) {
|
||||
char ts[35] = {0};
|
||||
struct tm *ptm;
|
||||
struct tm* ptm;
|
||||
int32_t precision = 1000;
|
||||
if (us) {
|
||||
precision = 1000000;
|
||||
|
@ -265,7 +266,7 @@ void httpJsonTimestamp(JsonBuf* buf, int64_t t, bool us) {
|
|||
|
||||
time_t tt = t / precision;
|
||||
ptm = localtime(&tt);
|
||||
int32_t length = (int32_t) strftime(ts, 35, "%Y-%m-%d %H:%M:%S", ptm);
|
||||
int32_t length = (int32_t)strftime(ts, 35, "%Y-%m-%d %H:%M:%S", ptm);
|
||||
if (us) {
|
||||
length += snprintf(ts + length, 8, ".%06" PRId64, t % precision);
|
||||
} else {
|
||||
|
@ -277,7 +278,7 @@ void httpJsonTimestamp(JsonBuf* buf, int64_t t, bool us) {
|
|||
|
||||
void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, bool us) {
|
||||
char ts[40] = {0};
|
||||
struct tm *ptm;
|
||||
struct tm* ptm;
|
||||
int32_t precision = 1000;
|
||||
if (us) {
|
||||
precision = 1000000;
|
||||
|
|
|
@ -237,7 +237,6 @@ static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const
|
|||
}
|
||||
httpTrace("context:%p, fd:%d, keepAlive:%d", pContext, pContext->fd, pContext->parser->keepAlive);
|
||||
}
|
||||
|
||||
#if 0
|
||||
else if (0 == strcasecmp(key, "Content-Encoding")) {
|
||||
if (0 == strcmp(val, "gzip")) {
|
||||
|
@ -246,7 +245,7 @@ static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
else if (0 == strcasecmp(key, "Transfer-Encoding") || 0 == strcasecmp(key, "Content-Encoding")) {
|
||||
if (strstr(val, "gzip")) {
|
||||
|
@ -410,9 +409,7 @@ static int32_t httpPopStack(HttpParser *parser) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static void httpClearStack(HttpStack *stack) {
|
||||
stack->pos = 0;
|
||||
}
|
||||
static void httpClearStack(HttpStack *stack) { stack->pos = 0; }
|
||||
|
||||
static int32_t httpCleanupStack(HttpStack *stack) {
|
||||
free(stack->stacks);
|
||||
|
@ -522,16 +519,27 @@ char *httpDecodeUrl(const char *enc) {
|
|||
char *p = strchr(enc, '%');
|
||||
if (!p) break;
|
||||
int32_t hex, cnt;
|
||||
int32_t n = sscanf(p+1, "%2x%n", &hex, &cnt);
|
||||
if (n!=1 && cnt !=2) { ok = 0; break; }
|
||||
if (httpAppendString(&str, enc, (int32_t)(p-enc))) { ok = 0; break; }
|
||||
int32_t n = sscanf(p + 1, "%2x%n", &hex, &cnt);
|
||||
if (n != 1 && cnt != 2) {
|
||||
ok = 0;
|
||||
break;
|
||||
}
|
||||
if (httpAppendString(&str, enc, (int32_t)(p - enc))) {
|
||||
ok = 0;
|
||||
break;
|
||||
}
|
||||
char c = (char)hex;
|
||||
if (httpAppendString(&str, &c, 1)) { ok = 0; break; }
|
||||
enc = p+3;
|
||||
if (httpAppendString(&str, &c, 1)) {
|
||||
ok = 0;
|
||||
break;
|
||||
}
|
||||
enc = p + 3;
|
||||
}
|
||||
char *dec = NULL;
|
||||
if (ok && *enc) {
|
||||
if (httpAppendString(&str, enc, (int32_t)strlen(enc))) { ok = 0; }
|
||||
if (httpAppendString(&str, enc, (int32_t)strlen(enc))) {
|
||||
ok = 0;
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
dec = str.str;
|
||||
|
@ -542,7 +550,7 @@ char *httpDecodeUrl(const char *enc) {
|
|||
}
|
||||
|
||||
static void httpOnData(ehttp_gzip_t *gzip, void *arg, const char *buf, int32_t len) {
|
||||
HttpParser *parser = (HttpParser*)arg;
|
||||
HttpParser *parser = (HttpParser *)arg;
|
||||
httpOnBody(parser, buf, len);
|
||||
}
|
||||
|
||||
|
@ -1029,7 +1037,7 @@ static int32_t httpParserOnEnd(HttpParser *parser, HTTP_PARSER_STATE state, cons
|
|||
}
|
||||
|
||||
static int32_t httpParseChar(HttpParser *parser, const char c, int32_t *again) {
|
||||
HttpContext *pContext = parser->pContext;
|
||||
HttpContext * pContext = parser->pContext;
|
||||
int32_t ok = 0;
|
||||
HTTP_PARSER_STATE state = httpTopStack(parser);
|
||||
do {
|
||||
|
@ -1119,7 +1127,7 @@ static int32_t httpParseChar(HttpParser *parser, const char c, int32_t *again) {
|
|||
|
||||
int32_t httpParseBuf(HttpParser *parser, const char *buf, int32_t len) {
|
||||
HttpContext *pContext = parser->pContext;
|
||||
const char *p = buf;
|
||||
const char * p = buf;
|
||||
int32_t ret = 0;
|
||||
int32_t i = 0;
|
||||
|
||||
|
|
|
@ -161,7 +161,8 @@ void httpSendTaosdInvalidSqlErrorResp(HttpContext *pContext, char *errMsg) {
|
|||
temp[i] = '\'';
|
||||
} else if (temp[i] == '\n') {
|
||||
temp[i] = ' ';
|
||||
} else {}
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
httpSendErrorRespImp(pContext, httpCode, "Bad Request", TSDB_CODE_TSC_INVALID_SQL & 0XFFFF, temp);
|
||||
|
|
|
@ -95,7 +95,6 @@ bool restProcessSqlRequest(HttpContext* pContext, int32_t timestampFmt) {
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* for async test
|
||||
*
|
||||
|
|
|
@ -83,7 +83,8 @@ void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result)
|
|||
httpJsonToken(jsonBuf, JsonArrStt);
|
||||
}
|
||||
|
||||
bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows, int32_t timestampFormat) {
|
||||
bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows,
|
||||
int32_t timestampFormat) {
|
||||
JsonBuf *jsonBuf = httpMallocJsonBuf(pContext);
|
||||
if (jsonBuf == NULL) return false;
|
||||
|
||||
|
@ -95,7 +96,7 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
|
|||
if (row == NULL) {
|
||||
continue;
|
||||
}
|
||||
int32_t* length = taos_fetch_lengths(result);
|
||||
int32_t *length = taos_fetch_lengths(result);
|
||||
|
||||
// data row array begin
|
||||
httpJsonItemToken(jsonBuf);
|
||||
|
@ -131,15 +132,17 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
|
|||
break;
|
||||
case TSDB_DATA_TYPE_BINARY:
|
||||
case TSDB_DATA_TYPE_NCHAR:
|
||||
httpJsonStringForTransMean(jsonBuf, (char*)row[i], length[i]);
|
||||
httpJsonStringForTransMean(jsonBuf, (char *)row[i], length[i]);
|
||||
break;
|
||||
case TSDB_DATA_TYPE_TIMESTAMP:
|
||||
if (timestampFormat == REST_TIMESTAMP_FMT_LOCAL_STRING) {
|
||||
httpJsonTimestamp(jsonBuf, *((int64_t *)row[i]), taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO);
|
||||
httpJsonTimestamp(jsonBuf, *((int64_t *)row[i]),
|
||||
taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO);
|
||||
} else if (timestampFormat == REST_TIMESTAMP_FMT_TIMESTAMP) {
|
||||
httpJsonInt64(jsonBuf, *((int64_t *)row[i]));
|
||||
} else {
|
||||
httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i]), taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO);
|
||||
httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i]),
|
||||
taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
@ -149,7 +152,7 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
|
|||
|
||||
// data row array end
|
||||
httpJsonToken(jsonBuf, JsonArrEnd);
|
||||
cmd->numOfRows ++;
|
||||
cmd->numOfRows++;
|
||||
|
||||
if (pContext->fd <= 0) {
|
||||
httpError("context:%p, fd:%d, user:%s, conn closed, abort retrieve", pContext, pContext->fd, pContext->user);
|
||||
|
@ -168,15 +171,15 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
|
|||
}
|
||||
|
||||
bool restBuildSqlTimestampJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows) {
|
||||
return restBuildSqlJson(pContext,cmd, result, numOfRows, REST_TIMESTAMP_FMT_TIMESTAMP);
|
||||
return restBuildSqlJson(pContext, cmd, result, numOfRows, REST_TIMESTAMP_FMT_TIMESTAMP);
|
||||
}
|
||||
|
||||
bool restBuildSqlLocalTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows) {
|
||||
return restBuildSqlJson(pContext,cmd, result, numOfRows, REST_TIMESTAMP_FMT_LOCAL_STRING);
|
||||
return restBuildSqlJson(pContext, cmd, result, numOfRows, REST_TIMESTAMP_FMT_LOCAL_STRING);
|
||||
}
|
||||
|
||||
bool restBuildSqlUtcTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows) {
|
||||
return restBuildSqlJson(pContext,cmd, result, numOfRows, REST_TIMESTAMP_FMT_UTC_STRING);
|
||||
return restBuildSqlJson(pContext, cmd, result, numOfRows, REST_TIMESTAMP_FMT_UTC_STRING);
|
||||
}
|
||||
|
||||
void restStopSqlJson(HttpContext *pContext, HttpSqlCmd *cmd) {
|
||||
|
|
|
@ -27,19 +27,21 @@
|
|||
|
||||
static bool httpReadData(HttpContext *pContext);
|
||||
|
||||
static void httpStopThread(HttpThread* pThread) {
|
||||
static void httpStopThread(HttpThread *pThread) {
|
||||
pThread->stop = true;
|
||||
|
||||
// signal the thread to stop, try graceful method first,
|
||||
// and use pthread_cancel when failed
|
||||
struct epoll_event event = { .events = EPOLLIN };
|
||||
struct epoll_event event = {.events = EPOLLIN};
|
||||
eventfd_t fd = eventfd(1, 0);
|
||||
if (fd == -1) {
|
||||
httpError("%s, failed to create eventfd, will call pthread_cancel instead, which may result in data corruption: %s", pThread->label, strerror(errno));
|
||||
httpError("%s, failed to create eventfd, will call pthread_cancel instead, which may result in data corruption: %s",
|
||||
pThread->label, strerror(errno));
|
||||
pThread->stop = true;
|
||||
pthread_cancel(pThread->thread);
|
||||
} else if (epoll_ctl(pThread->pollFd, EPOLL_CTL_ADD, fd, &event) < 0) {
|
||||
httpError("%s, failed to call epoll_ctl, will call pthread_cancel instead, which may result in data corruption: %s", pThread->label, strerror(errno));
|
||||
httpError("%s, failed to call epoll_ctl, will call pthread_cancel instead, which may result in data corruption: %s",
|
||||
pThread->label, strerror(errno));
|
||||
pthread_cancel(pThread->thread);
|
||||
}
|
||||
|
||||
|
@ -61,7 +63,7 @@ void httpCleanUpConnect() {
|
|||
}
|
||||
|
||||
for (int32_t i = 0; i < pServer->numOfThreads; ++i) {
|
||||
HttpThread* pThread = pServer->pThreads + i;
|
||||
HttpThread *pThread = pServer->pThreads + i;
|
||||
if (pThread != NULL) {
|
||||
httpStopThread(pThread);
|
||||
}
|
||||
|
@ -71,8 +73,8 @@ void httpCleanUpConnect() {
|
|||
}
|
||||
|
||||
static void httpProcessHttpData(void *param) {
|
||||
HttpServer *pServer = &tsHttpServer;
|
||||
HttpThread *pThread = (HttpThread *)param;
|
||||
HttpServer * pServer = &tsHttpServer;
|
||||
HttpThread * pThread = (HttpThread *)param;
|
||||
HttpContext *pContext;
|
||||
int32_t fdNum;
|
||||
|
||||
|
@ -92,8 +94,8 @@ static void httpProcessHttpData(void *param) {
|
|||
pContext = httpGetContext(events[i].data.ptr);
|
||||
if (pContext == NULL) {
|
||||
httpError("context:%p, is already released, close connect", events[i].data.ptr);
|
||||
//epoll_ctl(pThread->pollFd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
|
||||
//taosClose(events[i].data.fd);
|
||||
// epoll_ctl(pThread->pollFd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
|
||||
// taosClose(events[i].data.fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -210,8 +212,8 @@ static void *httpAcceptHttpConnection(void *arg) {
|
|||
|
||||
pContext = httpCreateContext(connFd);
|
||||
if (pContext == NULL) {
|
||||
httpError("fd:%d, ip:%s:%u, no enough resource to allocate http context", connFd, taosInetNtoa(clientAddr.sin_addr),
|
||||
htons(clientAddr.sin_port));
|
||||
httpError("fd:%d, ip:%s:%u, no enough resource to allocate http context", connFd,
|
||||
taosInetNtoa(clientAddr.sin_addr), htons(clientAddr.sin_port));
|
||||
taosCloseSocket(connFd);
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -35,7 +35,8 @@ void httpCreateSession(HttpContext *pContext, void *taos) {
|
|||
session.refCount = 1;
|
||||
int32_t len = snprintf(session.id, HTTP_SESSION_ID_LEN, "%s.%s", pContext->user, pContext->pass);
|
||||
|
||||
pContext->session = taosCachePut(server->sessionCache, session.id, len, &session, sizeof(HttpSession), tsHttpSessionExpire * 1000);
|
||||
pContext->session =
|
||||
taosCachePut(server->sessionCache, session.id, len, &session, sizeof(HttpSession), tsHttpSessionExpire * 1000);
|
||||
// void *temp = pContext->session;
|
||||
// taosCacheRelease(server->sessionCache, (void **)&temp, false);
|
||||
|
||||
|
|
|
@ -82,7 +82,7 @@ void httpProcessMultiSqlCallBackImp(void *param, TAOS_RES *result, int32_t code,
|
|||
HttpContext *pContext = (HttpContext *)param;
|
||||
if (pContext == NULL) return;
|
||||
|
||||
HttpSqlCmds *multiCmds = pContext->multiCmds;
|
||||
HttpSqlCmds * multiCmds = pContext->multiCmds;
|
||||
HttpEncodeMethod *encode = pContext->encodeMethod;
|
||||
|
||||
HttpSqlCmd *singleCmd = multiCmds->cmds + multiCmds->pos;
|
||||
|
@ -269,8 +269,8 @@ void httpProcessSingleSqlCallBackImp(void *param, TAOS_RES *result, int32_t code
|
|||
pContext->user, tstrerror(code), pObj, taos_errstr(pObj));
|
||||
httpSendTaosdInvalidSqlErrorResp(pContext, taos_errstr(pObj));
|
||||
} else {
|
||||
httpError("context:%p, fd:%d, user:%s, query error, code:%s, sqlObj:%p", pContext, pContext->fd,
|
||||
pContext->user, tstrerror(code), pObj);
|
||||
httpError("context:%p, fd:%d, user:%s, query error, code:%s, sqlObj:%p", pContext, pContext->fd, pContext->user,
|
||||
tstrerror(code), pObj);
|
||||
httpSendErrorResp(pContext, code);
|
||||
}
|
||||
taos_free_result(result);
|
||||
|
|
|
@ -114,6 +114,4 @@ void httpCleanUpSystem() {
|
|||
tsHttpServer.status = HTTP_SERVER_CLOSED;
|
||||
}
|
||||
|
||||
int32_t httpGetReqCount() {
|
||||
return atomic_exchange_32(&tsHttpServer.requestNum, 0);
|
||||
}
|
||||
int32_t httpGetReqCount() { return atomic_exchange_32(&tsHttpServer.requestNum, 0); }
|
||||
|
|
|
@ -227,14 +227,14 @@ ParseEnd:
|
|||
}
|
||||
}
|
||||
|
||||
int32_t tgParseSchema(const char *content, char*fileName) {
|
||||
int32_t tgParseSchema(const char *content, char *fileName) {
|
||||
cJSON *root = cJSON_Parse(content);
|
||||
if (root == NULL) {
|
||||
httpError("failed to parse telegraf schema file:%s, invalid json format, content:%s", fileName, content);
|
||||
return -1;
|
||||
}
|
||||
int32_t size = 0;
|
||||
cJSON *metrics = cJSON_GetObjectItem(root, "metrics");
|
||||
cJSON * metrics = cJSON_GetObjectItem(root, "metrics");
|
||||
if (metrics != NULL) {
|
||||
size = cJSON_GetArraySize(metrics);
|
||||
if (size <= 0) {
|
||||
|
@ -296,7 +296,7 @@ int32_t tgReadSchema(char *fileName) {
|
|||
}
|
||||
|
||||
void tgInitHandle(HttpServer *pServer) {
|
||||
char fileName[TSDB_FILENAME_LEN*2] = {0};
|
||||
char fileName[TSDB_FILENAME_LEN * 2] = {0};
|
||||
sprintf(fileName, "%s/taos.telegraf.cfg", configDir);
|
||||
if (tgReadSchema(fileName) <= 0) {
|
||||
tgFreeSchemas();
|
||||
|
@ -308,9 +308,7 @@ void tgInitHandle(HttpServer *pServer) {
|
|||
httpAddMethod(pServer, &tgDecodeMethod);
|
||||
}
|
||||
|
||||
void tgCleanupHandle() {
|
||||
tgFreeSchemas();
|
||||
}
|
||||
void tgCleanupHandle() { tgFreeSchemas(); }
|
||||
|
||||
bool tgGetUserFromUrl(HttpContext *pContext) {
|
||||
HttpParser *pParser = pContext->parser;
|
||||
|
@ -578,7 +576,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
|
|||
table_cmd->cmdType = HTTP_CMD_TYPE_INSERT;
|
||||
|
||||
// order by tag name
|
||||
cJSON *orderedTags[TG_MAX_SORT_TAG_SIZE] = {0};
|
||||
cJSON * orderedTags[TG_MAX_SORT_TAG_SIZE] = {0};
|
||||
int32_t orderTagsLen = 0;
|
||||
for (int32_t i = 0; i < tagsSize; ++i) {
|
||||
cJSON *tag = cJSON_GetArrayItem(tags, i);
|
||||
|
@ -603,7 +601,8 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
|
|||
if (tsTelegrafUseFieldNum == 0) {
|
||||
table_cmd->stable = stable_cmd->stable = httpAddToSqlCmdBuffer(pContext, "%s", stname);
|
||||
} else {
|
||||
table_cmd->stable = stable_cmd->stable = httpAddToSqlCmdBuffer(pContext, "%s_%d_%d", stname, fieldsSize, orderTagsLen);
|
||||
table_cmd->stable = stable_cmd->stable =
|
||||
httpAddToSqlCmdBuffer(pContext, "%s_%d_%d", stname, fieldsSize, orderTagsLen);
|
||||
}
|
||||
table_cmd->stable = stable_cmd->stable =
|
||||
httpShrinkTableName(pContext, table_cmd->stable, httpGetCmdsString(pContext, table_cmd->stable));
|
||||
|
@ -627,9 +626,11 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
|
|||
|
||||
// table name
|
||||
if (tsTelegrafUseFieldNum == 0) {
|
||||
table_cmd->table = stable_cmd->table = httpAddToSqlCmdBufferNoTerminal(pContext, "%s_%s", stname, host->valuestring);
|
||||
table_cmd->table = stable_cmd->table =
|
||||
httpAddToSqlCmdBufferNoTerminal(pContext, "%s_%s", stname, host->valuestring);
|
||||
} else {
|
||||
table_cmd->table = stable_cmd->table = httpAddToSqlCmdBufferNoTerminal(pContext, "%s_%d_%d_%s", stname, fieldsSize, orderTagsLen, host->valuestring);
|
||||
table_cmd->table = stable_cmd->table =
|
||||
httpAddToSqlCmdBufferNoTerminal(pContext, "%s_%d_%d_%s", stname, fieldsSize, orderTagsLen, host->valuestring);
|
||||
}
|
||||
for (int32_t i = 0; i < orderTagsLen; ++i) {
|
||||
cJSON *tag = orderedTags[i];
|
||||
|
|
|
@ -160,8 +160,7 @@ bool httpMallocMultiCmds(HttpContext *pContext, int32_t cmdSize, int32_t bufferS
|
|||
free(multiCmds->cmds);
|
||||
multiCmds->cmds = (HttpSqlCmd *)malloc((size_t)cmdSize * sizeof(HttpSqlCmd));
|
||||
if (multiCmds->cmds == NULL) {
|
||||
httpError("context:%p, fd:%d, user:%s, malloc cmds:%d error", pContext, pContext->fd,
|
||||
pContext->user, cmdSize);
|
||||
httpError("context:%p, fd:%d, user:%s, malloc cmds:%d error", pContext, pContext->fd, pContext->user, cmdSize);
|
||||
return false;
|
||||
}
|
||||
multiCmds->maxSize = (int16_t)cmdSize;
|
||||
|
@ -358,30 +357,32 @@ int32_t httpGzipDeCompress(char *srcData, int32_t nSrcData, char *destData, int3
|
|||
(((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
|
||||
};
|
||||
|
||||
gzipStream.zalloc = (alloc_func) 0;
|
||||
gzipStream.zfree = (free_func) 0;
|
||||
gzipStream.opaque = (voidpf) 0;
|
||||
gzipStream.next_in = (Bytef *) srcData;
|
||||
gzipStream.zalloc = (alloc_func)0;
|
||||
gzipStream.zfree = (free_func)0;
|
||||
gzipStream.opaque = (voidpf)0;
|
||||
gzipStream.next_in = (Bytef *)srcData;
|
||||
gzipStream.avail_in = 0;
|
||||
gzipStream.next_out = (Bytef *) destData;
|
||||
gzipStream.next_out = (Bytef *)destData;
|
||||
if (inflateInit2(&gzipStream, 47) != Z_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (gzipStream.total_out < *nDestData && gzipStream.total_in < nSrcData) {
|
||||
gzipStream.avail_in = gzipStream.avail_out = nSrcData; //1
|
||||
gzipStream.avail_in = gzipStream.avail_out = nSrcData; // 1
|
||||
if ((err = inflate(&gzipStream, Z_NO_FLUSH)) == Z_STREAM_END) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (err != Z_OK) {
|
||||
if (err == Z_DATA_ERROR) {
|
||||
gzipStream.next_in = (Bytef *) dummyHead;
|
||||
gzipStream.next_in = (Bytef *)dummyHead;
|
||||
gzipStream.avail_in = sizeof(dummyHead);
|
||||
if ((err = inflate(&gzipStream, Z_NO_FLUSH)) != Z_OK) {
|
||||
return -2;
|
||||
}
|
||||
} else return -3;
|
||||
} else {
|
||||
return -3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -394,23 +395,25 @@ int32_t httpGzipDeCompress(char *srcData, int32_t nSrcData, char *destData, int3
|
|||
}
|
||||
|
||||
int32_t httpGzipCompressInit(HttpContext *pContext) {
|
||||
pContext->gzipStream.zalloc = (alloc_func) 0;
|
||||
pContext->gzipStream.zfree = (free_func) 0;
|
||||
pContext->gzipStream.opaque = (voidpf) 0;
|
||||
if (deflateInit2(&pContext->gzipStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
|
||||
pContext->gzipStream.zalloc = (alloc_func)0;
|
||||
pContext->gzipStream.zfree = (free_func)0;
|
||||
pContext->gzipStream.opaque = (voidpf)0;
|
||||
if (deflateInit2(&pContext->gzipStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) !=
|
||||
Z_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t httpGzipCompress(HttpContext *pContext, char *srcData, int32_t nSrcData, char *destData, int32_t *nDestData, bool isTheLast) {
|
||||
int32_t httpGzipCompress(HttpContext *pContext, char *srcData, int32_t nSrcData, char *destData, int32_t *nDestData,
|
||||
bool isTheLast) {
|
||||
int32_t err = 0;
|
||||
int32_t lastTotalLen = (int32_t) (pContext->gzipStream.total_out);
|
||||
pContext->gzipStream.next_in = (Bytef *) srcData;
|
||||
pContext->gzipStream.avail_in = (uLong) nSrcData;
|
||||
pContext->gzipStream.next_out = (Bytef *) destData;
|
||||
pContext->gzipStream.avail_out = (uLong) (*nDestData);
|
||||
int32_t lastTotalLen = (int32_t)(pContext->gzipStream.total_out);
|
||||
pContext->gzipStream.next_in = (Bytef *)srcData;
|
||||
pContext->gzipStream.avail_in = (uLong)nSrcData;
|
||||
pContext->gzipStream.next_out = (Bytef *)destData;
|
||||
pContext->gzipStream.avail_out = (uLong)(*nDestData);
|
||||
|
||||
while (pContext->gzipStream.avail_in != 0) {
|
||||
if (deflate(&pContext->gzipStream, Z_FULL_FLUSH) != Z_OK) {
|
||||
|
@ -442,12 +445,12 @@ int32_t httpGzipCompress(HttpContext *pContext, char *srcData, int32_t nSrcData,
|
|||
}
|
||||
}
|
||||
|
||||
*nDestData = (int32_t) (pContext->gzipStream.total_out) - lastTotalLen;
|
||||
*nDestData = (int32_t)(pContext->gzipStream.total_out) - lastTotalLen;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool httpUrlMatch(HttpContext* pContext, int32_t pos, char* cmp) {
|
||||
HttpParser* pParser = pContext->parser;
|
||||
bool httpUrlMatch(HttpContext *pContext, int32_t pos, char *cmp) {
|
||||
HttpParser *pParser = pContext->parser;
|
||||
|
||||
if (pos < 0 || pos >= HTTP_MAX_URL) {
|
||||
return false;
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
system sh/stop_dnodes.sh
|
||||
sleep 2000
|
||||
system sh/deploy.sh -n dnode1 -i 1
|
||||
system sh/cfg.sh -n dnode1 -c wallevel -v 0
|
||||
system sh/cfg.sh -n dnode1 -c http -v 1
|
||||
system sh/cfg.sh -n dnode1 -c httpEnableRecordSql -v 1
|
||||
system sh/exec.sh -n dnode1 -s start
|
||||
|
||||
sleep 2000
|
||||
sql connect
|
||||
|
||||
print ============================ dnode1 start
|
||||
|
|
Loading…
Reference in New Issue