Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
adminstrot
TDengine
提交
eff4b580
TDengine
项目概览
adminstrot
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
eff4b580
编写于
9月 14, 2020
作者:
S
Shengliang Guan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
TD-1311
上级
01c69a63
变更
30
隐藏空白更改
内联
并排
Showing
30 changed file
with
1135 addition
and
1328 deletion
+1135
-1328
src/inc/taoserror.h
src/inc/taoserror.h
+35
-2
src/plugins/http/inc/httpAuth.h
src/plugins/http/inc/httpAuth.h
+3
-3
src/plugins/http/inc/httpGcJson.h
src/plugins/http/inc/httpGcJson.h
+1
-1
src/plugins/http/inc/httpGzip.h
src/plugins/http/inc/httpGzip.h
+22
-9
src/plugins/http/inc/httpInt.h
src/plugins/http/inc/httpInt.h
+34
-84
src/plugins/http/inc/httpJson.h
src/plugins/http/inc/httpJson.h
+31
-31
src/plugins/http/inc/httpParser.h
src/plugins/http/inc/httpParser.h
+86
-69
src/plugins/http/inc/httpResp.h
src/plugins/http/inc/httpResp.h
+3
-3
src/plugins/http/inc/httpRestJson.h
src/plugins/http/inc/httpRestJson.h
+4
-4
src/plugins/http/inc/httpServer.h
src/plugins/http/inc/httpServer.h
+2
-3
src/plugins/http/inc/httpSql.h
src/plugins/http/inc/httpSql.h
+7
-7
src/plugins/http/inc/httpTgJson.h
src/plugins/http/inc/httpTgJson.h
+3
-3
src/plugins/http/inc/httpUtil.h
src/plugins/http/inc/httpUtil.h
+8
-10
src/plugins/http/src/httpAuth.c
src/plugins/http/src/httpAuth.c
+8
-8
src/plugins/http/src/httpContext.c
src/plugins/http/src/httpContext.c
+10
-173
src/plugins/http/src/httpGcHandle.c
src/plugins/http/src/httpGcHandle.c
+12
-13
src/plugins/http/src/httpGcJson.c
src/plugins/http/src/httpGcJson.c
+15
-15
src/plugins/http/src/httpGzip.c
src/plugins/http/src/httpGzip.c
+25
-9
src/plugins/http/src/httpHandle.c
src/plugins/http/src/httpHandle.c
+3
-226
src/plugins/http/src/httpJson.c
src/plugins/http/src/httpJson.c
+56
-55
src/plugins/http/src/httpParser.c
src/plugins/http/src/httpParser.c
+599
-406
src/plugins/http/src/httpQueue.c
src/plugins/http/src/httpQueue.c
+3
-3
src/plugins/http/src/httpResp.c
src/plugins/http/src/httpResp.c
+20
-20
src/plugins/http/src/httpRestHandle.c
src/plugins/http/src/httpRestHandle.c
+8
-8
src/plugins/http/src/httpRestJson.c
src/plugins/http/src/httpRestJson.c
+11
-11
src/plugins/http/src/httpServer.c
src/plugins/http/src/httpServer.c
+22
-65
src/plugins/http/src/httpSystem.c
src/plugins/http/src/httpSystem.c
+3
-3
src/plugins/http/src/httpTgHandle.c
src/plugins/http/src/httpTgHandle.c
+51
-52
src/plugins/http/src/httpTgJson.c
src/plugins/http/src/httpTgJson.c
+7
-7
src/plugins/http/src/httpUtil.c
src/plugins/http/src/httpUtil.c
+43
-25
未找到文件。
src/inc/taoserror.h
浏览文件 @
eff4b580
...
...
@@ -97,8 +97,8 @@ TAOS_DEFINE_ERROR(TSDB_CODE_TSC_APP_ERROR, 0, 0x0211, "Applicatio
TAOS_DEFINE_ERROR
(
TSDB_CODE_TSC_ACTION_IN_PROGRESS
,
0
,
0x0212
,
"Action in progress"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_TSC_DISCONNECTED
,
0
,
0x0213
,
"Disconnected from service"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_TSC_NO_WRITE_AUTH
,
0
,
0x0214
,
"No write permission"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_TSC_CONN_KILLED
,
0
,
0x0215
,
"Connection killed"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_TSC_SQL_SYNTAX_ERROR
,
0
,
0x0216
,
"Syntax errr in SQL"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_TSC_CONN_KILLED
,
0
,
0x0215
,
"Connection killed"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_TSC_SQL_SYNTAX_ERROR
,
0
,
0x0216
,
"Syntax errr in SQL"
)
// mnode
TAOS_DEFINE_ERROR
(
TSDB_CODE_MND_MSG_NOT_PROCESSED
,
0
,
0x0300
,
"Message not processed"
)
...
...
@@ -247,6 +247,39 @@ TAOS_DEFINE_ERROR(TSDB_CODE_SYN_NOT_ENABLED, 0, 0x0901, "Sync modul
// wal
TAOS_DEFINE_ERROR
(
TSDB_CODE_WAL_APP_ERROR
,
0
,
0x1000
,
"Unexpected generic error in wal"
)
// http
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_UNSUPPORT_URL
,
0
,
0x1100
,
"http url is not support"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_NO_ENOUGH_MEMORY
,
0
,
0x1101
,
"no enough memory"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_INVALID_VERSION
,
0
,
0x1102
,
"invalid http version"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_INVALID_CONTENT_LENGTH
,
0
,
0x1103
,
"invalid content length"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_CREATE_GZIP_FAILED
,
0
,
0x1104
,
"failed to create gzip"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_FINISH_GZIP_FAILED
,
0
,
0x1105
,
"failed to finish gzip"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_INVALID_AUTH_TYPE
,
0
,
0x1106
,
"invalid type of Authorization"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_INVALID_AUTH_FORMAT
,
0
,
0x1107
,
"invalid format of Authorization"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_INVALID_BASIC_AUTH
,
0
,
0x1108
,
"invalid basic Authorization"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_INVALID_TAOSD_AUTH
,
0
,
0x1109
,
"invalid taosd Authorization"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_METHOD_FAILED
,
0
,
0x110A
,
"failed to parse method"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_TARGET_FAILED
,
0
,
0x110B
,
"failed to parse target"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_VERSION_FAILED
,
0
,
0x110C
,
"failed to parse http version"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_SP_FAILED
,
0
,
0x110D
,
"failed to parse sp"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_STATUS_FAILED
,
0
,
0x110E
,
"failed to parse status"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_PHRASE_FAILED
,
0
,
0x110F
,
"failed to parse phrase"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_CRLF_FAILED
,
0
,
0x1110
,
"failed to parse crlf"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_HEADER_FAILED
,
0
,
0x1111
,
"failed to parse header"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_HEADER_KEY_FAILED
,
0
,
0x1112
,
"failed to parse header key"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_HEADER_VAL_FAILED
,
0
,
0x1113
,
"failed to parse header val"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_CHUNK_SIZE_FAILED
,
0
,
0x1114
,
"failed to parse chunk size"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_CHUNK_FAILED
,
0
,
0x1115
,
"failed to parse chunk"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_END_FAILED
,
0
,
0x1116
,
"failed to parse end section"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_INVALID_STATE
,
0
,
0x1117
,
"invalid parse state"
)
TAOS_DEFINE_ERROR
(
TSDB_CODE_HTTP_PARSE_ERROR_STATE
,
0
,
0x1118
,
"failed to parse error section"
)
#ifdef TAOS_ERROR_C
};
#endif
...
...
src/plugins/http/inc/httpAuth.h
浏览文件 @
eff4b580
...
...
@@ -16,8 +16,8 @@
#ifndef TDENGINE_HTTP_TOKEN_H
#define TDENGINE_HTTP_TOKEN_H
bool
httpParseBasicAuthToken
(
HttpContext
*
pContext
,
char
*
token
,
int
len
);
bool
httpParseTaosdAuthToken
(
HttpContext
*
pContext
,
char
*
token
,
int
len
);
bool
httpGenTaosdAuthToken
(
HttpContext
*
pContext
,
char
*
token
,
int
maxLen
);
bool
httpParseBasicAuthToken
(
HttpContext
*
pContext
,
char
*
token
,
int
32_t
len
);
bool
httpParseTaosdAuthToken
(
HttpContext
*
pContext
,
char
*
token
,
int
32_t
len
);
bool
httpGenTaosdAuthToken
(
HttpContext
*
pContext
,
char
*
token
,
int
32_t
maxLen
);
#endif
\ No newline at end of file
src/plugins/http/inc/httpGcJson.h
浏览文件 @
eff4b580
...
...
@@ -24,7 +24,7 @@ void gcCleanQueryJson(HttpContext *pContext);
void
gcStartQueryJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
);
void
gcStopQueryJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
);
bool
gcBuildQueryJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
numOfRows
);
bool
gcBuildQueryJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
32_t
numOfRows
);
void
gcSendHeartBeatResp
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
);
...
...
src/plugins/http/inc/httpGzip.h
浏览文件 @
eff4b580
#ifndef _ehttp_gzip_h_9196791b_ac2a_4d73_9979_f4b41abbc4c0_
#define _ehttp_gzip_h_9196791b_ac2a_4d73_9979_f4b41abbc4c0_
#include <stddef.h>
/*
* 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/>.
*/
#ifndef HTTP_GZIP_H
#define HTTP_GZIP_H
#define EHTTP_GZIP_CHUNK_SIZE_DEFAULT (1024*16)
...
...
@@ -11,20 +24,20 @@ typedef struct ehttp_gzip_callbacks_s ehttp_gzip_callbacks_t;
typedef
struct
ehttp_gzip_conf_s
ehttp_gzip_conf_t
;
struct
ehttp_gzip_callbacks_s
{
void
(
*
on_data
)(
ehttp_gzip_t
*
gzip
,
void
*
arg
,
const
char
*
buf
,
size
_t
len
);
void
(
*
on_data
)(
ehttp_gzip_t
*
gzip
,
void
*
arg
,
const
char
*
buf
,
int32
_t
len
);
};
struct
ehttp_gzip_conf_s
{
int
get_header
:
2
;
// 0: not fetching header info
size
_t
chunk_size
;
// 0: fallback to default: EHTTP_GZIP_CHUNK_SIZE_DEFAULT
int
32_t
get_header
:
2
;
// 0: not fetching header info
int32
_t
chunk_size
;
// 0: fallback to default: EHTTP_GZIP_CHUNK_SIZE_DEFAULT
};
ehttp_gzip_t
*
ehttp_gzip_create_decompressor
(
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
);
int
ehttp_gzip_write
(
ehttp_gzip_t
*
gzip
,
const
char
*
buf
,
size
_t
len
);
int
ehttp_gzip_finish
(
ehttp_gzip_t
*
gzip
);
int
32_t
ehttp_gzip_write
(
ehttp_gzip_t
*
gzip
,
const
char
*
buf
,
int32
_t
len
);
int
32_t
ehttp_gzip_finish
(
ehttp_gzip_t
*
gzip
);
#endif // _ehttp_gzip_h_9196791b_ac2a_4d73_9979_f4b41abbc4c0_
src/plugins/http/inc/httpInt.h
浏览文件 @
eff4b580
...
...
@@ -30,51 +30,26 @@
#include "httpParser.h"
#define HTTP_MAX_CMD_SIZE 1024
#define HTTP_MAX_BUFFER_SIZE 1024*1024
#define HTTP_MAX_BUFFER_SIZE 1024*1024*8
#define HTTP_LABEL_SIZE 8
#define HTTP_MAX_EVENTS 10
#define HTTP_BUFFER_
SIZE 1024*65 //65k
#define HTTP_
DECOMPRESS_BUF_SIZE 1024*64
#define HTTP_BUFFER_
INIT 8192
#define HTTP_
BUFFER_SIZE 8192000
#define HTTP_STEP_SIZE 1024 //http message get process step by step
#define HTTP_MAX_URL 5 //http url stack size
#define HTTP_METHOD_SCANNER_SIZE 7 //http method fp size
#define HTTP_GC_TARGET_SIZE 512
#define HTTP_VERSION_10 0
#define HTTP_VERSION_11 1
//#define HTTP_VERSION_12 2
#define HTTP_UNCUNKED 0
#define HTTP_CHUNKED 1
#define HTTP_KEEPALIVE_NO_INPUT 0
#define HTTP_KEEPALIVE_ENABLE 1
#define HTTP_KEEPALIVE_DISABLE 2
#define HTTP_REQTYPE_OTHERS 0
#define HTTP_REQTYPE_LOGIN 1
#define HTTP_REQTYPE_HEARTBEAT 2
#define HTTP_REQTYPE_SINGLE_SQL 3
#define HTTP_REQTYPE_MULTI_SQL 4
#define HTTP_CHECK_BODY_ERROR -1
#define HTTP_CHECK_BODY_CONTINUE 0
#define HTTP_CHECK_BODY_SUCCESS 1
#define HTTP_READ_DATA_SUCCESS 0
#define HTTP_READ_DATA_FAILED 1
#define HTTP_WRITE_RETRY_TIMES 500
#define HTTP_WRITE_WAIT_TIME_MS 5
#define HTTP_EXPIRED_TIME 60000
#define HTTP_DELAY_CLOSE_TIME_MS 500
#define HTTP_COMPRESS_IDENTITY 0
#define HTTP_COMPRESS_GZIP 2
#define HTTP_SESSION_ID_LEN (TSDB_USER_LEN + TSDB_PASSWORD_LEN)
typedef
enum
HttpReqType
{
HTTP_REQTYPE_OTHERS
=
0
,
HTTP_REQTYPE_LOGIN
=
1
,
HTTP_REQTYPE_HEARTBEAT
=
2
,
HTTP_REQTYPE_SINGLE_SQL
=
3
,
HTTP_REQTYPE_MULTI_SQL
=
4
}
HttpReqType
;
typedef
enum
{
HTTP_SERVER_INIT
,
HTTP_SERVER_RUNNING
,
...
...
@@ -83,21 +58,12 @@ typedef enum {
}
HttpServerStatus
;
typedef
enum
{
HTTP_CONTEXT_STATE_READY
,
HTTP_CONTEXT_STATE_HANDLING
,
HTTP_CONTEXT_STATE_DROPPING
,
HTTP_CONTEXT_STATE_CLOSED
HTTP_CONTEXT_STATE_READY
,
HTTP_CONTEXT_STATE_HANDLING
,
HTTP_CONTEXT_STATE_DROPPING
,
HTTP_CONTEXT_STATE_CLOSED
}
HttpContextState
;
struct
HttpContext
;
struct
HttpThread
;
typedef
struct
{
char
id
[
HTTP_SESSION_ID_LEN
];
int
refCount
;
void
*
taos
;
}
HttpSession
;
typedef
enum
{
HTTP_CMD_TYPE_UN_SPECIFIED
,
HTTP_CMD_TYPE_CREATE_DB
,
...
...
@@ -109,6 +75,15 @@ typedef enum { HTTP_CMD_STATE_NOT_RUN_YET, HTTP_CMD_STATE_RUN_FINISHED } HttpSql
typedef
enum
{
HTTP_CMD_RETURN_TYPE_WITH_RETURN
,
HTTP_CMD_RETURN_TYPE_NO_RETURN
}
HttpSqlCmdReturnType
;
struct
HttpContext
;
struct
HttpThread
;
typedef
struct
{
char
id
[
HTTP_SESSION_ID_LEN
];
int32_t
refCount
;
void
*
taos
;
}
HttpSession
;
typedef
struct
{
// used by single cmd
char
*
nativSql
;
...
...
@@ -158,42 +133,17 @@ typedef struct {
void
(
*
setNextCmdFp
)(
struct
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
code
);
}
HttpEncodeMethod
;
typedef
struct
{
char
*
pos
;
int32_t
len
;
}
HttpBuf
;
typedef
enum
{
EHTTP_CONTEXT_PROCESS_FAILED
=
0x01
,
EHTTP_CONTEXT_PARSER_FAILED
=
0x02
}
EHTTP_CONTEXT_FAILED_CAUSE
;
typedef
struct
{
char
buffer
[
HTTP_BUFFER_SIZE
];
int
bufsize
;
char
*
pLast
;
char
*
pCur
;
HttpBuf
method
;
HttpBuf
path
[
HTTP_MAX_URL
];
// url: dbname/meter/query
HttpBuf
data
;
// body content
HttpBuf
token
;
// auth token
HttpDecodeMethod
*
pMethod
;
HttpParserObj
*
parser
;
int8_t
inited
;
int8_t
failed
;
}
HttpParser
;
typedef
struct
HttpContext
{
int32_t
refCount
;
int
fd
;
int
32_t
fd
;
uint32_t
accessTimes
;
uint32_t
lastAccessTime
;
int32_t
state
;
uint8_t
httpVersion
;
uint8_t
httpChunked
;
uint8_t
httpKeepAlive
;
// http1.0 and not keep-alive, close connection immediately
uint8_t
acceptEncoding
;
uint8_t
contentEncoding
;
uint8_t
reqType
;
uint8_t
parsed
;
char
ipstr
[
22
];
...
...
@@ -203,12 +153,12 @@ typedef struct HttpContext {
void
*
ppContext
;
HttpSession
*
session
;
z_stream
gzipStream
;
HttpParser
parser
;
HttpParser
*
parser
;
HttpSqlCmd
singleCmd
;
HttpSqlCmds
*
multiCmds
;
JsonBuf
*
jsonBuf
;
void
*
timer
;
Http
EncodeMethod
*
en
codeMethod
;
HttpEncodeMethod
*
encodeMethod
;
Http
DecodeMethod
*
de
codeMethod
;
struct
HttpThread
*
pThread
;
}
HttpContext
;
...
...
@@ -217,9 +167,9 @@ typedef struct HttpThread {
HttpContext
*
pHead
;
pthread_mutex_t
threadMutex
;
bool
stop
;
int
pollFd
;
int
numOfContexts
;
int
threadId
;
int
32_t
pollFd
;
int
32_t
numOfContexts
;
int
32_t
threadId
;
char
label
[
HTTP_LABEL_SIZE
];
bool
(
*
processData
)(
HttpContext
*
pContext
);
}
HttpThread
;
...
...
@@ -228,9 +178,9 @@ typedef struct HttpServer {
char
label
[
HTTP_LABEL_SIZE
];
uint32_t
serverIp
;
uint16_t
serverPort
;
int
fd
;
int
numOfThreads
;
int
methodScannerLen
;
int
32_t
fd
;
int
32_t
numOfThreads
;
int
32_t
methodScannerLen
;
int32_t
requestNum
;
int32_t
status
;
pthread_t
thread
;
...
...
src/plugins/http/inc/httpJson.h
浏览文件 @
eff4b580
...
...
@@ -37,65 +37,65 @@ extern char JsonTrueTkn[];
extern
char
JsonFalseTkn
[];
typedef
struct
{
int
size
;
int
total
;
char
*
lst
;
char
buf
[
JSON_BUFFER_SIZE
];
struct
HttpContext
*
pContext
;
int
32_t
size
;
int
32_t
total
;
char
*
lst
;
char
buf
[
JSON_BUFFER_SIZE
];
struct
HttpContext
*
pContext
;
}
JsonBuf
;
// http response
int
httpWriteBuf
(
struct
HttpContext
*
pContext
,
const
char
*
buf
,
in
t
sz
);
int
httpWriteBufNoTrace
(
struct
HttpContext
*
pContext
,
const
char
*
buf
,
in
t
sz
);
int
httpWriteBufByFd
(
struct
HttpContext
*
pContext
,
const
char
*
buf
,
in
t
sz
);
int
32_t
httpWriteBuf
(
struct
HttpContext
*
pContext
,
const
char
*
buf
,
int32_
t
sz
);
int
32_t
httpWriteBufNoTrace
(
struct
HttpContext
*
pContext
,
const
char
*
buf
,
int32_
t
sz
);
int
32_t
httpWriteBufByFd
(
struct
HttpContext
*
pContext
,
const
char
*
buf
,
int32_
t
sz
);
// builder callback
typedef
void
(
*
httpJsonBuilder
)(
JsonBuf
*
buf
,
void
*
jsnHandle
);
// buffer
void
httpInitJsonBuf
(
JsonBuf
*
buf
,
struct
HttpContext
*
pContext
);
void
httpWriteJsonBufHead
(
JsonBuf
*
buf
);
int
httpWriteJsonBufBody
(
JsonBuf
*
buf
,
bool
isTheLast
);
void
httpWriteJsonBufEnd
(
JsonBuf
*
buf
);
void
httpInitJsonBuf
(
JsonBuf
*
buf
,
struct
HttpContext
*
pContext
);
void
httpWriteJsonBufHead
(
JsonBuf
*
buf
);
int
32_t
httpWriteJsonBufBody
(
JsonBuf
*
buf
,
bool
isTheLast
);
void
httpWriteJsonBufEnd
(
JsonBuf
*
buf
);
// value
void
httpJsonString
(
JsonBuf
*
buf
,
char
*
sVal
,
int
len
);
void
httpJsonOriginString
(
JsonBuf
*
buf
,
char
*
sVal
,
int
len
);
void
httpJsonStringForTransMean
(
JsonBuf
*
buf
,
char
*
SVal
,
int
maxLen
);
void
httpJsonString
(
JsonBuf
*
buf
,
char
*
sVal
,
int
32_t
len
);
void
httpJsonOriginString
(
JsonBuf
*
buf
,
char
*
sVal
,
int
32_t
len
);
void
httpJsonStringForTransMean
(
JsonBuf
*
buf
,
char
*
SVal
,
int
32_t
maxLen
);
void
httpJsonInt64
(
JsonBuf
*
buf
,
int64_t
num
);
void
httpJsonTimestamp
(
JsonBuf
*
buf
,
int64_t
t
,
bool
us
);
void
httpJsonUtcTimestamp
(
JsonBuf
*
buf
,
int64_t
t
,
bool
us
);
void
httpJsonInt
(
JsonBuf
*
buf
,
int
num
);
void
httpJsonInt
(
JsonBuf
*
buf
,
int
32_t
num
);
void
httpJsonFloat
(
JsonBuf
*
buf
,
float
num
);
void
httpJsonDouble
(
JsonBuf
*
buf
,
double
num
);
void
httpJsonNull
(
JsonBuf
*
buf
);
void
httpJsonBool
(
JsonBuf
*
buf
,
int
val
);
void
httpJsonBool
(
JsonBuf
*
buf
,
int
32_t
val
);
// pair
void
httpJsonPair
(
JsonBuf
*
buf
,
char
*
name
,
int
nameLen
,
char
*
sVal
,
in
t
valLen
);
void
httpJsonPairOriginString
(
JsonBuf
*
buf
,
char
*
name
,
int
nameLen
,
char
*
sVal
,
in
t
valLen
);
void
httpJsonPairHead
(
JsonBuf
*
buf
,
char
*
name
,
int
len
);
void
httpJsonPairIntVal
(
JsonBuf
*
buf
,
char
*
name
,
int
nNameLen
,
in
t
num
);
void
httpJsonPairInt64Val
(
JsonBuf
*
buf
,
char
*
name
,
int
nNameLen
,
int64_t
num
);
void
httpJsonPairBoolVal
(
JsonBuf
*
buf
,
char
*
name
,
int
nNameLen
,
in
t
num
);
void
httpJsonPairFloatVal
(
JsonBuf
*
buf
,
char
*
name
,
int
nNameLen
,
float
num
);
void
httpJsonPairDoubleVal
(
JsonBuf
*
buf
,
char
*
name
,
int
nNameLen
,
double
num
);
void
httpJsonPairNullVal
(
JsonBuf
*
buf
,
char
*
name
,
int
nNameLen
);
void
httpJsonPair
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nameLen
,
char
*
sVal
,
int32_
t
valLen
);
void
httpJsonPairOriginString
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nameLen
,
char
*
sVal
,
int32_
t
valLen
);
void
httpJsonPairHead
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
len
);
void
httpJsonPairIntVal
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nNameLen
,
int32_
t
num
);
void
httpJsonPairInt64Val
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nNameLen
,
int64_t
num
);
void
httpJsonPairBoolVal
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nNameLen
,
int32_
t
num
);
void
httpJsonPairFloatVal
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nNameLen
,
float
num
);
void
httpJsonPairDoubleVal
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nNameLen
,
double
num
);
void
httpJsonPairNullVal
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nNameLen
);
// object
void
httpJsonPairArray
(
JsonBuf
*
buf
,
char
*
name
,
int
nLen
,
httpJsonBuilder
builder
,
void
*
dsHandle
);
void
httpJsonPairObject
(
JsonBuf
*
buf
,
char
*
name
,
int
nLen
,
httpJsonBuilder
builder
,
void
*
dsHandle
);
void
httpJsonPairArray
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nLen
,
httpJsonBuilder
builder
,
void
*
dsHandle
);
void
httpJsonPairObject
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nLen
,
httpJsonBuilder
builder
,
void
*
dsHandle
);
void
httpJsonObject
(
JsonBuf
*
buf
,
httpJsonBuilder
fnBuilder
,
void
*
dsHandle
);
void
httpJsonArray
(
JsonBuf
*
buf
,
httpJsonBuilder
fnBuidler
,
void
*
jsonHandle
);
// print
void
httpJsonTestBuf
(
JsonBuf
*
buf
,
int
safety
);
void
httpJsonTestBuf
(
JsonBuf
*
buf
,
int
32_t
safety
);
void
httpJsonToken
(
JsonBuf
*
buf
,
char
c
);
void
httpJsonItemToken
(
JsonBuf
*
buf
);
void
httpJsonPrint
(
JsonBuf
*
buf
,
const
char
*
json
,
int
len
);
void
httpJsonPrint
(
JsonBuf
*
buf
,
const
char
*
json
,
int
32_t
len
);
// quick
void
httpJsonPairStatus
(
JsonBuf
*
buf
,
int
code
);
void
httpJsonPairStatus
(
JsonBuf
*
buf
,
int
32_t
code
);
// http json printer
JsonBuf
*
httpMallocJsonBuf
(
struct
HttpContext
*
pContext
);
...
...
src/plugins/http/inc/httpParser.h
浏览文件 @
eff4b580
/*
* 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/>.
*/
#ifndef HTTP_PARSER_H
#define HTTP_PARSER_H
#include "httpGzip.h"
struct
HttpContext
;
#define HTTP_MAX_URL 5 // http url stack size
typedef
enum
HTTP_PARSER_STATE
{
HTTP_PARSER_BEGIN
,
...
...
@@ -24,79 +38,82 @@ typedef enum HTTP_PARSER_STATE {
HTTP_PARSER_ERROR
,
}
HTTP_PARSER_STATE
;
typedef
struct
HttpParserString
{
char
*
str
;
size_t
len
;
}
HttpParserString
;
typedef
struct
HttpParserStatusObj
{
int32_t
status_code
;
const
char
*
status_desc
;
}
HttpParserStatusObj
;
typedef
enum
HTTP_AUTH_TYPE
{
HTTP_INVALID_AUTH
,
HTTP_BASIC_AUTH
,
HTTP_TAOSD_AUTH
}
HTTP_AUTH_TYPE
;
typedef
struct
HttpParserCallbackObj
{
void
(
*
on_request_line
)(
void
*
arg
,
const
char
*
method
,
const
char
*
target
,
const
char
*
version
,
const
char
*
target_raw
);
void
(
*
on_status_line
)(
void
*
arg
,
const
char
*
version
,
int
status_code
,
const
char
*
reason_phrase
);
void
(
*
on_header_field
)(
void
*
arg
,
const
char
*
key
,
const
char
*
val
);
void
(
*
on_body
)(
void
*
arg
,
const
char
*
chunk
,
size_t
len
);
void
(
*
on_end
)(
void
*
arg
);
void
(
*
on_error
)(
void
*
arg
,
int
status_code
);
}
HttpParserCallbackObj
;
typedef
enum
HTTP_VERSION
{
HTTP_VERSION_10
=
0
,
HTTP_VERSION_11
=
1
,
HTTP_VERSION_12
=
2
,
HTTP_INVALID_VERSION
}
HTTP_VERSION
;
typedef
struct
HttpParserConfObj
{
size_t
flush_block_size
;
// <=0: immediately
}
HttpParserConfObj
;
typedef
enum
HTTP_KEEPALIVE
{
HTTP_KEEPALIVE_NO_INPUT
=
0
,
HTTP_KEEPALIVE_ENABLE
=
1
,
HTTP_KEEPALIVE_DISABLE
=
2
}
HTTP_KEEPALIVE
;
typedef
struct
HttpParseKvObj
{
char
*
key
;
char
*
val
;
}
HttpParseKvObj
;
typedef
struct
HttpString
{
char
*
str
;
int32_t
pos
;
int32_t
size
;
}
HttpString
;
typedef
struct
HttpParserObj
{
HttpParserCallbackObj
callbacks
;
HttpParserConfObj
conf
;
void
*
arg
;
char
*
method
;
char
*
target
;
char
*
target_raw
;
char
*
version
;
int
http_10
:
2
;
int
http_11
:
2
;
int
accept_encoding_gzip
:
2
;
int
accept_encoding_chunked
:
2
;
int
transfer_gzip
:
2
;
int
transfer_chunked
:
2
;
int
content_length_specified
:
2
;
int
content_chunked
:
2
;
int
status_code
;
char
*
reason_phrase
;
char
*
key
;
char
*
val
;
HttpParseKvObj
*
kvs
;
size_t
kvs_count
;
char
*
auth_basic
;
char
*
auth_taosd
;
size_t
content_length
;
size_t
chunk_size
;
size_t
received_chunk_size
;
size_t
received_size
;
ehttp_gzip_t
*
gzip
;
HttpParserString
str
;
HTTP_PARSER_STATE
*
stacks
;
size_t
stacks_count
;
}
HttpParserObj
;
typedef
struct
HttpStatus
{
int32_t
code
;
char
*
desc
;
}
HttpStatus
;
void
httpParserCleanupString
(
HttpParserString
*
str
);
int32_t
httpParserAppendString
(
HttpParserString
*
str
,
const
char
*
s
,
int32_t
len
);
void
httpParserClearString
(
HttpParserString
*
str
);
typedef
struct
HttpStack
{
int8_t
*
stacks
;
int32_t
pos
;
int32_t
size
;
}
HttpStack
;
struct
HttpContext
;
typedef
struct
HttpParser
{
struct
HttpContext
*
pContext
;
ehttp_gzip_t
*
gzip
;
HttpStack
stacks
;
HttpString
str
;
HttpString
body
;
HttpString
path
[
HTTP_MAX_URL
];
char
*
method
;
char
*
target
;
char
*
target_raw
;
char
*
version
;
char
*
reasonPhrase
;
char
*
key
;
char
*
val
;
char
*
authContent
;
int8_t
httpVersion
;
int8_t
acceptEncodingGzip
;
int8_t
acceptEncodingChunked
;
int8_t
contentLengthSpecified
;
int8_t
contentChunked
;
int8_t
transferGzip
;
int8_t
transferChunked
;
int8_t
keepAlive
;
int8_t
authType
;
int32_t
contentLength
;
int32_t
chunkSize
;
int32_t
receivedChunkSize
;
int32_t
receivedSize
;
int32_t
statusCode
;
int8_t
inited
;
int8_t
parsed
;
int16_t
httpCode
;
int32_t
parseCode
;
}
HttpParser
;
HttpParserObj
*
httpParserCreate
(
HttpParserCallbackObj
callbacks
,
HttpParserConfObj
conf
,
void
*
arg
);
void
httpParserDestroy
(
HttpParserObj
*
parser
);
int32_t
httpParserBuf
(
struct
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
const
char
*
buf
,
int32_t
len
);
char
*
ehttp_parser_urldecode
(
const
char
*
enc
);
const
char
*
ehttp_status_code_get_desc
(
const
int
status_code
);
void
httpInitParser
(
HttpParser
*
parser
);
HttpParser
*
httpCreateParser
(
struct
HttpContext
*
pContext
);
void
httpDestroyParser
(
HttpParser
*
parser
);
int32_t
httpParseBuf
(
HttpParser
*
parser
,
const
char
*
buf
,
int32_t
len
);
char
*
httpGetStatusDesc
(
int32_t
statusCode
);
#endif
src/plugins/http/inc/httpResp.h
浏览文件 @
eff4b580
...
...
@@ -32,9 +32,9 @@ enum _httpRespTempl {
extern
const
char
*
httpRespTemplate
[];
void
httpSendErrorResp
(
HttpContext
*
pContext
,
int
errNo
);
void
httpSendErrorRespWithDesc
(
HttpContext
*
pContext
,
int
errNo
,
char
*
desc
);
void
httpSendTaosdErrorResp
(
HttpContext
*
pContext
,
int
errCode
);
void
httpSendErrorResp
(
HttpContext
*
pContext
,
int
32_t
errNo
);
void
httpSendErrorRespWithDesc
(
HttpContext
*
pContext
,
int
32_t
errNo
,
char
*
desc
);
void
httpSendTaosdErrorResp
(
HttpContext
*
pContext
,
int
32_t
errCode
);
void
httpSendTaosdInvalidSqlErrorResp
(
HttpContext
*
pContext
,
char
*
errMsg
);
void
httpSendSuccResp
(
HttpContext
*
pContext
,
char
*
desc
);
void
httpSendOptionResp
(
HttpContext
*
pContext
,
char
*
desc
);
...
...
src/plugins/http/inc/httpRestJson.h
浏览文件 @
eff4b580
...
...
@@ -43,12 +43,12 @@
#define REST_TIMESTAMP_FMT_TIMESTAMP 1
#define REST_TIMESTAMP_FMT_UTC_STRING 2
void
restBuildSqlAffectRowsJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
affect_rows
);
void
restBuildSqlAffectRowsJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
32_t
affect_rows
);
void
restStartSqlJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
);
bool
restBuildSqlTimestampJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
numOfRows
);
bool
restBuildSqlLocalTimeStringJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
numOfRows
);
bool
restBuildSqlUtcTimeStringJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
numOfRows
);
bool
restBuildSqlTimestampJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
32_t
numOfRows
);
bool
restBuildSqlLocalTimeStringJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
32_t
numOfRows
);
bool
restBuildSqlUtcTimeStringJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
32_t
numOfRows
);
void
restStopSqlJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
);
#endif
\ No newline at end of file
src/plugins/http/inc/httpServer.h
浏览文件 @
eff4b580
...
...
@@ -21,8 +21,7 @@
bool
httpInitConnect
();
void
httpCleanUpConnect
();
void
*
httpInitServer
(
char
*
ip
,
uint16_t
port
,
char
*
label
,
int
numOfThreads
,
void
*
fp
,
void
*
shandle
);
void
httpCleanUpServer
(
HttpServer
*
pServer
);
int
httpReadDataImp
(
HttpContext
*
pContext
);
void
*
httpInitServer
(
char
*
ip
,
uint16_t
port
,
char
*
label
,
int32_t
numOfThreads
,
void
*
fp
,
void
*
shandle
);
void
httpCleanUpServer
(
HttpServer
*
pServer
);
#endif
src/plugins/http/inc/httpSql.h
浏览文件 @
eff4b580
...
...
@@ -19,20 +19,20 @@
int32_t
httpAddToSqlCmdBuffer
(
HttpContext
*
pContext
,
const
char
*
const
format
,
...);
int32_t
httpAddToSqlCmdBufferNoTerminal
(
HttpContext
*
pContext
,
const
char
*
const
format
,
...);
int32_t
httpAddToSqlCmdBufferWithSize
(
HttpContext
*
pContext
,
int
mallocSize
);
int32_t
httpAddToSqlCmdBufferWithSize
(
HttpContext
*
pContext
,
int
32_t
mallocSize
);
int32_t
httpAddToSqlCmdBufferTerminal
(
HttpContext
*
pContext
);
bool
httpMallocMultiCmds
(
HttpContext
*
pContext
,
int
cmdSize
,
in
t
bufferSize
);
bool
httpReMallocMultiCmdsSize
(
HttpContext
*
pContext
,
int
cmdSize
);
bool
httpReMallocMultiCmdsBuffer
(
HttpContext
*
pContext
,
int
bufferSize
);
bool
httpMallocMultiCmds
(
HttpContext
*
pContext
,
int
32_t
cmdSize
,
int32_
t
bufferSize
);
bool
httpReMallocMultiCmdsSize
(
HttpContext
*
pContext
,
int
32_t
cmdSize
);
bool
httpReMallocMultiCmdsBuffer
(
HttpContext
*
pContext
,
int
32_t
bufferSize
);
void
httpFreeMultiCmds
(
HttpContext
*
pContext
);
HttpSqlCmd
*
httpNewSqlCmd
(
HttpContext
*
pContext
);
HttpSqlCmd
*
httpCurrSqlCmd
(
HttpContext
*
pContext
);
int
httpCurSqlCmdPos
(
HttpContext
*
pContext
);
int
32_t
httpCurSqlCmdPos
(
HttpContext
*
pContext
);
void
httpTrimTableName
(
char
*
name
);
int
httpShrinkTableName
(
HttpContext
*
pContext
,
in
t
pos
,
char
*
name
);
char
*
httpGetCmdsString
(
HttpContext
*
pContext
,
int
pos
);
int
32_t
httpShrinkTableName
(
HttpContext
*
pContext
,
int32_
t
pos
,
char
*
name
);
char
*
httpGetCmdsString
(
HttpContext
*
pContext
,
int
32_t
pos
);
#endif
src/plugins/http/inc/httpTgJson.h
浏览文件 @
eff4b580
...
...
@@ -24,8 +24,8 @@ void tgInitQueryJson(HttpContext *pContext);
void
tgCleanQueryJson
(
HttpContext
*
pContext
);
void
tgStartQueryJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
);
void
tgStopQueryJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
);
void
tgBuildSqlAffectRowsJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
affect_rows
);
bool
tgCheckFinished
(
struct
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
code
);
void
tgSetNextCmd
(
struct
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
code
);
void
tgBuildSqlAffectRowsJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
32_t
affect_rows
);
bool
tgCheckFinished
(
struct
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
32_t
code
);
void
tgSetNextCmd
(
struct
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
32_t
code
);
#endif
\ No newline at end of file
src/plugins/http/inc/httpUtil.h
浏览文件 @
eff4b580
...
...
@@ -17,21 +17,19 @@
#define TDENGINE_HTTP_UTIL_H
bool
httpCheckUsedbSql
(
char
*
sql
);
void
httpTimeToString
(
time_t
t
,
char
*
buf
,
int
buflen
);
void
httpTimeToString
(
time_t
t
,
char
*
buf
,
int
32_t
buflen
);
bool
httpUrlMatch
(
HttpContext
*
pContext
,
in
t
pos
,
char
*
cmp
);
bool
httpParseRequest
(
HttpContext
*
pContext
);
int
httpCheckReadCompleted
(
HttpContext
*
pContext
);
void
httpReadDirtyData
(
HttpContext
*
pContext
);
bool
httpUrlMatch
(
HttpContext
*
pContext
,
int32_
t
pos
,
char
*
cmp
);
bool
httpParseRequest
(
HttpContext
*
pContext
);
int
32_t
httpCheckReadCompleted
(
HttpContext
*
pContext
);
void
httpReadDirtyData
(
HttpContext
*
pContext
);
int
httpGzipDeCompress
(
char
*
srcData
,
int32_t
nSrcData
,
char
*
destData
,
int32_t
*
nDestData
);
int
httpGzipCompressInit
(
HttpContext
*
pContext
);
int
httpGzipCompress
(
HttpContext
*
pContext
,
char
*
inSrcData
,
int32_t
inSrcDataLen
,
int
32_t
httpGzipDeCompress
(
char
*
srcData
,
int32_t
nSrcData
,
char
*
destData
,
int32_t
*
nDestData
);
int
32_t
httpGzipCompressInit
(
HttpContext
*
pContext
);
int
32_t
httpGzipCompress
(
HttpContext
*
pContext
,
char
*
inSrcData
,
int32_t
inSrcDataLen
,
char
*
outDestData
,
int32_t
*
outDestDataLen
,
bool
isTheLast
);
// http request parser
void
httpAddMethod
(
HttpServer
*
pServer
,
HttpDecodeMethod
*
pMethod
);
#endif
src/plugins/http/src/httpAuth.c
浏览文件 @
eff4b580
...
...
@@ -23,9 +23,9 @@
#define KEY_DES_4 4971256377704625728L
bool
httpParseBasicAuthToken
(
HttpContext
*
pContext
,
char
*
token
,
int
len
)
{
bool
httpParseBasicAuthToken
(
HttpContext
*
pContext
,
char
*
token
,
int
32_t
len
)
{
token
[
len
]
=
'\0'
;
int
outlen
=
0
;
int
32_t
outlen
=
0
;
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
);
...
...
@@ -40,7 +40,7 @@ bool httpParseBasicAuthToken(HttpContext *pContext, char *token, int len) {
return
false
;
}
int
user_len
=
(
in
t
)(
user
-
base64
);
int
32_t
user_len
=
(
int32_
t
)(
user
-
base64
);
if
(
user_len
<
1
||
user_len
>=
TSDB_USER_LEN
)
{
httpError
(
"context:%p, fd:%d, basic token:%s parse user error"
,
pContext
,
pContext
->
fd
,
token
);
free
(
base64
);
...
...
@@ -50,7 +50,7 @@ bool httpParseBasicAuthToken(HttpContext *pContext, char *token, int len) {
pContext
->
user
[
user_len
]
=
0
;
char
*
password
=
user
+
1
;
int
pass_len
=
(
in
t
)((
base64
+
outlen
)
-
password
);
int
32_t
pass_len
=
(
int32_
t
)((
base64
+
outlen
)
-
password
);
if
(
pass_len
<
1
||
pass_len
>=
TSDB_PASSWORD_LEN
)
{
httpError
(
"context:%p, fd:%d, basic token:%s parse password error"
,
pContext
,
pContext
->
fd
,
token
);
free
(
base64
);
...
...
@@ -64,9 +64,9 @@ bool httpParseBasicAuthToken(HttpContext *pContext, char *token, int len) {
return
true
;
}
bool
httpParseTaosdAuthToken
(
HttpContext
*
pContext
,
char
*
token
,
int
len
)
{
bool
httpParseTaosdAuthToken
(
HttpContext
*
pContext
,
char
*
token
,
int
32_t
len
)
{
token
[
len
]
=
'\0'
;
int
outlen
=
0
;
int
32_t
outlen
=
0
;
unsigned
char
*
base64
=
base64_decode
(
token
,
len
,
&
outlen
);
if
(
base64
==
NULL
||
outlen
==
0
)
{
httpError
(
"context:%p, fd:%d, taosd token:%s parsed error"
,
pContext
,
pContext
->
fd
,
token
);
...
...
@@ -96,7 +96,7 @@ bool httpParseTaosdAuthToken(HttpContext *pContext, char *token, int len) {
}
}
bool
httpGenTaosdAuthToken
(
HttpContext
*
pContext
,
char
*
token
,
int
maxLen
)
{
bool
httpGenTaosdAuthToken
(
HttpContext
*
pContext
,
char
*
token
,
int
32_t
maxLen
)
{
char
buffer
[
sizeof
(
pContext
->
user
)
+
sizeof
(
pContext
->
pass
)]
=
{
0
};
size_t
size
=
sizeof
(
pContext
->
user
);
tstrncpy
(
buffer
,
pContext
->
user
,
size
);
...
...
@@ -111,7 +111,7 @@ bool httpGenTaosdAuthToken(HttpContext *pContext, char *token, int maxLen) {
free
(
encrypt
);
free
(
base64
);
httpDebug
(
"context:%p, fd:%d, gen taosd token:%s"
,
pContext
,
pContext
->
fd
,
token
);
httpDebug
(
"context:%p, fd:%d, gen
erate
taosd token:%s"
,
pContext
,
pContext
->
fd
,
token
);
return
true
;
}
src/plugins/http/src/httpContext.c
浏览文件 @
eff4b580
...
...
@@ -27,19 +27,7 @@
#include "httpSql.h"
#include "httpSession.h"
#include "httpContext.h"
extern
bool
httpGetHttpMethod
(
HttpContext
*
pContext
);
extern
bool
httpParseURL
(
HttpContext
*
pContext
);
extern
bool
httpParseHttpVersion
(
HttpContext
*
pContext
);
extern
bool
httpGetDecodeMethod
(
HttpContext
*
pContext
);
extern
bool
httpParseHead
(
HttpContext
*
pContext
);
static
void
httpParseOnRequestLine
(
void
*
arg
,
const
char
*
method
,
const
char
*
target
,
const
char
*
version
,
const
char
*
target_raw
);
static
void
httpParseOnStatusLine
(
void
*
arg
,
const
char
*
version
,
int
status_code
,
const
char
*
reason_phrase
);
static
void
httpParseOnHeaderField
(
void
*
arg
,
const
char
*
key
,
const
char
*
val
);
static
void
httpParseOnBody
(
void
*
arg
,
const
char
*
chunk
,
size_t
len
);
static
void
httpParseOnEnd
(
void
*
arg
);
static
void
httpParseOnError
(
void
*
arg
,
int
status_code
);
#include "httpParser.h"
static
void
httpDestroyContext
(
void
*
data
);
...
...
@@ -70,9 +58,9 @@ static void httpDestroyContext(void *data) {
httpFreeJsonBuf
(
pContext
);
httpFreeMultiCmds
(
pContext
);
if
(
pContext
->
parser
.
parser
)
{
http
ParserDestroy
(
pContext
->
parser
.
parser
);
pContext
->
parser
.
parser
=
NULL
;
if
(
pContext
->
parser
)
{
http
DestroyParser
(
pContext
->
parser
);
pContext
->
parser
=
NULL
;
}
taosTFree
(
pContext
);
...
...
@@ -125,9 +113,9 @@ HttpContext *httpCreateContext(int32_t fd) {
if
(
pContext
==
NULL
)
return
NULL
;
pContext
->
fd
=
fd
;
pContext
->
httpVersion
=
HTTP_VERSION_10
;
pContext
->
lastAccessTime
=
taosGetTimestampSec
();
pContext
->
state
=
HTTP_CONTEXT_STATE_READY
;
pContext
->
parser
=
httpCreateParser
(
pContext
);
uint64_t
handleVal
=
(
uint64_t
)
pContext
;
HttpContext
**
ppContext
=
taosCachePut
(
tsHttpServer
.
contextCache
,
&
handleVal
,
sizeof
(
int64_t
),
&
pContext
,
sizeof
(
int64_t
),
3000
);
...
...
@@ -164,6 +152,7 @@ void httpReleaseContext(HttpContext *pContext) {
return
;
}
pContext
->
parser
->
inited
=
0
;
HttpContext
**
ppContext
=
pContext
->
ppContext
;
httpDebug
(
"context:%p, is released, data:%p refCount:%d"
,
pContext
,
ppContext
,
refCount
);
...
...
@@ -178,45 +167,24 @@ void httpReleaseContext(HttpContext *pContext) {
bool
httpInitContext
(
HttpContext
*
pContext
)
{
pContext
->
accessTimes
++
;
pContext
->
lastAccessTime
=
taosGetTimestampSec
();
pContext
->
httpVersion
=
HTTP_VERSION_10
;
pContext
->
httpKeepAlive
=
HTTP_KEEPALIVE_NO_INPUT
;
pContext
->
httpChunked
=
HTTP_UNCUNKED
;
pContext
->
acceptEncoding
=
HTTP_COMPRESS_IDENTITY
;
pContext
->
contentEncoding
=
HTTP_COMPRESS_IDENTITY
;
pContext
->
reqType
=
HTTP_REQTYPE_OTHERS
;
pContext
->
encodeMethod
=
NULL
;
pContext
->
timer
=
NULL
;
memset
(
&
pContext
->
singleCmd
,
0
,
sizeof
(
HttpSqlCmd
));
HttpParser
*
pParser
=
&
pContext
->
parser
;
memset
(
pParser
,
0
,
sizeof
(
HttpParser
));
pParser
->
pCur
=
pParser
->
pLast
=
pParser
->
buffer
;
HttpParserCallbackObj
callbacks
=
{
httpParseOnRequestLine
,
httpParseOnStatusLine
,
httpParseOnHeaderField
,
httpParseOnBody
,
httpParseOnEnd
,
httpParseOnError
};
HttpParserConfObj
conf
=
{
.
flush_block_size
=
0
};
pParser
->
parser
=
httpParserCreate
(
callbacks
,
conf
,
pContext
);
pParser
->
inited
=
1
;
httpDebug
(
"context:%p, fd:%d, parsed:%d"
,
pContext
,
pContext
->
fd
,
pContext
->
parsed
);
return
true
;
}
void
httpCloseContextByApp
(
HttpContext
*
pContext
)
{
HttpParser
*
parser
=
pContext
->
parser
;
pContext
->
parsed
=
false
;
bool
keepAlive
=
true
;
if
(
p
Context
->
httpVersion
==
HTTP_VERSION_10
&&
pContext
->
httpK
eepAlive
!=
HTTP_KEEPALIVE_ENABLE
)
{
if
(
p
arser
->
httpVersion
==
HTTP_VERSION_10
&&
parser
->
k
eepAlive
!=
HTTP_KEEPALIVE_ENABLE
)
{
keepAlive
=
false
;
}
else
if
(
p
Context
->
httpVersion
!=
HTTP_VERSION_10
&&
pContext
->
httpK
eepAlive
==
HTTP_KEEPALIVE_DISABLE
)
{
}
else
if
(
p
arser
->
httpVersion
!=
HTTP_VERSION_10
&&
parser
->
k
eepAlive
==
HTTP_KEEPALIVE_DISABLE
)
{
keepAlive
=
false
;
}
else
{
}
...
...
@@ -262,134 +230,3 @@ void httpCloseContextByServer(HttpContext *pContext) {
pContext
->
parsed
=
false
;
httpRemoveContextFromEpoll
(
pContext
);
}
static
void
httpParseOnRequestLine
(
void
*
arg
,
const
char
*
method
,
const
char
*
target
,
const
char
*
version
,
const
char
*
target_raw
)
{
HttpContext
*
pContext
=
(
HttpContext
*
)
arg
;
HttpParser
*
pParser
=
&
pContext
->
parser
;
int
avail
=
sizeof
(
pParser
->
buffer
)
-
(
pParser
->
pLast
-
pParser
->
buffer
);
int
n
=
snprintf
(
pParser
->
pLast
,
avail
,
"%s %s %s
\r\n
"
,
method
,
target_raw
,
version
);
char
*
last
=
pParser
->
pLast
;
do
{
if
(
n
>=
avail
)
{
httpDebug
(
"context:%p, fd:%d, request line(%s,%s,%s,%s), exceeding buffer size"
,
pContext
,
pContext
->
fd
,
method
,
target
,
version
,
target_raw
);
break
;
}
pParser
->
bufsize
+=
n
;
if
(
!
httpGetHttpMethod
(
pContext
))
{
httpDebug
(
"context:%p, fd:%d, request line(%s,%s,%s,%s), parse http method failed"
,
pContext
,
pContext
->
fd
,
method
,
target
,
version
,
target_raw
);
break
;
}
if
(
!
httpParseURL
(
pContext
))
{
httpDebug
(
"context:%p, fd:%d, request line(%s,%s,%s,%s), parse http url failed"
,
pContext
,
pContext
->
fd
,
method
,
target
,
version
,
target_raw
);
break
;
}
if
(
!
httpParseHttpVersion
(
pContext
))
{
httpDebug
(
"context:%p, fd:%d, request line(%s,%s,%s,%s), parse http version failed"
,
pContext
,
pContext
->
fd
,
method
,
target
,
version
,
target_raw
);
break
;
}
if
(
!
httpGetDecodeMethod
(
pContext
))
{
httpDebug
(
"context:%p, fd:%d, request line(%s,%s,%s,%s), get decode method failed"
,
pContext
,
pContext
->
fd
,
method
,
target
,
version
,
target_raw
);
break
;
}
last
+=
n
;
pParser
->
pLast
=
last
;
return
;
}
while
(
0
);
pParser
->
failed
|=
EHTTP_CONTEXT_PROCESS_FAILED
;
}
static
void
httpParseOnStatusLine
(
void
*
arg
,
const
char
*
version
,
int
status_code
,
const
char
*
reason_phrase
)
{
HttpContext
*
pContext
=
(
HttpContext
*
)
arg
;
HttpParser
*
pParser
=
&
pContext
->
parser
;
httpDebug
(
"context:%p, fd:%d, failed to parse status line "
,
pContext
,
pContext
->
fd
);
pParser
->
failed
|=
EHTTP_CONTEXT_PROCESS_FAILED
;
}
static
void
httpParseOnHeaderField
(
void
*
arg
,
const
char
*
key
,
const
char
*
val
)
{
HttpContext
*
pContext
=
(
HttpContext
*
)
arg
;
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
pParser
->
failed
)
return
;
httpDebug
(
"context:%p, fd:%d, key:%s val:%s"
,
pContext
,
pContext
->
fd
,
key
,
val
);
int
avail
=
sizeof
(
pParser
->
buffer
)
-
(
pParser
->
pLast
-
pParser
->
buffer
);
int
n
=
snprintf
(
pParser
->
pLast
,
avail
,
"%s: %s
\r\n
"
,
key
,
val
);
char
*
last
=
pParser
->
pLast
;
do
{
if
(
n
>=
avail
)
{
httpDebug
(
"context:%p, fd:%d, header field(%s,%s), exceeding buffer size"
,
pContext
,
pContext
->
fd
,
key
,
val
);
break
;
}
pParser
->
bufsize
+=
n
;
pParser
->
pCur
=
pParser
->
pLast
+
n
;
if
(
!
httpParseHead
(
pContext
))
{
httpDebug
(
"context:%p, fd:%d, header field(%s,%s), parse failed"
,
pContext
,
pContext
->
fd
,
key
,
val
);
break
;
}
last
+=
n
;
pParser
->
pLast
=
last
;
return
;
}
while
(
0
);
pParser
->
failed
|=
EHTTP_CONTEXT_PROCESS_FAILED
;
}
static
void
httpParseOnBody
(
void
*
arg
,
const
char
*
chunk
,
size_t
len
)
{
HttpContext
*
pContext
=
(
HttpContext
*
)
arg
;
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
pParser
->
failed
)
return
;
if
(
pParser
->
data
.
pos
==
0
)
{
pParser
->
data
.
pos
=
pParser
->
pLast
;
pParser
->
data
.
len
=
0
;
}
int
avail
=
sizeof
(
pParser
->
buffer
)
-
(
pParser
->
pLast
-
pParser
->
buffer
);
if
(
len
+
1
>=
avail
)
{
httpError
(
"context:%p, fd:%d, failed parse body, exceeding buffer size"
,
pContext
,
pContext
->
fd
);
pParser
->
failed
|=
EHTTP_CONTEXT_PROCESS_FAILED
;
return
;
}
memcpy
(
pParser
->
pLast
,
chunk
,
len
);
pParser
->
pLast
+=
len
;
pParser
->
data
.
len
+=
len
;
}
static
void
httpParseOnEnd
(
void
*
arg
)
{
HttpContext
*
pContext
=
(
HttpContext
*
)
arg
;
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
pParser
->
failed
)
return
;
if
(
pParser
->
data
.
pos
==
0
)
pParser
->
data
.
pos
=
pParser
->
pLast
;
if
(
!
pContext
->
parsed
)
{
pContext
->
parsed
=
true
;
}
httpDebug
(
"context:%p, fd:%d, parse success"
,
pContext
,
pContext
->
fd
);
}
static
void
httpParseOnError
(
void
*
arg
,
int
status_code
)
{
HttpContext
*
pContext
=
(
HttpContext
*
)
arg
;
HttpParser
*
pParser
=
&
pContext
->
parser
;
httpError
(
"context:%p, fd:%d, failed to parse, status_code:%d"
,
pContext
,
pContext
->
fd
,
status_code
);
pParser
->
failed
|=
EHTTP_CONTEXT_PARSER_FAILED
;
}
src/plugins/http/src/httpGcHandle.c
浏览文件 @
eff4b580
...
...
@@ -47,22 +47,22 @@ static HttpEncodeMethod gcQueryMethod = {
void
gcInitHandle
(
HttpServer
*
pServer
)
{
httpAddMethod
(
pServer
,
&
gcDecodeMethod
);
}
bool
gcGetUserFromUrl
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
pParser
->
path
[
GC_USER_URL_POS
].
len
>=
TSDB_USER_LEN
||
pParser
->
path
[
GC_USER_URL_POS
].
len
<=
0
)
{
HttpParser
*
pParser
=
pContext
->
parser
;
if
(
pParser
->
path
[
GC_USER_URL_POS
].
pos
>=
TSDB_USER_LEN
||
pParser
->
path
[
GC_USER_URL_POS
].
pos
<=
0
)
{
return
false
;
}
tstrncpy
(
pContext
->
user
,
pParser
->
path
[
GC_USER_URL_POS
].
pos
,
TSDB_USER_LEN
);
tstrncpy
(
pContext
->
user
,
pParser
->
path
[
GC_USER_URL_POS
].
str
,
TSDB_USER_LEN
);
return
true
;
}
bool
gcGetPassFromUrl
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
pParser
->
path
[
GC_PASS_URL_POS
].
len
>=
TSDB_PASSWORD_LEN
||
pParser
->
path
[
GC_PASS_URL_POS
].
len
<=
0
)
{
HttpParser
*
pParser
=
pContext
->
parser
;
if
(
pParser
->
path
[
GC_PASS_URL_POS
].
pos
>=
TSDB_PASSWORD_LEN
||
pParser
->
path
[
GC_PASS_URL_POS
].
pos
<=
0
)
{
return
false
;
}
tstrncpy
(
pContext
->
pass
,
pParser
->
path
[
GC_PASS_URL_POS
].
pos
,
TSDB_PASSWORD_LEN
);
tstrncpy
(
pContext
->
pass
,
pParser
->
path
[
GC_PASS_URL_POS
].
str
,
TSDB_PASSWORD_LEN
);
return
true
;
}
...
...
@@ -144,8 +144,7 @@ bool gcProcessLoginRequest(HttpContext* pContext) {
bool
gcProcessQueryRequest
(
HttpContext
*
pContext
)
{
httpDebug
(
"context:%p, fd:%d, process grafana query msg"
,
pContext
,
pContext
->
fd
);
HttpParser
*
pParser
=
&
pContext
->
parser
;
char
*
filter
=
pParser
->
data
.
pos
;
char
*
filter
=
pContext
->
parser
->
body
.
str
;
if
(
filter
==
NULL
)
{
httpSendErrorResp
(
pContext
,
HTTP_NO_MSG_INPUT
);
return
false
;
...
...
@@ -157,7 +156,7 @@ bool gcProcessQueryRequest(HttpContext* pContext) {
return
false
;
}
int
size
=
cJSON_GetArraySize
(
root
);
int
32_t
size
=
cJSON_GetArraySize
(
root
);
if
(
size
<=
0
)
{
httpSendErrorResp
(
pContext
,
HTTP_GC_QUERY_NULL
);
cJSON_Delete
(
root
);
...
...
@@ -176,7 +175,7 @@ bool gcProcessQueryRequest(HttpContext* pContext) {
return
false
;
}
for
(
int
i
=
0
;
i
<
size
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
size
;
++
i
)
{
cJSON
*
query
=
cJSON_GetArrayItem
(
root
,
i
);
if
(
query
==
NULL
)
continue
;
...
...
@@ -186,14 +185,14 @@ bool gcProcessQueryRequest(HttpContext* pContext) {
continue
;
}
int
refIdBuffer
=
httpAddToSqlCmdBuffer
(
pContext
,
refId
->
valuestring
);
int
32_t
refIdBuffer
=
httpAddToSqlCmdBuffer
(
pContext
,
refId
->
valuestring
);
if
(
refIdBuffer
==
-
1
)
{
httpWarn
(
"context:%p, fd:%d, user:%s, refId buffer is full"
,
pContext
,
pContext
->
fd
,
pContext
->
user
);
break
;
}
cJSON
*
alias
=
cJSON_GetObjectItem
(
query
,
"alias"
);
int
aliasBuffer
=
-
1
;
int
32_t
aliasBuffer
=
-
1
;
if
(
!
(
alias
==
NULL
||
alias
->
valuestring
==
NULL
||
strlen
(
alias
->
valuestring
)
==
0
))
{
aliasBuffer
=
httpAddToSqlCmdBuffer
(
pContext
,
alias
->
valuestring
);
if
(
aliasBuffer
==
-
1
)
{
...
...
@@ -211,7 +210,7 @@ bool gcProcessQueryRequest(HttpContext* pContext) {
continue
;
}
int
sqlBuffer
=
httpAddToSqlCmdBuffer
(
pContext
,
sql
->
valuestring
);
int
32_t
sqlBuffer
=
httpAddToSqlCmdBuffer
(
pContext
,
sql
->
valuestring
);
if
(
sqlBuffer
==
-
1
)
{
httpWarn
(
"context:%p, fd:%d, user:%s, sql buffer is full"
,
pContext
,
pContext
->
fd
,
pContext
->
user
);
break
;
...
...
src/plugins/http/src/httpGcJson.c
浏览文件 @
eff4b580
...
...
@@ -54,8 +54,8 @@ void gcWriteTargetStartJson(JsonBuf *jsonBuf, char *refId, char *target) {
httpJsonToken
(
jsonBuf
,
JsonObjStt
);
// target section
httpJsonPair
(
jsonBuf
,
"refId"
,
5
,
refId
,
(
int
)
strlen
(
refId
));
httpJsonPair
(
jsonBuf
,
"target"
,
6
,
target
,
(
int
)
strlen
(
target
));
httpJsonPair
(
jsonBuf
,
"refId"
,
5
,
refId
,
(
int
32_t
)
strlen
(
refId
));
httpJsonPair
(
jsonBuf
,
"target"
,
6
,
target
,
(
int
32_t
)
strlen
(
target
));
// data begin
httpJsonPairHead
(
jsonBuf
,
"datapoints"
,
10
);
...
...
@@ -82,25 +82,25 @@ void gcStopQueryJson(HttpContext *pContext, HttpSqlCmd *cmd) {
}
}
bool
gcBuildQueryJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
numOfRows
)
{
bool
gcBuildQueryJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
32_t
numOfRows
)
{
JsonBuf
*
jsonBuf
=
httpMallocJsonBuf
(
pContext
);
if
(
jsonBuf
==
NULL
)
return
false
;
int
num_fields
=
taos_num_fields
(
result
);
int
32_t
num_fields
=
taos_num_fields
(
result
);
TAOS_FIELD
*
fields
=
taos_fetch_fields
(
result
);
if
(
num_fields
==
0
)
{
return
false
;
}
int
precision
=
taos_result_precision
(
result
);
int
32_t
precision
=
taos_result_precision
(
result
);
// such as select count(*) from sys.cpu
// such as select count(*) from sys.cpu group by ipaddr
// such as select count(*) from sys.cpu interval(1d)
// such as select count(*) from sys.cpu interval(1d) group by ipaddr
// such as select count(*) count(*) from sys.cpu group by ipaddr interval(1d)
int
dataFields
=
-
1
;
int
groupFields
=
-
1
;
int
32_t
dataFields
=
-
1
;
int
32_t
groupFields
=
-
1
;
bool
hasTimestamp
=
fields
[
0
].
type
==
TSDB_DATA_TYPE_TIMESTAMP
;
if
(
hasTimestamp
)
{
dataFields
=
1
;
...
...
@@ -119,7 +119,7 @@ bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
}
cmd
->
numOfRows
+=
numOfRows
;
for
(
int
k
=
0
;
k
<
numOfRows
;
++
k
)
{
for
(
int
32_t
k
=
0
;
k
<
numOfRows
;
++
k
)
{
TAOS_ROW
row
=
taos_fetch_row
(
result
);
if
(
row
==
NULL
)
{
cmd
->
numOfRows
--
;
...
...
@@ -130,9 +130,9 @@ bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
// for group by
if
(
groupFields
!=
-
1
)
{
char
target
[
HTTP_GC_TARGET_SIZE
]
=
{
0
};
int
len
;
int
32_t
len
;
len
=
snprintf
(
target
,
HTTP_GC_TARGET_SIZE
,
"%s{"
,
aliasBuffer
);
for
(
int
i
=
dataFields
+
1
;
i
<
num_fields
;
i
++
){
for
(
int
32_t
i
=
dataFields
+
1
;
i
<
num_fields
;
i
++
){
switch
(
fields
[
i
].
type
)
{
case
TSDB_DATA_TYPE_BOOL
:
case
TSDB_DATA_TYPE_TINYINT
:
...
...
@@ -188,7 +188,7 @@ bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
httpJsonItemToken
(
jsonBuf
);
httpJsonToken
(
jsonBuf
,
JsonArrStt
);
for
(
int
i
=
dataFields
;
i
>=
0
;
i
--
)
{
for
(
int
32_t
i
=
dataFields
;
i
>=
0
;
i
--
)
{
httpJsonItemToken
(
jsonBuf
);
if
(
row
[
i
]
==
NULL
)
{
httpJsonOriginString
(
jsonBuf
,
"null"
,
4
);
...
...
@@ -253,13 +253,13 @@ void gcSendHeartBeatResp(HttpContext *pContext, HttpSqlCmd *cmd) {
httpInitJsonBuf
(
jsonBuf
,
pContext
);
httpJsonToken
(
jsonBuf
,
JsonObjStt
);
httpJsonPair
(
jsonBuf
,
"message"
,
(
int
)
strlen
(
"message"
),
desc
,
(
in
t
)
strlen
(
desc
));
httpJsonPair
(
jsonBuf
,
"message"
,
(
int
32_t
)
strlen
(
"message"
),
desc
,
(
int32_
t
)
strlen
(
desc
));
httpJsonToken
(
jsonBuf
,
JsonObjEnd
);
char
head
[
1024
];
int
hLen
=
sprintf
(
head
,
httpRespTemplate
[
HTTP_RESPONSE_GRAFANA
],
httpVersionStr
[
pContext
->
httpVersion
],
httpKeepAliveStr
[
pContext
->
httpK
eepAlive
],
(
jsonBuf
->
lst
-
jsonBuf
->
buf
));
int
32_t
hLen
=
sprintf
(
head
,
httpRespTemplate
[
HTTP_RESPONSE_GRAFANA
],
httpVersionStr
[
pContext
->
parser
->
httpVersion
],
httpKeepAliveStr
[
pContext
->
parser
->
k
eepAlive
],
(
jsonBuf
->
lst
-
jsonBuf
->
buf
));
httpWriteBuf
(
pContext
,
head
,
hLen
);
httpWriteBuf
(
pContext
,
jsonBuf
->
buf
,
(
int
)(
jsonBuf
->
lst
-
jsonBuf
->
buf
));
httpWriteBuf
(
pContext
,
jsonBuf
->
buf
,
(
int
32_t
)(
jsonBuf
->
lst
-
jsonBuf
->
buf
));
}
src/plugins/http/src/httpGzip.c
浏览文件 @
eff4b580
/*
* 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 "os.h"
#include "zlib.h"
#include "httpGzip.h"
...
...
@@ -16,10 +32,10 @@ struct ehttp_gzip_s {
gz_header
*
header
;
char
*
chunk
;
int
state
;
int
32_t
state
;
};
static
void
dummy_on_data
(
ehttp_gzip_t
*
gzip
,
void
*
arg
,
const
char
*
buf
,
size
_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
)
{
...
...
@@ -72,7 +88,7 @@ ehttp_gzip_t* ehttp_gzip_create_decompressor(ehttp_gzip_conf_t conf, ehttp_gzip_
// 868 below), inflate() will not automatically decode concatenated gzip streams.
// 869 inflate() will return Z_STREAM_END at the end of the gzip stream. The state
// 870 would need to be reset to continue decoding a subsequent gzip stream.
int
ret
=
inflateInit2
(
gzip
->
gzip
,
32
);
// 32/16? 32/16 + MAX_WBITS
int
32_t
ret
=
inflateInit2
(
gzip
->
gzip
,
32
);
// 32/16? 32/16 + MAX_WBITS
if
(
ret
!=
Z_OK
)
break
;
if
(
gzip
->
header
)
{
ret
=
inflateGetHeader
(
gzip
->
gzip
,
gzip
->
header
);
...
...
@@ -97,7 +113,7 @@ void ehttp_gzip_destroy(ehttp_gzip_t *gzip) {
free
(
gzip
);
}
int
ehttp_gzip_write
(
ehttp_gzip_t
*
gzip
,
const
char
*
buf
,
size
_t
len
)
{
int
32_t
ehttp_gzip_write
(
ehttp_gzip_t
*
gzip
,
const
char
*
buf
,
int32
_t
len
)
{
if
(
gzip
->
state
!=
EHTTP_GZIP_READY
)
return
-
1
;
if
(
len
<=
0
)
return
0
;
...
...
@@ -105,7 +121,7 @@ int ehttp_gzip_write(ehttp_gzip_t *gzip, const char *buf, size_t len) {
gzip
->
gzip
->
avail_in
=
len
;
while
(
gzip
->
gzip
->
avail_in
)
{
int
ret
;
int
32_t
ret
;
if
(
gzip
->
header
)
{
ret
=
inflate
(
gzip
->
gzip
,
Z_BLOCK
);
}
else
{
...
...
@@ -117,7 +133,7 @@ int ehttp_gzip_write(ehttp_gzip_t *gzip, const char *buf, size_t len) {
if
(
ret
!=
Z_STREAM_END
)
continue
;
}
size
_t
len
=
gzip
->
gzip
->
next_out
-
(
z_const
Bytef
*
)
gzip
->
chunk
;
int32
_t
len
=
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
);
...
...
@@ -128,18 +144,18 @@ int ehttp_gzip_write(ehttp_gzip_t *gzip, const char *buf, size_t len) {
return
0
;
}
int
ehttp_gzip_finish
(
ehttp_gzip_t
*
gzip
)
{
int
32_t
ehttp_gzip_finish
(
ehttp_gzip_t
*
gzip
)
{
if
(
gzip
->
state
!=
EHTTP_GZIP_READY
)
return
-
1
;
gzip
->
gzip
->
next_in
=
NULL
;
gzip
->
gzip
->
avail_in
=
0
;
int
ret
;
int
32_t
ret
;
ret
=
inflate
(
gzip
->
gzip
,
Z_FINISH
);
if
(
ret
!=
Z_STREAM_END
)
return
-
1
;
size
_t
len
=
gzip
->
gzip
->
next_out
-
(
z_const
Bytef
*
)
gzip
->
chunk
;
int32
_t
len
=
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
);
...
...
src/plugins/http/src/httpHandle.c
浏览文件 @
eff4b580
...
...
@@ -15,240 +15,17 @@
#define _DEFAULT_SOURCE
#include "os.h"
#include "taos.h"
#include "tglobal.h"
#include "tsocket.h"
#include "ttimer.h"
#include "httpInt.h"
#include "httpResp.h"
#include "httpAuth.h"
#include "httpServer.h"
#include "httpContext.h"
#include "httpHandle.h"
void
httpToLowerUrl
(
char
*
url
)
{
/*ignore case */
while
(
*
url
)
{
if
(
*
url
>=
'A'
&&
*
url
<=
'Z'
)
{
*
url
=
*
url
|
0x20
;
}
url
++
;
}
}
bool
httpUrlMatch
(
HttpContext
*
pContext
,
int
pos
,
char
*
cmp
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
pos
<
0
||
pos
>=
HTTP_MAX_URL
)
{
return
false
;
}
if
(
pParser
->
path
[
pos
].
len
<=
0
)
{
return
false
;
}
if
(
strcmp
(
pParser
->
path
[
pos
].
pos
,
cmp
)
!=
0
)
{
return
false
;
}
return
true
;
}
// /account/db/meter HTTP/1.1\r\nHost
bool
httpParseURL
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
char
*
pSeek
;
char
*
pEnd
=
strchr
(
pParser
->
pLast
,
' '
);
if
(
pEnd
==
NULL
)
{
httpSendErrorResp
(
pContext
,
HTTP_UNSUPPORT_URL
);
return
false
;
}
if
(
*
pParser
->
pLast
!=
'/'
)
{
httpSendErrorResp
(
pContext
,
HTTP_UNSUPPORT_URL
);
return
false
;
}
pParser
->
pLast
++
;
for
(
int
i
=
0
;
i
<
HTTP_MAX_URL
;
i
++
)
{
pSeek
=
strchr
(
pParser
->
pLast
,
'/'
);
if
(
pSeek
==
NULL
)
{
break
;
}
pParser
->
path
[
i
].
pos
=
pParser
->
pLast
;
if
(
pSeek
<=
pEnd
)
{
pParser
->
path
[
i
].
len
=
(
int16_t
)(
pSeek
-
pParser
->
pLast
);
pParser
->
path
[
i
].
pos
[
pParser
->
path
[
i
].
len
]
=
0
;
httpToLowerUrl
(
pParser
->
path
[
i
].
pos
);
pParser
->
pLast
=
pSeek
+
1
;
}
else
{
pParser
->
path
[
i
].
len
=
(
int16_t
)(
pEnd
-
pParser
->
pLast
);
pParser
->
path
[
i
].
pos
[
pParser
->
path
[
i
].
len
]
=
0
;
httpToLowerUrl
(
pParser
->
path
[
i
].
pos
);
pParser
->
pLast
=
pEnd
+
1
;
break
;
}
}
pParser
->
pLast
=
pEnd
+
1
;
if
(
pParser
->
path
[
0
].
len
==
0
)
{
httpSendErrorResp
(
pContext
,
HTTP_UNSUPPORT_URL
);
return
false
;
}
return
true
;
}
bool
httpParseHttpVersion
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
char
*
pEnd
=
strchr
(
pParser
->
pLast
,
'1'
);
if
(
pEnd
==
NULL
)
{
httpError
(
"context:%p, fd:%d, can't find http version at position:%s"
,
pContext
,
pContext
->
fd
,
pParser
->
pLast
);
httpSendErrorResp
(
pContext
,
HTTP_PARSE_HTTP_VERSION_ERROR
);
return
false
;
}
if
(
*
(
pEnd
+
1
)
!=
'.'
)
{
httpError
(
"context:%p, fd:%d, can't find http version at position:%s"
,
pContext
,
pContext
->
fd
,
pParser
->
pLast
);
httpSendErrorResp
(
pContext
,
HTTP_PARSE_HTTP_VERSION_ERROR
);
return
false
;
}
if
(
*
(
pEnd
+
2
)
==
'0'
)
pContext
->
httpVersion
=
HTTP_VERSION_10
;
else
if
(
*
(
pEnd
+
2
)
==
'1'
)
pContext
->
httpVersion
=
HTTP_VERSION_11
;
else
if
(
*
(
pEnd
+
2
)
==
'2'
)
pContext
->
httpVersion
=
HTTP_VERSION_11
;
else
pContext
->
httpVersion
=
HTTP_VERSION_10
;
httpDebug
(
"context:%p, fd:%d, httpVersion:1.%d"
,
pContext
,
pContext
->
fd
,
pContext
->
httpVersion
);
return
true
;
}
bool
httpGetNextLine
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
while
(
pParser
->
buffer
+
pParser
->
bufsize
-
pParser
->
pCur
++
>
0
)
{
if
(
*
(
pParser
->
pCur
)
==
'\n'
&&
*
(
pParser
->
pCur
-
1
)
==
'\r'
)
{
// cut the string
*
pParser
->
pCur
=
0
;
return
true
;
}
}
httpSendErrorResp
(
pContext
,
HTTP_PARSE_HEAD_ERROR
);
return
false
;
}
bool
httpGetHttpMethod
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
char
*
pSeek
=
strchr
(
pParser
->
pLast
,
' '
);
if
(
pSeek
==
NULL
)
{
httpError
(
"context:%p, fd:%d, failed to parse httpMethod"
,
pContext
,
pContext
->
fd
);
httpSendErrorResp
(
pContext
,
HTTP_PARSE_HTTP_METHOD_ERROR
);
return
false
;
}
pParser
->
method
.
pos
=
pParser
->
pLast
;
pParser
->
method
.
len
=
(
int16_t
)(
pSeek
-
pParser
->
pLast
);
pParser
->
method
.
pos
[
pParser
->
method
.
len
]
=
0
;
pParser
->
pLast
=
pSeek
+
1
;
httpTrace
(
"context:%p, fd:%d, httpMethod:%s"
,
pContext
,
pContext
->
fd
,
pParser
->
method
.
pos
);
return
true
;
}
bool
httpGetDecodeMethod
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
HttpServer
*
pServer
=
&
tsHttpServer
;
int
methodLen
=
pServer
->
methodScannerLen
;
for
(
int
i
=
0
;
i
<
methodLen
;
i
++
)
{
HttpDecodeMethod
*
method
=
pServer
->
methodScanner
[
i
];
if
(
strcmp
(
method
->
module
,
pParser
->
path
[
0
].
pos
)
!=
0
)
{
continue
;
}
pParser
->
pMethod
=
method
;
return
true
;
}
httpError
(
"context:%p, fd:%d, error:the url is not support, method:%s, path:%s"
,
pContext
,
pContext
->
fd
,
pParser
->
method
.
pos
,
pParser
->
path
[
0
].
pos
);
httpSendErrorResp
(
pContext
,
HTTP_UNSUPPORT_URL
);
return
false
;
}
bool
httpParseHead
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
strncasecmp
(
pParser
->
pLast
,
"Content-Length: "
,
16
)
==
0
)
{
pParser
->
data
.
len
=
(
int32_t
)
atoi
(
pParser
->
pLast
+
16
);
httpTrace
(
"context:%p, fd:%d, Content-Length:%d"
,
pContext
,
pContext
->
fd
,
pParser
->
data
.
len
);
}
else
if
(
strncasecmp
(
pParser
->
pLast
,
"Accept-Encoding: "
,
17
)
==
0
)
{
if
(
tsHttpEnableCompress
&&
strstr
(
pParser
->
pLast
+
17
,
"gzip"
)
!=
NULL
)
{
pContext
->
acceptEncoding
=
HTTP_COMPRESS_GZIP
;
httpTrace
(
"context:%p, fd:%d, Accept-Encoding:gzip"
,
pContext
,
pContext
->
fd
);
}
else
{
pContext
->
acceptEncoding
=
HTTP_COMPRESS_IDENTITY
;
httpTrace
(
"context:%p, fd:%d, Accept-Encoding:identity"
,
pContext
,
pContext
->
fd
);
}
}
else
if
(
strncasecmp
(
pParser
->
pLast
,
"Content-Encoding: "
,
18
)
==
0
)
{
if
(
strstr
(
pParser
->
pLast
+
18
,
"gzip"
)
!=
NULL
)
{
pContext
->
contentEncoding
=
HTTP_COMPRESS_GZIP
;
httpTrace
(
"context:%p, fd:%d, Content-Encoding:gzip"
,
pContext
,
pContext
->
fd
);
}
else
{
pContext
->
contentEncoding
=
HTTP_COMPRESS_IDENTITY
;
httpTrace
(
"context:%p, fd:%d, Content-Encoding:identity"
,
pContext
,
pContext
->
fd
);
}
}
else
if
(
strncasecmp
(
pParser
->
pLast
,
"Connection: "
,
12
)
==
0
)
{
if
(
strncasecmp
(
pParser
->
pLast
+
12
,
"Keep-Alive"
,
10
)
==
0
)
{
pContext
->
httpKeepAlive
=
HTTP_KEEPALIVE_ENABLE
;
}
else
{
pContext
->
httpKeepAlive
=
HTTP_KEEPALIVE_DISABLE
;
}
httpTrace
(
"context:%p, fd:%d, keepAlive:%d"
,
pContext
,
pContext
->
fd
,
pContext
->
httpKeepAlive
);
}
else
if
(
strncasecmp
(
pParser
->
pLast
,
"Transfer-Encoding: "
,
19
)
==
0
)
{
if
(
strncasecmp
(
pParser
->
pLast
+
19
,
"chunked"
,
7
)
==
0
)
{
pContext
->
httpChunked
=
HTTP_CHUNKED
;
}
}
else
if
(
strncasecmp
(
pParser
->
pLast
,
"Authorization: "
,
15
)
==
0
)
{
if
(
strncasecmp
(
pParser
->
pLast
+
15
,
"Basic "
,
6
)
==
0
)
{
pParser
->
token
.
pos
=
pParser
->
pLast
+
21
;
pParser
->
token
.
len
=
(
int16_t
)(
pParser
->
pCur
-
pParser
->
token
.
pos
-
1
);
bool
parsed
=
httpParseBasicAuthToken
(
pContext
,
pParser
->
token
.
pos
,
pParser
->
token
.
len
);
if
(
!
parsed
)
{
httpSendErrorResp
(
pContext
,
HTTP_INVALID_BASIC_AUTH_TOKEN
);
return
false
;
}
}
else
if
(
strncasecmp
(
pParser
->
pLast
+
15
,
"Taosd "
,
6
)
==
0
)
{
pParser
->
token
.
pos
=
pParser
->
pLast
+
21
;
pParser
->
token
.
len
=
(
int16_t
)(
pParser
->
pCur
-
pParser
->
token
.
pos
-
1
);
bool
parsed
=
httpParseTaosdAuthToken
(
pContext
,
pParser
->
token
.
pos
,
pParser
->
token
.
len
);
if
(
!
parsed
)
{
httpSendErrorResp
(
pContext
,
HTTP_INVALID_TAOSD_AUTH_TOKEN
);
return
false
;
}
}
else
{
httpSendErrorResp
(
pContext
,
HTTP_INVALID_AUTH_TOKEN
);
return
false
;
}
}
else
{
}
return
true
;
}
bool
httpDecodeRequest
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
pParser
->
pMethod
->
decodeFp
==
NULL
)
{
if
(
pContext
->
decodeMethod
->
decodeFp
==
NULL
)
{
return
false
;
}
return
(
*
p
Parser
->
p
Method
->
decodeFp
)(
pContext
);
return
(
*
p
Context
->
decode
Method
->
decodeFp
)(
pContext
);
}
/**
...
...
@@ -263,7 +40,7 @@ bool httpProcessData(HttpContext* pContext) {
}
// handle Cross-domain request
if
(
strcmp
(
pContext
->
parser
.
method
.
pos
,
"OPTIONS"
)
==
0
)
{
if
(
strcmp
(
pContext
->
parser
->
method
,
"OPTIONS"
)
==
0
)
{
httpDebug
(
"context:%p, fd:%d, process options request"
,
pContext
,
pContext
->
fd
);
httpSendOptionResp
(
pContext
,
"process options request success"
);
}
else
{
...
...
src/plugins/http/src/httpJson.c
浏览文件 @
eff4b580
...
...
@@ -17,6 +17,7 @@
#include "os.h"
#include "taosmsg.h"
#include "taoserror.h"
#include "tglobal.h"
#include "http.h"
#include "httpLog.h"
#include "httpCode.h"
...
...
@@ -38,14 +39,14 @@ char JsonNulTkn[] = "null";
char
JsonTrueTkn
[]
=
"true"
;
char
JsonFalseTkn
[]
=
"false"
;
int
httpWriteBufByFd
(
struct
HttpContext
*
pContext
,
const
char
*
buf
,
in
t
sz
)
{
int
len
;
int
countWait
=
0
;
int
writeLen
=
0
;
int
32_t
httpWriteBufByFd
(
struct
HttpContext
*
pContext
,
const
char
*
buf
,
int32_
t
sz
)
{
int
32_t
len
;
int
32_t
countWait
=
0
;
int
32_t
writeLen
=
0
;
do
{
if
(
pContext
->
fd
>
2
){
len
=
(
int
)
taosSend
(
pContext
->
fd
,
buf
+
writeLen
,
(
size_t
)(
sz
-
writeLen
),
MSG_NOSIGNAL
);
len
=
(
int
32_t
)
taosSend
(
pContext
->
fd
,
buf
+
writeLen
,
(
size_t
)(
sz
-
writeLen
),
MSG_NOSIGNAL
);
}
else
{
return
sz
;
...
...
@@ -68,8 +69,8 @@ int httpWriteBufByFd(struct HttpContext* pContext, const char* buf, int sz) {
return
writeLen
;
}
int
httpWriteBuf
(
struct
HttpContext
*
pContext
,
const
char
*
buf
,
in
t
sz
)
{
int
writeSz
=
httpWriteBufByFd
(
pContext
,
buf
,
sz
);
int
32_t
httpWriteBuf
(
struct
HttpContext
*
pContext
,
const
char
*
buf
,
int32_
t
sz
)
{
int
32_t
writeSz
=
httpWriteBufByFd
(
pContext
,
buf
,
sz
);
if
(
writeSz
!=
sz
)
{
httpError
(
"context:%p, fd:%d, dataSize:%d, writeSize:%d, failed to send response:
\n
%s"
,
pContext
,
pContext
->
fd
,
sz
,
writeSz
,
buf
);
...
...
@@ -80,8 +81,8 @@ int httpWriteBuf(struct HttpContext* pContext, const char* buf, int sz) {
return
writeSz
;
}
int
httpWriteBufNoTrace
(
struct
HttpContext
*
pContext
,
const
char
*
buf
,
in
t
sz
)
{
int
writeSz
=
httpWriteBufByFd
(
pContext
,
buf
,
sz
);
int
32_t
httpWriteBufNoTrace
(
struct
HttpContext
*
pContext
,
const
char
*
buf
,
int32_
t
sz
)
{
int
32_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
,
writeSz
);
...
...
@@ -90,8 +91,8 @@ int httpWriteBufNoTrace(struct HttpContext *pContext, const char *buf, int sz) {
return
writeSz
;
}
int
httpWriteJsonBufBody
(
JsonBuf
*
buf
,
bool
isTheLast
)
{
int
remain
=
0
;
int
32_t
httpWriteJsonBufBody
(
JsonBuf
*
buf
,
bool
isTheLast
)
{
int
32_t
remain
=
0
;
char
sLen
[
24
];
uint64_t
srcLen
=
(
uint64_t
)
(
buf
->
lst
-
buf
->
buf
);
...
...
@@ -108,28 +109,28 @@ int httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) {
* The remote endpoint then decodes the stream by concatenating the chunks and uncompressing the result.
*/
if
(
buf
->
pContext
->
acceptEncoding
==
HTTP_COMPRESS_IDENTITY
)
{
if
(
buf
->
pContext
->
parser
->
acceptEncodingGzip
==
0
||
!
tsHttpEnableCompress
)
{
if
(
buf
->
lst
==
buf
->
buf
)
{
httpTrace
(
"context:%p, fd:%d, no data need dump"
,
buf
->
pContext
,
buf
->
pContext
->
fd
);
return
0
;
// there is no data to dump.
}
else
{
int
len
=
sprintf
(
sLen
,
"%lx
\r\n
"
,
srcLen
);
int
32_t
len
=
sprintf
(
sLen
,
"%lx
\r\n
"
,
srcLen
);
httpTrace
(
"context:%p, fd:%d, write body, chunkSize:%"
PRIu64
", response:
\n
%s"
,
buf
->
pContext
,
buf
->
pContext
->
fd
,
srcLen
,
buf
->
buf
);
httpWriteBufNoTrace
(
buf
->
pContext
,
sLen
,
len
);
remain
=
httpWriteBufNoTrace
(
buf
->
pContext
,
buf
->
buf
,
(
int
)
srcLen
);
remain
=
httpWriteBufNoTrace
(
buf
->
pContext
,
buf
->
buf
,
(
int
32_t
)
srcLen
);
}
}
else
{
char
compressBuf
[
JSON_BUFFER_SIZE
]
=
{
0
};
int32_t
compressBufLen
=
JSON_BUFFER_SIZE
;
int
ret
=
httpGzipCompress
(
buf
->
pContext
,
buf
->
buf
,
srcLen
,
compressBuf
,
&
compressBufLen
,
isTheLast
);
int
32_t
ret
=
httpGzipCompress
(
buf
->
pContext
,
buf
->
buf
,
srcLen
,
compressBuf
,
&
compressBufLen
,
isTheLast
);
if
(
ret
==
0
)
{
if
(
compressBufLen
>
0
)
{
int
len
=
sprintf
(
sLen
,
"%x
\r\n
"
,
compressBufLen
);
int
32_t
len
=
sprintf
(
sLen
,
"%x
\r\n
"
,
compressBufLen
);
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
);
httpWriteBufNoTrace
(
buf
->
pContext
,
sLen
,
len
);
remain
=
httpWriteBufNoTrace
(
buf
->
pContext
,
(
const
char
*
)
compressBuf
,
(
int
)
compressBufLen
);
remain
=
httpWriteBufNoTrace
(
buf
->
pContext
,
(
const
char
*
)
compressBuf
,
compressBufLen
);
}
else
{
httpTrace
(
"context:%p, fd:%d, last:%d, compress already dumped, response:
\n
%s"
,
buf
->
pContext
,
buf
->
pContext
->
fd
,
isTheLast
,
buf
->
buf
);
...
...
@@ -143,9 +144,9 @@ int httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) {
}
httpWriteBufNoTrace
(
buf
->
pContext
,
"
\r\n
"
,
2
);
buf
->
total
+=
(
int
)
(
buf
->
lst
-
buf
->
buf
);
buf
->
total
+=
(
int
32_t
)
(
buf
->
lst
-
buf
->
buf
);
buf
->
lst
=
buf
->
buf
;
memset
(
buf
->
buf
,
0
,
(
size_t
)
buf
->
size
);
memset
(
buf
->
buf
,
0
,
(
size_t
)
buf
->
size
);
return
remain
;
}
...
...
@@ -155,14 +156,14 @@ void httpWriteJsonBufHead(JsonBuf* buf) {
}
char
msg
[
1024
]
=
{
0
};
int
len
=
-
1
;
int
32_t
len
=
-
1
;
if
(
buf
->
pContext
->
acceptEncoding
==
HTTP_COMPRESS_IDENTITY
)
{
len
=
sprintf
(
msg
,
httpRespTemplate
[
HTTP_RESPONSE_CHUNKED_UN_COMPRESS
],
httpVersionStr
[
buf
->
pContext
->
httpVersion
],
httpKeepAliveStr
[
buf
->
pContext
->
httpK
eepAlive
]);
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
->
k
eepAlive
]);
}
else
{
len
=
sprintf
(
msg
,
httpRespTemplate
[
HTTP_RESPONSE_CHUNKED_COMPRESS
],
httpVersionStr
[
buf
->
pContext
->
httpVersion
],
httpKeepAliveStr
[
buf
->
pContext
->
httpK
eepAlive
]);
len
=
sprintf
(
msg
,
httpRespTemplate
[
HTTP_RESPONSE_CHUNKED_COMPRESS
],
httpVersionStr
[
buf
->
pContext
->
parser
->
httpVersion
],
httpKeepAliveStr
[
buf
->
pContext
->
parser
->
k
eepAlive
]);
}
httpWriteBuf
(
buf
->
pContext
,
(
const
char
*
)
msg
,
len
);
...
...
@@ -185,7 +186,7 @@ void httpInitJsonBuf(JsonBuf* buf, struct HttpContext* pContext) {
buf
->
pContext
=
pContext
;
memset
(
buf
->
lst
,
0
,
JSON_BUFFER_SIZE
);
if
(
pContext
->
acceptEncoding
==
HTTP_COMPRESS_GZIP
)
{
if
(
pContext
->
parser
->
acceptEncodingGzip
==
1
&&
tsHttpEnableCompress
)
{
httpGzipCompressInit
(
buf
->
pContext
);
}
...
...
@@ -200,19 +201,19 @@ void httpJsonItemToken(JsonBuf* buf) {
if
(
buf
->
lst
>
buf
->
buf
)
httpJsonToken
(
buf
,
JsonItmTkn
);
}
void
httpJsonString
(
JsonBuf
*
buf
,
char
*
sVal
,
int
len
)
{
void
httpJsonString
(
JsonBuf
*
buf
,
char
*
sVal
,
int
32_t
len
)
{
httpJsonItemToken
(
buf
);
httpJsonToken
(
buf
,
JsonStrStt
);
httpJsonPrint
(
buf
,
sVal
,
len
);
httpJsonToken
(
buf
,
JsonStrEnd
);
}
void
httpJsonOriginString
(
JsonBuf
*
buf
,
char
*
sVal
,
int
len
)
{
void
httpJsonOriginString
(
JsonBuf
*
buf
,
char
*
sVal
,
int
32_t
len
)
{
httpJsonItemToken
(
buf
);
httpJsonPrint
(
buf
,
sVal
,
len
);
}
void
httpJsonStringForTransMean
(
JsonBuf
*
buf
,
char
*
sVal
,
int
maxLen
)
{
void
httpJsonStringForTransMean
(
JsonBuf
*
buf
,
char
*
sVal
,
int
32_t
maxLen
)
{
httpJsonItemToken
(
buf
);
httpJsonToken
(
buf
,
JsonStrStt
);
...
...
@@ -221,18 +222,18 @@ void httpJsonStringForTransMean(JsonBuf* buf, char* sVal, int maxLen) {
char
*
lastPos
=
sVal
;
char
*
curPos
=
sVal
;
for
(
int
i
=
0
;
i
<
maxLen
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
maxLen
;
++
i
)
{
if
(
*
curPos
==
0
)
{
break
;
}
if
(
*
curPos
==
'\"'
)
{
httpJsonPrint
(
buf
,
lastPos
,
(
int
)(
curPos
-
lastPos
));
httpJsonPrint
(
buf
,
lastPos
,
(
int
32_t
)(
curPos
-
lastPos
));
curPos
++
;
lastPos
=
curPos
;
httpJsonPrint
(
buf
,
"
\\\"
"
,
2
);
}
else
if
(
*
curPos
==
'\\'
)
{
httpJsonPrint
(
buf
,
lastPos
,
(
int
)(
curPos
-
lastPos
));
httpJsonPrint
(
buf
,
lastPos
,
(
int
32_t
)(
curPos
-
lastPos
));
curPos
++
;
lastPos
=
curPos
;
httpJsonPrint
(
buf
,
"
\\\\
"
,
2
);
...
...
@@ -242,7 +243,7 @@ void httpJsonStringForTransMean(JsonBuf* buf, char* sVal, int maxLen) {
}
if
(
*
lastPos
)
{
httpJsonPrint
(
buf
,
lastPos
,
(
int
)(
curPos
-
lastPos
));
httpJsonPrint
(
buf
,
lastPos
,
(
int
32_t
)(
curPos
-
lastPos
));
}
}
...
...
@@ -258,14 +259,14 @@ void httpJsonInt64(JsonBuf* buf, int64_t num) {
void
httpJsonTimestamp
(
JsonBuf
*
buf
,
int64_t
t
,
bool
us
)
{
char
ts
[
35
]
=
{
0
};
struct
tm
*
ptm
;
int
precision
=
1000
;
int
32_t
precision
=
1000
;
if
(
us
)
{
precision
=
1000000
;
}
time_t
tt
=
t
/
precision
;
ptm
=
localtime
(
&
tt
);
int
length
=
(
in
t
)
strftime
(
ts
,
35
,
"%Y-%m-%d %H:%M:%S"
,
ptm
);
int
32_t
length
=
(
int32_
t
)
strftime
(
ts
,
35
,
"%Y-%m-%d %H:%M:%S"
,
ptm
);
if
(
us
)
{
length
+=
snprintf
(
ts
+
length
,
8
,
".%06ld"
,
t
%
precision
);
}
else
{
...
...
@@ -278,25 +279,25 @@ 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
;
int
precision
=
1000
;
int
32_t
precision
=
1000
;
if
(
us
)
{
precision
=
1000000
;
}
time_t
tt
=
t
/
precision
;
ptm
=
localtime
(
&
tt
);
int
length
=
(
int
)
strftime
(
ts
,
40
,
"%Y-%m-%dT%H:%M:%S"
,
ptm
);
int
32_t
length
=
(
int32_t
)
strftime
(
ts
,
40
,
"%Y-%m-%dT%H:%M:%S"
,
ptm
);
if
(
us
)
{
length
+=
snprintf
(
ts
+
length
,
8
,
".%06ld"
,
t
%
precision
);
}
else
{
length
+=
snprintf
(
ts
+
length
,
5
,
".%03ld"
,
t
%
precision
);
}
length
+=
(
int
)
strftime
(
ts
+
length
,
40
-
length
,
"%z"
,
ptm
);
length
+=
(
int
32_t
)
strftime
(
ts
+
length
,
40
-
length
,
"%z"
,
ptm
);
httpJsonString
(
buf
,
ts
,
length
);
}
void
httpJsonInt
(
JsonBuf
*
buf
,
int
num
)
{
void
httpJsonInt
(
JsonBuf
*
buf
,
int
32_t
num
)
{
httpJsonItemToken
(
buf
);
httpJsonTestBuf
(
buf
,
MAX_NUM_STR_SZ
);
buf
->
lst
+=
snprintf
(
buf
->
lst
,
MAX_NUM_STR_SZ
,
"%d"
,
num
);
...
...
@@ -328,65 +329,65 @@ void httpJsonDouble(JsonBuf* buf, double num) {
void
httpJsonNull
(
JsonBuf
*
buf
)
{
httpJsonString
(
buf
,
"null"
,
4
);
}
void
httpJsonBool
(
JsonBuf
*
buf
,
int
val
)
{
void
httpJsonBool
(
JsonBuf
*
buf
,
int
32_t
val
)
{
if
(
val
==
0
)
httpJsonPrint
(
buf
,
JsonFalseTkn
,
sizeof
(
JsonFalseTkn
));
else
httpJsonPrint
(
buf
,
JsonTrueTkn
,
sizeof
(
JsonTrueTkn
));
}
void
httpJsonPairHead
(
JsonBuf
*
buf
,
char
*
name
,
int
len
)
{
void
httpJsonPairHead
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
len
)
{
httpJsonItemToken
(
buf
);
httpJsonString
(
buf
,
name
,
len
);
httpJsonToken
(
buf
,
JsonPairTkn
);
}
void
httpJsonPair
(
JsonBuf
*
buf
,
char
*
name
,
int
nameLen
,
char
*
sVal
,
in
t
valLen
)
{
void
httpJsonPair
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nameLen
,
char
*
sVal
,
int32_
t
valLen
)
{
httpJsonPairHead
(
buf
,
name
,
nameLen
);
httpJsonString
(
buf
,
sVal
,
valLen
);
}
void
httpJsonPairOriginString
(
JsonBuf
*
buf
,
char
*
name
,
int
nameLen
,
char
*
sVal
,
in
t
valLen
)
{
void
httpJsonPairOriginString
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nameLen
,
char
*
sVal
,
int32_
t
valLen
)
{
httpJsonPairHead
(
buf
,
name
,
nameLen
);
httpJsonOriginString
(
buf
,
sVal
,
valLen
);
}
void
httpJsonPairIntVal
(
JsonBuf
*
buf
,
char
*
name
,
int
nNameLen
,
in
t
num
)
{
void
httpJsonPairIntVal
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nNameLen
,
int32_
t
num
)
{
httpJsonPairHead
(
buf
,
name
,
nNameLen
);
httpJsonInt
(
buf
,
num
);
}
void
httpJsonPairInt64Val
(
JsonBuf
*
buf
,
char
*
name
,
int
nNameLen
,
int64_t
num
)
{
void
httpJsonPairInt64Val
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nNameLen
,
int64_t
num
)
{
httpJsonPairHead
(
buf
,
name
,
nNameLen
);
httpJsonInt64
(
buf
,
num
);
}
void
httpJsonPairBoolVal
(
JsonBuf
*
buf
,
char
*
name
,
int
nNameLen
,
in
t
num
)
{
void
httpJsonPairBoolVal
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nNameLen
,
int32_
t
num
)
{
httpJsonPairHead
(
buf
,
name
,
nNameLen
);
httpJsonBool
(
buf
,
num
);
}
void
httpJsonPairFloatVal
(
JsonBuf
*
buf
,
char
*
name
,
int
nNameLen
,
float
num
)
{
void
httpJsonPairFloatVal
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nNameLen
,
float
num
)
{
httpJsonPairHead
(
buf
,
name
,
nNameLen
);
httpJsonFloat
(
buf
,
num
);
}
void
httpJsonPairDoubleVal
(
JsonBuf
*
buf
,
char
*
name
,
int
nNameLen
,
double
num
)
{
void
httpJsonPairDoubleVal
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nNameLen
,
double
num
)
{
httpJsonPairHead
(
buf
,
name
,
nNameLen
);
httpJsonDouble
(
buf
,
num
);
}
void
httpJsonPairNullVal
(
JsonBuf
*
buf
,
char
*
name
,
int
nNameLen
)
{
void
httpJsonPairNullVal
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
nNameLen
)
{
httpJsonPairHead
(
buf
,
name
,
nNameLen
);
httpJsonNull
(
buf
);
}
void
httpJsonPairArray
(
JsonBuf
*
buf
,
char
*
name
,
int
len
,
httpJsonBuilder
fnBuilder
,
void
*
dsHandle
)
{
void
httpJsonPairArray
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
len
,
httpJsonBuilder
fnBuilder
,
void
*
dsHandle
)
{
httpJsonPairHead
(
buf
,
name
,
len
);
httpJsonArray
(
buf
,
fnBuilder
,
dsHandle
);
}
void
httpJsonPairObject
(
JsonBuf
*
buf
,
char
*
name
,
int
len
,
httpJsonBuilder
fnBuilder
,
void
*
dsHandle
)
{
void
httpJsonPairObject
(
JsonBuf
*
buf
,
char
*
name
,
int
32_t
len
,
httpJsonBuilder
fnBuilder
,
void
*
dsHandle
)
{
httpJsonPairHead
(
buf
,
name
,
len
);
httpJsonObject
(
buf
,
fnBuilder
,
dsHandle
);
}
...
...
@@ -405,7 +406,7 @@ void httpJsonArray(JsonBuf* buf, httpJsonBuilder fnBuilder, void* jsonHandle) {
httpJsonToken
(
buf
,
JsonArrEnd
);
}
void
httpJsonTestBuf
(
JsonBuf
*
buf
,
int
safety
)
{
void
httpJsonTestBuf
(
JsonBuf
*
buf
,
int
32_t
safety
)
{
if
((
buf
->
lst
-
buf
->
buf
+
safety
)
<
buf
->
size
)
return
;
// buf->slot = *buf->lst;
httpWriteJsonBufBody
(
buf
,
false
);
...
...
@@ -416,7 +417,7 @@ void httpJsonToken(JsonBuf* buf, char c) {
*
buf
->
lst
++
=
c
;
}
void
httpJsonPrint
(
JsonBuf
*
buf
,
const
char
*
json
,
int
len
)
{
void
httpJsonPrint
(
JsonBuf
*
buf
,
const
char
*
json
,
int
32_t
len
)
{
if
(
len
==
0
||
len
>=
JSON_BUFFER_SIZE
)
{
return
;
}
...
...
@@ -432,7 +433,7 @@ void httpJsonPrint(JsonBuf* buf, const char* json, int len) {
buf
->
lst
+=
len
;
}
void
httpJsonPairStatus
(
JsonBuf
*
buf
,
int
code
)
{
void
httpJsonPairStatus
(
JsonBuf
*
buf
,
int
32_t
code
)
{
if
(
code
==
0
)
{
httpJsonPair
(
buf
,
"status"
,
6
,
"succ"
,
4
);
}
else
{
...
...
@@ -445,7 +446,7 @@ void httpJsonPairStatus(JsonBuf* buf, int code) {
}
else
if
(
code
==
TSDB_CODE_MND_INVALID_TABLE_NAME
)
{
httpJsonPair
(
buf
,
"desc"
,
4
,
"failed to create table"
,
22
);
}
else
{
httpJsonPair
(
buf
,
"desc"
,
4
,
(
char
*
)
tstrerror
(
code
),
(
int
)
strlen
(
tstrerror
(
code
)));
httpJsonPair
(
buf
,
"desc"
,
4
,
(
char
*
)
tstrerror
(
code
),
(
int
32_t
)
strlen
(
tstrerror
(
code
)));
}
}
}
src/plugins/http/src/httpParser.c
浏览文件 @
eff4b580
/*
* 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 "os.h"
#include "taoserror.h"
#include "httpLog.h"
#include "httpContext.h"
#include "httpParser.h"
#include "httpGzip.h"
#include "httpAuth.h"
static
HttpParserStatusObj
status_codes
[]
=
{
static
void
httpOnData
(
ehttp_gzip_t
*
gzip
,
void
*
arg
,
const
char
*
buf
,
int32_t
len
);
static
HttpStatus
httpStatusCodes
[]
=
{
{
100
,
"Continue"
},
{
101
,
"Switching Protocol"
},
{
102
,
"Processing (WebDAV)"
},
...
...
@@ -71,127 +91,411 @@ static HttpParserStatusObj status_codes[] = {
{
0
,
NULL
}
};
c
onst
char
*
ehttp_status_code_get_desc
(
const
int
status_c
ode
)
{
Http
ParserStatusObj
*
p
=
status_c
odes
;
while
(
p
->
status_code
!=
0
)
{
if
(
p
->
status_code
==
status_code
)
return
p
->
status_
desc
;
c
har
*
httpGetStatusDesc
(
int32_t
statusC
ode
)
{
Http
Status
*
p
=
httpStatusC
odes
;
while
(
p
->
code
!=
0
)
{
if
(
p
->
code
==
statusCode
)
return
p
->
desc
;
++
p
;
}
return
"Unknow status code"
;
}
static
void
dummy_on_request_line
(
void
*
arg
,
const
char
*
method
,
const
char
*
target
,
const
char
*
version
,
const
char
*
target_raw
)
{
static
void
httpCleanupString
(
HttpString
*
str
)
{
free
(
str
->
str
);
str
->
str
=
NULL
;
str
->
pos
=
0
;
str
->
size
=
0
;
}
static
void
dummy_on_status_line
(
void
*
arg
,
const
char
*
version
,
int
status_code
,
const
char
*
reason_phrase
)
{
}
static
int32_t
httpAppendString
(
HttpString
*
str
,
const
char
*
s
,
int32_t
len
)
{
if
(
str
->
size
==
0
)
{
str
->
pos
=
0
;
str
->
size
=
32
;
str
->
str
=
malloc
(
str
->
size
);
}
else
if
(
str
->
pos
+
len
+
1
>
str
->
size
)
{
str
->
size
*=
10
;
str
->
str
=
realloc
(
str
->
str
,
str
->
size
);
}
else
{
}
static
void
dummy_on_header_field
(
void
*
arg
,
const
char
*
key
,
const
char
*
val
)
{
}
if
(
str
->
str
==
NULL
)
return
-
1
;
static
void
dummy_on_body
(
void
*
arg
,
const
char
*
chunk
,
size_t
len
)
{
memcpy
(
str
->
str
+
str
->
pos
,
s
,
len
);
str
->
pos
+=
len
;
str
->
str
[
str
->
pos
]
=
0
;
return
0
;
}
static
void
dummy_on_end
(
void
*
arg
)
{
static
void
httpClearString
(
HttpString
*
str
)
{
if
(
str
->
str
)
{
str
->
str
[
0
]
=
'\0'
;
str
->
pos
=
0
;
str
->
size
=
0
;
}
}
static
void
dummy_on_error
(
void
*
arg
,
int
status_code
)
{
static
int32_t
httpOnError
(
HttpParser
*
parser
,
int32_t
httpCode
,
int32_t
parseCode
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
if
(
httpCode
!=
0
)
parser
->
httpCode
=
httpCode
;
if
(
parseCode
!=
0
)
parser
->
parseCode
=
parseCode
;
httpError
(
"context:%p, fd:%d, parse failed, httpCode:%d parseCode:%d"
,
pContext
,
pContext
->
fd
,
httpCode
,
parseCode
);
return
0
;
}
static
HTTP_PARSER_STATE
httpParserTop
(
HttpParserObj
*
parser
)
{
ASSERT
(
parser
->
stacks_count
>=
1
);
ASSERT
(
parser
->
stacks
);
static
int32_t
httpOnRequestLine
(
HttpParser
*
pParser
,
char
*
method
,
char
*
target
,
char
*
version
)
{
HttpContext
*
pContext
=
pParser
->
pContext
;
httpDebug
(
"context:%p, fd:%d, method:%s target:%s version:%s"
,
pContext
,
pContext
->
fd
,
method
,
target
,
version
);
// parse url
char
*
pStart
=
target
+
1
;
for
(
int32_t
i
=
0
;
i
<
HTTP_MAX_URL
;
i
++
)
{
char
*
pSeek
=
strchr
(
pStart
,
'/'
);
if
(
pSeek
==
NULL
)
{
pParser
->
path
[
i
].
str
=
strdup
(
pStart
);
pParser
->
path
[
i
].
size
=
strlen
(
pStart
);
pParser
->
path
[
i
].
pos
=
pParser
->
path
[
i
].
size
;
break
;
}
else
{
int32_t
len
=
(
int32_t
)(
pSeek
-
pStart
);
pParser
->
path
[
i
].
str
=
malloc
(
len
+
1
);
memcpy
(
pParser
->
path
[
i
].
str
,
pStart
,
len
);
pParser
->
path
[
i
].
str
[
len
]
=
0
;
pParser
->
path
[
i
].
size
=
len
;
pParser
->
path
[
i
].
pos
=
len
;
}
pStart
=
pSeek
+
1
;
}
return
parser
->
stacks
[
parser
->
stacks_count
-
1
];
}
// parse decode method
for
(
int32_t
i
=
0
;
i
<
tsHttpServer
.
methodScannerLen
;
i
++
)
{
HttpDecodeMethod
*
method
=
tsHttpServer
.
methodScanner
[
i
];
if
(
strcmp
(
method
->
module
,
pParser
->
path
[
0
].
str
)
==
0
)
{
pContext
->
decodeMethod
=
method
;
break
;
}
}
static
int
httpParserPush
(
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
)
{
size_t
n
=
parser
->
stacks_count
+
1
;
// HTTP_PARSER_STATE *stacks = (HTTP_PARSER_STATE*)reallocarray(parser->stacks, n, sizeof(*stacks));
HTTP_PARSER_STATE
*
stacks
=
(
HTTP_PARSER_STATE
*
)
realloc
(
parser
->
stacks
,
n
*
sizeof
(
*
stacks
));
if
(
!
stacks
)
return
-
1
;
if
(
pContext
->
decodeMethod
!=
NULL
)
{
httpDebug
(
"context:%p, fd:%d, decode method is %s"
,
pContext
,
pContext
->
fd
,
pContext
->
decodeMethod
->
module
);
}
else
{
httpError
(
"context:%p, fd:%d, the url is not support, target:%s"
,
pContext
,
pContext
->
fd
,
target
);
httpOnError
(
pParser
,
0
,
TSDB_CODE_HTTP_UNSUPPORT_URL
);
return
-
1
;
}
parser
->
stacks_count
=
n
;
parser
->
stacks
=
stacks
;
parser
->
stacks
[
n
-
1
]
=
state
;
// parse version
if
(
pParser
->
httpVersion
<
HTTP_VERSION_10
||
pParser
->
httpVersion
>
HTTP_VERSION_12
)
{
httpError
(
"context:%p, fd:%d, unsupport httpVersion %d"
,
pContext
,
pContext
->
fd
,
pParser
->
httpVersion
);
httpOnError
(
pParser
,
0
,
TSDB_CODE_HTTP_INVALID_VERSION
);
}
else
{
httpDebug
(
"context:%p, fd:%d, httpVersion:1.%d"
,
pContext
,
pContext
->
fd
,
pParser
->
httpVersion
);
}
return
0
;
}
static
int
httpParserPop
(
HttpParserObj
*
parser
)
{
if
(
parser
->
stacks_count
<=
0
)
return
-
1
;
--
parser
->
stacks_count
;
static
int32_t
httpOnStatusLine
(
HttpParser
*
pParser
,
int32_t
code
,
const
char
*
reason
)
{
HttpContext
*
pContext
=
pParser
->
pContext
;
httpError
(
"context:%p, fd:%d, status line, code:%d reason:%s"
,
pContext
,
pContext
->
fd
,
code
,
reason
);
return
0
;
}
HttpParserObj
*
httpParserCreate
(
HttpParserCallbackObj
callbacks
,
HttpParserConfObj
conf
,
void
*
arg
)
{
HttpParserObj
*
parser
=
(
HttpParserObj
*
)
calloc
(
1
,
sizeof
(
*
parser
));
if
(
!
parser
)
return
NULL
;
parser
->
callbacks
=
callbacks
;
parser
->
arg
=
arg
;
parser
->
conf
=
conf
;
static
int32_t
httpOnParseHeaderField
(
HttpParser
*
parser
,
const
char
*
key
,
const
char
*
val
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
httpDebug
(
"context:%p, fd:%d, key:%s val:%s"
,
pContext
,
pContext
->
fd
,
key
,
val
);
if
(
0
==
strcasecmp
(
key
,
"Content-Length"
))
{
int32_t
len
=
0
;
int32_t
bytes
=
0
;
int32_t
n
=
sscanf
(
val
,
"%d%n"
,
&
len
,
&
bytes
);
if
(
n
==
1
&&
bytes
==
strlen
(
val
))
{
parser
->
contentLength
=
len
;
parser
->
chunkSize
=
len
;
parser
->
contentLengthSpecified
=
1
;
httpDebug
(
"context:%p, fd:%d, contentLength:%d chunkSize:%d contentLengthSpecified:%d"
,
pContext
,
pContext
->
fd
,
parser
->
contentLength
,
parser
->
chunkSize
,
parser
->
contentLengthSpecified
);
return
0
;
}
else
{
httpError
(
"context:%p, fd:%d, failed to parser %s:%s"
,
pContext
,
pContext
->
fd
,
key
,
val
);
httpOnError
(
parser
,
0
,
TSDB_CODE_HTTP_INVALID_CONTENT_LENGTH
);
return
-
1
;
}
}
if
(
parser
->
callbacks
.
on_request_line
==
NULL
)
{
parser
->
callbacks
.
on_request_line
=
dummy_on_request_line
;
else
if
(
0
==
strcasecmp
(
key
,
"Accept-Encoding"
))
{
if
(
strstr
(
val
,
"gzip"
))
{
parser
->
acceptEncodingGzip
=
1
;
httpDebug
(
"context:%p, fd:%d, acceptEncodingGzip:%d"
,
pContext
,
pContext
->
fd
,
parser
->
acceptEncodingGzip
);
}
if
(
strstr
(
val
,
"chunked"
))
{
parser
->
acceptEncodingChunked
=
1
;
httpDebug
(
"context:%p, fd:%d, acceptEncodingChunked:%d"
,
pContext
,
pContext
->
fd
,
parser
->
acceptEncodingChunked
);
}
return
0
;
}
if
(
parser
->
callbacks
.
on_status_line
==
NULL
)
{
parser
->
callbacks
.
on_status_line
=
dummy_on_status_line
;
else
if
(
strncasecmp
(
key
,
"Connection: "
,
12
)
==
0
)
{
if
(
strncasecmp
(
val
,
"Keep-Alive"
,
10
)
==
0
)
{
parser
->
keepAlive
=
HTTP_KEEPALIVE_ENABLE
;
}
else
{
parser
->
keepAlive
=
HTTP_KEEPALIVE_DISABLE
;
}
httpTrace
(
"context:%p, fd:%d, keepAlive:%d"
,
pContext
,
pContext
->
fd
,
pContext
->
parser
->
keepAlive
);
}
if
(
parser
->
callbacks
.
on_header_field
==
NULL
)
{
parser
->
callbacks
.
on_header_field
=
dummy_on_header_field
;
else
if
(
0
==
strcasecmp
(
key
,
"Content-Encoding"
))
{
if
(
0
==
strcmp
(
val
,
"gzip"
))
{
parser
->
contentChunked
=
1
;
httpDebug
(
"context:%p, fd:%d, contentChunked:%d"
,
pContext
,
pContext
->
fd
,
parser
->
contentChunked
);
}
return
0
;
}
if
(
parser
->
callbacks
.
on_body
==
NULL
)
{
parser
->
callbacks
.
on_body
=
dummy_on_body
;
else
if
(
0
==
strcasecmp
(
key
,
"Transfer-Encoding"
))
{
if
(
strstr
(
val
,
"gzip"
))
{
parser
->
transferGzip
=
1
;
ehttp_gzip_conf_t
conf
=
{
0
};
ehttp_gzip_callbacks_t
callbacks
=
{
0
};
callbacks
.
on_data
=
httpOnData
;
parser
->
gzip
=
ehttp_gzip_create_decompressor
(
conf
,
callbacks
,
parser
);
if
(
!
parser
->
gzip
)
{
httpError
(
"context:%p, fd:%d, failed to create gzip decompressor"
,
pContext
,
pContext
->
fd
);
httpOnError
(
parser
,
0
,
TSDB_CODE_HTTP_CREATE_GZIP_FAILED
);
return
-
1
;
}
}
if
(
strstr
(
val
,
"chunked"
))
{
parser
->
transferChunked
=
1
;
httpDebug
(
"context:%p, fd:%d, transferChunked:%d"
,
pContext
,
pContext
->
fd
,
parser
->
transferChunked
);
}
return
0
;
}
if
(
parser
->
callbacks
.
on_end
==
NULL
)
{
parser
->
callbacks
.
on_end
=
dummy_on_end
;
else
if
(
0
==
strcasecmp
(
key
,
"Authorization"
))
{
char
*
t
=
NULL
;
char
*
s
=
NULL
;
int32_t
bytes
=
0
;
int32_t
n
=
sscanf
(
val
,
"%ms %ms%n"
,
&
t
,
&
s
,
&
bytes
);
if
(
n
==
2
&&
t
&&
s
&&
bytes
==
strlen
(
val
))
{
if
(
strcmp
(
t
,
"Basic"
)
==
0
)
{
free
(
parser
->
authContent
);
parser
->
authContent
=
s
;
parser
->
authType
=
HTTP_BASIC_AUTH
;
s
=
NULL
;
free
(
t
);
free
(
s
);
httpDebug
(
"context:%p, fd:%d, basic auth:%s"
,
pContext
,
pContext
->
fd
,
parser
->
authContent
);
int32_t
ok
=
httpParseBasicAuthToken
(
pContext
,
parser
->
authContent
,
strlen
(
parser
->
authContent
));
if
(
ok
!=
0
)
{
httpOnError
(
parser
,
0
,
TSDB_CODE_HTTP_INVALID_BASIC_AUTH
);
return
-
1
;
}
return
0
;
}
else
if
(
strcmp
(
t
,
"Taosd"
)
==
0
)
{
free
(
parser
->
authContent
);
parser
->
authContent
=
s
;
parser
->
authType
=
HTTP_TAOSD_AUTH
;
s
=
NULL
;
free
(
t
);
free
(
s
);
httpDebug
(
"context:%p, fd:%d, taosd auth:%s"
,
pContext
,
pContext
->
fd
,
parser
->
authContent
);
int32_t
ok
=
httpParseTaosdAuthToken
(
pContext
,
parser
->
authContent
,
strlen
(
parser
->
authContent
));
if
(
ok
!=
0
)
{
httpOnError
(
parser
,
0
,
TSDB_CODE_HTTP_INVALID_TAOSD_AUTH
);
return
-
1
;
}
return
0
;
}
else
{
free
(
t
);
free
(
s
);
parser
->
authType
=
HTTP_INVALID_AUTH
;
httpError
(
"context:%p, fd:%d, invalid auth, t:%s s:%s"
,
pContext
,
pContext
->
fd
,
t
,
s
);
httpOnError
(
parser
,
0
,
TSDB_CODE_HTTP_INVALID_AUTH_TYPE
);
return
-
1
;
}
}
else
{
free
(
t
);
free
(
s
);
parser
->
authType
=
HTTP_INVALID_AUTH
;
httpError
(
"context:%p, fd:%d, parse auth failed, t:%s s:%s"
,
pContext
,
pContext
->
fd
,
t
,
s
);
httpOnError
(
parser
,
0
,
TSDB_CODE_HTTP_INVALID_AUTH_FORMAT
);
return
-
1
;
}
}
if
(
parser
->
callbacks
.
on_error
==
NULL
)
{
parser
->
callbacks
.
on_error
=
dummy_on_error
;
return
0
;
}
static
int32_t
httpOnBody
(
HttpParser
*
parser
,
const
char
*
chunk
,
int32_t
len
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
HttpString
*
buf
=
&
parser
->
body
;
if
(
parser
->
parseCode
!=
TSDB_CODE_SUCCESS
)
return
-
1
;
int32_t
avail
=
buf
->
size
-
buf
->
pos
;
if
(
len
+
1
>=
avail
)
{
if
(
buf
->
size
>=
HTTP_BUFFER_SIZE
)
{
httpError
(
"context:%p, fd:%d, failed parse body, exceeding buffer size %d"
,
pContext
,
pContext
->
fd
,
buf
->
size
);
httpOnError
(
parser
,
0
,
TSDB_CODE_HTTP_NO_ENOUGH_MEMORY
);
return
-
1
;
}
else
{
int32_t
newSize
=
buf
->
size
*
10
;
newSize
=
MIN
(
newSize
,
HTTP_BUFFER_SIZE
);
buf
->
str
=
realloc
(
buf
->
str
,
newSize
);
if
(
buf
->
str
==
NULL
)
{
httpError
(
"context:%p, fd:%d, failed parse body, realloc %d failed"
,
pContext
,
pContext
->
fd
,
newSize
);
httpOnError
(
parser
,
0
,
TSDB_CODE_HTTP_NO_ENOUGH_MEMORY
);
return
-
1
;
}
buf
->
size
=
newSize
;
}
}
httpParserPush
(
parser
,
HTTP_PARSER_BEGIN
);
memcpy
(
buf
->
str
+
buf
->
pos
,
chunk
,
len
);
buf
->
pos
+=
len
;
buf
->
str
[
buf
->
pos
]
=
0
;
return
parser
;
return
0
;
}
static
int32_t
httpOnEnd
(
HttpParser
*
parser
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
parser
->
parsed
=
true
;
if
(
parser
->
parseCode
!=
TSDB_CODE_SUCCESS
)
{
return
-
1
;
}
httpDebug
(
"context:%p, fd:%d, parse success"
,
pContext
,
pContext
->
fd
);
return
0
;
}
static
void
ehttp_parser_kvs_destroy
(
HttpParserObj
*
parser
)
{
if
(
!
parser
->
kvs
)
return
;
static
HTTP_PARSER_STATE
httpTopStack
(
HttpParser
*
parser
)
{
HttpStack
*
stack
=
&
parser
->
stacks
;
ASSERT
(
stack
->
pos
>=
1
);
for
(
size_t
i
=
0
;
i
<
parser
->
kvs_count
;
++
i
)
{
HttpParseKvObj
*
p
=
&
parser
->
kvs
[
i
];
free
(
p
->
key
);
p
->
key
=
NULL
;
free
(
p
->
val
);
p
->
val
=
NULL
;
return
stack
->
stacks
[
stack
->
pos
-
1
];
}
static
int32_t
httpPushStack
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
)
{
HttpStack
*
stack
=
&
parser
->
stacks
;
if
(
stack
->
size
==
0
)
{
stack
->
pos
=
0
;
stack
->
size
=
32
;
stack
->
stacks
=
malloc
(
stack
->
size
*
sizeof
(
int8_t
));
}
else
if
(
stack
->
pos
+
1
>
stack
->
size
)
{
stack
->
size
*=
10
;
stack
->
stacks
=
realloc
(
stack
->
stacks
,
stack
->
size
*
sizeof
(
int8_t
));
}
else
{
}
free
(
parser
->
kvs
);
parser
->
kvs
=
NULL
;
parser
->
kvs_count
=
0
;
free
(
parser
->
auth_basic
);
parser
->
auth_basic
=
NULL
;
if
(
stack
->
stacks
==
NULL
)
return
-
1
;
stack
->
stacks
[
stack
->
pos
]
=
state
;
stack
->
pos
++
;
return
0
;
}
void
httpParserDestroy
(
HttpParserObj
*
parser
)
{
if
(
!
parser
)
return
;
static
int32_t
httpPopStack
(
HttpParser
*
parser
)
{
HttpStack
*
stack
=
&
parser
->
stacks
;
ASSERT
(
stack
->
pos
>=
1
);
stack
->
pos
--
;
return
0
;
}
static
void
httpClearStack
(
HttpStack
*
stack
)
{
stack
->
pos
=
0
;
}
static
int32_t
httpCleanupStack
(
HttpStack
*
stack
)
{
free
(
stack
->
stacks
);
memset
(
stack
,
0
,
sizeof
(
HttpStack
));
return
0
;
}
void
httpInitParser
(
HttpParser
*
parser
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
httpTrace
(
"context:%p, fd:%d, free parser"
,
pContext
,
pContext
->
fd
);
parser
->
parsed
=
false
;
parser
->
inited
=
1
;
parser
->
httpVersion
=
0
;
parser
->
acceptEncodingGzip
=
0
;
parser
->
acceptEncodingChunked
=
0
;
parser
->
contentLengthSpecified
=
0
;
parser
->
contentChunked
=
0
;
parser
->
transferGzip
=
0
;
parser
->
transferChunked
=
0
;
parser
->
keepAlive
=
0
;
parser
->
authType
=
0
;
parser
->
contentLength
=
0
;
parser
->
chunkSize
=
0
;
parser
->
receivedChunkSize
=
0
;
parser
->
receivedSize
=
0
;
parser
->
statusCode
=
0
;
parser
->
httpCode
=
0
;
parser
->
parseCode
=
0
;
free
(
parser
->
method
);
parser
->
method
=
NULL
;
free
(
parser
->
target
);
parser
->
target
=
NULL
;
free
(
parser
->
target_raw
);
parser
->
target_raw
=
NULL
;
free
(
parser
->
version
);
parser
->
version
=
NULL
;
free
(
parser
->
reason
_phrase
);
parser
->
reason_phrase
=
NULL
;
free
(
parser
->
reason
Phrase
);
parser
->
reasonPhrase
=
NULL
;
free
(
parser
->
key
);
parser
->
key
=
NULL
;
free
(
parser
->
val
);
parser
->
val
=
NULL
;
free
(
parser
->
auth_basic
);
parser
->
auth_basic
=
NULL
;
free
(
parser
->
stacks
);
parser
->
stacks
=
NULL
;
free
(
parser
->
authContent
);
parser
->
authContent
=
NULL
;
httpClearStack
(
&
parser
->
stacks
);
httpClearString
(
&
parser
->
str
);
httpClearString
(
&
parser
->
body
);
for
(
int32_t
i
=
0
;
i
<
HTTP_MAX_URL
;
++
i
)
{
httpClearString
(
&
parser
->
path
[
i
]);
}
if
(
parser
->
gzip
!=
NULL
)
{
ehttp_gzip_destroy
(
parser
->
gzip
);
parser
->
gzip
=
NULL
;
}
parser
->
stacks_count
=
0
;
httpPushStack
(
parser
,
HTTP_PARSER_BEGIN
);
}
ehttp_parser_kvs_destroy
(
parser
);
HttpParser
*
httpCreateParser
(
HttpContext
*
pContext
)
{
HttpParser
*
parser
=
calloc
(
1
,
sizeof
(
HttpParser
));
if
(
!
parser
)
return
NULL
;
httpTrace
(
"context:%p, fd:%d, create parser"
,
pContext
,
pContext
->
fd
);
httpParserCleanupString
(
&
parser
->
str
);
if
(
parser
->
gzip
)
{
parser
->
pContext
=
pContext
;
return
parser
;
}
void
httpDestroyParser
(
HttpParser
*
parser
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
httpTrace
(
"context:%p, fd:%d, free parser"
,
pContext
,
pContext
->
fd
);
if
(
!
parser
)
return
;
free
(
parser
->
method
);
parser
->
method
=
NULL
;
free
(
parser
->
target
);
parser
->
target
=
NULL
;
free
(
parser
->
target_raw
);
parser
->
target_raw
=
NULL
;
free
(
parser
->
version
);
parser
->
version
=
NULL
;
free
(
parser
->
reasonPhrase
);
parser
->
reasonPhrase
=
NULL
;
free
(
parser
->
key
);
parser
->
key
=
NULL
;
free
(
parser
->
val
);
parser
->
val
=
NULL
;
free
(
parser
->
authContent
);
parser
->
authContent
=
NULL
;
httpCleanupStack
(
&
parser
->
stacks
);
httpCleanupString
(
&
parser
->
str
);
httpCleanupString
(
&
parser
->
body
);
for
(
int32_t
i
=
0
;
i
<
HTTP_MAX_URL
;
++
i
)
{
httpCleanupString
(
&
parser
->
path
[
i
]);
}
if
(
parser
->
gzip
!=
NULL
)
{
ehttp_gzip_destroy
(
parser
->
gzip
);
parser
->
gzip
=
NULL
;
}
...
...
@@ -201,209 +505,106 @@ void httpParserDestroy(HttpParserObj *parser) {
#define is_token(c) (strchr("!#$%&'*+-.^_`|~", c) || isdigit(c) || isalpha(c))
char
*
ehttp_parser_urldecode
(
const
char
*
enc
)
{
int
ok
=
1
;
Http
Parser
String
str
=
{
0
};
char
*
httpDecodeUrl
(
const
char
*
enc
)
{
int
32_t
ok
=
1
;
HttpString
str
=
{
0
};
while
(
*
enc
)
{
char
*
p
=
strchr
(
enc
,
'%'
);
if
(
!
p
)
break
;
int
hex
,
cnt
;
int
n
=
sscanf
(
p
+
1
,
"%2x%n"
,
&
hex
,
&
cnt
);
int
32_t
hex
,
cnt
;
int
32_t
n
=
sscanf
(
p
+
1
,
"%2x%n"
,
&
hex
,
&
cnt
);
if
(
n
!=
1
&&
cnt
!=
2
)
{
ok
=
0
;
break
;
}
if
(
http
Parser
AppendString
(
&
str
,
enc
,
p
-
enc
))
{
ok
=
0
;
break
;
}
if
(
httpAppendString
(
&
str
,
enc
,
p
-
enc
))
{
ok
=
0
;
break
;
}
char
c
=
(
char
)
hex
;
if
(
http
Parser
AppendString
(
&
str
,
&
c
,
1
))
{
ok
=
0
;
break
;
}
if
(
httpAppendString
(
&
str
,
&
c
,
1
))
{
ok
=
0
;
break
;
}
enc
=
p
+
3
;
}
char
*
dec
=
NULL
;
if
(
ok
&&
*
enc
)
{
if
(
http
Parser
AppendString
(
&
str
,
enc
,
strlen
(
enc
)))
{
ok
=
0
;
}
if
(
httpAppendString
(
&
str
,
enc
,
strlen
(
enc
)))
{
ok
=
0
;
}
}
if
(
ok
)
{
dec
=
str
.
str
;
str
.
str
=
NULL
;
}
http
Parser
CleanupString
(
&
str
);
httpCleanupString
(
&
str
);
return
dec
;
}
static
void
on_data
(
ehttp_gzip_t
*
gzip
,
void
*
arg
,
const
char
*
buf
,
size
_t
len
)
{
HttpParser
Obj
*
parser
=
(
HttpParserObj
*
)
arg
;
parser
->
callbacks
.
on_body
(
parser
->
arg
,
buf
,
len
);
static
void
httpOnData
(
ehttp_gzip_t
*
gzip
,
void
*
arg
,
const
char
*
buf
,
int32
_t
len
)
{
HttpParser
*
parser
=
(
HttpParser
*
)
arg
;
httpOnBody
(
parser
,
buf
,
len
);
}
static
int32_t
httpParserCheckField
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
const
char
*
key
,
const
char
*
val
)
{
int32_t
ok
=
0
;
do
{
if
(
0
==
strcasecmp
(
key
,
"Content-Length"
))
{
int32_t
len
=
0
;
int32_t
bytes
=
0
;
int32_t
n
=
sscanf
(
val
,
"%d%n"
,
&
len
,
&
bytes
);
if
(
n
==
1
&&
bytes
==
strlen
(
val
))
{
parser
->
content_length
=
len
;
parser
->
chunk_size
=
len
;
parser
->
content_length_specified
=
1
;
break
;
}
ok
=
-
1
;
break
;
}
if
(
0
==
strcasecmp
(
key
,
"Accept-Encoding"
))
{
if
(
strstr
(
val
,
"gzip"
))
{
parser
->
accept_encoding_gzip
=
1
;
}
if
(
strstr
(
val
,
"chunked"
))
{
parser
->
accept_encoding_chunked
=
1
;
}
break
;
}
if
(
0
==
strcasecmp
(
key
,
"Content-Encoding"
))
{
if
(
0
==
strcmp
(
val
,
"gzip"
))
{
parser
->
content_chunked
=
1
;
}
break
;
}
if
(
0
==
strcasecmp
(
key
,
"Transfer-Encoding"
))
{
if
(
strstr
(
val
,
"gzip"
))
{
parser
->
transfer_gzip
=
1
;
ehttp_gzip_conf_t
conf
=
{
0
};
ehttp_gzip_callbacks_t
callbacks
=
{
0
};
callbacks
.
on_data
=
on_data
;
parser
->
gzip
=
ehttp_gzip_create_decompressor
(
conf
,
callbacks
,
parser
);
if
(
!
parser
->
gzip
)
{
httpDebug
(
"failed to create gzip decompressor"
);
ok
=
-
1
;
break
;
}
}
if
(
strstr
(
val
,
"chunked"
))
{
parser
->
transfer_chunked
=
1
;
}
break
;
}
if
(
0
==
strcasecmp
(
key
,
"Authorization"
))
{
char
*
t
=
NULL
;
char
*
s
=
NULL
;
int32_t
bytes
=
0
;
int32_t
n
=
sscanf
(
val
,
"%ms %ms%n"
,
&
t
,
&
s
,
&
bytes
);
if
(
n
==
2
&&
t
&&
s
&&
bytes
==
strlen
(
val
))
{
if
(
strcmp
(
t
,
"Basic"
)
==
0
)
{
free
(
parser
->
auth_basic
);
parser
->
auth_basic
=
s
;
s
=
NULL
;
}
else
if
(
n
==
2
&&
t
&&
s
&&
strcmp
(
t
,
"Taosd"
)
==
0
)
{
free
(
parser
->
auth_taosd
);
parser
->
auth_taosd
=
s
;
s
=
NULL
;
}
else
{
httpError
(
"context:%p, fd:%d, invalid auth, t:%s s:%s"
,
pContext
,
pContext
->
fd
,
t
,
s
);
ok
=
-
1
;
}
}
else
{
httpError
(
"context:%p, fd:%d, parse auth failed, t:%s s:%s"
,
pContext
,
pContext
->
fd
,
t
,
s
);
ok
=
-
1
;
}
free
(
t
);
free
(
s
);
break
;
}
}
while
(
0
);
return
ok
;
}
static
int
httpParserAppendKv
(
HttpParserObj
*
parser
,
const
char
*
key
,
const
char
*
val
)
{
// HttpParseKvObj *kvs = (HttpParseKvObj*)reallocarray(parser->kvs, parser->kvs_count + 1, sizeof(*kvs));
HttpParseKvObj
*
kvs
=
(
HttpParseKvObj
*
)
realloc
(
parser
->
kvs
,
(
parser
->
kvs_count
+
1
)
*
sizeof
(
*
kvs
));
if
(
!
kvs
)
return
-
1
;
parser
->
kvs
=
kvs
;
kvs
[
parser
->
kvs_count
].
key
=
strdup
(
key
);
kvs
[
parser
->
kvs_count
].
val
=
strdup
(
val
);
if
(
kvs
[
parser
->
kvs_count
].
key
&&
kvs
[
parser
->
kvs_count
].
val
)
{
++
parser
->
kvs_count
;
return
0
;
}
free
(
kvs
[
parser
->
kvs_count
].
key
);
kvs
[
parser
->
kvs_count
].
key
=
NULL
;
free
(
kvs
[
parser
->
kvs_count
].
val
);
kvs
[
parser
->
kvs_count
].
val
=
NULL
;
return
-
1
;
}
static
int32_t
httpParserOnBegin
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
static
int32_t
httpParserOnBegin
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
if
(
c
==
'G'
||
c
==
'P'
||
c
==
'H'
||
c
==
'D'
||
c
==
'C'
||
c
==
'O'
||
c
==
'T'
)
{
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_METHOD_FAILED
);
break
;
}
httpP
arserPop
(
parser
);
httpP
arserPush
(
parser
,
HTTP_PARSER_REQUEST_OR_RESPONSE
);
httpP
opStack
(
parser
);
httpP
ushStack
(
parser
,
HTTP_PARSER_REQUEST_OR_RESPONSE
);
break
;
}
httpError
(
"context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
400
);
httpOnError
(
parser
,
400
,
TSDB_CODE_HTTP_PARSE_METHOD_FAILED
);
}
while
(
0
);
return
ok
;
}
static
int32_t
httpParserOnRquestOrResponse
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
static
int32_t
httpParserOnRquestOrResponse
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
if
(
parser
->
str
.
len
==
1
)
{
if
(
parser
->
str
.
pos
==
1
)
{
if
(
c
==
'T'
&&
parser
->
str
.
str
[
0
]
==
'H'
)
{
httpP
arserPop
(
parser
);
httpP
arserPush
(
parser
,
HTTP_PARSER_END
);
httpP
arserPush
(
parser
,
HTTP_PARSER_HEADER
);
httpP
arserPush
(
parser
,
HTTP_PARSER_CRLF
);
httpP
arserPush
(
parser
,
HTTP_PARSER_REASON_PHRASE
);
httpP
arserPush
(
parser
,
HTTP_PARSER_SP
);
httpP
arserPush
(
parser
,
HTTP_PARSER_STATUS_CODE
);
httpP
arserPush
(
parser
,
HTTP_PARSER_SP
);
httpP
arserPush
(
parser
,
HTTP_PARSER_HTTP_VERSION
);
httpP
opStack
(
parser
);
httpP
ushStack
(
parser
,
HTTP_PARSER_END
);
httpP
ushStack
(
parser
,
HTTP_PARSER_HEADER
);
httpP
ushStack
(
parser
,
HTTP_PARSER_CRLF
);
httpP
ushStack
(
parser
,
HTTP_PARSER_REASON_PHRASE
);
httpP
ushStack
(
parser
,
HTTP_PARSER_SP
);
httpP
ushStack
(
parser
,
HTTP_PARSER_STATUS_CODE
);
httpP
ushStack
(
parser
,
HTTP_PARSER_SP
);
httpP
ushStack
(
parser
,
HTTP_PARSER_HTTP_VERSION
);
*
again
=
1
;
break
;
}
httpP
arserPop
(
parser
);
httpP
arserPush
(
parser
,
HTTP_PARSER_END
);
httpP
arserPush
(
parser
,
HTTP_PARSER_HEADER
);
httpP
arserPush
(
parser
,
HTTP_PARSER_CRLF
);
httpP
arserPush
(
parser
,
HTTP_PARSER_HTTP_VERSION
);
httpP
arserPush
(
parser
,
HTTP_PARSER_SP
);
httpP
arserPush
(
parser
,
HTTP_PARSER_TARGET
);
httpP
arserPush
(
parser
,
HTTP_PARSER_SP
);
httpP
arserPush
(
parser
,
HTTP_PARSER_METHOD
);
httpP
opStack
(
parser
);
httpP
ushStack
(
parser
,
HTTP_PARSER_END
);
httpP
ushStack
(
parser
,
HTTP_PARSER_HEADER
);
httpP
ushStack
(
parser
,
HTTP_PARSER_CRLF
);
httpP
ushStack
(
parser
,
HTTP_PARSER_HTTP_VERSION
);
httpP
ushStack
(
parser
,
HTTP_PARSER_SP
);
httpP
ushStack
(
parser
,
HTTP_PARSER_TARGET
);
httpP
ushStack
(
parser
,
HTTP_PARSER_SP
);
httpP
ushStack
(
parser
,
HTTP_PARSER_METHOD
);
*
again
=
1
;
break
;
}
httpError
(
"context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
400
);
httpOnError
(
parser
,
400
,
TSDB_CODE_HTTP_PARSE_METHOD_FAILED
);
}
while
(
0
);
return
ok
;
}
static
int32_t
httpParserOnMethod
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
static
int32_t
httpParserOnMethod
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
if
(
isalnum
(
c
)
||
strchr
(
"!#$%&'*+-.^_`|~"
,
c
))
{
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpDebug
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_METHOD_FAILED
);
break
;
}
break
;
...
...
@@ -412,59 +613,63 @@ static int32_t httpParserOnMethod(HttpContext *pContext, HttpParserObj *parser,
if
(
!
parser
->
method
)
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_METHOD_FAILED
);
break
;
}
else
{
httpTrace
(
"context:%p, fd:%d, httpMethod:%s"
,
pContext
,
pContext
->
fd
,
parser
->
method
);
}
http
Parser
ClearString
(
&
parser
->
str
);
httpP
arserPop
(
parser
);
httpClearString
(
&
parser
->
str
);
httpP
opStack
(
parser
);
*
again
=
1
;
}
while
(
0
);
return
ok
;
}
static
int32_t
httpParserOnTarget
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
static
int32_t
httpParserOnTarget
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
if
(
!
isspace
(
c
)
&&
c
!=
'\r'
&&
c
!=
'\n'
)
{
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_TARGET_FAILED
);
break
;
}
break
;
}
parser
->
target_raw
=
strdup
(
parser
->
str
.
str
);
parser
->
target
=
ehttp_parser_urldecode
(
parser
->
str
.
str
);
parser
->
target
=
httpDecodeUrl
(
parser
->
str
.
str
);
if
(
!
parser
->
target_raw
||
!
parser
->
target
)
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_TARGET_FAILED
);
break
;
}
http
Parser
ClearString
(
&
parser
->
str
);
httpP
arserPop
(
parser
);
httpClearString
(
&
parser
->
str
);
httpP
opStack
(
parser
);
*
again
=
1
;
}
while
(
0
);
return
ok
;
}
static
int32_t
httpParserOnVersion
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
static
int32_t
httpParserOnVersion
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
const
char
*
prefix
=
"HTTP/1."
;
int32_t
len
=
strlen
(
prefix
);
if
(
parser
->
str
.
len
<
len
)
{
if
(
prefix
[
parser
->
str
.
len
]
!=
c
)
{
if
(
parser
->
str
.
pos
<
len
)
{
if
(
prefix
[
parser
->
str
.
pos
]
!=
c
)
{
httpError
(
"context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
400
);
httpOnError
(
parser
,
400
,
TSDB_CODE_HTTP_PARSE_VERSION_FAILED
);
break
;
}
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_VERSION_FAILED
);
break
;
}
break
;
...
...
@@ -473,135 +678,143 @@ static int32_t httpParserOnVersion(HttpContext *pContext, HttpParserObj *parser,
if
(
c
!=
'0'
&&
c
!=
'1'
)
{
httpError
(
"context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
400
);
httpOnError
(
parser
,
400
,
TSDB_CODE_HTTP_PARSE_VERSION_FAILED
);
break
;
}
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_VERSION_FAILED
);
break
;
}
if
(
c
==
'0'
)
parser
->
http_10
=
1
;
if
(
c
==
'1'
)
parser
->
http_11
=
1
;
if
(
c
==
'0'
)
parser
->
httpVersion
=
HTTP_VERSION_10
;
else
if
(
c
==
'1'
)
parser
->
httpVersion
=
HTTP_VERSION_11
;
else
if
(
c
==
'2'
)
parser
->
httpVersion
=
HTTP_VERSION_12
;
else
parser
->
httpVersion
=
HTTP_INVALID_VERSION
;
parser
->
version
=
strdup
(
parser
->
str
.
str
);
if
(
!
parser
->
version
)
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_VERSION_FAILED
);
break
;
}
if
(
parser
->
method
)
{
parser
->
callbacks
.
on_request_line
(
parser
->
arg
,
parser
->
method
,
parser
->
target
,
parser
->
version
,
parser
->
target_raw
);
ok
=
httpOnRequestLine
(
parser
,
parser
->
method
,
parser
->
target
,
parser
->
version
);
}
http
Parser
ClearString
(
&
parser
->
str
);
httpP
arserPop
(
parser
);
httpClearString
(
&
parser
->
str
);
httpP
opStack
(
parser
);
}
while
(
0
);
return
ok
;
}
static
int32_t
httpParserOnSp
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int
*
again
)
{
static
int32_t
httpParserOnSp
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
if
(
c
==
' '
)
{
httpP
arserPop
(
parser
);
httpP
opStack
(
parser
);
break
;
}
httpDebug
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_SP_FAILED
);
}
while
(
0
);
return
ok
;
}
static
int32_t
httpParserOnStatusCode
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int
*
again
)
{
int
ok
=
0
;
static
int32_t
httpParserOnStatusCode
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
if
(
isdigit
(
c
))
{
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_STATUS_FAILED
);
break
;
}
if
(
parser
->
str
.
len
<
3
)
break
;
if
(
parser
->
str
.
pos
<
3
)
break
;
sscanf
(
parser
->
str
.
str
,
"%d"
,
&
parser
->
status
_c
ode
);
http
Parser
ClearString
(
&
parser
->
str
);
httpP
arserPop
(
parser
);
sscanf
(
parser
->
str
.
str
,
"%d"
,
&
parser
->
status
C
ode
);
httpClearString
(
&
parser
->
str
);
httpP
opStack
(
parser
);
break
;
}
httpError
(
"context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
400
);
httpOnError
(
parser
,
400
,
TSDB_CODE_HTTP_PARSE_STATUS_FAILED
);
}
while
(
0
);
return
ok
;
}
static
int32_t
httpParserOnReasonPhrase
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int
*
again
)
{
int
ok
=
0
;
static
int32_t
httpParserOnReasonPhrase
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
if
(
c
==
'\r'
)
{
parser
->
reason
_p
hrase
=
strdup
(
parser
->
str
.
str
);
if
(
!
parser
->
reason
_p
hrase
)
{
if
(
c
==
'\r'
)
{
parser
->
reason
P
hrase
=
strdup
(
parser
->
str
.
str
);
if
(
!
parser
->
reason
P
hrase
)
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_PHRASE_FAILED
);
break
;
}
parser
->
callbacks
.
on_status_line
(
parser
->
arg
,
parser
->
version
,
parser
->
status_code
,
parser
->
reason_p
hrase
);
http
Parser
ClearString
(
&
parser
->
str
);
httpP
arserPop
(
parser
);
ok
=
httpOnStatusLine
(
parser
,
parser
->
statusCode
,
parser
->
reasonP
hrase
);
httpClearString
(
&
parser
->
str
);
httpP
opStack
(
parser
);
*
again
=
1
;
break
;
}
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_PHRASE_FAILED
);
break
;
}
}
while
(
0
);
return
ok
;
}
static
int32_t
httpParserPostProcess
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
)
{
static
int32_t
httpParserPostProcess
(
HttpParser
*
parser
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
if
(
parser
->
gzip
)
{
if
(
ehttp_gzip_finish
(
parser
->
gzip
))
{
httpError
(
"context:%p, fd:%d, gzip failed"
,
pContext
,
pContext
->
fd
);
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_FINISH_GZIP_FAILED
);
return
-
1
;
}
}
parser
->
callbacks
.
on_end
(
parser
->
arg
);
httpOnEnd
(
parser
);
return
0
;
}
static
int32_t
httpParserOnCrlf
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
static
int32_t
httpParserOnCrlf
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
const
char
*
s
=
"
\r\n
"
;
int32_t
len
=
strlen
(
s
);
if
(
s
[
parser
->
str
.
len
]
!=
c
)
{
if
(
s
[
parser
->
str
.
pos
]
!=
c
)
{
httpError
(
"context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
400
);
httpOnError
(
parser
,
400
,
TSDB_CODE_HTTP_PARSE_CRLF_FAILED
);
break
;
}
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_CRLF_FAILED
);
break
;
}
if
(
parser
->
str
.
len
==
len
)
{
http
Parser
ClearString
(
&
parser
->
str
);
httpP
arserPop
(
parser
);
if
(
http
ParserTop
(
parser
)
==
HTTP_PARSER_END
)
{
ok
=
httpParserPostProcess
(
p
Context
,
p
arser
);
if
(
parser
->
str
.
pos
==
len
)
{
httpClearString
(
&
parser
->
str
);
httpP
opStack
(
parser
);
if
(
http
TopStack
(
parser
)
==
HTTP_PARSER_END
)
{
ok
=
httpParserPostProcess
(
parser
);
}
}
break
;
...
...
@@ -609,141 +822,140 @@ static int32_t httpParserOnCrlf(HttpContext *pContext, HttpParserObj *parser, HT
return
ok
;
}
static
int32_t
httpParserOnHeader
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
static
int32_t
httpParserOnHeader
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
if
(
c
==
'\r'
)
{
httpP
arserPop
(
parser
);
if
(
parser
->
transfer
_c
hunked
)
{
httpP
arserPush
(
parser
,
HTTP_PARSER_CHUNK_SIZE
);
httpP
arserPush
(
parser
,
HTTP_PARSER_CRLF
);
if
(
c
==
'\r'
)
{
httpP
opStack
(
parser
);
if
(
parser
->
transfer
C
hunked
)
{
httpP
ushStack
(
parser
,
HTTP_PARSER_CHUNK_SIZE
);
httpP
ushStack
(
parser
,
HTTP_PARSER_CRLF
);
}
else
{
if
(
parser
->
content
_l
ength
>
0
)
{
httpP
arserPush
(
parser
,
HTTP_PARSER_CHUNK
);
if
(
parser
->
content
L
ength
>
0
)
{
httpP
ushStack
(
parser
,
HTTP_PARSER_CHUNK
);
}
httpP
arserPush
(
parser
,
HTTP_PARSER_CRLF
);
httpP
ushStack
(
parser
,
HTTP_PARSER_CRLF
);
}
*
again
=
1
;
break
;
}
if
(
c
!=
' '
&&
c
!=
'\t'
&&
c
!=
':'
)
{
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
c
!=
' '
&&
c
!=
'\t'
&&
c
!=
':'
)
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_HEADER_FAILED
);
break
;
}
httpP
arserPush
(
parser
,
HTTP_PARSER_CRLF
);
httpP
arserPush
(
parser
,
HTTP_PARSER_HEADER_VAL
);
httpP
arserPush
(
parser
,
HTTP_PARSER_SP
);
httpP
arserPush
(
parser
,
HTTP_PARSER_HEADER_KEY
);
httpP
ushStack
(
parser
,
HTTP_PARSER_CRLF
);
httpP
ushStack
(
parser
,
HTTP_PARSER_HEADER_VAL
);
httpP
ushStack
(
parser
,
HTTP_PARSER_SP
);
httpP
ushStack
(
parser
,
HTTP_PARSER_HEADER_KEY
);
break
;
}
httpError
(
"context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
400
);
httpOnError
(
parser
,
400
,
TSDB_CODE_HTTP_PARSE_HEADER_FAILED
);
}
while
(
0
);
return
ok
;
}
static
int
httpParserOnHeaderKey
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int
*
again
)
{
int
ok
=
0
;
static
int32_t
httpParserOnHeaderKey
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
if
(
isalnum
(
c
)
||
strchr
(
"!#$%&'*+-.^_`|~"
,
c
))
{
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_HEADER_KEY_FAILED
);
break
;
}
break
;
}
if
(
c
==
':'
)
{
if
(
c
==
':'
)
{
parser
->
key
=
strdup
(
parser
->
str
.
str
);
if
(
!
parser
->
key
)
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_HEADER_KEY_FAILED
);
break
;
}
http
Parser
ClearString
(
&
parser
->
str
);
httpP
arserPop
(
parser
);
httpClearString
(
&
parser
->
str
);
httpP
opStack
(
parser
);
break
;
}
httpError
(
"context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
400
);
httpOnError
(
parser
,
400
,
TSDB_CODE_HTTP_PARSE_HEADER_KEY_FAILED
);
}
while
(
0
);
return
ok
;
}
static
int32_t
httpParserOnHeaderVal
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
static
int32_t
httpParserOnHeaderVal
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
if
(
c
!=
'\r'
&&
c
!=
'\n'
&&
(
!
isspace
(
c
)
||
parser
->
str
.
len
>
0
))
{
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
c
!=
'\r'
&&
c
!=
'\n'
&&
(
!
isspace
(
c
)
||
parser
->
str
.
pos
>
0
))
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
parser
->
parseCode
=
TSDB_CODE_HTTP_PARSE_HEADER_VAL_FAILED
;
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_HEADER_VAL_FAILED
);
break
;
}
break
;
}
const
char
*
val
=
parser
->
str
.
str
;
ok
=
httpParserCheckField
(
pContext
,
parser
,
parser
->
key
,
val
);
if
(
httpParserAppendKv
(
parser
,
parser
->
key
,
val
))
{
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
}
else
{
parser
->
callbacks
.
on_header_field
(
parser
->
arg
,
parser
->
key
,
val
);
}
ok
=
httpOnParseHeaderField
(
parser
,
parser
->
key
,
val
);
free
(
parser
->
key
);
parser
->
key
=
NULL
;
val
=
NULL
;
if
(
ok
==
-
1
)
break
;
http
Parser
ClearString
(
&
parser
->
str
);
httpP
arserPop
(
parser
);
httpClearString
(
&
parser
->
str
);
httpP
opStack
(
parser
);
*
again
=
1
;
}
while
(
0
);
return
ok
;
}
static
int32_t
httpParserOnChunkSize
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
static
int32_t
httpParserOnChunkSize
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
int32_t
bytes
;
int32_t
len
;
int
n
;
int
32_t
n
;
do
{
if
(
isxdigit
(
c
))
{
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_CHUNK_SIZE_FAILED
);
break
;
}
break
;
}
if
(
c
==
'\r'
)
{
if
(
c
==
'\r'
)
{
n
=
sscanf
(
parser
->
str
.
str
,
"%x%n"
,
&
len
,
&
bytes
);
if
(
n
==
1
&&
bytes
==
strlen
(
parser
->
str
.
str
)
&&
len
>=
0
)
{
if
(
len
==
0
)
{
if
(
parser
->
content
_length_specified
==
0
||
parser
->
received_size
==
parser
->
content_l
ength
)
{
http
Parser
ClearString
(
&
parser
->
str
);
httpP
arserPop
(
parser
);
httpP
arserPush
(
parser
,
HTTP_PARSER_CRLF
);
httpP
arserPush
(
parser
,
HTTP_PARSER_CRLF
);
if
(
n
==
1
&&
bytes
==
strlen
(
parser
->
str
.
str
)
&&
len
>=
0
)
{
if
(
len
==
0
)
{
if
(
parser
->
content
LengthSpecified
==
0
||
parser
->
receivedSize
==
parser
->
contentL
ength
)
{
httpClearString
(
&
parser
->
str
);
httpP
opStack
(
parser
);
httpP
ushStack
(
parser
,
HTTP_PARSER_CRLF
);
httpP
ushStack
(
parser
,
HTTP_PARSER_CRLF
);
*
again
=
1
;
break
;
}
}
else
{
if
(
parser
->
content
_length_specified
==
0
||
parser
->
received_size
+
len
<=
parser
->
content_l
ength
)
{
parser
->
chunk
_s
ize
=
len
;
http
Parser
ClearString
(
&
parser
->
str
);
httpP
arserPop
(
parser
);
httpP
arserPush
(
parser
,
HTTP_PARSER_CHUNK_SIZE
);
httpP
arserPush
(
parser
,
HTTP_PARSER_CRLF
);
httpP
arserPush
(
parser
,
HTTP_PARSER_CHUNK
);
httpP
arserPush
(
parser
,
HTTP_PARSER_CRLF
);
if
(
parser
->
content
LengthSpecified
==
0
||
parser
->
receivedSize
+
len
<=
parser
->
contentL
ength
)
{
parser
->
chunk
S
ize
=
len
;
httpClearString
(
&
parser
->
str
);
httpP
opStack
(
parser
);
httpP
ushStack
(
parser
,
HTTP_PARSER_CHUNK_SIZE
);
httpP
ushStack
(
parser
,
HTTP_PARSER_CRLF
);
httpP
ushStack
(
parser
,
HTTP_PARSER_CHUNK
);
httpP
ushStack
(
parser
,
HTTP_PARSER_CRLF
);
*
again
=
1
;
break
;
}
...
...
@@ -752,116 +964,119 @@ static int32_t httpParserOnChunkSize(HttpContext *pContext, HttpParserObj *parse
}
httpError
(
"context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
400
);
httpOnError
(
parser
,
400
,
TSDB_CODE_HTTP_PARSE_CHUNK_SIZE_FAILED
);
}
while
(
0
);
return
ok
;
}
static
int
httpParserOnChunk
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int
*
again
)
{
int
ok
=
0
;
static
int32_t
httpParserOnChunk
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
if
(
http
Parser
AppendString
(
&
parser
->
str
,
&
c
,
1
))
{
if
(
httpAppendString
(
&
parser
->
str
,
&
c
,
1
))
{
httpError
(
"context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_CHUNK_FAILED
);
break
;
}
++
parser
->
received
_s
ize
;
++
parser
->
received
_chunk_s
ize
;
if
(
parser
->
received
_chunk_size
<
parser
->
chunk_s
ize
)
break
;
++
parser
->
received
S
ize
;
++
parser
->
received
ChunkS
ize
;
if
(
parser
->
received
ChunkSize
<
parser
->
chunkS
ize
)
break
;
if
(
parser
->
gzip
)
{
if
(
ehttp_gzip_write
(
parser
->
gzip
,
parser
->
str
.
str
,
parser
->
str
.
len
))
{
if
(
ehttp_gzip_write
(
parser
->
gzip
,
parser
->
str
.
str
,
parser
->
str
.
pos
))
{
httpError
(
"context:%p, fd:%d, gzip failed"
,
pContext
,
pContext
->
fd
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_CHUNK_FAILED
);
break
;
}
}
else
{
parser
->
callbacks
.
on_body
(
parser
->
arg
,
parser
->
str
.
str
,
parser
->
str
.
len
);
httpOnBody
(
parser
,
parser
->
str
.
str
,
parser
->
str
.
pos
);
}
parser
->
received
_chunk_s
ize
=
0
;
http
Parser
ClearString
(
&
parser
->
str
);
httpP
arserPop
(
parser
);
if
(
http
ParserTop
(
parser
)
==
HTTP_PARSER_END
)
{
ok
=
httpParserPostProcess
(
p
Context
,
p
arser
);
parser
->
received
ChunkS
ize
=
0
;
httpClearString
(
&
parser
->
str
);
httpP
opStack
(
parser
);
if
(
http
TopStack
(
parser
)
==
HTTP_PARSER_END
)
{
ok
=
httpParserPostProcess
(
parser
);
}
}
while
(
0
);
return
ok
;
}
static
int32_t
httpParserOnEnd
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
static
int32_t
httpParserOnEnd
(
HttpParser
*
parser
,
HTTP_PARSER_STATE
state
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
do
{
httpError
(
"context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
507
);
httpError
(
"context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x"
,
pContext
,
pContext
->
fd
,
state
,
c
,
c
);
httpOnError
(
parser
,
507
,
TSDB_CODE_HTTP_PARSE_END_FAILED
);
}
while
(
0
);
return
ok
;
}
static
int32_t
httpParseChar
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
const
char
c
,
int32_t
*
again
)
{
static
int32_t
httpParseChar
(
HttpParser
*
parser
,
const
char
c
,
int32_t
*
again
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
int32_t
ok
=
0
;
HTTP_PARSER_STATE
state
=
http
ParserTop
(
parser
);
HTTP_PARSER_STATE
state
=
http
TopStack
(
parser
);
do
{
if
(
state
==
HTTP_PARSER_BEGIN
)
{
ok
=
httpParserOnBegin
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnBegin
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_REQUEST_OR_RESPONSE
)
{
ok
=
httpParserOnRquestOrResponse
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnRquestOrResponse
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_METHOD
)
{
ok
=
httpParserOnMethod
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnMethod
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_TARGET
)
{
ok
=
httpParserOnTarget
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnTarget
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_HTTP_VERSION
)
{
ok
=
httpParserOnVersion
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnVersion
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_SP
)
{
ok
=
httpParserOnSp
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnSp
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_STATUS_CODE
)
{
ok
=
httpParserOnStatusCode
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnStatusCode
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_REASON_PHRASE
)
{
ok
=
httpParserOnReasonPhrase
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnReasonPhrase
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_CRLF
)
{
ok
=
httpParserOnCrlf
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnCrlf
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_HEADER
)
{
ok
=
httpParserOnHeader
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnHeader
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_HEADER_KEY
)
{
ok
=
httpParserOnHeaderKey
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnHeaderKey
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_HEADER_VAL
)
{
ok
=
httpParserOnHeaderVal
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnHeaderVal
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_CHUNK_SIZE
)
{
ok
=
httpParserOnChunkSize
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnChunkSize
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_CHUNK
)
{
ok
=
httpParserOnChunk
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnChunk
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_END
)
{
ok
=
httpParserOnEnd
(
p
Context
,
p
arser
,
state
,
c
,
again
);
ok
=
httpParserOnEnd
(
parser
,
state
,
c
,
again
);
break
;
}
if
(
state
==
HTTP_PARSER_ERROR
)
{
...
...
@@ -869,32 +1084,34 @@ static int32_t httpParseChar(HttpContext *pContext, HttpParserObj *parser, const
break
;
}
httpError
(
"context:%p, fd:%d, unknown parse state:%d"
,
pContext
,
pContext
->
fd
,
state
);
ok
=
-
1
;
parser
->
callbacks
.
on_error
(
parser
->
arg
,
500
);
httpError
(
"context:%p, fd:%d, unknown parse state:%d"
,
pContext
,
pContext
->
fd
,
state
);
httpOnError
(
parser
,
500
,
TSDB_CODE_HTTP_PARSE_INVALID_STATE
);
}
while
(
0
);
if
(
ok
==
-
1
)
{
httpError
(
"context:%p, fd:%d, failed to parse, state:%d
ok:%d"
,
pContext
,
pContext
->
fd
,
state
,
ok
);
httpP
arserPush
(
parser
,
HTTP_PARSER_ERROR
);
httpError
(
"context:%p, fd:%d, failed to parse, state:%d
"
,
pContext
,
pContext
->
fd
,
state
);
httpP
ushStack
(
parser
,
HTTP_PARSER_ERROR
);
}
if
(
ok
==
-
2
)
{
httpError
(
"context:%p, fd:%d, failed to parse, state:%d ok:%d"
,
pContext
,
pContext
->
fd
,
state
,
ok
);
ok
=
-
1
;
httpError
(
"context:%p, fd:%d, failed to parse, invalid state"
,
pContext
,
pContext
->
fd
);
httpOnError
(
parser
,
500
,
TSDB_CODE_HTTP_PARSE_ERROR_STATE
);
}
return
ok
;
}
int32_t
httpParserBuf
(
HttpContext
*
pContext
,
HttpParserObj
*
parser
,
const
char
*
buf
,
int32_t
len
)
{
int32_t
httpParseBuf
(
HttpParser
*
parser
,
const
char
*
buf
,
int32_t
len
)
{
HttpContext
*
pContext
=
parser
->
pContext
;
const
char
*
p
=
buf
;
int32_t
ret
=
0
;
int32_t
i
=
0
;
while
(
i
<
len
)
{
int32_t
again
=
0
;
ret
=
httpParseChar
(
p
Context
,
p
arser
,
*
p
,
&
again
);
ret
=
httpParseChar
(
parser
,
*
p
,
&
again
);
if
(
ret
!=
0
)
{
httpError
(
"context:%p, fd:%d, parse failed, ret:%d i:%d len:%d buf:%s"
,
pContext
,
pContext
->
fd
,
ret
,
i
,
len
,
buf
);
break
;
...
...
@@ -906,27 +1123,3 @@ int32_t httpParserBuf(HttpContext *pContext, HttpParserObj *parser, const char *
return
ret
;
}
void
httpParserCleanupString
(
HttpParserString
*
str
)
{
free
(
str
->
str
);
str
->
str
=
NULL
;
str
->
len
=
0
;
}
int32_t
httpParserAppendString
(
HttpParserString
*
str
,
const
char
*
s
,
int32_t
len
)
{
int32_t
n
=
str
->
len
;
char
*
p
=
(
char
*
)
realloc
(
str
->
str
,
n
+
len
+
1
);
if
(
!
p
)
return
-
1
;
strncpy
(
p
+
n
,
s
,
len
);
p
[
n
+
len
]
=
'\0'
;
str
->
str
=
p
;
str
->
len
=
n
+
len
;
return
0
;
}
void
httpParserClearString
(
HttpParserString
*
str
)
{
if
(
str
->
str
)
{
str
->
str
[
0
]
=
'\0'
;
str
->
len
=
0
;
}
}
src/plugins/http/src/httpQueue.c
浏览文件 @
eff4b580
...
...
@@ -39,15 +39,15 @@ typedef struct {
typedef
struct
{
void
*
param
;
void
*
result
;
int
numOfRows
;
void
(
*
fp
)(
void
*
param
,
void
*
result
,
int
numOfRows
);
int
32_t
numOfRows
;
void
(
*
fp
)(
void
*
param
,
void
*
result
,
int
32_t
numOfRows
);
}
SHttpResult
;
static
SHttpWorkerPool
tsHttpPool
;
static
taos_qset
tsHttpQset
;
static
taos_queue
tsHttpQueue
;
void
httpDispatchToResultQueue
(
void
*
param
,
TAOS_RES
*
result
,
int
numOfRows
,
void
(
*
fp
)(
void
*
param
,
void
*
result
,
in
t
numOfRows
))
{
void
httpDispatchToResultQueue
(
void
*
param
,
TAOS_RES
*
result
,
int
32_t
numOfRows
,
void
(
*
fp
)(
void
*
param
,
void
*
result
,
int32_
t
numOfRows
))
{
if
(
tsHttpQueue
!=
NULL
)
{
SHttpResult
*
pMsg
=
(
SHttpResult
*
)
taosAllocateQitem
(
sizeof
(
SHttpResult
));
pMsg
->
param
=
param
;
...
...
src/plugins/http/src/httpResp.c
浏览文件 @
eff4b580
...
...
@@ -45,23 +45,23 @@ const char *httpRespTemplate[] = {
"%s 200 OK
\r\n
Access-Control-Allow-Origin:*
\r\n
%sAccess-Control-Allow-Methods:POST, GET, OPTIONS, DELETE, PUT
\r\n
Access-Control-Allow-Headers:Accept, Content-Type
\r\n
Content-Type: application/json;charset=utf-8
\r\n
Content-Length: %d
\r\n\r\n
"
};
static
void
httpSendErrorRespImp
(
HttpContext
*
pContext
,
int
httpCode
,
char
*
httpCodeStr
,
in
t
errNo
,
char
*
desc
)
{
static
void
httpSendErrorRespImp
(
HttpContext
*
pContext
,
int
32_t
httpCode
,
char
*
httpCodeStr
,
int32_
t
errNo
,
char
*
desc
)
{
httpError
(
"context:%p, fd:%d, code:%d, error:%s"
,
pContext
,
pContext
->
fd
,
httpCode
,
desc
);
char
head
[
512
]
=
{
0
};
char
body
[
512
]
=
{
0
};
int
bodyLen
=
sprintf
(
body
,
httpRespTemplate
[
HTTP_RESPONSE_JSON_ERROR
],
errNo
,
desc
);
int
headLen
=
sprintf
(
head
,
httpRespTemplate
[
HTTP_RESPONSE_ERROR
],
httpVersionStr
[
pContext
->
httpVersion
],
httpCode
,
httpCodeStr
,
httpKeepAliveStr
[
pContext
->
httpK
eepAlive
],
bodyLen
);
int
32_t
bodyLen
=
sprintf
(
body
,
httpRespTemplate
[
HTTP_RESPONSE_JSON_ERROR
],
errNo
,
desc
);
int
32_t
headLen
=
sprintf
(
head
,
httpRespTemplate
[
HTTP_RESPONSE_ERROR
],
httpVersionStr
[
pContext
->
parser
->
httpVersion
]
,
httpCode
,
httpCodeStr
,
httpKeepAliveStr
[
pContext
->
parser
->
k
eepAlive
],
bodyLen
);
httpWriteBuf
(
pContext
,
head
,
headLen
);
httpWriteBuf
(
pContext
,
body
,
bodyLen
);
httpCloseContextByApp
(
pContext
);
}
void
httpSendErrorRespWithDesc
(
HttpContext
*
pContext
,
int
errNo
,
char
*
desc
)
{
int
httpCode
=
500
;
void
httpSendErrorRespWithDesc
(
HttpContext
*
pContext
,
int
32_t
errNo
,
char
*
desc
)
{
int
32_t
httpCode
=
500
;
char
*
httpCodeStr
=
"Internal Server Error"
;
switch
(
errNo
)
{
case
HTTP_SUCCESS
:
...
...
@@ -180,20 +180,20 @@ void httpSendErrorRespWithDesc(HttpContext *pContext, int errNo, char *desc) {
}
}
void
httpSendErrorResp
(
HttpContext
*
pContext
,
int
errNo
)
{
httpSendErrorRespWithDesc
(
pContext
,
errNo
,
NULL
);
}
void
httpSendErrorResp
(
HttpContext
*
pContext
,
int
32_t
errNo
)
{
httpSendErrorRespWithDesc
(
pContext
,
errNo
,
NULL
);
}
void
httpSendTaosdErrorResp
(
HttpContext
*
pContext
,
int
errCode
)
{
int
httpCode
=
400
;
void
httpSendTaosdErrorResp
(
HttpContext
*
pContext
,
int
32_t
errCode
)
{
int
32_t
httpCode
=
400
;
httpSendErrorRespImp
(
pContext
,
httpCode
,
"Bad Request"
,
errCode
&
0XFFFF
,
(
char
*
)
tstrerror
(
errCode
));
}
void
httpSendTaosdInvalidSqlErrorResp
(
HttpContext
*
pContext
,
char
*
errMsg
)
{
int
httpCode
=
400
;
char
temp
[
512
]
=
{
0
};
int
len
=
sprintf
(
temp
,
"invalid SQL: %s"
,
errMsg
);
void
httpSendTaosdInvalidSqlErrorResp
(
HttpContext
*
pContext
,
char
*
errMsg
)
{
int
32_t
httpCode
=
400
;
char
temp
[
512
]
=
{
0
};
int
32_t
len
=
sprintf
(
temp
,
"invalid SQL: %s"
,
errMsg
);
for
(
int
i
=
0
;
i
<
len
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
len
;
++
i
)
{
if
(
temp
[
i
]
==
'\"'
)
{
temp
[
i
]
=
'\''
;
}
else
if
(
temp
[
i
]
==
'\n'
)
{
...
...
@@ -208,9 +208,9 @@ void httpSendSuccResp(HttpContext *pContext, char *desc) {
char
head
[
1024
]
=
{
0
};
char
body
[
1024
]
=
{
0
};
int
bodyLen
=
sprintf
(
body
,
httpRespTemplate
[
HTTP_RESPONSE_JSON_OK
],
HTTP_SUCCESS
,
desc
);
int
headLen
=
sprintf
(
head
,
httpRespTemplate
[
HTTP_RESPONSE_OK
],
httpVersionStr
[
pContext
->
httpVersion
],
httpKeepAliveStr
[
pContext
->
httpK
eepAlive
],
bodyLen
);
int
32_t
bodyLen
=
sprintf
(
body
,
httpRespTemplate
[
HTTP_RESPONSE_JSON_OK
],
HTTP_SUCCESS
,
desc
);
int
32_t
headLen
=
sprintf
(
head
,
httpRespTemplate
[
HTTP_RESPONSE_OK
],
httpVersionStr
[
pContext
->
parser
->
httpVersion
],
httpKeepAliveStr
[
pContext
->
parser
->
k
eepAlive
],
bodyLen
);
httpWriteBuf
(
pContext
,
head
,
headLen
);
httpWriteBuf
(
pContext
,
body
,
bodyLen
);
...
...
@@ -221,9 +221,9 @@ void httpSendOptionResp(HttpContext *pContext, char *desc) {
char
head
[
1024
]
=
{
0
};
char
body
[
1024
]
=
{
0
};
int
bodyLen
=
sprintf
(
body
,
httpRespTemplate
[
HTTP_RESPONSE_JSON_OK
],
HTTP_SUCCESS
,
desc
);
int
headLen
=
sprintf
(
head
,
httpRespTemplate
[
HTTP_RESPONSE_OPTIONS
],
httpVersionStr
[
pContext
->
httpVersion
],
httpKeepAliveStr
[
pContext
->
httpK
eepAlive
],
bodyLen
);
int
32_t
bodyLen
=
sprintf
(
body
,
httpRespTemplate
[
HTTP_RESPONSE_JSON_OK
],
HTTP_SUCCESS
,
desc
);
int
32_t
headLen
=
sprintf
(
head
,
httpRespTemplate
[
HTTP_RESPONSE_OPTIONS
],
httpVersionStr
[
pContext
->
parser
->
httpVersion
],
httpKeepAliveStr
[
pContext
->
parser
->
k
eepAlive
],
bodyLen
);
httpWriteBuf
(
pContext
,
head
,
headLen
);
httpWriteBuf
(
pContext
,
body
,
bodyLen
);
...
...
src/plugins/http/src/httpRestHandle.c
浏览文件 @
eff4b580
...
...
@@ -60,22 +60,22 @@ void restInitHandle(HttpServer* pServer) {
}
bool
restGetUserFromUrl
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
pParser
->
path
[
REST_USER_URL_POS
].
len
>=
TSDB_USER_LEN
||
pParser
->
path
[
REST_USER_URL_POS
].
len
<=
0
)
{
HttpParser
*
pParser
=
pContext
->
parser
;
if
(
pParser
->
path
[
REST_USER_URL_POS
].
pos
>=
TSDB_USER_LEN
||
pParser
->
path
[
REST_USER_URL_POS
].
pos
<=
0
)
{
return
false
;
}
tstrncpy
(
pContext
->
user
,
pParser
->
path
[
REST_USER_URL_POS
].
pos
,
TSDB_USER_LEN
);
tstrncpy
(
pContext
->
user
,
pParser
->
path
[
REST_USER_URL_POS
].
str
,
TSDB_USER_LEN
);
return
true
;
}
bool
restGetPassFromUrl
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
pParser
->
path
[
REST_PASS_URL_POS
].
len
>=
TSDB_PASSWORD_LEN
||
pParser
->
path
[
REST_PASS_URL_POS
].
len
<=
0
)
{
HttpParser
*
pParser
=
pContext
->
parser
;
if
(
pParser
->
path
[
REST_PASS_URL_POS
].
pos
>=
TSDB_PASSWORD_LEN
||
pParser
->
path
[
REST_PASS_URL_POS
].
pos
<=
0
)
{
return
false
;
}
tstrncpy
(
pContext
->
pass
,
pParser
->
path
[
REST_PASS_URL_POS
].
pos
,
TSDB_PASSWORD_LEN
);
tstrncpy
(
pContext
->
pass
,
pParser
->
path
[
REST_PASS_URL_POS
].
str
,
TSDB_PASSWORD_LEN
);
return
true
;
}
...
...
@@ -85,10 +85,10 @@ bool restProcessLoginRequest(HttpContext* pContext) {
return
true
;
}
bool
restProcessSqlRequest
(
HttpContext
*
pContext
,
int
timestampFmt
)
{
bool
restProcessSqlRequest
(
HttpContext
*
pContext
,
int
32_t
timestampFmt
)
{
httpDebug
(
"context:%p, fd:%d, user:%s, process restful sql msg"
,
pContext
,
pContext
->
fd
,
pContext
->
user
);
char
*
sql
=
pContext
->
parser
.
data
.
pos
;
char
*
sql
=
pContext
->
parser
->
body
.
str
;
if
(
sql
==
NULL
)
{
httpSendErrorResp
(
pContext
,
HTTP_NO_SQL_INPUT
);
return
false
;
...
...
src/plugins/http/src/httpRestJson.c
浏览文件 @
eff4b580
...
...
@@ -21,7 +21,7 @@
#include "httpRestHandle.h"
#include "httpRestJson.h"
void
restBuildSqlAffectRowsJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
affect_rows
)
{
void
restBuildSqlAffectRowsJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
32_t
affect_rows
)
{
JsonBuf
*
jsonBuf
=
httpMallocJsonBuf
(
pContext
);
if
(
jsonBuf
==
NULL
)
return
;
...
...
@@ -43,7 +43,7 @@ void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result)
if
(
jsonBuf
==
NULL
)
return
;
TAOS_FIELD
*
fields
=
taos_fetch_fields
(
result
);
int
num_fields
=
taos_num_fields
(
result
);
int
32_t
num_fields
=
taos_num_fields
(
result
);
httpInitJsonBuf
(
jsonBuf
,
pContext
);
httpWriteJsonBufHead
(
jsonBuf
);
...
...
@@ -66,9 +66,9 @@ void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result)
httpJsonItemToken
(
jsonBuf
);
httpJsonString
(
jsonBuf
,
REST_JSON_AFFECT_ROWS
,
REST_JSON_AFFECT_ROWS_LEN
);
}
else
{
for
(
int
i
=
0
;
i
<
num_fields
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
num_fields
;
++
i
)
{
httpJsonItemToken
(
jsonBuf
);
httpJsonString
(
jsonBuf
,
fields
[
i
].
name
,
(
int
)
strlen
(
fields
[
i
].
name
));
httpJsonString
(
jsonBuf
,
fields
[
i
].
name
,
(
int
32_t
)
strlen
(
fields
[
i
].
name
));
}
}
...
...
@@ -83,16 +83,16 @@ void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result)
httpJsonToken
(
jsonBuf
,
JsonArrStt
);
}
bool
restBuildSqlJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
numOfRows
,
in
t
timestampFormat
)
{
bool
restBuildSqlJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
32_t
numOfRows
,
int32_
t
timestampFormat
)
{
JsonBuf
*
jsonBuf
=
httpMallocJsonBuf
(
pContext
);
if
(
jsonBuf
==
NULL
)
return
false
;
cmd
->
numOfRows
+=
numOfRows
;
int
num_fields
=
taos_num_fields
(
result
);
int
32_t
num_fields
=
taos_num_fields
(
result
);
TAOS_FIELD
*
fields
=
taos_fetch_fields
(
result
);
for
(
int
k
=
0
;
k
<
numOfRows
;
++
k
)
{
for
(
int
32_t
k
=
0
;
k
<
numOfRows
;
++
k
)
{
TAOS_ROW
row
=
taos_fetch_row
(
result
);
if
(
row
==
NULL
)
{
cmd
->
numOfRows
--
;
...
...
@@ -104,7 +104,7 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
httpJsonItemToken
(
jsonBuf
);
httpJsonToken
(
jsonBuf
,
JsonArrStt
);
for
(
int
i
=
0
;
i
<
num_fields
;
i
++
)
{
for
(
int
32_t
i
=
0
;
i
<
num_fields
;
i
++
)
{
httpJsonItemToken
(
jsonBuf
);
if
(
row
[
i
]
==
NULL
)
{
...
...
@@ -171,15 +171,15 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
}
}
bool
restBuildSqlTimestampJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
numOfRows
)
{
bool
restBuildSqlTimestampJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
32_t
numOfRows
)
{
return
restBuildSqlJson
(
pContext
,
cmd
,
result
,
numOfRows
,
REST_TIMESTAMP_FMT_TIMESTAMP
);
}
bool
restBuildSqlLocalTimeStringJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
numOfRows
)
{
bool
restBuildSqlLocalTimeStringJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
32_t
numOfRows
)
{
return
restBuildSqlJson
(
pContext
,
cmd
,
result
,
numOfRows
,
REST_TIMESTAMP_FMT_LOCAL_STRING
);
}
bool
restBuildSqlUtcTimeStringJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
numOfRows
)
{
bool
restBuildSqlUtcTimeStringJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
TAOS_RES
*
result
,
int
32_t
numOfRows
)
{
return
restBuildSqlJson
(
pContext
,
cmd
,
result
,
numOfRows
,
REST_TIMESTAMP_FMT_UTC_STRING
);
}
...
...
src/plugins/http/src/httpServer.c
浏览文件 @
eff4b580
...
...
@@ -61,7 +61,7 @@ void httpCleanUpConnect() {
if
(
pServer
->
pThreads
==
NULL
)
return
;
pthread_join
(
pServer
->
thread
,
NULL
);
for
(
int
i
=
0
;
i
<
pServer
->
numOfThreads
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
pServer
->
numOfThreads
;
++
i
)
{
HttpThread
*
pThread
=
pServer
->
pThreads
+
i
;
if
(
pThread
!=
NULL
)
{
httpStopThread
(
pThread
);
...
...
@@ -71,41 +71,11 @@ void httpCleanUpConnect() {
httpDebug
(
"http server:%s is cleaned up"
,
pServer
->
label
);
}
static
bool
httpDecompressData
(
HttpContext
*
pContext
)
{
if
(
pContext
->
contentEncoding
!=
HTTP_COMPRESS_GZIP
)
{
httpTraceL
(
"context:%p, fd:%d, content:%s"
,
pContext
,
pContext
->
fd
,
pContext
->
parser
.
data
.
pos
);
return
true
;
}
char
*
decompressBuf
=
calloc
(
HTTP_DECOMPRESS_BUF_SIZE
,
1
);
int32_t
decompressBufLen
=
HTTP_DECOMPRESS_BUF_SIZE
;
size_t
bufsize
=
sizeof
(
pContext
->
parser
.
buffer
)
-
(
pContext
->
parser
.
data
.
pos
-
pContext
->
parser
.
buffer
)
-
1
;
if
(
decompressBufLen
>
(
int
)
bufsize
)
{
decompressBufLen
=
(
int
)
bufsize
;
}
int
ret
=
httpGzipDeCompress
(
pContext
->
parser
.
data
.
pos
,
pContext
->
parser
.
data
.
len
,
decompressBuf
,
&
decompressBufLen
);
if
(
ret
==
0
)
{
memcpy
(
pContext
->
parser
.
data
.
pos
,
decompressBuf
,
decompressBufLen
);
pContext
->
parser
.
data
.
pos
[
decompressBufLen
]
=
0
;
httpTraceL
(
"context:%p, fd:%d, rawSize:%d, decompressSize:%d, content:%s"
,
pContext
,
pContext
->
fd
,
pContext
->
parser
.
data
.
len
,
decompressBufLen
,
decompressBuf
);
pContext
->
parser
.
data
.
len
=
decompressBufLen
;
}
else
{
httpError
(
"context:%p, fd:%d, failed to decompress data, rawSize:%d, error:%d"
,
pContext
,
pContext
->
fd
,
pContext
->
parser
.
data
.
len
,
ret
);
}
free
(
decompressBuf
);
return
ret
==
0
;
}
static
void
httpProcessHttpData
(
void
*
param
)
{
HttpServer
*
pServer
=
&
tsHttpServer
;
HttpThread
*
pThread
=
(
HttpThread
*
)
param
;
HttpContext
*
pContext
;
int
fdNum
;
int
32_t
fdNum
;
sigset_t
set
;
sigemptyset
(
&
set
);
...
...
@@ -122,7 +92,7 @@ static void httpProcessHttpData(void *param) {
}
if
(
fdNum
<=
0
)
continue
;
for
(
int
i
=
0
;
i
<
fdNum
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
fdNum
;
++
i
)
{
pContext
=
httpGetContext
(
events
[
i
].
data
.
ptr
);
if
(
pContext
==
NULL
)
{
httpError
(
"context:%p, is already released, close connect"
,
events
[
i
].
data
.
ptr
);
...
...
@@ -182,13 +152,13 @@ static void httpProcessHttpData(void *param) {
}
static
void
*
httpAcceptHttpConnection
(
void
*
arg
)
{
int
connFd
=
-
1
;
int
32_t
connFd
=
-
1
;
struct
sockaddr_in
clientAddr
;
int
threadId
=
0
;
int
32_t
threadId
=
0
;
HttpServer
*
pServer
=
&
tsHttpServer
;
HttpThread
*
pThread
=
NULL
;
HttpContext
*
pContext
=
NULL
;
int
totalFds
=
0
;
int
32_t
totalFds
=
0
;
sigset_t
set
;
sigemptyset
(
&
set
);
...
...
@@ -208,7 +178,7 @@ static void *httpAcceptHttpConnection(void *arg) {
while
(
1
)
{
socklen_t
addrlen
=
sizeof
(
clientAddr
);
connFd
=
(
int
)
accept
(
pServer
->
fd
,
(
struct
sockaddr
*
)
&
clientAddr
,
&
addrlen
);
connFd
=
(
int
32_t
)
accept
(
pServer
->
fd
,
(
struct
sockaddr
*
)
&
clientAddr
,
&
addrlen
);
if
(
connFd
==
-
1
)
{
if
(
errno
==
EINVAL
)
{
httpDebug
(
"http server:%s socket was shutdown, exiting..."
,
pServer
->
label
);
...
...
@@ -219,7 +189,7 @@ static void *httpAcceptHttpConnection(void *arg) {
}
totalFds
=
1
;
for
(
int
i
=
0
;
i
<
pServer
->
numOfThreads
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
pServer
->
numOfThreads
;
++
i
)
{
totalFds
+=
pServer
->
pThreads
[
i
].
numOfContexts
;
}
...
...
@@ -283,7 +253,7 @@ bool httpInitConnect() {
}
HttpThread
*
pThread
=
pServer
->
pThreads
;
for
(
int
i
=
0
;
i
<
pServer
->
numOfThreads
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
pServer
->
numOfThreads
;
++
i
)
{
sprintf
(
pThread
->
label
,
"%s%d"
,
pServer
->
label
,
i
);
pThread
->
processData
=
pServer
->
processData
;
pThread
->
threadId
=
i
;
...
...
@@ -331,52 +301,39 @@ bool httpInitConnect() {
}
static
bool
httpReadData
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
ASSERT
(
!
pContext
->
parsed
);
if
(
!
pParser
->
parser
)
{
if
(
!
pParser
->
inited
)
{
httpInitContext
(
pContext
);
}
if
(
!
pParser
->
parser
)
{
return
false
;
}
HttpParser
*
pParser
=
pContext
->
parser
;
ASSERT
(
!
pParser
->
parsed
);
if
(
!
pParser
->
inited
)
{
httpInitParser
(
pParser
);
}
pContext
->
accessTimes
++
;
pContext
->
lastAccessTime
=
taosGetTimestampSec
();
char
buf
[
HTTP_STEP_SIZE
+
1
]
=
{
0
};
int
nread
=
(
in
t
)
taosReadSocket
(
pContext
->
fd
,
buf
,
sizeof
(
buf
));
char
buf
[
HTTP_STEP_SIZE
+
1
]
=
{
0
};
int
32_t
nread
=
(
int32_
t
)
taosReadSocket
(
pContext
->
fd
,
buf
,
sizeof
(
buf
));
if
(
nread
>
0
)
{
buf
[
nread
]
=
'\0'
;
httpTrace
(
"context:%p, fd:%d, nread:%d content:%s"
,
pContext
,
pContext
->
fd
,
nread
,
buf
);
int
ok
=
httpParserBuf
(
pContext
,
pParser
->
p
arser
,
buf
,
nread
);
int
32_t
ok
=
httpParseBuf
(
pP
arser
,
buf
,
nread
);
if
(
ok
)
{
httpError
(
"context:%p, fd:%d,
init parse failed, reason:%d close connect"
,
pContext
,
pContext
->
fd
,
ok
);
httpError
(
"context:%p, fd:%d,
parse failed, ret:%d code:%d close connect"
,
pContext
,
pContext
->
fd
,
ok
,
pParser
->
parseCode
);
httpNotifyContextClose
(
pContext
);
return
false
;
}
if
(
p
Context
->
parser
.
failed
)
{
httpError
(
"context:%p, fd:%d, parse failed, c
lose connect"
,
pContext
,
pContext
->
fd
);
if
(
p
Parser
->
parseCode
)
{
httpError
(
"context:%p, fd:%d, parse failed, c
ode:%d close connect"
,
pContext
,
pContext
->
fd
,
pParser
->
parseCode
);
httpNotifyContextClose
(
pContext
);
return
false
;
}
if
(
pContext
->
parsed
)
{
httpDebug
(
"context:%p, fd:%d, read size:%d, dataLen:%d"
,
pContext
,
pContext
->
fd
,
pContext
->
parser
.
bufsize
,
pContext
->
parser
.
data
.
len
);
if
(
httpDecompressData
(
pContext
))
{
return
true
;
}
else
{
httpNotifyContextClose
(
pContext
);
return
false
;
}
if
(
pParser
->
parsed
)
{
httpDebug
(
"context:%p, fd:%d, len:%d, body:%s"
,
pContext
,
pContext
->
fd
,
pParser
->
body
.
pos
,
pParser
->
body
.
str
);
}
return
pContext
->
parsed
;
return
true
;
}
else
if
(
nread
<
0
)
{
if
(
errno
==
EINTR
||
errno
==
EAGAIN
||
errno
==
EWOULDBLOCK
)
{
httpDebug
(
"context:%p, fd:%d, read from socket error:%d, wait another event"
,
pContext
,
pContext
->
fd
,
errno
);
...
...
src/plugins/http/src/httpSystem.c
浏览文件 @
eff4b580
...
...
@@ -37,9 +37,9 @@ void opInitHandle(HttpServer* pServer) {}
#endif
HttpServer
tsHttpServer
;
void
taosInitNote
(
int
numOfNoteLines
,
in
t
maxNotes
,
char
*
lable
);
void
taosInitNote
(
int
32_t
numOfNoteLines
,
int32_
t
maxNotes
,
char
*
lable
);
int
httpInitSystem
()
{
int
32_t
httpInitSystem
()
{
strcpy
(
tsHttpServer
.
label
,
"rest"
);
tsHttpServer
.
serverIp
=
0
;
tsHttpServer
.
serverPort
=
tsHttpPort
;
...
...
@@ -60,7 +60,7 @@ int httpInitSystem() {
return
0
;
}
int
httpStartSystem
()
{
int
32_t
httpStartSystem
()
{
httpInfo
(
"start http server ..."
);
if
(
tsHttpServer
.
status
!=
HTTP_SERVER_INIT
)
{
...
...
src/plugins/http/src/httpTgHandle.c
浏览文件 @
eff4b580
...
...
@@ -83,16 +83,16 @@ static const char DEFAULT_TELEGRAF_CFG[] =
"]}"
;
typedef
struct
{
char
*
name
;
char
*
tbName
;
char
**
fields
;
int
fieldNum
;
char
*
name
;
char
*
tbName
;
char
**
fields
;
int
32_t
fieldNum
;
}
STgSchema
;
typedef
struct
{
STgSchema
*
schemas
;
int
size
;
int
pos
;
int
32_t
size
;
int
32_t
pos
;
}
STgSchemas
;
static
STgSchemas
tgSchemas
=
{
0
};
...
...
@@ -107,7 +107,7 @@ void tgFreeSchema(STgSchema *schema) {
schema
->
tbName
=
NULL
;
}
if
(
schema
->
fields
!=
NULL
)
{
for
(
int
f
=
0
;
f
<
schema
->
fieldNum
;
++
f
)
{
for
(
int
32_t
f
=
0
;
f
<
schema
->
fieldNum
;
++
f
)
{
if
(
schema
->
fields
[
f
]
!=
NULL
)
{
free
(
schema
->
fields
[
f
]);
schema
->
fields
[
f
]
=
NULL
;
...
...
@@ -121,7 +121,7 @@ void tgFreeSchema(STgSchema *schema) {
void
tgFreeSchemas
()
{
if
(
tgSchemas
.
schemas
!=
NULL
)
{
for
(
int
s
=
0
;
s
<
tgSchemas
.
size
;
++
s
)
{
for
(
int
32_t
s
=
0
;
s
<
tgSchemas
.
size
;
++
s
)
{
tgFreeSchema
(
&
tgSchemas
.
schemas
[
s
]);
}
free
(
tgSchemas
.
schemas
);
...
...
@@ -130,7 +130,7 @@ void tgFreeSchemas() {
}
}
void
tgInitSchemas
(
int
size
)
{
void
tgInitSchemas
(
int
32_t
size
)
{
tgFreeSchemas
();
tgSchemas
.
schemas
=
calloc
(
sizeof
(
STgSchema
),
size
);
tgSchemas
.
size
=
0
;
...
...
@@ -154,7 +154,7 @@ void tgParseSchemaMetric(cJSON *metric) {
parsedOk
=
false
;
goto
ParseEnd
;
}
int
nameLen
=
(
in
t
)
strlen
(
name
->
valuestring
);
int
32_t
nameLen
=
(
int32_
t
)
strlen
(
name
->
valuestring
);
if
(
nameLen
==
0
)
{
parsedOk
=
false
;
goto
ParseEnd
;
...
...
@@ -177,7 +177,7 @@ void tgParseSchemaMetric(cJSON *metric) {
parsedOk
=
false
;
goto
ParseEnd
;
}
int
tbnameLen
=
(
in
t
)
strlen
(
tbname
->
valuestring
);
int
32_t
tbnameLen
=
(
int32_
t
)
strlen
(
tbname
->
valuestring
);
if
(
tbnameLen
==
0
)
{
parsedOk
=
false
;
goto
ParseEnd
;
...
...
@@ -191,7 +191,7 @@ void tgParseSchemaMetric(cJSON *metric) {
if
(
fields
==
NULL
)
{
goto
ParseEnd
;
}
int
fieldSize
=
cJSON_GetArraySize
(
fields
);
int
32_t
fieldSize
=
cJSON_GetArraySize
(
fields
);
if
(
fieldSize
<=
0
||
fieldSize
>
TSDB_MAX_COLUMNS
)
{
goto
ParseEnd
;
}
...
...
@@ -199,7 +199,7 @@ void tgParseSchemaMetric(cJSON *metric) {
if
(
fieldSize
>
0
)
{
schema
.
fields
=
calloc
(
sizeof
(
STgSchema
),
(
size_t
)
fieldSize
);
schema
.
fieldNum
=
fieldSize
;
for
(
int
i
=
0
;
i
<
fieldSize
;
i
++
)
{
for
(
int
32_t
i
=
0
;
i
<
fieldSize
;
i
++
)
{
cJSON
*
field
=
cJSON_GetArrayItem
(
fields
,
i
);
if
(
field
==
NULL
)
{
parsedOk
=
false
;
...
...
@@ -209,7 +209,7 @@ void tgParseSchemaMetric(cJSON *metric) {
parsedOk
=
false
;
goto
ParseEnd
;
}
int
nameLen
=
(
in
t
)
strlen
(
field
->
valuestring
);
int
32_t
nameLen
=
(
int32_
t
)
strlen
(
field
->
valuestring
);
if
(
nameLen
==
0
||
nameLen
>=
TSDB_TABLE_NAME_LEN
)
{
parsedOk
=
false
;
goto
ParseEnd
;
...
...
@@ -227,13 +227,13 @@ ParseEnd:
}
}
int
tgParseSchema
(
const
char
*
content
,
char
*
fileName
)
{
int
32_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
;
}
int
size
=
0
;
int
32_t
size
=
0
;
cJSON
*
metrics
=
cJSON_GetObjectItem
(
root
,
"metrics"
);
if
(
metrics
!=
NULL
)
{
size
=
cJSON_GetArraySize
(
metrics
);
...
...
@@ -244,7 +244,7 @@ int tgParseSchema(const char *content, char*fileName) {
}
tgInitSchemas
(
size
);
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
for
(
int
32_t
i
=
0
;
i
<
size
;
i
++
)
{
cJSON
*
metric
=
cJSON_GetArrayItem
(
metrics
,
i
);
if
(
metric
!=
NULL
)
{
tgParseSchemaMetric
(
metric
);
...
...
@@ -260,7 +260,7 @@ int tgParseSchema(const char *content, char*fileName) {
return
size
;
}
int
tgReadSchema
(
char
*
fileName
)
{
int
32_t
tgReadSchema
(
char
*
fileName
)
{
FILE
*
fp
=
fopen
(
fileName
,
"r"
);
if
(
fp
==
NULL
)
{
return
-
1
;
...
...
@@ -286,7 +286,7 @@ int tgReadSchema(char *fileName) {
}
content
[
contentSize
]
=
0
;
int
schemaNum
=
tgParseSchema
(
content
,
fileName
);
int
32_t
schemaNum
=
tgParseSchema
(
content
,
fileName
);
free
(
content
);
fclose
(
fp
);
...
...
@@ -313,53 +313,53 @@ void tgCleanupHandle() {
}
bool
tgGetUserFromUrl
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
pParser
->
path
[
TG_USER_URL_POS
].
len
>=
TSDB_USER_LEN
||
pParser
->
path
[
TG_USER_URL_POS
].
len
<=
0
)
{
HttpParser
*
pParser
=
pContext
->
parser
;
if
(
pParser
->
path
[
TG_USER_URL_POS
].
pos
>=
TSDB_USER_LEN
||
pParser
->
path
[
TG_USER_URL_POS
].
pos
<=
0
)
{
return
false
;
}
tstrncpy
(
pContext
->
user
,
pParser
->
path
[
TG_USER_URL_POS
].
pos
,
sizeof
(
pContext
->
user
));
tstrncpy
(
pContext
->
user
,
pParser
->
path
[
TG_USER_URL_POS
].
str
,
sizeof
(
pContext
->
user
));
return
true
;
}
bool
tgGetPassFromUrl
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
pParser
->
path
[
TG_PASS_URL_POS
].
len
>=
TSDB_PASSWORD_LEN
||
pParser
->
path
[
TG_PASS_URL_POS
].
len
<=
0
)
{
HttpParser
*
pParser
=
pContext
->
parser
;
if
(
pParser
->
path
[
TG_PASS_URL_POS
].
pos
>=
TSDB_PASSWORD_LEN
||
pParser
->
path
[
TG_PASS_URL_POS
].
pos
<=
0
)
{
return
false
;
}
tstrncpy
(
pContext
->
pass
,
pParser
->
path
[
TG_PASS_URL_POS
].
pos
,
sizeof
(
pContext
->
pass
));
tstrncpy
(
pContext
->
pass
,
pParser
->
path
[
TG_PASS_URL_POS
].
str
,
sizeof
(
pContext
->
pass
));
return
true
;
}
char
*
tgGetDbFromUrl
(
HttpContext
*
pContext
)
{
HttpParser
*
pParser
=
&
pContext
->
parser
;
if
(
pParser
->
path
[
TG_DB_URL_POS
].
len
<=
0
)
{
HttpParser
*
pParser
=
pContext
->
parser
;
if
(
pParser
->
path
[
TG_DB_URL_POS
].
pos
<=
0
)
{
httpSendErrorResp
(
pContext
,
HTTP_TG_DB_NOT_INPUT
);
return
NULL
;
}
if
(
pParser
->
path
[
TG_DB_URL_POS
].
len
>=
TSDB_DB_NAME_LEN
)
{
if
(
pParser
->
path
[
TG_DB_URL_POS
].
pos
>=
TSDB_DB_NAME_LEN
)
{
httpSendErrorResp
(
pContext
,
HTTP_TG_DB_TOO_LONG
);
return
NULL
;
}
return
pParser
->
path
[
TG_DB_URL_POS
].
pos
;
return
pParser
->
path
[
TG_DB_URL_POS
].
str
;
}
char
*
tgGetStableName
(
char
*
stname
,
cJSON
*
fields
,
int
fieldsSize
)
{
for
(
int
s
=
0
;
s
<
tgSchemas
.
size
;
++
s
)
{
char
*
tgGetStableName
(
char
*
stname
,
cJSON
*
fields
,
int
32_t
fieldsSize
)
{
for
(
int
32_t
s
=
0
;
s
<
tgSchemas
.
size
;
++
s
)
{
STgSchema
*
schema
=
&
tgSchemas
.
schemas
[
s
];
if
(
strcasecmp
(
schema
->
name
,
stname
)
!=
0
)
{
continue
;
}
bool
schemaMatched
=
true
;
for
(
int
f
=
0
;
f
<
schema
->
fieldNum
;
++
f
)
{
for
(
int
32_t
f
=
0
;
f
<
schema
->
fieldNum
;
++
f
)
{
char
*
fieldName
=
schema
->
fields
[
f
];
bool
fieldMatched
=
false
;
for
(
int
i
=
0
;
i
<
fieldsSize
;
i
++
)
{
for
(
int
32_t
i
=
0
;
i
<
fieldsSize
;
i
++
)
{
cJSON
*
field
=
cJSON_GetArrayItem
(
fields
,
i
);
if
(
strcasecmp
(
field
->
string
,
fieldName
)
==
0
)
{
fieldMatched
=
true
;
...
...
@@ -412,7 +412,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
httpSendErrorResp
(
pContext
,
HTTP_TG_METRIC_NAME_NULL
);
return
false
;
}
int
nameLen
=
(
in
t
)
strlen
(
name
->
valuestring
);
int
32_t
nameLen
=
(
int32_
t
)
strlen
(
name
->
valuestring
);
if
(
nameLen
==
0
)
{
httpSendErrorResp
(
pContext
,
HTTP_TG_METRIC_NAME_NULL
);
return
false
;
...
...
@@ -444,7 +444,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
return
false
;
}
int
tagsSize
=
cJSON_GetArraySize
(
tags
);
int
32_t
tagsSize
=
cJSON_GetArraySize
(
tags
);
if
(
tagsSize
<=
0
)
{
httpSendErrorResp
(
pContext
,
HTTP_TG_TAGS_SIZE_0
);
return
false
;
...
...
@@ -457,7 +457,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
cJSON
*
host
=
NULL
;
for
(
int
i
=
0
;
i
<
tagsSize
;
i
++
)
{
for
(
int
32_t
i
=
0
;
i
<
tagsSize
;
i
++
)
{
cJSON
*
tag
=
cJSON_GetArrayItem
(
tags
,
i
);
if
(
tag
==
NULL
)
{
httpSendErrorResp
(
pContext
,
HTTP_TG_TAG_NULL
);
...
...
@@ -518,7 +518,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
return
false
;
}
int
fieldsSize
=
cJSON_GetArraySize
(
fields
);
int
32_t
fieldsSize
=
cJSON_GetArraySize
(
fields
);
if
(
fieldsSize
<=
0
)
{
httpSendErrorResp
(
pContext
,
HTTP_TG_FIELDS_SIZE_0
);
return
false
;
...
...
@@ -529,7 +529,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
return
false
;
}
for
(
int
i
=
0
;
i
<
fieldsSize
;
i
++
)
{
for
(
int
32_t
i
=
0
;
i
<
fieldsSize
;
i
++
)
{
cJSON
*
field
=
cJSON_GetArrayItem
(
fields
,
i
);
if
(
field
==
NULL
)
{
httpSendErrorResp
(
pContext
,
HTTP_TG_FIELD_NULL
);
...
...
@@ -579,11 +579,11 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
// order by tag name
cJSON
*
orderedTags
[
TG_MAX_SORT_TAG_SIZE
]
=
{
0
};
int
orderTagsLen
=
0
;
for
(
int
i
=
0
;
i
<
tagsSize
;
++
i
)
{
int
32_t
orderTagsLen
=
0
;
for
(
int
32_t
i
=
0
;
i
<
tagsSize
;
++
i
)
{
cJSON
*
tag
=
cJSON_GetArrayItem
(
tags
,
i
);
orderedTags
[
orderTagsLen
++
]
=
tag
;
for
(
int
j
=
orderTagsLen
-
1
;
j
>=
1
;
--
j
)
{
for
(
int
32_t
j
=
orderTagsLen
-
1
;
j
>=
1
;
--
j
)
{
cJSON
*
tag1
=
orderedTags
[
j
];
cJSON
*
tag2
=
orderedTags
[
j
-
1
];
if
(
strcasecmp
(
tag1
->
string
,
"host"
)
==
0
||
strcmp
(
tag1
->
string
,
tag2
->
string
)
<
0
)
{
...
...
@@ -609,7 +609,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
httpShrinkTableName
(
pContext
,
table_cmd
->
stable
,
httpGetCmdsString
(
pContext
,
table_cmd
->
stable
));
// stable tag for detail
for
(
int
i
=
0
;
i
<
orderTagsLen
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
orderTagsLen
;
++
i
)
{
cJSON
*
tag
=
orderedTags
[
i
];
stable_cmd
->
tagNames
[
i
]
=
table_cmd
->
tagNames
[
i
]
=
httpAddToSqlCmdBuffer
(
pContext
,
tag
->
string
);
...
...
@@ -631,7 +631,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
}
else
{
table_cmd
->
table
=
stable_cmd
->
table
=
httpAddToSqlCmdBufferNoTerminal
(
pContext
,
"%s_%d_%d_%s"
,
stname
,
fieldsSize
,
orderTagsLen
,
host
->
valuestring
);
}
for
(
int
i
=
0
;
i
<
orderTagsLen
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
orderTagsLen
;
++
i
)
{
cJSON
*
tag
=
orderedTags
[
i
];
if
(
tag
==
host
)
continue
;
if
(
tag
->
type
==
cJSON_String
)
...
...
@@ -653,7 +653,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
// assembling create stable sql
stable_cmd
->
sql
=
httpAddToSqlCmdBufferNoTerminal
(
pContext
,
"create table if not exists %s.%s(ts timestamp"
,
db
,
httpGetCmdsString
(
pContext
,
table_cmd
->
stable
));
for
(
int
i
=
0
;
i
<
fieldsSize
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
fieldsSize
;
++
i
)
{
cJSON
*
field
=
cJSON_GetArrayItem
(
fields
,
i
);
char
*
field_type
=
"double"
;
if
(
field
->
type
==
cJSON_String
)
...
...
@@ -668,7 +668,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
}
httpAddToSqlCmdBufferNoTerminal
(
pContext
,
") tags("
);
for
(
int
i
=
0
;
i
<
orderTagsLen
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
orderTagsLen
;
++
i
)
{
cJSON
*
tag
=
orderedTags
[
i
];
char
*
tag_type
=
"bigint"
;
if
(
tag
->
type
==
cJSON_String
)
...
...
@@ -689,7 +689,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
table_cmd
->
sql
=
httpAddToSqlCmdBufferNoTerminal
(
pContext
,
"import into %s.%s using %s.%s tags("
,
db
,
httpGetCmdsString
(
pContext
,
table_cmd
->
table
),
db
,
httpGetCmdsString
(
pContext
,
table_cmd
->
stable
));
for
(
int
i
=
0
;
i
<
orderTagsLen
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
orderTagsLen
;
++
i
)
{
cJSON
*
tag
=
orderedTags
[
i
];
if
(
i
!=
orderTagsLen
-
1
)
{
if
(
tag
->
type
==
cJSON_Number
)
...
...
@@ -719,7 +719,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
}
httpAddToSqlCmdBufferNoTerminal
(
pContext
,
" values(%"
PRId64
","
,
timestamp
->
valueint
);
for
(
int
i
=
0
;
i
<
fieldsSize
;
++
i
)
{
for
(
int
32_t
i
=
0
;
i
<
fieldsSize
;
++
i
)
{
cJSON
*
field
=
cJSON_GetArrayItem
(
fields
,
i
);
if
(
i
!=
fieldsSize
-
1
)
{
if
(
field
->
type
==
cJSON_Number
)
...
...
@@ -802,8 +802,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
bool
tgProcessQueryRequest
(
HttpContext
*
pContext
,
char
*
db
)
{
httpDebug
(
"context:%p, fd:%d, process telegraf query msg"
,
pContext
,
pContext
->
fd
);
HttpParser
*
pParser
=
&
pContext
->
parser
;
char
*
filter
=
pParser
->
data
.
pos
;
char
*
filter
=
pContext
->
parser
->
body
.
str
;
if
(
filter
==
NULL
)
{
httpSendErrorResp
(
pContext
,
HTTP_NO_MSG_INPUT
);
return
false
;
...
...
@@ -817,7 +816,7 @@ bool tgProcessQueryRequest(HttpContext *pContext, char *db) {
cJSON
*
metrics
=
cJSON_GetObjectItem
(
root
,
"metrics"
);
if
(
metrics
!=
NULL
)
{
int
size
=
cJSON_GetArraySize
(
metrics
);
int
32_t
size
=
cJSON_GetArraySize
(
metrics
);
httpDebug
(
"context:%p, fd:%d, multiple metrics:%d at one time"
,
pContext
,
pContext
->
fd
,
size
);
if
(
size
<=
0
)
{
httpSendErrorResp
(
pContext
,
HTTP_TG_METRICS_NULL
);
...
...
@@ -825,7 +824,7 @@ bool tgProcessQueryRequest(HttpContext *pContext, char *db) {
return
false
;
}
int
cmdSize
=
size
*
2
+
1
;
int
32_t
cmdSize
=
size
*
2
+
1
;
if
(
cmdSize
>
HTTP_MAX_CMD_SIZE
)
{
httpSendErrorResp
(
pContext
,
HTTP_TG_METRICS_SIZE
);
cJSON_Delete
(
root
);
...
...
@@ -848,7 +847,7 @@ bool tgProcessQueryRequest(HttpContext *pContext, char *db) {
cmd
->
cmdReturnType
=
HTTP_CMD_RETURN_TYPE_NO_RETURN
;
cmd
->
sql
=
httpAddToSqlCmdBuffer
(
pContext
,
"create database if not exists %s"
,
db
);
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
for
(
int
32_t
i
=
0
;
i
<
size
;
i
++
)
{
cJSON
*
metric
=
cJSON_GetArrayItem
(
metrics
,
i
);
if
(
metric
!=
NULL
)
{
if
(
!
tgProcessSingleMetric
(
pContext
,
metric
,
db
))
{
...
...
src/plugins/http/src/httpTgJson.c
浏览文件 @
eff4b580
...
...
@@ -61,19 +61,19 @@ void tgStartQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result)
// data
httpJsonItemToken
(
jsonBuf
);
httpJsonPair
(
jsonBuf
,
"metric"
,
6
,
httpGetCmdsString
(
pContext
,
cmd
->
stable
),
(
int
)
strlen
(
httpGetCmdsString
(
pContext
,
cmd
->
metric
)));
(
int
32_t
)
strlen
(
httpGetCmdsString
(
pContext
,
cmd
->
metric
)));
httpJsonItemToken
(
jsonBuf
);
httpJsonPair
(
jsonBuf
,
"stable"
,
6
,
httpGetCmdsString
(
pContext
,
cmd
->
stable
),
(
int
)
strlen
(
httpGetCmdsString
(
pContext
,
cmd
->
stable
)));
(
int
32_t
)
strlen
(
httpGetCmdsString
(
pContext
,
cmd
->
stable
)));
httpJsonItemToken
(
jsonBuf
);
httpJsonPair
(
jsonBuf
,
"table"
,
5
,
httpGetCmdsString
(
pContext
,
cmd
->
table
),
(
int
)
strlen
(
httpGetCmdsString
(
pContext
,
cmd
->
table
)));
(
int
32_t
)
strlen
(
httpGetCmdsString
(
pContext
,
cmd
->
table
)));
httpJsonItemToken
(
jsonBuf
);
httpJsonPair
(
jsonBuf
,
"timestamp"
,
9
,
httpGetCmdsString
(
pContext
,
cmd
->
timestamp
),
(
int
)
strlen
(
httpGetCmdsString
(
pContext
,
cmd
->
timestamp
)));
// hack way
(
int
32_t
)
strlen
(
httpGetCmdsString
(
pContext
,
cmd
->
timestamp
)));
// hack way
}
void
tgStopQueryJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
)
{
...
...
@@ -88,7 +88,7 @@ void tgStopQueryJson(HttpContext *pContext, HttpSqlCmd *cmd) {
httpJsonToken
(
jsonBuf
,
JsonObjEnd
);
}
void
tgBuildSqlAffectRowsJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
affect_rows
)
{
void
tgBuildSqlAffectRowsJson
(
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
32_t
affect_rows
)
{
JsonBuf
*
jsonBuf
=
httpMallocJsonBuf
(
pContext
);
if
(
jsonBuf
==
NULL
)
return
;
...
...
@@ -96,7 +96,7 @@ void tgBuildSqlAffectRowsJson(HttpContext *pContext, HttpSqlCmd *cmd, int affect
httpJsonPairIntVal
(
jsonBuf
,
"affected_rows"
,
13
,
affect_rows
);
}
bool
tgCheckFinished
(
struct
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
code
)
{
bool
tgCheckFinished
(
struct
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
32_t
code
)
{
HttpSqlCmds
*
multiCmds
=
pContext
->
multiCmds
;
httpDebug
(
"context:%p, fd:%d, check telegraf command, code:%s, state:%d, type:%d, rettype:%d, tags:%d"
,
pContext
,
pContext
->
fd
,
tstrerror
(
code
),
cmd
->
cmdState
,
cmd
->
cmdType
,
cmd
->
cmdReturnType
,
cmd
->
tagNum
);
...
...
@@ -133,7 +133,7 @@ bool tgCheckFinished(struct HttpContext *pContext, HttpSqlCmd *cmd, int code) {
return
true
;
}
void
tgSetNextCmd
(
struct
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
code
)
{
void
tgSetNextCmd
(
struct
HttpContext
*
pContext
,
HttpSqlCmd
*
cmd
,
int
32_t
code
)
{
HttpSqlCmds
*
multiCmds
=
pContext
->
multiCmds
;
httpDebug
(
"context:%p, fd:%d, get telegraf next command, pos:%d, code:%s, state:%d, type:%d, rettype:%d, tags:%d"
,
pContext
,
pContext
->
fd
,
multiCmds
->
pos
,
tstrerror
(
code
),
cmd
->
cmdState
,
cmd
->
cmdType
,
cmd
->
cmdReturnType
,
...
...
src/plugins/http/src/httpUtil.c
浏览文件 @
eff4b580
...
...
@@ -29,7 +29,7 @@ bool httpCheckUsedbSql(char *sql) {
return
false
;
}
void
httpTimeToString
(
time_t
t
,
char
*
buf
,
int
buflen
)
{
void
httpTimeToString
(
time_t
t
,
char
*
buf
,
int
32_t
buflen
)
{
memset
(
buf
,
0
,
(
size_t
)
buflen
);
char
ts
[
32
]
=
{
0
};
...
...
@@ -44,13 +44,13 @@ int32_t httpAddToSqlCmdBuffer(HttpContext *pContext, const char *const format, .
HttpSqlCmds
*
cmd
=
pContext
->
multiCmds
;
if
(
cmd
->
buffer
==
NULL
)
return
-
1
;
int
remainLength
=
cmd
->
bufferSize
-
cmd
->
bufferPos
;
int
32_t
remainLength
=
cmd
->
bufferSize
-
cmd
->
bufferPos
;
if
(
remainLength
<
4096
)
{
if
(
!
httpReMallocMultiCmdsBuffer
(
pContext
,
cmd
->
bufferSize
*
2
))
return
-
1
;
}
char
*
buffer
=
cmd
->
buffer
+
cmd
->
bufferPos
;
int
len
=
0
;
char
*
buffer
=
cmd
->
buffer
+
cmd
->
bufferPos
;
int
32_t
len
=
0
;
va_list
argpointer
;
va_start
(
argpointer
,
format
);
...
...
@@ -76,13 +76,13 @@ int32_t httpAddToSqlCmdBufferNoTerminal(HttpContext *pContext, const char *const
HttpSqlCmds
*
cmd
=
pContext
->
multiCmds
;
if
(
cmd
->
buffer
==
NULL
)
return
-
1
;
int
remainLength
=
cmd
->
bufferSize
-
cmd
->
bufferPos
;
int
32_t
remainLength
=
cmd
->
bufferSize
-
cmd
->
bufferPos
;
if
(
remainLength
<
4096
)
{
if
(
!
httpReMallocMultiCmdsBuffer
(
pContext
,
cmd
->
bufferSize
*
2
))
return
-
1
;
}
char
*
buffer
=
cmd
->
buffer
+
cmd
->
bufferPos
;
int
len
=
0
;
char
*
buffer
=
cmd
->
buffer
+
cmd
->
bufferPos
;
int
32_t
len
=
0
;
va_list
argpointer
;
va_start
(
argpointer
,
format
);
...
...
@@ -107,7 +107,7 @@ int32_t httpAddToSqlCmdBufferTerminal(HttpContext *pContext) {
HttpSqlCmds
*
cmd
=
pContext
->
multiCmds
;
if
(
cmd
->
buffer
==
NULL
)
return
-
1
;
int
remainLength
=
cmd
->
bufferSize
-
cmd
->
bufferPos
;
int
32_t
remainLength
=
cmd
->
bufferSize
-
cmd
->
bufferPos
;
if
(
remainLength
<
4096
)
{
if
(
!
httpReMallocMultiCmdsBuffer
(
pContext
,
cmd
->
bufferSize
*
2
))
return
-
1
;
}
...
...
@@ -124,7 +124,7 @@ int32_t httpAddToSqlCmdBufferTerminal(HttpContext *pContext) {
return
(
int32_t
)(
buffer
-
cmd
->
buffer
);
}
int32_t
httpAddToSqlCmdBufferWithSize
(
HttpContext
*
pContext
,
int
mallocSize
)
{
int32_t
httpAddToSqlCmdBufferWithSize
(
HttpContext
*
pContext
,
int
32_t
mallocSize
)
{
HttpSqlCmds
*
cmd
=
pContext
->
multiCmds
;
if
(
cmd
->
buffer
==
NULL
)
return
-
1
;
...
...
@@ -139,7 +139,7 @@ int32_t httpAddToSqlCmdBufferWithSize(HttpContext *pContext, int mallocSize) {
return
(
int32_t
)(
buffer
-
cmd
->
buffer
);
}
bool
httpMallocMultiCmds
(
HttpContext
*
pContext
,
int
cmdSize
,
in
t
bufferSize
)
{
bool
httpMallocMultiCmds
(
HttpContext
*
pContext
,
int
32_t
cmdSize
,
int32_
t
bufferSize
)
{
if
(
cmdSize
>
HTTP_MAX_CMD_SIZE
)
{
httpError
(
"context:%p, fd:%d, user:%s, mulitcmd size:%d large then %d"
,
pContext
,
pContext
->
fd
,
pContext
->
user
,
cmdSize
,
HTTP_MAX_CMD_SIZE
);
...
...
@@ -186,7 +186,7 @@ bool httpMallocMultiCmds(HttpContext *pContext, int cmdSize, int bufferSize) {
return
true
;
}
bool
httpReMallocMultiCmdsSize
(
HttpContext
*
pContext
,
int
cmdSize
)
{
bool
httpReMallocMultiCmdsSize
(
HttpContext
*
pContext
,
int
32_t
cmdSize
)
{
HttpSqlCmds
*
multiCmds
=
pContext
->
multiCmds
;
if
(
cmdSize
>
HTTP_MAX_CMD_SIZE
)
{
...
...
@@ -206,7 +206,7 @@ bool httpReMallocMultiCmdsSize(HttpContext *pContext, int cmdSize) {
return
true
;
}
bool
httpReMallocMultiCmdsBuffer
(
HttpContext
*
pContext
,
int
bufferSize
)
{
bool
httpReMallocMultiCmdsBuffer
(
HttpContext
*
pContext
,
int
32_t
bufferSize
)
{
HttpSqlCmds
*
multiCmds
=
pContext
->
multiCmds
;
if
(
bufferSize
>
HTTP_MAX_BUFFER_SIZE
)
{
...
...
@@ -258,7 +258,7 @@ bool httpCompareMethod(HttpDecodeMethod *pSrc, HttpDecodeMethod *pCmp) {
}
void
httpAddMethod
(
HttpServer
*
pServer
,
HttpDecodeMethod
*
pMethod
)
{
int
pos
=
0
;
int
32_t
pos
=
0
;
for
(
pos
=
0
;
pos
<
pServer
->
methodScannerLen
;
++
pos
)
{
if
(
httpCompareMethod
(
pServer
->
methodScanner
[
pos
],
pMethod
))
{
break
;
...
...
@@ -293,13 +293,13 @@ HttpSqlCmd *httpCurrSqlCmd(HttpContext *pContext) {
return
multiCmds
->
cmds
+
multiCmds
->
size
-
1
;
}
int
httpNextSqlCmdPos
(
HttpContext
*
pContext
)
{
int
32_t
httpNextSqlCmdPos
(
HttpContext
*
pContext
)
{
HttpSqlCmds
*
multiCmds
=
pContext
->
multiCmds
;
return
multiCmds
->
size
;
}
void
httpTrimTableName
(
char
*
name
)
{
for
(
int
i
=
0
;
name
[
i
]
!=
0
;
i
++
)
{
for
(
int
32_t
i
=
0
;
name
[
i
]
!=
0
;
i
++
)
{
if
(
name
[
i
]
==
' '
||
name
[
i
]
==
':'
||
name
[
i
]
==
'.'
||
name
[
i
]
==
'-'
||
name
[
i
]
==
'/'
||
name
[
i
]
==
'\''
)
name
[
i
]
=
'_'
;
if
(
i
==
TSDB_TABLE_NAME_LEN
)
{
...
...
@@ -309,9 +309,9 @@ void httpTrimTableName(char *name) {
}
}
int
httpShrinkTableName
(
HttpContext
*
pContext
,
in
t
pos
,
char
*
name
)
{
int
len
=
0
;
for
(
int
i
=
0
;
name
[
i
]
!=
0
;
i
++
)
{
int
32_t
httpShrinkTableName
(
HttpContext
*
pContext
,
int32_
t
pos
,
char
*
name
)
{
int
32_t
len
=
0
;
for
(
int
32_t
i
=
0
;
name
[
i
]
!=
0
;
i
++
)
{
if
(
name
[
i
]
==
' '
||
name
[
i
]
==
':'
||
name
[
i
]
==
'.'
||
name
[
i
]
==
'-'
||
name
[
i
]
==
'/'
||
name
[
i
]
==
'\''
||
name
[
i
]
==
'\"'
)
name
[
i
]
=
'_'
;
...
...
@@ -327,7 +327,7 @@ int httpShrinkTableName(HttpContext *pContext, int pos, char *name) {
MD5Update
(
&
context
,
(
uint8_t
*
)
name
,
(
uint32_t
)
len
);
MD5Final
(
&
context
);
int
table_name
=
httpAddToSqlCmdBuffer
(
int
32_t
table_name
=
httpAddToSqlCmdBuffer
(
pContext
,
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
,
context
.
digest
[
0
],
context
.
digest
[
1
],
context
.
digest
[
2
],
context
.
digest
[
3
],
context
.
digest
[
4
],
context
.
digest
[
5
],
context
.
digest
[
6
],
context
.
digest
[
7
],
context
.
digest
[
8
],
context
.
digest
[
9
],
context
.
digest
[
10
],
context
.
digest
[
11
],
...
...
@@ -340,7 +340,7 @@ int httpShrinkTableName(HttpContext *pContext, int pos, char *name) {
return
table_name
;
}
char
*
httpGetCmdsString
(
HttpContext
*
pContext
,
int
pos
)
{
char
*
httpGetCmdsString
(
HttpContext
*
pContext
,
int
32_t
pos
)
{
HttpSqlCmds
*
multiCmds
=
pContext
->
multiCmds
;
if
(
pos
<
0
||
pos
>=
multiCmds
->
bufferSize
)
{
return
""
;
...
...
@@ -349,8 +349,8 @@ char *httpGetCmdsString(HttpContext *pContext, int pos) {
return
multiCmds
->
buffer
+
pos
;
}
int
httpGzipDeCompress
(
char
*
srcData
,
int32_t
nSrcData
,
char
*
destData
,
int32_t
*
nDestData
)
{
int
err
=
0
;
int
32_t
httpGzipDeCompress
(
char
*
srcData
,
int32_t
nSrcData
,
char
*
destData
,
int32_t
*
nDestData
)
{
int
32_t
err
=
0
;
z_stream
gzipStream
=
{
0
};
static
char
dummyHead
[
2
]
=
{
...
...
@@ -393,7 +393,7 @@ int httpGzipDeCompress(char *srcData, int32_t nSrcData, char *destData, int32_t
return
0
;
}
int
httpGzipCompressInit
(
HttpContext
*
pContext
)
{
int
32_t
httpGzipCompressInit
(
HttpContext
*
pContext
)
{
pContext
->
gzipStream
.
zalloc
=
(
alloc_func
)
0
;
pContext
->
gzipStream
.
zfree
=
(
free_func
)
0
;
pContext
->
gzipStream
.
opaque
=
(
voidpf
)
0
;
...
...
@@ -404,8 +404,8 @@ int httpGzipCompressInit(HttpContext *pContext) {
return
0
;
}
int
httpGzipCompress
(
HttpContext
*
pContext
,
char
*
srcData
,
int32_t
nSrcData
,
char
*
destData
,
int32_t
*
nDestData
,
bool
isTheLast
)
{
int
err
=
0
;
int
32_t
httpGzipCompress
(
HttpContext
*
pContext
,
char
*
srcData
,
int32_t
nSrcData
,
char
*
destData
,
int32_t
*
nDestData
,
bool
isTheLast
)
{
int
32_t
err
=
0
;
pContext
->
gzipStream
.
next_in
=
(
Bytef
*
)
srcData
;
pContext
->
gzipStream
.
avail_in
=
(
uLong
)
nSrcData
;
pContext
->
gzipStream
.
next_out
=
(
Bytef
*
)
destData
;
...
...
@@ -439,3 +439,21 @@ int httpGzipCompress(HttpContext *pContext, char *srcData, int32_t nSrcData, cha
*
nDestData
=
(
int32_t
)
(
pContext
->
gzipStream
.
total_out
);
return
0
;
}
bool
httpUrlMatch
(
HttpContext
*
pContext
,
int32_t
pos
,
char
*
cmp
)
{
HttpParser
*
pParser
=
pContext
->
parser
;
if
(
pos
<
0
||
pos
>=
HTTP_MAX_URL
)
{
return
false
;
}
if
(
pParser
->
path
[
pos
].
pos
<=
0
)
{
return
false
;
}
if
(
strcmp
(
pParser
->
path
[
pos
].
str
,
cmp
)
!=
0
)
{
return
false
;
}
return
true
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录