rename info tests
[m6w6/ext-http] / php_http_message.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 static void message_headers(php_http_message_t *msg, php_http_buffer_t *str);
16
17 zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info TSRMLS_DC)
18 {
19 php_http_message_t *old = *message;
20
21 /* advance message */
22 if (!old || old->type || zend_hash_num_elements(&old->hdrs)) {
23 (*message) = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
24 (*message)->parent = old;
25 if (headers) {
26 (*headers) = &((*message)->hdrs);
27 }
28 }
29
30 if (info) {
31 php_http_message_set_info(*message, info);
32 }
33
34 return old != *message;
35 }
36
37 php_http_message_t *php_http_message_init(php_http_message_t *message, php_http_message_type_t type, php_http_message_body_t *body TSRMLS_DC)
38 {
39 if (!message) {
40 message = emalloc(sizeof(*message));
41 }
42 memset(message, 0, sizeof(*message));
43 TSRMLS_SET_CTX(message->ts);
44
45 php_http_message_set_type(message, type);
46 message->http.version.major = 1;
47 message->http.version.minor = 1;
48 zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0);
49 message->body = body ? body : php_http_message_body_init(NULL, NULL TSRMLS_CC);
50
51 return message;
52 }
53
54 php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_http_message_type_t type TSRMLS_DC)
55 {
56 int free_msg = !message;
57 zval *sval, tval;
58 php_http_message_body_t *mbody;
59
60 switch (type) {
61 case PHP_HTTP_REQUEST:
62 mbody = php_http_env_get_request_body(TSRMLS_C);
63 php_http_message_body_addref(mbody);
64 message = php_http_message_init(message, type, mbody TSRMLS_CC);
65 if ((sval = php_http_env_get_server_var(ZEND_STRL("SERVER_PROTOCOL"), 1 TSRMLS_CC)) && !strncmp(Z_STRVAL_P(sval), "HTTP/", lenof("HTTP/"))) {
66 php_http_version_parse(&message->http.version, Z_STRVAL_P(sval) TSRMLS_CC);
67 }
68 if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1 TSRMLS_CC))) {
69 message->http.info.request.method = estrdup(Z_STRVAL_P(sval));
70 }
71 if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1 TSRMLS_CC))) {
72 message->http.info.request.url = php_http_url_parse(Z_STRVAL_P(sval), Z_STRLEN_P(sval), ~0 TSRMLS_CC);
73 }
74
75 php_http_env_get_request_headers(&message->hdrs TSRMLS_CC);
76 break;
77
78 case PHP_HTTP_RESPONSE:
79 message = php_http_message_init(NULL, type, NULL TSRMLS_CC);
80 if (!SG(sapi_headers).http_status_line || !php_http_info_parse((php_http_info_t *) &message->http, SG(sapi_headers).http_status_line TSRMLS_CC)) {
81 if (!(message->http.info.response.code = SG(sapi_headers).http_response_code)) {
82 message->http.info.response.code = 200;
83 }
84 message->http.info.response.status = estrdup(php_http_env_get_response_status_for_code(message->http.info.response.code));
85 }
86
87 php_http_env_get_response_headers(&message->hdrs TSRMLS_CC);
88 #if PHP_VERSION_ID >= 50400
89 if (php_output_get_level(TSRMLS_C)) {
90 if (php_output_get_status(TSRMLS_C) & PHP_OUTPUT_SENT) {
91 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(TSRMLS_C), php_output_get_start_lineno(TSRMLS_C));
92 goto error;
93 } else if (SUCCESS != php_output_get_contents(&tval TSRMLS_CC)) {
94 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch response body");
95 goto error;
96 } else {
97 php_http_message_body_append(message->body, Z_STRVAL(tval), Z_STRLEN(tval));
98 zval_dtor(&tval);
99 }
100 }
101 #else
102 if (OG(ob_nesting_level)) {
103 if (php_get_output_start_filename(TSRMLS_C)) {
104 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch response body, output has already been sent at %s:%d", php_get_output_start_filename(TSRMLS_C), php_get_output_start_lineno(TSRMLS_C));
105 goto error;
106 } else if (SUCCESS != php_ob_get_buffer(&tval TSRMLS_CC)) {
107 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch response body");
108 goto error;
109 } else {
110 php_http_message_body_append(message->body, Z_STRVAL(tval), Z_STRLEN(tval));
111 zval_dtor(&tval);
112 }
113 }
114 #endif
115 break;
116
117 default:
118 error:
119 if (free_msg) {
120 if (message) {
121 php_http_message_free(&message);
122 }
123 } else {
124 message = NULL;
125 }
126 break;
127 }
128
129 return message;
130 }
131
132 php_http_message_t *php_http_message_parse(php_http_message_t *msg, const char *str, size_t len, zend_bool greedy TSRMLS_DC)
133 {
134 php_http_message_parser_t p;
135 php_http_buffer_t buf;
136 unsigned flags = PHP_HTTP_MESSAGE_PARSER_CLEANUP;
137 int free_msg;
138
139 php_http_buffer_from_string_ex(&buf, str, len);
140 php_http_message_parser_init(&p TSRMLS_CC);
141
142 if ((free_msg = !msg)) {
143 msg = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
144 }
145
146 if (greedy) {
147 flags |= PHP_HTTP_MESSAGE_PARSER_GREEDY;
148 }
149 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse(&p, &buf, flags, &msg)) {
150 if (free_msg) {
151 php_http_message_free(&msg);
152 }
153 msg = NULL;
154 }
155
156 php_http_message_parser_dtor(&p);
157 php_http_buffer_dtor(&buf);
158
159 return msg;
160 }
161
162 zval *php_http_message_header(php_http_message_t *msg, const char *key_str, size_t key_len, int join)
163 {
164 zval *ret = NULL, **header;
165 char *key;
166 ALLOCA_FLAG(free_key);
167
168 key = do_alloca(key_len + 1, free_key);
169 memcpy(key, key_str, key_len);
170 key[key_len] = '\0';
171 php_http_pretty_key(key, key_len, 1, 1);
172
173 if (SUCCESS == zend_symtable_find(&msg->hdrs, key, key_len + 1, (void *) &header)) {
174 if (join && Z_TYPE_PP(header) == IS_ARRAY) {
175 TSRMLS_FETCH_FROM_CTX(msg->ts);
176
177 ret = php_http_header_value_to_string(*header TSRMLS_CC);
178 } else {
179 Z_ADDREF_PP(header);
180 ret = *header;
181 }
182 }
183
184 free_alloca(key, free_key);
185
186 return ret;
187 }
188
189 zend_bool php_http_message_is_multipart(php_http_message_t *msg, char **boundary)
190 {
191 zval *ct = php_http_message_header(msg, ZEND_STRL("Content-Type"), 1);
192 zend_bool is_multipart = 0;
193 TSRMLS_FETCH_FROM_CTX(msg->ts);
194
195 if (ct) {
196 php_http_params_opts_t popts;
197 HashTable params;
198
199 ZEND_INIT_SYMTABLE(&params);
200 php_http_params_opts_default_get(&popts);
201 popts.input.str = Z_STRVAL_P(ct);
202 popts.input.len = Z_STRLEN_P(ct);
203
204 if (php_http_params_parse(&params, &popts TSRMLS_CC)) {
205 zval **cur, **arg;
206 char *ct_str;
207
208 zend_hash_internal_pointer_reset(&params);
209
210 if (SUCCESS == zend_hash_get_current_data(&params, (void *) &cur)
211 && Z_TYPE_PP(cur) == IS_ARRAY
212 && HASH_KEY_IS_STRING == zend_hash_get_current_key(&params, &ct_str, NULL, 0)
213 ) {
214 if (php_http_match(ct_str, "multipart", PHP_HTTP_MATCH_WORD)) {
215 is_multipart = 1;
216
217 /* get boundary */
218 if (boundary
219 && SUCCESS == zend_hash_find(Z_ARRVAL_PP(cur), ZEND_STRS("arguments"), (void *) &arg)
220 && Z_TYPE_PP(arg) == IS_ARRAY
221 ) {
222 zval **val;
223 HashPosition pos;
224 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
225
226 FOREACH_KEYVAL(pos, *arg, key, val) {
227 if (key.type == HASH_KEY_IS_STRING && !strcasecmp(key.str, "boundary")) {
228 zval *bnd = php_http_ztyp(IS_STRING, *val);
229
230 if (Z_STRLEN_P(bnd)) {
231 *boundary = estrndup(Z_STRVAL_P(bnd), Z_STRLEN_P(bnd));
232 }
233 zval_ptr_dtor(&bnd);
234 }
235 }
236 }
237 }
238 }
239 }
240 zend_hash_destroy(&params);
241 zval_ptr_dtor(&ct);
242 }
243
244 return is_multipart;
245 }
246
247 /* */
248 void php_http_message_set_type(php_http_message_t *message, php_http_message_type_t type)
249 {
250 /* just act if different */
251 if (type != message->type) {
252
253 /* free request info */
254 switch (message->type) {
255 case PHP_HTTP_REQUEST:
256 PTR_FREE(message->http.info.request.method);
257 PTR_FREE(message->http.info.request.url);
258 break;
259
260 case PHP_HTTP_RESPONSE:
261 PTR_FREE(message->http.info.response.status);
262 break;
263
264 default:
265 break;
266 }
267
268 message->type = type;
269 memset(&message->http, 0, sizeof(message->http));
270 }
271 }
272
273 void php_http_message_set_info(php_http_message_t *message, php_http_info_t *info)
274 {
275 php_http_message_set_type(message, info->type);
276 message->http.version = info->http.version;
277 switch (message->type) {
278 case PHP_HTTP_REQUEST:
279 PTR_SET(PHP_HTTP_INFO(message).request.url, PHP_HTTP_INFO(info).request.url ? php_http_url_copy(PHP_HTTP_INFO(info).request.url, 0) : NULL);
280 PTR_SET(PHP_HTTP_INFO(message).request.method, PHP_HTTP_INFO(info).request.method ? estrdup(PHP_HTTP_INFO(info).request.method) : NULL);
281 break;
282
283 case PHP_HTTP_RESPONSE:
284 PHP_HTTP_INFO(message).response.code = PHP_HTTP_INFO(info).response.code;
285 PTR_SET(PHP_HTTP_INFO(message).response.status, PHP_HTTP_INFO(info).response.status ? estrdup(PHP_HTTP_INFO(info).response.status) : NULL);
286 break;
287
288 default:
289 break;
290 }
291 }
292
293 void php_http_message_update_headers(php_http_message_t *msg)
294 {
295 zval *h;
296 size_t size;
297
298 if (php_http_message_body_stream(msg->body)->readfilters.head) {
299 /* if a read stream filter is attached to the body the caller must also care for the headers */
300 } else if ((size = php_http_message_body_size(msg->body))) {
301 MAKE_STD_ZVAL(h);
302 ZVAL_LONG(h, size);
303 zend_hash_update(&msg->hdrs, "Content-Length", sizeof("Content-Length"), &h, sizeof(zval *), NULL);
304
305 if (msg->body->boundary) {
306 char *str;
307 size_t len;
308
309 if (!(h = php_http_message_header(msg, ZEND_STRL("Content-Type"), 1))) {
310 len = spprintf(&str, 0, "multipart/form-data; boundary=\"%s\"", msg->body->boundary);
311 MAKE_STD_ZVAL(h);
312 ZVAL_STRINGL(h, str, len, 0);
313 zend_hash_update(&msg->hdrs, "Content-Type", sizeof("Content-Type"), &h, sizeof(zval *), NULL);
314 } else if (!php_http_match(Z_STRVAL_P(h), "boundary=", PHP_HTTP_MATCH_WORD)) {
315 zval_dtor(h);
316 Z_STRLEN_P(h) = spprintf(&Z_STRVAL_P(h), 0, "%s; boundary=\"%s\"", Z_STRVAL_P(h), msg->body->boundary);
317 zend_hash_update(&msg->hdrs, "Content-Type", sizeof("Content-Type"), &h, sizeof(zval *), NULL);
318 } else {
319 zval_ptr_dtor(&h);
320 }
321 }
322 } else if ((h = php_http_message_header(msg, ZEND_STRL("Content-Length"), 1))) {
323 zval *h_cpy = php_http_ztyp(IS_LONG, h);
324
325 zval_ptr_dtor(&h);
326 if (Z_LVAL_P(h_cpy)) {
327 zend_hash_del(&msg->hdrs, "Content-Length", sizeof("Content-Length"));
328 }
329 zval_ptr_dtor(&h_cpy);
330 }
331 }
332
333 static void message_headers(php_http_message_t *msg, php_http_buffer_t *str)
334 {
335 char *tmp = NULL;
336 TSRMLS_FETCH_FROM_CTX(msg->ts);
337
338 switch (msg->type) {
339 case PHP_HTTP_REQUEST:
340 php_http_buffer_appendf(str, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg->http, tmp, PHP_HTTP_CRLF));
341 PTR_FREE(tmp);
342 break;
343
344 case PHP_HTTP_RESPONSE:
345 php_http_buffer_appendf(str, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg->http, tmp, PHP_HTTP_CRLF));
346 PTR_FREE(tmp);
347 break;
348
349 default:
350 break;
351 }
352
353 php_http_message_update_headers(msg);
354 php_http_header_to_string(str, &msg->hdrs TSRMLS_CC);
355 }
356
357 void php_http_message_to_callback(php_http_message_t *msg, php_http_pass_callback_t cb, void *cb_arg)
358 {
359 php_http_buffer_t str;
360
361 php_http_buffer_init_ex(&str, 0x1000, 0);
362 message_headers(msg, &str);
363 cb(cb_arg, str.data, str.used);
364 php_http_buffer_dtor(&str);
365
366 if (php_http_message_body_size(msg->body)) {
367 cb(cb_arg, ZEND_STRL(PHP_HTTP_CRLF));
368 php_http_message_body_to_callback(msg->body, cb, cb_arg, 0, 0);
369 }
370 }
371
372 void php_http_message_to_string(php_http_message_t *msg, char **string, size_t *length)
373 {
374 php_http_buffer_t str;
375 char *data;
376
377 php_http_buffer_init_ex(&str, 0x1000, 0);
378 message_headers(msg, &str);
379 if (php_http_message_body_size(msg->body)) {
380 php_http_buffer_appends(&str, PHP_HTTP_CRLF);
381 php_http_message_body_to_callback(msg->body, (php_http_pass_callback_t) php_http_buffer_append, &str, 0, 0);
382 }
383
384 data = php_http_buffer_data(&str, string, length);
385 if (!string) {
386 efree(data);
387 }
388
389 php_http_buffer_dtor(&str);
390 }
391
392 void php_http_message_serialize(php_http_message_t *message, char **string, size_t *length)
393 {
394 char *buf;
395 php_http_buffer_t str;
396 php_http_message_t *msg;
397
398 php_http_buffer_init(&str);
399
400 msg = message = php_http_message_reverse(message);
401 do {
402 php_http_message_to_callback(message, (php_http_pass_callback_t) php_http_buffer_append, &str);
403 php_http_buffer_appends(&str, PHP_HTTP_CRLF);
404 } while ((message = message->parent));
405 php_http_message_reverse(msg);
406
407 buf = php_http_buffer_data(&str, string, length);
408 if (!string) {
409 efree(buf);
410 }
411
412 php_http_buffer_dtor(&str);
413 }
414
415 php_http_message_t *php_http_message_reverse(php_http_message_t *msg)
416 {
417 int i, c = 0;
418
419 php_http_message_count(c, msg);
420
421 if (c > 1) {
422 php_http_message_t *tmp = msg, **arr;
423
424 arr = ecalloc(c, sizeof(**arr));
425 for (i = 0; i < c; ++i) {
426 arr[i] = tmp;
427 tmp = tmp->parent;
428 }
429 arr[0]->parent = NULL;
430 for (i = 0; i < c-1; ++i) {
431 arr[i+1]->parent = arr[i];
432 }
433
434 msg = arr[c-1];
435 efree(arr);
436 }
437
438 return msg;
439 }
440
441 php_http_message_t *php_http_message_zip(php_http_message_t *one, php_http_message_t *two)
442 {
443 php_http_message_t *dst = php_http_message_copy(one, NULL), *src = php_http_message_copy(two, NULL), *tmp_dst, *tmp_src, *ret = dst;
444
445 while(dst && src) {
446 tmp_dst = dst->parent;
447 tmp_src = src->parent;
448 dst->parent = src;
449 if (tmp_dst) {
450 src->parent = tmp_dst;
451 }
452 src = tmp_src;
453 dst = tmp_dst;
454 }
455
456 return ret;
457 }
458
459 php_http_message_t *php_http_message_copy_ex(php_http_message_t *from, php_http_message_t *to, zend_bool parents)
460 {
461 php_http_message_t *temp, *copy = NULL;
462 php_http_info_t info;
463 TSRMLS_FETCH_FROM_CTX(from->ts);
464
465 if (from) {
466 info.type = from->type;
467 info.http = from->http;
468
469 copy = temp = php_http_message_init(to, 0, php_http_message_body_copy(from->body, NULL) TSRMLS_CC);
470 php_http_message_set_info(temp, &info);
471 zend_hash_copy(&temp->hdrs, &from->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
472
473 if (parents) while (from->parent) {
474 info.type = from->parent->type;
475 info.http = from->parent->http;
476
477 temp->parent = php_http_message_init(NULL, 0, php_http_message_body_copy(from->parent->body, NULL) TSRMLS_CC);
478 php_http_message_set_info(temp->parent, &info);
479 zend_hash_copy(&temp->parent->hdrs, &from->parent->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
480
481 temp = temp->parent;
482 from = from->parent;
483 }
484 }
485
486 return copy;
487 }
488
489 php_http_message_t *php_http_message_copy(php_http_message_t *from, php_http_message_t *to)
490 {
491 return php_http_message_copy_ex(from, to, 1);
492 }
493
494 void php_http_message_dtor(php_http_message_t *message)
495 {
496 if (message) {
497 zend_hash_destroy(&message->hdrs);
498 php_http_message_body_free(&message->body);
499
500 switch (message->type) {
501 case PHP_HTTP_REQUEST:
502 PTR_SET(message->http.info.request.method, NULL);
503 PTR_SET(message->http.info.request.url, NULL);
504 break;
505
506 case PHP_HTTP_RESPONSE:
507 PTR_SET(message->http.info.response.status, NULL);
508 break;
509
510 default:
511 break;
512 }
513 }
514 }
515
516 void php_http_message_free(php_http_message_t **message)
517 {
518 if (*message) {
519 if ((*message)->parent) {
520 php_http_message_free(&(*message)->parent);
521 }
522 php_http_message_dtor(*message);
523 efree(*message);
524 *message = NULL;
525 }
526 }
527
528 static zval *php_http_message_object_read_prop(zval *object, zval *member, int type PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC);
529 static void php_http_message_object_write_prop(zval *object, zval *member, zval *value PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC);
530 static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC);
531
532 static zend_object_handlers php_http_message_object_handlers;
533 static HashTable php_http_message_object_prophandlers;
534
535 typedef void (*php_http_message_object_prophandler_func_t)(php_http_message_object_t *o, zval *v TSRMLS_DC);
536
537 typedef struct php_http_message_object_prophandler {
538 php_http_message_object_prophandler_func_t read;
539 php_http_message_object_prophandler_func_t write;
540 } php_http_message_object_prophandler_t;
541
542 static STATUS php_http_message_object_add_prophandler(const char *prop_str, size_t prop_len, php_http_message_object_prophandler_func_t read, php_http_message_object_prophandler_func_t write) {
543 php_http_message_object_prophandler_t h = { read, write };
544 return zend_hash_add(&php_http_message_object_prophandlers, prop_str, prop_len + 1, (void *) &h, sizeof(h), NULL);
545 }
546 static STATUS php_http_message_object_get_prophandler(const char *prop_str, size_t prop_len, php_http_message_object_prophandler_t **handler) {
547 return zend_hash_find(&php_http_message_object_prophandlers, prop_str, prop_len + 1, (void *) handler);
548 }
549 static void php_http_message_object_prophandler_get_type(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
550 RETVAL_LONG(obj->message->type);
551 }
552 static void php_http_message_object_prophandler_set_type(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
553 zval *cpy = php_http_ztyp(IS_LONG, value);
554 php_http_message_set_type(obj->message, Z_LVAL_P(cpy));
555 zval_ptr_dtor(&cpy);
556 }
557 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
558 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message) && obj->message->http.info.request.method) {
559 RETVAL_STRING(obj->message->http.info.request.method, 1);
560 } else {
561 RETVAL_NULL();
562 }
563 }
564 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
565 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
566 zval *cpy = php_http_ztyp(IS_STRING, value);
567 PTR_SET(obj->message->http.info.request.method, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
568 zval_ptr_dtor(&cpy);
569 }
570 }
571 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
572 char *url_str;
573 size_t url_len;
574
575 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message) && obj->message->http.info.request.url && php_http_url_to_string(obj->message->http.info.request.url, &url_str, &url_len, 0)) {
576 RETVAL_STRINGL(url_str, url_len, 0);
577 } else {
578 RETVAL_NULL();
579 }
580 }
581 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
582 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
583 PTR_SET(obj->message->http.info.request.url, php_http_url_from_zval(value, ~0 TSRMLS_CC));
584 }
585 }
586 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
587 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message) && obj->message->http.info.response.status) {
588 RETVAL_STRING(obj->message->http.info.response.status, 1);
589 } else {
590 RETVAL_NULL();
591 }
592 }
593 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
594 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
595 zval *cpy = php_http_ztyp(IS_STRING, value);
596 PTR_SET(obj->message->http.info.response.status, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
597 zval_ptr_dtor(&cpy);
598 }
599 }
600 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
601 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
602 RETVAL_LONG(obj->message->http.info.response.code);
603 } else {
604 RETVAL_NULL();
605 }
606 }
607 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
608 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
609 zval *cpy = php_http_ztyp(IS_LONG, value);
610 obj->message->http.info.response.code = Z_LVAL_P(cpy);
611 PTR_SET(obj->message->http.info.response.status, estrdup(php_http_env_get_response_status_for_code(obj->message->http.info.response.code)));
612 zval_ptr_dtor(&cpy);
613 }
614 }
615 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
616 char *version_str;
617 size_t version_len;
618
619 php_http_version_to_string(&obj->message->http.version, &version_str, &version_len, NULL, NULL TSRMLS_CC);
620 RETVAL_STRINGL(version_str, version_len, 0);
621 }
622 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
623 zval *cpy = php_http_ztyp(IS_STRING, value);
624 php_http_version_parse(&obj->message->http.version, Z_STRVAL_P(cpy) TSRMLS_CC);
625 zval_ptr_dtor(&cpy);
626 }
627 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
628 array_init(return_value);
629 zend_hash_copy(Z_ARRVAL_P(return_value), &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
630 }
631 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
632 zval *cpy = php_http_ztyp(IS_ARRAY, value);
633
634 zend_hash_clean(&obj->message->hdrs);
635 zend_hash_copy(&obj->message->hdrs, Z_ARRVAL_P(cpy), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
636 zval_ptr_dtor(&cpy);
637 }
638 static void php_http_message_object_prophandler_get_body(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
639 if (obj->body) {
640 RETVAL_OBJVAL(obj->body->zv, 1);
641 } else {
642 RETVAL_NULL();
643 }
644 }
645 static void php_http_message_object_prophandler_set_body(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
646 php_http_message_object_set_body(obj, value TSRMLS_CC);
647 }
648 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
649 if (obj->message->parent) {
650 RETVAL_OBJVAL(obj->parent->zv, 1);
651 } else {
652 RETVAL_NULL();
653 }
654 }
655 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
656 if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_http_message_class_entry TSRMLS_CC)) {
657 php_http_message_object_t *parent_obj = zend_object_store_get_object(value TSRMLS_CC);
658
659 if (obj->message->parent) {
660 zend_objects_store_del_ref_by_handle(obj->parent->zv.handle TSRMLS_CC);
661 }
662 Z_OBJ_ADDREF_P(value);
663 obj->parent = parent_obj;
664 obj->message->parent = parent_obj->message;
665 }
666 }
667
668 #define PHP_HTTP_MESSAGE_OBJECT_INIT(obj) \
669 do { \
670 if (!obj->message) { \
671 obj->message = php_http_message_init(NULL, 0, NULL TSRMLS_CC); \
672 } \
673 } while(0)
674
675
676 void php_http_message_object_reverse(zval *this_ptr, zval *return_value TSRMLS_DC)
677 {
678 int i = 0;
679 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
680
681 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
682
683 /* count */
684 php_http_message_count(i, obj->message);
685
686 if (i > 1) {
687 php_http_message_object_t **objects;
688 int last;
689
690 objects = ecalloc(i, sizeof(**objects));
691
692 /* we are the first message */
693 objects[0] = obj;
694
695 /* fetch parents */
696 for (i = 1; obj->parent; ++i) {
697 objects[i] = obj = obj->parent;
698 }
699
700 /* reorder parents */
701 for (last = --i; i; --i) {
702 objects[i]->message->parent = objects[i-1]->message;
703 objects[i]->parent = objects[i-1];
704 }
705
706 objects[0]->message->parent = NULL;
707 objects[0]->parent = NULL;
708
709 /* add ref, because we previously have not been a parent message */
710 Z_OBJ_ADDREF_P(getThis());
711 RETVAL_OBJVAL(objects[last]->zv, 0);
712
713 efree(objects);
714 } else {
715 RETURN_ZVAL(getThis(), 1, 0);
716 }
717 }
718
719 void php_http_message_object_prepend(zval *this_ptr, zval *prepend, zend_bool top TSRMLS_DC)
720 {
721 zval m;
722 php_http_message_t *save_parent_msg = NULL;
723 php_http_message_object_t *save_parent_obj = NULL, *obj = zend_object_store_get_object(this_ptr TSRMLS_CC);
724 php_http_message_object_t *prepend_obj = zend_object_store_get_object(prepend TSRMLS_CC);
725
726 INIT_PZVAL(&m);
727 m.type = IS_OBJECT;
728
729 if (!top) {
730 save_parent_obj = obj->parent;
731 save_parent_msg = obj->message->parent;
732 } else {
733 /* iterate to the most parent object */
734 while (obj->parent) {
735 obj = obj->parent;
736 }
737 }
738
739 /* prepend */
740 obj->parent = prepend_obj;
741 obj->message->parent = prepend_obj->message;
742
743 /* add ref */
744 zend_objects_store_add_ref(prepend TSRMLS_CC);
745
746 if (!top) {
747 prepend_obj->parent = save_parent_obj;
748 prepend_obj->message->parent = save_parent_msg;
749 }
750 }
751
752 STATUS php_http_message_object_set_body(php_http_message_object_t *msg_obj, zval *zbody TSRMLS_DC)
753 {
754 zval *tmp = NULL;
755 php_stream *s;
756 zend_object_value ov;
757 php_http_message_body_t *body;
758 php_http_message_body_object_t *body_obj;
759
760 switch (Z_TYPE_P(zbody)) {
761 case IS_RESOURCE:
762 php_stream_from_zval_no_verify(s, &zbody);
763 if (!s) {
764 php_http_throw(unexpected_val, "The stream is not a valid resource", NULL);
765 return FAILURE;
766 }
767
768 is_resource:
769
770 body = php_http_message_body_init(NULL, s TSRMLS_CC);
771 if (SUCCESS != php_http_new(&ov, php_http_message_body_class_entry, (php_http_new_t) php_http_message_body_object_new_ex, NULL, body, NULL TSRMLS_CC)) {
772 php_http_message_body_free(&body);
773 return FAILURE;
774 }
775 MAKE_STD_ZVAL(tmp);
776 ZVAL_OBJVAL(tmp, ov, 0);
777 zbody = tmp;
778 break;
779
780 case IS_OBJECT:
781 if (instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC)) {
782 Z_OBJ_ADDREF_P(zbody);
783 break;
784 }
785 /* no break */
786
787 default:
788 tmp = php_http_ztyp(IS_STRING, zbody);
789 s = php_stream_temp_new();
790 php_stream_write(s, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
791 zval_ptr_dtor(&tmp);
792 tmp = NULL;
793 goto is_resource;
794
795 }
796
797 body_obj = zend_object_store_get_object(zbody TSRMLS_CC);
798 if (!body_obj->body) {
799 body_obj->body = php_http_message_body_init(NULL, NULL TSRMLS_CC);
800 }
801 if (msg_obj->body) {
802 zend_objects_store_del_ref_by_handle(msg_obj->body->zv.handle TSRMLS_CC);
803 }
804 if (msg_obj->message) {
805 php_http_message_body_free(&msg_obj->message->body);
806 msg_obj->message->body = php_http_message_body_init(&body_obj->body, NULL TSRMLS_CC);
807 } else {
808 msg_obj->message = php_http_message_init(NULL, 0, php_http_message_body_init(&body_obj->body, NULL TSRMLS_CC) TSRMLS_CC);
809 }
810 msg_obj->body = body_obj;
811
812 if (tmp) {
813 FREE_ZVAL(tmp);
814 }
815 return SUCCESS;
816 }
817
818 STATUS php_http_message_object_init_body_object(php_http_message_object_t *obj)
819 {
820 TSRMLS_FETCH_FROM_CTX(obj->message->ts);
821
822 php_http_message_body_addref(obj->message->body);
823 return php_http_new(NULL, php_http_message_body_class_entry, (php_http_new_t) php_http_message_body_object_new_ex, NULL, obj->message->body, (void *) &obj->body TSRMLS_CC);
824 }
825
826 zend_object_value php_http_message_object_new(zend_class_entry *ce TSRMLS_DC)
827 {
828 return php_http_message_object_new_ex(ce, NULL, NULL TSRMLS_CC);
829 }
830
831 zend_object_value php_http_message_object_new_ex(zend_class_entry *ce, php_http_message_t *msg, php_http_message_object_t **ptr TSRMLS_DC)
832 {
833 php_http_message_object_t *o;
834
835 o = ecalloc(1, sizeof(php_http_message_object_t));
836 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
837 object_properties_init((zend_object *) o, ce);
838
839 if (ptr) {
840 *ptr = o;
841 }
842
843 if (msg) {
844 o->message = msg;
845 if (msg->parent) {
846 php_http_message_object_new_ex(ce, msg->parent, &o->parent TSRMLS_CC);
847 }
848 php_http_message_body_object_new_ex(php_http_message_body_class_entry, php_http_message_body_init(&msg->body, NULL TSRMLS_CC), &o->body TSRMLS_CC);
849 }
850
851 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_object_free, NULL TSRMLS_CC);
852 o->zv.handlers = &php_http_message_object_handlers;
853
854 return o->zv;
855 }
856
857 zend_object_value php_http_message_object_clone(zval *this_ptr TSRMLS_DC)
858 {
859 zend_object_value new_ov;
860 php_http_message_object_t *new_obj = NULL;
861 php_http_message_object_t *old_obj = zend_object_store_get_object(this_ptr TSRMLS_CC);
862
863 new_ov = php_http_message_object_new_ex(old_obj->zo.ce, php_http_message_copy(old_obj->message, NULL), &new_obj TSRMLS_CC);
864 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
865
866 return new_ov;
867 }
868
869 void php_http_message_object_free(void *object TSRMLS_DC)
870 {
871 php_http_message_object_t *o = (php_http_message_object_t *) object;
872
873 if (o->iterator) {
874 zval_ptr_dtor(&o->iterator);
875 o->iterator = NULL;
876 }
877 if (o->message) {
878 /* do NOT free recursivly */
879 php_http_message_dtor(o->message);
880 efree(o->message);
881 o->message = NULL;
882 }
883 if (o->parent) {
884 zend_objects_store_del_ref_by_handle(o->parent->zv.handle TSRMLS_CC);
885 o->parent = NULL;
886 }
887 if (o->body) {
888 zend_objects_store_del_ref_by_handle(o->body->zv.handle TSRMLS_CC);
889 o->body = NULL;
890 }
891 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
892 efree(o);
893 }
894
895 static zval *php_http_message_object_read_prop(zval *object, zval *member, int type PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC)
896 {
897 php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
898 php_http_message_object_prophandler_t *handler;
899 zval *return_value, *copy = php_http_ztyp(IS_STRING, member);
900
901 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
902
903 if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
904 ALLOC_ZVAL(return_value);
905 Z_SET_REFCOUNT_P(return_value, 0);
906 Z_UNSET_ISREF_P(return_value);
907
908 if (type == BP_VAR_R) {
909 handler->read(obj, return_value TSRMLS_CC);
910 } else {
911 php_property_proxy_t *proxy = php_property_proxy_init(object, Z_STRVAL_P(copy), Z_STRLEN_P(copy) TSRMLS_CC);
912 RETVAL_OBJVAL(php_property_proxy_object_new_ex(php_property_proxy_get_class_entry(), proxy, NULL TSRMLS_CC), 0);
913 }
914 } else {
915 return_value = zend_get_std_object_handlers()->read_property(object, member, type PHP_HTTP_ZEND_LITERAL_CC TSRMLS_CC);
916 }
917
918 zval_ptr_dtor(&copy);
919
920 return return_value;
921 }
922
923 static void php_http_message_object_write_prop(zval *object, zval *member, zval *value PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC)
924 {
925 php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
926 php_http_message_object_prophandler_t *handler;
927 zval *copy = php_http_ztyp(IS_STRING, member);
928
929 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
930
931 if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
932 handler->write(obj, value TSRMLS_CC);
933 } else {
934 zend_get_std_object_handlers()->write_property(object, member, value PHP_HTTP_ZEND_LITERAL_CC TSRMLS_CC);
935 }
936
937 zval_ptr_dtor(&copy);
938 }
939
940 static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC)
941 {
942 zval *headers;
943 php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
944 HashTable *props = zend_get_std_object_handlers()->get_properties(object TSRMLS_CC);
945 zval array, *parent, *body;
946 char *ver_str, *url_str = NULL;
947 size_t ver_len, url_len = 0;
948
949 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
950 INIT_PZVAL_ARRAY(&array, props);
951
952 #define ASSOC_PROP(ptype, n, val) \
953 do { \
954 zend_property_info *pi; \
955 if (SUCCESS == zend_hash_find(&obj->zo.ce->properties_info, n, sizeof(n), (void *) &pi)) { \
956 add_assoc_ ##ptype## _ex(&array, pi->name, pi->name_length + 1, val); \
957 } \
958 } while(0) \
959
960 #define ASSOC_STRING(name, val) ASSOC_STRINGL(name, val, strlen(val))
961 #define ASSOC_STRINGL(name, val, len) ASSOC_STRINGL_EX(name, val, len, 1)
962 #define ASSOC_STRINGL_EX(n, val, len, cpy) \
963 do { \
964 zend_property_info *pi; \
965 if (SUCCESS == zend_hash_find(&obj->zo.ce->properties_info, n, sizeof(n), (void *) &pi)) { \
966 add_assoc_stringl_ex(&array, pi->name, pi->name_length + 1, val, len, cpy); \
967 } \
968 } while(0)
969
970 ASSOC_PROP(long, "type", obj->message->type);
971 ver_len = spprintf(&ver_str, 0, "%u.%u", obj->message->http.version.major, obj->message->http.version.minor);
972 ASSOC_STRINGL_EX("httpVersion", ver_str, ver_len, 0);
973
974 switch (obj->message->type) {
975 case PHP_HTTP_REQUEST:
976 ASSOC_PROP(long, "responseCode", 0);
977 ASSOC_STRINGL("responseStatus", "", 0);
978 ASSOC_STRING("requestMethod", STR_PTR(obj->message->http.info.request.method));
979 if (obj->message->http.info.request.url) {
980 php_http_url_to_string(obj->message->http.info.request.url, &url_str, &url_len, 0);
981 ASSOC_STRINGL_EX("requestUrl", url_str, url_len, 0);
982 } else {
983 ASSOC_STRINGL("requestUrl", "", 0);
984 }
985
986 break;
987
988 case PHP_HTTP_RESPONSE:
989 ASSOC_PROP(long, "responseCode", obj->message->http.info.response.code);
990 ASSOC_STRING("responseStatus", STR_PTR(obj->message->http.info.response.status));
991 ASSOC_STRINGL("requestMethod", "", 0);
992 ASSOC_STRINGL("requestUrl", "", 0);
993 break;
994
995 case PHP_HTTP_NONE:
996 default:
997 ASSOC_PROP(long, "responseCode", 0);
998 ASSOC_STRINGL("responseStatus", "", 0);
999 ASSOC_STRINGL("requestMethod", "", 0);
1000 ASSOC_STRINGL("requestUrl", "", 0);
1001 break;
1002 }
1003
1004 MAKE_STD_ZVAL(headers);
1005 array_init(headers);
1006 zend_hash_copy(Z_ARRVAL_P(headers), &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1007 ASSOC_PROP(zval, "headers", headers);
1008
1009 MAKE_STD_ZVAL(body);
1010 if (obj->body) {
1011 ZVAL_OBJVAL(body, obj->body->zv, 1);
1012 } else {
1013 ZVAL_NULL(body);
1014 }
1015 ASSOC_PROP(zval, "body", body);
1016
1017 MAKE_STD_ZVAL(parent);
1018 if (obj->message->parent) {
1019 ZVAL_OBJVAL(parent, obj->parent->zv, 1);
1020 } else {
1021 ZVAL_NULL(parent);
1022 }
1023 ASSOC_PROP(zval, "parentMessage", parent);
1024
1025 return props;
1026 }
1027
1028 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___construct, 0, 0, 0)
1029 ZEND_ARG_INFO(0, message)
1030 ZEND_ARG_INFO(0, greedy)
1031 ZEND_END_ARG_INFO();
1032 static PHP_METHOD(HttpMessage, __construct)
1033 {
1034 zend_bool greedy = 1;
1035 zval *zmessage = NULL;
1036 php_http_message_t *msg = NULL;
1037 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1038 zend_error_handling zeh;
1039
1040 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!b", &zmessage, &greedy), invalid_arg, return);
1041
1042 zend_replace_error_handling(EH_THROW, php_http_exception_bad_message_class_entry, &zeh TSRMLS_CC);
1043 if (zmessage && Z_TYPE_P(zmessage) == IS_RESOURCE) {
1044 php_stream *s;
1045 php_http_message_parser_t p;
1046 zend_error_handling zeh;
1047
1048 zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh TSRMLS_CC);
1049 php_stream_from_zval(s, &zmessage);
1050 zend_restore_error_handling(&zeh TSRMLS_CC);
1051
1052 if (s && php_http_message_parser_init(&p TSRMLS_CC)) {
1053 unsigned flags = (greedy ? PHP_HTTP_MESSAGE_PARSER_GREEDY : 0);
1054 php_http_buffer_t buf;
1055
1056 php_http_buffer_init_ex(&buf, 0x1000, PHP_HTTP_BUFFER_INIT_PREALLOC);
1057 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse_stream(&p, &buf, s, flags, &msg)) {
1058 if (!EG(exception)) {
1059 php_http_throw(bad_message, "Could not parse message from stream", NULL);
1060 }
1061 }
1062 php_http_buffer_dtor(&buf);
1063 php_http_message_parser_dtor(&p);
1064 }
1065
1066 if (!msg && !EG(exception)) {
1067 php_http_throw(bad_message, "Empty message received from stream", NULL);
1068 }
1069 } else if (zmessage) {
1070 zmessage = php_http_ztyp(IS_STRING, zmessage);
1071 msg = php_http_message_parse(NULL, Z_STRVAL_P(zmessage), Z_STRLEN_P(zmessage), greedy TSRMLS_CC);
1072
1073 if (!msg && !EG(exception)) {
1074 php_http_throw(bad_message, "Could not parse message: %.*s", MIN(25, Z_STRLEN_P(zmessage)), Z_STRVAL_P(zmessage));
1075 }
1076 zval_ptr_dtor(&zmessage);
1077 }
1078
1079 if (msg) {
1080 php_http_message_dtor(obj->message);
1081 obj->message = msg;
1082 if (obj->message->parent) {
1083 php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, &obj->parent TSRMLS_CC);
1084 }
1085 }
1086 zend_restore_error_handling(&zeh TSRMLS_CC);
1087 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1088 }
1089
1090 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getBody, 0, 0, 0)
1091 ZEND_END_ARG_INFO();
1092 static PHP_METHOD(HttpMessage, getBody)
1093 {
1094 php_http_message_object_t *obj;
1095
1096 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1097
1098 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1099
1100 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1101
1102 if (!obj->body) {
1103 php_http_message_object_init_body_object(obj);
1104
1105 }
1106 if (obj->body) {
1107 RETVAL_OBJVAL(obj->body->zv, 1);
1108 }
1109 }
1110
1111 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setBody, 0, 0, 1)
1112 ZEND_ARG_OBJ_INFO(0, body, http\\Message\\Body, 0)
1113 ZEND_END_ARG_INFO();
1114 static PHP_METHOD(HttpMessage, setBody)
1115 {
1116 zval *zbody;
1117
1118 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zbody, php_http_message_body_class_entry)) {
1119 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1120
1121 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1122 php_http_message_object_prophandler_set_body(obj, zbody TSRMLS_CC);
1123 }
1124 RETVAL_ZVAL(getThis(), 1, 0);
1125 }
1126
1127 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addBody, 0, 0, 1)
1128 ZEND_ARG_OBJ_INFO(0, body, http\\Message\\Body, 0)
1129 ZEND_END_ARG_INFO();
1130 static PHP_METHOD(HttpMessage, addBody)
1131 {
1132 zval *new_body;
1133
1134 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &new_body, php_http_message_body_class_entry)) {
1135 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1136 php_http_message_body_object_t *new_obj = zend_object_store_get_object(new_body TSRMLS_CC);
1137
1138 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1139 php_http_message_body_to_callback(new_obj->body, (php_http_pass_callback_t) php_http_message_body_append, obj->message->body, 0, 0);
1140 }
1141 RETVAL_ZVAL(getThis(), 1, 0);
1142 }
1143
1144 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeader, 0, 0, 1)
1145 ZEND_ARG_INFO(0, header)
1146 ZEND_ARG_INFO(0, into_class)
1147 ZEND_END_ARG_INFO();
1148 static PHP_METHOD(HttpMessage, getHeader)
1149 {
1150 char *header_str;
1151 int header_len;
1152 zend_class_entry *header_ce = NULL;
1153
1154 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|C!", &header_str, &header_len, &header_ce)) {
1155 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1156 zval *header;
1157
1158 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1159
1160 if ((header = php_http_message_header(obj->message, header_str, header_len, 0))) {
1161 if (!header_ce) {
1162 RETURN_ZVAL(header, 1, 1);
1163 } else if (instanceof_function(header_ce, php_http_header_class_entry TSRMLS_CC)) {
1164 php_http_object_method_t cb;
1165 zval *header_name, **argv[2];
1166
1167 MAKE_STD_ZVAL(header_name);
1168 ZVAL_STRINGL(header_name, header_str, header_len, 1);
1169
1170 argv[0] = &header_name;
1171 argv[1] = &header;
1172
1173 object_init_ex(return_value, header_ce);
1174 php_http_object_method_init(&cb, return_value, ZEND_STRL("__construct") TSRMLS_CC);
1175 php_http_object_method_call(&cb, return_value, NULL, 2, argv TSRMLS_CC);
1176 php_http_object_method_dtor(&cb);
1177
1178 zval_ptr_dtor(&header_name);
1179 zval_ptr_dtor(&header);
1180
1181 return;
1182 } else {
1183 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Class '%s' is not as descendant of http\\Header", header_ce->name);
1184 }
1185 }
1186 }
1187 RETURN_FALSE;
1188 }
1189
1190 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeaders, 0, 0, 0)
1191 ZEND_END_ARG_INFO();
1192 static PHP_METHOD(HttpMessage, getHeaders)
1193 {
1194 if (SUCCESS == zend_parse_parameters_none()) {
1195 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1196
1197 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1198
1199 array_init(return_value);
1200 array_copy(&obj->message->hdrs, Z_ARRVAL_P(return_value));
1201 }
1202 }
1203
1204 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeader, 0, 0, 1)
1205 ZEND_ARG_INFO(0, header)
1206 ZEND_ARG_INFO(0, value)
1207 ZEND_END_ARG_INFO();
1208 static PHP_METHOD(HttpMessage, setHeader)
1209 {
1210 zval *zvalue = NULL;
1211 char *name_str;
1212 int name_len;
1213
1214 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z!", &name_str, &name_len, &zvalue)) {
1215 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1216 char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
1217
1218 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1219
1220 if (!zvalue) {
1221 zend_symtable_del(&obj->message->hdrs, name, name_len + 1);
1222 } else {
1223 Z_ADDREF_P(zvalue);
1224 zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
1225 }
1226 efree(name);
1227 }
1228 RETVAL_ZVAL(getThis(), 1, 0);
1229 }
1230
1231 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeaders, 0, 0, 1)
1232 ZEND_ARG_ARRAY_INFO(0, headers, 1)
1233 ZEND_END_ARG_INFO();
1234 static PHP_METHOD(HttpMessage, setHeaders)
1235 {
1236 zval *new_headers = NULL;
1237
1238 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &new_headers)) {
1239 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1240
1241 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1242
1243 zend_hash_clean(&obj->message->hdrs);
1244 if (new_headers) {
1245 array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, 0, ARRAY_JOIN_PRETTIFY|ARRAY_JOIN_STRONLY);
1246 }
1247 }
1248 RETVAL_ZVAL(getThis(), 1, 0);
1249 }
1250
1251 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeader, 0, 0, 2)
1252 ZEND_ARG_INFO(0, header)
1253 ZEND_ARG_INFO(0, value)
1254 ZEND_END_ARG_INFO();
1255 static PHP_METHOD(HttpMessage, addHeader)
1256 {
1257 zval *zvalue;
1258 char *name_str;
1259 int name_len;
1260
1261 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name_str, &name_len, &zvalue)) {
1262 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1263 char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
1264 zval *header;
1265
1266 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1267
1268 Z_ADDREF_P(zvalue);
1269 if ((header = php_http_message_header(obj->message, name, name_len, 0))) {
1270 convert_to_array(header);
1271 zend_hash_next_index_insert(Z_ARRVAL_P(header), &zvalue, sizeof(void *), NULL);
1272 zval_ptr_dtor(&header);
1273 } else {
1274 zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
1275 }
1276 efree(name);
1277 }
1278 RETVAL_ZVAL(getThis(), 1, 0);
1279 }
1280
1281 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeaders, 0, 0, 1)
1282 ZEND_ARG_ARRAY_INFO(0, headers, 0)
1283 ZEND_ARG_INFO(0, append)
1284 ZEND_END_ARG_INFO();
1285 static PHP_METHOD(HttpMessage, addHeaders)
1286 {
1287 zval *new_headers;
1288 zend_bool append = 0;
1289
1290 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
1291 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1292
1293 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1294
1295 array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, append, ARRAY_JOIN_STRONLY|ARRAY_JOIN_PRETTIFY);
1296 }
1297 RETVAL_ZVAL(getThis(), 1, 0);
1298 }
1299
1300 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getType, 0, 0, 0)
1301 ZEND_END_ARG_INFO();
1302 static PHP_METHOD(HttpMessage, getType)
1303 {
1304 if (SUCCESS == zend_parse_parameters_none()) {
1305 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1306
1307 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1308
1309 RETURN_LONG(obj->message->type);
1310 }
1311 }
1312
1313 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setType, 0, 0, 1)
1314 ZEND_ARG_INFO(0, type)
1315 ZEND_END_ARG_INFO();
1316 static PHP_METHOD(HttpMessage, setType)
1317 {
1318 long type;
1319
1320 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
1321 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1322
1323 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1324
1325 php_http_message_set_type(obj->message, type);
1326 }
1327 RETVAL_ZVAL(getThis(), 1, 0);
1328 }
1329
1330 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getInfo, 0, 0, 0)
1331 ZEND_END_ARG_INFO();
1332 static PHP_METHOD(HttpMessage, getInfo)
1333 {
1334 if (SUCCESS == zend_parse_parameters_none()) {
1335 char *tmp = NULL;
1336 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1337
1338 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1339
1340 switch (obj->message->type) {
1341 case PHP_HTTP_REQUEST:
1342 Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj->message->http, tmp, ""));
1343 PTR_FREE(tmp);
1344 break;
1345 case PHP_HTTP_RESPONSE:
1346 Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj->message->http, tmp, ""));
1347 PTR_FREE(tmp);
1348 break;
1349 default:
1350 RETURN_NULL();
1351 break;
1352 }
1353 Z_TYPE_P(return_value) = IS_STRING;
1354 return;
1355 }
1356 }
1357
1358 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setInfo, 0, 0, 1)
1359 ZEND_ARG_INFO(0, http_info)
1360 ZEND_END_ARG_INFO();
1361 static PHP_METHOD(HttpMessage, setInfo)
1362 {
1363 char *str;
1364 int len;
1365 php_http_message_object_t *obj;
1366 php_http_info_t inf;
1367
1368 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len), invalid_arg, return);
1369
1370 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1371 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1372
1373 if (!php_http_info_parse(&inf, str TSRMLS_CC)) {
1374 php_http_throw(bad_header, "Could not parse message info '%s'", str);
1375 return;
1376 }
1377
1378 php_http_message_set_info(obj->message, &inf);
1379 php_http_info_dtor(&inf);
1380
1381 RETVAL_ZVAL(getThis(), 1, 0);
1382 }
1383
1384 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHttpVersion, 0, 0, 0)
1385 ZEND_END_ARG_INFO();
1386 static PHP_METHOD(HttpMessage, getHttpVersion)
1387 {
1388 if (SUCCESS == zend_parse_parameters_none()) {
1389 char *str;
1390 size_t len;
1391 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1392
1393 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1394
1395 php_http_version_to_string(&obj->message->http.version, &str, &len, NULL, NULL TSRMLS_CC);
1396 RETURN_STRINGL(str, len, 0);
1397 }
1398 }
1399
1400 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHttpVersion, 0, 0, 1)
1401 ZEND_ARG_INFO(0, http_version)
1402 ZEND_END_ARG_INFO();
1403 static PHP_METHOD(HttpMessage, setHttpVersion)
1404 {
1405 char *v_str;
1406 int v_len;
1407 php_http_version_t version;
1408 php_http_message_object_t *obj;
1409
1410 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &v_str, &v_len), invalid_arg, return);
1411
1412 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1413 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1414
1415 php_http_expect(php_http_version_parse(&version, v_str TSRMLS_CC), unexpected_val, return);
1416
1417 obj->message->http.version = version;
1418
1419 RETVAL_ZVAL(getThis(), 1, 0);
1420 }
1421
1422 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseCode, 0, 0, 0)
1423 ZEND_END_ARG_INFO();
1424 static PHP_METHOD(HttpMessage, getResponseCode)
1425 {
1426 if (SUCCESS == zend_parse_parameters_none()) {
1427 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1428
1429 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1430
1431 if (obj->message->type != PHP_HTTP_RESPONSE) {
1432 php_error_docref(NULL TSRMLS_CC, E_WARNING, "http\\Message is not if type response");
1433 RETURN_FALSE;
1434 }
1435
1436 RETURN_LONG(obj->message->http.info.response.code);
1437 }
1438 }
1439
1440 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseCode, 0, 0, 1)
1441 ZEND_ARG_INFO(0, response_code)
1442 ZEND_ARG_INFO(0, strict)
1443 ZEND_END_ARG_INFO();
1444 static PHP_METHOD(HttpMessage, setResponseCode)
1445 {
1446 long code;
1447 zend_bool strict = 1;
1448 php_http_message_object_t *obj;
1449
1450 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|b", &code, &strict), invalid_arg, return);
1451
1452 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1453
1454 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1455
1456 if (obj->message->type != PHP_HTTP_RESPONSE) {
1457 php_http_throw(bad_method_call, "http\\Message is not of type response", NULL);
1458 return;
1459 }
1460
1461 if (strict && (code < 100 || code > 599)) {
1462 php_http_throw(invalid_arg, "Invalid response code (100-599): %ld", code);
1463 return;
1464 }
1465
1466 obj->message->http.info.response.code = code;
1467 PTR_SET(obj->message->http.info.response.status, estrdup(php_http_env_get_response_status_for_code(code)));
1468
1469 RETVAL_ZVAL(getThis(), 1, 0);
1470 }
1471
1472 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseStatus, 0, 0, 0)
1473 ZEND_END_ARG_INFO();
1474 static PHP_METHOD(HttpMessage, getResponseStatus)
1475 {
1476 if (SUCCESS == zend_parse_parameters_none()) {
1477 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1478
1479 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1480
1481 if (obj->message->type != PHP_HTTP_RESPONSE) {
1482 php_error_docref(NULL TSRMLS_CC, E_WARNING, "http\\Message is not of type response");
1483 }
1484
1485 if (obj->message->http.info.response.status) {
1486 RETURN_STRING(obj->message->http.info.response.status, 1);
1487 } else {
1488 RETURN_EMPTY_STRING();
1489 }
1490 }
1491 }
1492
1493 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseStatus, 0, 0, 1)
1494 ZEND_ARG_INFO(0, response_status)
1495 ZEND_END_ARG_INFO();
1496 static PHP_METHOD(HttpMessage, setResponseStatus)
1497 {
1498 char *status;
1499 int status_len;
1500 php_http_message_object_t *obj;
1501
1502 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len), invalid_arg, return);
1503
1504 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1505
1506 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1507
1508 if (obj->message->type != PHP_HTTP_RESPONSE) {
1509 php_http_throw(bad_method_call, "http\\Message is not of type response", NULL);
1510 }
1511
1512 PTR_SET(obj->message->http.info.response.status, estrndup(status, status_len));
1513 RETVAL_ZVAL(getThis(), 1, 0);
1514 }
1515
1516 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestMethod, 0, 0, 0)
1517 ZEND_END_ARG_INFO();
1518 static PHP_METHOD(HttpMessage, getRequestMethod)
1519 {
1520 if (SUCCESS == zend_parse_parameters_none()) {
1521 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1522
1523 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1524
1525 if (obj->message->type != PHP_HTTP_REQUEST) {
1526 php_error_docref(NULL TSRMLS_CC, E_WARNING, "http\\Message is not of type request");
1527 RETURN_FALSE;
1528 }
1529
1530 if (obj->message->http.info.request.method) {
1531 RETURN_STRING(obj->message->http.info.request.method, 1);
1532 } else {
1533 RETURN_EMPTY_STRING();
1534 }
1535 }
1536 }
1537
1538 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestMethod, 0, 0, 1)
1539 ZEND_ARG_INFO(0, request_method)
1540 ZEND_END_ARG_INFO();
1541 static PHP_METHOD(HttpMessage, setRequestMethod)
1542 {
1543 char *method;
1544 int method_len;
1545 php_http_message_object_t *obj;
1546
1547 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len), invalid_arg, return);
1548
1549 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1550
1551 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1552
1553 if (obj->message->type != PHP_HTTP_REQUEST) {
1554 php_http_throw(bad_method_call, "http\\Message is not of type request", NULL);
1555 return;
1556 }
1557
1558 if (method_len < 1) {
1559 php_http_throw(invalid_arg, "Cannot set http\\Message's request method to an empty string", NULL);
1560 return;
1561 }
1562
1563 PTR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
1564 RETVAL_ZVAL(getThis(), 1, 0);
1565 }
1566
1567 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestUrl, 0, 0, 0)
1568 ZEND_END_ARG_INFO();
1569 static PHP_METHOD(HttpMessage, getRequestUrl)
1570 {
1571 if (SUCCESS == zend_parse_parameters_none()) {
1572 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1573
1574 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1575
1576 if (obj->message->type != PHP_HTTP_REQUEST) {
1577 php_error_docref(NULL TSRMLS_CC, E_WARNING, "http\\Message is not of type request");
1578 RETURN_FALSE;
1579 }
1580
1581 if (obj->message->http.info.request.url) {
1582 char *url_str;
1583 size_t url_len;
1584
1585 php_http_url_to_string(obj->message->http.info.request.url, &url_str, &url_len, 0);
1586 RETURN_STRINGL(url_str, url_len, 0);
1587 } else {
1588 RETURN_EMPTY_STRING();
1589 }
1590 }
1591 }
1592
1593 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestUrl, 0, 0, 1)
1594 ZEND_ARG_INFO(0, url)
1595 ZEND_END_ARG_INFO();
1596 static PHP_METHOD(HttpMessage, setRequestUrl)
1597 {
1598 zval *zurl;
1599 php_http_url_t *url;
1600 php_http_message_object_t *obj;
1601 zend_error_handling zeh;
1602
1603 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zurl), invalid_arg, return);
1604
1605 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1606
1607 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1608
1609 if (obj->message->type != PHP_HTTP_REQUEST) {
1610 php_http_throw(bad_method_call, "http\\Message is not of type request", NULL);
1611 return;
1612 }
1613
1614 zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh TSRMLS_CC);
1615 url = php_http_url_from_zval(zurl, ~0 TSRMLS_CC);
1616 zend_restore_error_handling(&zeh TSRMLS_CC);
1617
1618 if (php_http_url_is_empty(url)) {
1619 php_http_url_free(&url);
1620 php_http_throw(invalid_arg, "Cannot set http\\Message's request url to an empty string", NULL);
1621 } else {
1622 PTR_SET(obj->message->http.info.request.url, url);
1623 }
1624
1625 RETVAL_ZVAL(getThis(), 1, 0);
1626 }
1627
1628 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getParentMessage, 0, 0, 0)
1629 ZEND_END_ARG_INFO();
1630 static PHP_METHOD(HttpMessage, getParentMessage)
1631 {
1632 php_http_message_object_t *obj;
1633
1634 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1635
1636 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1637
1638 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1639
1640 if (!obj->message->parent) {
1641 php_http_throw(unexpected_val, "http\\Message has not parent message", NULL);
1642 return;
1643 }
1644
1645 RETVAL_OBJVAL(obj->parent->zv, 1);
1646 }
1647
1648 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___toString, 0, 0, 0)
1649 ZEND_END_ARG_INFO();
1650 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toString, 0, 0, 0)
1651 ZEND_ARG_INFO(0, include_parent)
1652 ZEND_END_ARG_INFO();
1653 static PHP_METHOD(HttpMessage, toString)
1654 {
1655 zend_bool include_parent = 0;
1656
1657 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
1658 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1659 char *string;
1660 size_t length;
1661
1662 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1663
1664 if (include_parent) {
1665 php_http_message_serialize(obj->message, &string, &length);
1666 } else {
1667 php_http_message_to_string(obj->message, &string, &length);
1668 }
1669 if (string) {
1670 RETURN_STRINGL(string, length, 0);
1671 }
1672 }
1673 RETURN_EMPTY_STRING();
1674 }
1675
1676 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toStream, 0, 0, 1)
1677 ZEND_ARG_INFO(0, stream)
1678 ZEND_END_ARG_INFO();
1679 static PHP_METHOD(HttpMessage, toStream)
1680 {
1681 zval *zstream;
1682
1683 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream)) {
1684 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1685 php_stream *s;
1686
1687 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1688
1689 php_stream_from_zval(s, &zstream);
1690 php_http_message_to_callback(obj->message, (php_http_pass_callback_t) _php_stream_write, s);
1691 }
1692 }
1693
1694 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toCallback, 0, 0, 1)
1695 ZEND_ARG_INFO(0, callback)
1696 ZEND_END_ARG_INFO();
1697 static PHP_METHOD(HttpMessage, toCallback)
1698 {
1699 php_http_pass_fcall_arg_t fcd;
1700
1701 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f", &fcd.fci, &fcd.fcc)) {
1702 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1703
1704 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1705
1706 fcd.fcz = getThis();
1707 Z_ADDREF_P(fcd.fcz);
1708 TSRMLS_SET_CTX(fcd.ts);
1709
1710 php_http_message_to_callback(obj->message, php_http_pass_fcall_callback, &fcd);
1711 zend_fcall_info_args_clear(&fcd.fci, 1);
1712
1713 zval_ptr_dtor(&fcd.fcz);
1714 RETURN_ZVAL(getThis(), 1, 0);
1715 }
1716 }
1717
1718 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_serialize, 0, 0, 0)
1719 ZEND_END_ARG_INFO();
1720 static PHP_METHOD(HttpMessage, serialize)
1721 {
1722 if (SUCCESS == zend_parse_parameters_none()) {
1723 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1724 char *string;
1725 size_t length;
1726
1727 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1728
1729 php_http_message_serialize(obj->message, &string, &length);
1730 RETURN_STRINGL(string, length, 0);
1731 }
1732 RETURN_EMPTY_STRING();
1733 }
1734
1735 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_unserialize, 0, 0, 1)
1736 ZEND_ARG_INFO(0, serialized)
1737 ZEND_END_ARG_INFO();
1738 static PHP_METHOD(HttpMessage, unserialize)
1739 {
1740 int length;
1741 char *serialized;
1742
1743 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &length)) {
1744 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1745 php_http_message_t *msg;
1746
1747 if (obj->message) {
1748 php_http_message_dtor(obj->message);
1749 efree(obj->message);
1750 }
1751 if ((msg = php_http_message_parse(NULL, serialized, (size_t) length, 1 TSRMLS_CC))) {
1752 obj->message = msg;
1753 } else {
1754 obj->message = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
1755 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not unserialize http\\Message");
1756 }
1757 }
1758 }
1759
1760 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_detach, 0, 0, 0)
1761 ZEND_END_ARG_INFO();
1762 static PHP_METHOD(HttpMessage, detach)
1763 {
1764 php_http_message_object_t *obj;
1765
1766 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1767
1768 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1769
1770 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1771
1772 RETVAL_OBJVAL(php_http_message_object_new_ex(obj->zo.ce, php_http_message_copy_ex(obj->message, NULL, 0), NULL TSRMLS_CC), 0);
1773 }
1774
1775 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_prepend, 0, 0, 1)
1776 ZEND_ARG_OBJ_INFO(0, message, http\\Message, 0)
1777 ZEND_ARG_INFO(0, top)
1778 ZEND_END_ARG_INFO();
1779 static PHP_METHOD(HttpMessage, prepend)
1780 {
1781 zval *prepend;
1782 zend_bool top = 1;
1783 php_http_message_t *msg[2];
1784 php_http_message_object_t *obj, *prepend_obj;
1785
1786 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &prepend, php_http_message_class_entry, &top), invalid_arg, return);
1787
1788 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1789 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1790 prepend_obj = zend_object_store_get_object(prepend TSRMLS_CC);
1791 PHP_HTTP_MESSAGE_OBJECT_INIT(prepend_obj);
1792
1793 /* safety check */
1794 for (msg[0] = obj->message; msg[0]; msg[0] = msg[0]->parent) {
1795 for (msg[1] = prepend_obj->message; msg[1]; msg[1] = msg[1]->parent) {
1796 if (msg[0] == msg[1]) {
1797 php_http_throw(unexpected_val, "Cannot prepend a message located within the same message chain", NULL);
1798 return;
1799 }
1800 }
1801 }
1802
1803 php_http_message_object_prepend(getThis(), prepend, top TSRMLS_CC);
1804 RETURN_ZVAL(getThis(), 1, 0);
1805 }
1806
1807 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_reverse, 0, 0, 0)
1808 ZEND_END_ARG_INFO();
1809 static PHP_METHOD(HttpMessage, reverse)
1810 {
1811 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1812
1813 php_http_message_object_reverse(getThis(), return_value TSRMLS_CC);
1814 }
1815
1816 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_isMultipart, 0, 0, 0)
1817 ZEND_ARG_INFO(1, boundary)
1818 ZEND_END_ARG_INFO();
1819 static PHP_METHOD(HttpMessage, isMultipart)
1820 {
1821 zval *zboundary = NULL;
1822
1823 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &zboundary)) {
1824 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1825 char *boundary = NULL;
1826
1827 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1828
1829 RETVAL_BOOL(php_http_message_is_multipart(obj->message, zboundary ? &boundary : NULL));
1830
1831 if (zboundary && boundary) {
1832 zval_dtor(zboundary);
1833 ZVAL_STRING(zboundary, boundary, 0);
1834 }
1835 }
1836 }
1837
1838 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_splitMultipartBody, 0, 0, 0)
1839 ZEND_END_ARG_INFO();
1840 static PHP_METHOD(HttpMessage, splitMultipartBody)
1841 {
1842 php_http_message_object_t *obj;
1843 php_http_message_t *msg;
1844 char *boundary = NULL;
1845
1846 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1847
1848 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1849
1850 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1851
1852 if (!php_http_message_is_multipart(obj->message, &boundary)) {
1853 php_http_throw(bad_method_call, "http\\Message is not a multipart message", NULL);
1854 return;
1855 }
1856
1857 php_http_expect(msg = php_http_message_body_split(obj->message->body, boundary), bad_message, return);
1858
1859 PTR_FREE(boundary);
1860
1861 RETURN_OBJVAL(php_http_message_object_new_ex(php_http_message_class_entry, msg, NULL TSRMLS_CC), 0);
1862 }
1863
1864 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_count, 0, 0, 0)
1865 ZEND_END_ARG_INFO();
1866 static PHP_METHOD(HttpMessage, count)
1867 {
1868 long count_mode = -1;
1869
1870 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &count_mode)) {
1871 long i = 0;
1872 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1873
1874 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1875
1876 php_http_message_count(i, obj->message);
1877 RETURN_LONG(i);
1878 }
1879 }
1880
1881 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_rewind, 0, 0, 0)
1882 ZEND_END_ARG_INFO();
1883 static PHP_METHOD(HttpMessage, rewind)
1884 {
1885 if (SUCCESS == zend_parse_parameters_none()) {
1886 zval *zobj = getThis();
1887 php_http_message_object_t *obj = zend_object_store_get_object(zobj TSRMLS_CC);
1888
1889 if (obj->iterator) {
1890 zval_ptr_dtor(&obj->iterator);
1891 }
1892 Z_ADDREF_P(zobj);
1893 obj->iterator = zobj;
1894 }
1895 }
1896
1897 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_valid, 0, 0, 0)
1898 ZEND_END_ARG_INFO();
1899 static PHP_METHOD(HttpMessage, valid)
1900 {
1901 if (SUCCESS == zend_parse_parameters_none()) {
1902 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1903
1904 RETURN_BOOL(obj->iterator != NULL);
1905 }
1906 }
1907
1908 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_next, 0, 0, 0)
1909 ZEND_END_ARG_INFO();
1910 static PHP_METHOD(HttpMessage, next)
1911 {
1912 if (SUCCESS == zend_parse_parameters_none()) {
1913 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1914
1915 if (obj->iterator) {
1916 php_http_message_object_t *itr = zend_object_store_get_object(obj->iterator TSRMLS_CC);
1917
1918 if (itr && itr->parent) {
1919 zval *old = obj->iterator;
1920 MAKE_STD_ZVAL(obj->iterator);
1921 ZVAL_OBJVAL(obj->iterator, itr->parent->zv, 1);
1922 zval_ptr_dtor(&old);
1923 } else {
1924 zval_ptr_dtor(&obj->iterator);
1925 obj->iterator = NULL;
1926 }
1927 }
1928 }
1929 }
1930
1931 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_key, 0, 0, 0)
1932 ZEND_END_ARG_INFO();
1933 static PHP_METHOD(HttpMessage, key)
1934 {
1935 if (SUCCESS == zend_parse_parameters_none()) {
1936 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1937
1938 RETURN_LONG(obj->iterator ? obj->iterator->value.obj.handle:0);
1939 }
1940 }
1941
1942 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_current, 0, 0, 0)
1943 ZEND_END_ARG_INFO();
1944 static PHP_METHOD(HttpMessage, current)
1945 {
1946 if (SUCCESS == zend_parse_parameters_none()) {
1947 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1948
1949 if (obj->iterator) {
1950 RETURN_ZVAL(obj->iterator, 1, 0);
1951 }
1952 }
1953 }
1954
1955 static zend_function_entry php_http_message_methods[] = {
1956 PHP_ME(HttpMessage, __construct, ai_HttpMessage___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1957 PHP_ME(HttpMessage, getBody, ai_HttpMessage_getBody, ZEND_ACC_PUBLIC)
1958 PHP_ME(HttpMessage, setBody, ai_HttpMessage_setBody, ZEND_ACC_PUBLIC)
1959 PHP_ME(HttpMessage, addBody, ai_HttpMessage_addBody, ZEND_ACC_PUBLIC)
1960 PHP_ME(HttpMessage, getHeader, ai_HttpMessage_getHeader, ZEND_ACC_PUBLIC)
1961 PHP_ME(HttpMessage, setHeader, ai_HttpMessage_setHeader, ZEND_ACC_PUBLIC)
1962 PHP_ME(HttpMessage, addHeader, ai_HttpMessage_addHeader, ZEND_ACC_PUBLIC)
1963 PHP_ME(HttpMessage, getHeaders, ai_HttpMessage_getHeaders, ZEND_ACC_PUBLIC)
1964 PHP_ME(HttpMessage, setHeaders, ai_HttpMessage_setHeaders, ZEND_ACC_PUBLIC)
1965 PHP_ME(HttpMessage, addHeaders, ai_HttpMessage_addHeaders, ZEND_ACC_PUBLIC)
1966 PHP_ME(HttpMessage, getType, ai_HttpMessage_getType, ZEND_ACC_PUBLIC)
1967 PHP_ME(HttpMessage, setType, ai_HttpMessage_setType, ZEND_ACC_PUBLIC)
1968 PHP_ME(HttpMessage, getInfo, ai_HttpMessage_getInfo, ZEND_ACC_PUBLIC)
1969 PHP_ME(HttpMessage, setInfo, ai_HttpMessage_setInfo, ZEND_ACC_PUBLIC)
1970 PHP_ME(HttpMessage, getResponseCode, ai_HttpMessage_getResponseCode, ZEND_ACC_PUBLIC)
1971 PHP_ME(HttpMessage, setResponseCode, ai_HttpMessage_setResponseCode, ZEND_ACC_PUBLIC)
1972 PHP_ME(HttpMessage, getResponseStatus, ai_HttpMessage_getResponseStatus, ZEND_ACC_PUBLIC)
1973 PHP_ME(HttpMessage, setResponseStatus, ai_HttpMessage_setResponseStatus, ZEND_ACC_PUBLIC)
1974 PHP_ME(HttpMessage, getRequestMethod, ai_HttpMessage_getRequestMethod, ZEND_ACC_PUBLIC)
1975 PHP_ME(HttpMessage, setRequestMethod, ai_HttpMessage_setRequestMethod, ZEND_ACC_PUBLIC)
1976 PHP_ME(HttpMessage, getRequestUrl, ai_HttpMessage_getRequestUrl, ZEND_ACC_PUBLIC)
1977 PHP_ME(HttpMessage, setRequestUrl, ai_HttpMessage_setRequestUrl, ZEND_ACC_PUBLIC)
1978 PHP_ME(HttpMessage, getHttpVersion, ai_HttpMessage_getHttpVersion, ZEND_ACC_PUBLIC)
1979 PHP_ME(HttpMessage, setHttpVersion, ai_HttpMessage_setHttpVersion, ZEND_ACC_PUBLIC)
1980 PHP_ME(HttpMessage, getParentMessage, ai_HttpMessage_getParentMessage, ZEND_ACC_PUBLIC)
1981 PHP_ME(HttpMessage, toString, ai_HttpMessage_toString, ZEND_ACC_PUBLIC)
1982 PHP_ME(HttpMessage, toCallback, ai_HttpMessage_toCallback, ZEND_ACC_PUBLIC)
1983 PHP_ME(HttpMessage, toStream, ai_HttpMessage_toStream, ZEND_ACC_PUBLIC)
1984
1985 /* implements Countable */
1986 PHP_ME(HttpMessage, count, ai_HttpMessage_count, ZEND_ACC_PUBLIC)
1987
1988 /* implements Serializable */
1989 PHP_ME(HttpMessage, serialize, ai_HttpMessage_serialize, ZEND_ACC_PUBLIC)
1990 PHP_ME(HttpMessage, unserialize, ai_HttpMessage_unserialize, ZEND_ACC_PUBLIC)
1991
1992 /* implements Iterator */
1993 PHP_ME(HttpMessage, rewind, ai_HttpMessage_rewind, ZEND_ACC_PUBLIC)
1994 PHP_ME(HttpMessage, valid, ai_HttpMessage_valid, ZEND_ACC_PUBLIC)
1995 PHP_ME(HttpMessage, current, ai_HttpMessage_current, ZEND_ACC_PUBLIC)
1996 PHP_ME(HttpMessage, key, ai_HttpMessage_key, ZEND_ACC_PUBLIC)
1997 PHP_ME(HttpMessage, next, ai_HttpMessage_next, ZEND_ACC_PUBLIC)
1998
1999 ZEND_MALIAS(HttpMessage, __toString, toString, ai_HttpMessage___toString, ZEND_ACC_PUBLIC)
2000
2001 PHP_ME(HttpMessage, detach, ai_HttpMessage_detach, ZEND_ACC_PUBLIC)
2002 PHP_ME(HttpMessage, prepend, ai_HttpMessage_prepend, ZEND_ACC_PUBLIC)
2003 PHP_ME(HttpMessage, reverse, ai_HttpMessage_reverse, ZEND_ACC_PUBLIC)
2004
2005 PHP_ME(HttpMessage, isMultipart, ai_HttpMessage_isMultipart, ZEND_ACC_PUBLIC)
2006 PHP_ME(HttpMessage, splitMultipartBody, ai_HttpMessage_splitMultipartBody, ZEND_ACC_PUBLIC)
2007
2008 EMPTY_FUNCTION_ENTRY
2009 };
2010
2011 zend_class_entry *php_http_message_class_entry;
2012
2013 PHP_MINIT_FUNCTION(http_message)
2014 {
2015 zend_class_entry ce = {0};
2016
2017 INIT_NS_CLASS_ENTRY(ce, "http", "Message", php_http_message_methods);
2018 php_http_message_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
2019 php_http_message_class_entry->create_object = php_http_message_object_new;
2020 memcpy(&php_http_message_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2021 php_http_message_object_handlers.clone_obj = php_http_message_object_clone;
2022 php_http_message_object_handlers.read_property = php_http_message_object_read_prop;
2023 php_http_message_object_handlers.write_property = php_http_message_object_write_prop;
2024 php_http_message_object_handlers.get_properties = php_http_message_object_get_props;
2025 php_http_message_object_handlers.get_property_ptr_ptr = NULL;
2026
2027 zend_class_implements(php_http_message_class_entry TSRMLS_CC, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);
2028
2029 zend_hash_init(&php_http_message_object_prophandlers, 9, NULL, NULL, 1);
2030 zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("type"), PHP_HTTP_NONE, ZEND_ACC_PROTECTED TSRMLS_CC);
2031 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type, php_http_message_object_prophandler_set_type);
2032 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("body"), ZEND_ACC_PROTECTED TSRMLS_CC);
2033 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body, php_http_message_object_prophandler_set_body);
2034 zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
2035 php_http_message_object_add_prophandler(ZEND_STRL("requestMethod"), php_http_message_object_prophandler_get_request_method, php_http_message_object_prophandler_set_request_method);
2036 zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
2037 php_http_message_object_add_prophandler(ZEND_STRL("requestUrl"), php_http_message_object_prophandler_get_request_url, php_http_message_object_prophandler_set_request_url);
2038 zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
2039 php_http_message_object_add_prophandler(ZEND_STRL("responseStatus"), php_http_message_object_prophandler_get_response_status, php_http_message_object_prophandler_set_response_status);
2040 zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED TSRMLS_CC);
2041 php_http_message_object_add_prophandler(ZEND_STRL("responseCode"), php_http_message_object_prophandler_get_response_code, php_http_message_object_prophandler_set_response_code);
2042 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED TSRMLS_CC);
2043 php_http_message_object_add_prophandler(ZEND_STRL("httpVersion"), php_http_message_object_prophandler_get_http_version, php_http_message_object_prophandler_set_http_version);
2044 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("headers"), ZEND_ACC_PROTECTED TSRMLS_CC);
2045 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers, php_http_message_object_prophandler_set_headers);
2046 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED TSRMLS_CC);
2047 php_http_message_object_add_prophandler(ZEND_STRL("parentMessage"), php_http_message_object_prophandler_get_parent_message, php_http_message_object_prophandler_set_parent_message);
2048
2049 zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE TSRMLS_CC);
2050 zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST TSRMLS_CC);
2051 zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE TSRMLS_CC);
2052
2053 return SUCCESS;
2054 }
2055
2056 PHP_MSHUTDOWN_FUNCTION(http_message)
2057 {
2058 zend_hash_destroy(&php_http_message_object_prophandlers);
2059
2060 return SUCCESS;
2061 }
2062
2063 /*
2064 * Local variables:
2065 * tab-width: 4
2066 * c-basic-offset: 4
2067 * End:
2068 * vim600: noet sw=4 ts=4 fdm=marker
2069 * vim<600: noet sw=4 ts=4
2070 */
2071