-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhpack.c
172 lines (140 loc) · 4.11 KB
/
hpack.c
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
/* hpack extension for PHP */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_hpack.h"
#include "hpack_arginfo.h"
#include <nghttp2/nghttp2.h>
/* For compatibility with older PHP versions */
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
ZEND_PARSE_PARAMETERS_START(0, 0) \
ZEND_PARSE_PARAMETERS_END()
#endif
#define MAKE_NV(K, V) \
{ \
(uint8_t *) Z_STRVAL_P(K), (uint8_t *) Z_STRVAL_P(V), Z_STRLEN_P(K), Z_STRLEN_P(V), \
NGHTTP2_NV_FLAG_NONE \
}
/* {{{ string hpack_encode( array $input ) */
PHP_FUNCTION(hpack_encode)
{
zval *input;
size_t i;
size_t sum;
size_t nvlen;
uint8_t *buf;
size_t buflen;
size_t outlen;
int rv;
zval *entry;
HashTable *myht;
nghttp2_hd_deflater *deflater;
zend_string *retval;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(input)
ZEND_PARSE_PARAMETERS_END();
myht = Z_ARRVAL_P(input);
uint32_t num = zend_hash_num_elements(myht);
nghttp2_nv nva[num];
i = 0;
ZEND_HASH_FOREACH_VAL(myht, entry) {
ZVAL_DEREF(entry);
zval *value = zend_hash_index_find(Z_ARRVAL_P(entry), 0);
zval *value2 = zend_hash_index_find(Z_ARRVAL_P(entry), 1);
nva[i] = (nghttp2_nv) MAKE_NV(value, value2);
++i;
} ZEND_HASH_FOREACH_END();
nvlen = sizeof(nva) / sizeof(nva[0]);
rv = nghttp2_hd_deflate_new(&deflater, 4096);
buflen = nghttp2_hd_deflate_bound(deflater, nva, nvlen);
buf = malloc(buflen);
rv = nghttp2_hd_deflate_hd(deflater, buf, buflen, nva, nvlen);
outlen = (size_t)rv;
retval = strpprintf(outlen, "%s", buf);
RETURN_STR(retval);
free(buf);
nghttp2_hd_deflate_del(deflater);
}
/* }}} */
/* {{{ array hpack_decode( string $input ) */
PHP_FUNCTION(hpack_decode)
{
char *var;
size_t var_len;
zend_string *retval;
ssize_t rv;
nghttp2_hd_inflater *inflater;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(var, var_len)
ZEND_PARSE_PARAMETERS_END();
rv = nghttp2_hd_inflate_new(&inflater);
size_t inlen = var_len;
zval ret;
zval arr;
array_init(&ret);
for (;;) {
nghttp2_nv nv;
int inflate_flags = 0;
size_t proclen;
rv = nghttp2_hd_inflate_hd(inflater, &nv, &inflate_flags, var, inlen, 1);
proclen = (size_t) rv;
var += proclen;
inlen -= proclen;
if (inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
array_init(&arr);
add_index_string(&arr, 0, nv.name);
add_index_string(&arr, 1, nv.value);
add_next_index_zval(&ret, &arr);
}
if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
nghttp2_hd_inflate_end_headers(inflater);
break;
}
if ((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 && inlen == 0) {
break;
}
}
nghttp2_hd_inflate_del(inflater);
RETURN_ZVAL(&ret, 0, 0);
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(hpack)
{
#if defined(ZTS) && defined(COMPILE_DL_HPACK)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(hpack)
{
php_info_print_table_start();
php_info_print_table_row(2, "hpack support", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ hpack_module_entry */
zend_module_entry hpack_module_entry = {
STANDARD_MODULE_HEADER,
"hpack", /* Extension name */
ext_functions, /* zend_function_entry */
NULL, /* PHP_MINIT - Module initialization */
NULL, /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(hpack), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(hpack), /* PHP_MINFO - Module info */
PHP_HPACK_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_HPACK
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(hpack)
#endif