Libevhtp  1.2.13
parser.c File Reference
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include "internal.h"
#include "evhtp/parser.h"
#include "evhtp/config.h"
#include "log.h"
Include dependency graph for parser.c:

Go to the source code of this file.

Data Structures

struct  htparser
 

Macros

#define PARSER_STACK_MAX   8192
 
#define LF   (unsigned char)10
 
#define CR   (unsigned char)13
 
#define CRLF   "\x0d\x0a"
 
#define log_htparser__s_(p)
 
#define _MIN_READ(a, b)   ((a) < (b) ? (a) : (b))
 
#define _str3_cmp(m, c0, c1, c2, c3)   *(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)
 
#define _str3Ocmp(m, c0, c1, c2, c3)   *(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)
 
#define _str4cmp(m, c0, c1, c2, c3)   *(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)
 
#define _str5cmp(m, c0, c1, c2, c3, c4)
 
#define _str6cmp(m, c0, c1, c2, c3, c4, c5)
 
#define _str7_cmp(m, c0, c1, c2, c3, c4, c5, c6, c7)
 
#define _str8cmp(m, c0, c1, c2, c3, c4, c5, c6, c7)
 
#define _str9cmp(m, c0, c1, c2, c3, c4, c5, c6, c7, c8)
 
#define __HTPARSE_GENHOOK(__n)
 
#define __HTPARSE_GENDHOOK(__n)
 
#define HTP_SET_BUF(CH)
 

Typedefs

typedef enum eval_hdr_val eval_hdr_val
 
typedef enum parser_flags parser_flags
 
typedef enum parser_state parser_state
 

Enumerations

enum  eval_hdr_val {
  eval_hdr_val_none = 0, eval_hdr_val_connection, eval_hdr_val_proxy_connection, eval_hdr_val_content_length,
  eval_hdr_val_transfer_encoding, eval_hdr_val_hostname, eval_hdr_val_content_type
}
 
enum  parser_flags { parser_flag_chunked = (1 << 0), parser_flag_connection_keep_alive = (1 << 1), parser_flag_connection_close = (1 << 2), parser_flag_trailing = (1 << 3) }
 
enum  parser_state {
  s_start = 0, s_method, s_spaces_before_uri, s_schema,
  s_schema_slash, s_schema_slash_slash, s_host, s_host_ipv6,
  s_host_done, s_port, s_after_slash_in_uri, s_check_uri,
  s_uri, s_http_09, s_http_H, s_http_HT,
  s_http_HTT, s_http_HTTP, s_first_major_digit, s_major_digit,
  s_first_minor_digit, s_minor_digit, s_spaces_after_digit, s_almost_done,
  s_done, s_hdrline_start, s_hdrline_hdr_almost_done, s_hdrline_hdr_done,
  s_hdrline_hdr_key, s_hdrline_hdr_space_before_val, s_hdrline_hdr_val, s_hdrline_almost_done,
  s_hdrline_done, s_body_read, s_chunk_size_start, s_chunk_size,
  s_chunk_size_almost_done, s_chunk_data, s_chunk_data_almost_done, s_chunk_data_done,
  s_status, s_space_after_status, s_status_text
}
 

Functions

static uint64_t str_to_uint64 (char *str, size_t n, int *err)
 
static ssize_t _str_to_ssize_t (char *str, size_t n)
 
htpparse_error htparser_get_error (htparser *p)
 
const char * htparser_get_strerror (htparser *p)
 
unsigned int htparser_get_status (htparser *p)
 
int htparser_should_keep_alive (htparser *p)
 
htp_scheme htparser_get_scheme (htparser *p)
 
htp_method htparser_get_method (htparser *p)
 
const char * htparser_get_methodstr_m (htp_method meth)
 
const char * htparser_get_methodstr (htparser *p)
 
void htparser_set_major (htparser *p, unsigned char major)
 
void htparser_set_minor (htparser *p, unsigned char minor)
 
unsigned char htparser_get_major (htparser *p)
 
unsigned char htparser_get_minor (htparser *p)
 
unsigned char htparser_get_multipart (htparser *p)
 
