From 7c434d6108e26a52995069978e1911efe40287b4 Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 1 Aug 2020 15:19:43 +0800 Subject: [PATCH] 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; -- GitLab