parser.c File Reference
`#include <stdlib.h>`
`#include <stddef.h>`
`#include <ctype.h>`
`#include <string.h>`
`#include "internal.h"`
`#include "evhtp/parser.h"`
`#include "evhtp/config.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)
#define | _str3_cmp(m, c0, c1, c2, c3)
#define | _str3Ocmp(m, c0, c1, c2, c3)
#define | _str4cmp(m, c0, c1, c2, c3)
#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)
htparser * | htparser_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; \
}
htparser
Definition parser.c:91

Definition at line 319 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 308 of file parser.c.

## _MIN_READ

#define _MIN_READ | ( |  | a,
|  |  | b )

Value:
((a) < (b) ? (a) : (b))

Definition at line 243 of file parser.c.

## _str3_cmp

#define _str3_cmp | ( |  | m,
|  |  | c0,
|  |  | c1,
|  |  | c2,
|  |  | c3 )

Value:
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)

Definition at line 247 of file parser.c.

## _str3Ocmp

#define _str3Ocmp | ( |  | m,
|  |  | c0,
|  |  | c1,
|  |  | c2,
|  |  | c3 )

Value:
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)

Definition at line 250 of file parser.c.

## _str4cmp

#define _str4cmp | ( |  | m,
|  |  | c0,
|  |  | c1,
|  |  | c2,
|  |  | c3 )

Value:
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)

Definition at line 253 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 256 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 260 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 264 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 268 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 272 of file parser.c.

## CR

#define CR (unsigned char)13

Definition at line 20 of file parser.c.

## CRLF

#define CRLF "\x0d\x0a"

Definition at line 21 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)
evhtp_likely
#define evhtp_likely(x)
Definition internal.h:17
PARSER_STACK_MAX
#define PARSER_STACK_MAX
Definition parser.c:18

Definition at line 711 of file parser.c.

## LF

#define LF (unsigned char)10

Definition at line 19 of file parser.c.

## log_htparser__s_

#define log_htparser__s_ | ( |  | p | ) |

Definition at line 182 of file parser.c.

## PARSER_STACK_MAX

#define PARSER_STACK_MAX 8192

Definition at line 18 of file parser.c.

## Typedef Documentation

## eval_hdr_val

typedef enum eval_hdr_val eval_hdr_val

Definition at line 86 of file parser.c.

## parser_flags

typedef enum parser_flags parser_flags

Definition at line 87 of file parser.c.

## parser_state

typedef enum parser_state parser_state

Definition at line 88 of file parser.c.

## Enumeration Type Documentation

## eval_hdr_val

enum 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 23 of file parser.c.

## parser_flags

enum parser_flags

Enumerator
parser_flag_chunked |
parser_flag_connection_keep_alive |
parser_flag_connection_close |
parser_flag_trailing |

Definition at line 33 of file parser.c.

## parser_state

enum 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 40 of file parser.c.

## Function Documentation

## _str_to_ssize_t()

ssize_t _str_to_ssize_t | ( | char * | str,
|  | size_t | n )

inline static

Definition at line 396 of file parser.c.

## get_method()

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

static

Definition at line 605 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 557 of file parser.c.

## htparser_get_content_length()

uint64_t htparser_get_content_length | ( | htparser * | p | ) |

Definition at line 551 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 545 of file parser.c.

## htparser_get_error()

htpparse_error htparser_get_error | ( | htparser * | p | ) |

Definition at line 426 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 515 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 480 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 497 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 486 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 521 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 527 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 474 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 445 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 432 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 563 of file parser.c.

## htparser_get_userdata()

void * htparser_get_userdata | ( | htparser * | p | ) |

Definition at line 533 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 569 of file parser.c.

Here is the caller graph for this function:

## htparser_new()

htparser * htparser_new | ( | void |  | ) |

Definition at line 581 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 723 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 503 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 509 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 539 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 451 of file parser.c.

Here is the caller graph for this function:

## is_host_char()

int is_host_char | ( | unsigned char | ch | ) |

static

Definition at line 587 of file parser.c.

Here is the caller graph for this function:

## str_to_uint64()

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

inline static

Definition at line 354 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_status",
"htparse_error_unknown"
}

Definition at line 207 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 224 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 196 of file parser.c.

## usual

uint32_t usual[]

static

Initial value:
= {
0xffffdbfe,
0x7fff37d6,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff
}

Definition at line 185 of file parser.c.