void * htparser_get_userdata (htparser *p)
 
void htparser_set_userdata (htparser *p, void *ud)
 
uint64_t htparser_get_content_pending (htparser *p)
 
uint64_t htparser_get_content_length (htparser *p)
 
uint64_t htparser_get_bytes_read (htparser *p)
 
uint64_t htparser_get_total_bytes_read (htparser *p)
 
void htparser_init (htparser *p, htp_type type)
 
htparserhtparser_new (void)
 
static int is_host_char (unsigned char ch)
 
static htp_method get_method (const char *m, const size_t sz)
 
size_t htparser_run (htparser *p, htparse_hooks *hooks, const char *data, size_t len)
 

Variables

static uint32_t usual []
 
static int8_t unhex [256]
 
static const char * errstr_map []
 
static const char * method_strmap []
 

Macro Definition Documentation

◆ __HTPARSE_GENDHOOK

#define __HTPARSE_GENDHOOK (   __n)
Value:
static inline int hook_ ## __n ## _run(htparser * p, \
htparse_hooks * hooks, \
const char * s, size_t l) { \
log_debug("enter"); \
if (hooks && (hooks)->__n) \
{ \
return (hooks)->__n(p, s, l); \
} \
\
return 0; \
}

Definition at line 314 of file parser.c.

◆ __HTPARSE_GENHOOK

#define __HTPARSE_GENHOOK (   __n)
Value:
static inline int hook_ ## __n ## _run(htparser * p, htparse_hooks * hooks) { \
log_debug("enter"); \
if (hooks && (hooks)->__n) \
{ \
return (hooks)->__n(p); \
} \
\
return 0; \
}

Definition at line 303 of file parser.c.

◆ _MIN_READ

#define _MIN_READ (   a,
 
)    ((a) < (b) ? (a) : (b))

Definition at line 238 of file parser.c.

◆ _str3_cmp

#define _str3_cmp (   m,
  c0,
  c1,
  c2,
  c3 
)    *(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)

Definition at line 242 of file parser.c.

◆ _str3Ocmp

#define _str3Ocmp (   m,
  c0,
  c1,
  c2,
  c3 
)    *(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)

Definition at line 245 of file parser.c.

◆ _str4cmp

#define _str4cmp (   m,
  c0,
  c1,
  c2,
  c3 
)    *(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)

Definition at line 248 of file parser.c.

◆ _str5cmp

#define _str5cmp (   m,
  c0,
  c1,
  c2,
  c3,
  c4 
)
Value:
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& m[4] == c4

Definition at line 251 of file parser.c.

◆ _str6cmp

#define _str6cmp (   m,
  c0,
  c1,
  c2,
  c3,
  c4,
  c5 
)
Value:
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& (((uint32_t *)m)[1] & 0xffff) == ((c5 << 8) | c4)

Definition at line 255 of file parser.c.

◆ _str7_cmp

#define _str7_cmp (   m,
  c0,
  c1,
  c2,
  c3,
  c4,
  c5,
  c6,
  c7 
)
Value:
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& ((uint32_t *)m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4)

Definition at line 259 of file parser.c.

◆ _str8cmp

#define _str8cmp (   m,
  c0,
  c1,
  c2,
  c3,
  c4,
  c5,
  c6,
  c7 
)
Value:
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& ((uint32_t *)m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4)

Definition at line 263 of file parser.c.

◆ _str9cmp

#define _str9cmp (   m,
  c0,
  c1,
  c2,
  c3,
  c4,
  c5,
  c6,
  c7,
  c8 
)
Value:
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& ((uint32_t *)m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4) \
&& m[8] == c8

Definition at line 267 of file parser.c.

◆ CR

#define CR   (unsigned char)13

Definition at line 16 of file parser.c.

◆ CRLF

#define CRLF   "\x0d\x0a"

Definition at line 17 of file parser.c.

◆ HTP_SET_BUF

