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