- add HttpDeflateStream and HttpInflateStream objects
[m6w6/ext-http] / http_message_object.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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
19
20 #include "php_http.h"
21
22 #ifdef ZEND_ENGINE_2
23
24 #include "php_http_api.h"
25 #include "php_http_message_api.h"
26 #include "php_http_message_object.h"
27 #include "php_http_exception_object.h"
28
29 #ifndef WONKY
30 # include "zend_interfaces.h"
31 # if defined(HAVE_SPL)
32 /* SPL doesn't install its headers */
33 extern PHPAPI zend_class_entry *spl_ce_Countable;
34 # endif
35 #endif
36
37 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
38 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
39 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
40
41 HTTP_BEGIN_ARGS(__construct, 0, 0)
42 HTTP_ARG_VAL(message, 0)
43 HTTP_END_ARGS;
44
45 HTTP_BEGIN_ARGS(fromString, 1, 1)
46 HTTP_ARG_VAL(message, 0)
47 HTTP_END_ARGS;
48
49 HTTP_EMPTY_ARGS(getBody, 0);
50 HTTP_BEGIN_ARGS(setBody, 0, 1)
51 HTTP_ARG_VAL(body, 0)
52 HTTP_END_ARGS;
53
54 HTTP_EMPTY_ARGS(getHeaders, 0);
55 HTTP_BEGIN_ARGS(setHeaders, 0, 1)
56 HTTP_ARG_VAL(headers, 0)
57 HTTP_END_ARGS;
58
59 HTTP_BEGIN_ARGS(addHeaders, 0, 1)
60 HTTP_ARG_VAL(headers, 0)
61 HTTP_ARG_VAL(append, 0)
62 HTTP_END_ARGS;
63
64 HTTP_EMPTY_ARGS(getType, 0);
65 HTTP_BEGIN_ARGS(setType, 0, 1)
66 HTTP_ARG_VAL(type, 0)
67 HTTP_END_ARGS;
68
69 HTTP_EMPTY_ARGS(getResponseCode, 0);
70 HTTP_BEGIN_ARGS(setResponseCode, 0, 1)
71 HTTP_ARG_VAL(response_code, 0)
72 HTTP_END_ARGS;
73
74 HTTP_EMPTY_ARGS(getRequestMethod, 0);
75 HTTP_BEGIN_ARGS(setRequestMethod, 0, 1)
76 HTTP_ARG_VAL(request_method, 0)
77 HTTP_END_ARGS;
78
79 HTTP_EMPTY_ARGS(getRequestUri, 0);
80 HTTP_BEGIN_ARGS(setRequestUri, 0, 1)
81 HTTP_ARG_VAL(uri, 0)
82 HTTP_END_ARGS;
83
84 HTTP_EMPTY_ARGS(getHttpVersion, 0);
85 HTTP_BEGIN_ARGS(setHttpVersion, 0, 1)
86 HTTP_ARG_VAL(http_version, 0)
87 HTTP_END_ARGS;
88
89 HTTP_EMPTY_ARGS(getParentMessage, 1);
90 HTTP_EMPTY_ARGS(send, 0);
91 HTTP_BEGIN_ARGS(toString, 0, 0)
92 HTTP_ARG_VAL(include_parent, 0)
93 HTTP_END_ARGS;
94
95 HTTP_EMPTY_ARGS(count, 0);
96
97 HTTP_EMPTY_ARGS(serialize, 0);
98 HTTP_BEGIN_ARGS(unserialize, 0, 1)
99 HTTP_ARG_VAL(serialized, 0)
100 HTTP_END_ARGS;
101
102 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
103 static inline void _http_message_object_declare_default_properties(TSRMLS_D);
104 #define http_message_object_read_prop _http_message_object_read_prop
105 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC);
106 #define http_message_object_write_prop _http_message_object_write_prop
107 static void _http_message_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC);
108 #define http_message_object_get_props _http_message_object_get_props
109 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC);
110
111 zend_class_entry *http_message_object_ce;
112 zend_function_entry http_message_object_fe[] = {
113 HTTP_MESSAGE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
114 HTTP_MESSAGE_ME(getBody, ZEND_ACC_PUBLIC)
115 HTTP_MESSAGE_ME(setBody, ZEND_ACC_PUBLIC)
116 HTTP_MESSAGE_ME(getHeaders, ZEND_ACC_PUBLIC)
117 HTTP_MESSAGE_ME(setHeaders, ZEND_ACC_PUBLIC)
118 HTTP_MESSAGE_ME(addHeaders, ZEND_ACC_PUBLIC)
119 HTTP_MESSAGE_ME(getType, ZEND_ACC_PUBLIC)
120 HTTP_MESSAGE_ME(setType, ZEND_ACC_PUBLIC)
121 HTTP_MESSAGE_ME(getResponseCode, ZEND_ACC_PUBLIC)
122 HTTP_MESSAGE_ME(setResponseCode, ZEND_ACC_PUBLIC)
123 HTTP_MESSAGE_ME(getRequestMethod, ZEND_ACC_PUBLIC)
124 HTTP_MESSAGE_ME(setRequestMethod, ZEND_ACC_PUBLIC)
125 HTTP_MESSAGE_ME(getRequestUri, ZEND_ACC_PUBLIC)
126 HTTP_MESSAGE_ME(setRequestUri, ZEND_ACC_PUBLIC)
127 HTTP_MESSAGE_ME(getHttpVersion, ZEND_ACC_PUBLIC)
128 HTTP_MESSAGE_ME(setHttpVersion, ZEND_ACC_PUBLIC)
129 HTTP_MESSAGE_ME(getParentMessage, ZEND_ACC_PUBLIC)
130 HTTP_MESSAGE_ME(send, ZEND_ACC_PUBLIC)
131 HTTP_MESSAGE_ME(toString, ZEND_ACC_PUBLIC)
132
133 /* implements Countable */
134 HTTP_MESSAGE_ME(count, ZEND_ACC_PUBLIC)
135
136 /* implements Serializable */
137 HTTP_MESSAGE_ME(serialize, ZEND_ACC_PUBLIC)
138 HTTP_MESSAGE_ME(unserialize, ZEND_ACC_PUBLIC)
139
140 ZEND_MALIAS(HttpMessage, __toString, toString, HTTP_ARGS(HttpMessage, toString), ZEND_ACC_PUBLIC)
141
142 HTTP_MESSAGE_ME(fromString, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
143
144 EMPTY_FUNCTION_ENTRY
145 };
146 static zend_object_handlers http_message_object_handlers;
147
148 PHP_MINIT_FUNCTION(http_message_object)
149 {
150 HTTP_REGISTER_CLASS_EX(HttpMessage, http_message_object, NULL, 0);
151 #ifndef WONKY
152 # ifdef HAVE_SPL
153 zend_class_implements(http_message_object_ce TSRMLS_CC, 2, spl_ce_Countable, zend_ce_serializable);
154 # else
155 zend_class_implements(http_message_object_ce TSRMLS_CC, 1, zend_ce_serializable);
156 # endif
157 #endif
158
159 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE);
160 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST);
161 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE);
162
163 http_message_object_handlers.clone_obj = _http_message_object_clone_obj;
164 http_message_object_handlers.read_property = http_message_object_read_prop;
165 http_message_object_handlers.write_property = http_message_object_write_prop;
166 http_message_object_handlers.get_properties = http_message_object_get_props;
167 http_message_object_handlers.get_property_ptr_ptr = NULL;
168
169 return SUCCESS;
170 }
171
172 zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC)
173 {
174 return http_message_object_new_ex(ce, NULL, NULL);
175 }
176
177 zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message *msg, http_message_object **ptr TSRMLS_DC)
178 {
179 zend_object_value ov;
180 http_message_object *o;
181
182 o = ecalloc(1, sizeof(http_message_object));
183 o->zo.ce = ce;
184
185 if (ptr) {
186 *ptr = o;
187 }
188
189 if (msg) {
190 o->message = msg;
191 if (msg->parent) {
192 o->parent = http_message_object_new_ex(ce, msg->parent, NULL);
193 }
194 }
195
196 ALLOC_HASHTABLE(OBJ_PROP(o));
197 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
198
199 ov.handle = putObject(http_message_object, o);
200 ov.handlers = &http_message_object_handlers;
201
202 return ov;
203 }
204
205 zend_object_value _http_message_object_clone_obj(zval *this_ptr TSRMLS_DC)
206 {
207 getObject(http_message_object, obj);
208 return http_message_object_new_ex(Z_OBJCE_P(this_ptr), http_message_dup(obj->message), NULL);
209 }
210
211 static inline void _http_message_object_declare_default_properties(TSRMLS_D)
212 {
213 zend_class_entry *ce = http_message_object_ce;
214
215 #ifndef WONKY
216 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE);
217 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST);
218 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE);
219 #endif
220
221 DCL_PROP(PROTECTED, long, type, HTTP_MSG_NONE);
222 DCL_PROP(PROTECTED, string, body, "");
223 DCL_PROP(PROTECTED, string, requestMethod, "");
224 DCL_PROP(PROTECTED, string, requestUri, "");
225 DCL_PROP(PROTECTED, long, responseCode, 0);
226 DCL_PROP_N(PROTECTED, httpVersion);
227 DCL_PROP_N(PROTECTED, headers);
228 DCL_PROP_N(PROTECTED, parentMessage);
229 }
230
231 void _http_message_object_free(zend_object *object TSRMLS_DC)
232 {
233 http_message_object *o = (http_message_object *) object;
234
235 if (OBJ_PROP(o)) {
236 zend_hash_destroy(OBJ_PROP(o));
237 FREE_HASHTABLE(OBJ_PROP(o));
238 }
239 if (o->message) {
240 http_message_dtor(o->message);
241 efree(o->message);
242 }
243 efree(o);
244 }
245
246 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC)
247 {
248 getObjectEx(http_message_object, obj, object);
249 http_message *msg = obj->message;
250 zval *return_value;
251 #ifdef WONKY
252 ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member)+1);
253 #else
254 zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
255
256 if (!pinfo || ACC_PROP_PUBLIC(pinfo->flags)) {
257 return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
258 }
259 #endif
260
261 if (type == BP_VAR_W) {
262 zend_error(E_ERROR, "Cannot access HttpMessage properties by reference or array key/index");
263 return NULL;
264 }
265
266 ALLOC_ZVAL(return_value);
267 return_value->refcount = 0;
268 return_value->is_ref = 0;
269
270 #ifdef WONKY
271 switch (h)
272 #else
273 switch (pinfo->h)
274 #endif
275 {
276 case HTTP_MSG_PROPHASH_TYPE:
277 case HTTP_MSG_CHILD_PROPHASH_TYPE:
278 RETVAL_LONG(msg->type);
279 break;
280
281 case HTTP_MSG_PROPHASH_HTTP_VERSION:
282 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION:
283 RETVAL_DOUBLE(msg->http.version);
284 break;
285
286 case HTTP_MSG_PROPHASH_BODY:
287 case HTTP_MSG_CHILD_PROPHASH_BODY:
288 phpstr_fix(PHPSTR(msg));
289 RETVAL_PHPSTR(PHPSTR(msg), 0, 1);
290 break;
291
292 case HTTP_MSG_PROPHASH_HEADERS:
293 case HTTP_MSG_CHILD_PROPHASH_HEADERS:
294 array_init(return_value);
295 zend_hash_copy(Z_ARRVAL_P(return_value), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
296 break;
297
298 case HTTP_MSG_PROPHASH_PARENT_MESSAGE:
299 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE:
300 if (msg->parent) {
301 RETVAL_OBJVAL(obj->parent);
302 } else {
303 RETVAL_NULL();
304 }
305 break;
306
307 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
308 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD:
309 if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.method) {
310 RETVAL_STRING(msg->http.info.request.method, 1);
311 } else {
312 RETVAL_NULL();
313 }
314 break;
315
316 case HTTP_MSG_PROPHASH_REQUEST_URI:
317 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI:
318 if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.URI) {
319 RETVAL_STRING(msg->http.info.request.URI, 1);
320 } else {
321 RETVAL_NULL();
322 }
323 break;
324
325 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
326 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE:
327 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
328 RETVAL_LONG(msg->http.info.response.code);
329 } else {
330 RETVAL_NULL();
331 }
332 break;
333
334 case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
335 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS:
336 if (HTTP_MSG_TYPE(RESPONSE, msg) && msg->http.info.response.status) {
337 RETVAL_STRING(msg->http.info.response.status, 1);
338 } else {
339 RETVAL_NULL();
340 }
341 break;
342
343 default:
344 #ifdef WONKY
345 return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
346 #else
347 RETVAL_NULL();
348 #endif
349 break;
350 }
351
352 return return_value;
353 }
354
355 static void _http_message_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC)
356 {
357 getObjectEx(http_message_object, obj, object);
358 http_message *msg = obj->message;
359 zval *cpy = NULL;
360 #ifdef WONKY
361 ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
362 #else
363 zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
364
365 if (!pinfo || ACC_PROP_PUBLIC(pinfo->flags)) {
366 zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
367 return;
368 }
369 #endif
370
371 ALLOC_ZVAL(cpy);
372 *cpy = *value;
373 zval_copy_ctor(cpy);
374 INIT_PZVAL(cpy);
375
376 #ifdef WONKY
377 switch (h)
378 #else
379 switch (pinfo->h)
380 #endif
381 {
382 case HTTP_MSG_PROPHASH_TYPE:
383 case HTTP_MSG_CHILD_PROPHASH_TYPE:
384 convert_to_long(cpy);
385 http_message_set_type(msg, Z_LVAL_P(cpy));
386 break;
387
388 case HTTP_MSG_PROPHASH_HTTP_VERSION:
389 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION:
390 convert_to_double(cpy);
391 msg->http.version = Z_DVAL_P(cpy);
392 break;
393
394 case HTTP_MSG_PROPHASH_BODY:
395 case HTTP_MSG_CHILD_PROPHASH_BODY:
396 convert_to_string(cpy);
397 phpstr_dtor(PHPSTR(msg));
398 phpstr_from_string_ex(PHPSTR(msg), Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
399 break;
400
401 case HTTP_MSG_PROPHASH_HEADERS:
402 case HTTP_MSG_CHILD_PROPHASH_HEADERS:
403 convert_to_array(cpy);
404 zend_hash_clean(&msg->hdrs);
405 zend_hash_copy(&msg->hdrs, Z_ARRVAL_P(cpy), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
406 break;
407
408 case HTTP_MSG_PROPHASH_PARENT_MESSAGE:
409 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE:
410 if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), http_message_object_ce TSRMLS_CC)) {
411 if (msg->parent) {
412 zval tmp;
413 tmp.value.obj = obj->parent;
414 Z_OBJ_DELREF(tmp);
415 }
416 Z_OBJ_ADDREF_P(value);
417 obj->parent = value->value.obj;
418 }
419 break;
420
421 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
422 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD:
423 if (HTTP_MSG_TYPE(REQUEST, msg)) {
424 convert_to_string(cpy);
425 STR_SET(msg->http.info.request.method, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
426 }
427 break;
428
429 case HTTP_MSG_PROPHASH_REQUEST_URI:
430 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI:
431 if (HTTP_MSG_TYPE(REQUEST, msg)) {
432 convert_to_string(cpy);
433 STR_SET(msg->http.info.request.URI, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
434 }
435 break;
436
437 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
438 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE:
439 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
440 convert_to_long(cpy);
441 msg->http.info.response.code = Z_LVAL_P(cpy);
442 }
443 break;
444
445 case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
446 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS:
447 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
448 convert_to_string(cpy);
449 STR_SET(msg->http.info.response.status, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
450 }
451 break;
452
453 default:
454 #ifdef WONKY
455 zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
456 #endif
457 break;
458 }
459 zval_ptr_dtor(&cpy);
460 }
461
462 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
463 {
464 zval *headers;
465 getObjectEx(http_message_object, obj, object);
466 http_message *msg = obj->message;
467 HashTable *props = OBJ_PROP(obj);
468 zval array;
469
470 INIT_ZARR(array, props);
471
472 #define ASSOC_PROP(array, ptype, name, val) \
473 { \
474 char *m_prop_name; \
475 int m_prop_len; \
476 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
477 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
478 efree(m_prop_name); \
479 }
480 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
481 #define ASSOC_STRINGL(array, name, val, len) \
482 { \
483 char *m_prop_name; \
484 int m_prop_len; \
485 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
486 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
487 efree(m_prop_name); \
488 }
489
490 ASSOC_PROP(array, long, "type", msg->type);
491 ASSOC_PROP(array, double, "httpVersion", msg->http.version);
492
493 switch (msg->type)
494 {
495 case HTTP_MSG_REQUEST:
496 ASSOC_PROP(array, long, "responseCode", 0);
497 ASSOC_STRINGL(array, "responseStatus", "", 0);
498 ASSOC_STRING(array, "requestMethod", msg->http.info.request.method);
499 ASSOC_STRING(array, "requestUri", msg->http.info.request.URI);
500 break;
501
502 case HTTP_MSG_RESPONSE:
503 ASSOC_PROP(array, long, "responseCode", msg->http.info.response.code);
504 ASSOC_STRING(array, "responseStatus", msg->http.info.response.status);
505 ASSOC_STRINGL(array, "requestMethod", "", 0);
506 ASSOC_STRINGL(array, "requestUri", "", 0);
507 break;
508
509 case HTTP_MSG_NONE:
510 default:
511 ASSOC_PROP(array, long, "responseCode", 0);
512 ASSOC_STRINGL(array, "responseStatus", "", 0);
513 ASSOC_STRINGL(array, "requestMethod", "", 0);
514 ASSOC_STRINGL(array, "requestUri", "", 0);
515 break;
516 }
517
518 MAKE_STD_ZVAL(headers);
519 array_init(headers);
520 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
521 ASSOC_PROP(array, zval, "headers", headers);
522 ASSOC_STRINGL(array, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg));
523
524 return OBJ_PROP(obj);
525 }
526
527 /* ### USERLAND ### */
528
529 /* {{{ proto void HttpMessage::__construct([string message])
530 *
531 * Instantiate a new HttpMessage object.
532 *
533 * Accepts an optional string parameter containing a single or several
534 * consecutive HTTP messages. The constructed object will actually
535 * represent the *last* message of the passed string. If there were
536 * prior messages, those can be accessed by HttpMessage::getParentMessage().
537 *
538 * Throws HttpMalformedHeaderException.
539 */
540 PHP_METHOD(HttpMessage, __construct)
541 {
542 int length = 0;
543 char *message = NULL;
544
545 getObject(http_message_object, obj);
546
547 SET_EH_THROW_HTTP();
548 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
549 http_message *msg = obj->message;
550
551 http_message_dtor(msg);
552 if ((obj->message = http_message_parse_ex(msg, message, length))) {
553 if (obj->message->parent) {
554 obj->parent = http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL);
555 }
556 } else {
557 obj->message = http_message_init(msg);
558 }
559 }
560 if (!obj->message) {
561 obj->message = http_message_new();
562 }
563 SET_EH_NORMAL();
564 }
565 /* }}} */
566
567 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
568 *
569 * Create an HttpMessage object from a string. Kind of a static constructor.
570 *
571 * Expects a string parameter containing a sinlge or several consecutive
572 * HTTP messages.
573 *
574 * Returns an HttpMessage object on success or NULL on failure.
575 *
576 * Throws HttpMalformedHeadersException.
577 */
578 PHP_METHOD(HttpMessage, fromString)
579 {
580 char *string = NULL;
581 int length = 0;
582 http_message *msg = NULL;
583
584 RETVAL_NULL();
585
586 SET_EH_THROW_HTTP();
587 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &length)) {
588 if ((msg = http_message_parse(string, length))) {
589 ZVAL_OBJVAL(return_value, http_message_object_new_ex(http_message_object_ce, msg, NULL));
590 }
591 }
592 SET_EH_NORMAL();
593 }
594 /* }}} */
595
596 /* {{{ proto string HttpMessage::getBody()
597 *
598 * Get the body of the parsed HttpMessage.
599 *
600 * Returns the message body as string.
601 */
602 PHP_METHOD(HttpMessage, getBody)
603 {
604 NO_ARGS;
605
606 IF_RETVAL_USED {
607 getObject(http_message_object, obj);
608 RETURN_PHPSTR(&obj->message->body, PHPSTR_FREE_NOT, 1);
609 }
610 }
611 /* }}} */
612
613 /* {{{ proto void HttpMessage::setBody(string body)
614 *
615 * Set the body of the HttpMessage.
616 * NOTE: Don't forget to update any headers accordingly.
617 *
618 * Expects a string parameter containing the new body of the message.
619 */
620 PHP_METHOD(HttpMessage, setBody)
621 {
622 char *body;
623 int len;
624 getObject(http_message_object, obj);
625
626 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &body, &len)) {
627 phpstr_dtor(PHPSTR(obj->message));
628 phpstr_from_string_ex(PHPSTR(obj->message), body, len);
629 }
630 }
631 /* }}} */
632
633 /* {{{ proto array HttpMessage::getHeaders()
634 *
635 * Get Message Headers.
636 *
637 * Returns an associative array containing the messages HTTP headers.
638 */
639 PHP_METHOD(HttpMessage, getHeaders)
640 {
641 NO_ARGS;
642
643 IF_RETVAL_USED {
644 zval headers;
645 getObject(http_message_object, obj);
646
647 INIT_ZARR(headers, &obj->message->hdrs);
648 array_init(return_value);
649 array_copy(&headers, return_value);
650 }
651 }
652 /* }}} */
653
654 /* {{{ proto void HttpMessage::setHeaders(array headers)
655 *
656 * Sets new headers.
657 *
658 * Expects an associative array as parameter containing the new HTTP headers,
659 * which will replace *all* previous HTTP headers of the message.
660 */
661 PHP_METHOD(HttpMessage, setHeaders)
662 {
663 zval *new_headers, old_headers;
664 getObject(http_message_object, obj);
665
666 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
667 return;
668 }
669
670 zend_hash_clean(&obj->message->hdrs);
671 INIT_ZARR(old_headers, &obj->message->hdrs);
672 array_copy(new_headers, &old_headers);
673 }
674 /* }}} */
675
676 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
677 *
678 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
679 *
680 * Expects an associative array as parameter containing the additional HTTP headers
681 * to add to the messages existing headers. If the optional bool parameter is true,
682 * and a header with the same name of one to add exists already, this respective
683 * header will be converted to an array containing both header values, otherwise
684 * it will be overwritten with the new header value.
685 */
686 PHP_METHOD(HttpMessage, addHeaders)
687 {
688 zval old_headers, *new_headers;
689 zend_bool append = 0;
690 getObject(http_message_object, obj);
691
692 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
693 return;
694 }
695
696 INIT_ZARR(old_headers, &obj->message->hdrs);
697 if (append) {
698 array_append(new_headers, &old_headers);
699 } else {
700 array_merge(new_headers, &old_headers);
701 }
702 }
703 /* }}} */
704
705 /* {{{ proto int HttpMessage::getType()
706 *
707 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
708 *
709 * Returns the HttpMessage::TYPE.
710 */
711 PHP_METHOD(HttpMessage, getType)
712 {
713 NO_ARGS;
714
715 IF_RETVAL_USED {
716 getObject(http_message_object, obj);
717 RETURN_LONG(obj->message->type);
718 }
719 }
720 /* }}} */
721
722 /* {{{ proto void HttpMessage::setType(int type)
723 *
724 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
725 *
726 * Exptects an int parameter, the HttpMessage::TYPE.
727 */
728 PHP_METHOD(HttpMessage, setType)
729 {
730 long type;
731 getObject(http_message_object, obj);
732
733 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
734 return;
735 }
736 http_message_set_type(obj->message, type);
737 }
738 /* }}} */
739
740 /* {{{ proto int HttpMessage::getResponseCode()
741 *
742 * Get the Response Code of the Message.
743 *
744 * Returns the HTTP response code if the message is of type
745 * HttpMessage::TYPE_RESPONSE, else FALSE.
746 */
747 PHP_METHOD(HttpMessage, getResponseCode)
748 {
749 NO_ARGS;
750
751 IF_RETVAL_USED {
752 getObject(http_message_object, obj);
753 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
754 RETURN_LONG(obj->message->http.info.response.code);
755 }
756 }
757 /* }}} */
758
759 /* {{{ proto bool HttpMessage::setResponseCode(int code)
760 *
761 * Set the response code of an HTTP Response Message.
762 *
763 * Expects an int parameter with the HTTP response code.
764 *
765 * Returns TRUE on success, or FALSE if the message is not of type
766 * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
767 */
768 PHP_METHOD(HttpMessage, setResponseCode)
769 {
770 long code;
771 getObject(http_message_object, obj);
772
773 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
774
775 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
776 RETURN_FALSE;
777 }
778 if (code < 100 || code > 510) {
779 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid response code (100-510): %ld", code);
780 RETURN_FALSE;
781 }
782
783 obj->message->http.info.response.code = code;
784 RETURN_TRUE;
785 }
786 /* }}} */
787
788 /* {{{ proto string HttpMessage::getRequestMethod()
789 *
790 * Get the Request Method of the Message.
791 *
792 * Returns the request method name on success, or FALSE if the message is
793 * not of type HttpMessage::TYPE_REQUEST.
794 */
795 PHP_METHOD(HttpMessage, getRequestMethod)
796 {
797 NO_ARGS;
798
799 IF_RETVAL_USED {
800 getObject(http_message_object, obj);
801 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
802 RETURN_STRING(obj->message->http.info.request.method, 1);
803 }
804 }
805 /* }}} */
806
807 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
808 *
809 * Set the Request Method of the HTTP Message.
810 *
811 * Expects a string parameter containing the request method name.
812 *
813 * Returns TRUE on success, or FALSE if the message is not of type
814 * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
815 */
816 PHP_METHOD(HttpMessage, setRequestMethod)
817 {
818 char *method;
819 int method_len;
820 getObject(http_message_object, obj);
821
822 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
823
824 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
825 RETURN_FALSE;
826 }
827 if (method_len < 1) {
828 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestMethod to an empty string");
829 RETURN_FALSE;
830 }
831 if (SUCCESS != http_check_method(method)) {
832 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unkown request method: %s", method);
833 RETURN_FALSE;
834 }
835
836 STR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
837 RETURN_TRUE;
838 }
839 /* }}} */
840
841 /* {{{ proto string HttpMessage::getRequestUri()
842 *
843 * Get the Request URI of the Message.
844 *
845 * Returns the request uri as string on success, or FALSE if the message
846 * is not of type HttpMessage::TYPE_REQUEST.
847 */
848 PHP_METHOD(HttpMessage, getRequestUri)
849 {
850 NO_ARGS;
851
852 IF_RETVAL_USED {
853 getObject(http_message_object, obj);
854 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
855 RETURN_STRING(obj->message->http.info.request.URI, 1);
856 }
857 }
858 /* }}} */
859
860 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
861 *
862 * Set the Request URI of the HTTP Message.
863 *
864 * Expects a string parameters containing the request uri.
865 *
866 * Returns TRUE on success, or FALSE if the message is not of type
867 * HttpMessage::TYPE_REQUEST or supplied URI was empty.
868 */
869 PHP_METHOD(HttpMessage, setRequestUri)
870 {
871 char *URI;
872 int URIlen;
873 getObject(http_message_object, obj);
874
875 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
876 RETURN_FALSE;
877 }
878 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
879 if (URIlen < 1) {
880 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestUri to an empty string");
881 RETURN_FALSE;
882 }
883
884 STR_SET(obj->message->http.info.request.URI, estrndup(URI, URIlen));
885 RETURN_TRUE;
886 }
887 /* }}} */
888
889 /* {{{ proto string HttpMessage::getHttpVersion()
890 *
891 * Get the HTTP Protocol Version of the Message.
892 *
893 * Returns the HTTP protocol version as string.
894 */
895 PHP_METHOD(HttpMessage, getHttpVersion)
896 {
897 NO_ARGS;
898
899 IF_RETVAL_USED {
900 char ver[4] = {0};
901 getObject(http_message_object, obj);
902
903 sprintf(ver, "%1.1lf", obj->message->http.version);
904 RETURN_STRINGL(ver, 3, 1);
905 }
906 }
907 /* }}} */
908
909 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
910 *
911 * Set the HTTP Protocol version of the Message.
912 *
913 * Expects a string parameter containing the HTTP protocol version.
914 *
915 * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
916 */
917 PHP_METHOD(HttpMessage, setHttpVersion)
918 {
919 char v[4];
920 zval *zv;
921 getObject(http_message_object, obj);
922
923 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &zv)) {
924 return;
925 }
926
927 convert_to_double(zv);
928 sprintf(v, "%1.1lf", Z_DVAL_P(zv));
929 if (strcmp(v, "1.0") && strcmp(v, "1.1")) {
930 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %s", v);
931 RETURN_FALSE;
932 }
933
934 obj->message->http.version = Z_DVAL_P(zv);
935 RETURN_TRUE;
936 }
937 /* }}} */
938
939 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
940 *
941 * Get parent Message.
942 *
943 * Returns the parent HttpMessage on success, or NULL if there's none.
944 */
945 PHP_METHOD(HttpMessage, getParentMessage)
946 {
947 NO_ARGS;
948
949 IF_RETVAL_USED {
950 getObject(http_message_object, obj);
951
952 if (obj->message->parent) {
953 RETVAL_OBJVAL(obj->parent);
954 } else {
955 RETVAL_NULL();
956 }
957 }
958 }
959 /* }}} */
960
961 /* {{{ proto bool HttpMessage::send()
962 *
963 * Send the Message according to its type as Response or Request.
964 * This provides limited functionality compared to HttpRequest and HttpResponse.
965 *
966 * Returns TRUE on success, or FALSE on failure.
967 */
968 PHP_METHOD(HttpMessage, send)
969 {
970 getObject(http_message_object, obj);
971
972 NO_ARGS;
973
974 RETURN_SUCCESS(http_message_send(obj->message));
975 }
976 /* }}} */
977
978 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
979 *
980 * Get the string representation of the Message.
981 *
982 * Accepts a bool parameter which specifies whether the returned string
983 * should also contain any parent messages.
984 *
985 * Returns the full message as string.
986 */
987 PHP_METHOD(HttpMessage, toString)
988 {
989 IF_RETVAL_USED {
990 char *string;
991 size_t length;
992 zend_bool include_parent = 0;
993 getObject(http_message_object, obj);
994
995 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
996 RETURN_FALSE;
997 }
998
999 if (include_parent) {
1000 http_message_serialize(obj->message, &string, &length);
1001 } else {
1002 http_message_tostring(obj->message, &string, &length);
1003 }
1004 RETURN_STRINGL(string, length, 0);
1005 }
1006 }
1007 /* }}} */
1008
1009 /* {{{ proto int HttpMessage::count()
1010 *
1011 * Implements Countable.
1012 *
1013 * Returns the number of parent messages + 1.
1014 */
1015 PHP_METHOD(HttpMessage, count)
1016 {
1017 NO_ARGS {
1018 long i;
1019 http_message *msg;
1020 getObject(http_message_object, obj);
1021
1022 for (i = 0, msg = obj->message; msg; msg = msg->parent, ++i);
1023 RETURN_LONG(i);
1024 }
1025 }
1026 /* }}} */
1027
1028 /* {{{ proto string HttpMessage::serialize()
1029 *
1030 * Implements Serializable.
1031 *
1032 * Returns the serialized representation of the HttpMessage.
1033 */
1034 PHP_METHOD(HttpMessage, serialize)
1035 {
1036 NO_ARGS {
1037 char *string;
1038 size_t length;
1039 getObject(http_message_object, obj);
1040
1041 http_message_serialize(obj->message, &string, &length);
1042 RETURN_STRINGL(string, length, 0);
1043 }
1044 }
1045 /* }}} */
1046
1047 /* {{{ proto void HttpMessage::unserialize(string serialized)
1048 *
1049 * Implements Serializable.
1050 *
1051 * Re-constructs the HttpMessage based upon the serialized string.
1052 */
1053 PHP_METHOD(HttpMessage, unserialize)
1054 {
1055 int length;
1056 char *serialized;
1057 getObject(http_message_object, obj);
1058
1059 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &length)) {
1060 http_message_dtor(obj->message);
1061 if (!http_message_parse_ex(obj->message, serialized, (size_t) length)) {
1062 http_error(HE_ERROR, HTTP_E_RUNTIME, "Could not unserialize HttpMessage");
1063 http_message_init(obj->message);
1064 }
1065 }
1066 }
1067 /* }}} */
1068
1069 #endif /* ZEND_ENGINE_2 */
1070
1071 /*
1072 * Local variables:
1073 * tab-width: 4
1074 * c-basic-offset: 4
1075 * End:
1076 * vim600: noet sw=4 ts=4 fdm=marker
1077 * vim<600: noet sw=4 ts=4
1078 */
1079