#define HTP_SET_BUF (   CH)
Value:
do { \
if (evhtp_likely((p->buf_idx + 1) < PARSER_STACK_MAX)) { \
p->buf[p->buf_idx++] = CH; \
p->buf[p->buf_idx] = '\0'; \
} else { \
p->error = htparse_error_too_big; \
return i + 1; \
} \
} while (0)
#define PARSER_STACK_MAX
Definition: parser.c:14
#define evhtp_likely(x)
Definition: internal.h:17

Definition at line 706 of file parser.c.

◆ LF

#define LF   (unsigned char)10

Definition at line 15 of file parser.c.

◆ log_htparser__s_

#define log_htparser__s_ (   p)

Definition at line 178 of file parser.c.

◆ PARSER_STACK_MAX

#define PARSER_STACK_MAX   8192

Definition at line 14 of file parser.c.

Typedef Documentation

◆ eval_hdr_val

typedef enum eval_hdr_val eval_hdr_val

Definition at line 82 of file parser.c.

◆ parser_flags

typedef enum parser_flags parser_flags

Definition at line 83 of file parser.c.

◆ parser_state

typedef enum parser_state parser_state

Definition at line 84 of file parser.c.

Enumeration Type Documentation

◆ eval_hdr_val

Enumerator
eval_hdr_val_none 
eval_hdr_val_connection 
eval_hdr_val_proxy_connection 
eval_hdr_val_content_length 
eval_hdr_val_transfer_encoding 
eval_hdr_val_hostname 
eval_hdr_val_content_type 

Definition at line 19 of file parser.c.

◆ parser_flags

Enumerator
parser_flag_chunked 
parser_flag_connection_keep_alive 
parser_flag_connection_close 
parser_flag_trailing 

Definition at line 29 of file parser.c.

◆ parser_state

Enumerator
s_start 
s_method 
s_spaces_before_uri 
s_schema 
s_schema_slash 
s_schema_slash_slash 
s_host 
s_host_ipv6 
s_host_done 
s_port 
s_after_slash_in_uri 
s_check_uri 
s_uri 
s_http_09 
s_http_H 
s_http_HT 
s_http_HTT 
s_http_HTTP 
s_first_major_digit 
s_major_digit 
s_first_minor_digit 
s_minor_digit 
s_spaces_after_digit 
s_almost_done 
s_done 
s_hdrline_start 
s_hdrline_hdr_almost_done 
s_hdrline_hdr_done 
s_hdrline_hdr_key 
s_hdrline_hdr_space_before_val 
s_hdrline_hdr_val 
s_hdrline_almost_done 
s_hdrline_done 
s_body_read 
s_chunk_size_start 
s_chunk_size 
s_chunk_size_almost_done 
s_chunk_data 
s_chunk_data_almost_done 
s_chunk_data_done 
s_status 
s_space_after_status 
s_status_text 

Definition at line 36 of file parser.c.

Function Documentation

◆ _str_to_ssize_t()

static ssize_t _str_to_ssize_t ( char *  str,
size_t  n 
)
inlinestatic

Definition at line 391 of file parser.c.

◆ get_method()

static htp_method get_method ( const char *  m,
const size_t  sz 
)
static

Definition at line 600 of file parser.c.

Here is the caller graph for this function:

◆ htparser_get_bytes_read()

uint64_t htparser_get_bytes_read ( htparser p)

Definition at line 552 of file parser.c.

◆ htparser_get_content_length()

uint64_t htparser_get_content_length ( htparser p)

Definition at line 546 of file parser.c.

Here is the caller graph for this function:

◆ htparser_get_content_pending()

uint64_t htparser_get_content_pending ( htparser p)

Definition at line 540 of file parser.c.

◆ htparser_get_error()

htpparse_error htparser_get_error ( htparser p)

Definition at line 421 of file parser.c.

Here is the caller graph for this function:

◆ htparser_get_major()

unsigned char htparser_get_major ( htparser p)

Definition at line 510 of file parser.c.

Here is the caller graph for this function:

◆ htparser_get_method()

htp_method htparser_get_method ( htparser p)

Definition at line 475 of file parser.c.

Here is the caller graph for this function:

◆ htparser_get_methodstr()

const char* htparser_get_methodstr ( htparser p)

Definition at line 492 of file parser.c.

