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