unfold url tests
[m6w6/ext-http] / php_http_header.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 STATUS php_http_header_parse(const char *header, size_t length, HashTable *headers, php_http_info_callback_t callback_func, void **callback_data TSRMLS_DC)
16 {
17 php_http_header_parser_t ctx;
18 php_http_buffer_t buf;
19 php_http_header_parser_state_t rs;
20
21 if (!php_http_buffer_from_string_ex(&buf, header, length)) {
22 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not allocate buffer");
23 return FAILURE;
24 }
25
26 if (!php_http_header_parser_init(&ctx TSRMLS_CC)) {
27 php_http_buffer_dtor(&buf);
28 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize header parser");
29 return FAILURE;
30 }
31
32 rs = php_http_header_parser_parse(&ctx, &buf, PHP_HTTP_HEADER_PARSER_CLEANUP, headers, callback_func, callback_data);
33 php_http_header_parser_dtor(&ctx);
34 php_http_buffer_dtor(&buf);
35
36 if (rs == PHP_HTTP_HEADER_PARSER_STATE_FAILURE) {
37 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse headers");
38 return FAILURE;
39 }
40
41 return SUCCESS;
42 }
43
44 void php_http_header_to_callback(HashTable *headers, zend_bool crlf, php_http_pass_format_callback_t cb, void *cb_arg TSRMLS_DC)
45 {
46 HashPosition pos1, pos2;
47 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
48 zval **header, **single_header;
49
50 FOREACH_HASH_KEYVAL(pos1, headers, key, header) {
51 if (key.type == HASH_KEY_IS_STRING) {
52 if (key.len == sizeof("Set-Cookie") && !strcasecmp(key.str, "Set-Cookie") && Z_TYPE_PP(header) == IS_ARRAY) {
53 FOREACH_VAL(pos2, *header, single_header) {
54 if (Z_TYPE_PP(single_header) == IS_ARRAY) {
55 php_http_cookie_list_t *cookie = php_http_cookie_list_from_struct(NULL, *single_header TSRMLS_CC);
56
57 if (cookie) {
58 char *buf;
59 size_t len;
60
61 php_http_cookie_list_to_string(cookie, &buf, &len);
62 cb(cb_arg, crlf ? "Set-Cookie: %s" PHP_HTTP_CRLF : "Set-Cookie: %s", buf);
63 php_http_cookie_list_free(&cookie);
64 efree(buf);
65 }
66 } else {
67 zval *strval = php_http_header_value_to_string(*single_header TSRMLS_CC);
68
69 cb(cb_arg, crlf ? "Set-Cookie: %s" PHP_HTTP_CRLF : "Set-Cookie: %s", Z_STRVAL_P(strval));
70 zval_ptr_dtor(&strval);
71 }
72 }
73 } else {
74 zval *strval = php_http_header_value_to_string(*header TSRMLS_CC);
75
76 cb(cb_arg, crlf ? "%s: %s" PHP_HTTP_CRLF : "%s: %s", key.str, Z_STRVAL_P(strval));
77 zval_ptr_dtor(&strval);
78 }
79 }
80 }
81 }
82
83 void php_http_header_to_string(php_http_buffer_t *str, HashTable *headers TSRMLS_DC)
84 {
85 php_http_header_to_callback(headers, 1, (php_http_pass_format_callback_t) php_http_buffer_appendf, str TSRMLS_CC);
86 }
87
88 zval *php_http_header_value_to_string(zval *header TSRMLS_DC)
89 {
90 zval *ret;
91
92 if (Z_TYPE_P(header) == IS_BOOL) {
93 MAKE_STD_ZVAL(ret);
94 ZVAL_STRING(ret, Z_BVAL_P(header) ? "true" : "false", 1);
95 } else if (Z_TYPE_P(header) == IS_ARRAY) {
96 zval **val;
97 HashPosition pos;
98 php_http_buffer_t str;
99
100 php_http_buffer_init(&str);
101 MAKE_STD_ZVAL(ret);
102 FOREACH_VAL(pos,header, val) {
103 zval *strval = php_http_header_value_to_string(*val TSRMLS_CC);
104
105 php_http_buffer_appendf(&str, str.used ? ", %s":"%s", Z_STRVAL_P(strval));
106 zval_ptr_dtor(&strval);
107 }
108 php_http_buffer_fix(&str);
109 ZVAL_STRINGL(ret, str.data, str.used, 0);
110 } else {
111 ret = php_http_zsep(1, IS_STRING, header);
112 }
113
114 return ret;
115 }
116
117 ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader___construct, 0, 0, 0)
118 ZEND_ARG_INFO(0, name)
119 ZEND_ARG_INFO(0, value)
120 ZEND_END_ARG_INFO();
121 PHP_METHOD(HttpHeader, __construct)
122 {
123 char *name_str = NULL, *value_str = NULL;
124 int name_len = 0, value_len = 0;
125
126 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!", &name_str, &name_len, &value_str, &value_len), invalid_arg, return);
127
128 if (name_str && name_len) {
129 char *pretty_str = estrndup(name_str, name_len);
130 zend_update_property_stringl(php_http_header_class_entry, getThis(), ZEND_STRL("name"), php_http_pretty_key(pretty_str, name_len, 1, 1), name_len TSRMLS_CC);
131 efree(pretty_str);
132 }
133 if (value_str && value_len) {
134 zend_update_property_stringl(php_http_header_class_entry, getThis(), ZEND_STRL("value"), value_str, value_len TSRMLS_CC);
135 }
136 }
137
138 ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_serialize, 0, 0, 0)
139 ZEND_END_ARG_INFO();
140 PHP_METHOD(HttpHeader, serialize)
141 {
142 if (SUCCESS == zend_parse_parameters_none()) {
143 php_http_buffer_t buf;
144 zval *zname, *zvalue;
145
146 php_http_buffer_init(&buf);
147 zname = php_http_ztyp(IS_STRING, zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("name"), 0 TSRMLS_CC));
148 php_http_buffer_append(&buf, Z_STRVAL_P(zname), Z_STRLEN_P(zname));
149 zval_ptr_dtor(&zname);
150 zvalue = php_http_ztyp(IS_STRING, zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0 TSRMLS_CC));
151 if (Z_STRLEN_P(zvalue)) {
152 php_http_buffer_appends(&buf, ": ");
153 php_http_buffer_append(&buf, Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue));
154 } else {
155 php_http_buffer_appends(&buf, ":");
156 }
157 zval_ptr_dtor(&zvalue);
158
159 RETURN_PHP_HTTP_BUFFER_VAL(&buf);
160 }
161 RETURN_EMPTY_STRING();
162 }
163
164 ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_unserialize, 0, 0, 1)
165 ZEND_ARG_INFO(0, serialized)
166 ZEND_END_ARG_INFO();
167 PHP_METHOD(HttpHeader, unserialize)
168 {
169 char *serialized_str;
170 int serialized_len;
171
172 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized_str, &serialized_len)) {
173 HashTable ht;
174
175 zend_hash_init(&ht, 1, NULL, ZVAL_PTR_DTOR, 0);
176 if (SUCCESS == php_http_header_parse(serialized_str, serialized_len, &ht, NULL, NULL TSRMLS_CC)) {
177 if (zend_hash_num_elements(&ht)) {
178 zval **val, *cpy;
179 char *str;
180 uint len;
181 ulong idx;
182
183 zend_hash_internal_pointer_reset(&ht);
184 switch (zend_hash_get_current_key_ex(&ht, &str, &len, &idx, 0, NULL)) {
185 case HASH_KEY_IS_STRING:
186 zend_update_property_stringl(php_http_header_class_entry, getThis(), ZEND_STRL("name"), str, len - 1 TSRMLS_CC);
187 break;
188 case HASH_KEY_IS_LONG:
189 zend_update_property_long(php_http_header_class_entry, getThis(), ZEND_STRL("name"), idx TSRMLS_CC);
190 break;
191 default:
192 break;
193 }
194 zend_hash_get_current_data(&ht, (void *) &val);
195 cpy = php_http_zsep(1, IS_STRING, *val);
196 zend_update_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), cpy TSRMLS_CC);
197 zval_ptr_dtor(&cpy);
198 }
199 }
200 zend_hash_destroy(&ht);
201 }
202
203 }
204
205 ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_match, 0, 0, 1)
206 ZEND_ARG_INFO(0, value)
207 ZEND_ARG_INFO(0, flags)
208 ZEND_END_ARG_INFO();
209 PHP_METHOD(HttpHeader, match)
210 {
211 char *val_str;
212 int val_len;
213 long flags = PHP_HTTP_MATCH_LOOSE;
214 zval *zvalue;
215
216 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &val_str, &val_len, &flags)) {
217 return;
218 }
219
220 zvalue = php_http_ztyp(IS_STRING, zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0 TSRMLS_CC));
221 RETVAL_BOOL(php_http_match(Z_STRVAL_P(zvalue), val_str, flags));
222 zval_ptr_dtor(&zvalue);
223 }
224
225 ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_negotiate, 0, 0, 1)
226 ZEND_ARG_INFO(0, supported)
227 ZEND_ARG_INFO(1, result)
228 ZEND_END_ARG_INFO();
229 PHP_METHOD(HttpHeader, negotiate)
230 {
231 HashTable *supported, *rs;
232 zval *zname, *zvalue, *rs_array = NULL;
233 char *sep_str = NULL;
234 size_t sep_len = 0;
235
236 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
237 return;
238 }
239 if (rs_array) {
240 zval_dtor(rs_array);
241 array_init(rs_array);
242 }
243
244 zname = php_http_ztyp(IS_STRING, zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("name"), 0 TSRMLS_CC));
245 if (!strcasecmp(Z_STRVAL_P(zname), "Accept")) {
246 sep_str = "/";
247 sep_len = 1;
248 } else if (!strcasecmp(Z_STRVAL_P(zname), "Accept-Language")) {
249 sep_str = "-";
250 sep_len = 1;
251 }
252 zval_ptr_dtor(&zname);
253
254 zvalue = php_http_ztyp(IS_STRING, zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0 TSRMLS_CC));
255 if ((rs = php_http_negotiate(Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue), supported, sep_str, sep_len TSRMLS_CC))) {
256 PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(rs, supported, rs_array);
257 } else {
258 PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array);
259 }
260 zval_ptr_dtor(&zvalue);
261 }
262
263 ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_getParams, 0, 0, 0)
264 ZEND_ARG_INFO(0, param_sep)
265 ZEND_ARG_INFO(0, arg_sep)
266 ZEND_ARG_INFO(0, val_sep)
267 ZEND_ARG_INFO(0, flags)
268 ZEND_END_ARG_INFO();
269 PHP_METHOD(HttpHeader, getParams)
270 {
271 zval zctor, *zparams_obj, **zargs = NULL;
272
273 INIT_PZVAL(&zctor);
274 ZVAL_STRINGL(&zctor, "__construct", lenof("__construct"), 0);
275
276 MAKE_STD_ZVAL(zparams_obj);
277 object_init_ex(zparams_obj, php_http_params_class_entry);
278
279 zargs = (zval **) ecalloc(ZEND_NUM_ARGS()+1, sizeof(zval *));
280 zargs[0] = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("value"), 0 TSRMLS_CC);
281 if (ZEND_NUM_ARGS()) {
282 zend_get_parameters_array(ZEND_NUM_ARGS(), ZEND_NUM_ARGS(), &zargs[1]);
283 }
284
285 if (SUCCESS == call_user_function(NULL, &zparams_obj, &zctor, return_value, ZEND_NUM_ARGS()+1, zargs TSRMLS_CC)) {
286 RETVAL_ZVAL(zparams_obj, 0, 1);
287 }
288
289 if (zargs) {
290 efree(zargs);
291 }
292 }
293
294 ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_parse, 0, 0, 1)
295 ZEND_ARG_INFO(0, string)
296 ZEND_ARG_INFO(0, header_class)
297 ZEND_END_ARG_INFO();
298 PHP_METHOD(HttpHeader, parse)
299 {
300 char *header_str;
301 int header_len;
302 zend_class_entry *ce = NULL;
303
304 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|C", &header_str, &header_len, &ce)) {
305 array_init(return_value);
306
307 if (SUCCESS != php_http_header_parse(header_str, header_len, Z_ARRVAL_P(return_value), NULL, NULL TSRMLS_CC)) {
308 zval_dtor(return_value);
309 RETURN_FALSE;
310 } else {
311 if (ce && instanceof_function(ce, php_http_header_class_entry TSRMLS_CC)) {
312 HashPosition pos;
313 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
314 zval **val;
315
316 FOREACH_KEYVAL(pos, return_value, key, val) {
317 zval *zho, *zkey, *zvalue;
318
319 Z_ADDREF_PP(val);
320 zvalue = *val;
321
322 MAKE_STD_ZVAL(zkey);
323 if (key.type == HASH_KEY_IS_LONG) {
324 ZVAL_LONG(zkey, key.num);
325 } else {
326 ZVAL_STRINGL(zkey, key.str, key.len - 1, 1);
327 }
328
329 MAKE_STD_ZVAL(zho);
330 object_init_ex(zho, ce);
331 zend_call_method_with_2_params(&zho, ce, NULL, "__construct", NULL, zkey, zvalue);
332
333 if (key.type == HASH_KEY_IS_LONG) {
334 zend_hash_index_update(Z_ARRVAL_P(return_value), key.num, (void *) &zho, sizeof(zval *), NULL);
335 } else {
336 zend_hash_update(Z_ARRVAL_P(return_value), key.str, key.len, (void *) &zho, sizeof(zval *), NULL);
337 }
338
339 zval_ptr_dtor(&zvalue);
340 zval_ptr_dtor(&zkey);
341 }
342 }
343 }
344 }
345 }
346
347 static zend_function_entry php_http_header_methods[] = {
348 PHP_ME(HttpHeader, __construct, ai_HttpHeader___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
349 PHP_ME(HttpHeader, serialize, ai_HttpHeader_serialize, ZEND_ACC_PUBLIC)
350 ZEND_MALIAS(HttpHeader, __toString, serialize, ai_HttpHeader_serialize, ZEND_ACC_PUBLIC)
351 ZEND_MALIAS(HttpHeader, toString, serialize, ai_HttpHeader_serialize, ZEND_ACC_PUBLIC)
352 PHP_ME(HttpHeader, unserialize, ai_HttpHeader_unserialize, ZEND_ACC_PUBLIC)
353 PHP_ME(HttpHeader, match, ai_HttpHeader_match, ZEND_ACC_PUBLIC)
354 PHP_ME(HttpHeader, negotiate, ai_HttpHeader_negotiate, ZEND_ACC_PUBLIC)
355 PHP_ME(HttpHeader, getParams, ai_HttpHeader_getParams, ZEND_ACC_PUBLIC)
356 PHP_ME(HttpHeader, parse, ai_HttpHeader_parse, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
357 EMPTY_FUNCTION_ENTRY
358 };
359
360 zend_class_entry *php_http_header_class_entry;
361
362 PHP_MINIT_FUNCTION(http_header)
363 {
364 zend_class_entry ce = {0};
365
366 INIT_NS_CLASS_ENTRY(ce, "http", "Header", php_http_header_methods);
367 php_http_header_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
368 zend_class_implements(php_http_header_class_entry TSRMLS_CC, 1, zend_ce_serializable);
369 zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_LOOSE"), PHP_HTTP_MATCH_LOOSE TSRMLS_CC);
370 zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_CASE"), PHP_HTTP_MATCH_CASE TSRMLS_CC);
371 zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_WORD"), PHP_HTTP_MATCH_WORD TSRMLS_CC);
372 zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_FULL"), PHP_HTTP_MATCH_FULL TSRMLS_CC);
373 zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_STRICT"), PHP_HTTP_MATCH_STRICT TSRMLS_CC);
374 zend_declare_property_null(php_http_header_class_entry, ZEND_STRL("name"), ZEND_ACC_PUBLIC TSRMLS_CC);
375 zend_declare_property_null(php_http_header_class_entry, ZEND_STRL("value"), ZEND_ACC_PUBLIC TSRMLS_CC);
376
377 return SUCCESS;
378 }
379
380 /*
381 * Local variables:
382 * tab-width: 4
383 * c-basic-offset: 4
384 * End:
385 * vim600: noet sw=4 ts=4 fdm=marker
386 * vim<600: noet sw=4 ts=4
387 */
388