- attempt to fix gcc-2.95 build by using less HTTP_HAVE_$EXT magic
[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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #define HTTP_WANT_MAGIC
18 #include "php_http.h"
19
20 #ifdef ZEND_ENGINE_2
21
22 #include "zend_interfaces.h"
23 #include "ext/standard/url.h"
24
25 #include "php_http_api.h"
26 #include "php_http_send_api.h"
27 #include "php_http_url_api.h"
28 #include "php_http_message_api.h"
29 #include "php_http_message_object.h"
30 #include "php_http_exception_object.h"
31 #include "php_http_response_object.h"
32 #include "php_http_request_method_api.h"
33 #include "php_http_request_api.h"
34 #include "php_http_request_object.h"
35
36 #if defined(HTTP_HAVE_SPL) && !defined(WONKY)
37 /* SPL doesn't install its headers */
38 extern PHPAPI zend_class_entry *spl_ce_Countable;
39 #endif
40
41 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
42 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
43 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
44
45 HTTP_BEGIN_ARGS(__construct, 0)
46 HTTP_ARG_VAL(message, 0)
47 HTTP_END_ARGS;
48
49 HTTP_BEGIN_ARGS(fromString, 1)
50 HTTP_ARG_VAL(message, 0)
51 HTTP_END_ARGS;
52
53 HTTP_EMPTY_ARGS(getBody);
54 HTTP_BEGIN_ARGS(setBody, 1)
55 HTTP_ARG_VAL(body, 0)
56 HTTP_END_ARGS;
57
58 HTTP_EMPTY_ARGS(getHeaders);
59 HTTP_BEGIN_ARGS(setHeaders, 1)
60 HTTP_ARG_VAL(headers, 0)
61 HTTP_END_ARGS;
62
63 HTTP_BEGIN_ARGS(addHeaders, 1)
64 HTTP_ARG_VAL(headers, 0)
65 HTTP_ARG_VAL(append, 0)
66 HTTP_END_ARGS;
67
68 HTTP_EMPTY_ARGS(getType);
69 HTTP_BEGIN_ARGS(setType, 1)
70 HTTP_ARG_VAL(type, 0)
71 HTTP_END_ARGS;
72
73 HTTP_EMPTY_ARGS(getResponseCode);
74 HTTP_BEGIN_ARGS(setResponseCode, 1)
75 HTTP_ARG_VAL(response_code, 0)
76 HTTP_END_ARGS;
77
78 HTTP_EMPTY_ARGS(getResponseStatus);
79 HTTP_BEGIN_ARGS(setResponseStatus, 1)
80 HTTP_ARG_VAL(response_status, 0)
81 HTTP_END_ARGS;
82
83 HTTP_EMPTY_ARGS(getRequestMethod);
84 HTTP_BEGIN_ARGS(setRequestMethod, 1)
85 HTTP_ARG_VAL(request_method, 0)
86 HTTP_END_ARGS;
87
88 HTTP_EMPTY_ARGS(getRequestUrl);
89 HTTP_BEGIN_ARGS(setRequestUrl, 1)
90 HTTP_ARG_VAL(url, 0)
91 HTTP_END_ARGS;
92
93 HTTP_EMPTY_ARGS(getHttpVersion);
94 HTTP_BEGIN_ARGS(setHttpVersion, 1)
95 HTTP_ARG_VAL(http_version, 0)
96 HTTP_END_ARGS;
97
98 HTTP_BEGIN_ARGS(guessContentType, 1)
99 HTTP_ARG_VAL(magic_file, 0)
100 HTTP_ARG_VAL(magic_mode, 0)
101 HTTP_END_ARGS;
102
103 HTTP_EMPTY_ARGS(getParentMessage);
104 HTTP_EMPTY_ARGS(send);
105 HTTP_BEGIN_ARGS(toString, 0)
106 HTTP_ARG_VAL(include_parent, 0)
107 HTTP_END_ARGS;
108
109 HTTP_EMPTY_ARGS(toMessageTypeObject);
110
111 HTTP_EMPTY_ARGS(count);
112
113 HTTP_EMPTY_ARGS(serialize);
114 HTTP_BEGIN_ARGS(unserialize, 1)
115 HTTP_ARG_VAL(serialized, 0)
116 HTTP_END_ARGS;
117
118 HTTP_EMPTY_ARGS(rewind);
119 HTTP_EMPTY_ARGS(valid);
120 HTTP_EMPTY_ARGS(key);
121 HTTP_EMPTY_ARGS(current);
122 HTTP_EMPTY_ARGS(next);
123
124 HTTP_EMPTY_ARGS(detach);
125 HTTP_BEGIN_ARGS(prepend, 1)
126 HTTP_ARG_OBJ(HttpMessage, message, 0)
127 HTTP_END_ARGS;
128 HTTP_EMPTY_ARGS(reverse);
129
130 #define http_message_object_read_prop _http_message_object_read_prop
131 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC);
132 #define http_message_object_write_prop _http_message_object_write_prop
133 static void _http_message_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC);
134 #define http_message_object_get_props _http_message_object_get_props
135 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC);
136
137 #define OBJ_PROP_CE http_message_object_ce
138 zend_class_entry *http_message_object_ce;
139 zend_function_entry http_message_object_fe[] = {
140 HTTP_MESSAGE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
141 HTTP_MESSAGE_ME(getBody, ZEND_ACC_PUBLIC)
142 HTTP_MESSAGE_ME(setBody, ZEND_ACC_PUBLIC)
143 HTTP_MESSAGE_ME(getHeaders, ZEND_ACC_PUBLIC)
144 HTTP_MESSAGE_ME(setHeaders, ZEND_ACC_PUBLIC)
145 HTTP_MESSAGE_ME(addHeaders, ZEND_ACC_PUBLIC)
146 HTTP_MESSAGE_ME(getType, ZEND_ACC_PUBLIC)
147 HTTP_MESSAGE_ME(setType, ZEND_ACC_PUBLIC)
148 HTTP_MESSAGE_ME(getResponseCode, ZEND_ACC_PUBLIC)
149 HTTP_MESSAGE_ME(setResponseCode, ZEND_ACC_PUBLIC)
150 HTTP_MESSAGE_ME(getResponseStatus, ZEND_ACC_PUBLIC)
151 HTTP_MESSAGE_ME(setResponseStatus, ZEND_ACC_PUBLIC)
152 HTTP_MESSAGE_ME(getRequestMethod, ZEND_ACC_PUBLIC)
153 HTTP_MESSAGE_ME(setRequestMethod, ZEND_ACC_PUBLIC)
154 HTTP_MESSAGE_ME(getRequestUrl, ZEND_ACC_PUBLIC)
155 HTTP_MESSAGE_ME(setRequestUrl, ZEND_ACC_PUBLIC)
156 HTTP_MESSAGE_ME(getHttpVersion, ZEND_ACC_PUBLIC)
157 HTTP_MESSAGE_ME(setHttpVersion, ZEND_ACC_PUBLIC)
158 HTTP_MESSAGE_ME(guessContentType, ZEND_ACC_PUBLIC)
159 HTTP_MESSAGE_ME(getParentMessage, ZEND_ACC_PUBLIC)
160 HTTP_MESSAGE_ME(send, ZEND_ACC_PUBLIC)
161 HTTP_MESSAGE_ME(toString, ZEND_ACC_PUBLIC)
162 HTTP_MESSAGE_ME(toMessageTypeObject, ZEND_ACC_PUBLIC)
163
164 /* implements Countable */
165 HTTP_MESSAGE_ME(count, ZEND_ACC_PUBLIC)
166
167 /* implements Serializable */
168 HTTP_MESSAGE_ME(serialize, ZEND_ACC_PUBLIC)
169 HTTP_MESSAGE_ME(unserialize, ZEND_ACC_PUBLIC)
170
171 /* implements Iterator */
172 HTTP_MESSAGE_ME(rewind, ZEND_ACC_PUBLIC)
173 HTTP_MESSAGE_ME(valid, ZEND_ACC_PUBLIC)
174 HTTP_MESSAGE_ME(current, ZEND_ACC_PUBLIC)
175 HTTP_MESSAGE_ME(key, ZEND_ACC_PUBLIC)
176 HTTP_MESSAGE_ME(next, ZEND_ACC_PUBLIC)
177
178 ZEND_MALIAS(HttpMessage, __toString, toString, HTTP_ARGS(HttpMessage, toString), ZEND_ACC_PUBLIC)
179
180 HTTP_MESSAGE_ME(fromString, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
181
182 HTTP_MESSAGE_ME(detach, ZEND_ACC_PUBLIC)
183 HTTP_MESSAGE_ME(prepend, ZEND_ACC_PUBLIC)
184 HTTP_MESSAGE_ME(reverse, ZEND_ACC_PUBLIC)
185
186 EMPTY_FUNCTION_ENTRY
187 };
188 static zend_object_handlers http_message_object_handlers;
189
190 PHP_MINIT_FUNCTION(http_message_object)
191 {
192 HTTP_REGISTER_CLASS_EX(HttpMessage, http_message_object, NULL, 0);
193
194 #ifndef WONKY
195 # ifdef HTTP_HAVE_SPL
196 zend_class_implements(http_message_object_ce TSRMLS_CC, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);
197 # else
198 zend_class_implements(http_message_object_ce TSRMLS_CC, 2, zend_ce_serializable, zend_ce_iterator);
199 # endif
200 #else
201 zend_class_implements(http_message_object_ce TSRMLS_CC, 1, zend_ce_iterator);
202 #endif
203
204 http_message_object_handlers.clone_obj = _http_message_object_clone_obj;
205 http_message_object_handlers.read_property = http_message_object_read_prop;
206 http_message_object_handlers.write_property = http_message_object_write_prop;
207 http_message_object_handlers.get_properties = http_message_object_get_props;
208 http_message_object_handlers.get_property_ptr_ptr = NULL;
209
210 DCL_PROP(PROTECTED, long, type, HTTP_MSG_NONE);
211 DCL_PROP(PROTECTED, string, body, "");
212 DCL_PROP(PROTECTED, string, requestMethod, "");
213 DCL_PROP(PROTECTED, string, requestUrl, "");
214 DCL_PROP(PROTECTED, string, responseStatus, "");
215 DCL_PROP(PROTECTED, long, responseCode, 0);
216 DCL_PROP_N(PROTECTED, httpVersion);
217 DCL_PROP_N(PROTECTED, headers);
218 DCL_PROP_N(PROTECTED, parentMessage);
219
220 #ifndef WONKY
221 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE);
222 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST);
223 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE);
224 #endif
225
226 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE);
227 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST);
228 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE);
229
230 return SUCCESS;
231 }
232
233 void _http_message_object_reverse(zval *this_ptr, zval *return_value TSRMLS_DC)
234 {
235 int i;
236 getObject(http_message_object, obj);
237
238 /* count */
239 http_message_count(i, obj->message);
240
241 if (i > 1) {
242 zval o;
243 zend_object_value *ovalues = NULL;
244 http_message_object **objects = NULL;
245 int last = i - 1;
246
247 objects = ecalloc(i, sizeof(http_message_object *));
248 ovalues = ecalloc(i, sizeof(zend_object_value));
249
250 /* we are the first message */
251 objects[0] = obj;
252 ovalues[0] = getThis()->value.obj;
253
254 /* fetch parents */
255 INIT_PZVAL(&o);
256 o.type = IS_OBJECT;
257 for (i = 1; obj->parent.handle; ++i) {
258 o.value.obj = obj->parent;
259 ovalues[i] = o.value.obj;
260 objects[i] = obj = zend_object_store_get_object(&o TSRMLS_CC);
261 }
262
263 /* reorder parents */
264 for (last = --i; i; --i) {
265 objects[i]->message->parent = objects[i-1]->message;
266 objects[i]->parent = ovalues[i-1];
267 }
268 objects[0]->message->parent = NULL;
269 objects[0]->parent.handle = 0;
270 objects[0]->parent.handlers = NULL;
271
272 /* add ref (why?) */
273 Z_OBJ_ADDREF_P(getThis());
274 RETVAL_OBJVAL(ovalues[last], 1);
275
276 efree(objects);
277 efree(ovalues);
278 } else {
279 RETURN_ZVAL(getThis(), 1, 0);
280 }
281 }
282
283 void _http_message_object_prepend_ex(zval *this_ptr, zval *prepend, zend_bool top TSRMLS_DC)
284 {
285 zval m;
286 http_message *save_parent_msg = NULL;
287 zend_object_value save_parent_obj = {0, NULL};
288 getObject(http_message_object, obj);
289 getObjectEx(http_message_object, prepend_obj, prepend);
290
291 INIT_PZVAL(&m);
292 m.type = IS_OBJECT;
293
294 if (!top) {
295 save_parent_obj = obj->parent;
296 save_parent_msg = obj->message->parent;
297 } else {
298 /* iterate to the most parent object */
299 while (obj->parent.handle) {
300 m.value.obj = obj->parent;
301 obj = zend_object_store_get_object(&m TSRMLS_CC);
302 }
303 }
304
305 /* prepend */
306 obj->parent = prepend->value.obj;
307 obj->message->parent = prepend_obj->message;
308
309 /* add ref */
310 zend_objects_store_add_ref(prepend TSRMLS_CC);
311 while (prepend_obj->parent.handle) {
312 m.value.obj = prepend_obj->parent;
313 zend_objects_store_add_ref(&m TSRMLS_CC);
314 prepend_obj = zend_object_store_get_object(&m TSRMLS_CC);
315 }
316
317 if (!top) {
318 prepend_obj->parent = save_parent_obj;
319 prepend_obj->message->parent = save_parent_msg;
320 }
321 }
322
323 zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC)
324 {
325 return http_message_object_new_ex(ce, NULL, NULL);
326 }
327
328 zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message *msg, http_message_object **ptr TSRMLS_DC)
329 {
330 zend_object_value ov;
331 http_message_object *o;
332
333 o = ecalloc(1, sizeof(http_message_object));
334 o->zo.ce = ce;
335
336 if (ptr) {
337 *ptr = o;
338 }
339
340 if (msg) {
341 o->message = msg;
342 if (msg->parent) {
343 o->parent = http_message_object_new_ex(ce, msg->parent, NULL);
344 }
345 }
346
347 ALLOC_HASHTABLE(OBJ_PROP(o));
348 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
349
350 ov.handle = putObject(http_message_object, o);
351 ov.handlers = &http_message_object_handlers;
352
353 return ov;
354 }
355
356 zend_object_value _http_message_object_clone_obj(zval *this_ptr TSRMLS_DC)
357 {
358 getObject(http_message_object, obj);
359 return http_message_object_new_ex(Z_OBJCE_P(this_ptr), http_message_dup(obj->message), NULL);
360 }
361
362 void _http_message_object_free(zend_object *object TSRMLS_DC)
363 {
364 http_message_object *o = (http_message_object *) object;
365
366 if (OBJ_PROP(o)) {
367 zend_hash_destroy(OBJ_PROP(o));
368 FREE_HASHTABLE(OBJ_PROP(o));
369 }
370 if (o->message) {
371 http_message_dtor(o->message);
372 efree(o->message);
373 }
374 if (o->parent.handle) {
375 zval p;
376
377 INIT_PZVAL(&p);
378 p.type = IS_OBJECT;
379 p.value.obj = o->parent;
380 zend_objects_store_del_ref(&p TSRMLS_CC);
381 }
382 efree(o);
383 }
384
385 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC)
386 {
387 getObjectEx(http_message_object, obj, object);
388 http_message *msg = obj->message;
389 zval *return_value;
390 #ifdef WONKY
391 ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member)+1);
392 #else
393 zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
394
395 if (!pinfo || ACC_PROP_PUBLIC(pinfo->flags)) {
396 return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
397 }
398 #endif
399
400 if (type == BP_VAR_W) {
401 zend_error(E_ERROR, "Cannot access HttpMessage properties by reference or array key/index");
402 return NULL;
403 }
404
405 ALLOC_ZVAL(return_value);
406 return_value->refcount = 0;
407 return_value->is_ref = 0;
408
409 #ifdef WONKY
410 switch (h)
411 #else
412 switch (pinfo->h)
413 #endif
414 {
415 case HTTP_MSG_PROPHASH_TYPE:
416 case HTTP_MSG_CHILD_PROPHASH_TYPE:
417 RETVAL_LONG(msg->type);
418 break;
419
420 case HTTP_MSG_PROPHASH_HTTP_VERSION:
421 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION:
422 RETVAL_DOUBLE(msg->http.version);
423 break;
424
425 case HTTP_MSG_PROPHASH_BODY:
426 case HTTP_MSG_CHILD_PROPHASH_BODY:
427 phpstr_fix(PHPSTR(msg));
428 RETVAL_PHPSTR(PHPSTR(msg), 0, 1);
429 break;
430
431 case HTTP_MSG_PROPHASH_HEADERS:
432 case HTTP_MSG_CHILD_PROPHASH_HEADERS:
433 array_init(return_value);
434 zend_hash_copy(Z_ARRVAL_P(return_value), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
435 break;
436
437 case HTTP_MSG_PROPHASH_PARENT_MESSAGE:
438 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE:
439 if (msg->parent) {
440 RETVAL_OBJVAL(obj->parent, 1);
441 } else {
442 RETVAL_NULL();
443 }
444 break;
445
446 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
447 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD:
448 if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.method) {
449 RETVAL_STRING(msg->http.info.request.method, 1);
450 } else {
451 RETVAL_NULL();
452 }
453 break;
454
455 case HTTP_MSG_PROPHASH_REQUEST_URL:
456 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL:
457 if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.url) {
458 RETVAL_STRING(msg->http.info.request.url, 1);
459 } else {
460 RETVAL_NULL();
461 }
462 break;
463
464 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
465 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE:
466 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
467 RETVAL_LONG(msg->http.info.response.code);
468 } else {
469 RETVAL_NULL();
470 }
471 break;
472
473 case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
474 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS:
475 if (HTTP_MSG_TYPE(RESPONSE, msg) && msg->http.info.response.status) {
476 RETVAL_STRING(msg->http.info.response.status, 1);
477 } else {
478 RETVAL_NULL();
479 }
480 break;
481
482 default:
483 #ifdef WONKY
484 return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
485 #else
486 RETVAL_NULL();
487 #endif
488 }
489
490 return return_value;
491 }
492
493 static void _http_message_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC)
494 {
495 getObjectEx(http_message_object, obj, object);
496 http_message *msg = obj->message;
497 zval *cpy = NULL;
498 #ifdef WONKY
499 ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
500 #else
501 zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
502
503 if (!pinfo || ACC_PROP_PUBLIC(pinfo->flags)) {
504 zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
505 return;
506 }
507 #endif
508
509 cpy = zval_copy(Z_TYPE_P(value), value);
510
511 #ifdef WONKY
512 switch (h)
513 #else
514 switch (pinfo->h)
515 #endif
516 {
517 case HTTP_MSG_PROPHASH_TYPE:
518 case HTTP_MSG_CHILD_PROPHASH_TYPE:
519 convert_to_long(cpy);
520 http_message_set_type(msg, Z_LVAL_P(cpy));
521 break;
522
523 case HTTP_MSG_PROPHASH_HTTP_VERSION:
524 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION:
525 convert_to_double(cpy);
526 msg->http.version = Z_DVAL_P(cpy);
527 break;
528
529 case HTTP_MSG_PROPHASH_BODY:
530 case HTTP_MSG_CHILD_PROPHASH_BODY:
531 convert_to_string(cpy);
532 phpstr_dtor(PHPSTR(msg));
533 phpstr_from_string_ex(PHPSTR(msg), Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
534 break;
535
536 case HTTP_MSG_PROPHASH_HEADERS:
537 case HTTP_MSG_CHILD_PROPHASH_HEADERS:
538 convert_to_array(cpy);
539 zend_hash_clean(&msg->hdrs);
540 zend_hash_copy(&msg->hdrs, Z_ARRVAL_P(cpy), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
541 break;
542
543 case HTTP_MSG_PROPHASH_PARENT_MESSAGE:
544 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE:
545 if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), http_message_object_ce TSRMLS_CC)) {
546 if (msg->parent) {
547 zval tmp;
548 tmp.value.obj = obj->parent;
549 Z_OBJ_DELREF(tmp);
550 }
551 Z_OBJ_ADDREF_P(value);
552 obj->parent = value->value.obj;
553 }
554 break;
555
556 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
557 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD:
558 if (HTTP_MSG_TYPE(REQUEST, msg)) {
559 convert_to_string(cpy);
560 STR_SET(msg->http.info.request.method, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
561 }
562 break;
563
564 case HTTP_MSG_PROPHASH_REQUEST_URL:
565 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL:
566 if (HTTP_MSG_TYPE(REQUEST, msg)) {
567 convert_to_string(cpy);
568 STR_SET(msg->http.info.request.url, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
569 }
570 break;
571
572 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
573 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE:
574 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
575 convert_to_long(cpy);
576 msg->http.info.response.code = Z_LVAL_P(cpy);
577 }
578 break;
579
580 case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
581 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS:
582 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
583 convert_to_string(cpy);
584 STR_SET(msg->http.info.response.status, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
585 }
586 break;
587
588 default:
589 #ifdef WONKY
590 zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
591 #endif
592 break;
593 }
594 zval_free(&cpy);
595 }
596
597 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
598 {
599 zval *headers;
600 getObjectEx(http_message_object, obj, object);
601 http_message *msg = obj->message;
602 HashTable *props = OBJ_PROP(obj);
603 zval array, *parent;
604
605 INIT_ZARR(array, props);
606
607 #define ASSOC_PROP(array, ptype, name, val) \
608 { \
609 char *m_prop_name; \
610 int m_prop_len; \
611 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
612 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
613 efree(m_prop_name); \
614 }
615 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
616 #define ASSOC_STRINGL(array, name, val, len) \
617 { \
618 char *m_prop_name; \
619 int m_prop_len; \
620 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
621 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
622 efree(m_prop_name); \
623 }
624
625 ASSOC_PROP(array, long, "type", msg->type);
626 ASSOC_PROP(array, double, "httpVersion", msg->http.version);
627
628 switch (msg->type) {
629 case HTTP_MSG_REQUEST:
630 ASSOC_PROP(array, long, "responseCode", 0);
631 ASSOC_STRINGL(array, "responseStatus", "", 0);
632 ASSOC_STRING(array, "requestMethod", msg->http.info.request.method);
633 ASSOC_STRING(array, "requestUrl", msg->http.info.request.url);
634 break;
635
636 case HTTP_MSG_RESPONSE:
637 ASSOC_PROP(array, long, "responseCode", msg->http.info.response.code);
638 ASSOC_STRING(array, "responseStatus", msg->http.info.response.status);
639 ASSOC_STRINGL(array, "requestMethod", "", 0);
640 ASSOC_STRINGL(array, "requestUrl", "", 0);
641 break;
642
643 case HTTP_MSG_NONE:
644 default:
645 ASSOC_PROP(array, long, "responseCode", 0);
646 ASSOC_STRINGL(array, "responseStatus", "", 0);
647 ASSOC_STRINGL(array, "requestMethod", "", 0);
648 ASSOC_STRINGL(array, "requestUrl", "", 0);
649 break;
650 }
651
652 MAKE_STD_ZVAL(headers);
653 array_init(headers);
654 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
655 ASSOC_PROP(array, zval, "headers", headers);
656 ASSOC_STRINGL(array, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg));
657
658 MAKE_STD_ZVAL(parent);
659 if (msg->parent) {
660 ZVAL_OBJVAL(parent, obj->parent, 1);
661 } else {
662 ZVAL_NULL(parent);
663 }
664 ASSOC_PROP(array, zval, "parentMessage", parent);
665
666 return OBJ_PROP(obj);
667 }
668
669 /* ### USERLAND ### */
670
671 /* {{{ proto void HttpMessage::__construct([string message])
672 *
673 * Instantiate a new HttpMessage object.
674 *
675 * Accepts an optional string parameter containing a single or several
676 * consecutive HTTP messages. The constructed object will actually
677 * represent the *last* message of the passed string. If there were
678 * prior messages, those can be accessed by HttpMessage::getParentMessage().
679 *
680 * Throws HttpMalformedHeaderException.
681 */
682 PHP_METHOD(HttpMessage, __construct)
683 {
684 int length = 0;
685 char *message = NULL;
686
687 getObject(http_message_object, obj);
688
689 SET_EH_THROW_HTTP();
690 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
691 http_message *msg = obj->message;
692
693 http_message_dtor(msg);
694 if ((obj->message = http_message_parse_ex(msg, message, length))) {
695 if (obj->message->parent) {
696 obj->parent = http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL);
697 }
698 } else {
699 obj->message = http_message_init(msg);
700 }
701 }
702 if (!obj->message) {
703 obj->message = http_message_new();
704 }
705 SET_EH_NORMAL();
706 }
707 /* }}} */
708
709 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message[, string class_name = "HttpMessage"])
710 *
711 * Create an HttpMessage object from a string. Kind of a static constructor.
712 *
713 * Expects a string parameter containing a single or several consecutive
714 * HTTP messages. Accepts an optional string parameter specifying the class to use.
715 *
716 * Returns an HttpMessage object on success or NULL on failure.
717 *
718 * Throws HttpMalformedHeadersException.
719 */
720 PHP_METHOD(HttpMessage, fromString)
721 {
722 char *string = NULL, *class_name = NULL;
723 int length = 0, class_length = 0;
724 http_message *msg = NULL;
725
726 RETVAL_NULL();
727
728 SET_EH_THROW_HTTP();
729 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &string, &length, &class_name, &class_length)) {
730 if ((msg = http_message_parse(string, length))) {
731 zend_class_entry *ce = http_message_object_ce;
732
733 if (class_name && *class_name) {
734 ce = zend_fetch_class(class_name, class_length, ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC);
735 if (ce && !instanceof_function(ce, http_message_object_ce TSRMLS_CC)) {
736 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Class %s does not extend HttpMessage", class_name);
737 ce = NULL;
738 }
739 }
740 if (ce) {
741 RETVAL_OBJVAL(http_message_object_new_ex(ce, msg, NULL), 0);
742 }
743 }
744 }
745 SET_EH_NORMAL();
746 }
747 /* }}} */
748
749 /* {{{ proto string HttpMessage::getBody()
750 *
751 * Get the body of the parsed HttpMessage.
752 *
753 * Returns the message body as string.
754 */
755 PHP_METHOD(HttpMessage, getBody)
756 {
757 NO_ARGS;
758
759 if (return_value_used) {
760 getObject(http_message_object, obj);
761 RETURN_PHPSTR(&obj->message->body, PHPSTR_FREE_NOT, 1);
762 }
763 }
764 /* }}} */
765
766 /* {{{ proto void HttpMessage::setBody(string body)
767 *
768 * Set the body of the HttpMessage.
769 * NOTE: Don't forget to update any headers accordingly.
770 *
771 * Expects a string parameter containing the new body of the message.
772 */
773 PHP_METHOD(HttpMessage, setBody)
774 {
775 char *body;
776 int len;
777 getObject(http_message_object, obj);
778
779 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &body, &len)) {
780 phpstr_dtor(PHPSTR(obj->message));
781 phpstr_from_string_ex(PHPSTR(obj->message), body, len);
782 }
783 }
784 /* }}} */
785
786 /* {{{ proto array HttpMessage::getHeaders()
787 *
788 * Get Message Headers.
789 *
790 * Returns an associative array containing the messages HTTP headers.
791 */
792 PHP_METHOD(HttpMessage, getHeaders)
793 {
794 NO_ARGS;
795
796 if (return_value_used) {
797 zval headers;
798 getObject(http_message_object, obj);
799
800 INIT_ZARR(headers, &obj->message->hdrs);
801 array_init(return_value);
802 array_copy(&headers, return_value);
803 }
804 }
805 /* }}} */
806
807 /* {{{ proto void HttpMessage::setHeaders(array headers)
808 *
809 * Sets new headers.
810 *
811 * Expects an associative array as parameter containing the new HTTP headers,
812 * which will replace *all* previous HTTP headers of the message.
813 */
814 PHP_METHOD(HttpMessage, setHeaders)
815 {
816 zval *new_headers, old_headers;
817 getObject(http_message_object, obj);
818
819 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
820 return;
821 }
822
823 zend_hash_clean(&obj->message->hdrs);
824 INIT_ZARR(old_headers, &obj->message->hdrs);
825 array_copy(new_headers, &old_headers);
826 }
827 /* }}} */
828
829 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
830 *
831 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
832 *
833 * Expects an associative array as parameter containing the additional HTTP headers
834 * to add to the messages existing headers. If the optional bool parameter is true,
835 * and a header with the same name of one to add exists already, this respective
836 * header will be converted to an array containing both header values, otherwise
837 * it will be overwritten with the new header value.
838 */
839 PHP_METHOD(HttpMessage, addHeaders)
840 {
841 zval old_headers, *new_headers;
842 zend_bool append = 0;
843 getObject(http_message_object, obj);
844
845 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
846 return;
847 }
848
849 INIT_ZARR(old_headers, &obj->message->hdrs);
850 if (append) {
851 array_append(new_headers, &old_headers);
852 } else {
853 array_merge(new_headers, &old_headers);
854 }
855 }
856 /* }}} */
857
858 /* {{{ proto int HttpMessage::getType()
859 *
860 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
861 *
862 * Returns the HttpMessage::TYPE.
863 */
864 PHP_METHOD(HttpMessage, getType)
865 {
866 NO_ARGS;
867
868 if (return_value_used) {
869 getObject(http_message_object, obj);
870 RETURN_LONG(obj->message->type);
871 }
872 }
873 /* }}} */
874
875 /* {{{ proto void HttpMessage::setType(int type)
876 *
877 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
878 *
879 * Expects an int parameter, the HttpMessage::TYPE.
880 */
881 PHP_METHOD(HttpMessage, setType)
882 {
883 long type;
884 getObject(http_message_object, obj);
885
886 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
887 return;
888 }
889 http_message_set_type(obj->message, type);
890 }
891 /* }}} */
892
893 /* {{{ proto int HttpMessage::getResponseCode()
894 *
895 * Get the Response Code of the Message.
896 *
897 * Returns the HTTP response code if the message is of type
898 * HttpMessage::TYPE_RESPONSE, else FALSE.
899 */
900 PHP_METHOD(HttpMessage, getResponseCode)
901 {
902 NO_ARGS;
903
904 if (return_value_used) {
905 getObject(http_message_object, obj);
906 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
907 RETURN_LONG(obj->message->http.info.response.code);
908 }
909 }
910 /* }}} */
911
912 /* {{{ proto bool HttpMessage::setResponseCode(int code)
913 *
914 * Set the response code of an HTTP Response Message.
915 *
916 * Expects an int parameter with the HTTP response code.
917 *
918 * Returns TRUE on success, or FALSE if the message is not of type
919 * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
920 */
921 PHP_METHOD(HttpMessage, setResponseCode)
922 {
923 long code;
924 getObject(http_message_object, obj);
925
926 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
927
928 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
929 RETURN_FALSE;
930 }
931 if (code < 100 || code > 510) {
932 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid response code (100-510): %ld", code);
933 RETURN_FALSE;
934 }
935
936 obj->message->http.info.response.code = code;
937 RETURN_TRUE;
938 }
939 /* }}} */
940
941 /* {{{ proto string HttpMessage::getResponseStatus()
942 *
943 * Get the Response Status of the message (i.e. the string following the response code).
944 *
945 * Returns the HTTP response status string if the message is of type
946 * HttpMessage::TYPE_RESPONSE, else FALSE.
947 */
948 PHP_METHOD(HttpMessage, getResponseStatus)
949 {
950 NO_ARGS;
951
952 if (return_value_used) {
953 getObject(http_message_object, obj);
954 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
955 RETURN_STRING(obj->message->http.info.response.status, 1);
956 }
957 }
958 /* }}} */
959
960 /* {{{ proto bool HttpMessage::setResponseStatus(string status)
961 *
962 * Set the Response Status of the HTTP message (i.e. the string following the response code).
963 *
964 * Expects a string parameter containing the response status text.
965 *
966 * Returns TRUE on success or FALSE if the message is not of type
967 * HttpMessage::TYPE_RESPONSE.
968 */
969 PHP_METHOD(HttpMessage, setResponseStatus)
970 {
971 char *status;
972 int status_len;
973 getObject(http_message_object, obj);
974
975 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
976
977 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len)) {
978 RETURN_FALSE;
979 }
980 STR_SET(obj->message->http.info.response.status, estrdup(status));
981 RETURN_TRUE;
982 }
983 /* }}} */
984
985 /* {{{ proto string HttpMessage::getRequestMethod()
986 *
987 * Get the Request Method of the Message.
988 *
989 * Returns the request method name on success, or FALSE if the message is
990 * not of type HttpMessage::TYPE_REQUEST.
991 */
992 PHP_METHOD(HttpMessage, getRequestMethod)
993 {
994 NO_ARGS;
995
996 if (return_value_used) {
997 getObject(http_message_object, obj);
998 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
999 RETURN_STRING(obj->message->http.info.request.method, 1);
1000 }
1001 }
1002 /* }}} */
1003
1004 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
1005 *
1006 * Set the Request Method of the HTTP Message.
1007 *
1008 * Expects a string parameter containing the request method name.
1009 *
1010 * Returns TRUE on success, or FALSE if the message is not of type
1011 * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
1012 */
1013 PHP_METHOD(HttpMessage, setRequestMethod)
1014 {
1015 char *method;
1016 int method_len;
1017 getObject(http_message_object, obj);
1018
1019 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
1020
1021 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1022 RETURN_FALSE;
1023 }
1024 if (method_len < 1) {
1025 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestMethod to an empty string");
1026 RETURN_FALSE;
1027 }
1028 if (!http_request_method_exists(1, 0, method)) {
1029 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unknown request method: %s", method);
1030 RETURN_FALSE;
1031 }
1032
1033 STR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
1034 RETURN_TRUE;
1035 }
1036 /* }}} */
1037
1038 /* {{{ proto string HttpMessage::getRequestUrl()
1039 *
1040 * Get the Request URL of the Message.
1041 *
1042 * Returns the request url as string on success, or FALSE if the message
1043 * is not of type HttpMessage::TYPE_REQUEST.
1044 */
1045 PHP_METHOD(HttpMessage, getRequestUrl)
1046 {
1047 NO_ARGS;
1048
1049 if (return_value_used) {
1050 getObject(http_message_object, obj);
1051 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
1052 RETURN_STRING(obj->message->http.info.request.url, 1);
1053 }
1054 }
1055 /* }}} */
1056
1057 /* {{{ proto bool HttpMessage::setRequestUrl(string url)
1058 *
1059 * Set the Request URL of the HTTP Message.
1060 *
1061 * Expects a string parameters containing the request url.
1062 *
1063 * Returns TRUE on success, or FALSE if the message is not of type
1064 * HttpMessage::TYPE_REQUEST or supplied URL was empty.
1065 */
1066 PHP_METHOD(HttpMessage, setRequestUrl)
1067 {
1068 char *URI;
1069 int URIlen;
1070 getObject(http_message_object, obj);
1071
1072 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
1073 RETURN_FALSE;
1074 }
1075 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
1076 if (URIlen < 1) {
1077 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestUrl to an empty string");
1078 RETURN_FALSE;
1079 }
1080
1081 STR_SET(obj->message->http.info.request.url, estrndup(URI, URIlen));
1082 RETURN_TRUE;
1083 }
1084 /* }}} */
1085
1086 /* {{{ proto string HttpMessage::getHttpVersion()
1087 *
1088 * Get the HTTP Protocol Version of the Message.
1089 *
1090 * Returns the HTTP protocol version as string.
1091 */
1092 PHP_METHOD(HttpMessage, getHttpVersion)
1093 {
1094 NO_ARGS;
1095
1096 if (return_value_used) {
1097 char ver[4] = {0};
1098 getObject(http_message_object, obj);
1099
1100 sprintf(ver, "%1.1lf", obj->message->http.version);
1101 RETURN_STRINGL(ver, 3, 1);
1102 }
1103 }
1104 /* }}} */
1105
1106 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
1107 *
1108 * Set the HTTP Protocol version of the Message.
1109 *
1110 * Expects a string parameter containing the HTTP protocol version.
1111 *
1112 * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
1113 */
1114 PHP_METHOD(HttpMessage, setHttpVersion)
1115 {
1116 char v[4];
1117 zval *zv;
1118 getObject(http_message_object, obj);
1119
1120 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &zv)) {
1121 return;
1122 }
1123
1124 convert_to_double(zv);
1125 sprintf(v, "%1.1lf", Z_DVAL_P(zv));
1126 if (strcmp(v, "1.0") && strcmp(v, "1.1")) {
1127 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %s", v);
1128 RETURN_FALSE;
1129 }
1130
1131 obj->message->http.version = Z_DVAL_P(zv);
1132 RETURN_TRUE;
1133 }
1134 /* }}} */
1135
1136 /* {{{ proto string HttpMessage::guessContentType(string magic_file[, int magic_mode = MAGIC_MIME])
1137 *
1138 * Attempts to guess the content type of supplied payload through libmagic.
1139 *
1140 * Expects a string parameter specifying the magic.mime database to use.
1141 * Additionally accepts an optional int parameter, being flags for libmagic.
1142 *
1143 * Returns the guessed content type on success, or FALSE on failure.
1144 *
1145 * Throws HttpRuntimeException, HttpInvalidParamException
1146 * if http.only_exceptions is TRUE.
1147 */
1148 PHP_METHOD(HttpMessage, guessContentType)
1149 {
1150 #ifdef HTTP_HAVE_MAGIC
1151 char *magic_file, *ct = NULL;
1152 int magic_file_len;
1153 long magic_mode = MAGIC_MIME;
1154
1155 RETVAL_FALSE;
1156 SET_EH_THROW_HTTP();
1157 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &magic_file, &magic_file_len, &magic_mode)) {
1158 getObject(http_message_object, obj);
1159 if ((ct = http_guess_content_type(magic_file, magic_mode, PHPSTR_VAL(&obj->message->body), PHPSTR_LEN(&obj->message->body), SEND_DATA))) {
1160 RETVAL_STRING(ct, 0);
1161 }
1162 }
1163 SET_EH_NORMAL();
1164 #else
1165 http_error(HE_THROW, HTTP_E_RUNTIME, "Cannot guess Content-Type; libmagic not available");
1166 RETURN_FALSE;
1167 #endif
1168 }
1169 /* }}} */
1170
1171 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1172 *
1173 * Get parent Message.
1174 *
1175 * Returns the parent HttpMessage on success, or NULL if there's none.
1176 *
1177 * Throws HttpRuntimeException.
1178 */
1179 PHP_METHOD(HttpMessage, getParentMessage)
1180 {
1181 SET_EH_THROW_HTTP();
1182 NO_ARGS {
1183 getObject(http_message_object, obj);
1184
1185 if (obj->message->parent) {
1186 RETVAL_OBJVAL(obj->parent, 1);
1187 } else {
1188 http_error(HE_WARNING, HTTP_E_RUNTIME, "HttpMessage does not have a parent message");
1189 }
1190 }
1191 SET_EH_NORMAL();
1192 }
1193 /* }}} */
1194
1195 /* {{{ proto bool HttpMessage::send()
1196 *
1197 * Send the Message according to its type as Response or Request.
1198 * This provides limited functionality compared to HttpRequest and HttpResponse.
1199 *
1200 * Returns TRUE on success, or FALSE on failure.
1201 */
1202 PHP_METHOD(HttpMessage, send)
1203 {
1204 getObject(http_message_object, obj);
1205
1206 NO_ARGS;
1207
1208 RETURN_SUCCESS(http_message_send(obj->message));
1209 }
1210 /* }}} */
1211
1212 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
1213 *
1214 * Get the string representation of the Message.
1215 *
1216 * Accepts a bool parameter which specifies whether the returned string
1217 * should also contain any parent messages.
1218 *
1219 * Returns the full message as string.
1220 */
1221 PHP_METHOD(HttpMessage, toString)
1222 {
1223 if (return_value_used) {
1224 char *string;
1225 size_t length;
1226 zend_bool include_parent = 0;
1227 getObject(http_message_object, obj);
1228
1229 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
1230 RETURN_FALSE;
1231 }
1232
1233 if (include_parent) {
1234 http_message_serialize(obj->message, &string, &length);
1235 } else {
1236 http_message_tostring(obj->message, &string, &length);
1237 }
1238 RETURN_STRINGL(string, length, 0);
1239 }
1240 }
1241 /* }}} */
1242
1243 /* {{{ proto HttpRequest|HttpResponse HttpMessage::toMessageTypeObject(void)
1244 *
1245 * Creates an object regarding to the type of the message.
1246 *
1247 * Returns either an HttpRequest or HttpResponse object on success, or NULL on failure.
1248 *
1249 * Throws HttpRuntimeException, HttpMessageTypeException, HttpHeaderException.
1250 */
1251 PHP_METHOD(HttpMessage, toMessageTypeObject)
1252 {
1253 SET_EH_THROW_HTTP();
1254
1255 NO_ARGS;
1256
1257 if (return_value_used) {
1258 getObject(http_message_object, obj);
1259
1260 switch (obj->message->type) {
1261 case HTTP_MSG_REQUEST:
1262 {
1263 #ifdef HTTP_HAVE_CURL
1264 int method;
1265 char *url;
1266 zval tmp, body, *array, *headers, *host = http_message_header(obj->message, "Host");
1267 php_url hurl, *purl = php_url_parse(obj->message->http.info.request.url);
1268
1269 MAKE_STD_ZVAL(array);
1270 array_init(array);
1271
1272 memset(&hurl, 0, sizeof(php_url));
1273 hurl.host = host ? Z_STRVAL_P(host) : NULL;
1274 http_build_url(HTTP_URL_REPLACE, purl, &hurl, NULL, &url, NULL);
1275 php_url_free(purl);
1276 add_assoc_string(array, "url", url, 0);
1277
1278 if ( (method = http_request_method_exists(1, 0, obj->message->http.info.request.method)) ||
1279 (method = http_request_method_register(obj->message->http.info.request.method, strlen(obj->message->http.info.request.method)))) {
1280 add_assoc_long(array, "method", method);
1281 }
1282
1283 if (10 == (int) (obj->message->http.version * 10)) {
1284 add_assoc_long(array, "protocol", CURL_HTTP_VERSION_1_0);
1285 }
1286
1287 MAKE_STD_ZVAL(headers);
1288 array_init(headers);
1289 INIT_ZARR(tmp, &obj->message->hdrs);
1290 array_copy(&tmp, headers);
1291 add_assoc_zval(array, "headers", headers);
1292
1293 object_init_ex(return_value, http_request_object_ce);
1294 zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setoptions", NULL, array);
1295 zval_ptr_dtor(&array);
1296
1297 INIT_PZVAL(&body);
1298 ZVAL_STRINGL(&body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 0);
1299 zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setrawpostdata", NULL, &body);
1300 #else
1301 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
1302 #endif
1303 break;
1304 }
1305
1306 case HTTP_MSG_RESPONSE:
1307 {
1308 #ifndef WONKY
1309 HashPosition pos1, pos2;
1310 ulong idx;
1311 uint key_len;
1312 char *key = NULL;
1313 zval **header, **h, *body;
1314
1315 if (obj->message->http.info.response.code) {
1316 http_send_status(obj->message->http.info.response.code);
1317 }
1318
1319 object_init_ex(return_value, http_response_object_ce);
1320
1321 FOREACH_HASH_KEYLENVAL(pos1, &obj->message->hdrs, key, key_len, idx, header) {
1322 if (key) {
1323 zval zkey;
1324
1325 INIT_PZVAL(&zkey);
1326 ZVAL_STRINGL(&zkey, key, key_len, 0);
1327
1328 switch (Z_TYPE_PP(header)) {
1329 case IS_ARRAY:
1330 case IS_OBJECT:
1331 FOREACH_HASH_VAL(pos2, HASH_OF(*header), h) {
1332 ZVAL_ADDREF(*h);
1333 zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, &zkey, *h);
1334 zval_ptr_dtor(h);
1335 }
1336 break;
1337
1338 default:
1339 ZVAL_ADDREF(*header);
1340 zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, &zkey, *header);
1341 zval_ptr_dtor(header);
1342 break;
1343 }
1344 key = NULL;
1345 }
1346 }
1347
1348 MAKE_STD_ZVAL(body);
1349 ZVAL_STRINGL(body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 1);
1350 zend_call_method_with_1_params(&return_value, http_response_object_ce, NULL, "setdata", NULL, body);
1351 zval_ptr_dtor(&body);
1352 #else
1353 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpResponse (need PHP 5.1+)");
1354 #endif
1355 break;
1356 }
1357
1358 default:
1359 http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type HttpMessage::TYPE_REQUEST nor HttpMessage::TYPE_RESPONSE");
1360 break;
1361 }
1362 }
1363 SET_EH_NORMAL();
1364 }
1365 /* }}} */
1366
1367 /* {{{ proto int HttpMessage::count()
1368 *
1369 * Implements Countable.
1370 *
1371 * Returns the number of parent messages + 1.
1372 */
1373 PHP_METHOD(HttpMessage, count)
1374 {
1375 NO_ARGS {
1376 long i;
1377 getObject(http_message_object, obj);
1378
1379 http_message_count(i, obj->message);
1380 RETURN_LONG(i);
1381 }
1382 }
1383 /* }}} */
1384
1385 /* {{{ proto string HttpMessage::serialize()
1386 *
1387 * Implements Serializable.
1388 *
1389 * Returns the serialized representation of the HttpMessage.
1390 */
1391 PHP_METHOD(HttpMessage, serialize)
1392 {
1393 NO_ARGS {
1394 char *string;
1395 size_t length;
1396 getObject(http_message_object, obj);
1397
1398 http_message_serialize(obj->message, &string, &length);
1399 RETURN_STRINGL(string, length, 0);
1400 }
1401 }
1402 /* }}} */
1403
1404 /* {{{ proto void HttpMessage::unserialize(string serialized)
1405 *
1406 * Implements Serializable.
1407 *
1408 * Re-constructs the HttpMessage based upon the serialized string.
1409 */
1410 PHP_METHOD(HttpMessage, unserialize)
1411 {
1412 int length;
1413 char *serialized;
1414 getObject(http_message_object, obj);
1415
1416 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &length)) {
1417 http_message_dtor(obj->message);
1418 if (!http_message_parse_ex(obj->message, serialized, (size_t) length)) {
1419 http_error(HE_ERROR, HTTP_E_RUNTIME, "Could not unserialize HttpMessage");
1420 http_message_init(obj->message);
1421 }
1422 }
1423 }
1424 /* }}} */
1425
1426 /* {{{ proto HttpMessage HttpMessage::detach(void)
1427 *
1428 * Returns a clone of an HttpMessage object detached from any parent messages.
1429 */
1430 PHP_METHOD(HttpMessage, detach)
1431 {
1432 http_info info;
1433 http_message *msg;
1434 getObject(http_message_object, obj);
1435
1436 NO_ARGS;
1437
1438 info.type = obj->message->type;
1439 memcpy(&HTTP_INFO(&info), &HTTP_INFO(obj->message), sizeof(struct http_info));
1440
1441 msg = http_message_new();
1442 http_message_set_info(msg, &info);
1443
1444 zend_hash_copy(&msg->hdrs, &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1445 phpstr_append(&msg->body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message));
1446
1447 RETVAL_OBJVAL(http_message_object_new_ex(Z_OBJCE_P(getThis()), msg, NULL), 0);
1448 }
1449 /* }}} */
1450
1451 /* {{{ proto void HttpMessage::prepend(HttpMessage message[, bool top = true])
1452 *
1453 * Prepends message(s) to the HTTP message.
1454 *
1455 * Expects an HttpMessage object as parameter.
1456 *
1457 * Throws HttpInvalidParamException if the message is located within the same message chain.
1458 */
1459 PHP_METHOD(HttpMessage, prepend)
1460 {
1461 zval *prepend;
1462 zend_bool top = 1;
1463
1464 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &prepend, http_message_object_ce, &top)) {
1465 http_message *msg[2];
1466 getObject(http_message_object, obj);
1467 getObjectEx(http_message_object, prepend_obj, prepend);
1468
1469 /* safety check */
1470 for (msg[0] = obj->message; msg[0]; msg[0] = msg[0]->parent) {
1471 for (msg[1] = prepend_obj->message; msg[1]; msg[1] = msg[1]->parent) {
1472 if (msg[0] == msg[1]) {
1473 http_error(HE_THROW, HTTP_E_INVALID_PARAM, "Cannot prepend a message located within the same message chain");
1474 return;
1475 }
1476 }
1477 }
1478
1479 http_message_object_prepend_ex(getThis(), prepend, top);
1480 }
1481 }
1482 /* }}} */
1483
1484 /* {{{ proto HttpMessage HttpMessage::reverse()
1485 *
1486 * Reorders the message chain in reverse order.
1487 *
1488 * Returns the most parent HttpMessage object.
1489 */
1490 PHP_METHOD(HttpMessage, reverse)
1491 {
1492 NO_ARGS {
1493 http_message_object_reverse(getThis(), return_value);
1494 }
1495 }
1496 /* }}} */
1497
1498 /* {{{ proto void HttpMessage::rewind(void)
1499 *
1500 * Implements Iterator.
1501 */
1502 PHP_METHOD(HttpMessage, rewind)
1503 {
1504 NO_ARGS {
1505 getObject(http_message_object, obj);
1506
1507 if (obj->iterator) {
1508 zval_ptr_dtor(&obj->iterator);
1509 }
1510 ZVAL_ADDREF(getThis());
1511 obj->iterator = getThis();
1512 }
1513 }
1514 /* }}} */
1515
1516 /* {{{ proto bool HttpMessage::valid(void)
1517 *
1518 * Implements Iterator.
1519 */
1520 PHP_METHOD(HttpMessage, valid)
1521 {
1522 NO_ARGS {
1523 getObject(http_message_object, obj);
1524
1525 RETURN_BOOL(obj->iterator != NULL);
1526 }
1527 }
1528 /* }}} */
1529
1530 /* {{{ proto void HttpMessage::next(void)
1531 *
1532 * Implements Iterator.
1533 */
1534 PHP_METHOD(HttpMessage, next)
1535 {
1536 NO_ARGS {
1537 getObject(http_message_object, obj);
1538 getObjectEx(http_message_object, itr, obj->iterator);
1539
1540 if (itr && itr->parent.handle) {
1541 zval *old = obj->iterator;
1542 MAKE_STD_ZVAL(obj->iterator);
1543 ZVAL_OBJVAL(obj->iterator, itr->parent, 1);
1544 zval_ptr_dtor(&old);
1545 } else {
1546 zval_ptr_dtor(&obj->iterator);
1547 obj->iterator = NULL;
1548 }
1549 }
1550 }
1551 /* }}} */
1552
1553 /* {{{ proto int HttpMessage::key(void)
1554 *
1555 * Implements Iterator.
1556 */
1557 PHP_METHOD(HttpMessage, key)
1558 {
1559 NO_ARGS {
1560 getObject(http_message_object, obj);
1561
1562 RETURN_LONG(obj->iterator ? obj->iterator->value.obj.handle:0);
1563 }
1564 }
1565 /* }}} */
1566
1567 /* {{{ proto HttpMessage HttpMessage::current(void)
1568 *
1569 * Implements Iterator.
1570 */
1571 PHP_METHOD(HttpMessage, current)
1572 {
1573 NO_ARGS {
1574 getObject(http_message_object, obj);
1575
1576 if (obj->iterator) {
1577 RETURN_ZVAL(obj->iterator, 1, 0);
1578 }
1579 }
1580 }
1581 /* }}} */
1582
1583 #endif /* ZEND_ENGINE_2 */
1584
1585 /*
1586 * Local variables:
1587 * tab-width: 4
1588 * c-basic-offset: 4
1589 * End:
1590 * vim600: noet sw=4 ts=4 fdm=marker
1591 * vim<600: noet sw=4 ts=4
1592 */
1593