From a33afdf3515abbe8b2612f7fd7c9e45757e888de Mon Sep 17 00:00:00 2001 From: xywang Date: Wed, 25 Aug 2021 14:04:59 +0800 Subject: [PATCH 1/5] [TD-6285]: added directive for db name in restful interface --- packaging/cfg/taos.cfg | 3 + src/common/inc/tglobal.h | 1 + src/common/src/tglobal.c | 11 ++ src/plugins/http/CMakeLists.txt | 1 + src/plugins/http/src/httpRestHandle.c | 9 ++ tests/script/general/http/restful_dbname.sim | 124 +++++++++++++++++++ 6 files changed, 149 insertions(+) create mode 100644 tests/script/general/http/restful_dbname.sim diff --git a/packaging/cfg/taos.cfg b/packaging/cfg/taos.cfg index 06e7cb7da0..310369aa14 100644 --- a/packaging/cfg/taos.cfg +++ b/packaging/cfg/taos.cfg @@ -194,6 +194,9 @@ keepColumnName 1 # maximum number of rows returned by the restful interface # restfulRowLimit 10240 +# database name must be specified in restful interface if the following parameter is set, off by default +# httpDbNameMandatory 1 + # The following parameter is used to limit the maximum number of lines in log files. # max number of lines per log filters # numOfLogLines 10000000 diff --git a/src/common/inc/tglobal.h b/src/common/inc/tglobal.h index 30ae6faf1c..285145f8f6 100644 --- a/src/common/inc/tglobal.h +++ b/src/common/inc/tglobal.h @@ -130,6 +130,7 @@ extern int32_t tsHttpMaxThreads; extern int8_t tsHttpEnableCompress; extern int8_t tsHttpEnableRecordSql; extern int8_t tsTelegrafUseFieldNum; +extern int8_t tsHttpDbNameMandatory; // mqtt extern int8_t tsEnableMqttModule; diff --git a/src/common/src/tglobal.c b/src/common/src/tglobal.c index 2d1c6780d1..0f1fe5e2b6 100644 --- a/src/common/src/tglobal.c +++ b/src/common/src/tglobal.c @@ -175,6 +175,7 @@ int32_t tsHttpMaxThreads = 2; int8_t tsHttpEnableCompress = 1; int8_t tsHttpEnableRecordSql = 0; int8_t tsTelegrafUseFieldNum = 0; +int8_t tsHttpDbNameMandatory = 0; // mqtt int8_t tsEnableMqttModule = 0; // not finished yet, not started it by default @@ -1287,6 +1288,16 @@ static void doInitGlobalConfig(void) { cfg.unitType = TAOS_CFG_UTYPE_NONE; taosInitConfigOption(cfg); + cfg.option = "httpDbNameMandatory"; + cfg.ptr = &tsHttpDbNameMandatory; + cfg.valType = TAOS_CFG_VTYPE_INT8; + cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG; + cfg.minValue = 0; + cfg.maxValue = 1; + cfg.ptrLength = 0; + cfg.unitType = TAOS_CFG_UTYPE_NONE; + taosInitConfigOption(cfg); + // debug flag cfg.option = "numOfLogLines"; cfg.ptr = &tsNumOfLogLines; diff --git a/src/plugins/http/CMakeLists.txt b/src/plugins/http/CMakeLists.txt index 57fc2ee3a2..89fdc141b6 100644 --- a/src/plugins/http/CMakeLists.txt +++ b/src/plugins/http/CMakeLists.txt @@ -6,6 +6,7 @@ INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/cJson/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/lz4/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/query/inc) +INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/common/inc) INCLUDE_DIRECTORIES(inc) AUX_SOURCE_DIRECTORY(src SRC) diff --git a/src/plugins/http/src/httpRestHandle.c b/src/plugins/http/src/httpRestHandle.c index a029adec0c..24e4f90244 100644 --- a/src/plugins/http/src/httpRestHandle.c +++ b/src/plugins/http/src/httpRestHandle.c @@ -19,6 +19,7 @@ #include "httpLog.h" #include "httpRestHandle.h" #include "httpRestJson.h" +#include "tglobal.h" static HttpDecodeMethod restDecodeMethod = {"rest", restProcessRequest}; static HttpDecodeMethod restDecodeMethod2 = {"restful", restProcessRequest}; @@ -111,6 +112,14 @@ bool restProcessSqlRequest(HttpContext* pContext, int32_t timestampFmt) { pContext->db[0] = '\0'; HttpString *path = &pContext->parser->path[REST_USER_USEDB_URL_POS]; + if (tsHttpDbNameMandatory) { + if (path->pos == 0) { + httpError("context:%p, fd:%d, user:%s, database name is mandatory", pContext, pContext->fd, pContext->user); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_INVALID_URL); + return false; + } + } + if (path->pos > 0 && !(strlen(sql) > 4 && (sql[0] == 'u' || sql[0] == 'U') && (sql[1] == 's' || sql[1] == 'S') && (sql[2] == 'e' || sql[2] == 'E') && sql[3] == ' ')) { diff --git a/tests/script/general/http/restful_dbname.sim b/tests/script/general/http/restful_dbname.sim new file mode 100644 index 0000000000..75cd198507 --- /dev/null +++ b/tests/script/general/http/restful_dbname.sim @@ -0,0 +1,124 @@ +system sh/stop_dnodes.sh +sleep 2000 +system sh/deploy.sh -n dnode1 -i 1 +system sh/cfg.sh -n dnode1 -c walLevel -v 1 +system sh/cfg.sh -n dnode1 -c http -v 1 +system sh/cfg.sh -n dnode1 -c httpDbNameMandatory -v 1 +system sh/exec.sh -n dnode1 -s start + +sleep 2000 +sql connect +sql drop database if exists db + +print ============================ dnode1 start + +print =============== step1 - login + +system_content curl 127.0.0.1:7111/rest/login/root/taosdata +print curl 127.0.0.1:7111/rest/login/root/taosdata -----> $system_content + +if $system_content != @{"status":"succ","code":0,"desc":"/KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04"}@ then + return -1 +endi + +print =============== step2 - execute sql without db_name + +print curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'show databases' 127.0.0.1:7111/rest/sql + +system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'show databases' 127.0.0.1:7111/rest/sql + +if $system_content != @{"status":"error","code":4354,"desc":"invalid url format"}@ then + return -1 +endi + +print curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'create database if not exists db' 127.0.0.1:7111/rest/sql + +system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'create database if not exists db' 127.0.0.1:7111/rest/sql + +if $system_content != @{"status":"error","code":4354,"desc":"invalid url format"}@ then + return -1 +endi + +print curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'create table table_rest (ts timestamp, i int)' 127.0.0.1:7111/rest/sql + +system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'create table table_rest (ts timestamp, i int)' 127.0.0.1:7111/rest/sql + +if $system_content != @{"status":"error","code":4354,"desc":"invalid url format"}@ then + return -1 +endi + +print curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'insert into table_rest (now, 1)' 127.0.0.1:7111/rest/sql + +system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'insert into table_rest values (now, 1)' 127.0.0.1:7111/rest/sql + +if $system_content != @{"status":"error","code":4354,"desc":"invalid url format"}@ then + return -1 +endi + +print curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'select * from table_rest' 127.0.0.1:7111/rest/sql + +system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'select * from table_rest' 127.0.0.1:7111/rest/sql + +if $system_content != @{"status":"error","code":4354,"desc":"invalid url format"}@ then + return -1 +endi + +print curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'drop database if exists db' 127.0.0.1:7111/rest/sql + +system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'drop database if exists db' 127.0.0.1:7111/rest/sql + +if $system_content != @{"status":"error","code":4354,"desc":"invalid url format"}@ then + return -1 +endi + +print =============== step3 - execute sql with db_name + +print curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'show databases' 127.0.0.1:7111/rest/sql/databases + +system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'show databases' 127.0.0.1:7111/rest/sql/databases + +if $system_content != @{"status":"succ","head":["name","created_time","ntables","vgroups","replica","quorum","days","keep","cache(MB)","blocks","minrows","maxrows","wallevel","fsync","comp","cachelast","precision","update","status"],"column_meta":[["name",8,32],["created_time",9,8],["ntables",4,4],["vgroups",4,4],["replica",3,2],["quorum",3,2],["days",3,2],["keep",8,24],["cache(MB)",4,4],["blocks",4,4],["minrows",4,4],["maxrows",4,4],["wallevel",2,1],["fsync",4,4],["comp",2,1],["cachelast",2,1],["precision",8,3],["update",2,1],["status",8,10]],"data":[],"rows":0}@ then + return -1 +endi + +print curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'create database if not exists db' 127.0.0.1:7111/rest/sql/db + +system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'create database if not exists db' 127.0.0.1:7111/rest/sql/db + +if $system_content != @{"status":"succ","head":["affected_rows"],"column_meta":[["affected_rows",4,4]],"data":[[0]],"rows":1}@ then + return -1 +endi + +print curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'create table table_rest (ts timestamp, i int)' 127.0.0.1:7111/rest/sql/db + +system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'create table table_rest (ts timestamp, i int)' 127.0.0.1:7111/rest/sql/db + +if $system_content != @{"status":"succ","head":["affected_rows"],"column_meta":[["affected_rows",4,4]],"data":[[0]],"rows":1}@ then + return -1 +endi + +print curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'insert into table_rest (now, 1)' 127.0.0.1:7111/rest/sql/db + +system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'insert into table_rest values (1629904789233, 1)' 127.0.0.1:7111/rest/sql/db + +if $system_content != @{"status":"succ","head":["affected_rows"],"column_meta":[["affected_rows",4,4]],"data":[[1]],"rows":1}@ then + return -1 +endi + +print curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'select * from table_rest' 127.0.0.1:7111/rest/sql/db + +system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'select * from table_rest' 127.0.0.1:7111/rest/sql/db + +if $system_content != @{"status":"succ","head":["ts","i"],"column_meta":[["ts",9,8],["i",4,4]],"data":[["2021-08-25 23:19:49.233",1]],"rows":1}@ then + return -1 +endi + +print curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'drop database if exists db' 127.0.0.1:7111/rest/sql/db + +system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'drop database if exists db' 127.0.0.1:7111/rest/sql/db + +if $system_content != @{"status":"succ","head":["affected_rows"],"column_meta":[["affected_rows",4,4]],"data":[[0]],"rows":1}@ then + return -1 +endi + +system sh/exec.sh -n dnode1 -s stop -x SIGINT From 28d689f47d66d94c976f839b886a1c914d702bdc Mon Sep 17 00:00:00 2001 From: xywang Date: Wed, 25 Aug 2021 19:34:36 +0800 Subject: [PATCH 2/5] [TD-6285]: fixed show tables failure when httpDbNameMandatory was on --- src/client/src/tscServer.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 783c7a3dbe..60140ffa94 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -26,6 +26,7 @@ #include "tscUtil.h" #include "tsclient.h" #include "ttimer.h" +#include "httpInt.h" int (*tscBuildMsg[TSDB_SQL_MAX])(SSqlObj *pSql, SSqlInfo *pInfo) = {0}; @@ -1434,6 +1435,9 @@ int32_t tscBuildShowMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pCmd->msgType = TSDB_MSG_TYPE_CM_SHOW; pCmd->payloadLen = sizeof(SShowMsg) + 100; + char *p = NULL; + HttpContext *pCtx = NULL; + if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, pCmd->payloadLen)) { tscError("0x%"PRIx64" failed to malloc for query msg", pSql->self); return TSDB_CODE_TSC_OUT_OF_MEMORY; @@ -1453,7 +1457,26 @@ int32_t tscBuildShowMsg(SSqlObj *pSql, SSqlInfo *pInfo) { if (tNameIsEmpty(&pTableMetaInfo->name)) { pthread_mutex_lock(&pObj->mutex); - tstrncpy(pShowMsg->db, pObj->db, sizeof(pShowMsg->db)); + STscObj *pTscObj = pSql->pTscObj; + switch (pTscObj->from) { + case TAOS_REQ_FROM_HTTP: + pCtx = pSql->param; + if (pCtx && pCtx->db[0] != '\0') { + char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN] = {0}; + int32_t len = sprintf(db, "%s%s%s", pTscObj->acctId, TS_PATH_DELIMITER, pCtx->db); + assert(len <= sizeof(db)); + + p = db; + } + break; + default: + break; + } + if (p == NULL) { + tstrncpy(pShowMsg->db, pObj->db, sizeof(pShowMsg->db)); + } else { + tstrncpy(pShowMsg->db, p, strlen(p) + 1); + } pthread_mutex_unlock(&pObj->mutex); } else { tNameGetFullDbName(&pTableMetaInfo->name, pShowMsg->db); From 8424e83eee5754cb103b89239b0f0474b3e19982 Mon Sep 17 00:00:00 2001 From: xywang Date: Thu, 26 Aug 2021 01:26:14 +0800 Subject: [PATCH 3/5] [TD-6285]: set cloneCurrentDBName as a public util function --- src/client/inc/tscUtil.h | 2 ++ src/client/src/tscSQLParser.c | 30 ------------------------------ src/client/src/tscServer.c | 30 +++--------------------------- src/client/src/tscUtil.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 57 deletions(-) diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index 64b5f8d7ee..eb2d431245 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -364,6 +364,8 @@ STblCond* tsGetTableFilter(SArray* filters, uint64_t uid, int16_t idx); void tscRemoveCachedTableMeta(STableMetaInfo* pTableMetaInfo, uint64_t id); +char* cloneCurrentDBName(SSqlObj* pSql); + #ifdef __cplusplus } #endif diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 5f888834ec..039a42c925 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -40,7 +40,6 @@ #include "qScript.h" #include "ttype.h" #include "qFilter.h" -#include "httpInt.h" #define DEFAULT_PRIMARY_TIMESTAMP_COL_NAME "_c0" @@ -72,7 +71,6 @@ static int convertTimestampStrToInt64(tVariant *pVar, int32_t precision); static bool serializeExprListToVariant(SArray* pList, tVariant **dst, int16_t colType, uint8_t precision); static bool has(SArray* pFieldList, int32_t startIdx, const char* name); -static char* cloneCurrentDBName(SSqlObj* pSql); static int32_t getDelimiterIndex(SStrToken* pTableName); static bool validateTableColumnInfo(SArray* pFieldList, SSqlCmd* pCmd); static bool validateTagParams(SArray* pTagsList, SArray* pFieldList, SSqlCmd* pCmd); @@ -1687,34 +1685,6 @@ static bool has(SArray* pFieldList, int32_t startIdx, const char* name) { static char* getAccountId(SSqlObj* pSql) { return pSql->pTscObj->acctId; } -static char* cloneCurrentDBName(SSqlObj* pSql) { - char *p = NULL; - HttpContext *pCtx = NULL; - - pthread_mutex_lock(&pSql->pTscObj->mutex); - STscObj *pTscObj = pSql->pTscObj; - switch (pTscObj->from) { - case TAOS_REQ_FROM_HTTP: - pCtx = pSql->param; - if (pCtx && pCtx->db[0] != '\0') { - char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN] = {0}; - int32_t len = sprintf(db, "%s%s%s", pTscObj->acctId, TS_PATH_DELIMITER, pCtx->db); - assert(len <= sizeof(db)); - - p = strdup(db); - } - break; - default: - break; - } - if (p == NULL) { - p = strdup(pSql->pTscObj->db); - } - pthread_mutex_unlock(&pSql->pTscObj->mutex); - - return p; -} - /* length limitation, strstr cannot be applied */ static int32_t getDelimiterIndex(SStrToken* pTableName) { for (uint32_t i = 0; i < pTableName->n; ++i) { diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 60140ffa94..30feb0db2c 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -26,7 +26,6 @@ #include "tscUtil.h" #include "tsclient.h" #include "ttimer.h" -#include "httpInt.h" int (*tscBuildMsg[TSDB_SQL_MAX])(SSqlObj *pSql, SSqlInfo *pInfo) = {0}; @@ -1430,14 +1429,10 @@ int32_t tscBuildSyncDbReplicaMsg(SSqlObj* pSql, SSqlInfo *pInfo) { } int32_t tscBuildShowMsg(SSqlObj *pSql, SSqlInfo *pInfo) { - STscObj *pObj = pSql->pTscObj; SSqlCmd *pCmd = &pSql->cmd; pCmd->msgType = TSDB_MSG_TYPE_CM_SHOW; pCmd->payloadLen = sizeof(SShowMsg) + 100; - char *p = NULL; - HttpContext *pCtx = NULL; - if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, pCmd->payloadLen)) { tscError("0x%"PRIx64" failed to malloc for query msg", pSql->self); return TSDB_CODE_TSC_OUT_OF_MEMORY; @@ -1456,28 +1451,9 @@ int32_t tscBuildShowMsg(SSqlObj *pSql, SSqlInfo *pInfo) { } if (tNameIsEmpty(&pTableMetaInfo->name)) { - pthread_mutex_lock(&pObj->mutex); - STscObj *pTscObj = pSql->pTscObj; - switch (pTscObj->from) { - case TAOS_REQ_FROM_HTTP: - pCtx = pSql->param; - if (pCtx && pCtx->db[0] != '\0') { - char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN] = {0}; - int32_t len = sprintf(db, "%s%s%s", pTscObj->acctId, TS_PATH_DELIMITER, pCtx->db); - assert(len <= sizeof(db)); - - p = db; - } - break; - default: - break; - } - if (p == NULL) { - tstrncpy(pShowMsg->db, pObj->db, sizeof(pShowMsg->db)); - } else { - tstrncpy(pShowMsg->db, p, strlen(p) + 1); - } - pthread_mutex_unlock(&pObj->mutex); + char *p = cloneCurrentDBName(pSql); + tstrncpy(pShowMsg->db, p, sizeof(pShowMsg->db)); + tfree(p); } else { tNameGetFullDbName(&pTableMetaInfo->name, pShowMsg->db); } diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index e73ce3583c..3394623d2f 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -29,6 +29,7 @@ #include "tsclient.h" #include "ttimer.h" #include "ttokendef.h" +#include "httpInt.h" static void freeQueryInfoImpl(SQueryInfo* pQueryInfo); @@ -5065,3 +5066,31 @@ void tscRemoveCachedTableMeta(STableMetaInfo* pTableMetaInfo, uint64_t id) { taosHashRemove(tscTableMetaMap, fname, len); tscDebug("0x%"PRIx64" remove table meta %s, numOfRemain:%d", id, fname, (int32_t) taosHashGetSize(tscTableMetaMap)); } + +char* cloneCurrentDBName(SSqlObj* pSql) { + char *p = NULL; + HttpContext *pCtx = NULL; + + pthread_mutex_lock(&pSql->pTscObj->mutex); + STscObj *pTscObj = pSql->pTscObj; + switch (pTscObj->from) { + case TAOS_REQ_FROM_HTTP: + pCtx = pSql->param; + if (pCtx && pCtx->db[0] != '\0') { + char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN] = {0}; + int32_t len = sprintf(db, "%s%s%s", pTscObj->acctId, TS_PATH_DELIMITER, pCtx->db); + assert(len <= sizeof(db)); + + p = strdup(db); + } + break; + default: + break; + } + if (p == NULL) { + p = strdup(pSql->pTscObj->db); + } + pthread_mutex_unlock(&pSql->pTscObj->mutex); + + return p; +} From 47f39f45abe7d495a19927f95e90292699a50650 Mon Sep 17 00:00:00 2001 From: wenzhouwww Date: Fri, 27 Aug 2021 11:53:09 +0800 Subject: [PATCH 4/5] [TD-6289]: add test case about restful for param 'httpDbNameMandatory' --- tests/pytest/fulltest.sh | 4 + tests/pytest/restful/restful_bind_db1.py | 134 ++++++++++++++++++ tests/pytest/restful/restful_bind_db2.py | 166 +++++++++++++++++++++++ 3 files changed, 304 insertions(+) create mode 100644 tests/pytest/restful/restful_bind_db1.py create mode 100644 tests/pytest/restful/restful_bind_db2.py diff --git a/tests/pytest/fulltest.sh b/tests/pytest/fulltest.sh index 42cd5d8055..6f0d9d8d33 100755 --- a/tests/pytest/fulltest.sh +++ b/tests/pytest/fulltest.sh @@ -172,6 +172,10 @@ python3 test.py -f tools/taosdemoTestSampleData.py python3 test.py -f tools/taosdemoTestInterlace.py python3 test.py -f tools/taosdemoTestQuery.py +# restful test for python +python3 test.py -f restful/restful_bind_db1.py +python3 test.py -f restful/restful_bind_db2.py + # nano support python3 test.py -f tools/taosdemoAllTest/NanoTestCase/taosdemoTestSupportNanoInsert.py python3 test.py -f tools/taosdemoAllTest/NanoTestCase/taosdemoTestSupportNanoQuery.py diff --git a/tests/pytest/restful/restful_bind_db1.py b/tests/pytest/restful/restful_bind_db1.py new file mode 100644 index 0000000000..2ea1ecc4f4 --- /dev/null +++ b/tests/pytest/restful/restful_bind_db1.py @@ -0,0 +1,134 @@ +# ################################################################# +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. + +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao + +# ################################################################# + +# -*- coding: utf-8 -*- + +# TODO: after TD-4518 and TD-4510 is resolved, add the exception test case for these situations + +from distutils.log import error +import sys + +from requests.api import head +from requests.models import Response +from util.log import * +from util.cases import * +from util.sql import * +import time, datetime + + +import requests, json +import threading +import string +import random + + +def check_unbind_db(url, data, header): + resp = requests.post(url, data, headers = header ) + resp.encoding='utf-8' + resp = eval(resp.text) + status = resp['status'] + desc = resp['desc'] + sqls = data + if status=="error" and desc == "invalid url format": + print(" %s : check pass" %sqls) + else: + printf(" error occured , ") + sys.exit() + +def check_bind_db(url, data, header): + resp = requests.post(url, data, headers = header ) + resp.encoding='utf-8' + resp_dict = eval(resp.text) + status = resp_dict['status'] + if status =="succ": + print("%s run success!"%data) + # print(resp.text) + else : + print("%s run failed !"%data) + print(resp.text) + sys.exit() + +class TDTestCase(): + updatecfgDict={'httpDbNameMandatory':1} + + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor(), logSql) + + def run(self): + tdSql.prepare() + tdSql.execute('reset query cache') + tdSql.execute('drop database if exists test') + tdSql.execute('drop database if exists db') + tdSql.execute('drop database if exists des') + tdSql.execute('create database test') + tdSql.execute('create database des') + + header = {'Authorization': 'Basic cm9vdDp0YW9zZGF0YQ=='} + url = "http://127.0.0.1:6041/rest/sql/" + + # test with no bind databases + + sqls = ["show databases;", + "use test", + "show tables;", + "show dnodes;", + "show vgroups;", + "create database db;", + "drop database db;", + "select client_version();" , + "use test", + "ALTER DATABASE test COMP 2;", + "create table tb (ts timestamp, id int , data double)", + "insert into tb values (now , 1, 1.0) ", + "select * from tb", + "show test.tables", + "show tables", + "insert into tb values (now , 2, 2.0) ", + "create table test.tb (ts timestamp, id int , data double)", + "insert into test.tb values (now , 2, 2.0) ", + "select * from tb", + "select * from test.tb"] + + + for sql in sqls: + print("===================") + check_unbind_db(url,sql,header) + + print("==================="*5) + + print(" check bind db about restful ") + + print("==================="*5) + + url = "http://127.0.0.1:6041/rest/sql/des" + for sql in sqls: + print("===================") + check_bind_db(url,sql,header) + + # check data + + tdSql.query("select * from test.tb") + tdSql.checkRows(1) + tdSql.query("select * from des.tb") + tdSql.checkRows(2) + + + + os.system('sudo timedatectl set-ntp on') + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) \ No newline at end of file diff --git a/tests/pytest/restful/restful_bind_db2.py b/tests/pytest/restful/restful_bind_db2.py new file mode 100644 index 0000000000..bdf3ae8757 --- /dev/null +++ b/tests/pytest/restful/restful_bind_db2.py @@ -0,0 +1,166 @@ +# ################################################################# +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. + +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao + +# ################################################################# + +# -*- coding: utf-8 -*- + +# TODO: after TD-4518 and TD-4510 is resolved, add the exception test case for these situations + +from distutils.log import error +import sys + +from requests.api import head +from requests.models import Response +from util.log import * +from util.cases import * +from util.sql import * +import time, datetime + + +import requests, json +import threading +import string +import random + + +def check_res(url, data, header): + resp = requests.post(url, data, headers = header ) + resp.encoding='utf-8' + resp_dict = eval(resp.text) + status = resp_dict['status'] + if status =="succ": + print("%s run success!"%data) + # print(resp.text) + else : + print("%s run failed !"%data) + print(resp.text) + sys.exit() + +class TDTestCase(): + # updatecfgDict={'httpDbNameMandatory':0} + + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor(), logSql) + + + def run(self): + tdSql.prepare() + tdSql.execute('reset query cache') + tdSql.execute('drop database if exists test') + tdSql.execute('drop database if exists db') + tdSql.execute('drop database if exists des') + tdSql.execute('create database test') + tdSql.execute('create database des') + + header = {'Authorization': 'Basic cm9vdDp0YW9zZGF0YQ=='} + url = "http://127.0.0.1:6041/rest/sql/" + + # test with no bind databases + + sqls = ["show databases;", + "use test", + "show tables;", + "show dnodes;", + "show vgroups;", + "create database db;", + "drop database db;", + "select client_version();" , + "use test", + "ALTER DATABASE test COMP 2;", + "create table tb (ts timestamp, id int , data double)", + "insert into tb values (now , 1, 1.0) ", + "select * from tb", + "show test.tables", + "show tables", + "insert into tb values (now , 2, 2.0) ", + "create table test.tb (ts timestamp, id int , data double)", + "insert into test.tb values (now , 3, 3.0) ", + "select * from tb", + "select * from test.tb", + "create table des.tb (ts timestamp, id int , data double)", + "insert into des.tb values (now , 3, 3.0)"] + + + for sql in sqls: + print("===================") + if sql == "create table test.tb (ts timestamp, id int , data double)": + resp = requests.post(url, sql, headers = header ) + print(resp.text) + print ("%s run occur error as expect ,check pass!" %(sql)) + else: + check_res(url,sql,header) + + tdSql.query("select * from test.tb") + tdSql.checkRows(3) + tdSql.query("select * from des.tb") + tdSql.checkRows(1) + + + print("==================="*5) + + print(" check bind db about restful ") + + print("==================="*5) + + + tdSql.execute('reset query cache') + tdSql.execute('drop database if exists test') + tdSql.execute('drop database if exists db') + tdSql.execute('drop database if exists des') + tdSql.execute('create database test') + tdSql.execute('create database des') + + url = "http://127.0.0.1:6041/rest/sql/des" + for sql in sqls: + print("===================") + if sql in ["create table des.tb (ts timestamp, id int , data double)"]: + resp = requests.post(url, sql, headers = header ) + print(resp.text) + print ("%s run occur error as expect ,check pass!" %(sql)) + else: + check_res(url,sql,header) + + # check data + + tdSql.query("select * from test.tb") + tdSql.checkRows(1) + tdSql.query("select * from des.tb") + tdSql.checkRows(3) + + + + + + + # curl(url ,data , header) + + + + # threads = [] + # for i in range(self.numOfThreads): + # thread = threading.Thread(target=self.insertData, args=(i,)) + # thread.start() + # threads.append(thread) + + + + + + + os.system('sudo timedatectl set-ntp on') + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) \ No newline at end of file From 00a07ec7215dae59d14f55d828fa377a2d864a5b Mon Sep 17 00:00:00 2001 From: wenzhouwww Date: Fri, 27 Aug 2021 12:00:53 +0800 Subject: [PATCH 5/5] [TD-6289] :modify case codes! --- tests/pytest/restful/restful_bind_db1.py | 13 +-------- tests/pytest/restful/restful_bind_db2.py | 35 +----------------------- 2 files changed, 2 insertions(+), 46 deletions(-) diff --git a/tests/pytest/restful/restful_bind_db1.py b/tests/pytest/restful/restful_bind_db1.py index 2ea1ecc4f4..9620535fd7 100644 --- a/tests/pytest/restful/restful_bind_db1.py +++ b/tests/pytest/restful/restful_bind_db1.py @@ -22,8 +22,6 @@ from util.log import * from util.cases import * from util.sql import * import time, datetime - - import requests, json import threading import string @@ -97,31 +95,23 @@ class TDTestCase(): "insert into test.tb values (now , 2, 2.0) ", "select * from tb", "select * from test.tb"] - for sql in sqls: print("===================") check_unbind_db(url,sql,header) print("==================="*5) - print(" check bind db about restful ") - print("==================="*5) - url = "http://127.0.0.1:6041/rest/sql/des" for sql in sqls: print("===================") check_bind_db(url,sql,header) - # check data - tdSql.query("select * from test.tb") tdSql.checkRows(1) tdSql.query("select * from des.tb") tdSql.checkRows(2) - - os.system('sudo timedatectl set-ntp on') @@ -129,6 +119,5 @@ class TDTestCase(): tdSql.close() tdLog.success("%s successfully executed" % __file__) - tdCases.addWindows(__file__, TDTestCase()) -tdCases.addLinux(__file__, TDTestCase()) \ No newline at end of file +tdCases.addLinux(__file__, TDTestCase()) diff --git a/tests/pytest/restful/restful_bind_db2.py b/tests/pytest/restful/restful_bind_db2.py index bdf3ae8757..35aa1408a7 100644 --- a/tests/pytest/restful/restful_bind_db2.py +++ b/tests/pytest/restful/restful_bind_db2.py @@ -22,13 +22,10 @@ from util.log import * from util.cases import * from util.sql import * import time, datetime - - import requests, json import threading import string import random - def check_res(url, data, header): resp = requests.post(url, data, headers = header ) @@ -49,7 +46,6 @@ class TDTestCase(): def init(self, conn, logSql): tdLog.debug("start to execute %s" % __file__) tdSql.init(conn.cursor(), logSql) - def run(self): tdSql.prepare() @@ -59,7 +55,6 @@ class TDTestCase(): tdSql.execute('drop database if exists des') tdSql.execute('create database test') tdSql.execute('create database des') - header = {'Authorization': 'Basic cm9vdDp0YW9zZGF0YQ=='} url = "http://127.0.0.1:6041/rest/sql/" @@ -87,8 +82,6 @@ class TDTestCase(): "select * from test.tb", "create table des.tb (ts timestamp, id int , data double)", "insert into des.tb values (now , 3, 3.0)"] - - for sql in sqls: print("===================") if sql == "create table test.tb (ts timestamp, id int , data double)": @@ -103,14 +96,10 @@ class TDTestCase(): tdSql.query("select * from des.tb") tdSql.checkRows(1) - print("==================="*5) - print(" check bind db about restful ") - print("==================="*5) - tdSql.execute('reset query cache') tdSql.execute('drop database if exists test') tdSql.execute('drop database if exists db') @@ -127,33 +116,11 @@ class TDTestCase(): print ("%s run occur error as expect ,check pass!" %(sql)) else: check_res(url,sql,header) - # check data - tdSql.query("select * from test.tb") tdSql.checkRows(1) tdSql.query("select * from des.tb") tdSql.checkRows(3) - - - - - - - # curl(url ,data , header) - - - - # threads = [] - # for i in range(self.numOfThreads): - # thread = threading.Thread(target=self.insertData, args=(i,)) - # thread.start() - # threads.append(thread) - - - - - os.system('sudo timedatectl set-ntp on') @@ -163,4 +130,4 @@ class TDTestCase(): tdCases.addWindows(__file__, TDTestCase()) -tdCases.addLinux(__file__, TDTestCase()) \ No newline at end of file +tdCases.addLinux(__file__, TDTestCase())