summaryrefslogtreecommitdiff
path: root/include/fhp/fhp.h
blob: d537a6e8a264110af62deb133eb7c2713ba3f22f (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#ifndef FHP_H_
#define FHP_H

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

//
// hash functions (djb2)
// (see http://aras-p.info/blog/2016/08/02/Hash-Functions-all-the-way-down/)
//

uint32_t fhp_hash_init(void);
uint32_t fhp_hash_push(uint32_t, uint8_t * const, size_t);
uint32_t fhp_hash_string(char * const);
uint32_t fhp_lc_hash_push(uint32_t, uint8_t * const, size_t);
uint32_t fhp_lc_hash_string(char * const);

//
// error functions
//

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_INVALID_BODY_TYPE,
  FHP_ERR_TOO_MANY_TES,
  FHP_ERR_INVALID_CHAR_IN_TE_NAME,
  FHP_ERR_INVALID_CHAR_IN_TE,
  FHP_ERR_BAD_TE_STATE,
  FHP_ERR_TE_PARSER_DONE,
  FHP_ERR_LAST
} fhp_err_t;

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

//
// token functions
//

typedef enum {
  FHP_TOKEN_METHOD_START,
  FHP_TOKEN_METHOD_FRAGMENT,
  FHP_TOKEN_METHOD_END,

  FHP_TOKEN_METHOD_GET,
  FHP_TOKEN_METHOD_POST,
  FHP_TOKEN_METHOD_HEAD,
  FHP_TOKEN_METHOD_PUT,
  FHP_TOKEN_METHOD_DELETE,
  FHP_TOKEN_METHOD_OPTIONS,
  FHP_TOKEN_METHOD_OTHER,

  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_VERSION_HTTP_10,
  FHP_TOKEN_VERSION_HTTP_11,
  FHP_TOKEN_VERSION_OTHER,

  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 enum {
  FHP_BODY_TYPE_NONE,
  FHP_BODY_TYPE_CONTENT_LENGTH,
  FHP_BODY_TYPE_TRANSFER_ENCODING,
  FHP_BODY_TYPE_LAST
} fhp_body_type_t;

//
// env functions
//

#define FHP_ENV_NUM_HASHES 6

typedef struct {
  uint32_t hashes[FHP_ENV_NUM_HASHES];
} fhp_env_t;

void fhp_env_init(fhp_env_t * const env);
fhp_env_t *fhp_get_default_env(void);

//
// transfer encoding parser functions
//

typedef enum {
  FHP_TE_STATE_INIT,
  FHP_TE_STATE_NAME,
  FHP_TE_STATE_IGNORE_UNTIL_COMMA,
  FHP_TE_STATE_SPACE,
  FHP_TE_STATE_ERROR,
  FHP_TE_STATE_DONE,
  FHP_TE_STATE_LAST
} fhp_te_state_t;

#define FHP_TE_MAX_BUF_SIZE 16
#define FHP_TE_MAX_TES 4

typedef struct {
  fhp_te_state_t state;

  uint8_t buf[FHP_TE_MAX_BUF_SIZE];
  size_t len;

  uint32_t buf_hash;

  uint32_t tes[FHP_TE_MAX_TES];
  size_t num_tes;

  fhp_err_t err;
} fhp_te_parser_t;

fhp_err_t
fhp_te_parser_init(fhp_te_parser_t * const te);

fhp_err_t
fhp_te_parser_push(fhp_te_parser_t *, uint8_t * const, size_t);

fhp_err_t
fhp_te_parser_done(fhp_te_parser_t * const);

//
// context functions
//

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_MAX_BUF_SIZE 1024
#define FHP_MAX_TRANSFER_ENCODINGS 4

struct fhp_t_ {
  // env pointer
  fhp_env_t *env;

  // opaque user data
  void *user_data;

  // current parser state
  fhp_state_t state;

  // user callback
  fhp_cb_t cb;

  // last error
  fhp_err_t err;

  // number of bytes read
  uint64_t ofs;

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

  // buffer hashing state
  bool is_hashing;
  uint32_t buf_hash;

  // cached http method and version
  fhp_token_t http_method, http_version;

  // hash of last header name
  uint32_t header_name_hash;

  // request body type
  fhp_body_type_t body_type;

  // content length
  uint64_t content_length;

  // transfer encodings
  uint32_t tes[FHP_MAX_TRANSFER_ENCODINGS];
  size_t num_tes;

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

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

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

fhp_env_t *
fhp_get_env(fhp_t * const);

void *
fhp_get_user_data(fhp_t * const);

#endif /* FHP_H */