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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/* Copyright 2015 greenbytes GmbH (https://www.greenbytes.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __mod_h2__h2_util__
#define __mod_h2__h2_util__
#include <nghttp2/nghttp2.h>
/*******************************************************************************
* some debugging/format helpers
******************************************************************************/
struct h2_request;
struct nghttp2_frame;
size_t h2_util_hex_dump(char *buffer, size_t maxlen,
const char *data, size_t datalen);
size_t h2_util_header_print(char *buffer, size_t maxlen,
const char *name, size_t namelen,
const char *value, size_t valuelen);
void h2_util_camel_case_header(char *s, size_t len);
int h2_util_frame_print(const nghttp2_frame *frame, char *buffer, size_t maxlen);
/*******************************************************************************
* ihash - hash for structs with int identifier
******************************************************************************/
typedef struct h2_ihash_t h2_ihash_t;
typedef int h2_ihash_iter_t(void *ctx, void *val);
/**
* Create a hash for structures that have an identifying int member.
* @param pool the pool to use
* @param offset_of_int the offsetof() the int member in the struct
*/
h2_ihash_t *h2_ihash_create(apr_pool_t *pool, size_t offset_of_int);
size_t h2_ihash_count(h2_ihash_t *ih);
int h2_ihash_empty(h2_ihash_t *ih);
void *h2_ihash_get(h2_ihash_t *ih, int id);
/**
* Iterate over the hash members (without defined order) and invoke
* fn for each member until 0 is returned.
* @param ih the hash to iterate over
* @param fn the function to invoke on each member
* @param ctx user supplied data passed into each iteration call
* @param 0 if one iteration returned 0, otherwise != 0
*/
int h2_ihash_iter(h2_ihash_t *ih, h2_ihash_iter_t *fn, void *ctx);
void h2_ihash_add(h2_ihash_t *ih, void *val);
void h2_ihash_remove(h2_ihash_t *ih, int id);
void h2_ihash_clear(h2_ihash_t *ih);
/*******************************************************************************
* ilist - sorted list for structs with int identifier as first member
******************************************************************************/
typedef struct h2_ilist_t h2_ilist_t;
typedef int h2_ilist_iter_t(void *ctx, void *val);
h2_ilist_t *h2_ilist_create(apr_pool_t *pool);
apr_status_t h2_ilist_add(h2_ilist_t *list, void *val);
void *h2_ilist_get(h2_ilist_t *list, int id);
void *h2_ilist_shift(h2_ilist_t *list);
void *h2_ilist_remove(h2_ilist_t *list, int id);
int h2_ilist_empty(h2_ilist_t *list);
apr_size_t h2_ilist_count(h2_ilist_t *list);
/* Iterator over all h2_io* in the set or until a
* callback returns 0. It is not safe to add or remove
* set members during iteration.
*
* @param set the set of h2_io to iterate over
* @param iter the function to call for each io
* @param ctx user data for the callback
* @return 1 iff iteration completed for all members
*/
int h2_ilist_iter(h2_ilist_t *lis, h2_ilist_iter_t *iter, void *ctx);
/*******************************************************************************
* iqueue - sorted list of int with user defined ordering
******************************************************************************/
typedef struct h2_iqueue {
int *elts;
int head;
int nelts;
int nalloc;
apr_pool_t *pool;
} h2_iqueue;
/**
* Comparator for two int to determine their order.
*
* @param i1 first int to compare
* @param i2 second int to compare
* @param ctx provided user data
* @return value is the same as for strcmp() and has the effect:
* == 0: s1 and s2 are treated equal in ordering
* < 0: s1 should be sorted before s2
* > 0: s2 should be sorted before s1
*/
typedef int h2_iq_cmp(int i1, int i2, void *ctx);
/**
* Allocate a new queue from the pool and initialize.
* @param id the identifier of the queue
* @param pool the memory pool
*/
h2_iqueue *h2_iq_create(apr_pool_t *pool, int capacity);
/**
* Return != 0 iff there are no tasks in the queue.
* @param q the queue to check
*/
int h2_iq_empty(h2_iqueue *q);
/**
* Return the number of int in the queue.
* @param q the queue to get size on
*/
int h2_iq_count(h2_iqueue *q);
/**
* Add a stream idto the queue.
*
* @param q the queue to append the task to
* @param sid the stream id to add
* @param cmp the comparator for sorting
* @param ctx user data for comparator
*/
void h2_iq_add(h2_iqueue *q, int i, h2_iq_cmp *cmp, void *ctx);
/**
* Remove the stream id from the queue. Return != 0 iff task
* was found in queue.
* @param q the task queue
* @param sid the stream id to remove
* @return != 0 iff task was found in queue
*/
int h2_iq_remove(h2_iqueue *q, int i);
/**
* Remove all entries in the queue.
*/
void h2_iq_clear(h2_iqueue *q);
/**
* Sort the stream idqueue again. Call if the task ordering
* has changed.
*
* @param q the queue to sort
* @param cmp the comparator for sorting
* @param ctx user data for the comparator
*/
void h2_iq_sort(h2_iqueue *q, h2_iq_cmp *cmp, void *ctx);
/**
* Get the first stream id from the queue or NULL if the queue is empty.
* The task will be removed.
*
* @param q the queue to get the first task from
* @return the first stream id of the queue, 0 if empty
*/
int h2_iq_shift(h2_iqueue *q);
/*******************************************************************************
* common helpers
******************************************************************************/
/* h2_log2(n) iff n is a power of 2 */
unsigned char h2_log2(apr_uint32_t n);
/**
* Count the bytes that all key/value pairs in a table have
* in length (exlucding terminating 0s), plus additional extra per pair.
*
* @param t the table to inspect
* @param pair_extra the extra amount to add per pair
* @return the number of bytes all key/value pairs have
*/
apr_size_t h2_util_table_bytes(apr_table_t *t, apr_size_t pair_extra);
/**
* Return != 0 iff the string s contains the token, as specified in
* HTTP header syntax, rfc7230.
*/
int h2_util_contains_token(apr_pool_t *pool, const char *s, const char *token);
const char *h2_util_first_token_match(apr_pool_t *pool, const char *s,
const char *tokens[], apr_size_t len);
/** Match a header value against a string constance, case insensitive */
#define H2_HD_MATCH_LIT(l, name, nlen) \
((nlen == sizeof(l) - 1) && !apr_strnatcasecmp(l, name))
/*******************************************************************************
* HTTP/2 header helpers
******************************************************************************/
int h2_req_ignore_header(const char *name, size_t len);
int h2_req_ignore_trailer(const char *name, size_t len);
int h2_res_ignore_trailer(const char *name, size_t len);
int h2_proxy_res_ignore_header(const char *name, size_t len);
/**
* Set the push policy for the given request. Takes request headers into
* account, see draft https://tools.ietf.org/html/draft-ruellan-http-accept-push-policy-00
* for details.
*
* @param req the request to determine the policy for
* @param p the pool to use
* @param push_enabled if HTTP/2 server push is generally enabled for this request
*/
void h2_push_policy_determine(struct h2_request *req, apr_pool_t *p, int push_enabled);
/*******************************************************************************
* base64 url encoding, different table from normal base64
******************************************************************************/
/**
* I always wanted to write my own base64url decoder...not. See
* https://tools.ietf.org/html/rfc4648#section-5 for description.
*/
apr_size_t h2_util_base64url_decode(const char **decoded,
const char *encoded,
apr_pool_t *pool);
const char *h2_util_base64url_encode(const char *data,
apr_size_t len, apr_pool_t *pool);
/*******************************************************************************
* nghttp2 helpers
******************************************************************************/
#define H2_HD_MATCH_LIT_CS(l, name) \
((strlen(name) == sizeof(l) - 1) && !apr_strnatcasecmp(l, name))
#define H2_CREATE_NV_LIT_CS(nv, NAME, VALUE) nv->name = (uint8_t *)NAME; \
nv->namelen = sizeof(NAME) - 1; \
nv->value = (uint8_t *)VALUE; \
nv->valuelen = strlen(VALUE)
#define H2_CREATE_NV_CS_LIT(nv, NAME, VALUE) nv->name = (uint8_t *)NAME; \
nv->namelen = strlen(NAME); \
nv->value = (uint8_t *)VALUE; \
nv->valuelen = sizeof(VALUE) - 1
#define H2_CREATE_NV_CS_CS(nv, NAME, VALUE) nv->name = (uint8_t *)NAME; \
nv->namelen = strlen(NAME); \
nv->value = (uint8_t *)VALUE; \
nv->valuelen = strlen(VALUE)
int h2_util_ignore_header(const char *name);
typedef struct h2_ngheader {
nghttp2_nv *nv;
apr_size_t nvlen;
} h2_ngheader;
h2_ngheader *h2_util_ngheader_make(apr_pool_t *p, apr_table_t *header);
h2_ngheader *h2_util_ngheader_make_res(apr_pool_t *p,
int http_status,
apr_table_t *header);
h2_ngheader *h2_util_ngheader_make_req(apr_pool_t *p,
const struct h2_request *req);
apr_status_t h2_headers_add_h1(apr_table_t *headers, apr_pool_t *pool,
const char *name, size_t nlen,
const char *value, size_t vlen);
/*******************************************************************************
* h2_request helpers
******************************************************************************/
struct h2_request *h2_req_createn(int id, apr_pool_t *pool, const char *method,
const char *scheme, const char *authority,
const char *path, apr_table_t *header,
int serialize);
struct h2_request *h2_req_create(int id, apr_pool_t *pool, int serialize);
apr_status_t h2_req_make(struct h2_request *req, apr_pool_t *pool,
const char *method, const char *scheme,
const char *authority, const char *path,
apr_table_t *headers);
/*******************************************************************************
* apr brigade helpers
******************************************************************************/
/**
* Concatenate at most length bytes from src to dest brigade, splitting
* buckets if necessary and reading buckets of indeterminate length.
*/
apr_status_t h2_brigade_concat_length(apr_bucket_brigade *dest,
apr_bucket_brigade *src,
apr_off_t length);
/**
* Copy at most length bytes from src to dest brigade, splitting
* buckets if necessary and reading buckets of indeterminate length.
*/
apr_status_t h2_brigade_copy_length(apr_bucket_brigade *dest,
apr_bucket_brigade *src,
apr_off_t length);
/**
* Return != 0 iff there is a FLUSH or EOS bucket in the brigade.
* @param bb the brigade to check on
* @return != 0 iff brigade holds FLUSH or EOS bucket (or both)
*/
int h2_util_has_eos(apr_bucket_brigade *bb, apr_off_t len);
/**
* Check how many bytes of the desired amount are available and if the
* end of stream is reached by that amount.
* @param bb the brigade to check
* @param plen the desired length and, on return, the available length
* @param on return, if eos has been reached
*/
apr_status_t h2_util_bb_avail(apr_bucket_brigade *bb,
apr_off_t *plen, int *peos);
typedef apr_status_t h2_util_pass_cb(void *ctx,
const char *data, apr_off_t len);
/**
* Read at most *plen bytes from the brigade and pass them into the
* given callback. If cb is NULL, just return the amount of data that
* could have been read.
* If an EOS was/would be encountered, set *peos != 0.
* @param bb the brigade to read from
* @param cb the callback to invoke for the read data
* @param ctx optional data passed to callback
* @param plen inout, as input gives the maximum number of bytes to read,
* on return specifies the actual/would be number of bytes
* @param peos != 0 iff an EOS bucket was/would be encountered.
*/
apr_status_t h2_util_bb_readx(apr_bucket_brigade *bb,
h2_util_pass_cb *cb, void *ctx,
apr_off_t *plen, int *peos);
/**
* Print a bucket's meta data (type and length) to the buffer.
* @return number of characters printed
*/
apr_size_t h2_util_bucket_print(char *buffer, apr_size_t bmax,
apr_bucket *b, const char *sep);
/**
* Prints the brigade bucket types and lengths into the given buffer
* up to bmax.
* @return number of characters printed
*/
apr_size_t h2_util_bb_print(char *buffer, apr_size_t bmax,
const char *tag, const char *sep,
apr_bucket_brigade *bb);
/**
* Logs the bucket brigade (which bucket types with what length)
* to the log at the given level.
* @param c the connection to log for
* @param stream_id the stream identifier this brigade belongs to
* @param level the log level (as in APLOG_*)
* @param tag a short message text about the context
* @param bb the brigade to log
*/
#define h2_util_bb_log(c, i, level, tag, bb) \
do { \
char buffer[4 * 1024]; \
const char *line = "(null)"; \
apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); \
len = h2_util_bb_print(buffer, bmax, (tag), "", (bb)); \
ap_log_cerror(APLOG_MARK, level, 0, (c), "bb_dump(%ld-%d): %s", \
(c)->id, (int)(i), (len? buffer : line)); \
} while(0)
/**
* Transfer buckets from one brigade to another with a limit on the
* maximum amount of bytes transfered. Does no setaside magic, lifetime
* of brigades must fit.
* @param to brigade to transfer buckets to
* @param from brigades to remove buckets from
* @param plen maximum bytes to transfer, actual bytes transferred
* @param peos if an EOS bucket was transferred
*/
apr_status_t h2_append_brigade(apr_bucket_brigade *to,
apr_bucket_brigade *from,
apr_off_t *plen,
int *peos);
/**
* Get an approximnation of the memory footprint of the given
* brigade. This varies from apr_brigade_length as
* - no buckets are ever read
* - only buckets known to allocate memory (HEAP+POOL) are counted
* - the bucket struct itself is counted
*/
apr_off_t h2_brigade_mem_size(apr_bucket_brigade *bb);
/* when will ap_casecmpstr() be backported finally? */
int h2_casecmpstr(const char *s1, const char *s2);
int h2_casecmpstrn(const char *s1, const char *s2, apr_size_t n);
#endif /* defined(__mod_h2__h2_util__) */
|