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