Here is the call graph for this function:

◆ htparser_get_methodstr_m()

const char* htparser_get_methodstr_m ( htp_method  meth)

Definition at line 481 of file parser.c.

Here is the caller graph for this function:

◆ htparser_get_minor()

unsigned char htparser_get_minor ( htparser p)

Definition at line 516 of file parser.c.

Here is the caller graph for this function:

◆ htparser_get_multipart()

unsigned char htparser_get_multipart ( htparser p)

Definition at line 522 of file parser.c.

Here is the caller graph for this function:

◆ htparser_get_scheme()

htp_scheme htparser_get_scheme ( htparser p)

Definition at line 469 of file parser.c.

Here is the caller graph for this function:

◆ htparser_get_status()

unsigned int htparser_get_status ( htparser p)

Definition at line 440 of file parser.c.

Here is the caller graph for this function:

◆ htparser_get_strerror()

const char* htparser_get_strerror ( htparser p)

Definition at line 427 of file parser.c.

Here is the call graph for this function:

◆ htparser_get_total_bytes_read()

uint64_t htparser_get_total_bytes_read ( htparser p)

Definition at line 558 of file parser.c.

◆ htparser_get_userdata()

void* htparser_get_userdata ( htparser p)

Definition at line 528 of file parser.c.

Here is the caller graph for this function:

◆ htparser_init()

void htparser_init ( htparser p,
htp_type  type 
)

Definition at line 564 of file parser.c.

Here is the caller graph for this function:

◆ htparser_new()

htparser* htparser_new ( void  )

Definition at line 576 of file parser.c.

Here is the caller graph for this function:

◆ htparser_run()

size_t htparser_run ( htparser p,
htparse_hooks *  hooks,
const char *  data,
size_t  len 
)

Definition at line 718 of file parser.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ htparser_set_major()

void htparser_set_major ( htparser p,
unsigned char  major 
)

Definition at line 498 of file parser.c.

Here is the caller graph for this function:

◆ htparser_set_minor()

void htparser_set_minor ( htparser p,
unsigned char  minor 
)

Definition at line 504 of file parser.c.

Here is the caller graph for this function:

◆ htparser_set_userdata()

void htparser_set_userdata ( htparser p,
void *  ud 
)

Definition at line 534 of file parser.c.

Here is the caller graph for this function:

◆ htparser_should_keep_alive()

int htparser_should_keep_alive ( htparser p)

Definition at line 446 of file parser.c.

Here is the caller graph for this function:

◆ is_host_char()

static int is_host_char ( unsigned char  ch)
static

Definition at line 582 of file parser.c.

Here is the caller graph for this function:

◆ str_to_uint64()

static uint64_t str_to_uint64 ( char *  str,
size_t  n,
int *  err 
)
inlinestatic

Definition at line 349 of file parser.c.

Here is the caller graph for this function:

Variable Documentation

◆ errstr_map

const char* errstr_map[]
static
Initial value:
= {
"htparse_error_none",
"htparse_error_too_big",
"htparse_error_invalid_method",
"htparse_error_invalid_requestline",
"htparse_error_invalid_schema",
"htparse_error_invalid_protocol",
"htparse_error_invalid_version",
"htparse_error_invalid_header",
"htparse_error_invalid_chunk_size",
"htparse_error_invalid_chunk",
"htparse_error_invalid_state",
"htparse_error_user",
"htparse_error_unknown"
}

Definition at line 203 of file parser.c.

◆ method_strmap

const char* method_strmap[]
static
Initial value:
= {
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"MKCOL",
"COPY",
"MOVE",
"OPTIONS",
"PROPFIND",
"PROPATCH",
"LOCK",
"UNLOCK",
"TRACE",
"CONNECT",
"PATCH",
}

Definition at line 219 of file parser.c.

◆ unhex

int8_t unhex[256]
static
Initial value:
= {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
}

Definition at line 192 of file parser.c.

◆ usual

uint32_t usual[]
static
Initial value:
= {
0xffffdbfe,
0x7fff37d6,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff
}

Definition at line 181 of file parser.c.