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