提交 11aed420 编写于 作者: S Shengliang Guan

TD-1311

上级 a1527b62
#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 <stddef.h>
#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;
typedef struct HttpParserConfObj {
size_t flush_block_size; // <=0: immediately
} HttpParserConfObj;
struct ehttp_parser_conf_s {
size_t flush_block_size; // <=0: immediately
};
typedef struct HttpParseKvObj {
char *key;
char *val;
} HttpParseKvObj;
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 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
#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 <stddef.h>
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
......@@ -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 {
......
此差异已折叠。
#include "os.h"
#include "ehttp_util_string.h"
#include <stdlib.h>
#include <string.h>
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;
......
......@@ -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);
......
......@@ -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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册