Apache2
h2_util.h
Go to the documentation of this file.
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements. See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __mod_h2__h2_util__
18 #define __mod_h2__h2_util__
19 
20 #include <nghttp2/nghttp2.h>
21 #include <http_protocol.h>
22 
23 #include "h2.h"
24 #include "h2_headers.h"
25 
26 /*******************************************************************************
27  * some debugging/format helpers
28  ******************************************************************************/
29 struct h2_request;
30 struct nghttp2_frame;
31 
32 size_t h2_util_hex_dump(char *buffer, size_t maxlen,
33  const char *data, size_t datalen);
34 
35 void h2_util_camel_case_header(char *s, size_t len);
36 
37 int h2_util_frame_print(const nghttp2_frame *frame, char *buffer, size_t maxlen);
38 
39 /*******************************************************************************
40  * ihash - hash for structs with int identifier
41  ******************************************************************************/
42 typedef struct h2_ihash_t h2_ihash_t;
43 typedef int h2_ihash_iter_t(void *ctx, void *val);
44 
50 h2_ihash_t *h2_ihash_create(apr_pool_t *pool, size_t offset_of_int);
51 
52 unsigned int h2_ihash_count(h2_ihash_t *ih);
54 void *h2_ihash_get(h2_ihash_t *ih, int id);
55 
64 int h2_ihash_iter(h2_ihash_t *ih, h2_ihash_iter_t *fn, void *ctx);
65 
66 void h2_ihash_add(h2_ihash_t *ih, void *val);
67 void h2_ihash_remove(h2_ihash_t *ih, int id);
68 void h2_ihash_remove_val(h2_ihash_t *ih, void *val);
70 
71 size_t h2_ihash_shift(h2_ihash_t *ih, void **buffer, size_t max);
72 
73 /*******************************************************************************
74  * iqueue - sorted list of int with user defined ordering
75  ******************************************************************************/
76 typedef struct h2_iqueue {
77  int *elts;
78  int head;
79  int nelts;
80  int nalloc;
83 
95 typedef int h2_iq_cmp(int i1, int i2, void *ctx);
96 
103 
109 
115 
125 int h2_iq_add(h2_iqueue *q, int sid, h2_iq_cmp *cmp, void *ctx);
126 
134 int h2_iq_append(h2_iqueue *q, int sid);
135 
142 int h2_iq_remove(h2_iqueue *q, int sid);
143 
148 
157 void h2_iq_sort(h2_iqueue *q, h2_iq_cmp *cmp, void *ctx);
158 
167 
176 size_t h2_iq_mshift(h2_iqueue *q, int *pint, size_t max);
177 
185 int h2_iq_contains(h2_iqueue *q, int sid);
186 
187 /*******************************************************************************
188  * FIFO queue (void* elements)
189  ******************************************************************************/
190 
195 typedef struct h2_fifo h2_fifo;
196 
202 
209 
211 
213 
222 apr_status_t h2_fifo_push(h2_fifo *fifo, void *elem);
224 
225 apr_status_t h2_fifo_pull(h2_fifo *fifo, void **pelem);
226 apr_status_t h2_fifo_try_pull(h2_fifo *fifo, void **pelem);
227 
228 typedef enum {
229  H2_FIFO_OP_PULL, /* pull the element from the queue, ie discard it */
230  H2_FIFO_OP_REPUSH, /* pull and immediately re-push it */
231 } h2_fifo_op_t;
232 
233 typedef h2_fifo_op_t h2_fifo_peek_fn(void *head, void *ctx);
234 
244 
249 
256 
257 /*******************************************************************************
258  * iFIFO queue (int elements)
259  ******************************************************************************/
260 
265 typedef struct h2_ififo h2_ififo;
266 
272 
279 
281 
283 
294 
297 
298 typedef h2_fifo_op_t h2_ififo_peek_fn(int head, void *ctx);
299 
309 
314 
321 
322 /*******************************************************************************
323  * common helpers
324  ******************************************************************************/
325 /* h2_log2(n) iff n is a power of 2 */
326 unsigned char h2_log2(int n);
327 
337 
339 #define H2_HD_MATCH_LIT(l, name, nlen) \
340  ((nlen == sizeof(l) - 1) && !ap_cstr_casecmp(l, name))
341 
342 /*******************************************************************************
343  * HTTP/2 header helpers
344  ******************************************************************************/
345 int h2_ignore_req_trailer(const char *name, size_t len);
346 int h2_ignore_resp_trailer(const char *name, size_t len);
347 
359 
360 /*******************************************************************************
361  * base64 url encoding, different table from normal base64
362  ******************************************************************************/
367 apr_size_t h2_util_base64url_decode(const char **decoded,
368  const char *encoded,
369  apr_pool_t *pool);
370 const char *h2_util_base64url_encode(const char *data,
371  apr_size_t len, apr_pool_t *pool);
372 
373 /*******************************************************************************
374  * nghttp2 helpers
375  ******************************************************************************/
376 
378 
379 typedef struct h2_ngheader {
380  nghttp2_nv *nv;
383 
384 #if AP_HAS_RESPONSE_BUCKETS
390  const struct h2_request *req);
391 #else
393  struct h2_headers *headers);
395  struct h2_headers *headers);
397  const struct h2_request *req);
398 #endif
399 
405  const char *name, size_t nlen,
406  const char *value, size_t vlen,
407  size_t max_field_len, int *pwas_added);
408 
409 /*******************************************************************************
410  * apr brigade helpers
411  ******************************************************************************/
412 
419  apr_off_t length);
420 
427  apr_off_t length);
428 
429 typedef apr_status_t h2_util_pass_cb(void *ctx,
430  const char *data, apr_off_t len);
431 
437  apr_bucket *b, const char *sep);
438 
445  const char *tag, const char *sep,
456 #define h2_util_bb_log(c, sid, level, tag, bb) \
457 if (APLOG_C_IS_LEVEL(c, level)) { \
458  do { \
459  char buffer[4 * 1024]; \
460  const char *line = "(null)"; \
461  apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); \
462  len = h2_util_bb_print(buffer, bmax, (tag), "", (bb)); \
463  ap_log_cerror(APLOG_MARK, level, 0, (c), "bb_dump(%ld): %s", \
464  ((c)->master? (c)->master->id : (c)->id), (len? buffer : line)); \
465  } while(0); \
466 }
467 
468 
469 typedef int h2_bucket_gate(apr_bucket *b);
480  apr_bucket_brigade *from,
481  apr_off_t *plen,
482  int *peos,
483  h2_bucket_gate *should_append);
484 
493 
498 
503 
504 
505 #if AP_HAS_RESPONSE_BUCKETS
510 apr_size_t headers_length_estimate(ap_bucket_headers *hdrs);
511 
516 apr_size_t response_length_estimate(ap_bucket_response *resp);
517 #endif /* AP_HAS_RESPONSE_BUCKETS */
518 
519 #endif /* defined(__mod_h2__h2_util__) */
dav_error dav_response * response
Definition: mod_dav.h:204
apr_bucket_brigade * bb
Definition: mod_dav.h:555
const char * s
Definition: mod_dav.h:1327
dav_error * src
Definition: mod_dav.h:186
apr_bucket_brigade request_rec apr_pool_t * pool
Definition: mod_dav.h:557
const char * name
Definition: mod_dav.h:805
apr_table_t * headers
Definition: mod_proxy.h:1484
int apr_status_t
Definition: apr_errno.h:44
off_t apr_off_t
Definition: apr.h:396
size_t apr_size_t
Definition: apr.h:394
struct apr_pool_t apr_pool_t
Definition: apr_pools.h:60
struct apr_table_t apr_table_t
Definition: apr_tables.h:56
void h2_iq_sort(h2_iqueue *q, h2_iq_cmp *cmp, void *ctx)
void h2_ihash_clear(h2_ihash_t *ih)
size_t h2_util_hex_dump(char *buffer, size_t maxlen, const char *data, size_t datalen)
void h2_ihash_add(h2_ihash_t *ih, void *val)
apr_status_t h2_res_create_ngtrailer(h2_ngheader **ph, apr_pool_t *p, struct h2_headers *headers)
h2_fifo_op_t
Definition: h2_util.h:228
@ H2_FIFO_OP_REPUSH
Definition: h2_util.h:230
@ H2_FIFO_OP_PULL
Definition: h2_util.h:229
apr_status_t h2_append_brigade(apr_bucket_brigade *to, apr_bucket_brigade *from, apr_off_t *plen, int *peos, h2_bucket_gate *should_append)
apr_status_t h2_util_wait_on_pipe(apr_file_t *pipe)
apr_status_t h2_fifo_peek(h2_fifo *fifo, h2_fifo_peek_fn *fn, void *ctx)
int h2_iq_add(h2_iqueue *q, int sid, h2_iq_cmp *cmp, void *ctx)
apr_status_t h2_ififo_try_peek(h2_ififo *fifo, h2_ififo_peek_fn *fn, void *ctx)
int h2_iq_cmp(int i1, int i2, void *ctx)
Definition: h2_util.h:95
apr_status_t h2_brigade_copy_length(apr_bucket_brigade *dest, apr_bucket_brigade *src, apr_off_t length)
struct h2_fifo h2_fifo
Definition: h2_util.h:195
int h2_ignore_req_trailer(const char *name, size_t len)
apr_status_t h2_ififo_term(h2_ififo *fifo)
void h2_util_drain_pipe(apr_file_t *pipe)
struct h2_ngheader h2_ngheader
h2_ihash_t * h2_ihash_create(apr_pool_t *pool, size_t offset_of_int)
apr_status_t h2_res_create_ngheader(h2_ngheader **ph, apr_pool_t *p, struct h2_headers *headers)
int h2_util_ignore_resp_header(const char *name)
void h2_ihash_remove_val(h2_ihash_t *ih, void *val)
size_t h2_iq_mshift(h2_iqueue *q, int *pint, size_t max)
apr_status_t h2_fifo_try_pull(h2_fifo *fifo, void **pelem)
void h2_ihash_remove(h2_ihash_t *ih, int id)
int h2_iq_contains(h2_iqueue *q, int sid)
apr_status_t h2_ififo_try_push(h2_ififo *fifo, int id)
apr_status_t h2_fifo_create(h2_fifo **pfifo, apr_pool_t *pool, int capacity)
int h2_iq_empty(h2_iqueue *q)
apr_status_t h2_ififo_peek(h2_ififo *fifo, h2_ififo_peek_fn *fn, void *ctx)
void * h2_ihash_get(h2_ihash_t *ih, int id)
int h2_iq_append(h2_iqueue *q, int sid)
apr_size_t h2_util_bucket_print(char *buffer, apr_size_t bmax, apr_bucket *b, const char *sep)
apr_status_t h2_fifo_set_create(h2_fifo **pfifo, apr_pool_t *pool, int capacity)
apr_status_t h2_brigade_concat_length(apr_bucket_brigade *dest, apr_bucket_brigade *src, apr_off_t length)
int h2_bucket_gate(apr_bucket *b)
Definition: h2_util.h:469
void h2_util_camel_case_header(char *s, size_t len)
apr_status_t h2_fifo_try_peek(h2_fifo *fifo, h2_fifo_peek_fn *fn, void *ctx)
apr_status_t h2_ififo_remove(h2_ififo *fifo, int id)
apr_status_t h2_ififo_create(h2_ififo **pfifo, apr_pool_t *pool, int capacity)
apr_status_t h2_util_pass_cb(void *ctx, const char *data, apr_off_t len)
Definition: h2_util.h:429
int h2_fifo_count(h2_fifo *fifo)
apr_status_t h2_fifo_term(h2_fifo *fifo)
int h2_util_frame_print(const nghttp2_frame *frame, char *buffer, size_t maxlen)
int h2_ignore_resp_trailer(const char *name, size_t len)
int h2_ihash_iter_t(void *ctx, void *val)
Definition: h2_util.h:43
apr_status_t h2_req_add_header(apr_table_t *headers, apr_pool_t *pool, const char *name, size_t nlen, const char *value, size_t vlen, size_t max_field_len, int *pwas_added)
int h2_ihash_iter(h2_ihash_t *ih, h2_ihash_iter_t *fn, void *ctx)
apr_status_t h2_fifo_pull(h2_fifo *fifo, void **pelem)
int h2_iq_remove(h2_iqueue *q, int sid)
size_t h2_ihash_shift(h2_ihash_t *ih, void **buffer, size_t max)
unsigned int h2_ihash_count(h2_ihash_t *ih)
apr_status_t h2_req_create_ngheader(h2_ngheader **ph, apr_pool_t *p, const struct h2_request *req)
int h2_ififo_count(h2_ififo *fifo)
int h2_push_policy_determine(apr_table_t *headers, apr_pool_t *p, int push_enabled)
apr_status_t h2_fifo_push(h2_fifo *fifo, void *elem)
apr_status_t h2_fifo_remove(h2_fifo *fifo, void *elem)
struct h2_iqueue h2_iqueue
apr_size_t h2_util_base64url_decode(const char **decoded, const char *encoded, apr_pool_t *pool)
struct h2_ififo h2_ififo
Definition: h2_util.h:265
unsigned char h2_log2(int n)
apr_off_t h2_brigade_mem_size(apr_bucket_brigade *bb)
apr_size_t h2_util_table_bytes(apr_table_t *t, apr_size_t pair_extra)
apr_status_t h2_ififo_set_create(h2_ififo **pfifo, apr_pool_t *pool, int capacity)
struct h2_ihash_t h2_ihash_t
Definition: h2_util.h:42
apr_status_t h2_ififo_push(h2_ififo *fifo, int id)
h2_iqueue * h2_iq_create(apr_pool_t *pool, int capacity)
void h2_iq_clear(h2_iqueue *q)
int h2_ihash_empty(h2_ihash_t *ih)
apr_status_t h2_ififo_try_pull(h2_ififo *fifo, int *pi)
h2_fifo_op_t h2_ififo_peek_fn(int head, void *ctx)
Definition: h2_util.h:298
int h2_iq_shift(h2_iqueue *q)
const char * h2_util_base64url_encode(const char *data, apr_size_t len, apr_pool_t *pool)
int h2_iq_count(h2_iqueue *q)
apr_status_t h2_fifo_try_push(h2_fifo *fifo, void *elem)
apr_status_t h2_ififo_pull(h2_ififo *fifo, int *pi)
apr_size_t h2_util_bb_print(char *buffer, apr_size_t bmax, const char *tag, const char *sep, apr_bucket_brigade *bb)
h2_fifo_op_t h2_fifo_peek_fn(void *head, void *ctx)
Definition: h2_util.h:233
HTTP protocol handling.
A bucket referring to an HTTP header set.
Definition: http_protocol.h:1270
A bucket referring to a HTTP response.
Definition: http_protocol.h:1199
Definition: apr_buckets.h:263
Definition: apr_buckets.h:229
Definition: apr_arch_file_io.h:107
Definition: h2_headers.h:27
Definition: h2_util.h:76
int nelts
Definition: h2_util.h:79
int head
Definition: h2_util.h:78
apr_pool_t * pool
Definition: h2_util.h:81
int * elts
Definition: h2_util.h:77
int nalloc
Definition: h2_util.h:80
Definition: h2_util.h:379
nghttp2_nv * nv
Definition: h2_util.h:380
apr_size_t nvlen
Definition: h2_util.h:381
Definition: h2.h:169
apr_pool_t * p