httpParser.h 3.0 KB
Newer Older
S
TD-1311  
Shengliang Guan 已提交
1 2
#ifndef HTTP_PARSER_H
#define HTTP_PARSER_H
F
freemine 已提交
3

S
Shengliang Guan 已提交
4
#include "httpGzip.h"
F
freemine 已提交
5

S
TD-1311  
Shengliang Guan 已提交
6
struct HttpContext;
F
freemine 已提交
7

S
TD-1311  
Shengliang Guan 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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;

S
Shengliang Guan 已提交
27 28 29 30 31
typedef struct HttpParserString {
  char * str;
  size_t len;
} HttpParserString;

S
TD-1311  
Shengliang Guan 已提交
32 33 34 35 36 37
typedef struct HttpParserStatusObj {
  int32_t     status_code;
  const char *status_desc;
} HttpParserStatusObj;

typedef struct HttpParserCallbackObj {
F
freemine 已提交
38 39 40 41 42 43
  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);
S
TD-1311  
Shengliang Guan 已提交
44 45 46 47 48
} HttpParserCallbackObj;

typedef struct HttpParserConfObj {
  size_t flush_block_size;  // <=0: immediately
} HttpParserConfObj;
F
freemine 已提交
49

S
TD-1311  
Shengliang Guan 已提交
50 51 52 53
typedef struct HttpParseKvObj {
  char *key;
  char *val;
} HttpParseKvObj;
F
freemine 已提交
54

S
TD-1311  
Shengliang Guan 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
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;
S
Shengliang Guan 已提交
84
  HttpParserString     str;
S
TD-1311  
Shengliang Guan 已提交
85 86 87 88
  HTTP_PARSER_STATE *stacks;
  size_t             stacks_count;
} HttpParserObj;

S
Shengliang Guan 已提交
89 90 91 92 93
void    httpParserCleanupString(HttpParserString *str);
int32_t httpParserAppendString(HttpParserString *str, const char *s, int32_t len);
void    httpParserClearString(HttpParserString *str);


S
TD-1311  
Shengliang Guan 已提交
94 95 96
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);
F
freemine 已提交
97 98 99 100 101

char* ehttp_parser_urldecode(const char *enc);

const char* ehttp_status_code_get_desc(const int status_code);

S
TD-1311  
Shengliang Guan 已提交
102
#endif