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