summaryrefslogtreecommitdiff
path: root/include/fhp/fhp.h
blob: 563d2adda6584e4b421a7a86bdf7c50203c27760 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef FHP_H_
#define FHP_H

#include <stdint.h> // for uint8_t
#include <stddef.h> // for size_t
#include <stdbool.h> // for size_t

typedef enum {
  FHP_OK,
  FHP_ERR_CB,
  FHP_ERR_BAD_STATE,
  FHP_ERR_INVALID_CHAR,
  FHP_ERR_INVALID_CHAR_IN_METHOD,
  FHP_ERR_INVALID_CHAR_IN_URL,
  FHP_ERR_INVALID_CHAR_IN_URL_PERCENT,
  FHP_ERR_INVALID_CHAR_IN_VERSION,
  FHP_ERR_INVALID_CHAR_AFTER_CR,
  FHP_ERR_INVALID_CHAR_IN_HEADER_NAME,
  FHP_ERR_INVALID_ERROR,
  FHP_ERR_BUFFER_TOO_SMALL,
  FHP_ERR_LAST
} fhp_err_t;

fhp_err_t
fhp_strerror(fhp_err_t, char * const, size_t);

typedef enum {
  FHP_TOKEN_METHOD_START,
  FHP_TOKEN_METHOD_FRAGMENT,
  FHP_TOKEN_METHOD_END,

  FHP_TOKEN_URL_START,
  FHP_TOKEN_URL_FRAGMENT,
  FHP_TOKEN_URL_END,

  FHP_TOKEN_VERSION_START,
  FHP_TOKEN_VERSION_FRAGMENT,
  FHP_TOKEN_VERSION_END,

  FHP_TOKEN_HEADER_NAME_START,
  FHP_TOKEN_HEADER_NAME_FRAGMENT,
  FHP_TOKEN_HEADER_NAME_END,

  FHP_TOKEN_HEADER_VALUE_START,
  FHP_TOKEN_HEADER_VALUE_FRAGMENT,
  FHP_TOKEN_HEADER_VALUE_END,

  FHP_TOKEN_LAST
} fhp_token_t;

fhp_err_t
fhp_strtoken(fhp_token_t, char * const, size_t);

typedef struct fhp_t_ fhp_t;

typedef bool (*fhp_cb_t)(
  fhp_t * const,
  fhp_token_t,
  uint8_t * const,
  size_t
);

typedef enum {
  FHP_STATE_INIT,
  FHP_STATE_ERROR,
  FHP_STATE_METHOD,
  FHP_STATE_METHOD_END,
  FHP_STATE_URL,
  FHP_STATE_URL_PERCENT,
  FHP_STATE_URL_PERCENT_LAST,
  FHP_STATE_URL_END,
  FHP_STATE_VERSION,
  FHP_STATE_VERSION_END,
  FHP_STATE_VERSION_END_CR,
  FHP_STATE_STATUS_END,
  FHP_STATE_STATUS_END_CR,
  FHP_STATE_HEADER_NAME,
  FHP_STATE_HEADER_NAME_END,
  FHP_STATE_HEADER_NAME_END_CR,
  FHP_STATE_HEADER_VALUE,
  FHP_STATE_HEADER_VALUE_END_CR,
  FHP_STATE_HEADER_VALUE_END,
  FHP_STATE_HEADERS_END,
  FHP_STATE_HEADERS_END_CR,
  FHP_STATE_LAST
} fhp_state_t;

#define FHP_BUF_SIZE 1024

struct fhp_t_ {
  // current parser state
  fhp_state_t state;

  // user callback
  fhp_cb_t cb;

  // opaque user data
  void *user_data;

  // last error
  fhp_err_t err;

  // number of bytes read
  uint64_t ofs;

  // fragment data buffer
  uint8_t buf[FHP_BUF_SIZE];
  size_t buf_len;

  // state for url hex decoder
  uint32_t hex;
};

fhp_err_t
fhp_init(fhp_t * const, fhp_cb_t, void * const);

fhp_err_t
fhp_push(fhp_t * const, uint8_t * const, size_t);

void *
fhp_user_data(fhp_t * const);

#endif /* FHP_H */