From 521d7c1519ce5b8bee984189e392c5308dafd7a0 Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 1 Aug 2020 08:22:03 +0800 Subject: [PATCH 01/30] add ehttp_* --- src/common/inc/elog.h | 71 ++ src/common/src/elog.c | 95 +++ src/plugins/http/inc/ehttp_gzip.h | 30 + src/plugins/http/inc/ehttp_parser.h | 36 + src/plugins/http/inc/ehttp_util_string.h | 18 + src/plugins/http/inc/httpInt.h | 11 + src/plugins/http/src/ehttp_gzip.c | 154 ++++ src/plugins/http/src/ehttp_parser.c | 965 +++++++++++++++++++++++ src/plugins/http/src/ehttp_util_string.c | 30 + src/plugins/http/src/httpContext.c | 161 ++++ src/plugins/http/src/httpServer.c | 63 ++ 11 files changed, 1634 insertions(+) create mode 100644 src/common/inc/elog.h create mode 100644 src/common/src/elog.c create mode 100644 src/plugins/http/inc/ehttp_gzip.h create mode 100644 src/plugins/http/inc/ehttp_parser.h create mode 100644 src/plugins/http/inc/ehttp_util_string.h create mode 100644 src/plugins/http/src/ehttp_gzip.c create mode 100644 src/plugins/http/src/ehttp_parser.c create mode 100644 src/plugins/http/src/ehttp_util_string.c diff --git a/src/common/inc/elog.h b/src/common/inc/elog.h new file mode 100644 index 0000000000..8e11c3e761 --- /dev/null +++ b/src/common/inc/elog.h @@ -0,0 +1,71 @@ +#ifndef _elog_h_8897be44_dda8_45b6_9d37_8d8691cb05fb_ +#define _elog_h_8897be44_dda8_45b6_9d37_8d8691cb05fb_ + +#include + +typedef enum { + ELOG_DEBUG, + ELOG_INFO, + ELOG_WARN, + ELOG_ERROR, + ELOG_CRITICAL, + ELOG_VERBOSE, + ELOG_ABORT, +} ELOG_LEVEL; + +void elog_set_level(ELOG_LEVEL base); // only log those not less than base +void elog_set_thread_name(const char *name); + +void elog(ELOG_LEVEL level, int fd, const char *file, int line, const char *func, const char *fmt, ...) +#ifdef __GNUC__ + __attribute__((format(printf, 6, 7))) +#endif +; + +#define DLOG(fd, fmt, ...) elog(ELOG_DEBUG, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) +#define ILOG(fd, fmt, ...) elog(ELOG_INFO, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) +#define WLOG(fd, fmt, ...) elog(ELOG_WARN, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) +#define ELOG(fd, fmt, ...) elog(ELOG_ERROR, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) +#define CLOG(fd, fmt, ...) elog(ELOG_CRITICAL, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) +#define VLOG(fd, fmt, ...) elog(ELOG_VERBOSE, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) +#define ALOG(fd, fmt, ...) elog(ELOG_ABORT, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) + +#define D(fmt, ...) elog(ELOG_DEBUG, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) +#define I(fmt, ...) elog(ELOG_INFO, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) +#define W(fmt, ...) elog(ELOG_WARN, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) +#define E(fmt, ...) elog(ELOG_ERROR, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) +#define C(fmt, ...) elog(ELOG_CRITICAL, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) +#define V(fmt, ...) elog(ELOG_VERBOSE, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) +#define A(fmt, ...) elog(ELOG_ABORT, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) + + + +// NOTE: https://en.wikipedia.org/wiki/Fail-fast +// for the sake of simplicity, both implementation and usage, +// we'll follow `fail-fast` or `let-it-crash` philosophy. + +// assertion in both debug/release build +#define EQ_ABORT(fmt, ...) A("Assertion failure: "fmt, ##__VA_ARGS__) + +#define EQ_ASSERT(statement) do { \ + if (statement) break; \ + A("Assertion failure: %s", #statement); \ +} while (0) + +#define EQ_ASSERT_EXT(statement, fmt, ...) do { \ + if (statement) break; \ + A("Assertion failure: %s: "fmt, #statement, ##__VA_ARGS__); \ +} while (0) + +#define EQ_ASSERT_API0(statement) do { \ + if (statement) break; \ + A("Assertion failure: %s failed: [%d]%s", #statement, errno, strerror(errno)); \ +} while (0) + +#define EQ_ASSERT_API(api) do { \ + A("Assertion failure: %s failed: [%d]%s", #api, errno, strerror(errno)); \ +} while (0) + + +#endif // _elog_h_8897be44_dda8_45b6_9d37_8d8691cb05fb_ + diff --git a/src/common/src/elog.c b/src/common/src/elog.c new file mode 100644 index 0000000000..95b580962c --- /dev/null +++ b/src/common/src/elog.c @@ -0,0 +1,95 @@ +#include "elog.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#define gettid() syscall(__NR_gettid) + +static ELOG_LEVEL elog_level_base = ELOG_DEBUG; + +static __thread long elog_thread_id; +static __thread char elog_thread_name[24] = {0}; + +void elog_set_level(ELOG_LEVEL base) { + elog_level_base = base; +} + +void elog_set_thread_name(const char *name) { + elog_thread_id = gettid(); + snprintf(elog_thread_name, sizeof(elog_thread_name), "%s", name); +} + +void elog(ELOG_LEVEL level, int fd, const char *file, int line, const char *func, const char *fmt, ...) +{ + if (level < elog_level_base) return; + if (fd == -1) return; + + if (elog_thread_name[0]=='\0') { + elog_set_thread_name("unknown"); + } + + char *p; + int n; + size_t bytes; + + char buf[4096]; + snprintf(buf, sizeof(buf), "%s", file); + + char fn[1024]; + snprintf(fn, sizeof(fn), "%s", basename(buf)); + + char C; + switch (level) { + case ELOG_DEBUG: C = 'D'; break; + case ELOG_INFO: C = 'I'; break; + case ELOG_WARN: C = 'W'; break; + case ELOG_ERROR: C = 'E'; break; + case ELOG_CRITICAL: C = 'C'; break; + case ELOG_VERBOSE: C = 'V'; break; + case ELOG_ABORT: C = 'A'; break; + default: return; + } + + struct tm t; + struct timeval tv; + + if (gettimeofday(&tv, NULL)) return; + if (!localtime_r(&tv.tv_sec, &t)) return; + + p = buf; + bytes = sizeof(buf); + + n = snprintf(p, bytes, "%c[%02d/%02d %02d:%02d:%02d.%06ld][%06ld]: ==", + C, + t.tm_mon + 1, t.tm_mday, + t.tm_hour, t.tm_min, t.tm_sec, + tv.tv_usec, + elog_thread_id); + p += n; bytes -= n; + + va_list arg; + va_start(arg, fmt); + if (bytes>0) { + n = vsnprintf(p, bytes, fmt, arg); + p += n; bytes -= n; + } + va_end(arg); + + if (bytes>0) { + n = snprintf(p, bytes, "== t:%s#%s[%d]#%s()", + elog_thread_name, fn, line, func); + } + + dprintf(fd, "%s\n", buf); + + if (level == ELOG_ABORT) { + abort(); + } +} + diff --git a/src/plugins/http/inc/ehttp_gzip.h b/src/plugins/http/inc/ehttp_gzip.h new file mode 100644 index 0000000000..b2d6ace9b0 --- /dev/null +++ b/src/plugins/http/inc/ehttp_gzip.h @@ -0,0 +1,30 @@ +#ifndef _ehttp_gzip_h_9196791b_ac2a_4d73_9979_f4b41abbc4c0_ +#define _ehttp_gzip_h_9196791b_ac2a_4d73_9979_f4b41abbc4c0_ + +#include + +#define EHTTP_GZIP_CHUNK_SIZE_DEFAULT (1024*16) + +typedef struct ehttp_gzip_s ehttp_gzip_t; + +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); +}; + +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 +}; + +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); + +#endif // _ehttp_gzip_h_9196791b_ac2a_4d73_9979_f4b41abbc4c0_ + diff --git a/src/plugins/http/inc/ehttp_parser.h b/src/plugins/http/inc/ehttp_parser.h new file mode 100644 index 0000000000..be87650128 --- /dev/null +++ b/src/plugins/http/inc/ehttp_parser.h @@ -0,0 +1,36 @@ +#ifndef _ehttp_parser_fc7f9ac9_52da_4ee3_b556_deb2e1c3866e +#define _ehttp_parser_fc7f9ac9_52da_4ee3_b556_deb2e1c3866e + +#include + +typedef struct ehttp_parser_s ehttp_parser_t; +typedef struct ehttp_parser_callbacks_s ehttp_parser_callbacks_t; +typedef struct ehttp_parser_conf_s ehttp_parser_conf_t; +typedef struct ehttp_status_code_s ehttp_status_code_t; + +struct ehttp_parser_callbacks_s { + 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); +}; + +struct ehttp_parser_conf_s { + size_t flush_block_size; // <=0: immediately +}; + +ehttp_parser_t* ehttp_parser_create(ehttp_parser_callbacks_t callbacks, ehttp_parser_conf_t conf, void *arg); +void ehttp_parser_destroy(ehttp_parser_t *parser); +int ehttp_parser_parse(ehttp_parser_t *parser, const char *buf, size_t len); +int ehttp_parser_parse_string(ehttp_parser_t *parser, const char *str); +int ehttp_parser_parse_char(ehttp_parser_t *parser, const char c); +int ehttp_parser_parse_end(ehttp_parser_t *parser); + +char* ehttp_parser_urldecode(const char *enc); + +const char* ehttp_status_code_get_desc(const int status_code); + +#endif // _ehttp_parser_fc7f9ac9_52da_4ee3_b556_deb2e1c3866e + diff --git a/src/plugins/http/inc/ehttp_util_string.h b/src/plugins/http/inc/ehttp_util_string.h new file mode 100644 index 0000000000..46c5a42827 --- /dev/null +++ b/src/plugins/http/inc/ehttp_util_string.h @@ -0,0 +1,18 @@ +#ifndef _ehttp_util_string_h_99dacde5_2e7d_4662_97d6_04611fde683b_ +#define _ehttp_util_string_h_99dacde5_2e7d_4662_97d6_04611fde683b_ + +#include + +typedef struct ehttp_util_string_s ehttp_util_string_t; + +struct ehttp_util_string_s { + char *str; + size_t len; +}; + +void ehttp_util_string_cleanup(ehttp_util_string_t *str); +int ehttp_util_string_append(ehttp_util_string_t *str, const char *s, size_t len); +void ehttp_util_string_clear(ehttp_util_string_t *str); + +#endif // _ehttp_util_string_h_99dacde5_2e7d_4662_97d6_04611fde683b_ + diff --git a/src/plugins/http/inc/httpInt.h b/src/plugins/http/inc/httpInt.h index ffd621be7a..044b5cc4cc 100644 --- a/src/plugins/http/inc/httpInt.h +++ b/src/plugins/http/inc/httpInt.h @@ -28,6 +28,8 @@ #include "httpLog.h" #include "httpJson.h" +#include "ehttp_parser.h" + #define HTTP_MAX_CMD_SIZE 1024 #define HTTP_MAX_BUFFER_SIZE 1024*1024 @@ -162,6 +164,11 @@ typedef struct { 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; @@ -172,6 +179,10 @@ typedef struct { HttpBuf data; // body content HttpBuf token; // auth token HttpDecodeMethod *pMethod; + + ehttp_parser_t *parser; + int inited:2; + int failed:4; } HttpParser; typedef struct HttpContext { diff --git a/src/plugins/http/src/ehttp_gzip.c b/src/plugins/http/src/ehttp_gzip.c new file mode 100644 index 0000000000..ded344dfea --- /dev/null +++ b/src/plugins/http/src/ehttp_gzip.c @@ -0,0 +1,154 @@ +#include "ehttp_gzip.h" + +#include "os.h" +#include "zlib.h" + +#include + +typedef enum { + EHTTP_GZIP_INITING, + EHTTP_GZIP_READY, + EHTTP_GZIP_CLOSED, +} EHTTP_GZIP_STATE; + +struct ehttp_gzip_s { + ehttp_gzip_conf_t conf; + ehttp_gzip_callbacks_t callbacks; + void *arg; + z_stream *gzip; + gz_header *header; + char *chunk; + + int state; +}; + +static void dummy_on_data(ehttp_gzip_t *gzip, void *arg, const char *buf, size_t len) { +} + +static void ehttp_gzip_cleanup(ehttp_gzip_t *gzip) { + switch(gzip->state) { + case EHTTP_GZIP_READY: { + inflateEnd(gzip->gzip); + } break; + default: break; + } + if (gzip->gzip) { + free(gzip->gzip); + gzip->gzip = NULL; + } + if (gzip->header) { + free(gzip->header); + gzip->header = NULL; + } + if (gzip->chunk) { + free(gzip->chunk); + gzip->chunk = NULL; + } + gzip->state = EHTTP_GZIP_CLOSED; +} + +ehttp_gzip_t* ehttp_gzip_create_decompressor(ehttp_gzip_conf_t conf, ehttp_gzip_callbacks_t callbacks, void *arg) { + ehttp_gzip_t *gzip = (ehttp_gzip_t*)calloc(1, sizeof(*gzip)); + if (!gzip) return NULL; + + do { + gzip->conf = conf; + gzip->callbacks = callbacks; + gzip->arg = arg; + if (gzip->callbacks.on_data == NULL) gzip->callbacks.on_data = dummy_on_data; + gzip->gzip = (z_stream*)calloc(1, sizeof(*gzip->gzip)); + if (gzip->conf.get_header) { + gzip->header = (gz_header*)calloc(1, sizeof(*gzip->header)); + } + if (gzip->conf.chunk_size<=0) gzip->conf.chunk_size = EHTTP_GZIP_CHUNK_SIZE_DEFAULT; + gzip->chunk = (char*)malloc(gzip->conf.chunk_size); + if (!gzip->gzip || (gzip->conf.get_header && !gzip->header) || !gzip->chunk) break; + gzip->gzip->zalloc = Z_NULL; + gzip->gzip->zfree = Z_NULL; + gzip->gzip->opaque = Z_NULL; + + // 863 windowBits can also be greater than 15 for optional gzip decoding. Add + // 864 32 to windowBits to enable zlib and gzip decoding with automatic header + // 865 detection, or add 16 to decode only the gzip format (the zlib format will + // 866 return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a + // 867 CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see + // 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 + if (ret != Z_OK) break; + if (gzip->header) { + ret = inflateGetHeader(gzip->gzip, gzip->header); + } + if (ret != Z_OK) break; + + gzip->gzip->next_out = (z_const Bytef*)gzip->chunk; + gzip->gzip->avail_out = gzip->conf.chunk_size; + gzip->state = EHTTP_GZIP_READY; + return gzip; + } while (0); + + ehttp_gzip_destroy(gzip); + return NULL; +} + +ehttp_gzip_t* ehttp_gzip_create_compressor(ehttp_gzip_conf_t conf, ehttp_gzip_callbacks_t callbacks, void *arg); + +void ehttp_gzip_destroy(ehttp_gzip_t *gzip) { + ehttp_gzip_cleanup(gzip); + + free(gzip); +} + +int ehttp_gzip_write(ehttp_gzip_t *gzip, const char *buf, size_t len) { + if (gzip->state != EHTTP_GZIP_READY) return -1; + if (len <= 0) return 0; + + gzip->gzip->next_in = (z_const Bytef*)buf; + gzip->gzip->avail_in = len; + + while (gzip->gzip->avail_in) { + int ret; + if (gzip->header) { + ret = inflate(gzip->gzip, Z_BLOCK); + } else { + ret = inflate(gzip->gzip, Z_SYNC_FLUSH); + } + if (ret != Z_OK && ret != Z_STREAM_END) return -1; + + if (gzip->gzip->avail_out>0) { + if (ret!=Z_STREAM_END) continue; + } + + size_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); + gzip->gzip->next_out = (z_const Bytef*)gzip->chunk; + gzip->gzip->avail_out = gzip->conf.chunk_size; + } + + return 0; +} + +int 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; + 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; + + gzip->gzip->next_out[0] = '\0'; + gzip->callbacks.on_data(gzip, gzip->arg, gzip->chunk, len); + gzip->gzip->next_out = NULL; + gzip->gzip->avail_out = 0; + + return 0; +} + diff --git a/src/plugins/http/src/ehttp_parser.c b/src/plugins/http/src/ehttp_parser.c new file mode 100644 index 0000000000..fbe15661b5 --- /dev/null +++ b/src/plugins/http/src/ehttp_parser.c @@ -0,0 +1,965 @@ +#include "ehttp_parser.h" + +#include "ehttp_gzip.h" +#include "ehttp_util_string.h" +#include "elog.h" + +#include +#include +#include + +struct ehttp_status_code_s { + int status_code; + const char *status_desc; +}; + +static ehttp_status_code_t status_codes[] = { + {100, "Continue"}, + {101, "Switching Protocol"}, + {102, "Processing (WebDAV)"}, + {103, "Early Hints"}, + {200, "OK"}, + {201, "Created"}, + {202, "Accepted"}, + {203, "Non-Authoritative Information"}, + {204, "No Content"}, + {205, "Reset Content"}, + {206, "Partial Content"}, + {207, "Multi-Status (WebDAV)"}, + {208, "Already Reported (WebDAV)"}, + {226, "IM Used (HTTP Delta encoding)"}, + {300, "Multiple Choice"}, + {301, "Moved Permanently"}, + {302, "Found"}, + {303, "See Other"}, + {304, "Not Modified"}, + {305, "Use Proxy"}, + {306, "unused"}, + {307, "Temporary Redirect"}, + {308, "Permanent Redirect"}, + {400, "Bad Request"}, + {401, "Unauthorized"}, + {402, "Payment Required"}, + {403, "Forbidden"}, + {404, "Not Found"}, + {405, "Method Not Allowed"}, + {406, "Not Acceptable"}, + {407, "Proxy Authentication Required"}, + {408, "Request Timeout"}, + {409, "Conflict"}, + {410, "Gone"}, + {411, "Length Required"}, + {412, "Precondition Failed"}, + {413, "Payload Too Large"}, + {414, "URI Too Long"}, + {415, "Unsupported Media Type"}, + {416, "Range Not Satisfiable"}, + {417, "Expectation Failed"}, + {418, "I'm a teapot"}, + {421, "Misdirected Request"}, + {422, "Unprocessable Entity (WebDAV)"}, + {423, "Locked (WebDAV)"}, + {424, "Failed Dependency (WebDAV)"}, + {425, "Too Early"}, + {426, "Upgrade Required"}, + {428, "Precondition Required"}, + {429, "Too Many Requests"}, + {431, "Request Header Fields Too Large"}, + {451, "Unavailable For Legal Reasons"}, + {500, "Internal Server Error"}, + {501, "Not Implemented"}, + {502, "Bad Gateway"}, + {503, "Service Unavailable"}, + {504, "Gateway Timeout"}, + {505, "HTTP Version Not Supported"}, + {506, "Variant Also Negotiates"}, + {507, "Insufficient Storage"}, + {508, "Loop Detected (WebDAV)"}, + {510, "Not Extended"}, + {511, "Network Authentication Required"}, + {0, NULL} +}; + +const char* ehttp_status_code_get_desc(const int status_code) { + ehttp_status_code_t *p = status_codes; + while (p->status_code!=0) { + if (p->status_code==status_code) return p->status_desc; + ++p; + } + return "Unknow status code"; +} + +typedef enum HTTP_PARSER_STATE { + HTTP_PARSER_BEGIN, + HTTP_PARSER_REQUEST_OR_RESPONSE, + HTTP_PARSER_METHOD, + HTTP_PARSER_TARGET, + HTTP_PARSER_HTTP_VERSION, + HTTP_PARSER_SP, + HTTP_PARSER_STATUS_CODE, + HTTP_PARSER_REASON_PHRASE, + HTTP_PARSER_CRLF, + HTTP_PARSER_HEADER, + HTTP_PARSER_HEADER_KEY, + HTTP_PARSER_HEADER_VAL, + HTTP_PARSER_CHUNK_SIZE, + HTTP_PARSER_CHUNK, + HTTP_PARSER_END, + HTTP_PARSER_ERROR, +} HTTP_PARSER_STATE; + +typedef struct ehttp_parser_kv_s ehttp_parser_kv_t; + +struct ehttp_parser_kv_s { + char *key; + char *val; +}; + +struct ehttp_parser_s { + ehttp_parser_callbacks_t callbacks; + void *arg; + ehttp_parser_conf_t conf; + + 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; + ehttp_parser_kv_t *kvs; + size_t kvs_count; + + char *auth_basic; + + size_t content_length; + + size_t chunk_size; + size_t received_chunk_size; + size_t received_size; + + ehttp_gzip_t *gzip; + ehttp_util_string_t str; + HTTP_PARSER_STATE *stacks; + size_t stacks_count; +}; + +static void dummy_on_request_line(void *arg, const char *method, const char *target, const char *version, const char *target_raw) { +} + +static void dummy_on_status_line(void *arg, const char *version, int status_code, const char *reason_phrase) { +} + +static void dummy_on_header_field(void *arg, const char *key, const char *val) { +} + +static void dummy_on_body(void *arg, const char *chunk, size_t len) { +} + +static void dummy_on_end(void *arg) { +} + +static void dummy_on_error(void *arg, int status_code) { +} + + +static HTTP_PARSER_STATE ehttp_parser_top(ehttp_parser_t *parser) { + EQ_ASSERT(parser->stacks_count >= 1); + EQ_ASSERT(parser->stacks); + + return parser->stacks[parser->stacks_count-1]; +} + +static int ehttp_parser_push(ehttp_parser_t *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)); + if (!stacks) return -1; + + parser->stacks_count = n; + parser->stacks = stacks; + parser->stacks[n-1] = state; + + return 0; +} + +static int ehttp_parser_pop(ehttp_parser_t *parser) { + if (parser->stacks_count <= 0) return -1; + --parser->stacks_count; + + return 0; +} + +ehttp_parser_t *ehttp_parser_create(ehttp_parser_callbacks_t callbacks, ehttp_parser_conf_t conf, void *arg) { + ehttp_parser_t *parser = (ehttp_parser_t*)calloc(1, sizeof(*parser)); + if (!parser) return NULL; + + parser->callbacks = callbacks; + parser->arg = arg; + parser->conf = conf; + + if (parser->callbacks.on_request_line == NULL) { + parser->callbacks.on_request_line = dummy_on_request_line; + } + if (parser->callbacks.on_status_line == NULL) { + parser->callbacks.on_status_line = dummy_on_status_line; + } + if (parser->callbacks.on_header_field == NULL) { + parser->callbacks.on_header_field = dummy_on_header_field; + } + if (parser->callbacks.on_body == NULL) { + parser->callbacks.on_body = dummy_on_body; + } + if (parser->callbacks.on_end == NULL) { + parser->callbacks.on_end = dummy_on_end; + } + if (parser->callbacks.on_error == NULL) { + parser->callbacks.on_error = dummy_on_error; + } + + ehttp_parser_push(parser, HTTP_PARSER_BEGIN); + + return parser; +} + +static void ehttp_parser_kvs_destroy(ehttp_parser_t *parser) { + if (!parser->kvs) return; + + for (size_t i=0; ikvs_count; ++i) { + ehttp_parser_kv_t *p = &parser->kvs[i]; + free(p->key); p->key = NULL; + free(p->val); p->val = NULL; + } + free(parser->kvs); + parser->kvs = NULL; + parser->kvs_count = 0; + + free(parser->auth_basic); + parser->auth_basic = NULL; +} + +void ehttp_parser_destroy(ehttp_parser_t *parser) { + 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->reason_phrase); parser->reason_phrase = 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; + + parser->stacks_count = 0; + + ehttp_parser_kvs_destroy(parser); + + ehttp_util_string_cleanup(&parser->str); + if (parser->gzip) { + ehttp_gzip_destroy(parser->gzip); + parser->gzip = NULL; + } + + free(parser); +} + +#define is_token(c) (strchr("!#$%&'*+-.^_`|~", c) || isdigit(c) || isalpha(c)) + +char *ehttp_parser_urldecode(const char *enc) { + int ok = 1; + ehttp_util_string_t str = {0}; + while (*enc) { + char *p = strchr(enc, '%'); + if (!p) break; + int hex, cnt; + int n = sscanf(p+1, "%2x%n", &hex, &cnt); + if (n!=1 && cnt !=2) { ok = 0; break; } + if (ehttp_util_string_append(&str, enc, p-enc)) { ok = 0; break; } + char c = (char)hex; + if (ehttp_util_string_append(&str, &c, 1)) { ok = 0; break; } + enc = p+3; + } + char *dec = NULL; + if (ok && *enc) { + if (ehttp_util_string_append(&str, enc, strlen(enc))) { ok = 0; } + } + if (ok) { + dec = str.str; + str.str = NULL; + } + ehttp_util_string_cleanup(&str); + return dec; +} + +static void on_data(ehttp_gzip_t *gzip, void *arg, const char *buf, size_t len) { + ehttp_parser_t *parser = (ehttp_parser_t*)arg; + parser->callbacks.on_body(parser->arg, buf, len); +} + +static int ehttp_parser_check_field(ehttp_parser_t *parser, const char *key, const char *val) { + int ok = 0; + do { + if (0==strcasecmp(key, "Content-Length")) { + size_t len = 0; + int bytes = 0; + int n = sscanf(val, "%ld%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) { + E("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; + int bytes = 0; + int n = sscanf(val, "%ms %ms%n", &t, &s, &bytes); + if (n==2 && t && s && bytes==strlen(val) && strcmp(t, "Basic")) { + free(parser->auth_basic); + parser->auth_basic = s; s = NULL; + } else { + ok = -1; + } + free(t); free(s); + break; + } + } while (0); + return ok; +} + +static int ehttp_parser_kvs_append_kv(ehttp_parser_t *parser, const char *key, const char *val) { + ehttp_parser_kv_t *kvs = (ehttp_parser_kv_t*)reallocarray(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 int on_begin(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + if (c=='G' || c=='P' || c=='H' || c=='D' || c=='C' || c=='O' || c=='T') { + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + ehttp_parser_pop(parser); + ehttp_parser_push(parser, HTTP_PARSER_REQUEST_OR_RESPONSE); + break; + } + E("parser state: %d, unexpected char: [%c]%02x", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 400); + } while (0); + return ok; +} + +static int on_request_or_response(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + if (parser->str.len==1) { + if (c=='T' && parser->str.str[0]=='H') { + ehttp_parser_pop(parser); + ehttp_parser_push(parser, HTTP_PARSER_END); + ehttp_parser_push(parser, HTTP_PARSER_HEADER); + ehttp_parser_push(parser, HTTP_PARSER_CRLF); + ehttp_parser_push(parser, HTTP_PARSER_REASON_PHRASE); + ehttp_parser_push(parser, HTTP_PARSER_SP); + ehttp_parser_push(parser, HTTP_PARSER_STATUS_CODE); + ehttp_parser_push(parser, HTTP_PARSER_SP); + ehttp_parser_push(parser, HTTP_PARSER_HTTP_VERSION); + *again = 1; + break; + } + ehttp_parser_pop(parser); + ehttp_parser_push(parser, HTTP_PARSER_END); + ehttp_parser_push(parser, HTTP_PARSER_HEADER); + ehttp_parser_push(parser, HTTP_PARSER_CRLF); + ehttp_parser_push(parser, HTTP_PARSER_HTTP_VERSION); + ehttp_parser_push(parser, HTTP_PARSER_SP); + ehttp_parser_push(parser, HTTP_PARSER_TARGET); + ehttp_parser_push(parser, HTTP_PARSER_SP); + ehttp_parser_push(parser, HTTP_PARSER_METHOD); + *again = 1; + break; + } + E("parser state: %d, unexpected char: [%c]%02x", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 400); + } while (0); + return ok; +} + +static int on_method(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + if (isalnum(c) || strchr("!#$%&'*+-.^_`|~", c)) { + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + break; + } + parser->method = strdup(parser->str.str); + if (!parser->method) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + ehttp_util_string_clear(&parser->str); + ehttp_parser_pop(parser); + *again = 1; + } while (0); + return ok; +} + +static int on_target(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + if (!isspace(c) && c!='\r' && c!='\n') { + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + break; + } + parser->target_raw = strdup(parser->str.str); + parser->target = ehttp_parser_urldecode(parser->str.str); + if (!parser->target_raw || !parser->target) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + ehttp_util_string_clear(&parser->str); + ehttp_parser_pop(parser); + *again = 1; + } while (0); + return ok; +} + +static int on_version(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + const char *prefix = "HTTP/1."; + int len = strlen(prefix); + if (parser->str.len < len) { + if (prefix[parser->str.len]!=c) { + E("parser state: %d, unexpected char: [%c]%02x", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 400); + break; + } + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + break; + } + + if (c!='0' && c!='1') { + E("parser state: %d, unexpected char: [%c]%02x", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 400); + break; + } + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + if (c=='0') parser->http_10 = 1; + if (c=='1') parser->http_11 = 1; + + parser->version = strdup(parser->str.str); + if (!parser->version) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + + if (parser->method) { + parser->callbacks.on_request_line(parser->arg, parser->method, parser->target, parser->version, parser->target_raw); + } + + ehttp_util_string_clear(&parser->str); + ehttp_parser_pop(parser); + } while (0); + return ok; +} + +static int on_sp(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + if (c==' ') { + ehttp_parser_pop(parser); + break; + } + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + } while (0); + return ok; +} + +static int on_status_code(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + if (isdigit(c)) { + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + if (parser->str.len < 3) break; + + sscanf(parser->str.str, "%d", &parser->status_code); + ehttp_util_string_clear(&parser->str); + ehttp_parser_pop(parser); + break; + } + E("parser state: %d, unexpected char: [%c]%02x", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 400); + } while (0); + return ok; +} + +static int on_reason_phrase(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + if (c=='\r') { + parser->reason_phrase = strdup(parser->str.str); + if (!parser->reason_phrase) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + parser->callbacks.on_status_line(parser->arg, parser->version, parser->status_code, parser->reason_phrase); + ehttp_util_string_clear(&parser->str); + ehttp_parser_pop(parser); + *again = 1; + break; + } + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + } while (0); + return ok; +} + +static int post_process(ehttp_parser_t *parser) { + if (parser->gzip) { + if (ehttp_gzip_finish(parser->gzip)) { + E("gzip failed"); + parser->callbacks.on_error(parser->arg, 507); + return -1; + } + } + parser->callbacks.on_end(parser->arg); + return 0; +} + +static int on_crlf(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + const char *s = "\r\n"; + int len = strlen(s); + if (s[parser->str.len]!=c) { + E("parser state: %d, unexpected char: [%c]%02x", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 400); + break; + } + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + if (parser->str.len == len) { + ehttp_util_string_clear(&parser->str); + ehttp_parser_pop(parser); + if (ehttp_parser_top(parser) == HTTP_PARSER_END) { + ok = post_process(parser); + } + } + break; + } while (0); + return ok; +} + +static int on_header(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + if (c=='\r') { + ehttp_parser_pop(parser); + if (parser->transfer_chunked) { + ehttp_parser_push(parser, HTTP_PARSER_CHUNK_SIZE); + ehttp_parser_push(parser, HTTP_PARSER_CRLF); + } else { + if (parser->content_length > 0) { + ehttp_parser_push(parser, HTTP_PARSER_CHUNK); + } + ehttp_parser_push(parser, HTTP_PARSER_CRLF); + } + *again = 1; + break; + } + if (c!=' ' && c!='\t' && c!=':' ) { + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + ehttp_parser_push(parser, HTTP_PARSER_CRLF); + ehttp_parser_push(parser, HTTP_PARSER_HEADER_VAL); + ehttp_parser_push(parser, HTTP_PARSER_SP); + ehttp_parser_push(parser, HTTP_PARSER_HEADER_KEY); + break; + } + E("parser state: %d, unexpected char: [%c]%02x", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 400); + } while (0); + return ok; +} + +static int on_header_key(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + if (isalnum(c) || strchr("!#$%&'*+-.^_`|~", c)) { + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + break; + } + if (c==':') { + parser->key = strdup(parser->str.str); + if (!parser->key) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + ehttp_util_string_clear(&parser->str); + ehttp_parser_pop(parser); + break; + } + E("parser state: %d, unexpected char: [%c]%02x", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 400); + } while (0); + return ok; +} + +static int on_header_val(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + if (c != '\r' && c != '\n' && (!isspace(c) || parser->str.len>0)) { + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + break; + } + const char *val = parser->str.str; + ok = ehttp_parser_check_field(parser, parser->key, val); + if (ehttp_parser_kvs_append_kv(parser, parser->key, val)) { + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + } else { + parser->callbacks.on_header_field(parser->arg, parser->key, val); + } + free(parser->key); parser->key = NULL; + val = NULL; + if (ok==-1) break; + ehttp_util_string_clear(&parser->str); + ehttp_parser_pop(parser); + *again = 1; + } while (0); + return ok; +} + +static int on_chunk_size(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + int bytes; + size_t len; + int n; + do { + if (isxdigit(c)) { + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + break; + } + if (c=='\r') { + n = sscanf(parser->str.str, "%lx%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_length) { + ehttp_util_string_clear(&parser->str); + ehttp_parser_pop(parser); + ehttp_parser_push(parser, HTTP_PARSER_CRLF); + ehttp_parser_push(parser, HTTP_PARSER_CRLF); + *again = 1; + break; + } + } else { + if (parser->content_length_specified == 0 || parser->received_size + len <= parser->content_length) { + parser->chunk_size = len; + ehttp_util_string_clear(&parser->str); + ehttp_parser_pop(parser); + ehttp_parser_push(parser, HTTP_PARSER_CHUNK_SIZE); + ehttp_parser_push(parser, HTTP_PARSER_CRLF); + ehttp_parser_push(parser, HTTP_PARSER_CHUNK); + ehttp_parser_push(parser, HTTP_PARSER_CRLF); + *again = 1; + break; + } + } + } + } + E("parser state: %d, unexpected char: [%c]%02x", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 400); + } while (0); + return ok; +} + +static int on_chunk(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + if (ehttp_util_string_append(&parser->str, &c, 1)) { + E("parser state: %d, char: [%c]%02x, oom", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + ++parser->received_size; + ++parser->received_chunk_size; + if (parser->received_chunk_size < parser->chunk_size) break; + + if (parser->gzip) { + if (ehttp_gzip_write(parser->gzip, parser->str.str, parser->str.len)) { + E("gzip failed"); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + break; + } + } else { + parser->callbacks.on_body(parser->arg, parser->str.str, parser->str.len); + } + parser->received_chunk_size = 0; + ehttp_util_string_clear(&parser->str); + ehttp_parser_pop(parser); + if (ehttp_parser_top(parser) == HTTP_PARSER_END) { + ok = post_process(parser); + } + } while (0); + return ok; +} + +static int on_end(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int ok = 0; + do { + E("parser state: %d, unexpected char: [%c]%02x", state, c, c); + ok = -1; + parser->callbacks.on_error(parser->arg, 507); + } while (0); + return ok; +} + +static int parse_char(ehttp_parser_t *parser, const char c, int *again) { + int ok = 0; + HTTP_PARSER_STATE state = ehttp_parser_top(parser); + do { + if (state == HTTP_PARSER_BEGIN) { + ok = on_begin(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_REQUEST_OR_RESPONSE) { + ok = on_request_or_response(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_METHOD) { + ok = on_method(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_TARGET) { + ok = on_target(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_HTTP_VERSION) { + ok = on_version(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_SP) { + ok = on_sp(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_STATUS_CODE) { + ok = on_status_code(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_REASON_PHRASE) { + ok = on_reason_phrase(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_CRLF) { + ok = on_crlf(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_HEADER) { + ok = on_header(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_HEADER_KEY) { + ok = on_header_key(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_HEADER_VAL) { + ok = on_header_val(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_CHUNK_SIZE) { + ok = on_chunk_size(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_CHUNK) { + ok = on_chunk(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_END) { + ok = on_end(parser, state, c, again); + break; + } + if (state == HTTP_PARSER_ERROR) { + ok = -2; + break; + } + E("unknown parser state: %d", state); + ok = -1; + parser->callbacks.on_error(parser->arg, 500); + } while (0); + if (ok==-1) { + ehttp_parser_push(parser, HTTP_PARSER_ERROR); + } + if (ok==-2) ok = -1; + return ok; +} + +int ehttp_parser_parse_string(ehttp_parser_t *parser, const char *str) { + return ehttp_parser_parse(parser, str, str?strlen(str):0); +} + +int ehttp_parser_parse_char(ehttp_parser_t *parser, const char c) { + return ehttp_parser_parse(parser, &c, 1); +} + +int ehttp_parser_parse(ehttp_parser_t *parser, const char *buf, size_t len) { + const char *p = buf; + int ret = 0; + size_t i = 0; + while (i < len) { + int again = 0; + ret = parse_char(parser, *p, &again); + if (ret) break; + if (again) continue; + ++p; + ++i; + } + return ret; +} + diff --git a/src/plugins/http/src/ehttp_util_string.c b/src/plugins/http/src/ehttp_util_string.c new file mode 100644 index 0000000000..94ebaaafa6 --- /dev/null +++ b/src/plugins/http/src/ehttp_util_string.c @@ -0,0 +1,30 @@ +#include "ehttp_util_string.h" + +#include +#include + +void ehttp_util_string_cleanup(ehttp_util_string_t *str) { + free(str->str); + str->str = NULL; + str->len = 0; +} + +int ehttp_util_string_append(ehttp_util_string_t *str, const char *s, size_t len) { + // int n = str->str?strlen(str->str):0; + int 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 ehttp_util_string_clear(ehttp_util_string_t *str) { + if (str->str) { + str->str[0] = '\0'; + str->len = 0; + } +} + diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c index f46d3fb427..4e235e24e8 100644 --- a/src/plugins/http/src/httpContext.c +++ b/src/plugins/http/src/httpContext.c @@ -28,6 +28,22 @@ #include "httpSql.h" #include "httpSession.h" +#include "elog.h" + +// dirty tweak +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 on_request_line(void *arg, const char *method, const char *target, const char *version, const char *target_raw); +static void on_status_line(void *arg, const char *version, int status_code, const char *reason_phrase); +static void on_header_field(void *arg, const char *key, const char *val); +static void on_body(void *arg, const char *chunk, size_t len); +static void on_end(void *arg); +static void on_error(void *arg, int status_code); + static void httpRemoveContextFromEpoll(HttpContext *pContext) { HttpThread *pThread = pContext->pThread; if (pContext->fd >= 0) { @@ -149,6 +165,11 @@ void httpReleaseContext(HttpContext *pContext) { httpDebug("context:%p, won't be destroyed for cache is already released", pContext); // httpDestroyContext((void **)(&ppContext)); } + + if (pContext->parser.parser) { + ehttp_parser_destroy(pContext->parser.parser); + pContext->parser.parser = NULL; + } } bool httpInitContext(HttpContext *pContext) { @@ -168,6 +189,20 @@ bool httpInitContext(HttpContext *pContext) { memset(pParser, 0, sizeof(HttpParser)); pParser->pCur = pParser->pLast = pParser->buffer; + ehttp_parser_callbacks_t callbacks = { + on_request_line, + on_status_line, + on_header_field, + on_body, + on_end, + on_error + }; + ehttp_parser_conf_t conf = { + .flush_block_size = 0 + }; + pParser->parser = ehttp_parser_create(callbacks, conf, pContext); + pParser->inited = 1; + httpDebug("context:%p, fd:%d, ip:%s, thread:%s, accessTimes:%d, parsed:%d", pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, pContext->parsed); return true; @@ -230,3 +265,129 @@ void httpCloseContextByServer(HttpContext *pContext) { httpRemoveContextFromEpoll(pContext); httpReleaseContext(pContext); } + + + + + +static void on_request_line(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, ip:%s, thread:%s, accessTimes:%d, on_request_line(%s,%s,%s,%s), exceeding buffer size", + pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, method, target, version, target_raw); + break; + } + pParser->bufsize += n; + + if (!httpGetHttpMethod(pContext)) { + httpDebug("context:%p, fd:%d, ip:%s, thread:%s, accessTimes:%d, on_request_line(%s,%s,%s,%s), parse http method failed", + pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, method, target, version, target_raw); + break; + } + if (!httpParseURL(pContext)) { + httpDebug("context:%p, fd:%d, ip:%s, thread:%s, accessTimes:%d, on_request_line(%s,%s,%s,%s), parse http url failed", + pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, method, target, version, target_raw); + break; + } + if (!httpParseHttpVersion(pContext)) { + httpDebug("context:%p, fd:%d, ip:%s, thread:%s, accessTimes:%d, on_request_line(%s,%s,%s,%s), parse http version failed", + pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, method, target, version, target_raw); + break; + } + if (!httpGetDecodeMethod(pContext)) { + httpDebug("context:%p, fd:%d, ip:%s, thread:%s, accessTimes:%d, on_request_line(%s,%s,%s,%s), get decode method failed", + pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, method, target, version, target_raw); + break; + } + + last += n; + pParser->pLast = last; + return; + } while (0); + + pParser->failed |= EHTTP_CONTEXT_PROCESS_FAILED; +} + +static void on_status_line(void *arg, const char *version, int status_code, const char *reason_phrase) { + HttpContext *pContext = (HttpContext*)arg; + HttpParser *pParser = &pContext->parser; + + pParser->failed |= EHTTP_CONTEXT_PROCESS_FAILED; +} + +static void on_header_field(void *arg, const char *key, const char *val) { + HttpContext *pContext = (HttpContext*)arg; + HttpParser *pParser = &pContext->parser; + + if (pParser->failed) return; + + 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, ip:%s, thread:%s, accessTimes:%d, on_header_field(%s,%s), exceeding buffer size", + pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, key, val); + break; + } + pParser->bufsize += n; + pParser->pCur = pParser->pLast; + + if (!httpParseHead(pContext)) { + httpDebug("context:%p, fd:%d, ip:%s, thread:%s, accessTimes:%d, on_header_field(%s,%s), parse head failed", + pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, key, val); + break; + } + + last += n; + pParser->pLast = last; + return; + } while (0); + + pParser->failed |= EHTTP_CONTEXT_PROCESS_FAILED; +} + +static void on_body(void *arg, const char *chunk, size_t len) { + HttpContext *pContext = (HttpContext*)arg; + HttpParser *pParser = &pContext->parser; + + if (pParser->failed) return; + + if (!pContext->parsed) { + pContext->parsed = true; + } + + A("not implemented yet"); +} + +static void on_end(void *arg) { + HttpContext *pContext = (HttpContext*)arg; + HttpParser *pParser = &pContext->parser; + + if (pParser->failed) return; + + if (!pContext->parsed) { + pContext->parsed = true; + } +} + +static void on_error(void *arg, int status_code) { + HttpContext *pContext = (HttpContext*)arg; + HttpParser *pParser = &pContext->parser; + + D("=="); + pParser->failed |= EHTTP_CONTEXT_PARSER_FAILED; +} + diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index ec3d2c0d44..d0b01c628c 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -26,10 +26,14 @@ #include "httpResp.h" #include "httpUtil.h" +#include "elog.h" + #ifndef EPOLLWAKEUP #define EPOLLWAKEUP (1u << 29) #endif +static bool ehttpReadData(HttpContext *pContext); + static void httpStopThread(HttpThread* pThread) { pThread->stop = true; @@ -134,6 +138,8 @@ static bool httpDecompressData(HttpContext *pContext) { } static bool httpReadData(HttpContext *pContext) { + if (1) return ehttpReadData(pContext); + if (!pContext->parsed) { httpInitContext(pContext); } @@ -405,3 +411,60 @@ bool httpInitConnect() { pServer->serverPort, pServer->numOfThreads); return true; } + + + + +static bool ehttpReadData(HttpContext *pContext) { + HttpParser *pParser = &pContext->parser; + EQ_ASSERT(!pContext->parsed); + if (!pParser->parser) { + if (!pParser->inited) { + httpInitContext(pContext); + } + if (!pParser->parser) { + return false; + } + } + + pContext->accessTimes++; + pContext->lastAccessTime = taosGetTimestampSec(); + + char buf[HTTP_STEP_SIZE+1] = {0}; + int nread = (int)taosReadSocket(pContext->fd, buf, sizeof(buf)); + if (nread > 0) { + buf[nread] = '\0'; + if (strstr(buf, "GET ")==buf && !strchr(buf, '\r') && !strchr(buf, '\n')) { + D("==half of request line received:\n%s\n==", buf); + } + if (ehttp_parser_parse(pParser->parser, buf, nread)) { + D("==parsing failed=="); + httpCloseContextByServer(pContext); + return false; + } + if (pContext->parser.failed) { + D("==parsing failed: [0x%x]==", pContext->parser.failed); + httpNotifyContextClose(pContext); + return false; + } + return pContext->parsed; + } else if (nread < 0) { + if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { + httpDebug("context:%p, fd:%d, ip:%s, read from socket error:%d, wait another event", + pContext, pContext->fd, pContext->ipstr, errno); + return false; // later again + } else { + httpError("context:%p, fd:%d, ip:%s, read from socket error:%d, close connect", + pContext, pContext->fd, pContext->ipstr, errno); + D("==releasing because of reading failed=="); + httpReleaseContext(pContext); + return false; + } + } else { + // eof + D("==releasing because of remote close/reset=="); + httpReleaseContext(pContext); + return false; + } +} + From d9c04b18e9b96a888e363210b8b903e19735ec7d Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 1 Aug 2020 10:59:12 +0800 Subject: [PATCH 02/30] elog to util; parser.data.pos --- src/plugins/http/src/httpContext.c | 13 ++++++++++--- src/plugins/http/src/httpServer.c | 25 ++++++++++++++++++++++++- src/{common => util}/inc/elog.h | 0 src/{common => util}/src/elog.c | 0 4 files changed, 34 insertions(+), 4 deletions(-) rename src/{common => util}/inc/elog.h (100%) rename src/{common => util}/src/elog.c (100%) diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c index 4e235e24e8..1e36fdef75 100644 --- a/src/plugins/http/src/httpContext.c +++ b/src/plugins/http/src/httpContext.c @@ -55,6 +55,7 @@ static void httpRemoveContextFromEpoll(HttpContext *pContext) { static void httpDestroyContext(void *data) { HttpContext *pContext = *(HttpContext **)data; + D("==context[%p] destroyed==", pContext); if (pContext->fd > 0) tclose(pContext->fd); HttpThread *pThread = pContext->pThread; @@ -80,6 +81,7 @@ bool httpInitContexts() { httpError("failed to init context cache"); return false; } + D("==cache [%p] created==", tsHttpServer.contextCache); return true; } @@ -120,6 +122,8 @@ HttpContext *httpCreateContext(int32_t fd) { HttpContext *pContext = calloc(1, sizeof(HttpContext)); if (pContext == NULL) return NULL; + D("==context[%p] created==", pContext); + pContext->fd = fd; pContext->httpVersion = HTTP_VERSION_10; pContext->lastAccessTime = taosGetTimestampSec(); @@ -209,6 +213,7 @@ bool httpInitContext(HttpContext *pContext) { } void httpCloseContextByApp(HttpContext *pContext) { + D("=="); pContext->parsed = false; bool keepAlive = true; @@ -220,6 +225,7 @@ void httpCloseContextByApp(HttpContext *pContext) { } if (keepAlive) { + D("==keepAlive=="); if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_HANDLING, HTTP_CONTEXT_STATE_READY)) { httpDebug("context:%p, fd:%d, ip:%s, last state:handling, keepAlive:true, reuse context", pContext, pContext->fd, pContext->ipstr); @@ -240,6 +246,7 @@ void httpCloseContextByApp(HttpContext *pContext) { pContext->ipstr, httpContextStateStr(pContext->state), pContext->state); } } else { + D("==not keepAlive=="); httpRemoveContextFromEpoll(pContext); httpDebug("context:%p, fd:%d, ip:%s, last state:%s:%d, keepAlive:false, close context", pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state), pContext->state); @@ -365,9 +372,7 @@ static void on_body(void *arg, const char *chunk, size_t len) { if (pParser->failed) return; - if (!pContext->parsed) { - pContext->parsed = true; - } + if (pParser->data.pos == 0) pParser->data.pos = pParser->pLast; A("not implemented yet"); } @@ -378,6 +383,8 @@ static void on_end(void *arg) { if (pParser->failed) return; + if (pParser->data.pos == 0) pParser->data.pos = pParser->pLast; + if (!pContext->parsed) { pContext->parsed = true; } diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index d0b01c628c..d21fa89d5c 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -138,7 +138,7 @@ static bool httpDecompressData(HttpContext *pContext) { } static bool httpReadData(HttpContext *pContext) { - if (1) return ehttpReadData(pContext); + if (0) return ehttpReadData(pContext); if (!pContext->parsed) { httpInitContext(pContext); @@ -447,6 +447,29 @@ static bool ehttpReadData(HttpContext *pContext) { httpNotifyContextClose(pContext); return false; } + if (pContext->parsed) { + int ret = httpCheckReadCompleted(pContext); + if (ret == HTTP_CHECK_BODY_CONTINUE) { + //httpDebug("context:%p, fd:%d, ip:%s, not finished yet, wait another event", pContext, pContext->fd, pContext->ipstr); + httpReleaseContext(pContext); + return false; + } else if (ret == HTTP_CHECK_BODY_SUCCESS){ + httpDebug("context:%p, fd:%d, ip:%s, thread:%s, read size:%d, dataLen:%d", + pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->parser.bufsize, pContext->parser.data.len); + if (httpDecompressData(pContext)) { + return true; + } else { + httpNotifyContextClose(pContext); + httpReleaseContext(pContext); + return false; + } + } else { + httpError("context:%p, fd:%d, ip:%s, failed to read http body, close connect", pContext, pContext->fd, pContext->ipstr); + httpNotifyContextClose(pContext); + httpReleaseContext(pContext); + return false; + } + } return pContext->parsed; } else if (nread < 0) { if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { diff --git a/src/common/inc/elog.h b/src/util/inc/elog.h similarity index 100% rename from src/common/inc/elog.h rename to src/util/inc/elog.h diff --git a/src/common/src/elog.c b/src/util/src/elog.c similarity index 100% rename from src/common/src/elog.c rename to src/util/src/elog.c From 93cb6386df27403bc04d957586b36140a477bf31 Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 1 Aug 2020 13:28:39 +0800 Subject: [PATCH 03/30] body process --- src/plugins/http/src/httpContext.c | 17 ++++++++++++++--- src/plugins/http/src/httpServer.c | 6 ++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c index 1e36fdef75..5012fd15f5 100644 --- a/src/plugins/http/src/httpContext.c +++ b/src/plugins/http/src/httpContext.c @@ -337,6 +337,7 @@ static void on_header_field(void *arg, const char *key, const char *val) { if (pParser->failed) return; + D("==key:[%s], val:[%s]==", key, val); int avail = sizeof(pParser->buffer) - (pParser->pLast - pParser->buffer); int n = snprintf(pParser->pLast, avail, "%s: %s\r\n", key, val); @@ -350,7 +351,7 @@ static void on_header_field(void *arg, const char *key, const char *val) { break; } pParser->bufsize += n; - pParser->pCur = pParser->pLast; + pParser->pCur = pParser->pLast + n; if (!httpParseHead(pContext)) { httpDebug("context:%p, fd:%d, ip:%s, thread:%s, accessTimes:%d, on_header_field(%s,%s), parse head failed", @@ -372,9 +373,19 @@ static void on_body(void *arg, const char *chunk, size_t len) { if (pParser->failed) return; - if (pParser->data.pos == 0) pParser->data.pos = pParser->pLast; + if (pParser->data.pos == 0) { + pParser->data.pos = pParser->pLast; + pParser->data.len = 0; + } - A("not implemented yet"); + int avail = sizeof(pParser->buffer) - (pParser->pLast - pParser->buffer); + if (len+1>=avail) { + pParser->failed |= EHTTP_CONTEXT_PROCESS_FAILED; + return; + } + memcpy(pParser->pLast, chunk, len); + pParser->pLast += len; + pParser->data.len += len; } static void on_end(void *arg) { diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index d21fa89d5c..4f2ea63dc2 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -138,7 +138,7 @@ static bool httpDecompressData(HttpContext *pContext) { } static bool httpReadData(HttpContext *pContext) { - if (0) return ehttpReadData(pContext); + if (1) return ehttpReadData(pContext); if (!pContext->parsed) { httpInitContext(pContext); @@ -448,7 +448,9 @@ static bool ehttpReadData(HttpContext *pContext) { return false; } if (pContext->parsed) { - int ret = httpCheckReadCompleted(pContext); + // int ret = httpCheckReadCompleted(pContext); + // already done in ehttp_parser + int ret = HTTP_CHECK_BODY_SUCCESS; if (ret == HTTP_CHECK_BODY_CONTINUE) { //httpDebug("context:%p, fd:%d, ip:%s, not finished yet, wait another event", pContext, pContext->fd, pContext->ipstr); httpReleaseContext(pContext); From 7c434d6108e26a52995069978e1911efe40287b4 Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 1 Aug 2020 15:19:43 +0800 Subject: [PATCH 04/30] add env FALLBACK, for the sake of easy debug in different mode --- src/plugins/http/inc/httpInt.h | 2 ++ src/plugins/http/src/httpContext.c | 40 ++++++++++++++++-------------- src/plugins/http/src/httpServer.c | 6 +++-- src/plugins/http/src/httpSystem.c | 6 +++++ 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/plugins/http/inc/httpInt.h b/src/plugins/http/inc/httpInt.h index 044b5cc4cc..bde799d6d6 100644 --- a/src/plugins/http/inc/httpInt.h +++ b/src/plugins/http/inc/httpInt.h @@ -242,6 +242,8 @@ typedef struct HttpServer { pthread_mutex_t serverMutex; HttpDecodeMethod *methodScanner[HTTP_METHOD_SCANNER_SIZE]; bool (*processData)(HttpContext *pContext); + + int fallback:2; } HttpServer; extern const char *httpKeepAliveStr[]; diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c index 5012fd15f5..4440da6d45 100644 --- a/src/plugins/http/src/httpContext.c +++ b/src/plugins/http/src/httpContext.c @@ -72,6 +72,13 @@ static void httpDestroyContext(void *data) { httpFreeJsonBuf(pContext); httpFreeMultiCmds(pContext); + if (!tsHttpServer.fallback) { + if (pContext->parser.parser) { + ehttp_parser_destroy(pContext->parser.parser); + pContext->parser.parser = NULL; + } + } + tfree(pContext); } @@ -169,11 +176,6 @@ void httpReleaseContext(HttpContext *pContext) { httpDebug("context:%p, won't be destroyed for cache is already released", pContext); // httpDestroyContext((void **)(&ppContext)); } - - if (pContext->parser.parser) { - ehttp_parser_destroy(pContext->parser.parser); - pContext->parser.parser = NULL; - } } bool httpInitContext(HttpContext *pContext) { @@ -193,19 +195,21 @@ bool httpInitContext(HttpContext *pContext) { memset(pParser, 0, sizeof(HttpParser)); pParser->pCur = pParser->pLast = pParser->buffer; - ehttp_parser_callbacks_t callbacks = { - on_request_line, - on_status_line, - on_header_field, - on_body, - on_end, - on_error - }; - ehttp_parser_conf_t conf = { - .flush_block_size = 0 - }; - pParser->parser = ehttp_parser_create(callbacks, conf, pContext); - pParser->inited = 1; + if (!tsHttpServer.fallback) { + ehttp_parser_callbacks_t callbacks = { + on_request_line, + on_status_line, + on_header_field, + on_body, + on_end, + on_error + }; + ehttp_parser_conf_t conf = { + .flush_block_size = 0 + }; + pParser->parser = ehttp_parser_create(callbacks, conf, pContext); + pParser->inited = 1; + } httpDebug("context:%p, fd:%d, ip:%s, thread:%s, accessTimes:%d, parsed:%d", pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, pContext->parsed); diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index 4f2ea63dc2..819f7a5f4a 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -138,7 +138,7 @@ static bool httpDecompressData(HttpContext *pContext) { } static bool httpReadData(HttpContext *pContext) { - if (1) return ehttpReadData(pContext); + if (!tsHttpServer.fallback) return ehttpReadData(pContext); if (!pContext->parsed) { httpInitContext(pContext); @@ -437,11 +437,13 @@ static bool ehttpReadData(HttpContext *pContext) { if (strstr(buf, "GET ")==buf && !strchr(buf, '\r') && !strchr(buf, '\n')) { D("==half of request line received:\n%s\n==", buf); } + if (ehttp_parser_parse(pParser->parser, buf, nread)) { D("==parsing failed=="); httpCloseContextByServer(pContext); return false; } + if (pContext->parser.failed) { D("==parsing failed: [0x%x]==", pContext->parser.failed); httpNotifyContextClose(pContext); @@ -450,7 +452,7 @@ static bool ehttpReadData(HttpContext *pContext) { if (pContext->parsed) { // int ret = httpCheckReadCompleted(pContext); // already done in ehttp_parser - int ret = HTTP_CHECK_BODY_SUCCESS; + int ret = HTTP_CHECK_BODY_SUCCESS; if (ret == HTTP_CHECK_BODY_CONTINUE) { //httpDebug("context:%p, fd:%d, ip:%s, not finished yet, wait another event", pContext, pContext->fd, pContext->ipstr); httpReleaseContext(pContext); diff --git a/src/plugins/http/src/httpSystem.c b/src/plugins/http/src/httpSystem.c index 3a0998f2e8..43466ee57e 100644 --- a/src/plugins/http/src/httpSystem.c +++ b/src/plugins/http/src/httpSystem.c @@ -39,6 +39,12 @@ HttpServer tsHttpServer; void taosInitNote(int numOfNoteLines, int maxNotes, char* lable); int httpInitSystem() { + tsHttpServer.fallback = 0; + const char *v = getenv("FALLBACK"); + if (v) { + tsHttpServer.fallback = 1; + } + strcpy(tsHttpServer.label, "rest"); tsHttpServer.serverIp = 0; tsHttpServer.serverPort = tsHttpPort; From e55a55a6f82e523e65a81785db6d3c36fb875561 Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 1 Aug 2020 21:57:07 +0800 Subject: [PATCH 05/30] add ehttpInc/DecContextRef --- src/plugins/http/inc/httpContext.h | 3 ++ src/plugins/http/inc/httpInt.h | 2 + src/plugins/http/src/httpContext.c | 76 ++++++++++++++++++++++++++---- src/plugins/http/src/httpServer.c | 29 ++++++++---- 4 files changed, 92 insertions(+), 18 deletions(-) diff --git a/src/plugins/http/inc/httpContext.h b/src/plugins/http/inc/httpContext.h index a2d50d6b7f..594900d0cf 100644 --- a/src/plugins/http/inc/httpContext.h +++ b/src/plugins/http/inc/httpContext.h @@ -31,4 +31,7 @@ void httpCloseContextByApp(HttpContext *pContext); void httpNotifyContextClose(HttpContext *pContext); bool httpAlterContextState(HttpContext *pContext, HttpContextState srcState, HttpContextState destState); +void ehttpIncContextRef(HttpContext *pContext); +void ehttpDecContextRef(HttpContext **ppContext); + #endif diff --git a/src/plugins/http/inc/httpInt.h b/src/plugins/http/inc/httpInt.h index bde799d6d6..40f980f101 100644 --- a/src/plugins/http/inc/httpInt.h +++ b/src/plugins/http/inc/httpInt.h @@ -212,6 +212,8 @@ typedef struct HttpContext { void * timer; HttpEncodeMethod * encodeMethod; struct HttpThread *pThread; + + int closed:2; } HttpContext; typedef struct HttpThread { diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c index 4440da6d45..b229673df2 100644 --- a/src/plugins/http/src/httpContext.c +++ b/src/plugins/http/src/httpContext.c @@ -28,6 +28,7 @@ #include "httpSql.h" #include "httpSession.h" +#include "httpContext.h" #include "elog.h" // dirty tweak @@ -44,12 +45,20 @@ static void on_body(void *arg, const char *chunk, size_t len); static void on_end(void *arg); static void on_error(void *arg, int status_code); +static void httpDestroyContext(void *data); +static void httpMightDestroyContext(void *data); +static void ehttpReleaseContext(HttpContext *pContext); + static void httpRemoveContextFromEpoll(HttpContext *pContext) { HttpThread *pThread = pContext->pThread; if (pContext->fd >= 0) { epoll_ctl(pThread->pollFd, EPOLL_CTL_DEL, pContext->fd, NULL); - taosCloseSocket(pContext->fd); + int32_t fd = pContext->fd; pContext->fd = -1; + taosCloseSocket(fd); + if (!tsHttpServer.fallback) { + ehttpDecContextRef(&pContext); + } } } @@ -83,12 +92,11 @@ static void httpDestroyContext(void *data) { } bool httpInitContexts() { - tsHttpServer.contextCache = taosCacheInit(TSDB_DATA_TYPE_BIGINT, 2, true, httpDestroyContext, "restc"); + tsHttpServer.contextCache = taosCacheInit(TSDB_DATA_TYPE_BIGINT, 2, true, httpMightDestroyContext, "restc"); if (tsHttpServer.contextCache == NULL) { httpError("failed to init context cache"); return false; } - D("==cache [%p] created==", tsHttpServer.contextCache); return true; } @@ -136,10 +144,12 @@ HttpContext *httpCreateContext(int32_t fd) { pContext->lastAccessTime = taosGetTimestampSec(); pContext->state = HTTP_CONTEXT_STATE_READY; + ehttpIncContextRef(pContext); HttpContext **ppContext = taosCachePut(tsHttpServer.contextCache, &pContext, sizeof(int64_t), &pContext, sizeof(int64_t), 3); pContext->ppContext = ppContext; httpDebug("context:%p, fd:%d, is created, data:%p", pContext, fd, ppContext); + ehttpIncContextRef(pContext); // set the ref to 0 taosCacheRelease(tsHttpServer.contextCache, (void**)&ppContext, false); @@ -148,10 +158,13 @@ HttpContext *httpCreateContext(int32_t fd) { HttpContext *httpGetContext(void *ptr) { HttpContext **ppContext = taosCacheAcquireByKey(tsHttpServer.contextCache, &ptr, sizeof(HttpContext *)); + EQ_ASSERT(ppContext); + EQ_ASSERT(*ppContext); if (ppContext) { HttpContext *pContext = *ppContext; if (pContext) { + if (!tsHttpServer.fallback) return pContext; int32_t refCount = atomic_add_fetch_32(&pContext->refCount, 1); httpDebug("context:%p, fd:%d, is accquired, data:%p refCount:%d", pContext, pContext->fd, ppContext, refCount); return pContext; @@ -161,6 +174,10 @@ HttpContext *httpGetContext(void *ptr) { } void httpReleaseContext(HttpContext *pContext) { + if (!tsHttpServer.fallback) { + ehttpReleaseContext(pContext); + return; + } int32_t refCount = atomic_sub_fetch_32(&pContext->refCount, 1); if (refCount < 0) { httpError("context:%p, is already released, refCount:%d", pContext, refCount); @@ -217,7 +234,9 @@ bool httpInitContext(HttpContext *pContext) { } void httpCloseContextByApp(HttpContext *pContext) { - D("=="); + if (!tsHttpServer.fallback) { + if (pContext->parsed == false) return; + } pContext->parsed = false; bool keepAlive = true; @@ -229,7 +248,6 @@ void httpCloseContextByApp(HttpContext *pContext) { } if (keepAlive) { - D("==keepAlive=="); if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_HANDLING, HTTP_CONTEXT_STATE_READY)) { httpDebug("context:%p, fd:%d, ip:%s, last state:handling, keepAlive:true, reuse context", pContext, pContext->fd, pContext->ipstr); @@ -250,16 +268,19 @@ void httpCloseContextByApp(HttpContext *pContext) { pContext->ipstr, httpContextStateStr(pContext->state), pContext->state); } } else { - D("==not keepAlive=="); httpRemoveContextFromEpoll(pContext); httpDebug("context:%p, fd:%d, ip:%s, last state:%s:%d, keepAlive:false, close context", pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state), pContext->state); } - httpReleaseContext(pContext); + if (tsHttpServer.fallback) httpReleaseContext(pContext); } void httpCloseContextByServer(HttpContext *pContext) { + if (!tsHttpServer.fallback) { + if (pContext->closed) return; + pContext->closed = 1; + } if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_HANDLING, HTTP_CONTEXT_STATE_DROPPING)) { httpDebug("context:%p, fd:%d, ip:%s, epoll finished, still used by app", pContext, pContext->fd, pContext->ipstr); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_DROPPING, HTTP_CONTEXT_STATE_DROPPING)) { @@ -274,7 +295,7 @@ void httpCloseContextByServer(HttpContext *pContext) { pContext->parsed = false; httpRemoveContextFromEpoll(pContext); - httpReleaseContext(pContext); + if (tsHttpServer.fallback) httpReleaseContext(pContext); } @@ -409,7 +430,44 @@ static void on_error(void *arg, int status_code) { HttpContext *pContext = (HttpContext*)arg; HttpParser *pParser = &pContext->parser; - D("=="); pParser->failed |= EHTTP_CONTEXT_PARSER_FAILED; } +static void httpMightDestroyContext(void *data) { + HttpContext *pContext = *(HttpContext **)data; + if (!tsHttpServer.fallback) { + httpRemoveContextFromEpoll(pContext); + ehttpDecContextRef(&pContext); + return; + } + int32_t refCount = atomic_sub_fetch_32(&pContext->refCount, 1); + if (refCount>0) return; + EQ_ASSERT(refCount==0); + httpDestroyContext(data); +} + +static void ehttpReleaseContext(HttpContext *pContext) { + HttpContext **ppContext = pContext->ppContext; + + if (tsHttpServer.contextCache != NULL) { + taosCacheRelease(tsHttpServer.contextCache, (void **)(&ppContext), false); + } else { + httpDebug("context:%p, won't be destroyed for cache is already released", pContext); + // httpDestroyContext((void **)(&ppContext)); + } +} + +void ehttpIncContextRef(HttpContext *pContext) { + if (tsHttpServer.fallback) return; + atomic_add_fetch_32(&pContext->refCount, 1); +} + +void ehttpDecContextRef(HttpContext **ppContext) { + if (tsHttpServer.fallback) return; + HttpContext *pContext = *ppContext; + int32_t refCount = atomic_sub_fetch_32(&pContext->refCount, 1); + if (refCount>0) return; + EQ_ASSERT(refCount==0); + httpDestroyContext(ppContext); +} + diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index 819f7a5f4a..5a785d2e55 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -194,6 +194,8 @@ static void httpProcessHttpData(void *param) { sigaddset(&set, SIGPIPE); pthread_sigmask(SIG_SETMASK, &set, NULL); + elog_set_thread_name("httpProcessHttpData"); + while (1) { struct epoll_event events[HTTP_MAX_EVENTS]; //-1 means uncertainty, 0-nowait, 1-wait 1 ms, set it from -1 to 1 @@ -209,14 +211,18 @@ static void httpProcessHttpData(void *param) { if (pContext == NULL) { httpError("context:%p, is already released, close connect", events[i].data.ptr); //epoll_ctl(pThread->pollFd, EPOLL_CTL_DEL, events[i].data.fd, NULL); - //tclose(events[i].data.fd); + //taosClose(events[i].data.fd); continue; } + ehttpIncContextRef(pContext); + if (events[i].events & EPOLLPRI) { httpDebug("context:%p, fd:%d, ip:%s, state:%s, EPOLLPRI events occured, accessed:%d, close connect", pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state), pContext->accessTimes); httpCloseContextByServer(pContext); + if (!tsHttpServer.fallback) httpReleaseContext(pContext); + ehttpDecContextRef(&pContext); continue; } @@ -224,6 +230,8 @@ static void httpProcessHttpData(void *param) { httpDebug("context:%p, fd:%d, ip:%s, state:%s, EPOLLRDHUP events occured, accessed:%d, close connect", pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state), pContext->accessTimes); httpCloseContextByServer(pContext); + httpReleaseContext(pContext); + ehttpDecContextRef(&pContext); continue; } @@ -231,6 +239,8 @@ static void httpProcessHttpData(void *param) { httpDebug("context:%p, fd:%d, ip:%s, state:%s, EPOLLERR events occured, accessed:%d, close connect", pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state), pContext->accessTimes); httpCloseContextByServer(pContext); + if (!tsHttpServer.fallback) httpReleaseContext(pContext); + ehttpDecContextRef(&pContext); continue; } @@ -238,6 +248,8 @@ static void httpProcessHttpData(void *param) { httpDebug("context:%p, fd:%d, ip:%s, state:%s, EPOLLHUP events occured, accessed:%d, close connect", pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state), pContext->accessTimes); httpCloseContextByServer(pContext); + if (!tsHttpServer.fallback) httpReleaseContext(pContext); + ehttpDecContextRef(&pContext); continue; } @@ -245,6 +257,7 @@ static void httpProcessHttpData(void *param) { httpDebug("context:%p, fd:%d, ip:%s, state:%s, not in ready state, ignore read events", pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state)); httpReleaseContext(pContext); + ehttpDecContextRef(&pContext); continue; } @@ -253,11 +266,15 @@ static void httpProcessHttpData(void *param) { pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state), pContext->accessTimes); httpSendErrorResp(pContext, HTTP_SERVER_OFFLINE); httpNotifyContextClose(pContext); + if (!tsHttpServer.fallback) httpReleaseContext(pContext); + ehttpDecContextRef(&pContext); } else { if (httpReadData(pContext)) { (*(pThread->processData))(pContext); atomic_fetch_add_32(&pServer->requestNum, 1); } + if (!tsHttpServer.fallback) httpReleaseContext(pContext); + ehttpDecContextRef(&pContext); } } } @@ -338,7 +355,8 @@ static void *httpAcceptHttpConnection(void *arg) { httpError("context:%p, fd:%d, ip:%s, thread:%s, failed to add http fd for epoll, error:%s", pContext, connFd, pContext->ipstr, pThread->label, strerror(errno)); tclose(pContext->fd); - httpReleaseContext(pContext); + if (tsHttpServer.fallback) httpReleaseContext(pContext); + ehttpDecContextRef(&pContext); continue; } @@ -455,7 +473,6 @@ static bool ehttpReadData(HttpContext *pContext) { int ret = HTTP_CHECK_BODY_SUCCESS; if (ret == HTTP_CHECK_BODY_CONTINUE) { //httpDebug("context:%p, fd:%d, ip:%s, not finished yet, wait another event", pContext, pContext->fd, pContext->ipstr); - httpReleaseContext(pContext); return false; } else if (ret == HTTP_CHECK_BODY_SUCCESS){ httpDebug("context:%p, fd:%d, ip:%s, thread:%s, read size:%d, dataLen:%d", @@ -464,13 +481,11 @@ static bool ehttpReadData(HttpContext *pContext) { return true; } else { httpNotifyContextClose(pContext); - httpReleaseContext(pContext); return false; } } else { httpError("context:%p, fd:%d, ip:%s, failed to read http body, close connect", pContext, pContext->fd, pContext->ipstr); httpNotifyContextClose(pContext); - httpReleaseContext(pContext); return false; } } @@ -483,14 +498,10 @@ static bool ehttpReadData(HttpContext *pContext) { } else { httpError("context:%p, fd:%d, ip:%s, read from socket error:%d, close connect", pContext, pContext->fd, pContext->ipstr, errno); - D("==releasing because of reading failed=="); - httpReleaseContext(pContext); return false; } } else { // eof - D("==releasing because of remote close/reset=="); - httpReleaseContext(pContext); return false; } } From aac52f9691fcb4aa13eac068c22b1b287070e7e0 Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 1 Aug 2020 22:12:35 +0800 Subject: [PATCH 06/30] no need to dec ref in fallback mode --- src/plugins/http/src/httpContext.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c index 98028fdbb4..ab10234662 100644 --- a/src/plugins/http/src/httpContext.c +++ b/src/plugins/http/src/httpContext.c @@ -438,9 +438,6 @@ static void httpMightDestroyContext(void *data) { ehttpDecContextRef(&pContext); return; } - int32_t refCount = atomic_sub_fetch_32(&pContext->refCount, 1); - if (refCount>0) return; - EQ_ASSERT(refCount==0); httpDestroyContext(data); } From 46cf2db0a267994c8e1113fe1f4d750cfcdcaa26 Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 1 Aug 2020 22:41:29 +0800 Subject: [PATCH 07/30] reallocarray => realloc, because of configuration in travis --- src/plugins/http/src/ehttp_parser.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/plugins/http/src/ehttp_parser.c b/src/plugins/http/src/ehttp_parser.c index fbe15661b5..30d37f8a0d 100644 --- a/src/plugins/http/src/ehttp_parser.c +++ b/src/plugins/http/src/ehttp_parser.c @@ -185,7 +185,8 @@ static HTTP_PARSER_STATE ehttp_parser_top(ehttp_parser_t *parser) { static int ehttp_parser_push(ehttp_parser_t *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*)reallocarray(parser->stacks, n, sizeof(*stacks)); + HTTP_PARSER_STATE *stacks = (HTTP_PARSER_STATE*)realloc(parser->stacks, n * sizeof(*stacks)); if (!stacks) return -1; parser->stacks_count = n; @@ -380,7 +381,8 @@ static int ehttp_parser_check_field(ehttp_parser_t *parser, const char *key, con } static int ehttp_parser_kvs_append_kv(ehttp_parser_t *parser, const char *key, const char *val) { - ehttp_parser_kv_t *kvs = (ehttp_parser_kv_t*)reallocarray(parser->kvs, parser->kvs_count + 1, sizeof(*kvs)); + // ehttp_parser_kv_t *kvs = (ehttp_parser_kv_t*)reallocarray(parser->kvs, parser->kvs_count + 1, sizeof(*kvs)); + ehttp_parser_kv_t *kvs = (ehttp_parser_kv_t*)realloc(parser->kvs, (parser->kvs_count + 1) * sizeof(*kvs)); if (!kvs) return -1; parser->kvs = kvs; From 6fb53b30523f824832a251f52374143b96980ce2 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sat, 12 Sep 2020 08:13:02 +0000 Subject: [PATCH 08/30] TD-1311 --- src/plugins/http/inc/httpContext.h | 3 - src/plugins/http/inc/httpInt.h | 4 - src/plugins/http/src/gcHandle.c | 26 ++-- src/plugins/http/src/httpAuth.c | 27 ++-- src/plugins/http/src/httpContext.c | 224 ++++++++++------------------- src/plugins/http/src/httpHandle.c | 162 +++------------------ src/plugins/http/src/httpJson.c | 45 +++--- src/plugins/http/src/httpResp.c | 4 +- src/plugins/http/src/httpServer.c | 189 +++++------------------- src/plugins/http/src/httpSession.c | 13 +- src/plugins/http/src/httpSql.c | 107 +++++++------- src/plugins/http/src/httpSystem.c | 6 - src/plugins/http/src/httpUtil.c | 27 ++-- src/plugins/http/src/restHandle.c | 6 +- src/plugins/http/src/restJson.c | 18 ++- src/plugins/http/src/tgHandle.c | 6 +- src/plugins/http/src/tgJson.c | 21 ++- 17 files changed, 265 insertions(+), 623 deletions(-) diff --git a/src/plugins/http/inc/httpContext.h b/src/plugins/http/inc/httpContext.h index 594900d0cf..a2d50d6b7f 100644 --- a/src/plugins/http/inc/httpContext.h +++ b/src/plugins/http/inc/httpContext.h @@ -31,7 +31,4 @@ void httpCloseContextByApp(HttpContext *pContext); void httpNotifyContextClose(HttpContext *pContext); bool httpAlterContextState(HttpContext *pContext, HttpContextState srcState, HttpContextState destState); -void ehttpIncContextRef(HttpContext *pContext); -void ehttpDecContextRef(HttpContext **ppContext); - #endif diff --git a/src/plugins/http/inc/httpInt.h b/src/plugins/http/inc/httpInt.h index 40f980f101..044b5cc4cc 100644 --- a/src/plugins/http/inc/httpInt.h +++ b/src/plugins/http/inc/httpInt.h @@ -212,8 +212,6 @@ typedef struct HttpContext { void * timer; HttpEncodeMethod * encodeMethod; struct HttpThread *pThread; - - int closed:2; } HttpContext; typedef struct HttpThread { @@ -244,8 +242,6 @@ typedef struct HttpServer { pthread_mutex_t serverMutex; HttpDecodeMethod *methodScanner[HTTP_METHOD_SCANNER_SIZE]; bool (*processData)(HttpContext *pContext); - - int fallback:2; } HttpServer; extern const char *httpKeepAliveStr[]; diff --git a/src/plugins/http/src/gcHandle.c b/src/plugins/http/src/gcHandle.c index 72b73b4bad..4aed6eb5cc 100644 --- a/src/plugins/http/src/gcHandle.c +++ b/src/plugins/http/src/gcHandle.c @@ -67,8 +67,7 @@ bool gcGetPassFromUrl(HttpContext* pContext) { } bool gcProcessLoginRequest(HttpContext* pContext) { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, process grafana login msg", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpDebug("context:%p, fd:%d, user:%s, process grafana login msg", pContext, pContext->fd, pContext->user); pContext->reqType = HTTP_REQTYPE_LOGIN; return true; } @@ -143,7 +142,7 @@ bool gcProcessLoginRequest(HttpContext* pContext) { //}] bool gcProcessQueryRequest(HttpContext* pContext) { - httpDebug("context:%p, fd:%d, ip:%s, process grafana query msg", pContext, pContext->fd, pContext->ipstr); + httpDebug("context:%p, fd:%d, process grafana query msg", pContext, pContext->fd); HttpParser* pParser = &pContext->parser; char* filter = pParser->data.pos; @@ -183,15 +182,13 @@ bool gcProcessQueryRequest(HttpContext* pContext) { cJSON* refId = cJSON_GetObjectItem(query, "refId"); if (refId == NULL || refId->valuestring == NULL || strlen(refId->valuestring) == 0) { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, refId is null", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpDebug("context:%p, fd:%d, user:%s, refId is null", pContext, pContext->fd, pContext->user); continue; } int refIdBuffer = httpAddToSqlCmdBuffer(pContext, refId->valuestring); if (refIdBuffer == -1) { - httpWarn("context:%p, fd:%d, ip:%s, user:%s, refId buffer is full", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpWarn("context:%p, fd:%d, user:%s, refId buffer is full", pContext, pContext->fd, pContext->user); break; } @@ -200,8 +197,7 @@ bool gcProcessQueryRequest(HttpContext* pContext) { if (!(alias == NULL || alias->valuestring == NULL || strlen(alias->valuestring) == 0)) { aliasBuffer = httpAddToSqlCmdBuffer(pContext, alias->valuestring); if (aliasBuffer == -1) { - httpWarn("context:%p, fd:%d, ip:%s, user:%s, alias buffer is full", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpWarn("context:%p, fd:%d, user:%s, alias buffer is full", pContext, pContext->fd, pContext->user); break; } } @@ -211,15 +207,13 @@ bool gcProcessQueryRequest(HttpContext* pContext) { cJSON* sql = cJSON_GetObjectItem(query, "sql"); if (sql == NULL || sql->valuestring == NULL || strlen(sql->valuestring) == 0) { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, sql is null", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpDebug("context:%p, fd:%d, user:%s, sql is null", pContext, pContext->fd, pContext->user); continue; } int sqlBuffer = httpAddToSqlCmdBuffer(pContext, sql->valuestring); if (sqlBuffer == -1) { - httpWarn("context:%p, fd:%d, ip:%s, user:%s, sql buffer is full", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpWarn("context:%p, fd:%d, user:%s, sql buffer is full", pContext, pContext->fd, pContext->user); break; } @@ -237,8 +231,8 @@ bool gcProcessQueryRequest(HttpContext* pContext) { cmd->timestamp = httpAddToSqlCmdBufferWithSize(pContext, HTTP_GC_TARGET_SIZE + 1); // hack way if (cmd->timestamp == -1) { - httpWarn("context:%p, fd:%d, ip:%s, user:%s, cant't malloc target size, sql buffer is full", - pContext, pContext->fd, pContext->ipstr, pContext->user); + httpWarn("context:%p, fd:%d, user:%s, cant't malloc target size, sql buffer is full", pContext, pContext->fd, + pContext->user); break; } } @@ -251,7 +245,7 @@ bool gcProcessQueryRequest(HttpContext* pContext) { } bool gcProcessHeartbeatRequest(HttpContext* pContext) { - httpDebug("context:%p, fd:%d, ip:%s, process grafana heartbeat msg", pContext, pContext->fd, pContext->ipstr); + httpDebug("context:%p, fd:%d, process grafana heartbeat msg", pContext, pContext->fd); pContext->reqType = HTTP_REQTYPE_HEARTBEAT; pContext->encodeMethod = &gcHeartBeatMethod; return true; diff --git a/src/plugins/http/src/httpAuth.c b/src/plugins/http/src/httpAuth.c index ea7024fad6..dd4d14c709 100644 --- a/src/plugins/http/src/httpAuth.c +++ b/src/plugins/http/src/httpAuth.c @@ -28,23 +28,21 @@ bool httpParseBasicAuthToken(HttpContext *pContext, char *token, int len) { int outlen = 0; char *base64 = (char *)base64_decode(token, len, &outlen); if (base64 == NULL || outlen == 0) { - httpError("context:%p, fd:%d, ip:%s, basic token:%s parsed error", pContext, pContext->fd, pContext->ipstr, token); + httpError("context:%p, fd:%d, basic token:%s parsed error", pContext, pContext->fd, token); free(base64); return false; } char *user = strstr(base64, ":"); if (user == NULL) { - httpError("context:%p, fd:%d, ip:%s, basic token:%s invalid format", pContext, pContext->fd, pContext->ipstr, - token); + httpError("context:%p, fd:%d, basic token:%s invalid format", pContext, pContext->fd, token); free(base64); return false; } int user_len = (int)(user - base64); if (user_len < 1 || user_len >= TSDB_USER_LEN) { - httpError("context:%p, fd:%d, ip:%s, basic token:%s parse user error", pContext, pContext->fd, pContext->ipstr, - token); + httpError("context:%p, fd:%d, basic token:%s parse user error", pContext, pContext->fd, token); free(base64); return false; } @@ -54,8 +52,7 @@ bool httpParseBasicAuthToken(HttpContext *pContext, char *token, int len) { char *password = user + 1; int pass_len = (int)((base64 + outlen) - password); if (pass_len < 1 || pass_len >= TSDB_PASSWORD_LEN) { - httpError("context:%p, fd:%d, ip:%s, basic token:%s parse password error", pContext, pContext->fd, pContext->ipstr, - token); + httpError("context:%p, fd:%d, basic token:%s parse password error", pContext, pContext->fd, token); free(base64); return false; } @@ -63,8 +60,7 @@ bool httpParseBasicAuthToken(HttpContext *pContext, char *token, int len) { pContext->pass[pass_len] = 0; free(base64); - httpDebug("context:%p, fd:%d, ip:%s, basic token parsed success, user:%s", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpDebug("context:%p, fd:%d, basic token parsed success, user:%s", pContext, pContext->fd, pContext->user); return true; } @@ -73,28 +69,27 @@ bool httpParseTaosdAuthToken(HttpContext *pContext, char *token, int len) { int outlen = 0; unsigned char *base64 = base64_decode(token, len, &outlen); if (base64 == NULL || outlen == 0) { - httpError("context:%p, fd:%d, ip:%s, taosd token:%s parsed error", pContext, pContext->fd, pContext->ipstr, token); + httpError("context:%p, fd:%d, taosd token:%s parsed error", pContext, pContext->fd, token); if (base64) free(base64); return false; } if (outlen != (TSDB_USER_LEN + TSDB_PASSWORD_LEN)) { - httpError("context:%p, fd:%d, ip:%s, taosd token:%s length error", pContext, pContext->fd, pContext->ipstr, token); + httpError("context:%p, fd:%d, taosd token:%s length error", pContext, pContext->fd, token); free(base64); return false; } char *descrypt = taosDesDecode(KEY_DES_4, (char *)base64, outlen); if (descrypt == NULL) { - httpError("context:%p, fd:%d, ip:%s, taosd token:%s descrypt error", pContext, pContext->fd, pContext->ipstr, - token); + httpError("context:%p, fd:%d, taosd token:%s descrypt error", pContext, pContext->fd, token); free(base64); return false; } else { tstrncpy(pContext->user, descrypt, sizeof(pContext->user)); tstrncpy(pContext->pass, descrypt + TSDB_USER_LEN, sizeof(pContext->pass)); - httpDebug("context:%p, fd:%d, ip:%s, taosd token:%s parsed success, user:%s", pContext, pContext->fd, - pContext->ipstr, token, pContext->user); + httpDebug("context:%p, fd:%d, taosd token:%s parsed success, user:%s", pContext, pContext->fd, token, + pContext->user); free(base64); free(descrypt); return true; @@ -116,7 +111,7 @@ bool httpGenTaosdAuthToken(HttpContext *pContext, char *token, int maxLen) { free(encrypt); free(base64); - httpDebug("context:%p, fd:%d, ip:%s, gen taosd token:%s", pContext, pContext->fd, pContext->ipstr, token); + httpDebug("context:%p, fd:%d, gen taosd token:%s", pContext, pContext->fd, token); return true; } diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c index 16d8e91899..59c81f5960 100644 --- a/src/plugins/http/src/httpContext.c +++ b/src/plugins/http/src/httpContext.c @@ -37,16 +37,14 @@ extern bool httpParseHttpVersion(HttpContext* pContext); extern bool httpGetDecodeMethod(HttpContext* pContext); extern bool httpParseHead(HttpContext* pContext); -static void on_request_line(void *arg, const char *method, const char *target, const char *version, const char *target_raw); -static void on_status_line(void *arg, const char *version, int status_code, const char *reason_phrase); -static void on_header_field(void *arg, const char *key, const char *val); -static void on_body(void *arg, const char *chunk, size_t len); -static void on_end(void *arg); -static void on_error(void *arg, int status_code); +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); static void httpDestroyContext(void *data); -static void httpMightDestroyContext(void *data); -static void ehttpReleaseContext(HttpContext *pContext); static void httpRemoveContextFromEpoll(HttpContext *pContext) { HttpThread *pThread = pContext->pThread; @@ -54,15 +52,11 @@ static void httpRemoveContextFromEpoll(HttpContext *pContext) { epoll_ctl(pThread->pollFd, EPOLL_CTL_DEL, pContext->fd, NULL); int32_t fd = atomic_val_compare_exchange_32(&pContext->fd, pContext->fd, -1); taosCloseSocket(fd); - if (!tsHttpServer.fallback) { - ehttpDecContextRef(&pContext); - } } } static void httpDestroyContext(void *data) { HttpContext *pContext = *(HttpContext **)data; - D("==context[%p] destroyed==", pContext); if (pContext->fd > 0) taosClose(pContext->fd); HttpThread *pThread = pContext->pThread; @@ -79,18 +73,16 @@ static void httpDestroyContext(void *data) { httpFreeJsonBuf(pContext); httpFreeMultiCmds(pContext); - if (!tsHttpServer.fallback) { - if (pContext->parser.parser) { - ehttp_parser_destroy(pContext->parser.parser); - pContext->parser.parser = NULL; - } + if (pContext->parser.parser) { + ehttp_parser_destroy(pContext->parser.parser); + pContext->parser.parser = NULL; } taosTFree(pContext); } bool httpInitContexts() { - tsHttpServer.contextCache = taosCacheInit(TSDB_DATA_TYPE_BIGINT, 2, true, httpMightDestroyContext, "restc"); + tsHttpServer.contextCache = taosCacheInit(TSDB_DATA_TYPE_BIGINT, 2, true, httpDestroyContext, "restc"); if (tsHttpServer.contextCache == NULL) { httpError("failed to init context cache"); return false; @@ -135,20 +127,16 @@ HttpContext *httpCreateContext(int32_t fd) { HttpContext *pContext = calloc(1, sizeof(HttpContext)); if (pContext == NULL) return NULL; - D("==context[%p] created==", pContext); - pContext->fd = fd; pContext->httpVersion = HTTP_VERSION_10; pContext->lastAccessTime = taosGetTimestampSec(); pContext->state = HTTP_CONTEXT_STATE_READY; - ehttpIncContextRef(pContext); uint64_t handleVal = (uint64_t)pContext; HttpContext **ppContext = taosCachePut(tsHttpServer.contextCache, &handleVal, sizeof(int64_t), &pContext, sizeof(int64_t), 3000); pContext->ppContext = ppContext; httpDebug("context:%p, fd:%d, is created, data:%p", pContext, fd, ppContext); - ehttpIncContextRef(pContext); // set the ref to 0 taosCacheRelease(tsHttpServer.contextCache, (void**)&ppContext, false); @@ -164,7 +152,6 @@ HttpContext *httpGetContext(void *ptr) { if (ppContext) { HttpContext *pContext = *ppContext; if (pContext) { - if (!tsHttpServer.fallback) return pContext; int32_t refCount = atomic_add_fetch_32(&pContext->refCount, 1); httpDebug("context:%p, fd:%d, is accquired, data:%p refCount:%d", pContext, pContext->fd, ppContext, refCount); return pContext; @@ -174,10 +161,6 @@ HttpContext *httpGetContext(void *ptr) { } void httpReleaseContext(HttpContext *pContext) { - if (!tsHttpServer.fallback) { - ehttpReleaseContext(pContext); - return; - } int32_t refCount = atomic_sub_fetch_32(&pContext->refCount, 1); if (refCount < 0) { httpError("context:%p, is already released, refCount:%d", pContext, refCount); @@ -212,31 +195,25 @@ bool httpInitContext(HttpContext *pContext) { memset(pParser, 0, sizeof(HttpParser)); pParser->pCur = pParser->pLast = pParser->buffer; - if (!tsHttpServer.fallback) { - ehttp_parser_callbacks_t callbacks = { - on_request_line, - on_status_line, - on_header_field, - on_body, - on_end, - on_error - }; - ehttp_parser_conf_t conf = { - .flush_block_size = 0 - }; - pParser->parser = ehttp_parser_create(callbacks, conf, pContext); - pParser->inited = 1; - } + ehttp_parser_callbacks_t callbacks = { + httpParseOnRequestLine, + httpParseOnStatusLine, + httpParseOnHeaderField, + httpParseOnBody, + httpParseOnEnd, + httpParseOnError + }; + ehttp_parser_conf_t conf = { + .flush_block_size = 0 + }; + pParser->parser = ehttp_parser_create(callbacks, conf, pContext); + pParser->inited = 1; - httpDebug("context:%p, fd:%d, ip:%s, accessTimes:%d, parsed:%d", pContext, pContext->fd, pContext->ipstr, - pContext->accessTimes, pContext->parsed); + httpDebug("context:%p, fd:%d, parsed:%d", pContext, pContext->fd, pContext->parsed); return true; } void httpCloseContextByApp(HttpContext *pContext) { - if (!tsHttpServer.fallback) { - if (pContext->parsed == false) return; - } pContext->parsed = false; bool keepAlive = true; @@ -249,150 +226,132 @@ void httpCloseContextByApp(HttpContext *pContext) { if (keepAlive) { if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_HANDLING, HTTP_CONTEXT_STATE_READY)) { - httpDebug("context:%p, fd:%d, ip:%s, last state:handling, keepAlive:true, reuse context", pContext, pContext->fd, - pContext->ipstr); + httpDebug("context:%p, fd:%d, last state:handling, keepAlive:true, reuse context", pContext, pContext->fd); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_DROPPING, HTTP_CONTEXT_STATE_CLOSED)) { httpRemoveContextFromEpoll(pContext); - httpDebug("context:%p, fd:%d, ip:%s, last state:dropping, keepAlive:true, close connect", pContext, pContext->fd, - pContext->ipstr); + httpDebug("context:%p, fd:%d, ast state:dropping, keepAlive:true, close connect", pContext, pContext->fd); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_READY, HTTP_CONTEXT_STATE_READY)) { - httpDebug("context:%p, fd:%d, ip:%s, last state:ready, keepAlive:true, reuse context", pContext, pContext->fd, - pContext->ipstr); + httpDebug("context:%p, fd:%d, last state:ready, keepAlive:true, reuse context", pContext, pContext->fd); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_CLOSED, HTTP_CONTEXT_STATE_CLOSED)) { httpRemoveContextFromEpoll(pContext); - httpDebug("context:%p, fd:%d, ip:%s, last state:ready, keepAlive:true, close connect", pContext, pContext->fd, - pContext->ipstr); + httpDebug("context:%p, fd:%d, last state:ready, keepAlive:true, close connect", pContext, pContext->fd); } else { httpRemoveContextFromEpoll(pContext); - httpError("context:%p, fd:%d, ip:%s, last state:%s:%d, keepAlive:true, close connect", pContext, pContext->fd, - pContext->ipstr, httpContextStateStr(pContext->state), pContext->state); + httpError("context:%p, fd:%d, last state:%s:%d, keepAlive:true, close connect", pContext, pContext->fd, + httpContextStateStr(pContext->state), pContext->state); } } else { httpRemoveContextFromEpoll(pContext); - httpDebug("context:%p, fd:%d, ip:%s, last state:%s:%d, keepAlive:false, close context", pContext, pContext->fd, - pContext->ipstr, httpContextStateStr(pContext->state), pContext->state); + httpDebug("context:%p, fd:%d, ilast state:%s:%d, keepAlive:false, close context", pContext, pContext->fd, + httpContextStateStr(pContext->state), pContext->state); } - if (tsHttpServer.fallback) httpReleaseContext(pContext); + httpReleaseContext(pContext); } void httpCloseContextByServer(HttpContext *pContext) { - if (!tsHttpServer.fallback) { - if (pContext->closed) return; - pContext->closed = 1; - } if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_HANDLING, HTTP_CONTEXT_STATE_DROPPING)) { - httpDebug("context:%p, fd:%d, ip:%s, epoll finished, still used by app", pContext, pContext->fd, pContext->ipstr); + httpDebug("context:%p, fd:%d, epoll finished, still used by app", pContext, pContext->fd); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_DROPPING, HTTP_CONTEXT_STATE_DROPPING)) { - httpDebug("context:%p, fd:%d, ip:%s, epoll already finished, wait app finished", pContext, pContext->fd, pContext->ipstr); + httpDebug("context:%p, fd:%d, epoll already finished, wait app finished", pContext, pContext->fd); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_READY, HTTP_CONTEXT_STATE_CLOSED)) { - httpDebug("context:%p, fd:%d, ip:%s, epoll finished, close connect", pContext, pContext->fd, pContext->ipstr); + httpDebug("context:%p, fd:%d, epoll finished, close connect", pContext, pContext->fd); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_CLOSED, HTTP_CONTEXT_STATE_CLOSED)) { - httpDebug("context:%p, fd:%d, ip:%s, epoll finished, will be closed soon", pContext, pContext->fd, pContext->ipstr); + httpDebug("context:%p, fd:%d, epoll finished, will be closed soon", pContext, pContext->fd); } else { - httpError("context:%p, fd:%d, ip:%s, unknown state:%d", pContext, pContext->fd, pContext->ipstr, pContext->state); + httpError("context:%p, fd:%d, unknown state:%d", pContext, pContext->fd, pContext->state); } pContext->parsed = false; httpRemoveContextFromEpoll(pContext); - if (tsHttpServer.fallback) httpReleaseContext(pContext); } - - - - -static void on_request_line(void *arg, const char *method, const char *target, const char *version, const char *target_raw) { +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); - + 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, ip:%s, thread:%s, accessTimes:%d, on_request_line(%s,%s,%s,%s), exceeding buffer size", - pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, method, target, version, target_raw); + 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, ip:%s, thread:%s, accessTimes:%d, on_request_line(%s,%s,%s,%s), parse http method failed", - pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, method, target, version, target_raw); + 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, ip:%s, thread:%s, accessTimes:%d, on_request_line(%s,%s,%s,%s), parse http url failed", - pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, method, target, version, target_raw); + 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, ip:%s, thread:%s, accessTimes:%d, on_request_line(%s,%s,%s,%s), parse http version failed", - pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, method, target, version, target_raw); + 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, ip:%s, thread:%s, accessTimes:%d, on_request_line(%s,%s,%s,%s), get decode method failed", - pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, method, target, version, target_raw); + 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; + last += n; + pParser->pLast = last; return; } while (0); pParser->failed |= EHTTP_CONTEXT_PROCESS_FAILED; } -static void on_status_line(void *arg, const char *version, int status_code, const char *reason_phrase) { +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 on_header_field(void *arg, const char *key, const char *val) { +static void httpParseOnHeaderField(void *arg, const char *key, const char *val) { HttpContext *pContext = (HttpContext*)arg; HttpParser *pParser = &pContext->parser; if (pParser->failed) return; - D("==key:[%s], val:[%s]==", key, val); - int avail = sizeof(pParser->buffer) - (pParser->pLast - pParser->buffer); - int n = snprintf(pParser->pLast, avail, - "%s: %s\r\n", key, val); - + 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, ip:%s, thread:%s, accessTimes:%d, on_header_field(%s,%s), exceeding buffer size", - pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, key, val); + 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; + pParser->pCur = pParser->pLast + n; if (!httpParseHead(pContext)) { - httpDebug("context:%p, fd:%d, ip:%s, thread:%s, accessTimes:%d, on_header_field(%s,%s), parse head failed", - pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->accessTimes, key, val); + httpDebug("context:%p, fd:%d, header field(%s,%s), parse failed", pContext, pContext->fd, key, val); break; } - last += n; - pParser->pLast = last; + last += n; + pParser->pLast = last; return; } while (0); pParser->failed |= EHTTP_CONTEXT_PROCESS_FAILED; } -static void on_body(void *arg, const char *chunk, size_t len) { +static void httpParseOnBody(void *arg, const char *chunk, size_t len) { HttpContext *pContext = (HttpContext*)arg; HttpParser *pParser = &pContext->parser; @@ -404,16 +363,18 @@ static void on_body(void *arg, const char *chunk, size_t len) { } int avail = sizeof(pParser->buffer) - (pParser->pLast - pParser->buffer); - if (len+1>=avail) { + 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 on_end(void *arg) { +static void httpParseOnEnd(void *arg) { HttpContext *pContext = (HttpContext*)arg; HttpParser *pParser = &pContext->parser; @@ -424,47 +385,14 @@ static void on_end(void *arg) { if (!pContext->parsed) { pContext->parsed = true; } + + httpDebug("context:%p, fd:%d, parse success", pContext, pContext->fd); } -static void on_error(void *arg, int status_code) { - HttpContext *pContext = (HttpContext*)arg; - HttpParser *pParser = &pContext->parser; +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; } - -static void httpMightDestroyContext(void *data) { - HttpContext *pContext = *(HttpContext **)data; - if (!tsHttpServer.fallback) { - httpRemoveContextFromEpoll(pContext); - ehttpDecContextRef(&pContext); - return; - } - httpDestroyContext(data); -} - -static void ehttpReleaseContext(HttpContext *pContext) { - HttpContext **ppContext = pContext->ppContext; - - if (tsHttpServer.contextCache != NULL) { - taosCacheRelease(tsHttpServer.contextCache, (void **)(&ppContext), false); - } else { - httpDebug("context:%p, won't be destroyed for cache is already released", pContext); - // httpDestroyContext((void **)(&ppContext)); - } -} - -void ehttpIncContextRef(HttpContext *pContext) { - if (tsHttpServer.fallback) return; - atomic_add_fetch_32(&pContext->refCount, 1); -} - -void ehttpDecContextRef(HttpContext **ppContext) { - if (tsHttpServer.fallback) return; - HttpContext *pContext = *ppContext; - int32_t refCount = atomic_sub_fetch_32(&pContext->refCount, 1); - if (refCount>0) return; - EQ_ASSERT(refCount==0); - httpDestroyContext(ppContext); -} - diff --git a/src/plugins/http/src/httpHandle.c b/src/plugins/http/src/httpHandle.c index 407d19b307..59b3268392 100644 --- a/src/plugins/http/src/httpHandle.c +++ b/src/plugins/http/src/httpHandle.c @@ -103,15 +103,13 @@ bool httpParseHttpVersion(HttpContext* pContext) { HttpParser* pParser = &pContext->parser; char* pEnd = strchr(pParser->pLast, '1'); if (pEnd == NULL) { - httpError("context:%p, fd:%d, ip:%s, can't find http version at position:%s", pContext, pContext->fd, - pContext->ipstr, pParser->pLast); + 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, ip:%s, can't find http version at position:%s", pContext, pContext->fd, - pContext->ipstr, pParser->pLast); + 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; } @@ -125,8 +123,7 @@ bool httpParseHttpVersion(HttpContext* pContext) { else pContext->httpVersion = HTTP_VERSION_10; - httpDebug("context:%p, fd:%d, ip:%s, httpVersion:1.%d", pContext, pContext->fd, pContext->ipstr, - pContext->httpVersion); + httpDebug("context:%p, fd:%d, httpVersion:1.%d", pContext, pContext->fd, pContext->httpVersion); return true; } @@ -147,18 +144,20 @@ bool httpGetNextLine(HttpContext* pContext) { 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, ip:%s, httpMethod:%s", pContext, pContext->fd, pContext->ipstr, pParser->method.pos); + httpTrace("context:%p, fd:%d, httpMethod:%s", pContext, pContext->fd, pParser->method.pos); return true; } @@ -176,8 +175,8 @@ bool httpGetDecodeMethod(HttpContext* pContext) { return true; } - httpError("context:%p, fd:%d, ip:%s, error:the url is not support, method:%s, path:%s", - pContext, pContext->fd, pContext->ipstr, pParser->method.pos, pParser->path[0].pos); + 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; @@ -187,23 +186,23 @@ 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, ip:%s, Content-Length:%d", pContext, pContext->fd, pContext->ipstr, + 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, ip:%s, Accept-Encoding:gzip", pContext, pContext->fd, pContext->ipstr); + httpTrace("context:%p, fd:%d, Accept-Encoding:gzip", pContext, pContext->fd); } else { pContext->acceptEncoding = HTTP_COMPRESS_IDENTITY; - httpTrace("context:%p, fd:%d, ip:%s, Accept-Encoding:identity", pContext, pContext->fd, pContext->ipstr); + 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, ip:%s, Content-Encoding:gzip", pContext, pContext->fd, pContext->ipstr); + httpTrace("context:%p, fd:%d, Content-Encoding:gzip", pContext, pContext->fd); } else { pContext->contentEncoding = HTTP_COMPRESS_IDENTITY; - httpTrace("context:%p, fd:%d, ip:%s, Content-Encoding:identity", pContext, pContext->fd, pContext->ipstr); + 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) { @@ -211,8 +210,7 @@ bool httpParseHead(HttpContext* pContext) { } else { pContext->httpKeepAlive = HTTP_KEEPALIVE_DISABLE; } - httpTrace("context:%p, fd:%d, ip:%s, keepAlive:%d", pContext, pContext->fd, pContext->ipstr, - pContext->httpKeepAlive); + 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; @@ -244,129 +242,6 @@ bool httpParseHead(HttpContext* pContext) { return true; } -bool httpParseChunkedBody(HttpContext* pContext, HttpParser* pParser, bool test) { - char* pEnd = pParser->buffer + pParser->bufsize; - char* pRet = pParser->data.pos; - char* pSize = pParser->data.pos; - size_t size = strtoul(pSize, NULL, 16); - if (size <= 0) return false; - - while (size > 0) { - char* pData = strstr(pSize, "\r\n"); - if (pData == NULL || pData >= pEnd) return false; - pData += 2; - - pSize = strstr(pData, "\r\n"); - if (pSize == NULL || pSize >= pEnd) return false; - if ((size_t)(pSize - pData) != size) return false; - pSize += 2; - - if (!test) { - memmove(pRet, pData, size); - pRet += size; - } - - size = strtoul(pSize, NULL, 16); - } - - if (!test) { - *pRet = '\0'; - } - - return true; -} - -int httpReadChunkedBody(HttpContext* pContext, HttpParser* pParser) { - bool parsedOk = httpParseChunkedBody(pContext, pParser, true); - if (parsedOk) { - httpParseChunkedBody(pContext, pParser, false); - return HTTP_CHECK_BODY_SUCCESS; - } else { - httpTrace("context:%p, fd:%d, ip:%s, chunked body not finished, continue read", pContext, pContext->fd, pContext->ipstr); - if (httpReadDataImp(pContext) != HTTP_READ_DATA_SUCCESS) { - httpError("context:%p, fd:%d, ip:%s, read chunked request error", pContext, pContext->fd, pContext->ipstr); - return HTTP_CHECK_BODY_ERROR; - } else { - return HTTP_CHECK_BODY_CONTINUE; - } - } -} - -int httpReadUnChunkedBody(HttpContext* pContext, HttpParser* pParser) { - int dataReadLen = pParser->bufsize - (int)(pParser->data.pos - pParser->buffer); - if (dataReadLen > pParser->data.len) { - httpError("context:%p, fd:%d, ip:%s, un-chunked body length invalid, read size:%d dataReadLen:%d > pContext->data.len:%d", - pContext, pContext->fd, pContext->ipstr, pContext->parser.bufsize, dataReadLen, pParser->data.len); - return HTTP_CHECK_BODY_ERROR; - } else if (dataReadLen < pParser->data.len) { - httpTrace("context:%p, fd:%d, ip:%s, un-chunked body not finished, read size:%d dataReadLen:%d < pContext->data.len:%d, continue read", - pContext, pContext->fd, pContext->ipstr, pContext->parser.bufsize, dataReadLen, pParser->data.len); - return HTTP_CHECK_BODY_CONTINUE; - } else { - return HTTP_CHECK_BODY_SUCCESS; - } -} - -bool httpParseRequest(HttpContext* pContext) { - HttpParser *pParser = &pContext->parser; - if (pContext->parsed) { - return true; - } - - httpTraceL("context:%p, fd:%d, ip:%s, thread:%s, numOfContexts:%d, read size:%d, raw data:\n%s", pContext, - pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->pThread->numOfContexts, - pContext->parser.bufsize, pContext->parser.buffer); - - if (!httpGetHttpMethod(pContext)) { - return false; - } - - if (!httpParseURL(pContext)) { - return false; - } - - if (!httpParseHttpVersion(pContext)) { - return false; - } - - if (!httpGetDecodeMethod(pContext)) { - return false; - } - - do { - if (!httpGetNextLine(pContext)) { - return false; - } - - // Empty line, end of the HTTP HEAD - if (pParser->pCur - pParser->pLast == 1) { - pParser->data.pos = ++pParser->pCur; - break; - } - - if (!httpParseHead(pContext)) { - return false; - } - - pParser->pLast = ++pParser->pCur; - } while (1); - - httpDebug("context:%p, fd:%d, ip:%s, parse http head ok", pContext, pContext->fd, pContext->ipstr); - - pContext->parsed = true; - return true; -} - -int httpCheckReadCompleted(HttpContext* pContext) { - HttpParser* pParser = &pContext->parser; - - if (pContext->httpChunked == HTTP_UNCUNKED) { - return httpReadUnChunkedBody(pContext, pParser); - } else { - return httpReadChunkedBody(pContext, pParser); - } -} - bool httpDecodeRequest(HttpContext* pContext) { HttpParser* pParser = &pContext->parser; if (pParser->pMethod->decodeFp == NULL) { @@ -380,17 +255,16 @@ bool httpDecodeRequest(HttpContext* pContext) { * Process the request from http pServer */ bool httpProcessData(HttpContext* pContext) { - if (!httpAlterContextState(pContext, HTTP_CONTEXT_STATE_READY, HTTP_CONTEXT_STATE_HANDLING)) { - httpDebug("context:%p, fd:%d, ip:%s, state:%s not in ready state, stop process request", - pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state)); + httpDebug("context:%p, fd:%d, state:%s not in ready state, stop process request", pContext, pContext->fd, + httpContextStateStr(pContext->state)); httpCloseContextByApp(pContext); return false; } // handle Cross-domain request if (strcmp(pContext->parser.method.pos, "OPTIONS") == 0) { - httpDebug("context:%p, fd:%d, ip:%s, process options request", pContext, pContext->fd, pContext->ipstr); + httpDebug("context:%p, fd:%d, process options request", pContext, pContext->fd); httpSendOptionResp(pContext, "process options request success"); } else { if (!httpDecodeRequest(pContext)) { diff --git a/src/plugins/http/src/httpJson.c b/src/plugins/http/src/httpJson.c index 4748f03b66..e200efbcef 100644 --- a/src/plugins/http/src/httpJson.c +++ b/src/plugins/http/src/httpJson.c @@ -52,14 +52,12 @@ int httpWriteBufByFd(struct HttpContext* pContext, const char* buf, int sz) { } if (len < 0) { - httpDebug("context:%p, fd:%d, ip:%s, socket write errno:%d, times:%d", - pContext, pContext->fd, pContext->ipstr, errno, countWait); + httpDebug("context:%p, fd:%d, socket write errno:%d, times:%d", pContext, pContext->fd, errno, countWait); if (++countWait > HTTP_WRITE_RETRY_TIMES) break; taosMsleep(HTTP_WRITE_WAIT_TIME_MS); continue; } else if (len == 0) { - httpDebug("context:%p, fd:%d, ip:%s, socket write errno:%d, connect already closed", - pContext, pContext->fd, pContext->ipstr, errno); + httpDebug("context:%p, fd:%d, socket write errno:%d, connect already closed", pContext, pContext->fd, errno); break; } else { countWait = 0; @@ -70,14 +68,13 @@ int httpWriteBufByFd(struct HttpContext* pContext, const char* buf, int sz) { return writeLen; } -int httpWriteBuf(struct HttpContext *pContext, const char *buf, int sz) { +int httpWriteBuf(struct HttpContext* pContext, const char* buf, int sz) { int writeSz = httpWriteBufByFd(pContext, buf, sz); if (writeSz != sz) { - httpError("context:%p, fd:%d, ip:%s, dataSize:%d, writeSize:%d, failed to send response:\n%s", - pContext, pContext->fd, pContext->ipstr, sz, writeSz, buf); + httpError("context:%p, fd:%d, dataSize:%d, writeSize:%d, failed to send response:\n%s", pContext, pContext->fd, sz, + writeSz, buf); } else { - httpTrace("context:%p, fd:%d, ip:%s, dataSize:%d, writeSize:%d, response:\n%s", pContext, pContext->fd, - pContext->ipstr, sz, writeSz, buf); + httpTrace("context:%p, fd:%d, dataSize:%d, writeSize:%d, response:\n%s", pContext, pContext->fd, sz, writeSz, buf); } return writeSz; @@ -86,8 +83,8 @@ int httpWriteBuf(struct HttpContext *pContext, const char *buf, int sz) { int httpWriteBufNoTrace(struct HttpContext *pContext, const char *buf, int sz) { int writeSz = httpWriteBufByFd(pContext, buf, sz); if (writeSz != sz) { - httpError("context:%p, fd:%d, ip:%s, dataSize:%d, writeSize:%d, failed to send response", - pContext, pContext->fd, pContext->ipstr, sz, writeSz); + httpError("context:%p, fd:%d, dataSize:%d, writeSize:%d, failed to send response", pContext, pContext->fd, sz, + writeSz); } return writeSz; @@ -99,7 +96,7 @@ int httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) { uint64_t srcLen = (uint64_t) (buf->lst - buf->buf); if (buf->pContext->fd <= 0) { - httpTrace("context:%p, fd:%d, ip:%s, write json body error", buf->pContext, buf->pContext->fd, buf->pContext->ipstr); + httpTrace("context:%p, fd:%d, write json body error", buf->pContext, buf->pContext->fd); buf->pContext->fd = -1; } @@ -113,12 +110,12 @@ int httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) { if (buf->pContext->acceptEncoding == HTTP_COMPRESS_IDENTITY) { if (buf->lst == buf->buf) { - httpTrace("context:%p, fd:%d, ip:%s, no data need dump", buf->pContext, buf->pContext->fd, buf->pContext->ipstr); + 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); - httpTrace("context:%p, fd:%d, ip:%s, write body, chunkSize:%" PRIu64 ", response:\n%s", - buf->pContext, buf->pContext->fd, buf->pContext->ipstr, srcLen, buf->buf); + 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); } @@ -129,18 +126,18 @@ int httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) { if (ret == 0) { if (compressBufLen > 0) { int len = sprintf(sLen, "%x\r\n", compressBufLen); - httpTrace("context:%p, fd:%d, ip:%s, write body, chunkSize:%" PRIu64 ", compressSize:%d, last:%d, response:\n%s", - buf->pContext, buf->pContext->fd, buf->pContext->ipstr, srcLen, compressBufLen, isTheLast, buf->buf); + 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, (int)compressBufLen); } else { - httpTrace("context:%p, fd:%d, ip:%s, last:%d, compress already dumped, response:\n%s", - buf->pContext, buf->pContext->fd, buf->pContext->ipstr, isTheLast, buf->buf); + httpTrace("context:%p, fd:%d, last:%d, compress already dumped, response:\n%s", buf->pContext, + buf->pContext->fd, isTheLast, buf->buf); return 0; // there is no data to dump. } } else { - httpError("context:%p, fd:%d, ip:%s, failed to compress data, chunkSize:%" PRIu64 ", last:%d, error:%d, response:\n%s", - buf->pContext, buf->pContext->fd, buf->pContext->ipstr, srcLen, isTheLast, ret, buf->buf); + httpError("context:%p, fd:%d, failed to compress data, chunkSize:%" PRIu64 ", last:%d, error:%d, response:\n%s", + buf->pContext, buf->pContext->fd, srcLen, isTheLast, ret, buf->buf); return 0; } } @@ -173,7 +170,7 @@ void httpWriteJsonBufHead(JsonBuf* buf) { void httpWriteJsonBufEnd(JsonBuf* buf) { if (buf->pContext->fd <= 0) { - httpTrace("context:%p, fd:%d, ip:%s, json buf fd is 0", buf->pContext, buf->pContext->fd, buf->pContext->ipstr); + httpTrace("context:%p, fd:%d, json buf fd is 0", buf->pContext, buf->pContext->fd); buf->pContext->fd = -1; } @@ -192,7 +189,7 @@ void httpInitJsonBuf(JsonBuf* buf, struct HttpContext* pContext) { httpGzipCompressInit(buf->pContext); } - httpDebug("context:%p, fd:%d, ip:%s, json buffer initialized", buf->pContext, buf->pContext->fd, buf->pContext->ipstr); + httpDebug("context:%p, fd:%d, json buffer initialized", buf->pContext, buf->pContext->fd); } void httpJsonItemToken(JsonBuf* buf) { diff --git a/src/plugins/http/src/httpResp.c b/src/plugins/http/src/httpResp.c index f53aff7831..a7c17dfdbb 100644 --- a/src/plugins/http/src/httpResp.c +++ b/src/plugins/http/src/httpResp.c @@ -46,7 +46,7 @@ const char *httpRespTemplate[] = { }; static void httpSendErrorRespImp(HttpContext *pContext, int httpCode, char *httpCodeStr, int errNo, char *desc) { - httpError("context:%p, fd:%d, ip:%s, code:%d, error:%s", pContext, pContext->fd, pContext->ipstr, httpCode, desc); + httpError("context:%p, fd:%d, code:%d, error:%s", pContext, pContext->fd, httpCode, desc); char head[512] = {0}; char body[512] = {0}; @@ -169,7 +169,7 @@ void httpSendErrorRespWithDesc(HttpContext *pContext, int errNo, char *desc) { httpCodeStr = "Bad Request"; break; default: - httpError("context:%p, fd:%d, ip:%s, error:%d not recognized", pContext, pContext->fd, pContext->ipstr, errNo); + httpError("context:%p, fd:%d, error:%d not recognized", pContext, pContext->fd, errNo); break; } diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index 6802d3624a..614cc92700 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -31,7 +31,7 @@ #define EPOLLWAKEUP (1u << 29) #endif -static bool ehttpReadData(HttpContext *pContext); +static bool httpReadData(HttpContext *pContext); static void httpStopThread(HttpThread* pThread) { pThread->stop = true; @@ -73,43 +73,9 @@ void httpCleanUpConnect() { httpDebug("http server:%s is cleaned up", pServer->label); } -int httpReadDataImp(HttpContext *pContext) { - HttpParser *pParser = &pContext->parser; - - while (pParser->bufsize <= (HTTP_BUFFER_SIZE - HTTP_STEP_SIZE)) { - int nread = (int)taosReadSocket(pContext->fd, pParser->buffer + pParser->bufsize, HTTP_STEP_SIZE); - if (nread >= 0 && nread < HTTP_STEP_SIZE) { - pParser->bufsize += nread; - break; - } else if (nread < 0) { - if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { - httpDebug("context:%p, fd:%d, ip:%s, read from socket error:%d, wait another event", - pContext, pContext->fd, pContext->ipstr, errno); - break; - } else { - httpError("context:%p, fd:%d, ip:%s, read from socket error:%d, close connect", - pContext, pContext->fd, pContext->ipstr, errno); - return HTTP_READ_DATA_FAILED; - } - } else { - pParser->bufsize += nread; - } - - if (pParser->bufsize >= (HTTP_BUFFER_SIZE - HTTP_STEP_SIZE)) { - httpError("context:%p, fd:%d, ip:%s, thread:%s, request big than:%d", - pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, HTTP_BUFFER_SIZE); - return HTTP_REQUSET_TOO_BIG; - } - } - - pParser->buffer[pParser->bufsize] = 0; - - return HTTP_READ_DATA_SUCCESS; -} - static bool httpDecompressData(HttpContext *pContext) { if (pContext->contentEncoding != HTTP_COMPRESS_GZIP) { - httpTraceL("context:%p, fd:%d, ip:%s, content:%s", pContext, pContext->fd, pContext->ipstr, pContext->parser.data.pos); + httpTraceL("context:%p, fd:%d, content:%s", pContext, pContext->fd, pContext->parser.data.pos); return true; } @@ -125,64 +91,18 @@ static bool httpDecompressData(HttpContext *pContext) { if (ret == 0) { memcpy(pContext->parser.data.pos, decompressBuf, decompressBufLen); pContext->parser.data.pos[decompressBufLen] = 0; - httpTraceL("context:%p, fd:%d, ip:%s, rawSize:%d, decompressSize:%d, content:%s", pContext, pContext->fd, - pContext->ipstr, pContext->parser.data.len, decompressBufLen, decompressBuf); + 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, ip:%s, failed to decompress data, rawSize:%d, error:%d", - pContext, pContext->fd, pContext->ipstr, pContext->parser.data.len, ret); + 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 bool httpReadData(HttpContext *pContext) { - if (!tsHttpServer.fallback) return ehttpReadData(pContext); - - if (!pContext->parsed) { - httpInitContext(pContext); - } - - int32_t code = httpReadDataImp(pContext); - if (code != HTTP_READ_DATA_SUCCESS) { - if (code == HTTP_READ_DATA_FAILED) { - httpReleaseContext(pContext); - } else { - httpSendErrorResp(pContext, code); - httpNotifyContextClose(pContext); - } - return false; - } - - if (!httpParseRequest(pContext)) { - httpNotifyContextClose(pContext); - return false; - } - - int ret = httpCheckReadCompleted(pContext); - if (ret == HTTP_CHECK_BODY_CONTINUE) { - //httpDebug("context:%p, fd:%d, ip:%s, not finished yet, wait another event", pContext, pContext->fd, pContext->ipstr); - httpReleaseContext(pContext); - return false; - } else if (ret == HTTP_CHECK_BODY_SUCCESS){ - httpDebug("context:%p, fd:%d, ip:%s, thread:%s, read size:%d, dataLen:%d", - pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->parser.bufsize, pContext->parser.data.len); - if (httpDecompressData(pContext)) { - return true; - } else { - httpNotifyContextClose(pContext); - httpReleaseContext(pContext); - return false; - } - } else { - httpError("context:%p, fd:%d, ip:%s, failed to read http body, close connect", pContext, pContext->fd, pContext->ipstr); - httpNotifyContextClose(pContext); - httpReleaseContext(pContext); - return false; - } -} - static void httpProcessHttpData(void *param) { HttpServer *pServer = &tsHttpServer; HttpThread *pThread = (HttpThread *)param; @@ -194,8 +114,6 @@ static void httpProcessHttpData(void *param) { sigaddset(&set, SIGPIPE); pthread_sigmask(SIG_SETMASK, &set, NULL); - elog_set_thread_name("httpProcessHttpData"); - while (1) { struct epoll_event events[HTTP_MAX_EVENTS]; //-1 means uncertainty, 0-nowait, 1-wait 1 ms, set it from -1 to 1 @@ -215,66 +133,51 @@ static void httpProcessHttpData(void *param) { continue; } - ehttpIncContextRef(pContext); - if (events[i].events & EPOLLPRI) { - httpDebug("context:%p, fd:%d, ip:%s, state:%s, EPOLLPRI events occured, accessed:%d, close connect", - pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state), pContext->accessTimes); + httpDebug("context:%p, fd:%d, state:%s, EPOLLPRI events occured, accessed:%d, close connect", pContext, + pContext->fd, httpContextStateStr(pContext->state), pContext->accessTimes); httpCloseContextByServer(pContext); - if (!tsHttpServer.fallback) httpReleaseContext(pContext); - ehttpDecContextRef(&pContext); continue; } if (events[i].events & EPOLLRDHUP) { - httpDebug("context:%p, fd:%d, ip:%s, state:%s, EPOLLRDHUP events occured, accessed:%d, close connect", - pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state), pContext->accessTimes); + httpDebug("context:%p, fd:%d, state:%s, EPOLLRDHUP events occured, accessed:%d, close connect", pContext, + pContext->fd, httpContextStateStr(pContext->state), pContext->accessTimes); httpCloseContextByServer(pContext); - httpReleaseContext(pContext); - ehttpDecContextRef(&pContext); continue; } if (events[i].events & EPOLLERR) { - httpDebug("context:%p, fd:%d, ip:%s, state:%s, EPOLLERR events occured, accessed:%d, close connect", - pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state), pContext->accessTimes); + httpDebug("context:%p, fd:%d, state:%s, EPOLLERR events occured, accessed:%d, close connect", pContext, + pContext->fd, httpContextStateStr(pContext->state), pContext->accessTimes); httpCloseContextByServer(pContext); - if (!tsHttpServer.fallback) httpReleaseContext(pContext); - ehttpDecContextRef(&pContext); continue; } if (events[i].events & EPOLLHUP) { - httpDebug("context:%p, fd:%d, ip:%s, state:%s, EPOLLHUP events occured, accessed:%d, close connect", - pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state), pContext->accessTimes); + httpDebug("context:%p, fd:%d, state:%s, EPOLLHUP events occured, accessed:%d, close connect", pContext, + pContext->fd, httpContextStateStr(pContext->state), pContext->accessTimes); httpCloseContextByServer(pContext); - if (!tsHttpServer.fallback) httpReleaseContext(pContext); - ehttpDecContextRef(&pContext); continue; } if (!httpAlterContextState(pContext, HTTP_CONTEXT_STATE_READY, HTTP_CONTEXT_STATE_READY)) { - httpDebug("context:%p, fd:%d, ip:%s, state:%s, not in ready state, ignore read events", - pContext, pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state)); + httpDebug("context:%p, fd:%d, state:%s, not in ready state, ignore read events", pContext, pContext->fd, + httpContextStateStr(pContext->state)); httpReleaseContext(pContext); - ehttpDecContextRef(&pContext); continue; } if (pServer->status != HTTP_SERVER_RUNNING) { - httpDebug("context:%p, fd:%d, ip:%s, state:%s, server is not running, accessed:%d, close connect", pContext, - pContext->fd, pContext->ipstr, httpContextStateStr(pContext->state), pContext->accessTimes); + httpDebug("context:%p, fd:%d, state:%s, server is not running, accessed:%d, close connect", pContext, + pContext->fd, httpContextStateStr(pContext->state), pContext->accessTimes); httpSendErrorResp(pContext, HTTP_SERVER_OFFLINE); httpNotifyContextClose(pContext); - if (!tsHttpServer.fallback) httpReleaseContext(pContext); - ehttpDecContextRef(&pContext); } else { if (httpReadData(pContext)) { (*(pThread->processData))(pContext); atomic_fetch_add_32(&pServer->requestNum, 1); } - if (!tsHttpServer.fallback) httpReleaseContext(pContext); - ehttpDecContextRef(&pContext); } } } @@ -355,8 +258,7 @@ static void *httpAcceptHttpConnection(void *arg) { httpError("context:%p, fd:%d, ip:%s, thread:%s, failed to add http fd for epoll, error:%s", pContext, connFd, pContext->ipstr, pThread->label, strerror(errno)); taosClose(pContext->fd); - if (tsHttpServer.fallback) httpReleaseContext(pContext); - ehttpDecContextRef(&pContext); + httpReleaseContext(pContext); continue; } @@ -430,12 +332,10 @@ bool httpInitConnect() { return true; } - - - -static bool ehttpReadData(HttpContext *pContext) { +static bool httpReadData(HttpContext *pContext) { HttpParser *pParser = &pContext->parser; EQ_ASSERT(!pContext->parsed); + if (!pParser->parser) { if (!pParser->inited) { httpInitContext(pContext); @@ -448,61 +348,44 @@ static bool ehttpReadData(HttpContext *pContext) { pContext->accessTimes++; pContext->lastAccessTime = taosGetTimestampSec(); - char buf[HTTP_STEP_SIZE+1] = {0}; - int nread = (int)taosReadSocket(pContext->fd, buf, sizeof(buf)); + char buf[HTTP_STEP_SIZE + 1] = {0}; + int nread = (int)taosReadSocket(pContext->fd, buf, sizeof(buf)); if (nread > 0) { buf[nread] = '\0'; - if (strstr(buf, "GET ")==buf && !strchr(buf, '\r') && !strchr(buf, '\n')) { - D("==half of request line received:\n%s\n==", buf); - } - if (ehttp_parser_parse(pParser->parser, buf, nread)) { - D("==parsing failed=="); - httpCloseContextByServer(pContext); + httpError("context:%p, fd:%d, init parse failed, close connect", pContext, pContext->fd); + httpNotifyContextClose(pContext); return false; } if (pContext->parser.failed) { - D("==parsing failed: [0x%x]==", pContext->parser.failed); + httpError("context:%p, fd:%d, parse failed, close connect", pContext, pContext->fd); httpNotifyContextClose(pContext); return false; } + if (pContext->parsed) { - // int ret = httpCheckReadCompleted(pContext); - // already done in ehttp_parser - int ret = HTTP_CHECK_BODY_SUCCESS; - if (ret == HTTP_CHECK_BODY_CONTINUE) { - //httpDebug("context:%p, fd:%d, ip:%s, not finished yet, wait another event", pContext, pContext->fd, pContext->ipstr); - return false; - } else if (ret == HTTP_CHECK_BODY_SUCCESS){ - httpDebug("context:%p, fd:%d, ip:%s, thread:%s, read size:%d, dataLen:%d", - pContext, pContext->fd, pContext->ipstr, pContext->pThread->label, pContext->parser.bufsize, pContext->parser.data.len); - if (httpDecompressData(pContext)) { - return true; - } else { - httpNotifyContextClose(pContext); - return false; - } + 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 { - httpError("context:%p, fd:%d, ip:%s, failed to read http body, close connect", pContext, pContext->fd, pContext->ipstr); httpNotifyContextClose(pContext); return false; } } + return pContext->parsed; } else if (nread < 0) { if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { - httpDebug("context:%p, fd:%d, ip:%s, read from socket error:%d, wait another event", - pContext, pContext->fd, pContext->ipstr, errno); - return false; // later again + httpDebug("context:%p, fd:%d, read from socket error:%d, wait another event", pContext, pContext->fd, errno); + return false; // later again } else { - httpError("context:%p, fd:%d, ip:%s, read from socket error:%d, close connect", - pContext, pContext->fd, pContext->ipstr, errno); + httpError("context:%p, fd:%d, read from socket error:%d, close connect", pContext, pContext->fd, errno); return false; } } else { - // eof + httpError("context:%p, fd:%d, nread:%d, wait another event", pContext, pContext->fd, nread); return false; } } - diff --git a/src/plugins/http/src/httpSession.c b/src/plugins/http/src/httpSession.c index f19679e072..4549192407 100644 --- a/src/plugins/http/src/httpSession.c +++ b/src/plugins/http/src/httpSession.c @@ -39,15 +39,15 @@ void httpCreateSession(HttpContext *pContext, void *taos) { // taosCacheRelease(server->sessionCache, (void **)&temp, false); if (pContext->session == NULL) { - httpError("context:%p, fd:%d, ip:%s, user:%s, error:%s", pContext, pContext->fd, pContext->ipstr, pContext->user, + httpError("context:%p, fd:%d, user:%s, error:%s", pContext, pContext->fd, pContext->user, httpMsg[HTTP_SESSION_FULL]); taos_close(taos); pthread_mutex_unlock(&server->serverMutex); return; } - httpDebug("context:%p, fd:%d, ip:%s, user:%s, create a new session:%p:%p sessionRef:%d", pContext, pContext->fd, - pContext->ipstr, pContext->user, pContext->session, pContext->session->taos, pContext->session->refCount); + httpDebug("context:%p, fd:%d, user:%s, create a new session:%p:%p sessionRef:%d", pContext, pContext->fd, + pContext->user, pContext->session, pContext->session->taos, pContext->session->refCount); pthread_mutex_unlock(&server->serverMutex); } @@ -61,11 +61,10 @@ static void httpFetchSessionImp(HttpContext *pContext) { pContext->session = taosCacheAcquireByKey(server->sessionCache, sessionId, len); if (pContext->session != NULL) { atomic_add_fetch_32(&pContext->session->refCount, 1); - httpDebug("context:%p, fd:%d, ip:%s, user:%s, find an exist session:%p:%p, sessionRef:%d", pContext, pContext->fd, - pContext->ipstr, pContext->user, pContext->session, pContext->session->taos, pContext->session->refCount); + httpDebug("context:%p, fd:%d, user:%s, find an exist session:%p:%p, sessionRef:%d", pContext, pContext->fd, + pContext->user, pContext->session, pContext->session->taos, pContext->session->refCount); } else { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, session not found", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpDebug("context:%p, fd:%d, user:%s, session not found", pContext, pContext->fd, pContext->user); } pthread_mutex_unlock(&server->serverMutex); diff --git a/src/plugins/http/src/httpSql.c b/src/plugins/http/src/httpSql.c index 07cdea1380..cbaa0b36d8 100644 --- a/src/plugins/http/src/httpSql.c +++ b/src/plugins/http/src/httpSql.c @@ -56,18 +56,18 @@ void httpProcessMultiSqlRetrieveCallBackImp(void *param, TAOS_RES *result, int n if (isContinue) { // retrieve next batch of rows - httpDebug("context:%p, fd:%d, ip:%s, user:%s, process pos:%d, continue retrieve, numOfRows:%d, sql:%s", - pContext, pContext->fd, pContext->ipstr, pContext->user, multiCmds->pos, numOfRows, sql); + httpDebug("context:%p, fd:%d, user:%s, process pos:%d, continue retrieve, numOfRows:%d, sql:%s", pContext, + pContext->fd, pContext->user, multiCmds->pos, numOfRows, sql); taos_fetch_rows_a(result, httpProcessMultiSqlRetrieveCallBack, param); } else { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, process pos:%d, stop retrieve, numOfRows:%d, sql:%s", - pContext, pContext->fd, pContext->ipstr, pContext->user, multiCmds->pos, numOfRows, sql); + httpDebug("context:%p, fd:%d, user:%s, process pos:%d, stop retrieve, numOfRows:%d, sql:%s", pContext, pContext->fd, + pContext->user, multiCmds->pos, numOfRows, sql); if (numOfRows < 0) { - httpError("context:%p, fd:%d, ip:%s, user:%s, process pos:%d, retrieve failed code:%s, sql:%s", - pContext, pContext->fd, pContext->ipstr, pContext->user, multiCmds->pos, tstrerror(numOfRows), sql); - } - + httpError("context:%p, fd:%d, user:%s, process pos:%d, retrieve failed code:%s, sql:%s", pContext, pContext->fd, + pContext->user, multiCmds->pos, tstrerror(numOfRows), sql); + } + taos_free_result(result); if (singleCmd->cmdReturnType == HTTP_CMD_RETURN_TYPE_WITH_RETURN && encode->stopJsonFp) { @@ -94,20 +94,20 @@ void httpProcessMultiSqlCallBackImp(void *param, TAOS_RES *result, int code) { char * sql = httpGetCmdsString(pContext, singleCmd->sql); if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) { - httpWarn("context:%p, fd:%d, ip:%s, user:%s, process pos:%d, code:%s:inprogress, sql:%s", - pContext, pContext->fd, pContext->ipstr, pContext->user, multiCmds->pos, tstrerror(code), sql); + httpWarn("context:%p, fd:%d, user:%s, process pos:%d, code:%s:inprogress, sql:%s", pContext, pContext->fd, + pContext->user, multiCmds->pos, tstrerror(code), sql); return; } if (code < 0) { if (encode->checkFinishedFp != NULL && !encode->checkFinishedFp(pContext, singleCmd, code)) { singleCmd->code = code; - httpDebug("context:%p, fd:%d, ip:%s, user:%s, process pos jump to:%d, last code:%s, last sql:%s", - pContext, pContext->fd, pContext->ipstr, pContext->user, multiCmds->pos + 1, tstrerror(code), sql); + httpDebug("context:%p, fd:%d, user:%s, process pos jump to:%d, last code:%s, last sql:%s", pContext, pContext->fd, + pContext->user, multiCmds->pos + 1, tstrerror(code), sql); } else { singleCmd->code = code; - httpError("context:%p, fd:%d, ip:%s, user:%s, process pos:%d, error code:%s, sql:%s", - pContext, pContext->fd, pContext->ipstr, pContext->user, multiCmds->pos, tstrerror(code), sql); + httpError("context:%p, fd:%d, user:%s, process pos:%d, error code:%s, sql:%s", pContext, pContext->fd, + pContext->user, multiCmds->pos, tstrerror(code), sql); if (singleCmd->cmdReturnType == HTTP_CMD_RETURN_TYPE_WITH_RETURN) { if (encode->startJsonFp) (encode->startJsonFp)(pContext, singleCmd, result); @@ -125,8 +125,8 @@ void httpProcessMultiSqlCallBackImp(void *param, TAOS_RES *result, int code) { if (isUpdate) { // not select or show commands int affectRows = taos_affected_rows(result); - httpDebug("context:%p, fd:%d, ip:%s, user:%s, process pos:%d, affect rows:%d, sql:%s", - pContext, pContext->fd, pContext->ipstr, pContext->user, multiCmds->pos, affectRows, sql); + httpDebug("context:%p, fd:%d, user:%s, process pos:%d, affect rows:%d, sql:%s", pContext, pContext->fd, + pContext->user, multiCmds->pos, affectRows, sql); singleCmd->code = 0; @@ -151,8 +151,8 @@ void httpProcessMultiSqlCallBackImp(void *param, TAOS_RES *result, int code) { taos_free_result(result); httpProcessMultiSql(pContext); } else { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, process pos:%d, start retrieve, sql:%s", - pContext, pContext->fd, pContext->ipstr, pContext->user, multiCmds->pos, sql); + httpDebug("context:%p, fd:%d, user:%s, process pos:%d, start retrieve, sql:%s", pContext, pContext->fd, + pContext->user, multiCmds->pos, sql); if (singleCmd->cmdReturnType == HTTP_CMD_RETURN_TYPE_WITH_RETURN && encode->startJsonFp) { (encode->startJsonFp)(pContext, singleCmd, result); @@ -170,8 +170,8 @@ void httpProcessMultiSql(HttpContext *pContext) { HttpEncodeMethod *encode = pContext->encodeMethod; if (multiCmds->pos >= multiCmds->size) { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, process pos:%d, size:%d, stop mulit-querys", - pContext, pContext->fd, pContext->ipstr, pContext->user, multiCmds->pos, multiCmds->size); + httpDebug("context:%p, fd:%d, user:%s, process pos:%d, size:%d, stop mulit-querys", pContext, pContext->fd, + pContext->user, multiCmds->pos, multiCmds->size); if (encode->cleanJsonFp) { (encode->cleanJsonFp)(pContext); } @@ -182,8 +182,8 @@ void httpProcessMultiSql(HttpContext *pContext) { HttpSqlCmd *cmd = multiCmds->cmds + multiCmds->pos; char *sql = httpGetCmdsString(pContext, cmd->sql); - httpTraceL("context:%p, fd:%d, ip:%s, user:%s, process pos:%d, start query, sql:%s", pContext, pContext->fd, - pContext->ipstr, pContext->user, multiCmds->pos, sql); + httpTraceL("context:%p, fd:%d, user:%s, process pos:%d, start query, sql:%s", pContext, pContext->fd, pContext->user, + multiCmds->pos, sql); taosNotePrintHttp(sql); taos_query_a(pContext->session->taos, sql, httpProcessMultiSqlCallBack, (void *)pContext); } @@ -197,8 +197,8 @@ void httpProcessMultiSqlCmd(HttpContext *pContext) { return; } - httpDebug("context:%p, fd:%d, ip:%s, user:%s, start multi-querys pos:%d, size:%d", pContext, pContext->fd, - pContext->ipstr, pContext->user, multiCmds->pos, multiCmds->size); + httpDebug("context:%p, fd:%d, user:%s, start multi-querys pos:%d, size:%d", pContext, pContext->fd, pContext->user, + multiCmds->pos, multiCmds->size); HttpEncodeMethod *encode = pContext->encodeMethod; if (encode->initJsonFp) { (encode->initJsonFp)(pContext); @@ -226,24 +226,23 @@ void httpProcessSingleSqlRetrieveCallBackImp(void *param, TAOS_RES *result, int #if 0 // todo refactor if (tscResultsetFetchCompleted(result)) { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, resultset fetch completed", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpDebug("context:%p, fd:%d, user:%s, resultset fetch completed", pContext, pContext->fd, pContext->user); isContinue = false; } #endif if (isContinue) { // retrieve next batch of rows - httpDebug("context:%p, fd:%d, ip:%s, user:%s, continue retrieve, numOfRows:%d", pContext, pContext->fd, - pContext->ipstr, pContext->user, numOfRows); + httpDebug("context:%p, fd:%d, user:%s, continue retrieve, numOfRows:%d", pContext, pContext->fd, pContext->user, + numOfRows); taos_fetch_rows_a(result, httpProcessSingleSqlRetrieveCallBack, param); } else { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, stop retrieve, numOfRows:%d", pContext, pContext->fd, pContext->ipstr, - pContext->user, numOfRows); + httpDebug("context:%p, fd:%d, user:%s, stop retrieve, numOfRows:%d", pContext, pContext->fd, pContext->user, + numOfRows); if (numOfRows < 0) { - httpError("context:%p, fd:%d, ip:%s, user:%s, retrieve failed, code:%s", pContext, pContext->fd, pContext->ipstr, - pContext->user, tstrerror(numOfRows)); + httpError("context:%p, fd:%d, user:%s, retrieve failed, code:%s", pContext, pContext->fd, pContext->user, + tstrerror(numOfRows)); } taos_free_result(result); @@ -269,20 +268,20 @@ void httpProcessSingleSqlCallBackImp(void *param, TAOS_RES *result, int unUsedCo HttpEncodeMethod *encode = pContext->encodeMethod; if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) { - httpError("context:%p, fd:%d, ip:%s, user:%s, query error, taos:%p, code:%s:inprogress, sqlObj:%p", - pContext, pContext->fd, pContext->ipstr, pContext->user, pContext->session->taos, tstrerror(code), (SSqlObj *)result); + httpError("context:%p, fd:%d, user:%s, query error, taos:%p, code:%s:inprogress, sqlObj:%p", pContext, pContext->fd, + pContext->user, pContext->session->taos, tstrerror(code), (SSqlObj *)result); return; } if (code < 0) { SSqlObj *pObj = (SSqlObj *)result; if (code == TSDB_CODE_TSC_INVALID_SQL) { - httpError("context:%p, fd:%d, ip:%s, user:%s, query error, taos:%p, code:%s, sqlObj:%p, error:%s", - pContext, pContext->fd, pContext->ipstr, pContext->user, pContext->session->taos, tstrerror(code), pObj, pObj->cmd.payload); + httpError("context:%p, fd:%d, user:%s, query error, taos:%p, code:%s, sqlObj:%p, error:%s", pContext, + pContext->fd, pContext->user, pContext->session->taos, tstrerror(code), pObj, pObj->cmd.payload); httpSendTaosdInvalidSqlErrorResp(pContext, pObj->cmd.payload); } else { - httpError("context:%p, fd:%d, ip:%s, user:%s, query error, taos:%p, code:%s, sqlObj:%p", - pContext, pContext->fd, pContext->ipstr, pContext->user, pContext->session->taos, tstrerror(code), pObj); + httpError("context:%p, fd:%d, user:%s, query error, taos:%p, code:%s, sqlObj:%p", pContext, pContext->fd, + pContext->user, pContext->session->taos, tstrerror(code), pObj); httpSendTaosdErrorResp(pContext, code); } taos_free_result(result); @@ -294,8 +293,8 @@ void httpProcessSingleSqlCallBackImp(void *param, TAOS_RES *result, int unUsedCo // not select or show commands int affectRows = taos_affected_rows(result); - httpDebug("context:%p, fd:%d, ip:%s, user:%s, affect rows:%d, stop query, sqlObj:%p", - pContext, pContext->fd, pContext->ipstr, pContext->user, affectRows, result); + httpDebug("context:%p, fd:%d, user:%s, affect rows:%d, stop query, sqlObj:%p", pContext, pContext->fd, + pContext->user, affectRows, result); if (encode->startJsonFp) { (encode->startJsonFp)(pContext, &pContext->singleCmd, result); @@ -312,8 +311,7 @@ void httpProcessSingleSqlCallBackImp(void *param, TAOS_RES *result, int unUsedCo taos_free_result(result); httpCloseContextByApp(pContext); } else { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, start retrieve", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpDebug("context:%p, fd:%d, user:%s, start retrieve", pContext, pContext->fd, pContext->user); if (encode->startJsonFp) { (encode->startJsonFp)(pContext, &pContext->singleCmd, result); @@ -333,14 +331,12 @@ void httpProcessSingleSqlCmd(HttpContext *pContext) { HttpSession *pSession = pContext->session; if (sql == NULL || sql[0] == 0) { - httpError("context:%p, fd:%d, ip:%s, user:%s, error:no sql input", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpError("context:%p, fd:%d, user:%s, error:no sql input", pContext, pContext->fd, pContext->user); httpSendErrorResp(pContext, HTTP_NO_SQL_INPUT); return; } - httpTraceL("context:%p, fd:%d, ip:%s, user:%s, start query, sql:%s", pContext, pContext->fd, pContext->ipstr, - pContext->user, sql); + httpTraceL("context:%p, fd:%d, user:%s, start query, sql:%s", pContext, pContext->fd, pContext->user, sql); taosNotePrintHttp(sql); taos_query_a(pSession->taos, sql, httpProcessSingleSqlCallBack, (void *)pContext); } @@ -350,8 +346,8 @@ void httpProcessLoginCmd(HttpContext *pContext) { if (!httpGenTaosdAuthToken(pContext, token, 128)) { httpSendErrorResp(pContext, HTTP_GEN_TAOSD_TOKEN_ERR); } else { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, login via http, return token:%s", - pContext, pContext->fd, pContext->ipstr, pContext->user, token); + httpDebug("context:%p, fd:%d, user:%s, login via http, return token:%s", pContext, pContext->fd, pContext->user, + token); httpSendSuccResp(pContext, token); } } @@ -397,17 +393,16 @@ void httpProcessRequestCb(void *param, TAOS_RES *result, int code) { if (pContext == NULL) return; if (code < 0) { - httpError("context:%p, fd:%d, ip:%s, user:%s, login error, code:%s", pContext, pContext->fd, pContext->ipstr, - pContext->user, tstrerror(code)); + httpError("context:%p, fd:%d, user:%s, login error, code:%s", pContext, pContext->fd, pContext->user, + tstrerror(code)); httpSendTaosdErrorResp(pContext, code); return; } - httpDebug("context:%p, fd:%d, ip:%s, user:%s, connect tdengine success, taos:%p", pContext, pContext->fd, - pContext->ipstr, pContext->user, pContext->taos); + httpDebug("context:%p, fd:%d, user:%s, connect tdengine success, taos:%p", pContext, pContext->fd, pContext->user, + pContext->taos); if (pContext->taos == NULL) { - httpError("context:%p, fd:%d, ip:%s, user:%s, login error, taos is empty", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpError("context:%p, fd:%d, user:%s, login error, taos is empty", pContext, pContext->fd, pContext->user); httpSendErrorResp(pContext, HTTP_NO_ENOUGH_SESSIONS); return; } @@ -428,8 +423,8 @@ void httpProcessRequest(HttpContext *pContext) { if (pContext->session == NULL || pContext->reqType == HTTP_REQTYPE_LOGIN) { taos_connect_a(NULL, pContext->user, pContext->pass, "", 0, httpProcessRequestCb, (void *)pContext, &(pContext->taos)); - httpDebug("context:%p, fd:%d, ip:%s, user:%s, try connect tdengine, taos:%p", pContext, pContext->fd, - pContext->ipstr, pContext->user, pContext->taos); + httpDebug("context:%p, fd:%d, user:%s, try connect tdengine, taos:%p", pContext, pContext->fd, pContext->user, + pContext->taos); } else { httpExecCmd(pContext); } diff --git a/src/plugins/http/src/httpSystem.c b/src/plugins/http/src/httpSystem.c index dc1b9a93be..e51c8dd4f7 100644 --- a/src/plugins/http/src/httpSystem.c +++ b/src/plugins/http/src/httpSystem.c @@ -40,12 +40,6 @@ HttpServer tsHttpServer; void taosInitNote(int numOfNoteLines, int maxNotes, char* lable); int httpInitSystem() { - tsHttpServer.fallback = 0; - const char *v = getenv("FALLBACK"); - if (v) { - tsHttpServer.fallback = 1; - } - strcpy(tsHttpServer.label, "rest"); tsHttpServer.serverIp = 0; tsHttpServer.serverPort = tsHttpPort; diff --git a/src/plugins/http/src/httpUtil.c b/src/plugins/http/src/httpUtil.c index d1a0eb90f0..f6e05dc1ae 100644 --- a/src/plugins/http/src/httpUtil.c +++ b/src/plugins/http/src/httpUtil.c @@ -141,16 +141,15 @@ int32_t httpAddToSqlCmdBufferWithSize(HttpContext *pContext, int mallocSize) { bool httpMallocMultiCmds(HttpContext *pContext, int cmdSize, int bufferSize) { if (cmdSize > HTTP_MAX_CMD_SIZE) { - httpError("context:%p, fd:%d, ip:%s, user:%s, mulitcmd size:%d large then %d", pContext, pContext->fd, - pContext->ipstr, pContext->user, 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); return false; } if (pContext->multiCmds == NULL) { pContext->multiCmds = (HttpSqlCmds *)malloc(sizeof(HttpSqlCmds)); if (pContext->multiCmds == NULL) { - httpError("context:%p, fd:%d, ip:%s, user:%s, malloc multiCmds error", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpError("context:%p, fd:%d, user:%s, malloc multiCmds error", pContext, pContext->fd, pContext->user); return false; } memset(pContext->multiCmds, 0, sizeof(HttpSqlCmds)); @@ -161,7 +160,7 @@ bool httpMallocMultiCmds(HttpContext *pContext, int cmdSize, int bufferSize) { free(multiCmds->cmds); multiCmds->cmds = (HttpSqlCmd *)malloc((size_t)cmdSize * sizeof(HttpSqlCmd)); if (multiCmds->cmds == NULL) { - httpError("context:%p, fd:%d, ip:%s, user:%s, malloc cmds:%d error", pContext, pContext->fd, pContext->ipstr, + httpError("context:%p, fd:%d, user:%s, malloc cmds:%d error", pContext, pContext->fd, pContext->user, cmdSize); return false; } @@ -172,8 +171,8 @@ bool httpMallocMultiCmds(HttpContext *pContext, int cmdSize, int bufferSize) { free(multiCmds->buffer); multiCmds->buffer = (char *)malloc((size_t)bufferSize); if (multiCmds->buffer == NULL) { - httpError("context:%p, fd:%d, ip:%s, user:%s, malloc buffer:%d error", pContext, pContext->fd, pContext->ipstr, - pContext->user, bufferSize); + httpError("context:%p, fd:%d, user:%s, malloc buffer:%d error", pContext, pContext->fd, pContext->user, + bufferSize); return false; } multiCmds->bufferSize = bufferSize; @@ -191,15 +190,14 @@ bool httpReMallocMultiCmdsSize(HttpContext *pContext, int cmdSize) { HttpSqlCmds *multiCmds = pContext->multiCmds; if (cmdSize > HTTP_MAX_CMD_SIZE) { - httpError("context:%p, fd:%d, ip:%s, user:%s, mulitcmd size:%d large then %d", pContext, pContext->fd, - pContext->ipstr, pContext->user, 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); return false; } multiCmds->cmds = (HttpSqlCmd *)realloc(multiCmds->cmds, (size_t)cmdSize * sizeof(HttpSqlCmd)); if (multiCmds->cmds == NULL) { - httpError("context:%p, fd:%d, ip:%s, user:%s, malloc cmds:%d error", pContext, pContext->fd, pContext->ipstr, - pContext->user, cmdSize); + httpError("context:%p, fd:%d, user:%s, malloc cmds:%d error", pContext, pContext->fd, pContext->user, cmdSize); return false; } memset(multiCmds->cmds + multiCmds->maxSize, 0, (size_t)(cmdSize - multiCmds->maxSize) * sizeof(HttpSqlCmd)); @@ -212,15 +210,14 @@ bool httpReMallocMultiCmdsBuffer(HttpContext *pContext, int bufferSize) { HttpSqlCmds *multiCmds = pContext->multiCmds; if (bufferSize > HTTP_MAX_BUFFER_SIZE) { - httpError("context:%p, fd:%d, ip:%s, user:%s, mulitcmd buffer size:%d large then %d", - pContext, pContext->fd, pContext->ipstr, pContext->user, bufferSize, HTTP_MAX_BUFFER_SIZE); + httpError("context:%p, fd:%d, user:%s, mulitcmd buffer size:%d large then %d", pContext, pContext->fd, + pContext->user, bufferSize, HTTP_MAX_BUFFER_SIZE); return false; } multiCmds->buffer = (char *)realloc(multiCmds->buffer, (size_t)bufferSize); if (multiCmds->buffer == NULL) { - httpError("context:%p, fd:%d, ip:%s, user:%s, malloc buffer:%d error", pContext, pContext->fd, pContext->ipstr, - pContext->user, bufferSize); + httpError("context:%p, fd:%d, user:%s, malloc buffer:%d error", pContext, pContext->fd, pContext->user, bufferSize); return false; } memset(multiCmds->buffer + multiCmds->bufferSize, 0, (size_t)(bufferSize - multiCmds->bufferSize)); diff --git a/src/plugins/http/src/restHandle.c b/src/plugins/http/src/restHandle.c index f0841e2f99..8f998420de 100644 --- a/src/plugins/http/src/restHandle.c +++ b/src/plugins/http/src/restHandle.c @@ -80,15 +80,13 @@ bool restGetPassFromUrl(HttpContext* pContext) { } bool restProcessLoginRequest(HttpContext* pContext) { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, process restful login msg", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpDebug("context:%p, fd:%d, user:%s, process restful login msg", pContext, pContext->fd, pContext->user); pContext->reqType = HTTP_REQTYPE_LOGIN; return true; } bool restProcessSqlRequest(HttpContext* pContext, int timestampFmt) { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, process restful sql msg", pContext, pContext->fd, pContext->ipstr, - pContext->user); + httpDebug("context:%p, fd:%d, user:%s, process restful sql msg", pContext, pContext->fd, pContext->user); char* sql = pContext->parser.data.pos; if (sql == NULL) { diff --git a/src/plugins/http/src/restJson.c b/src/plugins/http/src/restJson.c index 7a73f6559f..4a7836ad7a 100644 --- a/src/plugins/http/src/restJson.c +++ b/src/plugins/http/src/restJson.c @@ -155,19 +155,17 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, } if (cmd->numOfRows >= tsRestRowLimit) { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, retrieve rows:%d larger than limit:%d, abort retrieve", pContext, - pContext->fd, pContext->ipstr, pContext->user, cmd->numOfRows, tsRestRowLimit); + httpDebug("context:%p, fd:%d, user:%s, retrieve rows:%d larger than limit:%d, abort retrieve", pContext, + pContext->fd, pContext->user, cmd->numOfRows, tsRestRowLimit); return false; - } - else { + } else { if (pContext->fd <= 0) { - httpError("context:%p, fd:%d, ip:%s, user:%s, connection is closed, abort retrieve", pContext, pContext->fd, - pContext->ipstr, pContext->user); + httpError("context:%p, fd:%d, user:%s, connection is closed, abort retrieve", pContext, pContext->fd, + pContext->user); return false; - } - else { - httpDebug("context:%p, fd:%d, ip:%s, user:%s, total rows:%d retrieved", pContext, pContext->fd, pContext->ipstr, - pContext->user, cmd->numOfRows); + } else { + httpDebug("context:%p, fd:%d, user:%s, total rows:%d retrieved", pContext, pContext->fd, pContext->user, + cmd->numOfRows); return true; } } diff --git a/src/plugins/http/src/tgHandle.c b/src/plugins/http/src/tgHandle.c index 48c66c4c07..cae46f222a 100644 --- a/src/plugins/http/src/tgHandle.c +++ b/src/plugins/http/src/tgHandle.c @@ -800,7 +800,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) { } */ bool tgProcessQueryRequest(HttpContext *pContext, char *db) { - httpDebug("context:%p, fd:%d, ip:%s, process telegraf query msg", pContext, pContext->fd, pContext->ipstr); + httpDebug("context:%p, fd:%d, process telegraf query msg", pContext, pContext->fd); HttpParser *pParser = &pContext->parser; char * filter = pParser->data.pos; @@ -818,7 +818,7 @@ bool tgProcessQueryRequest(HttpContext *pContext, char *db) { cJSON *metrics = cJSON_GetObjectItem(root, "metrics"); if (metrics != NULL) { int size = cJSON_GetArraySize(metrics); - httpDebug("context:%p, fd:%d, ip:%s, multiple metrics:%d at one time", pContext, pContext->fd, pContext->ipstr, + httpDebug("context:%p, fd:%d, multiple metrics:%d at one time", pContext, pContext->fd, size); if (size <= 0) { httpSendErrorResp(pContext, HTTP_TG_METRICS_NULL); @@ -859,7 +859,7 @@ bool tgProcessQueryRequest(HttpContext *pContext, char *db) { } } } else { - httpDebug("context:%p, fd:%d, ip:%s, single metric", pContext, pContext->fd, pContext->ipstr); + httpDebug("context:%p, fd:%d, single metric", pContext, pContext->fd); if (!httpMallocMultiCmds(pContext, 3, HTTP_BUFFER_SIZE)) { httpSendErrorResp(pContext, HTTP_NO_ENOUGH_MEMORY); diff --git a/src/plugins/http/src/tgJson.c b/src/plugins/http/src/tgJson.c index ed4ee0d7de..170a1b343e 100644 --- a/src/plugins/http/src/tgJson.c +++ b/src/plugins/http/src/tgJson.c @@ -98,8 +98,8 @@ void tgBuildSqlAffectRowsJson(HttpContext *pContext, HttpSqlCmd *cmd, int affect bool tgCheckFinished(struct HttpContext *pContext, HttpSqlCmd *cmd, int code) { HttpSqlCmds *multiCmds = pContext->multiCmds; - httpDebug("context:%p, fd:%d, ip:%s, check telegraf command, code:%s, state:%d, type:%d, rettype:%d, tags:%d", - pContext, pContext->fd, pContext->ipstr, tstrerror(code), cmd->cmdState, cmd->cmdType, cmd->cmdReturnType, cmd->tagNum); + 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); if (cmd->cmdType == HTTP_CMD_TYPE_INSERT) { if (cmd->cmdState == HTTP_CMD_STATE_NOT_RUN_YET) { @@ -107,16 +107,14 @@ bool tgCheckFinished(struct HttpContext *pContext, HttpSqlCmd *cmd, int code) { cmd->cmdState = HTTP_CMD_STATE_RUN_FINISHED; if (multiCmds->cmds[0].cmdState == HTTP_CMD_STATE_NOT_RUN_YET) { multiCmds->pos = (int16_t)-1; - httpDebug("context:%p, fd:%d, ip:%s, import failed, try create database", pContext, pContext->fd, - pContext->ipstr); + httpDebug("context:%p, fd:%d, import failed, try create database", pContext, pContext->fd); return false; } } else if (code == TSDB_CODE_MND_INVALID_TABLE_NAME) { cmd->cmdState = HTTP_CMD_STATE_RUN_FINISHED; if (multiCmds->cmds[multiCmds->pos - 1].cmdState == HTTP_CMD_STATE_NOT_RUN_YET) { multiCmds->pos = (int16_t)(multiCmds->pos - 2); - httpDebug("context:%p, fd:%d, ip:%s, import failed, try create stable", pContext, pContext->fd, - pContext->ipstr); + httpDebug("context:%p, fd:%d, import failed, try create stable", pContext, pContext->fd); return false; } } else { @@ -125,11 +123,10 @@ bool tgCheckFinished(struct HttpContext *pContext, HttpSqlCmd *cmd, int code) { } } else if (cmd->cmdType == HTTP_CMD_TYPE_CREATE_DB) { cmd->cmdState = HTTP_CMD_STATE_RUN_FINISHED; - httpDebug("context:%p, fd:%d, ip:%s, code:%s, create database failed", pContext, pContext->fd, pContext->ipstr, - tstrerror(code)); + httpDebug("context:%p, fd:%d, code:%s, create database failed", pContext, pContext->fd, tstrerror(code)); } else if (cmd->cmdType == HTTP_CMD_TYPE_CREATE_STBALE) { cmd->cmdState = HTTP_CMD_STATE_RUN_FINISHED; - httpDebug("context:%p, fd:%d, ip:%s, code:%s, create stable failed", pContext, pContext->fd, pContext->ipstr, tstrerror(code)); + httpDebug("context:%p, fd:%d, code:%s, create stable failed", pContext, pContext->fd, tstrerror(code)); } else { } @@ -138,9 +135,9 @@ bool tgCheckFinished(struct HttpContext *pContext, HttpSqlCmd *cmd, int code) { void tgSetNextCmd(struct HttpContext *pContext, HttpSqlCmd *cmd, int code) { HttpSqlCmds *multiCmds = pContext->multiCmds; - httpDebug("context:%p, fd:%d, ip:%s, get telegraf next command, pos:%d, code:%s, state:%d, type:%d, rettype:%d, tags:%d", - pContext, pContext->fd, pContext->ipstr, multiCmds->pos, tstrerror(code), cmd->cmdState, cmd->cmdType, - cmd->cmdReturnType, cmd->tagNum); + 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, + cmd->tagNum); if (cmd->cmdType == HTTP_CMD_TYPE_INSERT) { multiCmds->pos = (int16_t)(multiCmds->pos + 2); From a1527b6206b6bb4abf01abf66d899db86d78fa76 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sat, 12 Sep 2020 08:13:12 +0000 Subject: [PATCH 09/30] TD-1311 --- src/plugins/http/src/tgHandle.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/plugins/http/src/tgHandle.c b/src/plugins/http/src/tgHandle.c index cae46f222a..84d7d13fde 100644 --- a/src/plugins/http/src/tgHandle.c +++ b/src/plugins/http/src/tgHandle.c @@ -818,8 +818,7 @@ bool tgProcessQueryRequest(HttpContext *pContext, char *db) { cJSON *metrics = cJSON_GetObjectItem(root, "metrics"); if (metrics != NULL) { int size = cJSON_GetArraySize(metrics); - httpDebug("context:%p, fd:%d, multiple metrics:%d at one time", pContext, pContext->fd, - size); + httpDebug("context:%p, fd:%d, multiple metrics:%d at one time", pContext, pContext->fd, size); if (size <= 0) { httpSendErrorResp(pContext, HTTP_TG_METRICS_NULL); cJSON_Delete(root); From 11aed4209e3ee9fcb681f84284544c8c68b196c7 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sat, 12 Sep 2020 14:50:29 +0000 Subject: [PATCH 10/30] TD-1311 --- src/plugins/http/inc/ehttp_parser.h | 97 +++- src/plugins/http/inc/ehttp_util_string.h | 25 +- src/plugins/http/inc/httpInt.h | 7 +- src/plugins/http/src/ehttp_parser.c | 555 +++++++++++------------ src/plugins/http/src/ehttp_util_string.c | 13 +- src/plugins/http/src/httpContext.c | 10 +- src/plugins/http/src/httpServer.c | 7 +- 7 files changed, 357 insertions(+), 357 deletions(-) diff --git a/src/plugins/http/inc/ehttp_parser.h b/src/plugins/http/inc/ehttp_parser.h index be87650128..7ddfcb32db 100644 --- a/src/plugins/http/inc/ehttp_parser.h +++ b/src/plugins/http/inc/ehttp_parser.h @@ -1,36 +1,93 @@ -#ifndef _ehttp_parser_fc7f9ac9_52da_4ee3_b556_deb2e1c3866e -#define _ehttp_parser_fc7f9ac9_52da_4ee3_b556_deb2e1c3866e +#ifndef HTTP_PARSER_H +#define HTTP_PARSER_H -#include +#include "ehttp_util_string.h" +#include "ehttp_gzip.h" -typedef struct ehttp_parser_s ehttp_parser_t; -typedef struct ehttp_parser_callbacks_s ehttp_parser_callbacks_t; -typedef struct ehttp_parser_conf_s ehttp_parser_conf_t; -typedef struct ehttp_status_code_s ehttp_status_code_t; +struct HttpContext; -struct ehttp_parser_callbacks_s { +typedef enum HTTP_PARSER_STATE { + HTTP_PARSER_BEGIN, + HTTP_PARSER_REQUEST_OR_RESPONSE, + HTTP_PARSER_METHOD, + HTTP_PARSER_TARGET, + HTTP_PARSER_HTTP_VERSION, + HTTP_PARSER_SP, + HTTP_PARSER_STATUS_CODE, + HTTP_PARSER_REASON_PHRASE, + HTTP_PARSER_CRLF, + HTTP_PARSER_HEADER, + HTTP_PARSER_HEADER_KEY, + HTTP_PARSER_HEADER_VAL, + HTTP_PARSER_CHUNK_SIZE, + HTTP_PARSER_CHUNK, + HTTP_PARSER_END, + HTTP_PARSER_ERROR, +} HTTP_PARSER_STATE; + +typedef struct HttpParserStatusObj { + int32_t status_code; + const char *status_desc; +} HttpParserStatusObj; + +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; -struct ehttp_parser_conf_s { - size_t flush_block_size; // <=0: immediately -}; +typedef struct HttpParserConfObj { + size_t flush_block_size; // <=0: immediately +} HttpParserConfObj; -ehttp_parser_t* ehttp_parser_create(ehttp_parser_callbacks_t callbacks, ehttp_parser_conf_t conf, void *arg); -void ehttp_parser_destroy(ehttp_parser_t *parser); -int ehttp_parser_parse(ehttp_parser_t *parser, const char *buf, size_t len); -int ehttp_parser_parse_string(ehttp_parser_t *parser, const char *str); -int ehttp_parser_parse_char(ehttp_parser_t *parser, const char c); -int ehttp_parser_parse_end(ehttp_parser_t *parser); +typedef struct HttpParseKvObj { + char *key; + char *val; +} HttpParseKvObj; + +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; + HttpUtilString str; + HTTP_PARSER_STATE *stacks; + size_t stacks_count; +} HttpParserObj; + +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); -#endif // _ehttp_parser_fc7f9ac9_52da_4ee3_b556_deb2e1c3866e - +#endif diff --git a/src/plugins/http/inc/ehttp_util_string.h b/src/plugins/http/inc/ehttp_util_string.h index 46c5a42827..eb44805f96 100644 --- a/src/plugins/http/inc/ehttp_util_string.h +++ b/src/plugins/http/inc/ehttp_util_string.h @@ -1,18 +1,13 @@ -#ifndef _ehttp_util_string_h_99dacde5_2e7d_4662_97d6_04611fde683b_ -#define _ehttp_util_string_h_99dacde5_2e7d_4662_97d6_04611fde683b_ +#ifndef HTTP_UTIL_STRING +#define HTTP_UTIL_STRING -#include +typedef struct HttpUtilString { + char * str; + size_t len; +} HttpUtilString; -typedef struct ehttp_util_string_s ehttp_util_string_t; - -struct ehttp_util_string_s { - char *str; - size_t len; -}; - -void ehttp_util_string_cleanup(ehttp_util_string_t *str); -int ehttp_util_string_append(ehttp_util_string_t *str, const char *s, size_t len); -void ehttp_util_string_clear(ehttp_util_string_t *str); - -#endif // _ehttp_util_string_h_99dacde5_2e7d_4662_97d6_04611fde683b_ +void httpParserCleanupString(HttpUtilString *str); +int32_t httpParserAppendString(HttpUtilString *str, const char *s, int32_t len); +void httpParserClearString(HttpUtilString *str); +#endif diff --git a/src/plugins/http/inc/httpInt.h b/src/plugins/http/inc/httpInt.h index 044b5cc4cc..89972adc87 100644 --- a/src/plugins/http/inc/httpInt.h +++ b/src/plugins/http/inc/httpInt.h @@ -179,10 +179,9 @@ typedef struct { HttpBuf data; // body content HttpBuf token; // auth token HttpDecodeMethod *pMethod; - - ehttp_parser_t *parser; - int inited:2; - int failed:4; + HttpParserObj * parser; + int8_t inited; + int8_t failed; } HttpParser; typedef struct HttpContext { diff --git a/src/plugins/http/src/ehttp_parser.c b/src/plugins/http/src/ehttp_parser.c index 30d37f8a0d..0d0382078f 100644 --- a/src/plugins/http/src/ehttp_parser.c +++ b/src/plugins/http/src/ehttp_parser.c @@ -1,19 +1,20 @@ +#include "os.h" +#include "httpLog.h" +#include "httpContext.h" +#include "ehttp_util_string.h" #include "ehttp_parser.h" #include "ehttp_gzip.h" #include "ehttp_util_string.h" #include "elog.h" + #include #include #include -struct ehttp_status_code_s { - int status_code; - const char *status_desc; -}; -static ehttp_status_code_t status_codes[] = { +static HttpParserStatusObj status_codes[] = { {100, "Continue"}, {101, "Switching Protocol"}, {102, "Processing (WebDAV)"}, @@ -81,7 +82,7 @@ static ehttp_status_code_t status_codes[] = { }; const char* ehttp_status_code_get_desc(const int status_code) { - ehttp_status_code_t *p = status_codes; + HttpParserStatusObj *p = status_codes; while (p->status_code!=0) { if (p->status_code==status_code) return p->status_desc; ++p; @@ -89,74 +90,6 @@ const char* ehttp_status_code_get_desc(const int status_code) { return "Unknow status code"; } -typedef enum HTTP_PARSER_STATE { - HTTP_PARSER_BEGIN, - HTTP_PARSER_REQUEST_OR_RESPONSE, - HTTP_PARSER_METHOD, - HTTP_PARSER_TARGET, - HTTP_PARSER_HTTP_VERSION, - HTTP_PARSER_SP, - HTTP_PARSER_STATUS_CODE, - HTTP_PARSER_REASON_PHRASE, - HTTP_PARSER_CRLF, - HTTP_PARSER_HEADER, - HTTP_PARSER_HEADER_KEY, - HTTP_PARSER_HEADER_VAL, - HTTP_PARSER_CHUNK_SIZE, - HTTP_PARSER_CHUNK, - HTTP_PARSER_END, - HTTP_PARSER_ERROR, -} HTTP_PARSER_STATE; - -typedef struct ehttp_parser_kv_s ehttp_parser_kv_t; - -struct ehttp_parser_kv_s { - char *key; - char *val; -}; - -struct ehttp_parser_s { - ehttp_parser_callbacks_t callbacks; - void *arg; - ehttp_parser_conf_t conf; - - 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; - ehttp_parser_kv_t *kvs; - size_t kvs_count; - - char *auth_basic; - - size_t content_length; - - size_t chunk_size; - size_t received_chunk_size; - size_t received_size; - - ehttp_gzip_t *gzip; - ehttp_util_string_t str; - HTTP_PARSER_STATE *stacks; - size_t stacks_count; -}; - static void dummy_on_request_line(void *arg, const char *method, const char *target, const char *version, const char *target_raw) { } @@ -175,15 +108,14 @@ static void dummy_on_end(void *arg) { static void dummy_on_error(void *arg, int status_code) { } +static HTTP_PARSER_STATE httpParserTop(HttpParserObj *parser) { + ASSERT(parser->stacks_count >= 1); + ASSERT(parser->stacks); -static HTTP_PARSER_STATE ehttp_parser_top(ehttp_parser_t *parser) { - EQ_ASSERT(parser->stacks_count >= 1); - EQ_ASSERT(parser->stacks); - - return parser->stacks[parser->stacks_count-1]; + return parser->stacks[parser->stacks_count - 1]; } -static int ehttp_parser_push(ehttp_parser_t *parser, HTTP_PARSER_STATE state) { +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)); @@ -196,15 +128,15 @@ static int ehttp_parser_push(ehttp_parser_t *parser, HTTP_PARSER_STATE state) { return 0; } -static int ehttp_parser_pop(ehttp_parser_t *parser) { +static int httpParserPop(HttpParserObj *parser) { if (parser->stacks_count <= 0) return -1; --parser->stacks_count; return 0; } -ehttp_parser_t *ehttp_parser_create(ehttp_parser_callbacks_t callbacks, ehttp_parser_conf_t conf, void *arg) { - ehttp_parser_t *parser = (ehttp_parser_t*)calloc(1, sizeof(*parser)); +HttpParserObj *httpParserCreate(HttpParserCallbackObj callbacks, HttpParserConfObj conf, void *arg) { + HttpParserObj *parser = (HttpParserObj*)calloc(1, sizeof(*parser)); if (!parser) return NULL; parser->callbacks = callbacks; @@ -230,16 +162,16 @@ ehttp_parser_t *ehttp_parser_create(ehttp_parser_callbacks_t callbacks, ehttp_pa parser->callbacks.on_error = dummy_on_error; } - ehttp_parser_push(parser, HTTP_PARSER_BEGIN); + httpParserPush(parser, HTTP_PARSER_BEGIN); return parser; } -static void ehttp_parser_kvs_destroy(ehttp_parser_t *parser) { +static void ehttp_parser_kvs_destroy(HttpParserObj *parser) { if (!parser->kvs) return; for (size_t i=0; ikvs_count; ++i) { - ehttp_parser_kv_t *p = &parser->kvs[i]; + HttpParseKvObj *p = &parser->kvs[i]; free(p->key); p->key = NULL; free(p->val); p->val = NULL; } @@ -251,7 +183,7 @@ static void ehttp_parser_kvs_destroy(ehttp_parser_t *parser) { parser->auth_basic = NULL; } -void ehttp_parser_destroy(ehttp_parser_t *parser) { +void httpParserDestroy(HttpParserObj *parser) { if (!parser) return; free(parser->method); parser->method = NULL; @@ -268,7 +200,7 @@ void ehttp_parser_destroy(ehttp_parser_t *parser) { ehttp_parser_kvs_destroy(parser); - ehttp_util_string_cleanup(&parser->str); + httpParserCleanupString(&parser->str); if (parser->gzip) { ehttp_gzip_destroy(parser->gzip); parser->gzip = NULL; @@ -281,52 +213,52 @@ void ehttp_parser_destroy(ehttp_parser_t *parser) { char *ehttp_parser_urldecode(const char *enc) { int ok = 1; - ehttp_util_string_t str = {0}; + HttpUtilString str = {0}; while (*enc) { char *p = strchr(enc, '%'); if (!p) break; int hex, cnt; int n = sscanf(p+1, "%2x%n", &hex, &cnt); if (n!=1 && cnt !=2) { ok = 0; break; } - if (ehttp_util_string_append(&str, enc, p-enc)) { ok = 0; break; } + if (httpParserAppendString(&str, enc, p-enc)) { ok = 0; break; } char c = (char)hex; - if (ehttp_util_string_append(&str, &c, 1)) { ok = 0; break; } + if (httpParserAppendString(&str, &c, 1)) { ok = 0; break; } enc = p+3; } char *dec = NULL; if (ok && *enc) { - if (ehttp_util_string_append(&str, enc, strlen(enc))) { ok = 0; } + if (httpParserAppendString(&str, enc, strlen(enc))) { ok = 0; } } if (ok) { dec = str.str; str.str = NULL; } - ehttp_util_string_cleanup(&str); + httpParserCleanupString(&str); return dec; } static void on_data(ehttp_gzip_t *gzip, void *arg, const char *buf, size_t len) { - ehttp_parser_t *parser = (ehttp_parser_t*)arg; + HttpParserObj *parser = (HttpParserObj*)arg; parser->callbacks.on_body(parser->arg, buf, len); } -static int ehttp_parser_check_field(ehttp_parser_t *parser, const char *key, const char *val) { - int ok = 0; +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")) { - size_t len = 0; - int bytes = 0; - int n = sscanf(val, "%ld%n", &len, &bytes); - if (n==1 && bytes==strlen(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->content_length = len; - parser->chunk_size = len; + parser->chunk_size = len; parser->content_length_specified = 1; break; } ok = -1; break; } - if (0==strcasecmp(key, "Accept-Encoding")) { + if (0 == strcasecmp(key, "Accept-Encoding")) { if (strstr(val, "gzip")) { parser->accept_encoding_gzip = 1; } @@ -335,13 +267,13 @@ static int ehttp_parser_check_field(ehttp_parser_t *parser, const char *key, con } break; } - if (0==strcasecmp(key, "Content-Encoding")) { - if (0==strcmp(val, "gzip")) { + if (0 == strcasecmp(key, "Content-Encoding")) { + if (0 == strcmp(val, "gzip")) { parser->content_chunked = 1; } break; } - if (0==strcasecmp(key, "Transfer-Encoding")) { + if (0 == strcasecmp(key, "Transfer-Encoding")) { if (strstr(val, "gzip")) { parser->transfer_gzip = 1; ehttp_gzip_conf_t conf = {0}; @@ -352,7 +284,7 @@ static int ehttp_parser_check_field(ehttp_parser_t *parser, const char *key, con parser->gzip = ehttp_gzip_create_decompressor(conf, callbacks, parser); if (!parser->gzip) { - E("failed to create gzip decompressor"); + httpDebug("failed to create gzip decompressor"); ok = -1; break; } @@ -365,24 +297,37 @@ static int ehttp_parser_check_field(ehttp_parser_t *parser, const char *key, con if (0==strcasecmp(key, "Authorization")) { char *t = NULL; char *s = NULL; - int bytes = 0; - int n = sscanf(val, "%ms %ms%n", &t, &s, &bytes); - if (n==2 && t && s && bytes==strlen(val) && strcmp(t, "Basic")) { - free(parser->auth_basic); - parser->auth_basic = s; 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); + + free(t); + free(s); break; } } while (0); return ok; } -static int ehttp_parser_kvs_append_kv(ehttp_parser_t *parser, const char *key, const char *val) { - // ehttp_parser_kv_t *kvs = (ehttp_parser_kv_t*)reallocarray(parser->kvs, parser->kvs_count + 1, sizeof(*kvs)); - ehttp_parser_kv_t *kvs = (ehttp_parser_kv_t*)realloc(parser->kvs, (parser->kvs_count + 1) * sizeof(*kvs)); +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; @@ -403,69 +348,70 @@ static int ehttp_parser_kvs_append_kv(ehttp_parser_t *parser, const char *key, c return -1; } -static int on_begin(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { - int ok = 0; +static int32_t httpParserOnBegin(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { + int32_t ok = 0; do { - if (c=='G' || c=='P' || c=='H' || c=='D' || c=='C' || c=='O' || c=='T') { - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (c == 'G' || c == 'P' || c == 'H' || c == 'D' || c == 'C' || c == 'O' || c == 'T') { + if (httpParserAppendString(&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); break; } - ehttp_parser_pop(parser); - ehttp_parser_push(parser, HTTP_PARSER_REQUEST_OR_RESPONSE); + httpParserPop(parser); + httpParserPush(parser, HTTP_PARSER_REQUEST_OR_RESPONSE); break; } - E("parser state: %d, unexpected char: [%c]%02x", state, c, 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); } while (0); return ok; } -static int on_request_or_response(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { - int ok = 0; +static int32_t httpParserOnRquestOrResponse(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { + int32_t ok = 0; do { - if (parser->str.len==1) { - if (c=='T' && parser->str.str[0]=='H') { - ehttp_parser_pop(parser); - ehttp_parser_push(parser, HTTP_PARSER_END); - ehttp_parser_push(parser, HTTP_PARSER_HEADER); - ehttp_parser_push(parser, HTTP_PARSER_CRLF); - ehttp_parser_push(parser, HTTP_PARSER_REASON_PHRASE); - ehttp_parser_push(parser, HTTP_PARSER_SP); - ehttp_parser_push(parser, HTTP_PARSER_STATUS_CODE); - ehttp_parser_push(parser, HTTP_PARSER_SP); - ehttp_parser_push(parser, HTTP_PARSER_HTTP_VERSION); + if (parser->str.len == 1) { + if (c == 'T' && parser->str.str[0] == 'H') { + httpParserPop(parser); + httpParserPush(parser, HTTP_PARSER_END); + httpParserPush(parser, HTTP_PARSER_HEADER); + httpParserPush(parser, HTTP_PARSER_CRLF); + httpParserPush(parser, HTTP_PARSER_REASON_PHRASE); + httpParserPush(parser, HTTP_PARSER_SP); + httpParserPush(parser, HTTP_PARSER_STATUS_CODE); + httpParserPush(parser, HTTP_PARSER_SP); + httpParserPush(parser, HTTP_PARSER_HTTP_VERSION); *again = 1; break; } - ehttp_parser_pop(parser); - ehttp_parser_push(parser, HTTP_PARSER_END); - ehttp_parser_push(parser, HTTP_PARSER_HEADER); - ehttp_parser_push(parser, HTTP_PARSER_CRLF); - ehttp_parser_push(parser, HTTP_PARSER_HTTP_VERSION); - ehttp_parser_push(parser, HTTP_PARSER_SP); - ehttp_parser_push(parser, HTTP_PARSER_TARGET); - ehttp_parser_push(parser, HTTP_PARSER_SP); - ehttp_parser_push(parser, HTTP_PARSER_METHOD); + httpParserPop(parser); + httpParserPush(parser, HTTP_PARSER_END); + httpParserPush(parser, HTTP_PARSER_HEADER); + httpParserPush(parser, HTTP_PARSER_CRLF); + httpParserPush(parser, HTTP_PARSER_HTTP_VERSION); + httpParserPush(parser, HTTP_PARSER_SP); + httpParserPush(parser, HTTP_PARSER_TARGET); + httpParserPush(parser, HTTP_PARSER_SP); + httpParserPush(parser, HTTP_PARSER_METHOD); *again = 1; break; } - E("parser state: %d, unexpected char: [%c]%02x", state, c, 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); } while (0); return ok; } -static int on_method(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { - int ok = 0; +static int32_t httpParserOnMethod(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { + int32_t ok = 0; do { if (isalnum(c) || strchr("!#$%&'*+-.^_`|~", c)) { - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (httpParserAppendString(&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); break; @@ -474,24 +420,24 @@ static int on_method(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char } parser->method = strdup(parser->str.str); if (!parser->method) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + 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); break; } - ehttp_util_string_clear(&parser->str); - ehttp_parser_pop(parser); + httpParserClearString(&parser->str); + httpParserPop(parser); *again = 1; } while (0); return ok; } -static int on_target(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { - int ok = 0; +static int32_t httpParserOnTarget(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { + int32_t ok = 0; do { - if (!isspace(c) && c!='\r' && c!='\n') { - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (!isspace(c) && c != '\r' && c != '\n') { + if (httpParserAppendString(&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); break; @@ -499,34 +445,34 @@ static int on_target(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char break; } parser->target_raw = strdup(parser->str.str); - parser->target = ehttp_parser_urldecode(parser->str.str); + parser->target = ehttp_parser_urldecode(parser->str.str); if (!parser->target_raw || !parser->target) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + 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); break; } - ehttp_util_string_clear(&parser->str); - ehttp_parser_pop(parser); + httpParserClearString(&parser->str); + httpParserPop(parser); *again = 1; } while (0); return ok; } -static int on_version(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { - int ok = 0; +static int32_t httpParserOnVersion(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { + int32_t ok = 0; do { const char *prefix = "HTTP/1."; - int len = strlen(prefix); + int32_t len = strlen(prefix); if (parser->str.len < len) { if (prefix[parser->str.len]!=c) { - E("parser state: %d, unexpected char: [%c]%02x", state, c, 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); break; } - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (httpParserAppendString(&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); break; @@ -535,13 +481,13 @@ static int on_version(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const cha } if (c!='0' && c!='1') { - E("parser state: %d, unexpected char: [%c]%02x", state, c, 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); break; } - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (httpParserAppendString(&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); break; @@ -551,7 +497,7 @@ static int on_version(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const cha parser->version = strdup(parser->str.str); if (!parser->version) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + 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); break; @@ -561,32 +507,32 @@ static int on_version(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const cha parser->callbacks.on_request_line(parser->arg, parser->method, parser->target, parser->version, parser->target_raw); } - ehttp_util_string_clear(&parser->str); - ehttp_parser_pop(parser); + httpParserClearString(&parser->str); + httpParserPop(parser); } while (0); return ok; } -static int on_sp(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { - int ok = 0; +static int32_t httpParserOnSp(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int *again) { + int32_t ok = 0; do { - if (c==' ') { - ehttp_parser_pop(parser); + if (c == ' ') { + httpParserPop(parser); break; } - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + 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); } while (0); return ok; } -static int on_status_code(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { +static int32_t httpParserOnStatusCode(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int *again) { int ok = 0; do { if (isdigit(c)) { - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (httpParserAppendString(&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); break; @@ -594,36 +540,36 @@ static int on_status_code(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const if (parser->str.len < 3) break; sscanf(parser->str.str, "%d", &parser->status_code); - ehttp_util_string_clear(&parser->str); - ehttp_parser_pop(parser); + httpParserClearString(&parser->str); + httpParserPop(parser); break; } - E("parser state: %d, unexpected char: [%c]%02x", state, c, 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); } while (0); return ok; } -static int on_reason_phrase(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { +static int32_t httpParserOnReasonPhrase(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int *again) { int ok = 0; do { if (c=='\r') { parser->reason_phrase = strdup(parser->str.str); if (!parser->reason_phrase) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + 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); break; } parser->callbacks.on_status_line(parser->arg, parser->version, parser->status_code, parser->reason_phrase); - ehttp_util_string_clear(&parser->str); - ehttp_parser_pop(parser); + httpParserClearString(&parser->str); + httpParserPop(parser); *again = 1; break; } - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (httpParserAppendString(&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); break; @@ -632,10 +578,10 @@ static int on_reason_phrase(ehttp_parser_t *parser, HTTP_PARSER_STATE state, con return ok; } -static int post_process(ehttp_parser_t *parser) { +static int32_t httpParserPostProcess(HttpContext *pContext, HttpParserObj *parser) { if (parser->gzip) { if (ehttp_gzip_finish(parser->gzip)) { - E("gzip failed"); + httpError("context:%p, fd:%d, gzip failed", pContext, pContext->fd); parser->callbacks.on_error(parser->arg, 507); return -1; } @@ -644,28 +590,28 @@ static int post_process(ehttp_parser_t *parser) { return 0; } -static int on_crlf(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { - int ok = 0; +static int32_t httpParserOnCrlf(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { + int32_t ok = 0; do { const char *s = "\r\n"; - int len = strlen(s); + int32_t len = strlen(s); if (s[parser->str.len]!=c) { - E("parser state: %d, unexpected char: [%c]%02x", state, c, 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); break; } - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (httpParserAppendString(&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); break; } if (parser->str.len == len) { - ehttp_util_string_clear(&parser->str); - ehttp_parser_pop(parser); - if (ehttp_parser_top(parser) == HTTP_PARSER_END) { - ok = post_process(parser); + httpParserClearString(&parser->str); + httpParserPop(parser); + if (httpParserTop(parser) == HTTP_PARSER_END) { + ok = httpParserPostProcess(pContext, parser); } } break; @@ -673,49 +619,49 @@ static int on_crlf(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c return ok; } -static int on_header(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { - int ok = 0; +static int32_t httpParserOnHeader(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { + int32_t ok = 0; do { if (c=='\r') { - ehttp_parser_pop(parser); + httpParserPop(parser); if (parser->transfer_chunked) { - ehttp_parser_push(parser, HTTP_PARSER_CHUNK_SIZE); - ehttp_parser_push(parser, HTTP_PARSER_CRLF); + httpParserPush(parser, HTTP_PARSER_CHUNK_SIZE); + httpParserPush(parser, HTTP_PARSER_CRLF); } else { if (parser->content_length > 0) { - ehttp_parser_push(parser, HTTP_PARSER_CHUNK); + httpParserPush(parser, HTTP_PARSER_CHUNK); } - ehttp_parser_push(parser, HTTP_PARSER_CRLF); + httpParserPush(parser, HTTP_PARSER_CRLF); } *again = 1; break; } if (c!=' ' && c!='\t' && c!=':' ) { - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (httpParserAppendString(&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); break; } - ehttp_parser_push(parser, HTTP_PARSER_CRLF); - ehttp_parser_push(parser, HTTP_PARSER_HEADER_VAL); - ehttp_parser_push(parser, HTTP_PARSER_SP); - ehttp_parser_push(parser, HTTP_PARSER_HEADER_KEY); + httpParserPush(parser, HTTP_PARSER_CRLF); + httpParserPush(parser, HTTP_PARSER_HEADER_VAL); + httpParserPush(parser, HTTP_PARSER_SP); + httpParserPush(parser, HTTP_PARSER_HEADER_KEY); break; } - E("parser state: %d, unexpected char: [%c]%02x", state, c, 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); } while (0); return ok; } -static int on_header_key(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { +static int httpParserOnHeaderKey(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int *again) { int ok = 0; do { if (isalnum(c) || strchr("!#$%&'*+-.^_`|~", c)) { - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (httpParserAppendString(&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); break; @@ -725,61 +671,62 @@ static int on_header_key(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const if (c==':') { parser->key = strdup(parser->str.str); if (!parser->key) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + 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); break; } - ehttp_util_string_clear(&parser->str); - ehttp_parser_pop(parser); + httpParserClearString(&parser->str); + httpParserPop(parser); break; } - E("parser state: %d, unexpected char: [%c]%02x", state, c, 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); } while (0); return ok; } -static int on_header_val(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { - int ok = 0; +static int32_t httpParserOnHeaderVal(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { + int32_t ok = 0; do { if (c != '\r' && c != '\n' && (!isspace(c) || parser->str.len>0)) { - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (httpParserAppendString(&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); break; } break; } - const char *val = parser->str.str; - ok = ehttp_parser_check_field(parser, parser->key, val); - if (ehttp_parser_kvs_append_kv(parser, parser->key, val)) { + 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); } - free(parser->key); parser->key = NULL; + free(parser->key); + parser->key = NULL; val = NULL; - if (ok==-1) break; - ehttp_util_string_clear(&parser->str); - ehttp_parser_pop(parser); + if (ok == -1) break; + httpParserClearString(&parser->str); + httpParserPop(parser); *again = 1; } while (0); return ok; } -static int on_chunk_size(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { - int ok = 0; - int bytes; - size_t len; +static int32_t httpParserOnChunkSize(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { + int32_t ok = 0; + int32_t bytes; + int32_t len; int n; do { if (isxdigit(c)) { - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (httpParserAppendString(&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); break; @@ -787,44 +734,44 @@ static int on_chunk_size(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const break; } if (c=='\r') { - n = sscanf(parser->str.str, "%lx%n", &len, &bytes); + 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_length) { - ehttp_util_string_clear(&parser->str); - ehttp_parser_pop(parser); - ehttp_parser_push(parser, HTTP_PARSER_CRLF); - ehttp_parser_push(parser, HTTP_PARSER_CRLF); + httpParserClearString(&parser->str); + httpParserPop(parser); + httpParserPush(parser, HTTP_PARSER_CRLF); + httpParserPush(parser, HTTP_PARSER_CRLF); *again = 1; break; } } else { if (parser->content_length_specified == 0 || parser->received_size + len <= parser->content_length) { parser->chunk_size = len; - ehttp_util_string_clear(&parser->str); - ehttp_parser_pop(parser); - ehttp_parser_push(parser, HTTP_PARSER_CHUNK_SIZE); - ehttp_parser_push(parser, HTTP_PARSER_CRLF); - ehttp_parser_push(parser, HTTP_PARSER_CHUNK); - ehttp_parser_push(parser, HTTP_PARSER_CRLF); + httpParserClearString(&parser->str); + httpParserPop(parser); + httpParserPush(parser, HTTP_PARSER_CHUNK_SIZE); + httpParserPush(parser, HTTP_PARSER_CRLF); + httpParserPush(parser, HTTP_PARSER_CHUNK); + httpParserPush(parser, HTTP_PARSER_CRLF); *again = 1; break; } } } } - E("parser state: %d, unexpected char: [%c]%02x", state, c, 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); } while (0); return ok; } -static int on_chunk(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { +static int httpParserOnChunk(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int *again) { int ok = 0; do { - if (ehttp_util_string_append(&parser->str, &c, 1)) { - E("parser state: %d, char: [%c]%02x, oom", state, c, c); + if (httpParserAppendString(&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); break; @@ -835,7 +782,7 @@ static int on_chunk(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char if (parser->gzip) { if (ehttp_gzip_write(parser->gzip, parser->str.str, parser->str.len)) { - E("gzip failed"); + httpError("context:%p, fd:%d, gzip failed", pContext, pContext->fd); ok = -1; parser->callbacks.on_error(parser->arg, 507); break; @@ -844,124 +791,128 @@ static int on_chunk(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char parser->callbacks.on_body(parser->arg, parser->str.str, parser->str.len); } parser->received_chunk_size = 0; - ehttp_util_string_clear(&parser->str); - ehttp_parser_pop(parser); - if (ehttp_parser_top(parser) == HTTP_PARSER_END) { - ok = post_process(parser); + httpParserClearString(&parser->str); + httpParserPop(parser); + if (httpParserTop(parser) == HTTP_PARSER_END) { + ok = httpParserPostProcess(pContext, parser); } } while (0); return ok; } -static int on_end(ehttp_parser_t *parser, HTTP_PARSER_STATE state, const char c, int *again) { - int ok = 0; +static int32_t httpParserOnEnd(HttpContext *pContext, HttpParserObj *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { + int32_t ok = 0; do { - E("parser state: %d, unexpected char: [%c]%02x", state, c, 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, 507); } while (0); return ok; } -static int parse_char(ehttp_parser_t *parser, const char c, int *again) { - int ok = 0; - HTTP_PARSER_STATE state = ehttp_parser_top(parser); +static int32_t httpParseChar(HttpContext *pContext, HttpParserObj *parser, const char c, int32_t *again) { + int32_t ok = 0; + HTTP_PARSER_STATE state = httpParserTop(parser); do { if (state == HTTP_PARSER_BEGIN) { - ok = on_begin(parser, state, c, again); + ok = httpParserOnBegin(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_REQUEST_OR_RESPONSE) { - ok = on_request_or_response(parser, state, c, again); + ok = httpParserOnRquestOrResponse(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_METHOD) { - ok = on_method(parser, state, c, again); + ok = httpParserOnMethod(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_TARGET) { - ok = on_target(parser, state, c, again); + ok = httpParserOnTarget(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_HTTP_VERSION) { - ok = on_version(parser, state, c, again); + ok = httpParserOnVersion(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_SP) { - ok = on_sp(parser, state, c, again); + ok = httpParserOnSp(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_STATUS_CODE) { - ok = on_status_code(parser, state, c, again); + ok = httpParserOnStatusCode(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_REASON_PHRASE) { - ok = on_reason_phrase(parser, state, c, again); + ok = httpParserOnReasonPhrase(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_CRLF) { - ok = on_crlf(parser, state, c, again); + ok = httpParserOnCrlf(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_HEADER) { - ok = on_header(parser, state, c, again); + ok = httpParserOnHeader(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_HEADER_KEY) { - ok = on_header_key(parser, state, c, again); + ok = httpParserOnHeaderKey(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_HEADER_VAL) { - ok = on_header_val(parser, state, c, again); + ok = httpParserOnHeaderVal(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_CHUNK_SIZE) { - ok = on_chunk_size(parser, state, c, again); + ok = httpParserOnChunkSize(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_CHUNK) { - ok = on_chunk(parser, state, c, again); + ok = httpParserOnChunk(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_END) { - ok = on_end(parser, state, c, again); + ok = httpParserOnEnd(pContext, parser, state, c, again); break; } if (state == HTTP_PARSER_ERROR) { ok = -2; break; } - E("unknown parser state: %d", state); + + httpError("context:%p, fd:%d, unknown parse state:%d", pContext, pContext->fd, state); ok = -1; parser->callbacks.on_error(parser->arg, 500); } while (0); - if (ok==-1) { - ehttp_parser_push(parser, HTTP_PARSER_ERROR); + + if (ok == -1) { + httpError("context:%p, fd:%d, failed to parse, state:%d ok:%d", pContext, pContext->fd, state, ok); + httpParserPush(parser, HTTP_PARSER_ERROR); } - if (ok==-2) ok = -1; + + if (ok == -2) { + httpError("context:%p, fd:%d, failed to parse, state:%d ok:%d", pContext, pContext->fd, state, ok); + ok = -1; + } + return ok; } -int ehttp_parser_parse_string(ehttp_parser_t *parser, const char *str) { - return ehttp_parser_parse(parser, str, str?strlen(str):0); -} - -int ehttp_parser_parse_char(ehttp_parser_t *parser, const char c) { - return ehttp_parser_parse(parser, &c, 1); -} - -int ehttp_parser_parse(ehttp_parser_t *parser, const char *buf, size_t len) { +int32_t httpParserBuf(HttpContext *pContext, HttpParserObj *parser, const char *buf, int32_t len) { const char *p = buf; - int ret = 0; - size_t i = 0; + int32_t ret = 0; + int32_t i = 0; + while (i < len) { - int again = 0; - ret = parse_char(parser, *p, &again); - if (ret) break; + int32_t again = 0; + ret = httpParseChar(pContext, 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; + } if (again) continue; ++p; ++i; } + return ret; } - diff --git a/src/plugins/http/src/ehttp_util_string.c b/src/plugins/http/src/ehttp_util_string.c index 94ebaaafa6..8900aa89bb 100644 --- a/src/plugins/http/src/ehttp_util_string.c +++ b/src/plugins/http/src/ehttp_util_string.c @@ -1,17 +1,14 @@ +#include "os.h" #include "ehttp_util_string.h" -#include -#include - -void ehttp_util_string_cleanup(ehttp_util_string_t *str) { +void httpParserCleanupString(HttpUtilString *str) { free(str->str); str->str = NULL; str->len = 0; } -int ehttp_util_string_append(ehttp_util_string_t *str, const char *s, size_t len) { - // int n = str->str?strlen(str->str):0; - int n = str->len; +int32_t httpParserAppendString(HttpUtilString *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); @@ -21,7 +18,7 @@ int ehttp_util_string_append(ehttp_util_string_t *str, const char *s, size_t len return 0; } -void ehttp_util_string_clear(ehttp_util_string_t *str) { +void httpParserClearString(HttpUtilString *str) { if (str->str) { str->str[0] = '\0'; str->len = 0; diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c index 59c81f5960..987de519c7 100644 --- a/src/plugins/http/src/httpContext.c +++ b/src/plugins/http/src/httpContext.c @@ -26,11 +26,9 @@ #include "httpResp.h" #include "httpSql.h" #include "httpSession.h" - #include "httpContext.h" #include "elog.h" -// dirty tweak extern bool httpGetHttpMethod(HttpContext* pContext); extern bool httpParseURL(HttpContext* pContext); extern bool httpParseHttpVersion(HttpContext* pContext); @@ -74,7 +72,7 @@ static void httpDestroyContext(void *data) { httpFreeMultiCmds(pContext); if (pContext->parser.parser) { - ehttp_parser_destroy(pContext->parser.parser); + httpParserDestroy(pContext->parser.parser); pContext->parser.parser = NULL; } @@ -195,7 +193,7 @@ bool httpInitContext(HttpContext *pContext) { memset(pParser, 0, sizeof(HttpParser)); pParser->pCur = pParser->pLast = pParser->buffer; - ehttp_parser_callbacks_t callbacks = { + HttpParserCallbackObj callbacks = { httpParseOnRequestLine, httpParseOnStatusLine, httpParseOnHeaderField, @@ -203,10 +201,10 @@ bool httpInitContext(HttpContext *pContext) { httpParseOnEnd, httpParseOnError }; - ehttp_parser_conf_t conf = { + HttpParserConfObj conf = { .flush_block_size = 0 }; - pParser->parser = ehttp_parser_create(callbacks, conf, pContext); + pParser->parser = httpParserCreate(callbacks, conf, pContext); pParser->inited = 1; httpDebug("context:%p, fd:%d, parsed:%d", pContext, pContext->fd, pContext->parsed); diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index 614cc92700..b42e2cb0aa 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -352,8 +352,11 @@ static bool httpReadData(HttpContext *pContext) { int nread = (int)taosReadSocket(pContext->fd, buf, sizeof(buf)); if (nread > 0) { buf[nread] = '\0'; - if (ehttp_parser_parse(pParser->parser, buf, nread)) { - httpError("context:%p, fd:%d, init parse failed, close connect", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, nread:%d content:%s", pContext, pContext->fd, nread, buf); + int ok = httpParserBuf(pContext, pParser->parser, buf, nread); + + if (ok) { + httpError("context:%p, fd:%d, init parse failed, reason:%d close connect", pContext, pContext->fd, ok); httpNotifyContextClose(pContext); return false; } From 01c69a637754d9724cc29719b77b79195ec66916 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sat, 12 Sep 2020 15:02:21 +0000 Subject: [PATCH 11/30] TD-1225 rename files --- src/plugins/http/inc/ehttp_util_string.h | 13 --- .../http/inc/{gcHandle.h => httpGcHandle.h} | 0 .../http/inc/{gcJson.h => httpGcJson.h} | 0 .../http/inc/{ehttp_gzip.h => httpGzip.h} | 0 src/plugins/http/inc/httpInt.h | 3 +- .../http/inc/{ehttp_parser.h => httpParser.h} | 15 ++- .../inc/{restHandle.h => httpRestHandle.h} | 0 .../http/inc/{restJson.h => httpRestJson.h} | 0 .../http/inc/{tgHandle.h => httpTgHandle.h} | 0 .../http/inc/{tgJson.h => httpTgJson.h} | 0 src/plugins/http/src/ehttp_util_string.c | 27 ------ src/plugins/http/src/httpContext.c | 5 +- .../http/src/{gcHandle.c => httpGcHandle.c} | 6 +- .../http/src/{gcJson.c => httpGcJson.c} | 4 +- .../http/src/{ehttp_gzip.c => httpGzip.c} | 5 +- .../http/src/{ehttp_parser.c => httpParser.c} | 40 +++++--- .../src/{restHandle.c => httpRestHandle.c} | 4 +- .../http/src/{restJson.c => httpRestJson.c} | 4 +- src/plugins/http/src/httpServer.c | 4 +- src/plugins/http/src/httpSystem.c | 6 +- .../http/src/{tgHandle.c => httpTgHandle.c} | 4 +- .../http/src/{tgJson.c => httpTgJson.c} | 4 +- src/util/inc/elog.h | 71 -------------- src/util/src/elog.c | 95 ------------------- 24 files changed, 60 insertions(+), 250 deletions(-) delete mode 100644 src/plugins/http/inc/ehttp_util_string.h rename src/plugins/http/inc/{gcHandle.h => httpGcHandle.h} (100%) rename src/plugins/http/inc/{gcJson.h => httpGcJson.h} (100%) rename src/plugins/http/inc/{ehttp_gzip.h => httpGzip.h} (100%) rename src/plugins/http/inc/{ehttp_parser.h => httpParser.h} (89%) rename src/plugins/http/inc/{restHandle.h => httpRestHandle.h} (100%) rename src/plugins/http/inc/{restJson.h => httpRestJson.h} (100%) rename src/plugins/http/inc/{tgHandle.h => httpTgHandle.h} (100%) rename src/plugins/http/inc/{tgJson.h => httpTgJson.h} (100%) delete mode 100644 src/plugins/http/src/ehttp_util_string.c rename src/plugins/http/src/{gcHandle.c => httpGcHandle.c} (99%) rename src/plugins/http/src/{gcJson.c => httpGcJson.c} (99%) rename src/plugins/http/src/{ehttp_gzip.c => httpGzip.c} (99%) rename src/plugins/http/src/{ehttp_parser.c => httpParser.c} (98%) rename src/plugins/http/src/{restHandle.c => httpRestHandle.c} (98%) rename src/plugins/http/src/{restJson.c => httpRestJson.c} (99%) rename src/plugins/http/src/{tgHandle.c => httpTgHandle.c} (99%) rename src/plugins/http/src/{tgJson.c => httpTgJson.c} (99%) delete mode 100644 src/util/inc/elog.h delete mode 100644 src/util/src/elog.c diff --git a/src/plugins/http/inc/ehttp_util_string.h b/src/plugins/http/inc/ehttp_util_string.h deleted file mode 100644 index eb44805f96..0000000000 --- a/src/plugins/http/inc/ehttp_util_string.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef HTTP_UTIL_STRING -#define HTTP_UTIL_STRING - -typedef struct HttpUtilString { - char * str; - size_t len; -} HttpUtilString; - -void httpParserCleanupString(HttpUtilString *str); -int32_t httpParserAppendString(HttpUtilString *str, const char *s, int32_t len); -void httpParserClearString(HttpUtilString *str); - -#endif diff --git a/src/plugins/http/inc/gcHandle.h b/src/plugins/http/inc/httpGcHandle.h similarity index 100% rename from src/plugins/http/inc/gcHandle.h rename to src/plugins/http/inc/httpGcHandle.h diff --git a/src/plugins/http/inc/gcJson.h b/src/plugins/http/inc/httpGcJson.h similarity index 100% rename from src/plugins/http/inc/gcJson.h rename to src/plugins/http/inc/httpGcJson.h diff --git a/src/plugins/http/inc/ehttp_gzip.h b/src/plugins/http/inc/httpGzip.h similarity index 100% rename from src/plugins/http/inc/ehttp_gzip.h rename to src/plugins/http/inc/httpGzip.h diff --git a/src/plugins/http/inc/httpInt.h b/src/plugins/http/inc/httpInt.h index 89972adc87..4348e5e090 100644 --- a/src/plugins/http/inc/httpInt.h +++ b/src/plugins/http/inc/httpInt.h @@ -27,8 +27,7 @@ #include "httpCode.h" #include "httpLog.h" #include "httpJson.h" - -#include "ehttp_parser.h" +#include "httpParser.h" #define HTTP_MAX_CMD_SIZE 1024 #define HTTP_MAX_BUFFER_SIZE 1024*1024 diff --git a/src/plugins/http/inc/ehttp_parser.h b/src/plugins/http/inc/httpParser.h similarity index 89% rename from src/plugins/http/inc/ehttp_parser.h rename to src/plugins/http/inc/httpParser.h index 7ddfcb32db..4facea0286 100644 --- a/src/plugins/http/inc/ehttp_parser.h +++ b/src/plugins/http/inc/httpParser.h @@ -1,8 +1,7 @@ #ifndef HTTP_PARSER_H #define HTTP_PARSER_H -#include "ehttp_util_string.h" -#include "ehttp_gzip.h" +#include "httpGzip.h" struct HttpContext; @@ -25,6 +24,11 @@ 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; @@ -77,11 +81,16 @@ typedef struct HttpParserObj { size_t received_chunk_size; size_t received_size; ehttp_gzip_t * gzip; - HttpUtilString str; + HttpParserString str; HTTP_PARSER_STATE *stacks; size_t stacks_count; } HttpParserObj; +void httpParserCleanupString(HttpParserString *str); +int32_t httpParserAppendString(HttpParserString *str, const char *s, int32_t len); +void httpParserClearString(HttpParserString *str); + + 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); diff --git a/src/plugins/http/inc/restHandle.h b/src/plugins/http/inc/httpRestHandle.h similarity index 100% rename from src/plugins/http/inc/restHandle.h rename to src/plugins/http/inc/httpRestHandle.h diff --git a/src/plugins/http/inc/restJson.h b/src/plugins/http/inc/httpRestJson.h similarity index 100% rename from src/plugins/http/inc/restJson.h rename to src/plugins/http/inc/httpRestJson.h diff --git a/src/plugins/http/inc/tgHandle.h b/src/plugins/http/inc/httpTgHandle.h similarity index 100% rename from src/plugins/http/inc/tgHandle.h rename to src/plugins/http/inc/httpTgHandle.h diff --git a/src/plugins/http/inc/tgJson.h b/src/plugins/http/inc/httpTgJson.h similarity index 100% rename from src/plugins/http/inc/tgJson.h rename to src/plugins/http/inc/httpTgJson.h diff --git a/src/plugins/http/src/ehttp_util_string.c b/src/plugins/http/src/ehttp_util_string.c deleted file mode 100644 index 8900aa89bb..0000000000 --- a/src/plugins/http/src/ehttp_util_string.c +++ /dev/null @@ -1,27 +0,0 @@ -#include "os.h" -#include "ehttp_util_string.h" - -void httpParserCleanupString(HttpUtilString *str) { - free(str->str); - str->str = NULL; - str->len = 0; -} - -int32_t httpParserAppendString(HttpUtilString *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(HttpUtilString *str) { - if (str->str) { - str->str[0] = '\0'; - str->len = 0; - } -} - diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c index 987de519c7..d310f27ee6 100644 --- a/src/plugins/http/src/httpContext.c +++ b/src/plugins/http/src/httpContext.c @@ -27,7 +27,6 @@ #include "httpSql.h" #include "httpSession.h" #include "httpContext.h" -#include "elog.h" extern bool httpGetHttpMethod(HttpContext* pContext); extern bool httpParseURL(HttpContext* pContext); @@ -144,8 +143,8 @@ HttpContext *httpCreateContext(int32_t fd) { HttpContext *httpGetContext(void *ptr) { uint64_t handleVal = (uint64_t)ptr; HttpContext **ppContext = taosCacheAcquireByKey(tsHttpServer.contextCache, &handleVal, sizeof(HttpContext *)); - EQ_ASSERT(ppContext); - EQ_ASSERT(*ppContext); + ASSERT(ppContext); + ASSERT(*ppContext); if (ppContext) { HttpContext *pContext = *ppContext; diff --git a/src/plugins/http/src/gcHandle.c b/src/plugins/http/src/httpGcHandle.c similarity index 99% rename from src/plugins/http/src/gcHandle.c rename to src/plugins/http/src/httpGcHandle.c index 4aed6eb5cc..09f17cae66 100644 --- a/src/plugins/http/src/gcHandle.c +++ b/src/plugins/http/src/httpGcHandle.c @@ -15,11 +15,11 @@ #define _DEFAULT_SOURCE #include "os.h" +#include "taosdef.h" #include "cJSON.h" #include "httpLog.h" -#include "gcHandle.h" -#include "gcJson.h" -#include "taosdef.h" +#include "httpGcHandle.h" +#include "httpGcJson.h" static HttpDecodeMethod gcDecodeMethod = {"grafana", gcProcessRequest}; static HttpEncodeMethod gcHeartBeatMethod = { diff --git a/src/plugins/http/src/gcJson.c b/src/plugins/http/src/httpGcJson.c similarity index 99% rename from src/plugins/http/src/gcJson.c rename to src/plugins/http/src/httpGcJson.c index 94d53db6ef..85cae24201 100644 --- a/src/plugins/http/src/gcJson.c +++ b/src/plugins/http/src/httpGcJson.c @@ -15,8 +15,8 @@ #define _DEFAULT_SOURCE #include "os.h" -#include "gcHandle.h" -#include "gcJson.h" +#include "httpGcHandle.h" +#include "httpGcJson.h" #include "httpJson.h" #include "httpResp.h" diff --git a/src/plugins/http/src/ehttp_gzip.c b/src/plugins/http/src/httpGzip.c similarity index 99% rename from src/plugins/http/src/ehttp_gzip.c rename to src/plugins/http/src/httpGzip.c index ded344dfea..5712aff7ec 100644 --- a/src/plugins/http/src/ehttp_gzip.c +++ b/src/plugins/http/src/httpGzip.c @@ -1,9 +1,6 @@ -#include "ehttp_gzip.h" - #include "os.h" #include "zlib.h" - -#include +#include "httpGzip.h" typedef enum { EHTTP_GZIP_INITING, diff --git a/src/plugins/http/src/ehttp_parser.c b/src/plugins/http/src/httpParser.c similarity index 98% rename from src/plugins/http/src/ehttp_parser.c rename to src/plugins/http/src/httpParser.c index 0d0382078f..c1fd481efd 100644 --- a/src/plugins/http/src/ehttp_parser.c +++ b/src/plugins/http/src/httpParser.c @@ -1,18 +1,8 @@ #include "os.h" #include "httpLog.h" #include "httpContext.h" -#include "ehttp_util_string.h" -#include "ehttp_parser.h" - -#include "ehttp_gzip.h" -#include "ehttp_util_string.h" -#include "elog.h" - - -#include -#include -#include - +#include "httpParser.h" +#include "httpGzip.h" static HttpParserStatusObj status_codes[] = { {100, "Continue"}, @@ -213,7 +203,7 @@ void httpParserDestroy(HttpParserObj *parser) { char *ehttp_parser_urldecode(const char *enc) { int ok = 1; - HttpUtilString str = {0}; + HttpParserString str = {0}; while (*enc) { char *p = strchr(enc, '%'); if (!p) break; @@ -916,3 +906,27 @@ 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; + } +} diff --git a/src/plugins/http/src/restHandle.c b/src/plugins/http/src/httpRestHandle.c similarity index 98% rename from src/plugins/http/src/restHandle.c rename to src/plugins/http/src/httpRestHandle.c index 8f998420de..b285b19c3f 100644 --- a/src/plugins/http/src/restHandle.c +++ b/src/plugins/http/src/httpRestHandle.c @@ -16,8 +16,8 @@ #define _DEFAULT_SOURCE #include "os.h" #include "httpLog.h" -#include "restHandle.h" -#include "restJson.h" +#include "httpRestHandle.h" +#include "httpRestJson.h" static HttpDecodeMethod restDecodeMethod = {"rest", restProcessRequest}; static HttpDecodeMethod restDecodeMethod2 = {"restful", restProcessRequest}; diff --git a/src/plugins/http/src/restJson.c b/src/plugins/http/src/httpRestJson.c similarity index 99% rename from src/plugins/http/src/restJson.c rename to src/plugins/http/src/httpRestJson.c index 4a7836ad7a..c16727faa0 100644 --- a/src/plugins/http/src/restJson.c +++ b/src/plugins/http/src/httpRestJson.c @@ -18,8 +18,8 @@ #include "tglobal.h" #include "httpLog.h" #include "httpJson.h" -#include "restHandle.h" -#include "restJson.h" +#include "httpRestHandle.h" +#include "httpRestJson.h" void restBuildSqlAffectRowsJson(HttpContext *pContext, HttpSqlCmd *cmd, int affect_rows) { JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index b42e2cb0aa..e0d2a90994 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -25,8 +25,6 @@ #include "httpResp.h" #include "httpUtil.h" -#include "elog.h" - #ifndef EPOLLWAKEUP #define EPOLLWAKEUP (1u << 29) #endif @@ -334,7 +332,7 @@ bool httpInitConnect() { static bool httpReadData(HttpContext *pContext) { HttpParser *pParser = &pContext->parser; - EQ_ASSERT(!pContext->parsed); + ASSERT(!pContext->parsed); if (!pParser->parser) { if (!pParser->inited) { diff --git a/src/plugins/http/src/httpSystem.c b/src/plugins/http/src/httpSystem.c index e51c8dd4f7..ee43b3c826 100644 --- a/src/plugins/http/src/httpSystem.c +++ b/src/plugins/http/src/httpSystem.c @@ -27,9 +27,9 @@ #include "httpResp.h" #include "httpHandle.h" #include "httpQueue.h" -#include "gcHandle.h" -#include "restHandle.h" -#include "tgHandle.h" +#include "httpGcHandle.h" +#include "httpRestHandle.h" +#include "httpTgHandle.h" #ifndef _ADMIN void adminInitHandle(HttpServer* pServer) {} diff --git a/src/plugins/http/src/tgHandle.c b/src/plugins/http/src/httpTgHandle.c similarity index 99% rename from src/plugins/http/src/tgHandle.c rename to src/plugins/http/src/httpTgHandle.c index 84d7d13fde..a7444676ae 100644 --- a/src/plugins/http/src/tgHandle.c +++ b/src/plugins/http/src/httpTgHandle.c @@ -19,8 +19,8 @@ #include "taosdef.h" #include "taosmsg.h" #include "httpInt.h" -#include "tgHandle.h" -#include "tgJson.h" +#include "httpTgHandle.h" +#include "httpTgJson.h" #include "cJSON.h" /* diff --git a/src/plugins/http/src/tgJson.c b/src/plugins/http/src/httpTgJson.c similarity index 99% rename from src/plugins/http/src/tgJson.c rename to src/plugins/http/src/httpTgJson.c index 170a1b343e..5c6985cd69 100644 --- a/src/plugins/http/src/tgJson.c +++ b/src/plugins/http/src/httpTgJson.c @@ -19,8 +19,8 @@ #include "httpLog.h" #include "httpJson.h" #include "httpResp.h" -#include "tgHandle.h" -#include "tgJson.h" +#include "httpTgHandle.h" +#include "httpTgJson.h" void tgInitQueryJson(HttpContext *pContext) { JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); diff --git a/src/util/inc/elog.h b/src/util/inc/elog.h deleted file mode 100644 index 8e11c3e761..0000000000 --- a/src/util/inc/elog.h +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef _elog_h_8897be44_dda8_45b6_9d37_8d8691cb05fb_ -#define _elog_h_8897be44_dda8_45b6_9d37_8d8691cb05fb_ - -#include - -typedef enum { - ELOG_DEBUG, - ELOG_INFO, - ELOG_WARN, - ELOG_ERROR, - ELOG_CRITICAL, - ELOG_VERBOSE, - ELOG_ABORT, -} ELOG_LEVEL; - -void elog_set_level(ELOG_LEVEL base); // only log those not less than base -void elog_set_thread_name(const char *name); - -void elog(ELOG_LEVEL level, int fd, const char *file, int line, const char *func, const char *fmt, ...) -#ifdef __GNUC__ - __attribute__((format(printf, 6, 7))) -#endif -; - -#define DLOG(fd, fmt, ...) elog(ELOG_DEBUG, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define ILOG(fd, fmt, ...) elog(ELOG_INFO, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define WLOG(fd, fmt, ...) elog(ELOG_WARN, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define ELOG(fd, fmt, ...) elog(ELOG_ERROR, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define CLOG(fd, fmt, ...) elog(ELOG_CRITICAL, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define VLOG(fd, fmt, ...) elog(ELOG_VERBOSE, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define ALOG(fd, fmt, ...) elog(ELOG_ABORT, fd, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) - -#define D(fmt, ...) elog(ELOG_DEBUG, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define I(fmt, ...) elog(ELOG_INFO, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define W(fmt, ...) elog(ELOG_WARN, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define E(fmt, ...) elog(ELOG_ERROR, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define C(fmt, ...) elog(ELOG_CRITICAL, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define V(fmt, ...) elog(ELOG_VERBOSE, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define A(fmt, ...) elog(ELOG_ABORT, fileno(stdout), __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) - - - -// NOTE: https://en.wikipedia.org/wiki/Fail-fast -// for the sake of simplicity, both implementation and usage, -// we'll follow `fail-fast` or `let-it-crash` philosophy. - -// assertion in both debug/release build -#define EQ_ABORT(fmt, ...) A("Assertion failure: "fmt, ##__VA_ARGS__) - -#define EQ_ASSERT(statement) do { \ - if (statement) break; \ - A("Assertion failure: %s", #statement); \ -} while (0) - -#define EQ_ASSERT_EXT(statement, fmt, ...) do { \ - if (statement) break; \ - A("Assertion failure: %s: "fmt, #statement, ##__VA_ARGS__); \ -} while (0) - -#define EQ_ASSERT_API0(statement) do { \ - if (statement) break; \ - A("Assertion failure: %s failed: [%d]%s", #statement, errno, strerror(errno)); \ -} while (0) - -#define EQ_ASSERT_API(api) do { \ - A("Assertion failure: %s failed: [%d]%s", #api, errno, strerror(errno)); \ -} while (0) - - -#endif // _elog_h_8897be44_dda8_45b6_9d37_8d8691cb05fb_ - diff --git a/src/util/src/elog.c b/src/util/src/elog.c deleted file mode 100644 index 95b580962c..0000000000 --- a/src/util/src/elog.c +++ /dev/null @@ -1,95 +0,0 @@ -#include "elog.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#define gettid() syscall(__NR_gettid) - -static ELOG_LEVEL elog_level_base = ELOG_DEBUG; - -static __thread long elog_thread_id; -static __thread char elog_thread_name[24] = {0}; - -void elog_set_level(ELOG_LEVEL base) { - elog_level_base = base; -} - -void elog_set_thread_name(const char *name) { - elog_thread_id = gettid(); - snprintf(elog_thread_name, sizeof(elog_thread_name), "%s", name); -} - -void elog(ELOG_LEVEL level, int fd, const char *file, int line, const char *func, const char *fmt, ...) -{ - if (level < elog_level_base) return; - if (fd == -1) return; - - if (elog_thread_name[0]=='\0') { - elog_set_thread_name("unknown"); - } - - char *p; - int n; - size_t bytes; - - char buf[4096]; - snprintf(buf, sizeof(buf), "%s", file); - - char fn[1024]; - snprintf(fn, sizeof(fn), "%s", basename(buf)); - - char C; - switch (level) { - case ELOG_DEBUG: C = 'D'; break; - case ELOG_INFO: C = 'I'; break; - case ELOG_WARN: C = 'W'; break; - case ELOG_ERROR: C = 'E'; break; - case ELOG_CRITICAL: C = 'C'; break; - case ELOG_VERBOSE: C = 'V'; break; - case ELOG_ABORT: C = 'A'; break; - default: return; - } - - struct tm t; - struct timeval tv; - - if (gettimeofday(&tv, NULL)) return; - if (!localtime_r(&tv.tv_sec, &t)) return; - - p = buf; - bytes = sizeof(buf); - - n = snprintf(p, bytes, "%c[%02d/%02d %02d:%02d:%02d.%06ld][%06ld]: ==", - C, - t.tm_mon + 1, t.tm_mday, - t.tm_hour, t.tm_min, t.tm_sec, - tv.tv_usec, - elog_thread_id); - p += n; bytes -= n; - - va_list arg; - va_start(arg, fmt); - if (bytes>0) { - n = vsnprintf(p, bytes, fmt, arg); - p += n; bytes -= n; - } - va_end(arg); - - if (bytes>0) { - n = snprintf(p, bytes, "== t:%s#%s[%d]#%s()", - elog_thread_name, fn, line, func); - } - - dprintf(fd, "%s\n", buf); - - if (level == ELOG_ABORT) { - abort(); - } -} - From eff4b5807a9a98b4d2c5767448510fa7098f3771 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 14 Sep 2020 04:50:45 +0000 Subject: [PATCH 12/30] TD-1311 --- src/inc/taoserror.h | 37 +- src/plugins/http/inc/httpAuth.h | 6 +- src/plugins/http/inc/httpGcJson.h | 2 +- src/plugins/http/inc/httpGzip.h | 29 +- src/plugins/http/inc/httpInt.h | 118 +-- src/plugins/http/inc/httpJson.h | 62 +- src/plugins/http/inc/httpParser.h | 155 ++-- src/plugins/http/inc/httpResp.h | 6 +- src/plugins/http/inc/httpRestJson.h | 8 +- src/plugins/http/inc/httpServer.h | 5 +- src/plugins/http/inc/httpSql.h | 14 +- src/plugins/http/inc/httpTgJson.h | 6 +- src/plugins/http/inc/httpUtil.h | 18 +- src/plugins/http/src/httpAuth.c | 16 +- src/plugins/http/src/httpContext.c | 183 +---- src/plugins/http/src/httpGcHandle.c | 25 +- src/plugins/http/src/httpGcJson.c | 30 +- src/plugins/http/src/httpGzip.c | 34 +- src/plugins/http/src/httpHandle.c | 229 +----- src/plugins/http/src/httpJson.c | 111 +-- src/plugins/http/src/httpParser.c | 1027 +++++++++++++++---------- src/plugins/http/src/httpQueue.c | 6 +- src/plugins/http/src/httpResp.c | 40 +- src/plugins/http/src/httpRestHandle.c | 16 +- src/plugins/http/src/httpRestJson.c | 22 +- src/plugins/http/src/httpServer.c | 87 +-- src/plugins/http/src/httpSystem.c | 6 +- src/plugins/http/src/httpTgHandle.c | 103 ++- src/plugins/http/src/httpTgJson.c | 14 +- src/plugins/http/src/httpUtil.c | 68 +- 30 files changed, 1145 insertions(+), 1338 deletions(-) diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index d8e5c8f1d7..3f7995f25e 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -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 diff --git a/src/plugins/http/inc/httpAuth.h b/src/plugins/http/inc/httpAuth.h index b8fabbe1ec..4becae6332 100644 --- a/src/plugins/http/inc/httpAuth.h +++ b/src/plugins/http/inc/httpAuth.h @@ -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, int32_t len); +bool httpParseTaosdAuthToken(HttpContext *pContext, char *token, int32_t len); +bool httpGenTaosdAuthToken(HttpContext *pContext, char *token, int32_t maxLen); #endif \ No newline at end of file diff --git a/src/plugins/http/inc/httpGcJson.h b/src/plugins/http/inc/httpGcJson.h index 609bb9b95e..0ba860687d 100644 --- a/src/plugins/http/inc/httpGcJson.h +++ b/src/plugins/http/inc/httpGcJson.h @@ -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, int32_t numOfRows); void gcSendHeartBeatResp(HttpContext *pContext, HttpSqlCmd *cmd); diff --git a/src/plugins/http/inc/httpGzip.h b/src/plugins/http/inc/httpGzip.h index b2d6ace9b0..aeac79c975 100644 --- a/src/plugins/http/inc/httpGzip.h +++ b/src/plugins/http/inc/httpGzip.h @@ -1,7 +1,20 @@ -#ifndef _ehttp_gzip_h_9196791b_ac2a_4d73_9979_f4b41abbc4c0_ -#define _ehttp_gzip_h_9196791b_ac2a_4d73_9979_f4b41abbc4c0_ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ -#include +#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 + int32_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); +int32_t ehttp_gzip_write(ehttp_gzip_t *gzip, const char *buf, int32_t len); +int32_t ehttp_gzip_finish(ehttp_gzip_t *gzip); #endif // _ehttp_gzip_h_9196791b_ac2a_4d73_9979_f4b41abbc4c0_ diff --git a/src/plugins/http/inc/httpInt.h b/src/plugins/http/inc/httpInt.h index 4348e5e090..86e1eeb6d6 100644 --- a/src/plugins/http/inc/httpInt.h +++ b/src/plugins/http/inc/httpInt.h @@ -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; + int32_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; - HttpEncodeMethod * encodeMethod; + HttpEncodeMethod *encodeMethod; + HttpDecodeMethod *decodeMethod; 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; + int32_t pollFd; + int32_t numOfContexts; + int32_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; + int32_t fd; + int32_t numOfThreads; + int32_t methodScannerLen; int32_t requestNum; int32_t status; pthread_t thread; diff --git a/src/plugins/http/inc/httpJson.h b/src/plugins/http/inc/httpJson.h index 905460c67b..ac0a632137 100644 --- a/src/plugins/http/inc/httpJson.h +++ b/src/plugins/http/inc/httpJson.h @@ -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; + int32_t size; + int32_t total; + char* lst; + char buf[JSON_BUFFER_SIZE]; + struct HttpContext* pContext; } JsonBuf; // http response -int httpWriteBuf(struct HttpContext* pContext, const char* buf, int sz); -int httpWriteBufNoTrace(struct HttpContext* pContext, const char* buf, int sz); -int httpWriteBufByFd(struct HttpContext* pContext, const char* buf, int sz); +int32_t httpWriteBuf(struct HttpContext* pContext, const char* buf, int32_t sz); +int32_t httpWriteBufNoTrace(struct HttpContext* pContext, const char* buf, int32_t sz); +int32_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); +int32_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, int32_t len); +void httpJsonOriginString(JsonBuf* buf, char* sVal, int32_t len); +void httpJsonStringForTransMean(JsonBuf* buf, char* SVal, int32_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, int32_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, int32_t val); // pair -void httpJsonPair(JsonBuf* buf, char* name, int nameLen, char* sVal, int valLen); -void httpJsonPairOriginString(JsonBuf* buf, char* name, int nameLen, char* sVal, int valLen); -void httpJsonPairHead(JsonBuf* buf, char* name, int len); -void httpJsonPairIntVal(JsonBuf* buf, char* name, int nNameLen, int num); -void httpJsonPairInt64Val(JsonBuf* buf, char* name, int nNameLen, int64_t num); -void httpJsonPairBoolVal(JsonBuf* buf, char* name, int nNameLen, int 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, int32_t nameLen, char* sVal, int32_t valLen); +void httpJsonPairOriginString(JsonBuf* buf, char* name, int32_t nameLen, char* sVal, int32_t valLen); +void httpJsonPairHead(JsonBuf* buf, char* name, int32_t len); +void httpJsonPairIntVal(JsonBuf* buf, char* name, int32_t nNameLen, int32_t num); +void httpJsonPairInt64Val(JsonBuf* buf, char* name, int32_t nNameLen, int64_t num); +void httpJsonPairBoolVal(JsonBuf* buf, char* name, int32_t nNameLen, int32_t num); +void httpJsonPairFloatVal(JsonBuf* buf, char* name, int32_t nNameLen, float num); +void httpJsonPairDoubleVal(JsonBuf* buf, char* name, int32_t nNameLen, double num); +void httpJsonPairNullVal(JsonBuf* buf, char* name, int32_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, int32_t nLen, httpJsonBuilder builder, void* dsHandle); +void httpJsonPairObject(JsonBuf* buf, char* name, int32_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, int32_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, int32_t len); // quick -void httpJsonPairStatus(JsonBuf* buf, int code); +void httpJsonPairStatus(JsonBuf* buf, int32_t code); // http json printer JsonBuf* httpMallocJsonBuf(struct HttpContext* pContext); diff --git a/src/plugins/http/inc/httpParser.h b/src/plugins/http/inc/httpParser.h index 4facea0286..5694c8f0cd 100644 --- a/src/plugins/http/inc/httpParser.h +++ b/src/plugins/http/inc/httpParser.h @@ -1,9 +1,23 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + #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 enum HTTP_AUTH_TYPE { + HTTP_INVALID_AUTH, + HTTP_BASIC_AUTH, + HTTP_TAOSD_AUTH +} HTTP_AUTH_TYPE; -typedef struct HttpParserStatusObj { - int32_t status_code; - const char *status_desc; -} HttpParserStatusObj; +typedef enum HTTP_VERSION { + HTTP_VERSION_10 = 0, + HTTP_VERSION_11 = 1, + HTTP_VERSION_12 = 2, + HTTP_INVALID_VERSION +} HTTP_VERSION; -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_KEEPALIVE { + HTTP_KEEPALIVE_NO_INPUT = 0, + HTTP_KEEPALIVE_ENABLE = 1, + HTTP_KEEPALIVE_DISABLE = 2 +} HTTP_KEEPALIVE; -typedef struct HttpParserConfObj { - size_t flush_block_size; // <=0: immediately -} HttpParserConfObj; +typedef struct HttpString { + char * str; + int32_t pos; + int32_t size; +} HttpString; -typedef struct HttpParseKvObj { - char *key; - char *val; -} HttpParseKvObj; +typedef struct HttpStatus { + int32_t code; + char * desc; +} HttpStatus; -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 HttpStack{ + int8_t *stacks; + int32_t pos; + int32_t size; +} HttpStack; -void httpParserCleanupString(HttpParserString *str); -int32_t httpParserAppendString(HttpParserString *str, const char *s, int32_t len); -void httpParserClearString(HttpParserString *str); +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 diff --git a/src/plugins/http/inc/httpResp.h b/src/plugins/http/inc/httpResp.h index 5eaaa2a037..9086d0c9d3 100644 --- a/src/plugins/http/inc/httpResp.h +++ b/src/plugins/http/inc/httpResp.h @@ -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, int32_t errNo); +void httpSendErrorRespWithDesc(HttpContext *pContext, int32_t errNo, char *desc); +void httpSendTaosdErrorResp(HttpContext *pContext, int32_t errCode); void httpSendTaosdInvalidSqlErrorResp(HttpContext *pContext, char* errMsg); void httpSendSuccResp(HttpContext *pContext, char *desc); void httpSendOptionResp(HttpContext *pContext, char *desc); diff --git a/src/plugins/http/inc/httpRestJson.h b/src/plugins/http/inc/httpRestJson.h index 7cff21d190..112e845f36 100644 --- a/src/plugins/http/inc/httpRestJson.h +++ b/src/plugins/http/inc/httpRestJson.h @@ -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, int32_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, int32_t numOfRows); +bool restBuildSqlLocalTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows); +bool restBuildSqlUtcTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows); void restStopSqlJson(HttpContext *pContext, HttpSqlCmd *cmd); #endif \ No newline at end of file diff --git a/src/plugins/http/inc/httpServer.h b/src/plugins/http/inc/httpServer.h index 508baa6112..58ed3545f3 100644 --- a/src/plugins/http/inc/httpServer.h +++ b/src/plugins/http/inc/httpServer.h @@ -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 diff --git a/src/plugins/http/inc/httpSql.h b/src/plugins/http/inc/httpSql.h index 09f5b142fb..660a65e44d 100644 --- a/src/plugins/http/inc/httpSql.h +++ b/src/plugins/http/inc/httpSql.h @@ -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, int32_t mallocSize); int32_t httpAddToSqlCmdBufferTerminal(HttpContext *pContext); -bool httpMallocMultiCmds(HttpContext *pContext, int cmdSize, int bufferSize); -bool httpReMallocMultiCmdsSize(HttpContext *pContext, int cmdSize); -bool httpReMallocMultiCmdsBuffer(HttpContext *pContext, int bufferSize); +bool httpMallocMultiCmds(HttpContext *pContext, int32_t cmdSize, int32_t bufferSize); +bool httpReMallocMultiCmdsSize(HttpContext *pContext, int32_t cmdSize); +bool httpReMallocMultiCmdsBuffer(HttpContext *pContext, int32_t bufferSize); void httpFreeMultiCmds(HttpContext *pContext); HttpSqlCmd *httpNewSqlCmd(HttpContext *pContext); HttpSqlCmd *httpCurrSqlCmd(HttpContext *pContext); -int httpCurSqlCmdPos(HttpContext *pContext); +int32_t httpCurSqlCmdPos(HttpContext *pContext); void httpTrimTableName(char *name); -int httpShrinkTableName(HttpContext *pContext, int pos, char *name); -char *httpGetCmdsString(HttpContext *pContext, int pos); +int32_t httpShrinkTableName(HttpContext *pContext, int32_t pos, char *name); +char *httpGetCmdsString(HttpContext *pContext, int32_t pos); #endif diff --git a/src/plugins/http/inc/httpTgJson.h b/src/plugins/http/inc/httpTgJson.h index bf3aa093ae..6b7d0681b6 100644 --- a/src/plugins/http/inc/httpTgJson.h +++ b/src/plugins/http/inc/httpTgJson.h @@ -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, int32_t affect_rows); +bool tgCheckFinished(struct HttpContext *pContext, HttpSqlCmd *cmd, int32_t code); +void tgSetNextCmd(struct HttpContext *pContext, HttpSqlCmd *cmd, int32_t code); #endif \ No newline at end of file diff --git a/src/plugins/http/inc/httpUtil.h b/src/plugins/http/inc/httpUtil.h index c82f702ebc..61cd50a77a 100644 --- a/src/plugins/http/inc/httpUtil.h +++ b/src/plugins/http/inc/httpUtil.h @@ -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, int32_t buflen); -bool httpUrlMatch(HttpContext *pContext, int 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); +int32_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, +int32_t httpGzipDeCompress(char *srcData, int32_t nSrcData, char *destData, int32_t *nDestData); +int32_t httpGzipCompressInit(HttpContext *pContext); +int32_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 diff --git a/src/plugins/http/src/httpAuth.c b/src/plugins/http/src/httpAuth.c index dd4d14c709..40bb691b5d 100644 --- a/src/plugins/http/src/httpAuth.c +++ b/src/plugins/http/src/httpAuth.c @@ -23,9 +23,9 @@ #define KEY_DES_4 4971256377704625728L -bool httpParseBasicAuthToken(HttpContext *pContext, char *token, int len) { +bool httpParseBasicAuthToken(HttpContext *pContext, char *token, int32_t len) { token[len] = '\0'; - int outlen = 0; + int32_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 = (int)(user - base64); + int32_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 = (int)((base64 + outlen) - password); + int32_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, int32_t len) { token[len] = '\0'; - int outlen = 0; + int32_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, int32_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, generate taosd token:%s", pContext, pContext->fd, token); return true; } diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c index d310f27ee6..2c65b9d16a 100644 --- a/src/plugins/http/src/httpContext.c +++ b/src/plugins/http/src/httpContext.c @@ -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) { - httpParserDestroy(pContext->parser.parser); - pContext->parser.parser = NULL; + if (pContext->parser) { + httpDestroyParser(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 (pContext->httpVersion == HTTP_VERSION_10 && pContext->httpKeepAlive != HTTP_KEEPALIVE_ENABLE) { + if (parser->httpVersion == HTTP_VERSION_10 && parser->keepAlive != HTTP_KEEPALIVE_ENABLE) { keepAlive = false; - } else if (pContext->httpVersion != HTTP_VERSION_10 && pContext->httpKeepAlive == HTTP_KEEPALIVE_DISABLE) { + } else if (parser->httpVersion != HTTP_VERSION_10 && parser->keepAlive == 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; -} diff --git a/src/plugins/http/src/httpGcHandle.c b/src/plugins/http/src/httpGcHandle.c index 09f17cae66..e010c77ffd 100644 --- a/src/plugins/http/src/httpGcHandle.c +++ b/src/plugins/http/src/httpGcHandle.c @@ -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); + int32_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 (int32_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); + int32_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; + int32_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); + int32_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; diff --git a/src/plugins/http/src/httpGcJson.c b/src/plugins/http/src/httpGcJson.c index 85cae24201..a291641dc3 100644 --- a/src/plugins/http/src/httpGcJson.c +++ b/src/plugins/http/src/httpGcJson.c @@ -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, (int32_t)strlen(refId)); + httpJsonPair(jsonBuf, "target", 6, target, (int32_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, int32_t numOfRows) { JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); if (jsonBuf == NULL) return false; - int num_fields = taos_num_fields(result); + int32_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); + int32_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; + int32_t dataFields = -1; + int32_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 (int32_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; + int32_t len; len = snprintf(target,HTTP_GC_TARGET_SIZE,"%s{",aliasBuffer); - for (int i = dataFields + 1; i= 0; i--) { + for (int32_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, (int)strlen(desc)); + httpJsonPair(jsonBuf, "message", (int32_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->httpKeepAlive], (jsonBuf->lst - jsonBuf->buf)); + int32_t hLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_GRAFANA], httpVersionStr[pContext->parser->httpVersion], + httpKeepAliveStr[pContext->parser->keepAlive], (jsonBuf->lst - jsonBuf->buf)); httpWriteBuf(pContext, head, hLen); - httpWriteBuf(pContext, jsonBuf->buf, (int)(jsonBuf->lst - jsonBuf->buf)); + httpWriteBuf(pContext, jsonBuf->buf, (int32_t)(jsonBuf->lst - jsonBuf->buf)); } diff --git a/src/plugins/http/src/httpGzip.c b/src/plugins/http/src/httpGzip.c index 5712aff7ec..54f900c755 100644 --- a/src/plugins/http/src/httpGzip.c +++ b/src/plugins/http/src/httpGzip.c @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#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; + int32_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 + int32_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) { +int32_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; + int32_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) { +int32_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; + int32_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); diff --git a/src/plugins/http/src/httpHandle.c b/src/plugins/http/src/httpHandle.c index 59b3268392..9fb46c5734 100644 --- a/src/plugins/http/src/httpHandle.c +++ b/src/plugins/http/src/httpHandle.c @@ -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 (*pParser->pMethod->decodeFp)(pContext); + return (*pContext->decodeMethod->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 { diff --git a/src/plugins/http/src/httpJson.c b/src/plugins/http/src/httpJson.c index e200efbcef..e02782357c 100644 --- a/src/plugins/http/src/httpJson.c +++ b/src/plugins/http/src/httpJson.c @@ -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, int sz) { - int len; - int countWait = 0; - int writeLen = 0; +int32_t httpWriteBufByFd(struct HttpContext* pContext, const char* buf, int32_t sz) { + int32_t len; + int32_t countWait = 0; + int32_t writeLen = 0; do { if (pContext->fd > 2){ - len = (int)taosSend(pContext->fd, buf + writeLen, (size_t)(sz - writeLen), MSG_NOSIGNAL); + len = (int32_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, int sz) { - int writeSz = httpWriteBufByFd(pContext, buf, sz); +int32_t httpWriteBuf(struct HttpContext* pContext, const char* buf, int32_t sz) { + int32_t writeSz = httpWriteBufByFd(pContext, buf, sz); if (writeSz != sz) { httpError("context:%p, fd:%d, dataSize:%d, writeSize:%d, failed to send response:\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, int sz) { - int writeSz = httpWriteBufByFd(pContext, buf, sz); +int32_t httpWriteBufNoTrace(struct HttpContext *pContext, const char *buf, int32_t sz) { + int32_t writeSz = httpWriteBufByFd(pContext, buf, sz); if (writeSz != sz) { httpError("context:%p, fd:%d, dataSize:%d, writeSize:%d, failed to send response", pContext, pContext->fd, sz, 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; +int32_t httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) { + int32_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); + int32_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, (int32_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); + int32_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); + int32_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 += (int32_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; + int32_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->httpKeepAlive]); + if (buf->pContext->parser->acceptEncodingGzip == 0 || !tsHttpEnableCompress) { + len = sprintf(msg, httpRespTemplate[HTTP_RESPONSE_CHUNKED_UN_COMPRESS], httpVersionStr[buf->pContext->parser->httpVersion], + httpKeepAliveStr[buf->pContext->parser->keepAlive]); } else { - len = sprintf(msg, httpRespTemplate[HTTP_RESPONSE_CHUNKED_COMPRESS], httpVersionStr[buf->pContext->httpVersion], - httpKeepAliveStr[buf->pContext->httpKeepAlive]); + len = sprintf(msg, httpRespTemplate[HTTP_RESPONSE_CHUNKED_COMPRESS], httpVersionStr[buf->pContext->parser->httpVersion], + httpKeepAliveStr[buf->pContext->parser->keepAlive]); } 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, int32_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, int32_t len) { httpJsonItemToken(buf); httpJsonPrint(buf, sVal, len); } -void httpJsonStringForTransMean(JsonBuf* buf, char* sVal, int maxLen) { +void httpJsonStringForTransMean(JsonBuf* buf, char* sVal, int32_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 (int32_t i = 0; i < maxLen; ++i) { if (*curPos == 0) { break; } if (*curPos == '\"') { - httpJsonPrint(buf, lastPos, (int)(curPos - lastPos)); + httpJsonPrint(buf, lastPos, (int32_t)(curPos - lastPos)); curPos++; lastPos = curPos; httpJsonPrint(buf, "\\\"", 2); } else if (*curPos == '\\') { - httpJsonPrint(buf, lastPos, (int)(curPos - lastPos)); + httpJsonPrint(buf, lastPos, (int32_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, (int32_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; + int32_t precision = 1000; if (us) { precision = 1000000; } time_t tt = t / precision; ptm = localtime(&tt); - int length = (int) strftime(ts, 35, "%Y-%m-%d %H:%M:%S", ptm); + int32_t length = (int32_t) strftime(ts, 35, "%Y-%m-%d %H:%M:%S", ptm); if (us) { length += snprintf(ts + length, 8, ".%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; + int32_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); + int32_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 += (int32_t)strftime(ts + length, 40 - length, "%z", ptm); httpJsonString(buf, ts, length); } -void httpJsonInt(JsonBuf* buf, int num) { +void httpJsonInt(JsonBuf* buf, int32_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, int32_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, int32_t len) { httpJsonItemToken(buf); httpJsonString(buf, name, len); httpJsonToken(buf, JsonPairTkn); } -void httpJsonPair(JsonBuf* buf, char* name, int nameLen, char* sVal, int valLen) { +void httpJsonPair(JsonBuf* buf, char* name, int32_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, int valLen) { +void httpJsonPairOriginString(JsonBuf* buf, char* name, int32_t nameLen, char* sVal, int32_t valLen) { httpJsonPairHead(buf, name, nameLen); httpJsonOriginString(buf, sVal, valLen); } -void httpJsonPairIntVal(JsonBuf* buf, char* name, int nNameLen, int num) { +void httpJsonPairIntVal(JsonBuf* buf, char* name, int32_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, int32_t nNameLen, int64_t num) { httpJsonPairHead(buf, name, nNameLen); httpJsonInt64(buf, num); } -void httpJsonPairBoolVal(JsonBuf* buf, char* name, int nNameLen, int num) { +void httpJsonPairBoolVal(JsonBuf* buf, char* name, int32_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, int32_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, int32_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, int32_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, int32_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, int32_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, int32_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, int32_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, int32_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), (int32_t)strlen(tstrerror(code))); } } } diff --git a/src/plugins/http/src/httpParser.c b/src/plugins/http/src/httpParser.c index c1fd481efd..0238c39eb8 100644 --- a/src/plugins/http/src/httpParser.c +++ b/src/plugins/http/src/httpParser.c @@ -1,10 +1,30 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#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} }; -const char* ehttp_status_code_get_desc(const int status_code) { - HttpParserStatusObj *p = status_codes; - while (p->status_code!=0) { - if (p->status_code==status_code) return p->status_desc; +char *httpGetStatusDesc(int32_t statusCode) { + HttpStatus *p = httpStatusCodes; + 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 { + } + + if (str->str == NULL) return -1; + + memcpy(str->str + str->pos, s, len); + str->pos += len; + str->str[str->pos] = 0; + return 0; } -static void dummy_on_header_field(void *arg, const char *key, const char *val) { +static void httpClearString(HttpString *str) { + if (str->str) { + str->str[0] = '\0'; + str->pos = 0; + str->size = 0; + } } -static void dummy_on_body(void *arg, const char *chunk, size_t len) { +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 void dummy_on_end(void *arg) { -} +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); -static void dummy_on_error(void *arg, int status_code) { -} + // 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; + } -static HTTP_PARSER_STATE httpParserTop(HttpParserObj *parser) { - ASSERT(parser->stacks_count >= 1); - ASSERT(parser->stacks); + // 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; + } + } - return parser->stacks[parser->stacks_count - 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; + } -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; - - 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; +} + +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; + } + } + + 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; + } + + 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); + } + + 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; + } + + 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; + } + + 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; + } + } return 0; } -HttpParserObj *httpParserCreate(HttpParserCallbackObj callbacks, HttpParserConfObj conf, void *arg) { - HttpParserObj *parser = (HttpParserObj*)calloc(1, sizeof(*parser)); +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; + } + } + + memcpy(buf->str + buf->pos, chunk, len); + buf->pos += len; + buf->str[buf->pos] = 0; + + 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 HTTP_PARSER_STATE httpTopStack(HttpParser *parser) { + HttpStack *stack = &parser->stacks; + ASSERT(stack->pos >= 1); + + 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 { + } + + if (stack->stacks == NULL) return -1; + + stack->stacks[stack->pos] = state; + stack->pos++; + + return 0; +} + +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->reasonPhrase); parser->reasonPhrase = NULL; + free(parser->key); parser->key = NULL; + free(parser->val); parser->val = 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; + } + + httpPushStack(parser, HTTP_PARSER_BEGIN); +} + +HttpParser *httpCreateParser(HttpContext *pContext) { + HttpParser *parser = calloc(1, sizeof(HttpParser)); if (!parser) return NULL; + httpTrace("context:%p, fd:%d, create parser", pContext, pContext->fd); - parser->callbacks = callbacks; - parser->arg = arg; - parser->conf = conf; - - if (parser->callbacks.on_request_line == NULL) { - parser->callbacks.on_request_line = dummy_on_request_line; - } - if (parser->callbacks.on_status_line == NULL) { - parser->callbacks.on_status_line = dummy_on_status_line; - } - if (parser->callbacks.on_header_field == NULL) { - parser->callbacks.on_header_field = dummy_on_header_field; - } - if (parser->callbacks.on_body == NULL) { - parser->callbacks.on_body = dummy_on_body; - } - if (parser->callbacks.on_end == NULL) { - parser->callbacks.on_end = dummy_on_end; - } - if (parser->callbacks.on_error == NULL) { - parser->callbacks.on_error = dummy_on_error; - } - - httpParserPush(parser, HTTP_PARSER_BEGIN); - + parser->pContext = pContext; return parser; } -static void ehttp_parser_kvs_destroy(HttpParserObj *parser) { - if (!parser->kvs) return; +void httpDestroyParser(HttpParser *parser) { + HttpContext *pContext = parser->pContext; + httpTrace("context:%p, fd:%d, free parser", pContext, pContext->fd); - for (size_t i=0; ikvs_count; ++i) { - HttpParseKvObj *p = &parser->kvs[i]; - free(p->key); p->key = NULL; - free(p->val); p->val = NULL; - } - free(parser->kvs); - parser->kvs = NULL; - parser->kvs_count = 0; - - free(parser->auth_basic); - parser->auth_basic = NULL; -} - -void httpParserDestroy(HttpParserObj *parser) { 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->reason_phrase); parser->reason_phrase = NULL; + free(parser->reasonPhrase); 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; + + httpCleanupStack(&parser->stacks); + httpCleanupString(&parser->str); + httpCleanupString(&parser->body); + for (int32_t i = 0; i < HTTP_MAX_URL; ++i) { + httpCleanupString(&parser->path[i]); + } - parser->stacks_count = 0; - - ehttp_parser_kvs_destroy(parser); - - httpParserCleanupString(&parser->str); - if (parser->gzip) { + 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; - HttpParserString str = {0}; +char *httpDecodeUrl(const char *enc) { + int32_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); + int32_t hex, cnt; + int32_t n = sscanf(p+1, "%2x%n", &hex, &cnt); if (n!=1 && cnt !=2) { ok = 0; break; } - if (httpParserAppendString(&str, enc, p-enc)) { ok = 0; break; } + if (httpAppendString(&str, enc, p-enc)) { ok = 0; break; } char c = (char)hex; - if (httpParserAppendString(&str, &c, 1)) { ok = 0; break; } + if (httpAppendString(&str, &c, 1)) { ok = 0; break; } enc = p+3; } char *dec = NULL; if (ok && *enc) { - if (httpParserAppendString(&str, enc, strlen(enc))) { ok = 0; } + if (httpAppendString(&str, enc, strlen(enc))) { ok = 0; } } if (ok) { dec = str.str; str.str = NULL; } - httpParserCleanupString(&str); + httpCleanupString(&str); return dec; } -static void on_data(ehttp_gzip_t *gzip, void *arg, const char *buf, size_t len) { - HttpParserObj *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 (httpParserAppendString(&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; } - httpParserPop(parser); - httpParserPush(parser, HTTP_PARSER_REQUEST_OR_RESPONSE); + httpPopStack(parser); + httpPushStack(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') { - httpParserPop(parser); - httpParserPush(parser, HTTP_PARSER_END); - httpParserPush(parser, HTTP_PARSER_HEADER); - httpParserPush(parser, HTTP_PARSER_CRLF); - httpParserPush(parser, HTTP_PARSER_REASON_PHRASE); - httpParserPush(parser, HTTP_PARSER_SP); - httpParserPush(parser, HTTP_PARSER_STATUS_CODE); - httpParserPush(parser, HTTP_PARSER_SP); - httpParserPush(parser, HTTP_PARSER_HTTP_VERSION); + httpPopStack(parser); + httpPushStack(parser, HTTP_PARSER_END); + httpPushStack(parser, HTTP_PARSER_HEADER); + httpPushStack(parser, HTTP_PARSER_CRLF); + httpPushStack(parser, HTTP_PARSER_REASON_PHRASE); + httpPushStack(parser, HTTP_PARSER_SP); + httpPushStack(parser, HTTP_PARSER_STATUS_CODE); + httpPushStack(parser, HTTP_PARSER_SP); + httpPushStack(parser, HTTP_PARSER_HTTP_VERSION); *again = 1; break; } - httpParserPop(parser); - httpParserPush(parser, HTTP_PARSER_END); - httpParserPush(parser, HTTP_PARSER_HEADER); - httpParserPush(parser, HTTP_PARSER_CRLF); - httpParserPush(parser, HTTP_PARSER_HTTP_VERSION); - httpParserPush(parser, HTTP_PARSER_SP); - httpParserPush(parser, HTTP_PARSER_TARGET); - httpParserPush(parser, HTTP_PARSER_SP); - httpParserPush(parser, HTTP_PARSER_METHOD); + httpPopStack(parser); + httpPushStack(parser, HTTP_PARSER_END); + httpPushStack(parser, HTTP_PARSER_HEADER); + httpPushStack(parser, HTTP_PARSER_CRLF); + httpPushStack(parser, HTTP_PARSER_HTTP_VERSION); + httpPushStack(parser, HTTP_PARSER_SP); + httpPushStack(parser, HTTP_PARSER_TARGET); + httpPushStack(parser, HTTP_PARSER_SP); + httpPushStack(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 (httpParserAppendString(&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); } - httpParserClearString(&parser->str); - httpParserPop(parser); + httpClearString(&parser->str); + httpPopStack(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 (httpParserAppendString(&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; } - httpParserClearString(&parser->str); - httpParserPop(parser); + httpClearString(&parser->str); + httpPopStack(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 (httpParserAppendString(&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 (httpParserAppendString(&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); } - httpParserClearString(&parser->str); - httpParserPop(parser); + httpClearString(&parser->str); + httpPopStack(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 == ' ') { - httpParserPop(parser); + httpPopStack(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 (httpParserAppendString(&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_code); - httpParserClearString(&parser->str); - httpParserPop(parser); + sscanf(parser->str.str, "%d", &parser->statusCode); + httpClearString(&parser->str); + httpPopStack(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_phrase = strdup(parser->str.str); - if (!parser->reason_phrase) { + if (c == '\r') { + parser->reasonPhrase = strdup(parser->str.str); + if (!parser->reasonPhrase) { 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_phrase); - httpParserClearString(&parser->str); - httpParserPop(parser); + ok = httpOnStatusLine(parser, parser->statusCode, parser->reasonPhrase); + httpClearString(&parser->str); + httpPopStack(parser); *again = 1; break; } - if (httpParserAppendString(&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 (httpParserAppendString(&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) { - httpParserClearString(&parser->str); - httpParserPop(parser); - if (httpParserTop(parser) == HTTP_PARSER_END) { - ok = httpParserPostProcess(pContext, parser); + if (parser->str.pos == len) { + httpClearString(&parser->str); + httpPopStack(parser); + if (httpTopStack(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') { - httpParserPop(parser); - if (parser->transfer_chunked) { - httpParserPush(parser, HTTP_PARSER_CHUNK_SIZE); - httpParserPush(parser, HTTP_PARSER_CRLF); + if (c == '\r') { + httpPopStack(parser); + if (parser->transferChunked) { + httpPushStack(parser, HTTP_PARSER_CHUNK_SIZE); + httpPushStack(parser, HTTP_PARSER_CRLF); } else { - if (parser->content_length > 0) { - httpParserPush(parser, HTTP_PARSER_CHUNK); + if (parser->contentLength > 0) { + httpPushStack(parser, HTTP_PARSER_CHUNK); } - httpParserPush(parser, HTTP_PARSER_CRLF); + httpPushStack(parser, HTTP_PARSER_CRLF); } *again = 1; break; } - if (c!=' ' && c!='\t' && c!=':' ) { - if (httpParserAppendString(&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; } - httpParserPush(parser, HTTP_PARSER_CRLF); - httpParserPush(parser, HTTP_PARSER_HEADER_VAL); - httpParserPush(parser, HTTP_PARSER_SP); - httpParserPush(parser, HTTP_PARSER_HEADER_KEY); + httpPushStack(parser, HTTP_PARSER_CRLF); + httpPushStack(parser, HTTP_PARSER_HEADER_VAL); + httpPushStack(parser, HTTP_PARSER_SP); + httpPushStack(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 (httpParserAppendString(&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; } - httpParserClearString(&parser->str); - httpParserPop(parser); + httpClearString(&parser->str); + httpPopStack(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 (httpParserAppendString(&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; - httpParserClearString(&parser->str); - httpParserPop(parser); + httpClearString(&parser->str); + httpPopStack(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; + int32_t n; do { if (isxdigit(c)) { - if (httpParserAppendString(&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_length) { - httpParserClearString(&parser->str); - httpParserPop(parser); - httpParserPush(parser, HTTP_PARSER_CRLF); - httpParserPush(parser, HTTP_PARSER_CRLF); + if (n == 1 && bytes == strlen(parser->str.str) && len >= 0) { + if (len == 0) { + if (parser->contentLengthSpecified == 0 || parser->receivedSize == parser->contentLength) { + httpClearString(&parser->str); + httpPopStack(parser); + httpPushStack(parser, HTTP_PARSER_CRLF); + httpPushStack(parser, HTTP_PARSER_CRLF); *again = 1; break; } } else { - if (parser->content_length_specified == 0 || parser->received_size + len <= parser->content_length) { - parser->chunk_size = len; - httpParserClearString(&parser->str); - httpParserPop(parser); - httpParserPush(parser, HTTP_PARSER_CHUNK_SIZE); - httpParserPush(parser, HTTP_PARSER_CRLF); - httpParserPush(parser, HTTP_PARSER_CHUNK); - httpParserPush(parser, HTTP_PARSER_CRLF); + if (parser->contentLengthSpecified == 0 || parser->receivedSize + len <= parser->contentLength) { + parser->chunkSize = len; + httpClearString(&parser->str); + httpPopStack(parser); + httpPushStack(parser, HTTP_PARSER_CHUNK_SIZE); + httpPushStack(parser, HTTP_PARSER_CRLF); + httpPushStack(parser, HTTP_PARSER_CHUNK); + httpPushStack(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 (httpParserAppendString(&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_size; - ++parser->received_chunk_size; - if (parser->received_chunk_size < parser->chunk_size) break; + ++parser->receivedSize; + ++parser->receivedChunkSize; + if (parser->receivedChunkSize < parser->chunkSize) 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_size = 0; - httpParserClearString(&parser->str); - httpParserPop(parser); - if (httpParserTop(parser) == HTTP_PARSER_END) { - ok = httpParserPostProcess(pContext, parser); + parser->receivedChunkSize = 0; + httpClearString(&parser->str); + httpPopStack(parser); + if (httpTopStack(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 = httpParserTop(parser); + HTTP_PARSER_STATE state = httpTopStack(parser); do { if (state == HTTP_PARSER_BEGIN) { - ok = httpParserOnBegin(pContext, parser, state, c, again); + ok = httpParserOnBegin(parser, state, c, again); break; } if (state == HTTP_PARSER_REQUEST_OR_RESPONSE) { - ok = httpParserOnRquestOrResponse(pContext, parser, state, c, again); + ok = httpParserOnRquestOrResponse(parser, state, c, again); break; } if (state == HTTP_PARSER_METHOD) { - ok = httpParserOnMethod(pContext, parser, state, c, again); + ok = httpParserOnMethod(parser, state, c, again); break; } if (state == HTTP_PARSER_TARGET) { - ok = httpParserOnTarget(pContext, parser, state, c, again); + ok = httpParserOnTarget(parser, state, c, again); break; } if (state == HTTP_PARSER_HTTP_VERSION) { - ok = httpParserOnVersion(pContext, parser, state, c, again); + ok = httpParserOnVersion(parser, state, c, again); break; } if (state == HTTP_PARSER_SP) { - ok = httpParserOnSp(pContext, parser, state, c, again); + ok = httpParserOnSp(parser, state, c, again); break; } if (state == HTTP_PARSER_STATUS_CODE) { - ok = httpParserOnStatusCode(pContext, parser, state, c, again); + ok = httpParserOnStatusCode(parser, state, c, again); break; } if (state == HTTP_PARSER_REASON_PHRASE) { - ok = httpParserOnReasonPhrase(pContext, parser, state, c, again); + ok = httpParserOnReasonPhrase(parser, state, c, again); break; } if (state == HTTP_PARSER_CRLF) { - ok = httpParserOnCrlf(pContext, parser, state, c, again); + ok = httpParserOnCrlf(parser, state, c, again); break; } if (state == HTTP_PARSER_HEADER) { - ok = httpParserOnHeader(pContext, parser, state, c, again); + ok = httpParserOnHeader(parser, state, c, again); break; } if (state == HTTP_PARSER_HEADER_KEY) { - ok = httpParserOnHeaderKey(pContext, parser, state, c, again); + ok = httpParserOnHeaderKey(parser, state, c, again); break; } if (state == HTTP_PARSER_HEADER_VAL) { - ok = httpParserOnHeaderVal(pContext, parser, state, c, again); + ok = httpParserOnHeaderVal(parser, state, c, again); break; } if (state == HTTP_PARSER_CHUNK_SIZE) { - ok = httpParserOnChunkSize(pContext, parser, state, c, again); + ok = httpParserOnChunkSize(parser, state, c, again); break; } if (state == HTTP_PARSER_CHUNK) { - ok = httpParserOnChunk(pContext, parser, state, c, again); + ok = httpParserOnChunk(parser, state, c, again); break; } if (state == HTTP_PARSER_END) { - ok = httpParserOnEnd(pContext, parser, 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); - httpParserPush(parser, HTTP_PARSER_ERROR); + httpError("context:%p, fd:%d, failed to parse, state:%d", pContext, pContext->fd, state); + httpPushStack(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(pContext, parser, *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; - } -} diff --git a/src/plugins/http/src/httpQueue.c b/src/plugins/http/src/httpQueue.c index 9625102f74..76632eb508 100644 --- a/src/plugins/http/src/httpQueue.c +++ b/src/plugins/http/src/httpQueue.c @@ -39,15 +39,15 @@ typedef struct { typedef struct { void *param; void *result; - int numOfRows; - void (*fp)(void *param, void *result, int numOfRows); + int32_t numOfRows; + void (*fp)(void *param, void *result, int32_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, int numOfRows)) { +void httpDispatchToResultQueue(void *param, TAOS_RES *result, int32_t numOfRows, void (*fp)(void *param, void *result, int32_t numOfRows)) { if (tsHttpQueue != NULL) { SHttpResult *pMsg = (SHttpResult *)taosAllocateQitem(sizeof(SHttpResult)); pMsg->param = param; diff --git a/src/plugins/http/src/httpResp.c b/src/plugins/http/src/httpResp.c index a7c17dfdbb..def8ecf5e1 100644 --- a/src/plugins/http/src/httpResp.c +++ b/src/plugins/http/src/httpResp.c @@ -45,23 +45,23 @@ const char *httpRespTemplate[] = { "%s 200 OK\r\nAccess-Control-Allow-Origin:*\r\n%sAccess-Control-Allow-Methods:POST, GET, OPTIONS, DELETE, PUT\r\nAccess-Control-Allow-Headers:Accept, Content-Type\r\nContent-Type: application/json;charset=utf-8\r\nContent-Length: %d\r\n\r\n" }; -static void httpSendErrorRespImp(HttpContext *pContext, int httpCode, char *httpCodeStr, int errNo, char *desc) { +static void httpSendErrorRespImp(HttpContext *pContext, int32_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->httpKeepAlive], bodyLen); + int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_ERROR], errNo, desc); + int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_ERROR], httpVersionStr[pContext->parser->httpVersion], + httpCode, httpCodeStr, httpKeepAliveStr[pContext->parser->keepAlive], 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, int32_t errNo, char *desc) { + int32_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, int32_t errNo) { httpSendErrorRespWithDesc(pContext, errNo, NULL); } -void httpSendTaosdErrorResp(HttpContext *pContext, int errCode) { - int httpCode = 400; +void httpSendTaosdErrorResp(HttpContext *pContext, int32_t errCode) { + int32_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) { + int32_t httpCode = 400; + char temp[512] = {0}; + int32_t len = sprintf(temp, "invalid SQL: %s", errMsg); - for (int i = 0; i < len; ++i) { + for (int32_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->httpKeepAlive], bodyLen); + int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_OK], HTTP_SUCCESS, desc); + int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_OK], httpVersionStr[pContext->parser->httpVersion], + httpKeepAliveStr[pContext->parser->keepAlive], 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->httpKeepAlive], bodyLen); + int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_OK], HTTP_SUCCESS, desc); + int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_OPTIONS], httpVersionStr[pContext->parser->httpVersion], + httpKeepAliveStr[pContext->parser->keepAlive], bodyLen); httpWriteBuf(pContext, head, headLen); httpWriteBuf(pContext, body, bodyLen); diff --git a/src/plugins/http/src/httpRestHandle.c b/src/plugins/http/src/httpRestHandle.c index b285b19c3f..fbdce566f0 100644 --- a/src/plugins/http/src/httpRestHandle.c +++ b/src/plugins/http/src/httpRestHandle.c @@ -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, int32_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; diff --git a/src/plugins/http/src/httpRestJson.c b/src/plugins/http/src/httpRestJson.c index c16727faa0..26f0441519 100644 --- a/src/plugins/http/src/httpRestJson.c +++ b/src/plugins/http/src/httpRestJson.c @@ -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, int32_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); + int32_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 (int32_t i = 0; i < num_fields; ++i) { httpJsonItemToken(jsonBuf); - httpJsonString(jsonBuf, fields[i].name, (int)strlen(fields[i].name)); + httpJsonString(jsonBuf, fields[i].name, (int32_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, int timestampFormat) { +bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows, int32_t timestampFormat) { JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); if (jsonBuf == NULL) return false; cmd->numOfRows += numOfRows; - int num_fields = taos_num_fields(result); + int32_t num_fields = taos_num_fields(result); TAOS_FIELD *fields = taos_fetch_fields(result); - for (int k = 0; k < numOfRows; ++k) { + for (int32_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 (int32_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, int32_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, int32_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, int32_t numOfRows) { return restBuildSqlJson(pContext,cmd, result, numOfRows, REST_TIMESTAMP_FMT_UTC_STRING); } diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index e0d2a90994..212ce46473 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -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 (int32_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; + int32_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 (int32_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; + int32_t connFd = -1; struct sockaddr_in clientAddr; - int threadId = 0; + int32_t threadId = 0; HttpServer * pServer = &tsHttpServer; HttpThread * pThread = NULL; HttpContext * pContext = NULL; - int totalFds = 0; + int32_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 = (int32_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 (int32_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 (int32_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 = (int)taosReadSocket(pContext->fd, buf, sizeof(buf)); + char buf[HTTP_STEP_SIZE + 1] = {0}; + int32_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->parser, buf, nread); + int32_t ok = httpParseBuf(pParser, 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 (pContext->parser.failed) { - httpError("context:%p, fd:%d, parse failed, close connect", pContext, pContext->fd); + if (pParser->parseCode) { + httpError("context:%p, fd:%d, parse failed, code:%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); diff --git a/src/plugins/http/src/httpSystem.c b/src/plugins/http/src/httpSystem.c index ee43b3c826..8993b233dd 100644 --- a/src/plugins/http/src/httpSystem.c +++ b/src/plugins/http/src/httpSystem.c @@ -37,9 +37,9 @@ void opInitHandle(HttpServer* pServer) {} #endif HttpServer tsHttpServer; -void taosInitNote(int numOfNoteLines, int maxNotes, char* lable); +void taosInitNote(int32_t numOfNoteLines, int32_t maxNotes, char* lable); -int httpInitSystem() { +int32_t httpInitSystem() { strcpy(tsHttpServer.label, "rest"); tsHttpServer.serverIp = 0; tsHttpServer.serverPort = tsHttpPort; @@ -60,7 +60,7 @@ int httpInitSystem() { return 0; } -int httpStartSystem() { +int32_t httpStartSystem() { httpInfo("start http server ..."); if (tsHttpServer.status != HTTP_SERVER_INIT) { diff --git a/src/plugins/http/src/httpTgHandle.c b/src/plugins/http/src/httpTgHandle.c index a7444676ae..83b31f652b 100644 --- a/src/plugins/http/src/httpTgHandle.c +++ b/src/plugins/http/src/httpTgHandle.c @@ -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; + int32_t fieldNum; } STgSchema; typedef struct { STgSchema *schemas; - int size; - int pos; + int32_t size; + int32_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 (int32_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 (int32_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(int32_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 = (int)strlen(name->valuestring); + int32_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 = (int)strlen(tbname->valuestring); + int32_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); + int32_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 (int32_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 = (int)strlen(field->valuestring); + int32_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) { +int32_t tgParseSchema(const char *content, char*fileName) { cJSON *root = cJSON_Parse(content); if (root == NULL) { httpError("failed to parse telegraf schema file:%s, invalid json format, content:%s", fileName, content); return -1; } - int size = 0; + int32_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 (int32_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) { +int32_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); + int32_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, int32_t fieldsSize) { + for (int32_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 (int32_t f = 0; f < schema->fieldNum; ++f) { char *fieldName = schema->fields[f]; bool fieldMatched = false; - for (int i = 0; i < fieldsSize; i++) { + for (int32_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 = (int)strlen(name->valuestring); + int32_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); + int32_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 (int32_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); + int32_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 (int32_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) { + int32_t orderTagsLen = 0; + for (int32_t i = 0; i < tagsSize; ++i) { cJSON *tag = cJSON_GetArrayItem(tags, i); orderedTags[orderTagsLen++] = tag; - for (int j = orderTagsLen - 1; j >= 1; --j) { + for (int32_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 (int32_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 (int32_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 (int32_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 (int32_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 (int32_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 (int32_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); + int32_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; + int32_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 (int32_t i = 0; i < size; i++) { cJSON *metric = cJSON_GetArrayItem(metrics, i); if (metric != NULL) { if (!tgProcessSingleMetric(pContext, metric, db)) { diff --git a/src/plugins/http/src/httpTgJson.c b/src/plugins/http/src/httpTgJson.c index 5c6985cd69..603092f09d 100644 --- a/src/plugins/http/src/httpTgJson.c +++ b/src/plugins/http/src/httpTgJson.c @@ -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))); + (int32_t)strlen(httpGetCmdsString(pContext, cmd->metric))); httpJsonItemToken(jsonBuf); httpJsonPair(jsonBuf, "stable", 6, httpGetCmdsString(pContext, cmd->stable), - (int)strlen(httpGetCmdsString(pContext, cmd->stable))); + (int32_t)strlen(httpGetCmdsString(pContext, cmd->stable))); httpJsonItemToken(jsonBuf); httpJsonPair(jsonBuf, "table", 5, httpGetCmdsString(pContext, cmd->table), - (int)strlen(httpGetCmdsString(pContext, cmd->table))); + (int32_t)strlen(httpGetCmdsString(pContext, cmd->table))); httpJsonItemToken(jsonBuf); httpJsonPair(jsonBuf, "timestamp", 9, httpGetCmdsString(pContext, cmd->timestamp), - (int)strlen(httpGetCmdsString(pContext, cmd->timestamp))); // hack way + (int32_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, int32_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, int32_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, int32_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, diff --git a/src/plugins/http/src/httpUtil.c b/src/plugins/http/src/httpUtil.c index f6e05dc1ae..2c8879f880 100644 --- a/src/plugins/http/src/httpUtil.c +++ b/src/plugins/http/src/httpUtil.c @@ -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, int32_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; + int32_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; + int32_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; + int32_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; + int32_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; + int32_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, int32_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, int bufferSize) { +bool httpMallocMultiCmds(HttpContext *pContext, int32_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, int32_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, int32_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; + int32_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) { +int32_t httpNextSqlCmdPos(HttpContext *pContext) { HttpSqlCmds *multiCmds = pContext->multiCmds; return multiCmds->size; } void httpTrimTableName(char *name) { - for (int i = 0; name[i] != 0; i++) { + for (int32_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, int pos, char *name) { - int len = 0; - for (int i = 0; name[i] != 0; i++) { +int32_t httpShrinkTableName(HttpContext *pContext, int32_t pos, char *name) { + int32_t len = 0; + for (int32_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( + int32_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, int32_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; +int32_t httpGzipDeCompress(char *srcData, int32_t nSrcData, char *destData, int32_t *nDestData) { + int32_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) { +int32_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; +int32_t httpGzipCompress(HttpContext *pContext, char *srcData, int32_t nSrcData, char *destData, int32_t *nDestData, bool isTheLast) { + int32_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; +} From 7dd32d26cf843f29ec75ebc111161525e5f9eb4b Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 14 Sep 2020 06:49:00 +0000 Subject: [PATCH 13/30] TD-1311 --- src/inc/taoserror.h | 121 +++++++++++---- src/plugins/http/inc/httpCode.h | 112 -------------- src/plugins/http/inc/httpInt.h | 3 +- src/plugins/http/inc/httpResp.h | 2 - src/plugins/http/src/httpCode.c | 108 ------------- src/plugins/http/src/httpGcHandle.c | 17 ++- src/plugins/http/src/httpGcJson.c | 26 ++-- src/plugins/http/src/httpJson.c | 1 - src/plugins/http/src/httpParser.c | 2 +- src/plugins/http/src/httpResp.c | 210 ++++++++++---------------- src/plugins/http/src/httpRestHandle.c | 9 +- src/plugins/http/src/httpServer.c | 2 +- src/plugins/http/src/httpSession.c | 3 +- src/plugins/http/src/httpSql.c | 15 +- src/plugins/http/src/httpTgHandle.c | 80 +++++----- 15 files changed, 255 insertions(+), 456 deletions(-) delete mode 100644 src/plugins/http/inc/httpCode.h delete mode 100644 src/plugins/http/src/httpCode.c diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index 3f7995f25e..d8bd293dba 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -248,37 +248,104 @@ TAOS_DEFINE_ERROR(TSDB_CODE_SYN_NOT_ENABLED, 0, 0x0901, "Sync modul 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") - - +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_SERVER_OFFLINE, 0, 0x1100, "http server is not onlin") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_UNSUPPORT_URL, 0, 0x1101, "url is not support") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_INVLALID_URL, 0, 0x1102, "invalid url format") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_NO_ENOUGH_MEMORY, 0, 0x1103, "no enough memory") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_REQUSET_TOO_BIG, 0, 0x1104, "request size is too big") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_NO_AUTH_INFO, 0, 0x1105, "no auth info input") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_NO_MSG_INPUT, 0, 0x1106, "request is empty") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_NO_SQL_INPUT, 0, 0x1107, "no sql input") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_NO_EXEC_USEDB, 0, 0x1108, "no need to execute use db cmd") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_SESSION_FULL, 0, 0x1109, "session list was full") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_GEN_TAOSD_TOKEN_ERR, 0, 0x110A, "generate taosd token error") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_INVALID_MULTI_REQUEST, 0, 0x110B, "size of multi request is 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_CREATE_GZIP_FAILED, 0, 0x110C, "failed to create gzip") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_FINISH_GZIP_FAILED, 0, 0x110D, "failed to finish gzip") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_LOGIN_FAILED, 0, 0x110E, "failed to login") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_INVALID_VERSION, 0, 0x1120, "invalid http version") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_INVALID_CONTENT_LENGTH, 0, 0x1121, "invalid content length") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_INVALID_AUTH_TYPE, 0, 0x1122, "invalid type of Authorization") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_INVALID_AUTH_FORMAT, 0, 0x1123, "invalid format of Authorization") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_INVALID_BASIC_AUTH, 0, 0x1124, "invalid basic Authorization") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_INVALID_TAOSD_AUTH, 0, 0x1125, "invalid taosd Authorization") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_METHOD_FAILED, 0, 0x1126, "failed to parse method") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_TARGET_FAILED, 0, 0x1127, "failed to parse target") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_VERSION_FAILED, 0, 0x1128, "failed to parse http version") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_SP_FAILED, 0, 0x1129, "failed to parse sp") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_STATUS_FAILED, 0, 0x112A, "failed to parse status") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_PHRASE_FAILED, 0, 0x112B, "failed to parse phrase") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_CRLF_FAILED, 0, 0x112C, "failed to parse crlf") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_HEADER_FAILED, 0, 0x112D, "failed to parse header") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_HEADER_KEY_FAILED, 0, 0x112E, "failed to parse header key") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_HEADER_VAL_FAILED, 0, 0x112F, "failed to parse header val") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_CHUNK_SIZE_FAILED, 0, 0x1130, "failed to parse chunk size") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_CHUNK_FAILED, 0, 0x1131, "failed to parse chunk") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_END_FAILED, 0, 0x1132, "failed to parse end section") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_INVALID_STATE, 0, 0x1134, "invalid parse state") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_PARSE_ERROR_STATE, 0, 0x1135, "failed to parse error section") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_GC_QUERY_NULL, 0, 0x1150, "query size is 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_GC_QUERY_SIZE, 0, 0x1151, "query size can not more than 100") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_GC_REQ_PARSE_ERROR, 0, 0x1152, "parse grafana json error") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_DB_NOT_INPUT, 0, 0x1160, "database name can not be null") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_DB_TOO_LONG, 0, 0x1161, "database name too long") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_INVALID_JSON, 0, 0x1162, "invalid telegraf json fromat") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_METRICS_NULL, 0, 0x1163, "metrics size is 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_METRICS_SIZE, 0, 0x1164, "metrics size can not more than 1K") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_METRIC_NULL, 0, 0x1165, "metric name not find") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_METRIC_TYPE, 0, 0x1166, "metric name type should be string") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_METRIC_NAME_NULL, 0, 0x1167, "metric name length is 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_METRIC_NAME_LONG, 0, 0x1168, "metric name length too long") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TIMESTAMP_NULL, 0, 0x1169, "timestamp not find") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TIMESTAMP_TYPE, 0, 0x116A, "timestamp type should be integer") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TIMESTAMP_VAL_NULL, 0, 0x116B, "timestamp value smaller than 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAGS_NULL, 0, 0x116C, "tags not find") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAGS_SIZE_0, 0, 0x116D, "tags size too long") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAGS_SIZE_LONG, 0, 0x116E, "tags size is 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAG_NULL, 0, 0x116F, "tag is null") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAG_NAME_NULL, 0, 0x1170, "tag name is null") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAG_NAME_SIZE, 0, 0x1171, "tag name length too long") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAG_VALUE_TYPE, 0, 0x1172, "tag value type should be number or string") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAG_VALUE_NULL, 0, 0x1173, "tag value is null") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TABLE_NULL, 0, 0x1174, "table is null") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TABLE_SIZE, 0, 0x1175, "table name length too long") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_FIELDS_NULL, 0, 0x1176, "fields not find") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_FIELDS_SIZE_0, 0, 0x1177, "fields size is 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_FIELDS_SIZE_LONG, 0, 0x1178, "fields size too long") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_FIELD_NULL, 0, 0x1179, "field is null") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_FIELD_NAME_NULL, 0, 0x117A, "field name is null") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_FIELD_NAME_SIZE, 0, 0x117B, "field name length too long") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_FIELD_VALUE_TYPE, 0, 0x117C, "field value type should be number or string") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_FIELD_VALUE_NULL, 0, 0x117D, "field value is null") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_HOST_NOT_STRING, 0, 0x117E, "host type should be string") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_STABLE_NOT_EXIST, 0, 0x117F, "stable not exist") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_DB_NOT_INPUT, 0, 0x1190, "database name can not be null") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_DB_TOO_LONG, 0, 0x1191, "database name too long") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_INVALID_JSON, 0, 0x1192, "invalid opentsdb json fromat") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_METRICS_NULL, 0, 0x1193, "metrics size is 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_METRICS_SIZE, 0, 0x1194, "metrics size can not more than 10K") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_METRIC_NULL, 0, 0x1195, "metric name not find") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_METRIC_TYPE, 0, 0x1196, "metric name type should be string") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_METRIC_NAME_NULL, 0, 0x1197, "metric name length is 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_METRIC_NAME_LONG, 0, 0x1198, "metric name length can not more than 22") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TIMESTAMP_NULL, 0, 0x1199, "timestamp not find") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TIMESTAMP_TYPE, 0, 0x119A, "timestamp type should be integer") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TIMESTAMP_VAL_NULL, 0, 0x119B, "timestamp value smaller than 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAGS_NULL, 0, 0x119C, "tags not find") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAGS_SIZE_0, 0, 0x119D, "tags size is 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAGS_SIZE_LONG, 0, 0x119E, "tags size too long") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAG_NULL, 0, 0x119F, "tag is null") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAG_NAME_NULL, 0, 0x11A0, "tag name is null") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAG_NAME_SIZE, 0, 0x11A1, "tag name length too long") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAG_VALUE_TYPE, 0, 0x11A2, "tag value type should be boolean, number or string") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAG_VALUE_NULL, 0, 0x11A3, "tag value is null") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAG_VALUE_TOO_LONG, 0, 0x11A4, "tag value can not more than 64") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_VALUE_NULL, 0, 0x11A5, "value not find") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_VALUE_TYPE, 0, 0x11A5, "value type should be boolean, number or string") #ifdef TAOS_ERROR_C }; diff --git a/src/plugins/http/inc/httpCode.h b/src/plugins/http/inc/httpCode.h deleted file mode 100644 index 08111260e9..0000000000 --- a/src/plugins/http/inc/httpCode.h +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_HTTP_CODE_H -#define TDENGINE_HTTP_CODE_H - -//for fixed msg info -#define HTTP_SUCCESS 0 -#define HTTP_SERVER_OFFLINE 1 -#define HTTP_UNSUPPORT_URL 2 -#define HTTP_PARSE_HTTP_METHOD_ERROR 3 -#define HTTP_PARSE_HTTP_VERSION_ERROR 4 -#define HTTP_PARSE_HEAD_ERROR 5 -#define HTTP_REQUSET_TOO_BIG 6 -#define HTTP_PARSE_BODY_ERROR 7 -#define HTTP_PARSE_CHUNKED_BODY_ERROR 8 -#define HTTP_PARSE_URL_ERROR 9 -#define HTTP_INVALID_AUTH_TOKEN 10 -#define HTTP_PARSE_USR_ERROR 11 -#define HTTP_NO_SQL_INPUT 12 -#define HTTP_SESSION_FULL 13 -#define HTTP_NO_ENOUGH_MEMORY 14 -#define HTTP_GEN_TAOSD_TOKEN_ERR 15 -#define HTTP_INVALID_DB_TABLE 16 -#define HTTP_NO_EXEC_USEDB 17 -#define HTTP_PARSE_GC_REQ_ERROR 18 -#define HTTP_INVALID_MULTI_REQUEST 19 -#define HTTP_NO_MSG_INPUT 20 -#define HTTP_NO_ENOUGH_SESSIONS 21 - -//telegraf -#define HTTP_TG_DB_NOT_INPUT 22 -#define HTTP_TG_DB_TOO_LONG 23 -#define HTTP_TG_INVALID_JSON 24 -#define HTTP_TG_METRICS_NULL 25 -#define HTTP_TG_METRICS_SIZE 26 -#define HTTP_TG_METRIC_NULL 27 -#define HTTP_TG_METRIC_TYPE 28 -#define HTTP_TG_METRIC_NAME_NULL 29 -#define HTTP_TG_METRIC_NAME_LONG 30 -#define HTTP_TG_TIMESTAMP_NULL 31 -#define HTTP_TG_TIMESTAMP_TYPE 32 -#define HTTP_TG_TIMESTAMP_VAL_NULL 33 -#define HTTP_TG_TAGS_NULL 34 -#define HTTP_TG_TAGS_SIZE_0 35 -#define HTTP_TG_TAGS_SIZE_LONG 36 -#define HTTP_TG_TAG_NULL 37 -#define HTTP_TG_TAG_NAME_NULL 38 -#define HTTP_TG_TAG_NAME_SIZE 39 -#define HTTP_TG_TAG_VALUE_TYPE 40 -#define HTTP_TG_TAG_VALUE_NULL 41 -#define HTTP_TG_TABLE_NULL 42 -#define HTTP_TG_TABLE_SIZE 43 -#define HTTP_TG_FIELDS_NULL 44 -#define HTTP_TG_FIELDS_SIZE_0 45 -#define HTTP_TG_FIELDS_SIZE_LONG 46 -#define HTTP_TG_FIELD_NULL 47 -#define HTTP_TG_FIELD_NAME_NULL 48 -#define HTTP_TG_FIELD_NAME_SIZE 49 -#define HTTP_TG_FIELD_VALUE_TYPE 50 -#define HTTP_TG_FIELD_VALUE_NULL 51 -#define HTTP_INVALID_BASIC_AUTH_TOKEN 52 -#define HTTP_INVALID_TAOSD_AUTH_TOKEN 53 -#define HTTP_TG_HOST_NOT_STRING 54 - -//grafana -#define HTTP_GC_QUERY_NULL 55 -#define HTTP_GC_QUERY_SIZE 56 - -//opentsdb -#define HTTP_OP_DB_NOT_INPUT 57 -#define HTTP_OP_DB_TOO_LONG 58 -#define HTTP_OP_INVALID_JSON 59 -#define HTTP_OP_METRICS_NULL 60 -#define HTTP_OP_METRICS_SIZE 61 -#define HTTP_OP_METRIC_NULL 62 -#define HTTP_OP_METRIC_TYPE 63 -#define HTTP_OP_METRIC_NAME_NULL 64 -#define HTTP_OP_METRIC_NAME_LONG 65 -#define HTTP_OP_TIMESTAMP_NULL 66 -#define HTTP_OP_TIMESTAMP_TYPE 67 -#define HTTP_OP_TIMESTAMP_VAL_NULL 68 -#define HTTP_OP_TAGS_NULL 69 -#define HTTP_OP_TAGS_SIZE_0 70 -#define HTTP_OP_TAGS_SIZE_LONG 71 -#define HTTP_OP_TAG_NULL 72 -#define HTTP_OP_TAG_NAME_NULL 73 -#define HTTP_OP_TAG_NAME_SIZE 74 -#define HTTP_OP_TAG_VALUE_TYPE 75 -#define HTTP_OP_TAG_VALUE_NULL 76 -#define HTTP_OP_TAG_VALUE_TOO_LONG 77 -#define HTTP_OP_VALUE_NULL 78 -#define HTTP_OP_VALUE_TYPE 79 - -//tgf -#define HTTP_TG_STABLE_NOT_EXIST 80 - -extern char *httpMsg[]; - -#endif \ No newline at end of file diff --git a/src/plugins/http/inc/httpInt.h b/src/plugins/http/inc/httpInt.h index 86e1eeb6d6..38a52356f8 100644 --- a/src/plugins/http/inc/httpInt.h +++ b/src/plugins/http/inc/httpInt.h @@ -24,7 +24,6 @@ #include "tutil.h" #include "zlib.h" #include "http.h" -#include "httpCode.h" #include "httpLog.h" #include "httpJson.h" #include "httpParser.h" @@ -37,7 +36,7 @@ #define HTTP_BUFFER_SIZE 8192000 #define HTTP_STEP_SIZE 1024 //http message get process step by step #define HTTP_METHOD_SCANNER_SIZE 7 //http method fp size -#define HTTP_GC_TARGET_SIZE 512 +#define TSDB_CODE_HTTP_GC_TARGET_SIZE 512 #define HTTP_WRITE_RETRY_TIMES 500 #define HTTP_WRITE_WAIT_TIME_MS 5 #define HTTP_SESSION_ID_LEN (TSDB_USER_LEN + TSDB_PASSWORD_LEN) diff --git a/src/plugins/http/inc/httpResp.h b/src/plugins/http/inc/httpResp.h index 9086d0c9d3..a528bcc39e 100644 --- a/src/plugins/http/inc/httpResp.h +++ b/src/plugins/http/inc/httpResp.h @@ -33,8 +33,6 @@ enum _httpRespTempl { extern const char *httpRespTemplate[]; void httpSendErrorResp(HttpContext *pContext, int32_t errNo); -void httpSendErrorRespWithDesc(HttpContext *pContext, int32_t errNo, char *desc); -void httpSendTaosdErrorResp(HttpContext *pContext, int32_t errCode); void httpSendTaosdInvalidSqlErrorResp(HttpContext *pContext, char* errMsg); void httpSendSuccResp(HttpContext *pContext, char *desc); void httpSendOptionResp(HttpContext *pContext, char *desc); diff --git a/src/plugins/http/src/httpCode.c b/src/plugins/http/src/httpCode.c deleted file mode 100644 index 9ec07fd851..0000000000 --- a/src/plugins/http/src/httpCode.c +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE - - -char* httpMsg[] = { - "success", // 0 - "http server is not online", // 1 - "http url is not support", // 2 - "http method parse error", // 3 - "http version should be 1.0, 1.1 or 1.2", // 4 - "http head parse error", // 5 - "request size is too big", - "http body size invalid", - "http chunked body parse error", // 8 - "http url parse error", // 9 - "invalid type of Authorization", - "no auth info input", - "no sql input", - "session list was full", - "no enough memory to alloc sqls", - "generate taosd token error", - "db and table can not be null", - "no need to execute use db cmd", - "parse grafana json error", - "size of multi request is 0", // 19 - "request is empty", // 20 - "no enough connections for http", // 21 - - // telegraf - "database name can not be null", // 22 - "database name too long", - "invalid telegraf json fromat", - "metrics size is 0", - "metrics size can not more than 1K", // 26 - "metric name not find", - "metric name type should be string", - "metric name length is 0", - "metric name length too long", - "timestamp not find", // 31 - "timestamp type should be integer", - "timestamp value smaller than 0", - "tags not find", - "tags size is 0", - "tags size too long", // 36 - "tag is null", - "tag name is null", - "tag name length too long", // 39 - "tag value type should be number or string", - "tag value is null", - "table is null", // 42 - "table name length too long", - "fields not find", // 44 - "fields size is 0", - "fields size too long", - "field is null", // 47 - "field name is null", - "field name length too long", // 49 - "field value type should be number or string", - "field value is null", // 51 - "parse basic auth token error", - "parse http auth token error", - "host type should be string", - - // grafana - "query size is 0", // 55 - "query size can not more than 100", - - // opentsdb - "database name can not be null", // 57 - "database name too long", - "invalid opentsdb json fromat", // 59 - "metrics size is 0", - "metrics size can not more than 10K", // 61 - "metric name not find", - "metric name type should be string", - "metric name length is 0", - "metric name length can not more than 22", - "timestamp not find", - "timestamp type should be integer", - "timestamp value smaller than 0", - "tags not find", - "tags size is 0", - "tags size too long", // 71 - "tag is null", - "tag name is null", - "tag name length too long", // 74 - "tag value type should be boolean, number or string", - "tag value is null", - "tag value can not more than 64", // 77 - "value not find", - "value type should be boolean, number or string", - "stable not exist", - -}; diff --git a/src/plugins/http/src/httpGcHandle.c b/src/plugins/http/src/httpGcHandle.c index e010c77ffd..01be301637 100644 --- a/src/plugins/http/src/httpGcHandle.c +++ b/src/plugins/http/src/httpGcHandle.c @@ -16,6 +16,7 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taosdef.h" +#include "taoserror.h" #include "cJSON.h" #include "httpLog.h" #include "httpGcHandle.h" @@ -146,31 +147,31 @@ bool gcProcessQueryRequest(HttpContext* pContext) { char* filter = pContext->parser->body.str; if (filter == NULL) { - httpSendErrorResp(pContext, HTTP_NO_MSG_INPUT); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_MSG_INPUT); return false; } cJSON* root = cJSON_Parse(filter); if (root == NULL) { - httpSendErrorResp(pContext, HTTP_PARSE_GC_REQ_ERROR); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_GC_REQ_PARSE_ERROR); return false; } int32_t size = cJSON_GetArraySize(root); if (size <= 0) { - httpSendErrorResp(pContext, HTTP_GC_QUERY_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_GC_QUERY_NULL); cJSON_Delete(root); return false; } if (size > 100) { - httpSendErrorResp(pContext, HTTP_GC_QUERY_SIZE); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_GC_QUERY_SIZE); cJSON_Delete(root); return false; } if (!httpMallocMultiCmds(pContext, size, HTTP_BUFFER_SIZE)) { - httpSendErrorResp(pContext, HTTP_NO_ENOUGH_MEMORY); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); cJSON_Delete(root); return false; } @@ -218,7 +219,7 @@ bool gcProcessQueryRequest(HttpContext* pContext) { HttpSqlCmd* cmd = httpNewSqlCmd(pContext); if (cmd == NULL) { - httpSendErrorResp(pContext, HTTP_NO_ENOUGH_MEMORY); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); cJSON_Delete(root); return false; } @@ -227,7 +228,7 @@ bool gcProcessQueryRequest(HttpContext* pContext) { cmd->values = refIdBuffer; cmd->table = aliasBuffer; cmd->numOfRows = 0; // hack way as target flags - cmd->timestamp = httpAddToSqlCmdBufferWithSize(pContext, HTTP_GC_TARGET_SIZE + 1); // hack way + cmd->timestamp = httpAddToSqlCmdBufferWithSize(pContext, TSDB_CODE_HTTP_GC_TARGET_SIZE + 1); // hack way if (cmd->timestamp == -1) { httpWarn("context:%p, fd:%d, user:%s, cant't malloc target size, sql buffer is full", pContext, pContext->fd, @@ -260,7 +261,7 @@ bool gcProcessRequest(struct HttpContext* pContext) { } if (strlen(pContext->user) == 0 || strlen(pContext->pass) == 0) { - httpSendErrorResp(pContext, HTTP_PARSE_USR_ERROR); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_AUTH_INFO); return false; } diff --git a/src/plugins/http/src/httpGcJson.c b/src/plugins/http/src/httpGcJson.c index a291641dc3..e864d54ac0 100644 --- a/src/plugins/http/src/httpGcJson.c +++ b/src/plugins/http/src/httpGcJson.c @@ -129,48 +129,48 @@ bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, // for group by if (groupFields != -1) { - char target[HTTP_GC_TARGET_SIZE] = {0}; + char target[TSDB_CODE_HTTP_GC_TARGET_SIZE] = {0}; int32_t len; - len = snprintf(target,HTTP_GC_TARGET_SIZE,"%s{",aliasBuffer); + len = snprintf(target,TSDB_CODE_HTTP_GC_TARGET_SIZE,"%s{",aliasBuffer); for (int32_t i = dataFields + 1; i= 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); + httpOnError(parser, 0, TSDB_CODE_HTTP_REQUSET_TOO_BIG); return -1; } else { int32_t newSize = buf->size * 10; diff --git a/src/plugins/http/src/httpResp.c b/src/plugins/http/src/httpResp.c index def8ecf5e1..77dfdb1bf4 100644 --- a/src/plugins/http/src/httpResp.c +++ b/src/plugins/http/src/httpResp.c @@ -19,7 +19,6 @@ #include "taosmsg.h" #include "httpLog.h" #include "httpResp.h" -#include "httpCode.h" #include "httpJson.h" #include "httpContext.h" @@ -45,7 +44,7 @@ const char *httpRespTemplate[] = { "%s 200 OK\r\nAccess-Control-Allow-Origin:*\r\n%sAccess-Control-Allow-Methods:POST, GET, OPTIONS, DELETE, PUT\r\nAccess-Control-Allow-Headers:Accept, Content-Type\r\nContent-Type: application/json;charset=utf-8\r\nContent-Length: %d\r\n\r\n" }; -static void httpSendErrorRespImp(HttpContext *pContext, int32_t httpCode, char *httpCodeStr, int32_t errNo, char *desc) { +static void httpSendErrorRespImp(HttpContext *pContext, int32_t httpCode, char *httpCodeStr, int32_t errNo, const char *desc) { httpError("context:%p, fd:%d, code:%d, error:%s", pContext, pContext->fd, httpCode, desc); char head[512] = {0}; @@ -60,132 +59,85 @@ static void httpSendErrorRespImp(HttpContext *pContext, int32_t httpCode, char * httpCloseContextByApp(pContext); } -void httpSendErrorRespWithDesc(HttpContext *pContext, int32_t errNo, char *desc) { - int32_t httpCode = 500; - char *httpCodeStr = "Internal Server Error"; - switch (errNo) { - case HTTP_SUCCESS: - httpCode = 200; - httpCodeStr = "OK"; - break; - case HTTP_SERVER_OFFLINE: - case HTTP_UNSUPPORT_URL: - httpCode = 404; - httpCodeStr = "Not Found"; - break; - case HTTP_PARSE_HTTP_METHOD_ERROR: - httpCode = 405; - httpCodeStr = "Method Not Allowed"; - break; - case HTTP_PARSE_HTTP_VERSION_ERROR: - httpCode = 505; - httpCodeStr = "HTTP Version Not Supported"; - break; - case HTTP_PARSE_HEAD_ERROR: - httpCode = 406; - httpCodeStr = "Not Acceptable"; - break; - case HTTP_REQUSET_TOO_BIG: - httpCode = 413; - httpCodeStr = "Request Entity Too Large"; - break; - case HTTP_PARSE_BODY_ERROR: - case HTTP_PARSE_CHUNKED_BODY_ERROR: - httpCode = 409; - httpCodeStr = "Conflict"; - break; - case HTTP_PARSE_URL_ERROR: - httpCode = 414; - httpCodeStr = "Request-URI Invalid"; - break; - case HTTP_INVALID_AUTH_TOKEN: - case HTTP_PARSE_USR_ERROR: - httpCode = 401; - httpCodeStr = "Unauthorized"; - break; - case HTTP_NO_SQL_INPUT: - httpCode = 400; - httpCodeStr = "Bad Request"; - break; - case HTTP_SESSION_FULL: - httpCode = 421; - httpCodeStr = "Too many connections"; - break; - case HTTP_NO_ENOUGH_MEMORY: - case HTTP_GEN_TAOSD_TOKEN_ERR: - httpCode = 507; - httpCodeStr = "Insufficient Storage"; - break; - case HTTP_INVALID_DB_TABLE: - case HTTP_NO_EXEC_USEDB: - case HTTP_PARSE_GC_REQ_ERROR: - case HTTP_INVALID_MULTI_REQUEST: - case HTTP_NO_MSG_INPUT: - httpCode = 400; - httpCodeStr = "Bad Request"; - break; - case HTTP_NO_ENOUGH_SESSIONS: - httpCode = 421; - httpCodeStr = "Too many connections"; - break; - // telegraf - case HTTP_TG_DB_NOT_INPUT: - case HTTP_TG_DB_TOO_LONG: - case HTTP_TG_INVALID_JSON: - case HTTP_TG_METRICS_NULL: - case HTTP_TG_METRICS_SIZE: - case HTTP_TG_METRIC_NULL: - case HTTP_TG_METRIC_TYPE: - case HTTP_TG_METRIC_NAME_NULL: - case HTTP_TG_METRIC_NAME_LONG: - case HTTP_TG_TIMESTAMP_NULL: - case HTTP_TG_TIMESTAMP_TYPE: - case HTTP_TG_TIMESTAMP_VAL_NULL: - case HTTP_TG_TAGS_NULL: - case HTTP_TG_TAGS_SIZE_0: - case HTTP_TG_TAGS_SIZE_LONG: - case HTTP_TG_TAG_NULL: - case HTTP_TG_TAG_NAME_NULL: - case HTTP_TG_TAG_NAME_SIZE: - case HTTP_TG_TAG_VALUE_TYPE: - case HTTP_TG_TAG_VALUE_NULL: - case HTTP_TG_TABLE_NULL: - case HTTP_TG_TABLE_SIZE: - case HTTP_TG_FIELDS_NULL: - case HTTP_TG_FIELDS_SIZE_0: - case HTTP_TG_FIELDS_SIZE_LONG: - case HTTP_TG_FIELD_NULL: - case HTTP_TG_FIELD_NAME_NULL: - case HTTP_TG_FIELD_NAME_SIZE: - case HTTP_TG_FIELD_VALUE_TYPE: - case HTTP_TG_FIELD_VALUE_NULL: - case HTTP_INVALID_BASIC_AUTH_TOKEN: - case HTTP_INVALID_TAOSD_AUTH_TOKEN: - case HTTP_TG_HOST_NOT_STRING: - // grafana - case HTTP_GC_QUERY_NULL: - case HTTP_GC_QUERY_SIZE: - httpCode = 400; - httpCodeStr = "Bad Request"; - break; - default: - httpError("context:%p, fd:%d, error:%d not recognized", pContext, pContext->fd, errNo); - break; - } +void httpSendErrorResp(HttpContext *pContext, int32_t errNo) { + int32_t httpCode = 500; + if (errNo == TSDB_CODE_SUCCESS) + httpCode = 200; + else if (errNo == TSDB_CODE_HTTP_SERVER_OFFLINE) + httpCode = 404; + else if (errNo == TSDB_CODE_HTTP_UNSUPPORT_URL) + httpCode = 404; + else if (errNo == TSDB_CODE_HTTP_INVLALID_URL) + httpCode = 404; + else if (errNo == TSDB_CODE_HTTP_NO_ENOUGH_MEMORY) + httpCode = 507; + else if (errNo == TSDB_CODE_HTTP_REQUSET_TOO_BIG) + httpCode = 413; + else if (errNo == TSDB_CODE_HTTP_NO_AUTH_INFO) + httpCode = 401; + else if (errNo == TSDB_CODE_HTTP_NO_MSG_INPUT) + httpCode = 400; + else if (errNo == TSDB_CODE_HTTP_NO_SQL_INPUT) + httpCode = 400; + else if (errNo == TSDB_CODE_HTTP_NO_EXEC_USEDB) + httpCode = 400; + else if (errNo == TSDB_CODE_HTTP_SESSION_FULL) + httpCode = 421; + else if (errNo == TSDB_CODE_HTTP_GEN_TAOSD_TOKEN_ERR) + httpCode = 507; + else if (errNo == TSDB_CODE_HTTP_INVALID_MULTI_REQUEST) + httpCode = 400; + else if (errNo == TSDB_CODE_HTTP_CREATE_GZIP_FAILED) + httpCode = 507; + else if (errNo == TSDB_CODE_HTTP_FINISH_GZIP_FAILED) + httpCode = 507; + else if (errNo == TSDB_CODE_HTTP_INVALID_VERSION) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_INVALID_CONTENT_LENGTH) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_INVALID_AUTH_TYPE) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_INVALID_AUTH_FORMAT) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_INVALID_BASIC_AUTH) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_INVALID_TAOSD_AUTH) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_METHOD_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_TARGET_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_VERSION_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_SP_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_STATUS_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_PHRASE_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_CRLF_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_HEADER_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_HEADER_KEY_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_HEADER_VAL_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_CHUNK_SIZE_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_CHUNK_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_END_FAILED) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_INVALID_STATE) + httpCode = 406; + else if (errNo == TSDB_CODE_HTTP_PARSE_ERROR_STATE) + httpCode = 406; + else + httpCode = 400; - if (desc == NULL) { - httpSendErrorRespImp(pContext, httpCode, httpCodeStr, errNo + 5000, httpMsg[errNo]); - } else { - httpSendErrorRespImp(pContext, httpCode, httpCodeStr, errNo + 5000, desc); - } -} - -void httpSendErrorResp(HttpContext *pContext, int32_t errNo) { httpSendErrorRespWithDesc(pContext, errNo, NULL); } - -void httpSendTaosdErrorResp(HttpContext *pContext, int32_t errCode) { - int32_t httpCode = 400; - - httpSendErrorRespImp(pContext, httpCode, "Bad Request", errCode & 0XFFFF, (char*)tstrerror(errCode)); + char *httpCodeStr = httpGetStatusDesc(httpCode); + httpSendErrorRespImp(pContext, httpCode, httpCodeStr, errNo & 0XFFFF, tstrerror(errNo)); } void httpSendTaosdInvalidSqlErrorResp(HttpContext *pContext, char *errMsg) { @@ -208,7 +160,7 @@ void httpSendSuccResp(HttpContext *pContext, char *desc) { char head[1024] = {0}; char body[1024] = {0}; - int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_OK], HTTP_SUCCESS, desc); + int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_OK], TSDB_CODE_SUCCESS, desc); int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_OK], httpVersionStr[pContext->parser->httpVersion], httpKeepAliveStr[pContext->parser->keepAlive], bodyLen); @@ -221,7 +173,7 @@ void httpSendOptionResp(HttpContext *pContext, char *desc) { char head[1024] = {0}; char body[1024] = {0}; - int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_OK], HTTP_SUCCESS, desc); + int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_OK], TSDB_CODE_SUCCESS, desc); int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_OPTIONS], httpVersionStr[pContext->parser->httpVersion], httpKeepAliveStr[pContext->parser->keepAlive], bodyLen); diff --git a/src/plugins/http/src/httpRestHandle.c b/src/plugins/http/src/httpRestHandle.c index fbdce566f0..0a28c431ef 100644 --- a/src/plugins/http/src/httpRestHandle.c +++ b/src/plugins/http/src/httpRestHandle.c @@ -15,6 +15,7 @@ #define _DEFAULT_SOURCE #include "os.h" +#include "taoserror.h" #include "httpLog.h" #include "httpRestHandle.h" #include "httpRestJson.h" @@ -90,7 +91,7 @@ bool restProcessSqlRequest(HttpContext* pContext, int32_t timestampFmt) { char* sql = pContext->parser->body.str; if (sql == NULL) { - httpSendErrorResp(pContext, HTTP_NO_SQL_INPUT); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_SQL_INPUT); return false; } @@ -99,7 +100,7 @@ bool restProcessSqlRequest(HttpContext* pContext, int32_t timestampFmt) { * for async test * if (httpCheckUsedbSql(sql)) { - httpSendErrorResp(pContext, HTTP_NO_EXEC_USEDB); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_EXEC_USEDB); return false; } */ @@ -126,7 +127,7 @@ bool restProcessRequest(struct HttpContext* pContext) { } if (strlen(pContext->user) == 0 || strlen(pContext->pass) == 0) { - httpSendErrorResp(pContext, HTTP_PARSE_USR_ERROR); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_AUTH_INFO); return false; } @@ -141,6 +142,6 @@ bool restProcessRequest(struct HttpContext* pContext) { } else { } - httpSendErrorResp(pContext, HTTP_PARSE_URL_ERROR); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_INVLALID_URL); return false; } diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index 212ce46473..2a558e25d5 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -139,7 +139,7 @@ static void httpProcessHttpData(void *param) { if (pServer->status != HTTP_SERVER_RUNNING) { httpDebug("context:%p, fd:%d, state:%s, server is not running, accessed:%d, close connect", pContext, pContext->fd, httpContextStateStr(pContext->state), pContext->accessTimes); - httpSendErrorResp(pContext, HTTP_SERVER_OFFLINE); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_SERVER_OFFLINE); httpNotifyContextClose(pContext); } else { if (httpReadData(pContext)) { diff --git a/src/plugins/http/src/httpSession.c b/src/plugins/http/src/httpSession.c index 4549192407..a96e4433b0 100644 --- a/src/plugins/http/src/httpSession.c +++ b/src/plugins/http/src/httpSession.c @@ -16,6 +16,7 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taos.h" +#include "taoserror.h" #include "tglobal.h" #include "tcache.h" #include "httpInt.h" @@ -40,7 +41,7 @@ void httpCreateSession(HttpContext *pContext, void *taos) { if (pContext->session == NULL) { httpError("context:%p, fd:%d, user:%s, error:%s", pContext, pContext->fd, pContext->user, - httpMsg[HTTP_SESSION_FULL]); + tstrerror(TSDB_CODE_HTTP_SESSION_FULL)); taos_close(taos); pthread_mutex_unlock(&server->serverMutex); return; diff --git a/src/plugins/http/src/httpSql.c b/src/plugins/http/src/httpSql.c index cbaa0b36d8..c60124a991 100644 --- a/src/plugins/http/src/httpSql.c +++ b/src/plugins/http/src/httpSql.c @@ -17,6 +17,7 @@ #include "os.h" #include "tnote.h" #include "taos.h" +#include "taoserror.h" #include "tsclient.h" #include "httpInt.h" #include "httpContext.h" @@ -193,7 +194,7 @@ void httpProcessMultiSqlCmd(HttpContext *pContext) { HttpSqlCmds *multiCmds = pContext->multiCmds; if (multiCmds == NULL || multiCmds->size <= 0 || multiCmds->pos >= multiCmds->size || multiCmds->pos < 0) { - httpSendErrorResp(pContext, HTTP_INVALID_MULTI_REQUEST); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_INVALID_MULTI_REQUEST); return; } @@ -282,7 +283,7 @@ void httpProcessSingleSqlCallBackImp(void *param, TAOS_RES *result, int unUsedCo } else { httpError("context:%p, fd:%d, user:%s, query error, taos:%p, code:%s, sqlObj:%p", pContext, pContext->fd, pContext->user, pContext->session->taos, tstrerror(code), pObj); - httpSendTaosdErrorResp(pContext, code); + httpSendErrorResp(pContext, code); } taos_free_result(result); return; @@ -332,7 +333,7 @@ void httpProcessSingleSqlCmd(HttpContext *pContext) { if (sql == NULL || sql[0] == 0) { httpError("context:%p, fd:%d, user:%s, error:no sql input", pContext, pContext->fd, pContext->user); - httpSendErrorResp(pContext, HTTP_NO_SQL_INPUT); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_SQL_INPUT); return; } @@ -344,7 +345,7 @@ void httpProcessSingleSqlCmd(HttpContext *pContext) { void httpProcessLoginCmd(HttpContext *pContext) { char token[128] = {0}; if (!httpGenTaosdAuthToken(pContext, token, 128)) { - httpSendErrorResp(pContext, HTTP_GEN_TAOSD_TOKEN_ERR); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_GEN_TAOSD_TOKEN_ERR); } else { httpDebug("context:%p, fd:%d, user:%s, login via http, return token:%s", pContext, pContext->fd, pContext->user, token); @@ -395,7 +396,7 @@ void httpProcessRequestCb(void *param, TAOS_RES *result, int code) { if (code < 0) { httpError("context:%p, fd:%d, user:%s, login error, code:%s", pContext, pContext->fd, pContext->user, tstrerror(code)); - httpSendTaosdErrorResp(pContext, code); + httpSendErrorResp(pContext, code); return; } @@ -403,14 +404,14 @@ void httpProcessRequestCb(void *param, TAOS_RES *result, int code) { pContext->taos); if (pContext->taos == NULL) { httpError("context:%p, fd:%d, user:%s, login error, taos is empty", pContext, pContext->fd, pContext->user); - httpSendErrorResp(pContext, HTTP_NO_ENOUGH_SESSIONS); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_LOGIN_FAILED); return; } httpCreateSession(pContext, pContext->taos); if (pContext->session == NULL) { - httpSendErrorResp(pContext, HTTP_SESSION_FULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_SESSION_FULL); httpCloseContextByApp(pContext); } else { httpExecCmd(pContext); diff --git a/src/plugins/http/src/httpTgHandle.c b/src/plugins/http/src/httpTgHandle.c index 83b31f652b..e2b57b87bb 100644 --- a/src/plugins/http/src/httpTgHandle.c +++ b/src/plugins/http/src/httpTgHandle.c @@ -335,12 +335,12 @@ bool tgGetPassFromUrl(HttpContext *pContext) { char *tgGetDbFromUrl(HttpContext *pContext) { HttpParser *pParser = pContext->parser; if (pParser->path[TG_DB_URL_POS].pos <= 0) { - httpSendErrorResp(pContext, HTTP_TG_DB_NOT_INPUT); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_DB_NOT_INPUT); return NULL; } if (pParser->path[TG_DB_URL_POS].pos >= TSDB_DB_NAME_LEN) { - httpSendErrorResp(pContext, HTTP_TG_DB_TOO_LONG); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_DB_TOO_LONG); return NULL; } @@ -401,57 +401,57 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) { // metric name cJSON *name = cJSON_GetObjectItem(metric, "name"); if (name == NULL) { - httpSendErrorResp(pContext, HTTP_TG_METRIC_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRIC_NULL); return false; } if (name->type != cJSON_String) { - httpSendErrorResp(pContext, HTTP_TG_METRIC_TYPE); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRIC_TYPE); return false; } if (name->valuestring == NULL) { - httpSendErrorResp(pContext, HTTP_TG_METRIC_NAME_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRIC_NAME_NULL); return false; } int32_t nameLen = (int32_t)strlen(name->valuestring); if (nameLen == 0) { - httpSendErrorResp(pContext, HTTP_TG_METRIC_NAME_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRIC_NAME_NULL); return false; } if (nameLen >= TSDB_TABLE_NAME_LEN - 8) { - httpSendErrorResp(pContext, HTTP_TG_METRIC_NAME_LONG); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRIC_NAME_LONG); return false; } // timestamp cJSON *timestamp = cJSON_GetObjectItem(metric, "timestamp"); if (timestamp == NULL) { - httpSendErrorResp(pContext, HTTP_TG_TIMESTAMP_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TIMESTAMP_NULL); return false; } if (timestamp->type != cJSON_Number) { - httpSendErrorResp(pContext, HTTP_TG_TIMESTAMP_TYPE); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TIMESTAMP_TYPE); return false; } if (timestamp->valueint <= 0) { - httpSendErrorResp(pContext, HTTP_TG_TIMESTAMP_VAL_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TIMESTAMP_VAL_NULL); return false; } // tags cJSON *tags = cJSON_GetObjectItem(metric, "tags"); if (tags == NULL) { - httpSendErrorResp(pContext, HTTP_TG_TAGS_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAGS_NULL); return false; } int32_t tagsSize = cJSON_GetArraySize(tags); if (tagsSize <= 0) { - httpSendErrorResp(pContext, HTTP_TG_TAGS_SIZE_0); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAGS_SIZE_0); return false; } if (tagsSize > TG_MAX_SORT_TAG_SIZE) { - httpSendErrorResp(pContext, HTTP_TG_TAGS_SIZE_LONG); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAGS_SIZE_LONG); return false; } @@ -460,11 +460,11 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) { for (int32_t i = 0; i < tagsSize; i++) { cJSON *tag = cJSON_GetArrayItem(tags, i); if (tag == NULL) { - httpSendErrorResp(pContext, HTTP_TG_TAG_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAG_NULL); return false; } if (tag->string == NULL || strlen(tag->string) == 0) { - httpSendErrorResp(pContext, HTTP_TG_TAG_NAME_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAG_NAME_NULL); return false; } @@ -474,19 +474,19 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) { */ if (0) { if (strlen(tag->string) >= TSDB_COL_NAME_LEN) { - httpSendErrorResp(pContext, HTTP_TG_TAG_NAME_SIZE); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAG_NAME_SIZE); return false; } } if (tag->type != cJSON_Number && tag->type != cJSON_String) { - httpSendErrorResp(pContext, HTTP_TG_TAG_VALUE_TYPE); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAG_VALUE_TYPE); return false; } if (tag->type == cJSON_String) { if (tag->valuestring == NULL || strlen(tag->valuestring) == 0) { - httpSendErrorResp(pContext, HTTP_TG_TAG_VALUE_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAG_VALUE_NULL); return false; } } @@ -497,46 +497,46 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) { } if (host == NULL) { - httpSendErrorResp(pContext, HTTP_TG_TABLE_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TABLE_NULL); return false; } if (host->type != cJSON_String) { - httpSendErrorResp(pContext, HTTP_TG_HOST_NOT_STRING); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_HOST_NOT_STRING); return false; } if (strlen(host->valuestring) >= TSDB_TABLE_NAME_LEN - 1) { - httpSendErrorResp(pContext, HTTP_TG_TABLE_SIZE); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TABLE_SIZE); return false; } // fields cJSON *fields = cJSON_GetObjectItem(metric, "fields"); if (fields == NULL) { - httpSendErrorResp(pContext, HTTP_TG_FIELDS_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELDS_NULL); return false; } int32_t fieldsSize = cJSON_GetArraySize(fields); if (fieldsSize <= 0) { - httpSendErrorResp(pContext, HTTP_TG_FIELDS_SIZE_0); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELDS_SIZE_0); return false; } if (fieldsSize > (TSDB_MAX_COLUMNS - TSDB_MAX_TAGS - 1)) { - httpSendErrorResp(pContext, HTTP_TG_FIELDS_SIZE_LONG); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELDS_SIZE_LONG); return false; } for (int32_t i = 0; i < fieldsSize; i++) { cJSON *field = cJSON_GetArrayItem(fields, i); if (field == NULL) { - httpSendErrorResp(pContext, HTTP_TG_FIELD_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELD_NULL); return false; } if (field->string == NULL || strlen(field->string) == 0) { - httpSendErrorResp(pContext, HTTP_TG_FIELD_NAME_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELD_NAME_NULL); return false; } /* @@ -545,17 +545,17 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) { */ if (0) { if (strlen(field->string) >= TSDB_COL_NAME_LEN) { - httpSendErrorResp(pContext, HTTP_TG_FIELD_NAME_SIZE); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELD_NAME_SIZE); return false; } } if (field->type != cJSON_Number && field->type != cJSON_String) { - httpSendErrorResp(pContext, HTTP_TG_FIELD_VALUE_TYPE); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELD_VALUE_TYPE); return false; } if (field->type == cJSON_String) { if (field->valuestring == NULL || strlen(field->valuestring) == 0) { - httpSendErrorResp(pContext, HTTP_TG_FIELD_VALUE_NULL); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELD_VALUE_NULL); return false; } } @@ -564,7 +564,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) { // assembling cmds HttpSqlCmd *stable_cmd = httpNewSqlCmd(pContext); if (stable_cmd == NULL) { - httpSendErrorResp(pContext, HTTP_NO_ENOUGH_MEMORY); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); return false; } stable_cmd->cmdType = HTTP_CMD_TYPE_CREATE_STBALE; @@ -572,7 +572,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) { HttpSqlCmd *table_cmd = httpNewSqlCmd(pContext); if (table_cmd == NULL) { - httpSendErrorResp(pContext, HTTP_NO_ENOUGH_MEMORY); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); return false; } table_cmd->cmdType = HTTP_CMD_TYPE_INSERT; @@ -804,13 +804,13 @@ bool tgProcessQueryRequest(HttpContext *pContext, char *db) { char *filter = pContext->parser->body.str; if (filter == NULL) { - httpSendErrorResp(pContext, HTTP_NO_MSG_INPUT); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_MSG_INPUT); return false; } cJSON *root = cJSON_Parse(filter); if (root == NULL) { - httpSendErrorResp(pContext, HTTP_TG_INVALID_JSON); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_INVALID_JSON); return false; } @@ -819,27 +819,27 @@ bool tgProcessQueryRequest(HttpContext *pContext, char *db) { int32_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); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRICS_NULL); cJSON_Delete(root); return false; } int32_t cmdSize = size * 2 + 1; if (cmdSize > HTTP_MAX_CMD_SIZE) { - httpSendErrorResp(pContext, HTTP_TG_METRICS_SIZE); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRICS_SIZE); cJSON_Delete(root); return false; } if (!httpMallocMultiCmds(pContext, cmdSize, HTTP_BUFFER_SIZE)) { - httpSendErrorResp(pContext, HTTP_NO_ENOUGH_MEMORY); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); cJSON_Delete(root); return false; } HttpSqlCmd *cmd = httpNewSqlCmd(pContext); if (cmd == NULL) { - httpSendErrorResp(pContext, HTTP_NO_ENOUGH_MEMORY); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); cJSON_Delete(root); return false; } @@ -860,14 +860,14 @@ bool tgProcessQueryRequest(HttpContext *pContext, char *db) { httpDebug("context:%p, fd:%d, single metric", pContext, pContext->fd); if (!httpMallocMultiCmds(pContext, 3, HTTP_BUFFER_SIZE)) { - httpSendErrorResp(pContext, HTTP_NO_ENOUGH_MEMORY); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); cJSON_Delete(root); return false; } HttpSqlCmd *cmd = httpNewSqlCmd(pContext); if (cmd == NULL) { - httpSendErrorResp(pContext, HTTP_NO_ENOUGH_MEMORY); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); cJSON_Delete(root); return false; } @@ -892,7 +892,7 @@ bool tgProcessQueryRequest(HttpContext *pContext, char *db) { bool tgProcessRquest(struct HttpContext *pContext) { if (strlen(pContext->user) == 0 || strlen(pContext->pass) == 0) { - httpSendErrorResp(pContext, HTTP_PARSE_USR_ERROR); + httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_AUTH_INFO); return false; } From 3af6d957ffc379049c294d95005c8473b4c628cf Mon Sep 17 00:00:00 2001 From: root Date: Mon, 14 Sep 2020 16:17:52 +0800 Subject: [PATCH 14/30] TD-1311 --- src/inc/taoserror.h | 6 +- src/plugins/http/inc/httpAuth.h | 6 +- src/plugins/http/inc/httpParser.h | 1 - src/plugins/http/src/httpAuth.c | 26 ++++----- src/plugins/http/src/httpParser.c | 30 +++++----- src/plugins/http/src/httpResp.c | 4 ++ src/plugins/http/src/httpServer.c | 10 +++- src/plugins/http/src/httpSql.c | 2 +- tests/script/general/http/grafana.sim | 10 ++-- tests/script/general/http/restful_full.sim | 14 ++--- tests/script/general/http/telegraf.sim | 64 +++++++++++----------- tests/script/sh/deploy.sh | 32 +++++------ tests/script/unique/http/admin.sim | 16 +++--- tests/script/unique/http/opentsdb.sim | 42 +++++++------- 14 files changed, 135 insertions(+), 128 deletions(-) diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index d8bd293dba..67e2d43c98 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -303,8 +303,8 @@ TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TIMESTAMP_NULL, 0, 0x1169, "timestamp TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TIMESTAMP_TYPE, 0, 0x116A, "timestamp type should be integer") TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TIMESTAMP_VAL_NULL, 0, 0x116B, "timestamp value smaller than 0") TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAGS_NULL, 0, 0x116C, "tags not find") -TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAGS_SIZE_0, 0, 0x116D, "tags size too long") -TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAGS_SIZE_LONG, 0, 0x116E, "tags size is 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAGS_SIZE_0, 0, 0x116D, "tags size is 0") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAGS_SIZE_LONG, 0, 0x116E, "tags size too long") TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAG_NULL, 0, 0x116F, "tag is null") TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAG_NAME_NULL, 0, 0x1170, "tag name is null") TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_TG_TAG_NAME_SIZE, 0, 0x1171, "tag name length too long") @@ -345,7 +345,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAG_VALUE_TYPE, 0, 0x11A2, "tag value TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAG_VALUE_NULL, 0, 0x11A3, "tag value is null") TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAG_VALUE_TOO_LONG, 0, 0x11A4, "tag value can not more than 64") TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_VALUE_NULL, 0, 0x11A5, "value not find") -TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_VALUE_TYPE, 0, 0x11A5, "value type should be boolean, number or string") +TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_VALUE_TYPE, 0, 0x11A6, "value type should be boolean, number or string") #ifdef TAOS_ERROR_C }; diff --git a/src/plugins/http/inc/httpAuth.h b/src/plugins/http/inc/httpAuth.h index 4becae6332..2ce9725d4b 100644 --- a/src/plugins/http/inc/httpAuth.h +++ b/src/plugins/http/inc/httpAuth.h @@ -16,8 +16,8 @@ #ifndef TDENGINE_HTTP_TOKEN_H #define TDENGINE_HTTP_TOKEN_H -bool httpParseBasicAuthToken(HttpContext *pContext, char *token, int32_t len); -bool httpParseTaosdAuthToken(HttpContext *pContext, char *token, int32_t len); -bool httpGenTaosdAuthToken(HttpContext *pContext, char *token, int32_t maxLen); +int32_t httpParseBasicAuthToken(HttpContext *pContext, char *token, int32_t len); +int32_t httpParseTaosdAuthToken(HttpContext *pContext, char *token, int32_t len); +int32_t httpGenTaosdAuthToken(HttpContext *pContext, char *token, int32_t maxLen); #endif \ No newline at end of file diff --git a/src/plugins/http/inc/httpParser.h b/src/plugins/http/inc/httpParser.h index 5694c8f0cd..7dd6d3e0fc 100644 --- a/src/plugins/http/inc/httpParser.h +++ b/src/plugins/http/inc/httpParser.h @@ -84,7 +84,6 @@ typedef struct HttpParser { HttpString path[HTTP_MAX_URL]; char * method; char * target; - char * target_raw; char * version; char * reasonPhrase; char * key; diff --git a/src/plugins/http/src/httpAuth.c b/src/plugins/http/src/httpAuth.c index 40bb691b5d..8beef7c042 100644 --- a/src/plugins/http/src/httpAuth.c +++ b/src/plugins/http/src/httpAuth.c @@ -23,28 +23,28 @@ #define KEY_DES_4 4971256377704625728L -bool httpParseBasicAuthToken(HttpContext *pContext, char *token, int32_t len) { +int32_t httpParseBasicAuthToken(HttpContext *pContext, char *token, int32_t len) { token[len] = '\0'; int32_t outlen = 0; char *base64 = (char *)base64_decode(token, len, &outlen); if (base64 == NULL || outlen == 0) { httpError("context:%p, fd:%d, basic token:%s parsed error", pContext, pContext->fd, token); free(base64); - return false; + return -1; } char *user = strstr(base64, ":"); if (user == NULL) { httpError("context:%p, fd:%d, basic token:%s invalid format", pContext, pContext->fd, token); free(base64); - return false; + return -1; } int32_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); - return false; + return -1; } strncpy(pContext->user, base64, (size_t)user_len); pContext->user[user_len] = 0; @@ -54,36 +54,36 @@ bool httpParseBasicAuthToken(HttpContext *pContext, char *token, int32_t len) { 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); - return false; + return -1; } strncpy(pContext->pass, password, (size_t)pass_len); pContext->pass[pass_len] = 0; free(base64); httpDebug("context:%p, fd:%d, basic token parsed success, user:%s", pContext, pContext->fd, pContext->user); - return true; + return 0; } -bool httpParseTaosdAuthToken(HttpContext *pContext, char *token, int32_t len) { +int32_t httpParseTaosdAuthToken(HttpContext *pContext, char *token, int32_t len) { token[len] = '\0'; int32_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); if (base64) free(base64); - return false; + return 01; } if (outlen != (TSDB_USER_LEN + TSDB_PASSWORD_LEN)) { httpError("context:%p, fd:%d, taosd token:%s length error", pContext, pContext->fd, token); free(base64); - return false; + return -1; } char *descrypt = taosDesDecode(KEY_DES_4, (char *)base64, outlen); if (descrypt == NULL) { httpError("context:%p, fd:%d, taosd token:%s descrypt error", pContext, pContext->fd, token); free(base64); - return false; + return -1; } else { tstrncpy(pContext->user, descrypt, sizeof(pContext->user)); tstrncpy(pContext->pass, descrypt + TSDB_USER_LEN, sizeof(pContext->pass)); @@ -92,11 +92,11 @@ bool httpParseTaosdAuthToken(HttpContext *pContext, char *token, int32_t len) { pContext->user); free(base64); free(descrypt); - return true; + return 0; } } -bool httpGenTaosdAuthToken(HttpContext *pContext, char *token, int32_t maxLen) { +int32_t httpGenTaosdAuthToken(HttpContext *pContext, char *token, int32_t maxLen) { char buffer[sizeof(pContext->user) + sizeof(pContext->pass)] = {0}; size_t size = sizeof(pContext->user); tstrncpy(buffer, pContext->user, size); @@ -113,5 +113,5 @@ bool httpGenTaosdAuthToken(HttpContext *pContext, char *token, int32_t maxLen) { httpDebug("context:%p, fd:%d, generate taosd token:%s", pContext, pContext->fd, token); - return true; + return 0; } diff --git a/src/plugins/http/src/httpParser.c b/src/plugins/http/src/httpParser.c index d223b8f3a4..ec71bb2daf 100644 --- a/src/plugins/http/src/httpParser.c +++ b/src/plugins/http/src/httpParser.c @@ -112,7 +112,8 @@ static int32_t httpAppendString(HttpString *str, const char *s, int32_t len) { str->pos = 0; str->size = 32; str->str = malloc(str->size); - } else if (str->pos + len + 1 > str->size) { + } else if (str->pos + len + 1 >= str->size) { + str->size += len; str->size *= 10; str->str = realloc(str->str, str->size); } else { @@ -130,7 +131,6 @@ static void httpClearString(HttpString *str) { if (str->str) { str->str[0] = '\0'; str->pos = 0; - str->size = 0; } } @@ -138,7 +138,9 @@ static int32_t httpOnError(HttpParser *parser, int32_t httpCode, int32_t parseCo 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); + + httpError("context:%p, fd:%d, parse failed, httpCode:%d parseCode:%d reason:%s", pContext, pContext->fd, httpCode, + parseCode & 0XFFFF, tstrerror(parseCode)); return 0; } @@ -309,19 +311,19 @@ static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const } 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); + free(t); + free(s); 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); + free(t); + free(s); return -1; } } @@ -334,14 +336,15 @@ static int32_t httpOnBody(HttpParser *parser, const char *chunk, int32_t len) { HttpString * buf = &parser->body; if (parser->parseCode != TSDB_CODE_SUCCESS) return -1; - int32_t avail = buf->size - buf->pos; - if (len + 1 >= avail) { + int32_t newSize = buf->pos + len + 1; + if (newSize >= buf->size) { 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_REQUSET_TOO_BIG); return -1; } else { - int32_t newSize = buf->size * 10; + newSize = MAX(newSize, 32); + newSize *= 10; newSize = MIN(newSize, HTTP_BUFFER_SIZE); buf->str = realloc(buf->str, newSize); if (buf->str == NULL) { @@ -442,7 +445,6 @@ void httpInitParser(HttpParser *parser) { 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; @@ -481,7 +483,6 @@ void httpDestroyParser(HttpParser *parser) { 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; @@ -638,9 +639,8 @@ static int32_t httpParserOnTarget(HttpParser *parser, HTTP_PARSER_STATE state, c } break; } - parser->target_raw = strdup(parser->str.str); - parser->target = httpDecodeUrl(parser->str.str); - if (!parser->target_raw || !parser->target) { + parser->target = strdup(parser->str.str); + if (!parser->target) { httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); ok = -1; httpOnError(parser, 507, TSDB_CODE_HTTP_PARSE_TARGET_FAILED); diff --git a/src/plugins/http/src/httpResp.c b/src/plugins/http/src/httpResp.c index 77dfdb1bf4..755dad2d85 100644 --- a/src/plugins/http/src/httpResp.c +++ b/src/plugins/http/src/httpResp.c @@ -136,6 +136,10 @@ void httpSendErrorResp(HttpContext *pContext, int32_t errNo) { else httpCode = 400; + if (pContext->parser->httpCode != 0) { + httpCode = pContext->parser->httpCode; + } + char *httpCodeStr = httpGetStatusDesc(httpCode); httpSendErrorRespImp(pContext, httpCode, httpCodeStr, errNo & 0XFFFF, tstrerror(errNo)); } diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index 2a558e25d5..0bca8cf041 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -319,21 +319,25 @@ static bool httpReadData(HttpContext *pContext) { if (ok) { httpError("context:%p, fd:%d, parse failed, ret:%d code:%d close connect", pContext, pContext->fd, ok, pParser->parseCode); + httpSendErrorResp(pContext, pParser->parseCode); httpNotifyContextClose(pContext); return false; } if (pParser->parseCode) { httpError("context:%p, fd:%d, parse failed, code:%d close connect", pContext, pContext->fd, pParser->parseCode); + httpSendErrorResp(pContext, pParser->parseCode); httpNotifyContextClose(pContext); return false; } - if (pParser->parsed) { + if (!pParser->parsed) { + httpDebug("context:%p, fd:%d, read not over yet, len:%d", pContext, pContext->fd, pParser->body.pos); + return false; + } else { httpDebug("context:%p, fd:%d, len:%d, body:%s", pContext, pContext->fd, pParser->body.pos, pParser->body.str); + return true; } - - 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); diff --git a/src/plugins/http/src/httpSql.c b/src/plugins/http/src/httpSql.c index c60124a991..207f5d7923 100644 --- a/src/plugins/http/src/httpSql.c +++ b/src/plugins/http/src/httpSql.c @@ -344,7 +344,7 @@ void httpProcessSingleSqlCmd(HttpContext *pContext) { void httpProcessLoginCmd(HttpContext *pContext) { char token[128] = {0}; - if (!httpGenTaosdAuthToken(pContext, token, 128)) { + if (httpGenTaosdAuthToken(pContext, token, 128) != 0) { httpSendErrorResp(pContext, TSDB_CODE_HTTP_GEN_TAOSD_TOKEN_ERR); } else { httpDebug("context:%p, fd:%d, user:%s, login via http, return token:%s", pContext, pContext->fd, pContext->user, diff --git a/tests/script/general/http/grafana.sim b/tests/script/general/http/grafana.sim index cea804cfbb..c7866e5f4c 100644 --- a/tests/script/general/http/grafana.sim +++ b/tests/script/general/http/grafana.sim @@ -54,13 +54,13 @@ print =============== step2 - login system_content curl 127.0.0.1:7111/grafana/ print 1-> $system_content -if $system_content != @{"status":"error","code":5011,"desc":"no auth info input"}@ then +if $system_content != @{"status":"error","code":4357,"desc":"no auth info input"}@ then return -1 endi system_content curl 127.0.0.1:7111/grafana/xx print 2-> $system_content -if $system_content != @{"status":"error","code":5011,"desc":"no auth info input"}@ then +if $system_content != @{"status":"error","code":4357,"desc":"no auth info input"}@ then return -1 endi @@ -72,7 +72,7 @@ endi system_content curl 127.0.0.1:7111/grafana/root/1/123/1/1/3 print 4-> $system_content -if $system_content != @{"status":"error","code":5011,"desc":"no auth info input"}@ then +if $system_content != @{"status":"error","code":4357,"desc":"no auth info input"}@ then return -1 endi @@ -84,13 +84,13 @@ endi system_content curl -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3d3cudGFvc2RhdGEuY29tIiwicGFzcyI6InRhb3NkYXRhIiwic3ViIjoicm9vdCJ9.xPv3b5odlR7YF8G_QWASjIRbMtA5v4ItToJ35fFgi' -d 'show databases' 127.0.0.1:7111/grafana/root/1/login print 6-> $system_content -if $system_content != @{"status":"error","code":5010,"desc":"invalid type of Authorization"}@ then +if $system_content != @{"status":"error","code":4386,"desc":"invalid type of Authorization"}@ then return -1 endi system_content curl -H 'Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3d3cudGFvc2RhdGEuY29tIiwicGFzcyI6InRhb3NkYXRhIiwic3ViIjoicm9vdCJ9.xPv3b5odlR7YF8G_QWASjIRbMtA5v4ItToJ35fFgi' -d 'show databases' 127.0.0.1:7111/grafana/root/1/login print 7-> $system_content -if $system_content != @{"status":"error","code":5010,"desc":"invalid type of Authorization"}@ then +if $system_content != @{"status":"error","code":4387,"desc":"invalid format of Authorization"}@ then return -1 endi diff --git a/tests/script/general/http/restful_full.sim b/tests/script/general/http/restful_full.sim index b7f98e49e0..a02140a419 100644 --- a/tests/script/general/http/restful_full.sim +++ b/tests/script/general/http/restful_full.sim @@ -14,26 +14,26 @@ print =============== step1 - login system_content curl 127.0.0.1:7111/rest/ print 1-> $system_content -if $system_content != @{"status":"error","code":5011,"desc":"no auth info input"}@ then +if $system_content != @{"status":"error","code":4357,"desc":"no auth info input"}@ then return -1 endi system_content curl 127.0.0.1:7111/rest/xx print 2-> $system_content -if $system_content != @{"status":"error","code":5011,"desc":"no auth info input"}@ then +if $system_content != @{"status":"error","code":4357,"desc":"no auth info input"}@ then return -1 endi system_content curl 127.0.0.1:7111/rest/login print 3-> $system_content -if $system_content != @{"status":"error","code":5011,"desc":"no auth info input"}@ then +if $system_content != @{"status":"error","code":4357,"desc":"no auth info input"}@ then return -1 endi #4 system_content curl 127.0.0.1:7111/rest/login/root print 4-> $system_content -if $system_content != @{"status":"error","code":5011,"desc":"no auth info input"}@ then +if $system_content != @{"status":"error","code":4357,"desc":"no auth info input"}@ then return -1 endi @@ -58,13 +58,13 @@ endi #8 system_content curl -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3d3cudGFvc2RhdGEuY29tIiwicGFzcyI6InRhb3NkYXRhIiwic3ViIjoicm9vdCJ9.xPv3b5odlR7YF8G_QWASjIRbMtA5v4ItToJ35fFgi' -d 'show databases' 127.0.0.1:7111/rest/login/root/1 print 8-> $system_content -if $system_content != @{"status":"error","code":5010,"desc":"invalid type of Authorization"}@ then +if $system_content != @{"status":"error","code":4386,"desc":"invalid type of Authorization"}@ then return -1 endi system_content curl -H 'Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3d3cudGFvc2RhdGEuY29tIiwicGFzcyI6InRhb3NkYXRhIiwic3ViIjoicm9vdCJ9.xPv3b5odlR7YF8G_QWASjIRbMtA5v4ItToJ35fFgi' -d 'show databases' 127.0.0.1:7111/rest/login/root/1 print 9-> $system_content -if $system_content != @{"status":"error","code":5010,"desc":"invalid type of Authorization"}@ then +if $system_content != @{"status":"error","code":4387,"desc":"invalid format of Authorization"}@ then return -1 endi @@ -100,7 +100,7 @@ endi #14 system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d '' 127.0.0.1:7111/rest/sql print 14-> $system_content -if $system_content != @{"status":"error","code":5012,"desc":"no sql input"}@ then +if $system_content != @{"status":"error","code":4359,"desc":"no sql input"}@ then return -1 endi diff --git a/tests/script/general/http/telegraf.sim b/tests/script/general/http/telegraf.sim index e54af99ad7..4018d9661a 100644 --- a/tests/script/general/http/telegraf.sim +++ b/tests/script/general/http/telegraf.sim @@ -16,224 +16,224 @@ print =============== step1 - parse system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/ print $system_content -if $system_content != @{"status":"error","code":5022,"desc":"database name can not be null"}@ then +if $system_content != @{"status":"error","code":4448,"desc":"database name can not be null"}@ then return -1 endi system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'select * from d1.table_admin' -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/ print $system_content -if $system_content != @{"status":"error","code":5022,"desc":"database name can not be null"}@ then +if $system_content != @{"status":"error","code":4448,"desc":"database name can not be null"}@ then return -1 endi system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'select * from d1.table_admin' -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/d123456789012345678901234567890123456 print $system_content -if $system_content != @{"status":"error","code":5023,"desc":"database name too long"}@ then +if $system_content != @{"status":"error","code":4449,"desc":"database name too long"}@ then return -1 endi system_content curl -u root:taosdata -d '[]' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5027,"desc":"metric name not find"}@ then +if $system_content != @{"status":"error","code":4453,"desc":"metric name not find"}@ then return -1 endi system_content curl -u root:taosdata -d '{}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5027,"desc":"metric name not find"}@ then +if $system_content != @{"status":"error","code":4453,"desc":"metric name not find"}@ then return -1 endi system_content curl -u root:taosdata -d '[{}]' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5027,"desc":"metric name not find"}@ then +if $system_content != @{"status":"error","code":4453,"desc":"metric name not find"}@ then return -1 endi system_content curl -u root:taosdata -d '{"metrics": []}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5025,"desc":"metrics size is 0"}@ then +if $system_content != @{"status":"error","code":4451,"desc":"metrics size is 0"}@ then return -1 endi system_content curl -u root:taosdata -d '{"metrics": [{}]}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5027,"desc":"metric name not find"}@ then +if $system_content != @{"status":"error","code":4453,"desc":"metric name not find"}@ then return -1 endi system_content curl -u root:taosdata -d '{"metrics": 12}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5025,"desc":"metrics size is 0"}@ then +if $system_content != @{"status":"error","code":4451,"desc":"metrics size is 0"}@ then return -1 endi #system_content curl -u root:taosdata -d '{"metrics": [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]}' 127.0.0.1:7111/telegraf/db/root/taosdata1 #print $system_content -#if $system_content != @{"status":"error","code":5026,"desc":"metrics size can not more than 50"}@ then +#if $system_content != @{"status":"error","code":4452,"desc":"metrics size can not more than 50"}@ then # return -1 #endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5027,"desc":"metric name not find"}@ then +if $system_content != @{"status":"error","code":4453,"desc":"metric name not find"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":111,"tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5028,"desc":"metric name type should be string"}@ then +if $system_content != @{"status":"error","code":4454,"desc":"metric name type should be string"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"","tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5029,"desc":"metric name length is 0"}@ then +if $system_content != @{"status":"error","code":4455,"desc":"metric name length is 0"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"a1234567890123456789012345678901234a1234567890123456789012345678901234a1234567890123456789012345678901234a1234567890123456789012345678901234a1234567890123456789012345678901234a1234567890123456789012345678901234a1234567890123456789012345678901234a1234567890123456789012345678901234a1234567890123456789012345678901234a1234567890123456789012345678901234a1234567890123456789012345678901234a1234567890123456789012345678901234","tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5030,"desc":"metric name length too long"}@ then +if $system_content != @{"status":"error","code":4456,"desc":"metric name length too long"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":"windows","instance":"1","objectname":"Processor"}}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5031,"desc":"timestamp not find"}@ then +if $system_content != @{"status":"error","code":4457,"desc":"timestamp not find"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":""}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5032,"desc":"timestamp type should be integer"}@ then +if $system_content != @{"status":"error","code":4458,"desc":"timestamp type should be integer"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":-1}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5033,"desc":"timestamp value smaller than 0"}@ then +if $system_content != @{"status":"error","code":4459,"desc":"timestamp value smaller than 0"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5034,"desc":"tags not find"}@ then +if $system_content != @{"status":"error","code":4460,"desc":"tags not find"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5035,"desc":"tags size is 0"}@ then +if $system_content != @{"status":"error","code":4461,"desc":"tags size is 0"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":"","timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5035,"desc":"tags size is 0"}@ then +if $system_content != @{"status":"error","code":4461,"desc":"tags size is 0"}@ then return -1 endi #system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":"windows","instance":"1","objectname":"Processor","host":"windows","instance":"1","objectname":"Processor","objectname":"Processor"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata #print $system_content -#if $system_content != @{"status":"error","code":5036,"desc":"tags size too long"}@ then +#if $system_content != @{"status":"error","code":4461,"desc":"tags size too long"}@ then # return -1 #endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5035,"desc":"tags size is 0"}@ then +if $system_content != @{"status":"error","code":4461,"desc":"tags size is 0"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"":"windows"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5038,"desc":"tag name is null"}@ then +if $system_content != @{"status":"error","code":4464,"desc":"tag name is null"}@ then return -1 endi #system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host111111111111222222222222222222222":""},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 #print $system_content -#if $system_content != @{"status":"error","code":5039,"desc":"tag name length too long"}@ then +#if $system_content != @{"status":"error","code":4465,"desc":"tag name length too long"}@ then # return -1 #endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":true},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5040,"desc":"tag value type should be number or string"}@ then +if $system_content != @{"status":"error","code":4466,"desc":"tag value type should be number or string"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":""},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5041,"desc":"tag value is null"}@ then +if $system_content != @{"status":"error","code":4467,"desc":"tag value is null"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"5022":"111"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5042,"desc":"table is null"}@ then +if $system_content != @{"status":"error","code":4468,"desc":"table is null"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":"host111111111111222222222222222222222host111111111111222222222222222222222host111111111111222222222222222222222host111111111111222222222222222222222host111111111111222222222222222222222host111111111111222222222222222222222host111111111111222222222222222222222host111111111111222222222222222222222"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5043,"desc":"table name length too long"}@ then +if $system_content != @{"status":"error","code":4469,"desc":"table name length too long"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{},"name":"win_cpu","tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5045,"desc":"fields size is 0"}@ then +if $system_content != @{"status":"error","code":4471,"desc":"fields size is 0"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"":0,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5048,"desc":"field name is null"}@ then +if $system_content != @{"status":"error","code":4474,"desc":"field name is null"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":"","Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5051,"desc":"field value is null"}@ then +if $system_content != @{"status":"error","code":4477,"desc":"field value is null"}@ then return -1 endi system_content curl -u root:taosdata -d '{"fields":{"Percent_DPC_Time":true,"Percent_Idle_Time":95.59830474853516,"Percent_Interrupt_Time":0,"Percent_Privileged_Time":0,"Percent_Processor_Time":0,"Percent_User_Time":0},"name":"win_cpu","tags":{"host":"windows","instance":"1","objectname":"Processor"},"timestamp":1535784122}' 127.0.0.1:7111/telegraf/db/root/taosdata1 print $system_content -if $system_content != @{"status":"error","code":5050,"desc":"field value type should be number or string"}@ then +if $system_content != @{"status":"error","code":4476,"desc":"field value type should be number or string"}@ then return -1 endi diff --git a/tests/script/sh/deploy.sh b/tests/script/sh/deploy.sh index 0d444a5a6e..45739b7d99 100755 --- a/tests/script/sh/deploy.sh +++ b/tests/script/sh/deploy.sh @@ -111,23 +111,23 @@ echo "serverPort ${NODE}" >> $TAOS_CFG echo "dataDir $DATA_DIR" >> $TAOS_CFG echo "logDir $LOG_DIR" >> $TAOS_CFG echo "debugFlag 0" >> $TAOS_CFG -echo "mDebugFlag 135" >> $TAOS_CFG -echo "sdbDebugFlag 135" >> $TAOS_CFG -echo "dDebugFlag 135" >> $TAOS_CFG -echo "vDebugFlag 135" >> $TAOS_CFG -echo "tsdbDebugFlag 135" >> $TAOS_CFG -echo "cDebugFlag 135" >> $TAOS_CFG -echo "jnidebugFlag 135" >> $TAOS_CFG -echo "odbcdebugFlag 135" >> $TAOS_CFG -echo "httpDebugFlag 135" >> $TAOS_CFG -echo "monitorDebugFlag 135" >> $TAOS_CFG -echo "mqttDebugFlag 135" >> $TAOS_CFG -echo "qdebugFlag 135" >> $TAOS_CFG -echo "rpcDebugFlag 135" >> $TAOS_CFG +echo "mDebugFlag 131" >> $TAOS_CFG +echo "sdbDebugFlag 131" >> $TAOS_CFG +echo "dDebugFlag 131" >> $TAOS_CFG +echo "vDebugFlag 131" >> $TAOS_CFG +echo "tsdbDebugFlag 131" >> $TAOS_CFG +echo "cDebugFlag 131" >> $TAOS_CFG +echo "jnidebugFlag 131" >> $TAOS_CFG +echo "odbcdebugFlag 131" >> $TAOS_CFG +echo "httpDebugFlag 207" >> $TAOS_CFG +echo "monitorDebugFlag 131" >> $TAOS_CFG +echo "mqttDebugFlag 131" >> $TAOS_CFG +echo "qdebugFlag 131" >> $TAOS_CFG +echo "rpcDebugFlag 131" >> $TAOS_CFG echo "tmrDebugFlag 131" >> $TAOS_CFG -echo "udebugFlag 135" >> $TAOS_CFG -echo "sdebugFlag 135" >> $TAOS_CFG -echo "wdebugFlag 135" >> $TAOS_CFG +echo "udebugFlag 131" >> $TAOS_CFG +echo "sdebugFlag 131" >> $TAOS_CFG +echo "wdebugFlag 131" >> $TAOS_CFG echo "monitor 0" >> $TAOS_CFG echo "monitorInterval 1" >> $TAOS_CFG echo "http 0" >> $TAOS_CFG diff --git a/tests/script/unique/http/admin.sim b/tests/script/unique/http/admin.sim index dc17520d02..8833397487 100644 --- a/tests/script/unique/http/admin.sim +++ b/tests/script/unique/http/admin.sim @@ -33,25 +33,25 @@ print =============== step1 - login system_content curl 127.0.0.1:7111/admin/ print 1-> $system_content -if $system_content != @{"status":"error","code":5011,"desc":"no auth info input"}@ then +if $system_content != @{"status":"error","code":4357,"desc":"no auth info input"}@ then return -1 endi system_content curl 127.0.0.1:7111/admin/xx print 2-> $system_content -if $system_content != @{"status":"error","code":5011,"desc":"no auth info input"}@ then +if $system_content != @{"status":"error","code":4357,"desc":"no auth info input"}@ then return -1 endi system_content curl 127.0.0.1:7111/admin/login print 3-> $system_content -if $system_content != @{"status":"error","code":5011,"desc":"no auth info input"}@ then +if $system_content != @{"status":"error","code":4357,"desc":"no auth info input"}@ then return -1 endi system_content curl 127.0.0.1:7111/admin/login/root print 4-> $system_content -if $system_content != @{"status":"error","code":5011,"desc":"no auth info input"}@ then +if $system_content != @{"status":"error","code":4357,"desc":"no auth info input"}@ then return -1 endi @@ -69,13 +69,13 @@ endi system_content curl -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.' -d 'show databases' 127.0.0.1:7111/admin/login/root/1 print 7-> $system_content -if $system_content != @{"status":"error","code":5010,"desc":"invalid type of Authorization"}@ then +if $system_content != @{"status":"error","code":4386,"desc":"invalid type of Authorization"}@ then return -1 endi system_content curl -H 'Authorization: Taosd eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3d3cudGFvc2RhdGEuY29tIiwicGFzcyI6InRhb3NkYXRhIiwic3ViIjoicm9vdCJ9.xPv3b5odlR7YF8G_QWASjIRbMtA5v4ItToJ35fFgi' 127.0.0.1:7111/admin/login/root/1 print 8-> $system_content -if $system_content != @{"status":"error","code":5053,"desc":"parse http auth token error"}@ then +if $system_content != @{"status":"error","code":4389,"desc":"invalid taosd Authorization"}@ then return -1 endi @@ -105,7 +105,7 @@ endi system_content curl 127.0.0.1:7111/admin/logout print 11 -----> $system_content -if $system_content != @{"status":"error","code":5011,"desc":"no auth info input"}@ then +if $system_content != @{"status":"error","code":4357,"desc":"no auth info input"}@ then return -1 endi @@ -168,7 +168,7 @@ print =============== step7 - use dbs system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d 'use d1;' 127.0.0.1:7111/admin/all print 23-> $system_content -if $system_content != @{"status":"error","code":5017,"desc":"no need to execute use db cmd"}@ then +if $system_content != @{"status":"error","code":4360,"desc":"no need to execute use db cmd"}@ then return -1 endi diff --git a/tests/script/unique/http/opentsdb.sim b/tests/script/unique/http/opentsdb.sim index 4901c5b3fd..5269817165 100644 --- a/tests/script/unique/http/opentsdb.sim +++ b/tests/script/unique/http/opentsdb.sim @@ -13,62 +13,62 @@ print ============================ dnode1 start print =============== step1 - parse system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","timestamp": 1346846400,"value": 18,"tags": {"host": "web01","group1": "1","dc": "lga"}}]' 127.0.0.1:7111/opentsdb/ print $system_content -if $system_content != @{"status":"error","code":5057,"desc":"database name can not be null"}@ then +if $system_content != @{"status":"error","code":4496,"desc":"database name can not be null"}@ then return -1 endi system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","timestamp": 1346846400,"value": 18,"tags": {"host": "web01","group1": "1","dc": "lga"}}]' 127.0.0.1:7111/opentsdb/db123456789012345678901234567890db print $system_content -if $system_content != @{"status":"error","code":5058,"desc":"database name too long"}@ then +if $system_content != @{"status":"error","code":4497,"desc":"database name too long"}@ then return -1 endi system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d '[{"metric": "sys_cpu","timestamp": 1346846400,"value": 18,"tags": {"host": "web01","group1": "1","dc": "lga"}}]' 127.0.0.1:7111/opentsdb/ print $system_content -if $system_content != @{"status":"error","code":5057,"desc":"database name can not be null"}@ then +if $system_content != @{"status":"error","code":4496,"desc":"database name can not be null"}@ then return -1 endi system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d '[{"metric": "sys_cpu","timestamp": 1346846400,"value": 18,"tags": {"host": "web01","group1": "1","dc": "lga"}}]' 127.0.0.1:7111/opentsdb/db/put2 print $system_content -if $system_content != @{"status":"error","code":5009,"desc":"http url parse error"}@ then +if $system_content != @{"status":"error","code":4354,"desc":"invalid url format"}@ then return -1 endi system_content curl -u root:taosdata -d '[]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5060,"desc":"metrics size is 0"}@ then +if $system_content != @{"status":"error","code":4499,"desc":"metrics size is 0"}@ then return -1 endi system_content curl -u root:taosdata -d '[' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5059,"desc":"invalid opentsdb json fromat"}@ then +if $system_content != @{"status":"error","code":4498,"desc":"invalid opentsdb json fromat"}@ then return -1 endi system_content curl -u root:taosdata -d '{}' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5060,"desc":"metrics size is 0"}@ then +if $system_content != @{"status":"error","code":4499,"desc":"metrics size is 0"}@ then return -1 endi system_content curl -u root:taosdata -d '[{}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5062,"desc":"metric name not find"}@ then +if $system_content != @{"status":"error","code":4501,"desc":"metric name not find"}@ then return -1 endi system_content curl -u root:taosdata -d '[{"metric": 1,"timestamp": 1346846400,"value": 18,"tags": {"host": "web01","group1": "1","dc": "lga"}}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5063,"desc":"metric name type should be string"}@ then +if $system_content != @{"status":"error","code":4502,"desc":"metric name type should be string"}@ then return -1 endi system_content curl -u root:taosdata -d '[{"metric": "","timestamp": 1346846400,"value": 18,"tags": {"host": "web01","group1": "1","dc": "lga"}}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5064,"desc":"metric name length is 0"}@ then +if $system_content != @{"status":"error","code":4503,"desc":"metric name length is 0"}@ then return -1 endi @@ -80,25 +80,25 @@ endi system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","value": 18,"tags": {"host": "web01","group1": "1","dc": "lga"}}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5066,"desc":"timestamp not find"}@ then +if $system_content != @{"status":"error","code":4505,"desc":"timestamp not find"}@ then return -1 endi system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","timestamp": "2","value": 18,"tags": {"host": "web01","group1": "1","dc": "lga"}}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5067,"desc":"timestamp type should be integer"}@ then +if $system_content != @{"status":"error","code":4506,"desc":"timestamp type should be integer"}@ then return -1 endi system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","timestamp": -1,"value": 18,"tags": {"host": "web01","group1": "1","dc": "lga"}}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5068,"desc":"timestamp value smaller than 0"}@ then +if $system_content != @{"status":"error","code":4507,"desc":"timestamp value smaller than 0"}@ then return -1 endi system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","timestamp": 1346846400,"tags": {"host": "web01","group1": "1","dc": "lga"}}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5078,"desc":"value not find"}@ then +if $system_content != @{"status":"error","code":4517,"desc":"value not find"}@ then return -1 endi @@ -106,19 +106,19 @@ endi system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","timestamp": 1346846400,"value": 18}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5069,"desc":"tags not find"}@ then +if $system_content != @{"status":"error","code":4508,"desc":"tags not find"}@ then return -1 endi system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","timestamp": 1346846400,"value": 18,"tags": {}}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5070,"desc":"tags size is 0"}@ then +if $system_content != @{"status":"error","code":4509,"desc":"tags size is 0"}@ then return -1 endi system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","timestamp": 1346846400,"value": 18,"tags": 0}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5070,"desc":"tags size is 0"}@ then +if $system_content != @{"status":"error","code":4509,"desc":"tags size is 0"}@ then return -1 endi @@ -130,25 +130,25 @@ endi system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","timestamp": 1346846400,"value": 18,"tags": {"": "web01"}}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5073,"desc":"tag name is null"}@ then +if $system_content != @{"status":"error","code":4512,"desc":"tag name is null"}@ then return -1 endi system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","timestamp": 1346846400,"value": 18,"tags": {"host01123456789001123456789001123456789001123456789001123456789001123456789": "01"}}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5074,"desc":"tag name length too long"}@ then +if $system_content != @{"status":"error","code":4513,"desc":"tag name length too long"}@ then return -1 endi system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","timestamp": 1346846400,"value": 18,"tags": {"host": "web011234567890011234567890011234567890011234567890011234567890011234567890011234567890011234567890"}}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5077,"desc":"tag value can not more than 64"}@ then +if $system_content != @{"status":"error","code":4516,"desc":"tag value can not more than 64"}@ then return -1 endi system_content curl -u root:taosdata -d '[{"metric": "sys_cpu","timestamp": 1346846400,"value": 18,"tags": {"host": ""}}]' 127.0.0.1:7111/opentsdb/db/put print $system_content -if $system_content != @{"status":"error","code":5076,"desc":"tag value is null"}@ then +if $system_content != @{"status":"error","code":4515,"desc":"tag value is null"}@ then return -1 endi From dfa681df3c2a7fa18d70bc9386579f7f08bbc548 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 14 Sep 2020 17:24:08 +0800 Subject: [PATCH 15/30] [TD-1291] --- src/plugins/http/inc/httpParser.h | 1 + src/plugins/http/src/httpContext.c | 26 ++++++++++---------- src/plugins/http/src/httpHandle.c | 4 ++-- src/plugins/http/src/httpJson.c | 2 +- src/plugins/http/src/httpParser.c | 38 ++++++++++++++++++------------ src/plugins/http/src/httpQueue.c | 2 +- src/plugins/http/src/httpServer.c | 6 ++--- tests/script/sh/deploy.sh | 32 ++++++++++++------------- 8 files changed, 60 insertions(+), 51 deletions(-) diff --git a/src/plugins/http/inc/httpParser.h b/src/plugins/http/inc/httpParser.h index 7dd6d3e0fc..85ba843716 100644 --- a/src/plugins/http/inc/httpParser.h +++ b/src/plugins/http/inc/httpParser.h @@ -111,6 +111,7 @@ typedef struct HttpParser { void httpInitParser(HttpParser *parser); HttpParser *httpCreateParser(struct HttpContext *pContext); +void httpClearParser(HttpParser *parser); void httpDestroyParser(HttpParser *parser); int32_t httpParseBuf(HttpParser *parser, const char *buf, int32_t len); char * httpGetStatusDesc(int32_t statusCode); diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c index 2c65b9d16a..3114fb702e 100644 --- a/src/plugins/http/src/httpContext.c +++ b/src/plugins/http/src/httpContext.c @@ -138,7 +138,7 @@ HttpContext *httpGetContext(void *ptr) { HttpContext *pContext = *ppContext; if (pContext) { int32_t refCount = atomic_add_fetch_32(&pContext->refCount, 1); - httpDebug("context:%p, fd:%d, is accquired, data:%p refCount:%d", pContext, pContext->fd, ppContext, refCount); + httpTrace("context:%p, fd:%d, is accquired, data:%p refCount:%d", pContext, pContext->fd, ppContext, refCount); return pContext; } } @@ -152,9 +152,9 @@ void httpReleaseContext(HttpContext *pContext) { return; } - pContext->parser->inited = 0; + httpClearParser(pContext->parser); HttpContext **ppContext = pContext->ppContext; - httpDebug("context:%p, is released, data:%p refCount:%d", pContext, ppContext, refCount); + httpTrace("context:%p, is released, data:%p refCount:%d", pContext, ppContext, refCount); if (tsHttpServer.contextCache != NULL) { taosCacheRelease(tsHttpServer.contextCache, (void **)(&ppContext), false); @@ -173,7 +173,7 @@ bool httpInitContext(HttpContext *pContext) { memset(&pContext->singleCmd, 0, sizeof(HttpSqlCmd)); - httpDebug("context:%p, fd:%d, parsed:%d", pContext, pContext->fd, pContext->parsed); + httpTrace("context:%p, fd:%d, parsed:%d", pContext, pContext->fd, pContext->parsed); return true; } @@ -191,15 +191,15 @@ void httpCloseContextByApp(HttpContext *pContext) { if (keepAlive) { if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_HANDLING, HTTP_CONTEXT_STATE_READY)) { - httpDebug("context:%p, fd:%d, last state:handling, keepAlive:true, reuse context", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, last state:handling, keepAlive:true, reuse context", pContext, pContext->fd); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_DROPPING, HTTP_CONTEXT_STATE_CLOSED)) { httpRemoveContextFromEpoll(pContext); - httpDebug("context:%p, fd:%d, ast state:dropping, keepAlive:true, close connect", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, ast state:dropping, keepAlive:true, close connect", pContext, pContext->fd); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_READY, HTTP_CONTEXT_STATE_READY)) { - httpDebug("context:%p, fd:%d, last state:ready, keepAlive:true, reuse context", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, last state:ready, keepAlive:true, reuse context", pContext, pContext->fd); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_CLOSED, HTTP_CONTEXT_STATE_CLOSED)) { httpRemoveContextFromEpoll(pContext); - httpDebug("context:%p, fd:%d, last state:ready, keepAlive:true, close connect", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, last state:ready, keepAlive:true, close connect", pContext, pContext->fd); } else { httpRemoveContextFromEpoll(pContext); httpError("context:%p, fd:%d, last state:%s:%d, keepAlive:true, close connect", pContext, pContext->fd, @@ -207,7 +207,7 @@ void httpCloseContextByApp(HttpContext *pContext) { } } else { httpRemoveContextFromEpoll(pContext); - httpDebug("context:%p, fd:%d, ilast state:%s:%d, keepAlive:false, close context", pContext, pContext->fd, + httpTrace("context:%p, fd:%d, ilast state:%s:%d, keepAlive:false, close context", pContext, pContext->fd, httpContextStateStr(pContext->state), pContext->state); } @@ -216,13 +216,13 @@ void httpCloseContextByApp(HttpContext *pContext) { void httpCloseContextByServer(HttpContext *pContext) { if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_HANDLING, HTTP_CONTEXT_STATE_DROPPING)) { - httpDebug("context:%p, fd:%d, epoll finished, still used by app", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, epoll finished, still used by app", pContext, pContext->fd); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_DROPPING, HTTP_CONTEXT_STATE_DROPPING)) { - httpDebug("context:%p, fd:%d, epoll already finished, wait app finished", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, epoll already finished, wait app finished", pContext, pContext->fd); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_READY, HTTP_CONTEXT_STATE_CLOSED)) { - httpDebug("context:%p, fd:%d, epoll finished, close connect", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, epoll finished, close connect", pContext, pContext->fd); } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_CLOSED, HTTP_CONTEXT_STATE_CLOSED)) { - httpDebug("context:%p, fd:%d, epoll finished, will be closed soon", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, epoll finished, will be closed soon", pContext, pContext->fd); } else { httpError("context:%p, fd:%d, unknown state:%d", pContext, pContext->fd, pContext->state); } diff --git a/src/plugins/http/src/httpHandle.c b/src/plugins/http/src/httpHandle.c index 9fb46c5734..b50217cfc4 100644 --- a/src/plugins/http/src/httpHandle.c +++ b/src/plugins/http/src/httpHandle.c @@ -33,7 +33,7 @@ bool httpDecodeRequest(HttpContext* pContext) { */ bool httpProcessData(HttpContext* pContext) { if (!httpAlterContextState(pContext, HTTP_CONTEXT_STATE_READY, HTTP_CONTEXT_STATE_HANDLING)) { - httpDebug("context:%p, fd:%d, state:%s not in ready state, stop process request", pContext, pContext->fd, + httpTrace("context:%p, fd:%d, state:%s not in ready state, stop process request", pContext, pContext->fd, httpContextStateStr(pContext->state)); httpCloseContextByApp(pContext); return false; @@ -41,7 +41,7 @@ bool httpProcessData(HttpContext* pContext) { // handle Cross-domain request if (strcmp(pContext->parser->method, "OPTIONS") == 0) { - httpDebug("context:%p, fd:%d, process options request", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, process options request", pContext, pContext->fd); httpSendOptionResp(pContext, "process options request success"); } else { if (!httpDecodeRequest(pContext)) { diff --git a/src/plugins/http/src/httpJson.c b/src/plugins/http/src/httpJson.c index 669e7c9781..e9a8947df2 100644 --- a/src/plugins/http/src/httpJson.c +++ b/src/plugins/http/src/httpJson.c @@ -189,7 +189,7 @@ void httpInitJsonBuf(JsonBuf* buf, struct HttpContext* pContext) { httpGzipCompressInit(buf->pContext); } - httpDebug("context:%p, fd:%d, json buffer initialized", buf->pContext, buf->pContext->fd); + httpTrace("context:%p, fd:%d, json buffer initialized", buf->pContext, buf->pContext->fd); } void httpJsonItemToken(JsonBuf* buf) { diff --git a/src/plugins/http/src/httpParser.c b/src/plugins/http/src/httpParser.c index ec71bb2daf..6f64e55b73 100644 --- a/src/plugins/http/src/httpParser.c +++ b/src/plugins/http/src/httpParser.c @@ -178,7 +178,7 @@ static int32_t httpOnRequestLine(HttpParser *pParser, char *method, char *target } if (pContext->decodeMethod != NULL) { - httpDebug("context:%p, fd:%d, decode method is %s", pContext, pContext->fd, pContext->decodeMethod->module); + httpTrace("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); @@ -190,7 +190,7 @@ static int32_t httpOnRequestLine(HttpParser *pParser, char *method, char *target 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); + httpTrace("context:%p, fd:%d, httpVersion:1.%d", pContext, pContext->fd, pParser->httpVersion); } return 0; @@ -204,7 +204,7 @@ static int32_t httpOnStatusLine(HttpParser *pParser, int32_t code, const char *r 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); + httpTrace("context:%p, fd:%d, key:%s val:%s", pContext, pContext->fd, key, val); if (0 == strcasecmp(key, "Content-Length")) { int32_t len = 0; @@ -214,7 +214,7 @@ static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const parser->contentLength = len; parser->chunkSize = len; parser->contentLengthSpecified = 1; - httpDebug("context:%p, fd:%d, contentLength:%d chunkSize:%d contentLengthSpecified:%d", pContext, pContext->fd, + httpTrace("context:%p, fd:%d, contentLength:%d chunkSize:%d contentLengthSpecified:%d", pContext, pContext->fd, parser->contentLength, parser->chunkSize, parser->contentLengthSpecified); return 0; } else { @@ -227,11 +227,11 @@ static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const 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); + httpTrace("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); + httpTrace("context:%p, fd:%d, acceptEncodingChunked:%d", pContext, pContext->fd, parser->acceptEncodingChunked); } return 0; } @@ -248,7 +248,7 @@ static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const 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); + httpTrace("context:%p, fd:%d, contentChunked:%d", pContext, pContext->fd, parser->contentChunked); } return 0; } @@ -271,7 +271,7 @@ static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const } if (strstr(val, "chunked")) { parser->transferChunked = 1; - httpDebug("context:%p, fd:%d, transferChunked:%d", pContext, pContext->fd, parser->transferChunked); + httpTrace("context:%p, fd:%d, transferChunked:%d", pContext, pContext->fd, parser->transferChunked); } return 0; } @@ -289,7 +289,7 @@ static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const s = NULL; free(t); free(s); - httpDebug("context:%p, fd:%d, basic auth:%s", pContext, pContext->fd, parser->authContent); + httpTrace("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); @@ -303,7 +303,7 @@ static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const s = NULL; free(t); free(s); - httpDebug("context:%p, fd:%d, taosd auth:%s", pContext, pContext->fd, parser->authContent); + httpTrace("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); @@ -371,7 +371,7 @@ static int32_t httpOnEnd(HttpParser *parser) { return -1; } - httpDebug("context:%p, fd:%d, parse success", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, parse success", pContext, pContext->fd); return 0; } @@ -422,7 +422,7 @@ static int32_t httpCleanupStack(HttpStack *stack) { void httpInitParser(HttpParser *parser) { HttpContext *pContext = parser->pContext; - httpTrace("context:%p, fd:%d, free parser", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, init parser", pContext, pContext->fd); parser->parsed = false; parser->inited = 1; @@ -475,9 +475,17 @@ HttpParser *httpCreateParser(HttpContext *pContext) { return parser; } +void httpClearParser(HttpParser *parser) { + HttpContext *pContext = parser->pContext; + httpTrace("context:%p, fd:%d, clear parser", pContext, pContext->fd); + + pContext->parser->inited = 0; + pContext->parser->parsed = false; +} + void httpDestroyParser(HttpParser *parser) { HttpContext *pContext = parser->pContext; - httpTrace("context:%p, fd:%d, free parser", pContext, pContext->fd); + httpTrace("context:%p, fd:%d, destroy parser", pContext, pContext->fd); if (!parser) return; @@ -603,7 +611,7 @@ static int32_t httpParserOnMethod(HttpParser *parser, HTTP_PARSER_STATE state, c do { if (isalnum(c) || strchr("!#$%&'*+-.^_`|~", c)) { if (httpAppendString(&parser->str, &c, 1)) { - httpDebug("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); + httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); ok = -1; httpOnError(parser, 507, TSDB_CODE_HTTP_PARSE_METHOD_FAILED); break; @@ -719,7 +727,7 @@ static int32_t httpParserOnSp(HttpParser *parser, HTTP_PARSER_STATE state, const httpPopStack(parser); break; } - httpDebug("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); + httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); ok = -1; httpOnError(parser, 507, TSDB_CODE_HTTP_PARSE_SP_FAILED); } while (0); diff --git a/src/plugins/http/src/httpQueue.c b/src/plugins/http/src/httpQueue.c index 76632eb508..86a97a6abe 100644 --- a/src/plugins/http/src/httpQueue.c +++ b/src/plugins/http/src/httpQueue.c @@ -71,7 +71,7 @@ static void *httpProcessResultQueue(void *param) { break; } - httpDebug("context:%p, res:%p will be processed in result queue", pMsg->param, pMsg->result); + httpTrace("context:%p, res:%p will be processed in result queue", pMsg->param, pMsg->result); (*pMsg->fp)(pMsg->param, pMsg->result, pMsg->numOfRows); taosFreeQitem(pMsg); } diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index 0bca8cf041..a4d84b7a03 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -302,11 +302,11 @@ bool httpInitConnect() { static bool httpReadData(HttpContext *pContext) { HttpParser *pParser = pContext->parser; - ASSERT(!pParser->parsed); if (!pParser->inited) { httpInitParser(pParser); } + ASSERT(!pParser->parsed); pContext->accessTimes++; pContext->lastAccessTime = taosGetTimestampSec(); @@ -332,10 +332,10 @@ static bool httpReadData(HttpContext *pContext) { } if (!pParser->parsed) { - httpDebug("context:%p, fd:%d, read not over yet, len:%d", pContext, pContext->fd, pParser->body.pos); + httpTrace("context:%p, fd:%d, read not over yet, len:%d", pContext, pContext->fd, pParser->body.pos); return false; } else { - httpDebug("context:%p, fd:%d, len:%d, body:%s", pContext, pContext->fd, pParser->body.pos, pParser->body.str); + httpTraceL("context:%p, fd:%d, len:%d, body:%s", pContext, pContext->fd, pParser->body.pos, pParser->body.str); return true; } } else if (nread < 0) { diff --git a/tests/script/sh/deploy.sh b/tests/script/sh/deploy.sh index 45739b7d99..0d444a5a6e 100755 --- a/tests/script/sh/deploy.sh +++ b/tests/script/sh/deploy.sh @@ -111,23 +111,23 @@ echo "serverPort ${NODE}" >> $TAOS_CFG echo "dataDir $DATA_DIR" >> $TAOS_CFG echo "logDir $LOG_DIR" >> $TAOS_CFG echo "debugFlag 0" >> $TAOS_CFG -echo "mDebugFlag 131" >> $TAOS_CFG -echo "sdbDebugFlag 131" >> $TAOS_CFG -echo "dDebugFlag 131" >> $TAOS_CFG -echo "vDebugFlag 131" >> $TAOS_CFG -echo "tsdbDebugFlag 131" >> $TAOS_CFG -echo "cDebugFlag 131" >> $TAOS_CFG -echo "jnidebugFlag 131" >> $TAOS_CFG -echo "odbcdebugFlag 131" >> $TAOS_CFG -echo "httpDebugFlag 207" >> $TAOS_CFG -echo "monitorDebugFlag 131" >> $TAOS_CFG -echo "mqttDebugFlag 131" >> $TAOS_CFG -echo "qdebugFlag 131" >> $TAOS_CFG -echo "rpcDebugFlag 131" >> $TAOS_CFG +echo "mDebugFlag 135" >> $TAOS_CFG +echo "sdbDebugFlag 135" >> $TAOS_CFG +echo "dDebugFlag 135" >> $TAOS_CFG +echo "vDebugFlag 135" >> $TAOS_CFG +echo "tsdbDebugFlag 135" >> $TAOS_CFG +echo "cDebugFlag 135" >> $TAOS_CFG +echo "jnidebugFlag 135" >> $TAOS_CFG +echo "odbcdebugFlag 135" >> $TAOS_CFG +echo "httpDebugFlag 135" >> $TAOS_CFG +echo "monitorDebugFlag 135" >> $TAOS_CFG +echo "mqttDebugFlag 135" >> $TAOS_CFG +echo "qdebugFlag 135" >> $TAOS_CFG +echo "rpcDebugFlag 135" >> $TAOS_CFG echo "tmrDebugFlag 131" >> $TAOS_CFG -echo "udebugFlag 131" >> $TAOS_CFG -echo "sdebugFlag 131" >> $TAOS_CFG -echo "wdebugFlag 131" >> $TAOS_CFG +echo "udebugFlag 135" >> $TAOS_CFG +echo "sdebugFlag 135" >> $TAOS_CFG +echo "wdebugFlag 135" >> $TAOS_CFG echo "monitor 0" >> $TAOS_CFG echo "monitorInterval 1" >> $TAOS_CFG echo "http 0" >> $TAOS_CFG From 0b9e809e20c2dc26100ed21a4d7c6f6874d4725d Mon Sep 17 00:00:00 2001 From: Ping Xiao Date: Mon, 14 Sep 2020 17:47:30 +0800 Subject: [PATCH 16/30] fix TAOS SQL display issue on web site --- documentation20/webdocs/markdowndocs/TAOS SQL-ch.md | 1 + 1 file changed, 1 insertion(+) diff --git a/documentation20/webdocs/markdowndocs/TAOS SQL-ch.md b/documentation20/webdocs/markdowndocs/TAOS SQL-ch.md index ae42582687..cb7a017411 100644 --- a/documentation20/webdocs/markdowndocs/TAOS SQL-ch.md +++ b/documentation20/webdocs/markdowndocs/TAOS SQL-ch.md @@ -110,6 +110,7 @@ TDengine缺省的时间戳是毫秒精度,但通过修改配置参数enableMic **Tips**: 以上所有参数修改后都可以用show databases来确认是否修改成功。 + - **显示系统所有数据库** ```mysql SHOW DATABASES; From 6bd79daa4628f97151a12a5fe7f94f7d0f0484d5 Mon Sep 17 00:00:00 2001 From: Shuduo Sang Date: Mon, 14 Sep 2020 18:30:45 +0800 Subject: [PATCH 17/30] [TECO-19] add windows compile steps to README --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index 22984d8cfe..411dddb9f7 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,8 @@ cd TDengine ## Build TDengine +### On Linux platform + ```bash mkdir debug && cd debug cmake .. && cmake --build . @@ -109,6 +111,34 @@ aarch32: cmake .. -DCPUTYPE=aarch32 && cmake --build . ``` +### On Windows platform + +If you use the Visual Studio 2013, please open a command window by executing "cmd.exe". +Please specify "x86_amd64" for 64 bits Windows or specify "x86" is for 32 bits Windows when you execute vcvarsall.bat. +``` +mkdir debug && cd debug +"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" < x86_amd64 | x86 > +cmake .. -G "NMake Makefiles" +nmake +``` + +If you use the Visual Studio 2019, please open a command window by executing "cmd.exe". +Please specify "x64" for 64 bits Windows or specify "x86" is for 32 bits Windows when you execute vcvarsall.bat. +``` +mkdir debug && cd debug +"c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" < x64 | x86 > +cmake .. -G "NMake Makefiles" +nmake +``` + +Or, you can open a command window by clicking Visual Studio 2019 menu "Tools -> Command Line -> Developer Command Prompt" or "Tools -> Command Line -> Developer PowerShell" then execute commands as follows: +``` +mkdir debug && cd debug +cmake .. -G "NMake Makefiles" +nmake +``` + +# Quick Run # Quick Run To quickly start a TDengine server after building, run the command below in terminal: ```cmd From f878512588d4cbc7742476964a139c7f9d681e94 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 14 Sep 2020 18:34:22 +0800 Subject: [PATCH 18/30] [TD-1291] --- src/plugins/http/inc/httpContext.h | 2 +- src/plugins/http/inc/httpInt.h | 6 ++-- src/plugins/http/src/httpContext.c | 9 ++++-- src/plugins/http/src/httpParser.c | 45 +++++++++++++++--------------- src/plugins/http/src/httpServer.c | 8 ++++-- 5 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/plugins/http/inc/httpContext.h b/src/plugins/http/inc/httpContext.h index a2d50d6b7f..260858c5cc 100644 --- a/src/plugins/http/inc/httpContext.h +++ b/src/plugins/http/inc/httpContext.h @@ -25,7 +25,7 @@ const char *httpContextStateStr(HttpContextState state); HttpContext *httpCreateContext(int32_t fd); bool httpInitContext(HttpContext *pContext); HttpContext *httpGetContext(void * pContext); -void httpReleaseContext(HttpContext *pContext); +void httpReleaseContext(HttpContext *pContext, bool clearRes); void httpCloseContextByServer(HttpContext *pContext); void httpCloseContextByApp(HttpContext *pContext); void httpNotifyContextClose(HttpContext *pContext); diff --git a/src/plugins/http/inc/httpInt.h b/src/plugins/http/inc/httpInt.h index 38a52356f8..4fae4e74c9 100644 --- a/src/plugins/http/inc/httpInt.h +++ b/src/plugins/http/inc/httpInt.h @@ -32,9 +32,9 @@ #define HTTP_MAX_BUFFER_SIZE 1024*1024*8 #define HTTP_LABEL_SIZE 8 #define HTTP_MAX_EVENTS 10 -#define HTTP_BUFFER_INIT 8192 -#define HTTP_BUFFER_SIZE 8192000 -#define HTTP_STEP_SIZE 1024 //http message get process step by step +#define HTTP_BUFFER_INIT 4096 +#define HTTP_BUFFER_SIZE 8388608 +#define HTTP_STEP_SIZE 4096 //http message get process step by step #define HTTP_METHOD_SCANNER_SIZE 7 //http method fp size #define TSDB_CODE_HTTP_GC_TARGET_SIZE 512 #define HTTP_WRITE_RETRY_TIMES 500 diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c index 3114fb702e..c0c0c494de 100644 --- a/src/plugins/http/src/httpContext.c +++ b/src/plugins/http/src/httpContext.c @@ -145,14 +145,17 @@ HttpContext *httpGetContext(void *ptr) { return NULL; } -void httpReleaseContext(HttpContext *pContext) { +void httpReleaseContext(HttpContext *pContext, bool clearRes) { int32_t refCount = atomic_sub_fetch_32(&pContext->refCount, 1); if (refCount < 0) { httpError("context:%p, is already released, refCount:%d", pContext, refCount); return; } - httpClearParser(pContext->parser); + if (clearRes) { + httpClearParser(pContext->parser); + } + HttpContext **ppContext = pContext->ppContext; httpTrace("context:%p, is released, data:%p refCount:%d", pContext, ppContext, refCount); @@ -211,7 +214,7 @@ void httpCloseContextByApp(HttpContext *pContext) { httpContextStateStr(pContext->state), pContext->state); } - httpReleaseContext(pContext); + httpReleaseContext(pContext, true); } void httpCloseContextByServer(HttpContext *pContext) { diff --git a/src/plugins/http/src/httpParser.c b/src/plugins/http/src/httpParser.c index 6f64e55b73..cad5dd8f91 100644 --- a/src/plugins/http/src/httpParser.c +++ b/src/plugins/http/src/httpParser.c @@ -110,11 +110,11 @@ static void httpCleanupString(HttpString *str) { static int32_t httpAppendString(HttpString *str, const char *s, int32_t len) { if (str->size == 0) { str->pos = 0; - str->size = 32; + str->size = 64; str->str = malloc(str->size); } else if (str->pos + len + 1 >= str->size) { str->size += len; - str->size *= 10; + str->size *= 4; str->str = realloc(str->str, str->size); } else { } @@ -153,17 +153,10 @@ static int32_t httpOnRequestLine(HttpParser *pParser, char *method, char *target 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; + httpAppendString(pParser->path + i, pStart, strlen(pStart)); 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; + httpAppendString(pParser->path + i, pStart, (int32_t)(pSeek - pStart)); } pStart = pSeek + 1; } @@ -336,23 +329,29 @@ static int32_t httpOnBody(HttpParser *parser, const char *chunk, int32_t len) { HttpString * buf = &parser->body; if (parser->parseCode != TSDB_CODE_SUCCESS) return -1; + if (buf->size <= 0) { + buf->size = MIN(len + 2, HTTP_BUFFER_SIZE); + buf->str = malloc(buf->size); + } + int32_t newSize = buf->pos + len + 1; if (newSize >= buf->size) { 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_REQUSET_TOO_BIG); return -1; - } else { - newSize = MAX(newSize, 32); - newSize *= 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; + } + + newSize = MAX(newSize, HTTP_BUFFER_INIT); + newSize *= 4; + newSize = MIN(newSize, HTTP_BUFFER_SIZE); + buf->str = realloc(buf->str, newSize); + buf->size = newSize; + + if (buf->str == NULL) { + httpError("context:%p, fd:%d, failed parse body, realloc %d failed", pContext, pContext->fd, buf->size); + httpOnError(parser, 0, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); + return -1; } } @@ -389,7 +388,7 @@ static int32_t httpPushStack(HttpParser *parser, HTTP_PARSER_STATE state) { stack->size = 32; stack->stacks = malloc(stack->size * sizeof(int8_t)); } else if (stack->pos + 1 > stack->size) { - stack->size *= 10; + stack->size *= 2; stack->stacks = realloc(stack->stacks, stack->size * sizeof(int8_t)); } else { } diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index a4d84b7a03..0dfdee32da 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -132,7 +132,7 @@ static void httpProcessHttpData(void *param) { if (!httpAlterContextState(pContext, HTTP_CONTEXT_STATE_READY, HTTP_CONTEXT_STATE_READY)) { httpDebug("context:%p, fd:%d, state:%s, not in ready state, ignore read events", pContext, pContext->fd, httpContextStateStr(pContext->state)); - httpReleaseContext(pContext); + httpReleaseContext(pContext, true); continue; } @@ -145,6 +145,8 @@ static void httpProcessHttpData(void *param) { if (httpReadData(pContext)) { (*(pThread->processData))(pContext); atomic_fetch_add_32(&pServer->requestNum, 1); + } else { + httpReleaseContext(pContext, false); } } } @@ -226,7 +228,7 @@ static void *httpAcceptHttpConnection(void *arg) { httpError("context:%p, fd:%d, ip:%s, thread:%s, failed to add http fd for epoll, error:%s", pContext, connFd, pContext->ipstr, pThread->label, strerror(errno)); taosClose(pContext->fd); - httpReleaseContext(pContext); + httpReleaseContext(pContext, true); continue; } @@ -314,7 +316,7 @@ static bool httpReadData(HttpContext *pContext) { int32_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); + httpTrace("context:%p, fd:%d, nread:%d", pContext, pContext->fd, nread); int32_t ok = httpParseBuf(pParser, buf, nread); if (ok) { From 6e46c77f6c09787bb9a47c17ac73e937ed6ba4f0 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 14 Sep 2020 11:36:17 +0000 Subject: [PATCH 19/30] TD-1326 --- src/plugins/http/inc/httpInt.h | 2 +- src/plugins/http/inc/httpSql.h | 6 +++--- src/plugins/http/src/httpGcHandle.c | 2 +- src/plugins/http/src/httpGcJson.c | 26 +++++++++++++------------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/plugins/http/inc/httpInt.h b/src/plugins/http/inc/httpInt.h index 4fae4e74c9..36a022159f 100644 --- a/src/plugins/http/inc/httpInt.h +++ b/src/plugins/http/inc/httpInt.h @@ -36,7 +36,7 @@ #define HTTP_BUFFER_SIZE 8388608 #define HTTP_STEP_SIZE 4096 //http message get process step by step #define HTTP_METHOD_SCANNER_SIZE 7 //http method fp size -#define TSDB_CODE_HTTP_GC_TARGET_SIZE 512 +#define HTTP_GC_TARGET_SIZE 512 #define HTTP_WRITE_RETRY_TIMES 500 #define HTTP_WRITE_WAIT_TIME_MS 5 #define HTTP_SESSION_ID_LEN (TSDB_USER_LEN + TSDB_PASSWORD_LEN) diff --git a/src/plugins/http/inc/httpSql.h b/src/plugins/http/inc/httpSql.h index 660a65e44d..db3e3a3b16 100644 --- a/src/plugins/http/inc/httpSql.h +++ b/src/plugins/http/inc/httpSql.h @@ -29,10 +29,10 @@ void httpFreeMultiCmds(HttpContext *pContext); HttpSqlCmd *httpNewSqlCmd(HttpContext *pContext); HttpSqlCmd *httpCurrSqlCmd(HttpContext *pContext); -int32_t httpCurSqlCmdPos(HttpContext *pContext); +int32_t httpCurSqlCmdPos(HttpContext *pContext); -void httpTrimTableName(char *name); +void httpTrimTableName(char *name); int32_t httpShrinkTableName(HttpContext *pContext, int32_t pos, char *name); -char *httpGetCmdsString(HttpContext *pContext, int32_t pos); +char * httpGetCmdsString(HttpContext *pContext, int32_t pos); #endif diff --git a/src/plugins/http/src/httpGcHandle.c b/src/plugins/http/src/httpGcHandle.c index 01be301637..5d4cb0c680 100644 --- a/src/plugins/http/src/httpGcHandle.c +++ b/src/plugins/http/src/httpGcHandle.c @@ -228,7 +228,7 @@ bool gcProcessQueryRequest(HttpContext* pContext) { cmd->values = refIdBuffer; cmd->table = aliasBuffer; cmd->numOfRows = 0; // hack way as target flags - cmd->timestamp = httpAddToSqlCmdBufferWithSize(pContext, TSDB_CODE_HTTP_GC_TARGET_SIZE + 1); // hack way + cmd->timestamp = httpAddToSqlCmdBufferWithSize(pContext, HTTP_GC_TARGET_SIZE + 1); // hack way if (cmd->timestamp == -1) { httpWarn("context:%p, fd:%d, user:%s, cant't malloc target size, sql buffer is full", pContext, pContext->fd, diff --git a/src/plugins/http/src/httpGcJson.c b/src/plugins/http/src/httpGcJson.c index e864d54ac0..a291641dc3 100644 --- a/src/plugins/http/src/httpGcJson.c +++ b/src/plugins/http/src/httpGcJson.c @@ -129,48 +129,48 @@ bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, // for group by if (groupFields != -1) { - char target[TSDB_CODE_HTTP_GC_TARGET_SIZE] = {0}; + char target[HTTP_GC_TARGET_SIZE] = {0}; int32_t len; - len = snprintf(target,TSDB_CODE_HTTP_GC_TARGET_SIZE,"%s{",aliasBuffer); + len = snprintf(target,HTTP_GC_TARGET_SIZE,"%s{",aliasBuffer); for (int32_t i = dataFields + 1; i Date: Mon, 14 Sep 2020 22:23:50 +0800 Subject: [PATCH 20/30] TD-1291 --- src/plugins/http/src/httpServer.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index 0dfdee32da..2d95a0ac72 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -308,7 +308,11 @@ static bool httpReadData(HttpContext *pContext) { httpInitParser(pParser); } - ASSERT(!pParser->parsed); + if (pParser->parsed) { + httpDebug("context:%p, fd:%d, not in ready state, parsed:%d", pContext, pContext->fd, pParser->parsed); + return false; + } + pContext->accessTimes++; pContext->lastAccessTime = taosGetTimestampSec(); From 95457415469356deb3c7bad6bb9d4ab9032e46b8 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 14 Sep 2020 23:38:00 +0800 Subject: [PATCH 21/30] TD-1291 --- tests/test/c/CMakeLists.txt | 14 ++++++++++++-- tests/test/c/httpTest.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 tests/test/c/httpTest.c diff --git a/tests/test/c/CMakeLists.txt b/tests/test/c/CMakeLists.txt index e1fedaee3c..5339203ec2 100644 --- a/tests/test/c/CMakeLists.txt +++ b/tests/test/c/CMakeLists.txt @@ -5,6 +5,12 @@ INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/os/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/util/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/common/inc) +INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/query/inc) +INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/mnode/inc) +INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/tsdb/inc) +INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/plugins/http/inc) +INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) +INCLUDE_DIRECTORIES(${TD_ENTERPRISE_DIR}/src/inc) IF (TD_LINUX) #add_executable(insertPerTable insertPerTable.c) @@ -28,6 +34,10 @@ IF (TD_LINUX) #add_executable(createNormalTable createNormalTable.c) #target_link_libraries(createNormalTable taos_static tutil common pthread) - add_executable(queryPerformance queryPerformance.c) - target_link_libraries(queryPerformance taos_static tutil common pthread) + #add_executable(queryPerformance queryPerformance.c) + #target_link_libraries(queryPerformance taos_static tutil common pthread) + + add_executable(httpTest httpTest.c) + target_link_libraries(httpTest taos_static tutil common pthread mnode monitor http tsdb twal vnode cJson lz4) ENDIF() + diff --git a/tests/test/c/httpTest.c b/tests/test/c/httpTest.c new file mode 100644 index 0000000000..dd6d9e4cf8 --- /dev/null +++ b/tests/test/c/httpTest.c @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#include "os.h" +#include "os.h" +#include "tglobal.h" +#include "taoserror.h" +#include "httpSystem.h" + +int main(int argc, char *argv[]) { + // Initialize the system + if (httpInitSystem() < 0) { + exit(EXIT_FAILURE); + } + + if (httpStartSystem() < 0) { + exit(EXIT_FAILURE); + } + + while (1) { + sleep(1000); + } +} From 3a58f9f4a92a0ece691f88e5f94ef82ecb6b7e35 Mon Sep 17 00:00:00 2001 From: Ping Xiao Date: Mon, 14 Sep 2020 23:36:49 +0800 Subject: [PATCH 22/30] fix test failures --- tests/pytest/import_merge/importToCommit.py | 2 +- tests/pytest/tag_lite/datatype-without-alter.py | 2 +- tests/pytest/tag_lite/datatype.py | 2 +- tests/pytest/util/sql.py | 8 ++++++-- tests/script/fullGeneralSuite.sim | 1 - 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/pytest/import_merge/importToCommit.py b/tests/pytest/import_merge/importToCommit.py index 9a17ae95fa..7bec5fcd5d 100644 --- a/tests/pytest/import_merge/importToCommit.py +++ b/tests/pytest/import_merge/importToCommit.py @@ -33,7 +33,7 @@ class TDTestCase: tdDnodes.start(1) tdSql.execute('reset query cache') tdSql.execute('drop database if exists db') - tdSql.execute('create database db cache 128 maxtables 10') + tdSql.execute('create database db cache 128') tdSql.execute('use db') tdLog.info("================= step1") diff --git a/tests/pytest/tag_lite/datatype-without-alter.py b/tests/pytest/tag_lite/datatype-without-alter.py index 42bc42bdbf..da52e149a5 100644 --- a/tests/pytest/tag_lite/datatype-without-alter.py +++ b/tests/pytest/tag_lite/datatype-without-alter.py @@ -38,7 +38,7 @@ class TDTestCase: tdLog.info("drop database db if exits") tdSql.execute('drop database if exists db') tdLog.info("================= step1") - tdSql.execute('create database db maxtables 4') + tdSql.execute('create database db') tdLog.sleep(5) tdSql.execute('use db') diff --git a/tests/pytest/tag_lite/datatype.py b/tests/pytest/tag_lite/datatype.py index bc99cf74b0..f7fa9fa3a2 100644 --- a/tests/pytest/tag_lite/datatype.py +++ b/tests/pytest/tag_lite/datatype.py @@ -38,7 +38,7 @@ class TDTestCase: tdLog.info("drop database db if exits") tdSql.execute('drop database if exists db') tdLog.info("================= step1") - tdSql.execute('create database db maxtables 4') + tdSql.execute('create database db') tdLog.sleep(5) tdSql.execute('use db') diff --git a/tests/pytest/util/sql.py b/tests/pytest/util/sql.py index 627d712474..7de8efdfe9 100644 --- a/tests/pytest/util/sql.py +++ b/tests/pytest/util/sql.py @@ -123,8 +123,12 @@ class TDSql: def checkData(self, row, col, data): self.checkRowCol(row, col) - if str(self.queryResult[row][col]) != str(data): - if isinstance(data, float) and abs(self.queryResult[row][col] - data) <= 0.000001: + if self.queryResult[row][col] != data: + if str(self.queryResult[row][col]) != str(data): + tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" % + (self.sql, row, col, self.queryResult[row][col], data)) + return + elif isinstance(data, float) and abs(self.queryResult[row][col] - data) <= 0.000001: tdLog.info("sql:%s, row:%d col:%d data:%f == expect:%f" % (self.sql, row, col, self.queryResult[row][col], data)) return diff --git a/tests/script/fullGeneralSuite.sim b/tests/script/fullGeneralSuite.sim index d137e53d27..4df7900265 100644 --- a/tests/script/fullGeneralSuite.sim +++ b/tests/script/fullGeneralSuite.sim @@ -130,7 +130,6 @@ run general/parser/join.sim run general/parser/join_multivnode.sim run general/parser/select_with_tags.sim run general/parser/groupby.sim -run general/parser/bug.sim run general/parser/tags_dynamically_specifiy.sim run general/parser/set_tag_vals.sim #unsupport run general/parser/repeatAlter.sim From c8cf0720826ec08293c0f2162ddcccbe03bee314 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 14 Sep 2020 23:52:02 +0800 Subject: [PATCH 23/30] minor changes --- tests/test/c/httpTest.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test/c/httpTest.c b/tests/test/c/httpTest.c index dd6d9e4cf8..261546770e 100644 --- a/tests/test/c/httpTest.c +++ b/tests/test/c/httpTest.c @@ -19,7 +19,20 @@ #include "taoserror.h" #include "httpSystem.h" +void signal_handler(int signum) { + httpStopSystem(); + httpCleanUpSystem(); + exit(EXIT_SUCCESS); +} + int main(int argc, char *argv[]) { + struct sigaction act; + act.sa_handler = signal_handler; + sigaction(SIGTERM, &act, NULL); + sigaction(SIGHUP, &act, NULL); + sigaction(SIGINT, &act, NULL); + sigaction(SIGABRT, &act, NULL); + // Initialize the system if (httpInitSystem() < 0) { exit(EXIT_FAILURE); From 53b2d31c389ae6e493bae3374de7d971bc519d60 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 15 Sep 2020 03:24:05 +0000 Subject: [PATCH 24/30] minor changes --- tests/test/c/CMakeLists.txt | 3 + tests/test/c/cacheTest.c | 136 ++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 tests/test/c/cacheTest.c diff --git a/tests/test/c/CMakeLists.txt b/tests/test/c/CMakeLists.txt index 5339203ec2..ffab39d41c 100644 --- a/tests/test/c/CMakeLists.txt +++ b/tests/test/c/CMakeLists.txt @@ -39,5 +39,8 @@ IF (TD_LINUX) add_executable(httpTest httpTest.c) target_link_libraries(httpTest taos_static tutil common pthread mnode monitor http tsdb twal vnode cJson lz4) + + add_executable(cacheTest cacheTest.c) + target_link_libraries(cacheTest taos_static tutil common pthread mnode monitor http tsdb twal vnode cJson lz4) ENDIF() diff --git a/tests/test/c/cacheTest.c b/tests/test/c/cacheTest.c new file mode 100644 index 0000000000..091a6662c8 --- /dev/null +++ b/tests/test/c/cacheTest.c @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include "taos.h" +#include "tcache.h" +#include "tulog.h" +#include "tutil.h" + +#define MAX_REFRESH_TIME_SEC 2 +#define MAX_RANDOM_POINTS 20000 +#define GREEN "\033[1;32m" +#define NC "\033[0m" + +int32_t tsKeepTimeInSec = 3; +int32_t tsNumOfRows = 10000; +int32_t tsSizeOfRow = 64 * 1024 * 100; +void * tsCacheObj = NULL; + +typedef int64_t CacheTestKey; +typedef struct CacheTestRow { + int32_t index; + void ** ppRow; + void * data; +} CacheTestRow; + +CacheTestRow *initRow(int32_t index) { + CacheTestRow *row = calloc(sizeof(CacheTestRow), 1); + row->index = index; + row->data = malloc(tsSizeOfRow * sizeof(int8_t)); + return row; +} + +void detroyRow(void *data) { + CacheTestRow *row = *(CacheTestRow **)data; + free(row->data); + free(row); +} + +void initCache() { + tsCacheObj = taosCacheInit(TSDB_DATA_TYPE_BIGINT, MAX_REFRESH_TIME_SEC, true, detroyRow, "cachetest"); +} + +void putRowInCache() { + for (int index = 0; index < tsNumOfRows; ++index) { + CacheTestRow *row = initRow(index); + uint64_t key = (uint64_t)row; + void **ppRow = taosCachePut(tsCacheObj, &key, sizeof(int64_t), &row, sizeof(int64_t), tsKeepTimeInSec * 1000); + row->ppRow = ppRow; + taosCacheRelease(tsCacheObj, (void **)&ppRow, false); + } +} + +void cleanupCache() { + taosCacheCleanup(tsCacheObj); +} + +void initGetMemory() { + osInit(); + taos_init(); +} + +float getProcMemory() { + float procMemoryUsedMB = 0; + taosGetProcMemory(&procMemoryUsedMB); + return procMemoryUsedMB; +} + +void doTest() { + initCache(); + pPrint("%s initialize procMemory %f MB %s", GREEN, getProcMemory(), NC); + + putRowInCache(); + pPrint("%s insert %d rows, procMemory %f MB %s", GREEN, tsNumOfRows, getProcMemory(), NC); + + int32_t sleepMs = (MAX_REFRESH_TIME_SEC * 3) * 1000 + tsKeepTimeInSec * 1000; + taosMsleep(sleepMs); + pPrint("%s after sleep %d ms, procMemory %f MB %s", GREEN, sleepMs, getProcMemory(), NC); + + cleanupCache(); + taosMsleep(sleepMs); + pPrint("%s after cleanup cache, procMemory %f MB %s", GREEN, getProcMemory(), NC); +} + +void printHelp() { + char indent[10] = " "; + printf("Used to test the performance of cache\n"); + + printf("%s%s\n", indent, "-k"); + printf("%s%s%s%d\n", indent, indent, "KeepTimeInSec, default is ", tsKeepTimeInSec); + printf("%s%s\n", indent, "-n"); + printf("%s%s%s%d\n", indent, indent, "NumOfRows, default is ", tsNumOfRows); + printf("%s%s\n", indent, "-s"); + printf("%s%s%s%d\n", indent, indent, "SizeOfData, default is ", tsSizeOfRow); + + exit(EXIT_SUCCESS); +} + +void parseArgument(int argc, char *argv[]) { + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { + printHelp(); + exit(0); + } else if (strcmp(argv[i], "-k") == 0) { + tsKeepTimeInSec = atoi(argv[++i]); + } else if (strcmp(argv[i], "-n") == 0) { + tsNumOfRows = atoi(argv[++i]); + } else if (strcmp(argv[i], "-s") == 0) { + tsSizeOfRow = atoi(argv[++i]); + } else { + } + } + + pPrint("%s KeepTimeInSec:%d %s", GREEN, tsKeepTimeInSec, NC); + pPrint("%s NumOfRows:%d %s", GREEN, tsNumOfRows, NC); + pPrint("%s SizeOfData:%d %s", GREEN, tsSizeOfRow, NC); +} + +int main(int argc, char *argv[]) { + initGetMemory(); + parseArgument(argc, argv); + doTest(); +} From 80eddb19df849558db6dab972bb5af2810cce387 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 15 Sep 2020 11:53:46 +0800 Subject: [PATCH 25/30] minor changes --- tests/test/c/cacheTest.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/test/c/cacheTest.c b/tests/test/c/cacheTest.c index 091a6662c8..341b64638e 100644 --- a/tests/test/c/cacheTest.c +++ b/tests/test/c/cacheTest.c @@ -26,9 +26,10 @@ #define NC "\033[0m" int32_t tsKeepTimeInSec = 3; -int32_t tsNumOfRows = 10000; -int32_t tsSizeOfRow = 64 * 1024 * 100; +int32_t tsNumOfRows = 1000000; +int32_t tsSizeOfRow = 64 * 1024; void * tsCacheObj = NULL; +int32_t destroyTimes = 0; typedef int64_t CacheTestKey; typedef struct CacheTestRow { @@ -48,6 +49,10 @@ void detroyRow(void *data) { CacheTestRow *row = *(CacheTestRow **)data; free(row->data); free(row); + destroyTimes++; + if (destroyTimes % 50000 == 0) { + pPrint("%s ===> destroyTimes:%d %s", GREEN, destroyTimes, NC); + } } void initCache() { @@ -86,13 +91,15 @@ void doTest() { putRowInCache(); pPrint("%s insert %d rows, procMemory %f MB %s", GREEN, tsNumOfRows, getProcMemory(), NC); - int32_t sleepMs = (MAX_REFRESH_TIME_SEC * 3) * 1000 + tsKeepTimeInSec * 1000; + int32_t sleepMs = (MAX_REFRESH_TIME_SEC * 3 + 10) * 1000 + tsKeepTimeInSec * 1000; taosMsleep(sleepMs); pPrint("%s after sleep %d ms, procMemory %f MB %s", GREEN, sleepMs, getProcMemory(), NC); - cleanupCache(); + //cleanupCache(); taosMsleep(sleepMs); pPrint("%s after cleanup cache, procMemory %f MB %s", GREEN, getProcMemory(), NC); + + pPrint("%s finally destroyTimes:%d %s", GREEN, destroyTimes, NC); } void printHelp() { From c233807ddc9f0e8492a5c3e9b6fd63e5df2fd4ec Mon Sep 17 00:00:00 2001 From: Shuduo Sang Date: Tue, 15 Sep 2020 14:30:40 +0800 Subject: [PATCH 26/30] [TECO-20] remove docker icon --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 411dddb9f7..522fc0ebc1 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ [![Build status](https://ci.appveyor.com/api/projects/status/kf3pwh2or5afsgl9/branch/master?svg=true)](https://ci.appveyor.com/project/sangshuduo/tdengine-2n8ge/branch/master) [![Coverage Status](https://coveralls.io/repos/github/taosdata/TDengine/badge.svg?branch=develop)](https://coveralls.io/github/taosdata/TDengine?branch=develop) [![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/4201/badge)](https://bestpractices.coreinfrastructure.org/projects/4201) -[![Docker Pulls](https://img.shields.io/docker/pulls/tdengine/tdengine)](https://hub.docker.com/repository/docker/tdengine/tdengine) [![tdengine](https://snapcraft.io//tdengine/badge.svg)](https://snapcraft.io/tdengine) [![TDengine](TDenginelogo.png)](https://www.taosdata.com) From bffd30ef80ddb2162cfcca2b7da696d916c221d1 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Tue, 15 Sep 2020 15:21:22 +0800 Subject: [PATCH 27/30] [td-1290] --- src/query/src/qExecutor.c | 131 ++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 75 deletions(-) diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index d48d7d5ea1..6b67c7ceb9 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -1951,36 +1951,36 @@ static void changeExecuteScanOrder(SQInfo *pQInfo, SQueryTableMsg* pQueryMsg, bo // todo handle the case the the order irrelevant query type mixed up with order critical query type // descending order query for last_row query - if (isFirstLastRowQuery(pQuery) && !QUERY_IS_ASC_QUERY(pQuery)) { + if (isFirstLastRowQuery(pQuery)) { qDebug("QInfo:%p scan order changed for last_row query, old:%d, new:%d", GET_QINFO_ADDR(pQuery), pQuery->order.order, TSDB_ORDER_ASC); - SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); pQuery->order.order = TSDB_ORDER_ASC; - assert (pQuery->window.skey <= pQuery->window.ekey); + if (pQuery->window.skey > pQuery->window.ekey) { + SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); + } + + return; + } + + if (isGroupbyNormalCol(pQuery->pGroupbyExpr) && pQuery->order.order == TSDB_ORDER_DESC) { + pQuery->order.order = TSDB_ORDER_ASC; + if (pQuery->window.skey > pQuery->window.ekey) { + SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); + } doExchangeTimeWindow(pQInfo, &pQuery->window); return; } - if (isGroupbyNormalCol(pQuery->pGroupbyExpr) && !QUERY_IS_ASC_QUERY(pQuery)) { - pQuery->order.order = TSDB_ORDER_ASC; - SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); - assert (pQuery->window.skey <= pQuery->window.ekey); - - doExchangeTimeWindow(pQInfo, &pQuery->window); - return; - } - - if (isPointInterpoQuery(pQuery) && (pQuery->intervalTime == 0) && !QUERY_IS_ASC_QUERY(pQuery)) { - qDebug(msg, GET_QINFO_ADDR(pQuery), "interp", pQuery->order.order, TSDB_ORDER_ASC, pQuery->window.skey, - pQuery->window.ekey, pQuery->window.ekey, pQuery->window.skey); - SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); + if (isPointInterpoQuery(pQuery) && pQuery->intervalTime == 0) { + if (!QUERY_IS_ASC_QUERY(pQuery)) { + qDebug(msg, GET_QINFO_ADDR(pQuery), "interp", pQuery->order.order, TSDB_ORDER_ASC, pQuery->window.skey, + pQuery->window.ekey, pQuery->window.ekey, pQuery->window.skey); + SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); + } pQuery->order.order = TSDB_ORDER_ASC; - - assert (pQuery->window.skey <= pQuery->window.ekey); - doExchangeTimeWindow(pQInfo, &pQuery->window); return; } @@ -2365,16 +2365,16 @@ static void ensureOutputBuffer(SQueryRuntimeEnv* pRuntimeEnv, SDataBlockInfo* pB memset(tmp + sizeof(tFilePage) + bytes * pRec->rows, 0, (size_t)((newSize - pRec->rows) * bytes)); pQuery->sdata[i] = (tFilePage *)tmp; } - + // set the pCtx output buffer position pRuntimeEnv->pCtx[i].aOutputBuf = pQuery->sdata[i]->data + pRec->rows * bytes; - + int32_t functionId = pQuery->pSelectExpr[i].base.functionId; if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_DIFF) { pRuntimeEnv->pCtx[i].ptsOutputBuf = pRuntimeEnv->pCtx[0].aOutputBuf; } } - + qDebug("QInfo:%p realloc output buffer, new size: %d rows, old:%" PRId64 ", remain:%" PRId64, GET_QINFO_ADDR(pRuntimeEnv), newSize, pRec->capacity, newSize - pRec->rows); @@ -2920,11 +2920,11 @@ int32_t mergeIntoGroupResultImpl(SQInfo *pQInfo, SArray *pGroup) { STableQueryInfo *item = taosArrayGetP(pGroup, i); SIDList list = getDataBufPagesIdList(pRuntimeEnv->pResultBuf, TSDB_TABLEID(item->pTable)->tid); + pageList = list; + tid = TSDB_TABLEID(item->pTable)->tid; if (taosArrayGetSize(list) > 0 && item->windowResInfo.size > 0) { pTableList[numOfTables++] = item; - tid = TSDB_TABLEID(item->pTable)->tid; - pageList = list; } } @@ -3357,7 +3357,7 @@ void skipResults(SQueryRuntimeEnv *pRuntimeEnv) { for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { int32_t functionId = pQuery->pSelectExpr[i].base.functionId; int32_t bytes = pRuntimeEnv->pCtx[i].outputBytes; - + memmove(pQuery->sdata[i]->data, (char*)pQuery->sdata[i]->data + bytes * numOfSkip, (size_t)(pQuery->rec.rows * bytes)); pRuntimeEnv->pCtx[i].aOutputBuf = ((char*) pQuery->sdata[i]->data) + pQuery->rec.rows * bytes; @@ -4354,32 +4354,6 @@ static bool skipTimeInterval(SQueryRuntimeEnv *pRuntimeEnv, TSKEY* start) { return true; } -static void freeTableQueryInfo(STableGroupInfo* pTableGroupInfo) { - if (pTableGroupInfo->pGroupList == NULL) { - assert(pTableGroupInfo->numOfTables == 0); - } else { - size_t numOfGroups = taosArrayGetSize(pTableGroupInfo->pGroupList); - for (int32_t i = 0; i < numOfGroups; ++i) { - SArray *p = taosArrayGetP(pTableGroupInfo->pGroupList, i); - - size_t num = taosArrayGetSize(p); - for(int32_t j = 0; j < num; ++j) { - STableQueryInfo* item = taosArrayGetP(p, j); - destroyTableQueryInfo(item); - } - - taosArrayDestroy(p); - } - - taosArrayDestroy(pTableGroupInfo->pGroupList); - pTableGroupInfo->pGroupList = NULL; - pTableGroupInfo->numOfTables = 0; - } - - taosHashCleanup(pTableGroupInfo->map); - pTableGroupInfo->map = NULL; -} - static int32_t setupQueryHandle(void* tsdb, SQInfo* pQInfo, bool isSTableQuery) { SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; SQuery *pQuery = pQInfo->runtimeEnv.pQuery; @@ -4415,22 +4389,20 @@ static int32_t setupQueryHandle(void* tsdb, SQInfo* pQInfo, bool isSTableQuery) terrno = TSDB_CODE_SUCCESS; if (isFirstLastRowQuery(pQuery)) { pRuntimeEnv->pQueryHandle = tsdbQueryLastRow(tsdb, &cond, &pQInfo->tableGroupInfo, pQInfo); - if (pRuntimeEnv->pQueryHandle == NULL) { // no data in current stable, clear all - freeTableQueryInfo(&pQInfo->tableqinfoGroupInfo); - } else { // update the query time window - pQuery->window = cond.twindow; - size_t numOfGroups = GET_NUM_OF_TABLEGROUP(pQInfo); - for (int32_t i = 0; i < numOfGroups; ++i) { - SArray *group = GET_TABLEGROUP(pQInfo, i); + // update the query time window + pQuery->window = cond.twindow; - size_t t = taosArrayGetSize(group); - for (int32_t j = 0; j < t; ++j) { - STableQueryInfo *pCheckInfo = taosArrayGetP(group, j); + size_t numOfGroups = GET_NUM_OF_TABLEGROUP(pQInfo); + for(int32_t i = 0; i < numOfGroups; ++i) { + SArray *group = GET_TABLEGROUP(pQInfo, i); - pCheckInfo->win = pQuery->window; - pCheckInfo->lastKey = pCheckInfo->win.skey; - } + size_t t = taosArrayGetSize(group); + for (int32_t j = 0; j < t; ++j) { + STableQueryInfo *pCheckInfo = taosArrayGetP(group, j); + + pCheckInfo->win = pQuery->window; + pCheckInfo->lastKey = pCheckInfo->win.skey; } } } else if (isPointInterpoQuery(pQuery)) { @@ -4484,12 +4456,6 @@ int32_t doInitQInfo(SQInfo *pQInfo, STSBuf *pTsBuf, void *tsdb, int32_t vgId, bo return code; } - if (pQInfo->tableqinfoGroupInfo.numOfTables == 0) { - qDebug("QInfo:%p no table qualified for tag filter, abort query", pQInfo); - setQueryStatus(pQuery, QUERY_COMPLETED); - return TSDB_CODE_SUCCESS; - } - pQInfo->tsdb = tsdb; pQInfo->vgId = vgId; @@ -4612,7 +4578,7 @@ static int64_t scanMultiTableDataBlocks(SQInfo *pQInfo) { SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; SQuery* pQuery = pRuntimeEnv->pQuery; SQueryCostInfo* summary = &pRuntimeEnv->summary; - + int64_t st = taosGetTimestampMs(); TsdbQueryHandleT pQueryHandle = IS_MASTER_SCAN(pRuntimeEnv)? pRuntimeEnv->pQueryHandle : pRuntimeEnv->pSecQueryHandle; @@ -4622,7 +4588,7 @@ static int64_t scanMultiTableDataBlocks(SQInfo *pQInfo) { while (tsdbNextDataBlock(pQueryHandle)) { summary->totalBlocks += 1; - + if (IS_QUERY_KILLED(pQInfo)) { longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED); } @@ -4659,7 +4625,7 @@ static int64_t scanMultiTableDataBlocks(SQInfo *pQInfo) { summary->totalRows += blockInfo.rows; stableApplyFunctionsOnBlock(pRuntimeEnv, &blockInfo, pStatis, pDataBlock, binarySearchForKey); - + qDebug("QInfo:%p check data block completed, uid:%"PRId64", tid:%d, brange:%" PRId64 "-%" PRId64 ", numOfRows:%d, " "lastKey:%" PRId64, pQInfo, blockInfo.uid, blockInfo.tid, blockInfo.window.skey, blockInfo.window.ekey, blockInfo.rows, @@ -6383,10 +6349,25 @@ static void freeQInfo(SQInfo *pQInfo) { taosTFree(pQuery); } - freeTableQueryInfo(&pQInfo->tableqinfoGroupInfo); + // todo refactor, extract method to destroytableDataInfo + if (pQInfo->tableqinfoGroupInfo.pGroupList != NULL) { + int32_t numOfGroups = (int32_t)(GET_NUM_OF_TABLEGROUP(pQInfo)); + for (int32_t i = 0; i < numOfGroups; ++i) { + SArray *p = GET_TABLEGROUP(pQInfo, i); + + size_t num = taosArrayGetSize(p); + for(int32_t j = 0; j < num; ++j) { + STableQueryInfo* item = taosArrayGetP(p, j); + destroyTableQueryInfo(item); + } + + taosArrayDestroy(p); + } + } taosTFree(pQInfo->pBuf); - + taosArrayDestroy(pQInfo->tableqinfoGroupInfo.pGroupList); + taosHashCleanup(pQInfo->tableqinfoGroupInfo.map); tsdbDestroyTableGroup(&pQInfo->tableGroupInfo); taosArrayDestroy(pQInfo->arrTableIdInfo); From 41a21ca29ebefe2bd69b9c5a96c53498730efed0 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 15 Sep 2020 16:23:37 +0800 Subject: [PATCH 28/30] TD-1225 --- src/client/src/tscParseInsert.c | 2 +- src/plugins/http/src/httpServer.c | 6 ++-- tests/script/general/http/autocreate.sim | 5 +++ tests/script/general/http/chunked.sim | 37 +++++++++++++++++++++ tests/script/general/http/gzip.sim | 27 ++++++++++++++++ tests/script/general/http/testSuite.sim | 2 ++ tests/script/jenkins/basic.txt | 2 ++ tests/test/c/cacheTest.c | 11 ++++--- tests/tsim/inc/sim.h | 2 ++ tests/tsim/src/simExe.c | 41 ++++++++++++++++++++++++ tests/tsim/src/simParse.c | 14 ++++++++ 11 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 tests/script/general/http/chunked.sim create mode 100644 tests/script/general/http/gzip.sim diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index 09eb8f167e..18926a84b9 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -526,7 +526,7 @@ int tsParseValues(char **str, STableDataBlocks *pDataBlock, STableMeta *pTableMe int32_t index = 0; SStrToken sToken; - int16_t numOfRows = 0; + int32_t numOfRows = 0; SSchema *pSchema = tscGetTableSchema(pTableMeta); STableComInfo tinfo = tscGetTableInfo(pTableMeta); diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index 2d95a0ac72..d85a236cb1 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -317,10 +317,10 @@ static bool httpReadData(HttpContext *pContext) { pContext->lastAccessTime = taosGetTimestampSec(); char buf[HTTP_STEP_SIZE + 1] = {0}; - int32_t nread = (int32_t)taosReadSocket(pContext->fd, buf, sizeof(buf)); + int32_t nread = (int32_t)taosReadSocket(pContext->fd, buf, HTTP_STEP_SIZE); if (nread > 0) { buf[nread] = '\0'; - httpTrace("context:%p, fd:%d, nread:%d", pContext, pContext->fd, nread); + httpTraceL("context:%p, fd:%d, nread:%d content:%s", pContext, pContext->fd, nread, buf); int32_t ok = httpParseBuf(pParser, buf, nread); if (ok) { @@ -341,7 +341,7 @@ static bool httpReadData(HttpContext *pContext) { httpTrace("context:%p, fd:%d, read not over yet, len:%d", pContext, pContext->fd, pParser->body.pos); return false; } else { - httpTraceL("context:%p, fd:%d, len:%d, body:%s", pContext, pContext->fd, pParser->body.pos, pParser->body.str); + httpDebug("context:%p, fd:%d, totalLen:%d", pContext, pContext->fd, pParser->body.pos); return true; } } else if (nread < 0) { diff --git a/tests/script/general/http/autocreate.sim b/tests/script/general/http/autocreate.sim index 6a005b028a..98d64ab839 100644 --- a/tests/script/general/http/autocreate.sim +++ b/tests/script/general/http/autocreate.sim @@ -24,5 +24,10 @@ print curl 127.0.0.1:7111/rest/sql -----> $system_content # return -1 #endi +sql select * from db.win_cpu_windows_1_processor +print rows: $rows +if $rows != 1 then + return -1 +endi #system sh/exec.sh -n dnode1 -s stop -x SIGINT \ No newline at end of file diff --git a/tests/script/general/http/chunked.sim b/tests/script/general/http/chunked.sim new file mode 100644 index 0000000000..8673655d96 --- /dev/null +++ b/tests/script/general/http/chunked.sim @@ -0,0 +1,37 @@ +system sh/stop_dnodes.sh +sleep 3000 +system sh/deploy.sh -n dnode1 -i 1 +system sh/cfg.sh -n dnode1 -c wallevel -v 0 +system sh/cfg.sh -n dnode1 -c http -v 1 +system sh/cfg.sh -n dnode1 -c maxSQLLength -v 7340032 +system sh/exec.sh -n dnode1 -s start + +sleep 3000 +sql connect + +print ============================ dnode1 start + +print =============== step1 - prepare data +sql create database d1 +sql use d1 + +sql create table table_rest (ts timestamp, i int) +print sql length is 270KB +restful d1 table_rest 1591072800 10000 +restful d1 table_rest 1591172800 10000 +restful d1 table_rest 1591272800 10000 +restful d1 table_rest 1591372800 10000 +restful d1 table_rest 1591472800 10000 +restful d1 table_rest 1591572800 10000 +restful d1 table_rest 1591672800 10000 +restful d1 table_rest 1591772800 10000 +restful d1 table_rest 1591872800 10000 +restful d1 table_rest 1591972800 10000 + +sql select * from table_rest; +print rows: $rows +if $rows != 100000 then + return -1 +endi + +system sh/exec.sh -n dnode1 -s stop -x SIGINT \ No newline at end of file diff --git a/tests/script/general/http/gzip.sim b/tests/script/general/http/gzip.sim new file mode 100644 index 0000000000..0289e337a6 --- /dev/null +++ b/tests/script/general/http/gzip.sim @@ -0,0 +1,27 @@ +system sh/stop_dnodes.sh +sleep 3000 +system sh/deploy.sh -n dnode1 -i 1 +system sh/cfg.sh -n dnode1 -c wallevel -v 0 +system sh/cfg.sh -n dnode1 -c http -v 1 +system sh/cfg.sh -n dnode1 -c maxSQLLength -v 7340032 +system sh/exec.sh -n dnode1 -s start + +sleep 3000 +sql connect + +print ============================ dnode1 start + +print =============== step1 - prepare data +sql create database d1 +sql use d1 + +sql create table table_rest (ts timestamp, i int) +print sql length is 270KB +restful d1 table_rest 1591072800 10000 gzip +sql select * from table_rest; +print rows: $rows +if $rows != 10000 then + return -1 +endi + +system sh/exec.sh -n dnode1 -s stop -x SIGINT \ No newline at end of file diff --git a/tests/script/general/http/testSuite.sim b/tests/script/general/http/testSuite.sim index d91e9f452d..f35362bf07 100644 --- a/tests/script/general/http/testSuite.sim +++ b/tests/script/general/http/testSuite.sim @@ -1,3 +1,5 @@ +run general/http/autocreate.sim +run general/http/chunked.sim run general/http/restful.sim run general/http/restful_insert.sim run general/http/restful_limit.sim diff --git a/tests/script/jenkins/basic.txt b/tests/script/jenkins/basic.txt index 063d11bd9d..adb22aa265 100644 --- a/tests/script/jenkins/basic.txt +++ b/tests/script/jenkins/basic.txt @@ -77,6 +77,8 @@ cd ../../../debug; make ./test.sh -f general/field/smallint.sim ./test.sh -f general/field/tinyint.sim +./test.sh -f general/http/autocreate.sim +./test.sh -f general/http/chunked.sim ./test.sh -f general/http/restful.sim ./test.sh -f general/http/restful_insert.sim ./test.sh -f general/http/restful_limit.sim diff --git a/tests/test/c/cacheTest.c b/tests/test/c/cacheTest.c index 341b64638e..54aca0038e 100644 --- a/tests/test/c/cacheTest.c +++ b/tests/test/c/cacheTest.c @@ -14,6 +14,7 @@ */ #define _DEFAULT_SOURCE +#include #include "os.h" #include "taos.h" #include "tcache.h" @@ -91,15 +92,17 @@ void doTest() { putRowInCache(); pPrint("%s insert %d rows, procMemory %f MB %s", GREEN, tsNumOfRows, getProcMemory(), NC); - int32_t sleepMs = (MAX_REFRESH_TIME_SEC * 3 + 10) * 1000 + tsKeepTimeInSec * 1000; + int32_t sleepMs = (MAX_REFRESH_TIME_SEC * 3) * 1000 + tsKeepTimeInSec * 1000; taosMsleep(sleepMs); pPrint("%s after sleep %d ms, procMemory %f MB %s", GREEN, sleepMs, getProcMemory(), NC); - //cleanupCache(); + cleanupCache(); taosMsleep(sleepMs); pPrint("%s after cleanup cache, procMemory %f MB %s", GREEN, getProcMemory(), NC); - - pPrint("%s finally destroyTimes:%d %s", GREEN, destroyTimes, NC); + + malloc_trim(0); + taosMsleep(sleepMs); + pPrint("%s after malloc_trim, procMemory %f MB %s", GREEN, getProcMemory(), NC); } void printHelp() { diff --git a/tests/tsim/inc/sim.h b/tests/tsim/inc/sim.h index 6f3bc7099d..58e58a442c 100644 --- a/tests/tsim/inc/sim.h +++ b/tests/tsim/inc/sim.h @@ -84,6 +84,7 @@ enum { SIM_CMD_SQL, SIM_CMD_SQL_ERROR, SIM_CMD_SQL_SLOW, + SIM_CMD_RESTFUL, SIM_CMD_TEST, SIM_CMD_RETURN, SIM_CMD_END @@ -172,6 +173,7 @@ bool simExecuteReturnCmd(SScript *script, char *option); bool simExecuteSqlCmd(SScript *script, char *option); bool simExecuteSqlErrorCmd(SScript *script, char *rest); bool simExecuteSqlSlowCmd(SScript *script, char *option); +bool simExecuteRestfulCmd(SScript *script, char *rest); void simVisuallizeOption(SScript *script, char *src, char *dst); #endif \ No newline at end of file diff --git a/tests/tsim/src/simExe.c b/tests/tsim/src/simExe.c index adc2fd0b9d..463dc33c7c 100644 --- a/tests/tsim/src/simExe.c +++ b/tests/tsim/src/simExe.c @@ -915,6 +915,47 @@ bool simExecuteSqlSlowCmd(SScript *script, char *rest) { return simExecuteSqlImpCmd(script, rest, isSlow); } +bool simExecuteRestfulCmd(SScript *script, char *rest) { + FILE *fp = NULL; + char filename[256]; + sprintf(filename, "%s/tmp.sql", tsScriptDir); + fp = fopen(filename, "w"); + if (fp == NULL) { + fprintf(stderr, "ERROR: failed to open file: %s\n", filename); + return false; + } + + char db[64] = {0}; + char tb[64] = {0}; + char gzip[32] = {0}; + int32_t ts; + int32_t times; + sscanf(rest, "%s %s %d %d %s", db, tb, &ts, ×, gzip); + + fprintf(fp, "insert into %s.%s values ", db, tb); + for (int i = 0; i < times; ++i) { + fprintf(fp, "(%d000, %d)", ts + i, ts); + } + fprintf(fp, " \n"); + fflush(fp); + fclose(fp); + + char cmd[1024] = {0}; + if (strcmp(gzip, "gzip") == 0) { + sprintf(cmd, + "curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' --header " + "--compressed --data-ascii @%s 127.0.0.1:7111/rest/sql", + filename); + } else { + sprintf(cmd, + "curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' --header " + "'Transfer-Encoding: chunked' --data-ascii @%s 127.0.0.1:7111/rest/sql", + filename); + } + + return simExecuteSystemCmd(script, cmd); +} + bool simExecuteSqlErrorCmd(SScript *script, char *rest) { char buf[3000]; SCmdLine *line = &script->lines[script->linePos]; diff --git a/tests/tsim/src/simParse.c b/tests/tsim/src/simParse.c index 8dcf83806f..2e6121304f 100644 --- a/tests/tsim/src/simParse.c +++ b/tests/tsim/src/simParse.c @@ -721,6 +721,12 @@ bool simParseSqlSlowCmd(char *rest, SCommand *pCmd, int lineNum) { return true; } +bool simParseRestfulCmd(char *rest, SCommand *pCmd, int lineNum) { + simParseSqlCmd(rest, pCmd, lineNum); + cmdLine[numOfLines - 1].cmdno = SIM_CMD_RESTFUL; + return true; +} + bool simParseSystemCmd(char *rest, SCommand *pCmd, int lineNum) { int expLen; @@ -1020,6 +1026,14 @@ void simInitsimCmdList() { simCmdList[cmdno].executeCmd = simExecuteSqlSlowCmd; simAddCmdIntoHash(&(simCmdList[cmdno])); + cmdno = SIM_CMD_RESTFUL; + simCmdList[cmdno].cmdno = cmdno; + strcpy(simCmdList[cmdno].name, "restful"); + simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name); + simCmdList[cmdno].parseCmd = simParseRestfulCmd; + simCmdList[cmdno].executeCmd = simExecuteRestfulCmd; + simAddCmdIntoHash(&(simCmdList[cmdno])); + /* test is only an internal command */ cmdno = SIM_CMD_TEST; simCmdList[cmdno].cmdno = cmdno; From 2372e4decc278be058a643376eb498213081365b Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 15 Sep 2020 17:26:48 +0800 Subject: [PATCH 29/30] TD-1451 --- src/plugins/http/src/httpParser.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/plugins/http/src/httpParser.c b/src/plugins/http/src/httpParser.c index cad5dd8f91..0c3204687a 100644 --- a/src/plugins/http/src/httpParser.c +++ b/src/plugins/http/src/httpParser.c @@ -238,6 +238,7 @@ static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const httpTrace("context:%p, fd:%d, keepAlive:%d", pContext, pContext->fd, pContext->parser->keepAlive); } +#if 0 else if (0 == strcasecmp(key, "Content-Encoding")) { if (0 == strcmp(val, "gzip")) { parser->contentChunked = 1; @@ -245,8 +246,9 @@ static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const } return 0; } + #endif - else if (0 == strcasecmp(key, "Transfer-Encoding")) { + else if (0 == strcasecmp(key, "Transfer-Encoding") || 0 == strcasecmp(key, "Content-Encoding")) { if (strstr(val, "gzip")) { parser->transferGzip = 1; ehttp_gzip_conf_t conf = {0}; From 53c4f51116cd1cf66752f7ea12b4b4ec65f92288 Mon Sep 17 00:00:00 2001 From: Hui Li Date: Tue, 15 Sep 2020 18:21:40 +0800 Subject: [PATCH 30/30] [TD-1427] --- cmake/install.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/install.inc b/cmake/install.inc index 7a92a396e3..0531d40048 100755 --- a/cmake/install.inc +++ b/cmake/install.inc @@ -13,7 +13,7 @@ ELSEIF (TD_WINDOWS) IF (NOT TD_GODLL) #INSTALL(DIRECTORY ${TD_COMMUNITY_DIR}/src/connector/go DESTINATION connector) #INSTALL(DIRECTORY ${TD_COMMUNITY_DIR}/src/connector/grafana DESTINATION connector) - #INSTALL(DIRECTORY ${TD_COMMUNITY_DIR}/src/connector/python DESTINATION connector) + INSTALL(DIRECTORY ${TD_COMMUNITY_DIR}/src/connector/python DESTINATION connector) INSTALL(DIRECTORY ${TD_COMMUNITY_DIR}/tests/examples DESTINATION .) INSTALL(DIRECTORY ${TD_COMMUNITY_DIR}/packaging/cfg DESTINATION .) INSTALL(FILES ${TD_COMMUNITY_DIR}/src/inc/taos.h DESTINATION include)