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