better fix for robs issue
[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->iterator) {
397 zval_ptr_dtor(&o->iterator);
398 o->iterator = NULL;
399 }
400 if (o->message) {
401 http_message_dtor(o->message);
402 efree(o->message);
403 }
404 if (o->parent.handle) {
405 zval p;
406
407 INIT_PZVAL(&p);
408 p.type = IS_OBJECT;
409 p.value.obj = o->parent;
410 zend_objects_store_del_ref(&p TSRMLS_CC);
411 }
412 freeObject(o);
413 }
414
415 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC)
416 {
417 getObjectEx(http_message_object, obj, object);
418 http_message *msg = obj->message;
419 zval *return_value;
420 #ifdef WONKY
421 ulong h = zend_hash_func(Z_STRVAL_P(member), Z_STRLEN_P(member)+1);
422 #else
423 zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
424
425 if (!pinfo) {
426 return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
427 }
428 #endif
429
430 if (type == BP_VAR_W) {
431 zend_error(E_ERROR, "Cannot access HttpMessage properties by reference or array key/index");
432 return NULL;
433 }
434
435 ALLOC_ZVAL(return_value);
436 #ifdef Z_SET_REFCOUNT
437 Z_SET_REFCOUNT_P(return_value, 0);
438 Z_UNSET_ISREF_P(return_value);
439 #else
440 return_value->refcount = 0;
441 return_value->is_ref = 0;
442 #endif
443
444 #ifdef WONKY
445 switch (h)
446 #else
447 switch (pinfo->h)
448 #endif
449 {
450 case HTTP_MSG_PROPHASH_TYPE:
451 case HTTP_MSG_CHILD_PROPHASH_TYPE:
452 RETVAL_LONG(msg->type);
453 break;
454
455 case HTTP_MSG_PROPHASH_HTTP_VERSION:
456 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION:
457 RETVAL_DOUBLE(msg->http.version);
458 break;
459
460 case HTTP_MSG_PROPHASH_BODY:
461 case HTTP_MSG_CHILD_PROPHASH_BODY:
462 phpstr_fix(PHPSTR(msg));
463 RETVAL_PHPSTR(PHPSTR(msg), 0, 1);
464 break;
465
466 case HTTP_MSG_PROPHASH_HEADERS:
467 case HTTP_MSG_CHILD_PROPHASH_HEADERS:
468 array_init(return_value);
469 zend_hash_copy(Z_ARRVAL_P(return_value), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
470 break;
471
472 case HTTP_MSG_PROPHASH_PARENT_MESSAGE:
473 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE:
474 if (msg->parent) {
475 RETVAL_OBJVAL(obj->parent, 1);
476 } else {
477 RETVAL_NULL();
478 }
479 break;
480
481 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
482 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD:
483 if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.method) {
484 RETVAL_STRING(msg->http.info.request.method, 1);
485 } else {
486 RETVAL_NULL();
487 }
488 break;
489
490 case HTTP_MSG_PROPHASH_REQUEST_URL:
491 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL:
492 if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.url) {
493 RETVAL_STRING(msg->http.info.request.url, 1);
494 } else {
495 RETVAL_NULL();
496 }
497 break;
498
499 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
500 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE:
501 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
502 RETVAL_LONG(msg->http.info.response.code);
503 } else {
504 RETVAL_NULL();
505 }
506 break;
507
508 case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
509 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS:
510 if (HTTP_MSG_TYPE(RESPONSE, msg) && msg->http.info.response.status) {
511 RETVAL_STRING(msg->http.info.response.status, 1);
512 } else {
513 RETVAL_NULL();
514 }
515 break;
516
517 default:
518 FREE_ZVAL(return_value);
519 return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
520 }
521
522 return return_value;
523 }
524
525 static void _http_message_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC)
526 {
527 getObjectEx(http_message_object, obj, object);
528 http_message *msg = obj->message;
529 zval *cpy = NULL;
530 #ifdef WONKY
531 ulong h = zend_hash_func(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
532 #else
533 zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
534
535 if (!pinfo) {
536 zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
537 return;
538 }
539 #endif
540
541 #ifdef WONKY
542 switch (h)
543 #else
544 switch (pinfo->h)
545 #endif
546 {
547 case HTTP_MSG_PROPHASH_TYPE:
548 case HTTP_MSG_CHILD_PROPHASH_TYPE:
549 cpy = http_zsep(IS_LONG, value);
550 http_message_set_type(msg, Z_LVAL_P(cpy));
551 break;
552
553 case HTTP_MSG_PROPHASH_HTTP_VERSION:
554 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION:
555 cpy = http_zsep(IS_DOUBLE, value);
556 msg->http.version = Z_DVAL_P(cpy);
557 break;
558
559 case HTTP_MSG_PROPHASH_BODY:
560 case HTTP_MSG_CHILD_PROPHASH_BODY:
561 cpy = http_zsep(IS_STRING, value);
562 phpstr_dtor(PHPSTR(msg));
563 phpstr_from_string_ex(PHPSTR(msg), Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
564 break;
565
566 case HTTP_MSG_PROPHASH_HEADERS:
567 case HTTP_MSG_CHILD_PROPHASH_HEADERS:
568 cpy = http_zsep(IS_ARRAY, value);
569 zend_hash_clean(&msg->hdrs);
570 zend_hash_copy(&msg->hdrs, Z_ARRVAL_P(cpy), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
571 break;
572
573 case HTTP_MSG_PROPHASH_PARENT_MESSAGE:
574 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE:
575 if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), http_message_object_ce TSRMLS_CC)) {
576 if (msg->parent) {
577 zval tmp;
578 tmp.value.obj = obj->parent;
579 Z_OBJ_DELREF(tmp);
580 }
581 Z_OBJ_ADDREF_P(value);
582 obj->parent = value->value.obj;
583 }
584 break;
585
586 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
587 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD:
588 if (HTTP_MSG_TYPE(REQUEST, msg)) {
589 cpy = http_zsep(IS_STRING, value);
590 STR_SET(msg->http.info.request.method, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
591 }
592 break;
593
594 case HTTP_MSG_PROPHASH_REQUEST_URL:
595 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL:
596 if (HTTP_MSG_TYPE(REQUEST, msg)) {
597 cpy = http_zsep(IS_STRING, value);
598 STR_SET(msg->http.info.request.url, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
599 }
600 break;
601
602 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
603 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE:
604 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
605 cpy = http_zsep(IS_LONG, value);
606 msg->http.info.response.code = Z_LVAL_P(cpy);
607 }
608 break;
609
610 case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
611 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS:
612 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
613 cpy = http_zsep(IS_STRING, value);
614 STR_SET(msg->http.info.response.status, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
615 }
616 break;
617
618 default:
619 zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
620 break;
621 }
622 if (cpy) {
623 zval_ptr_dtor(&cpy);
624 }
625 }
626
627 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
628 {
629 zval *headers;
630 getObjectEx(http_message_object, obj, object);
631 http_message *msg = obj->message;
632 HashTable *props = OBJ_PROP(obj);
633 zval array, *parent;
634
635 INIT_ZARR(array, props);
636
637 #define ASSOC_PROP(array, ptype, name, val) \
638 { \
639 char *m_prop_name; \
640 int m_prop_len; \
641 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
642 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
643 efree(m_prop_name); \
644 }
645 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
646 #define ASSOC_STRINGL(array, name, val, len) \
647 { \
648 char *m_prop_name; \
649 int m_prop_len; \
650 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
651 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
652 efree(m_prop_name); \
653 }
654
655 ASSOC_PROP(array, long, "type", msg->type);
656 ASSOC_PROP(array, double, "httpVersion", msg->http.version);
657
658 switch (msg->type) {
659 case HTTP_MSG_REQUEST:
660 ASSOC_PROP(array, long, "responseCode", 0);
661 ASSOC_STRINGL(array, "responseStatus", "", 0);
662 ASSOC_STRING(array, "requestMethod", STR_PTR(msg->http.info.request.method));
663 ASSOC_STRING(array, "requestUrl", STR_PTR(msg->http.info.request.url));
664 break;
665
666 case HTTP_MSG_RESPONSE:
667 ASSOC_PROP(array, long, "responseCode", msg->http.info.response.code);
668 ASSOC_STRING(array, "responseStatus", STR_PTR(msg->http.info.response.status));
669 ASSOC_STRINGL(array, "requestMethod", "", 0);
670 ASSOC_STRINGL(array, "requestUrl", "", 0);
671 break;
672
673 case HTTP_MSG_NONE:
674 default:
675 ASSOC_PROP(array, long, "responseCode", 0);
676 ASSOC_STRINGL(array, "responseStatus", "", 0);
677 ASSOC_STRINGL(array, "requestMethod", "", 0);
678 ASSOC_STRINGL(array, "requestUrl", "", 0);
679 break;
680 }
681
682 MAKE_STD_ZVAL(headers);
683 array_init(headers);
684 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
685 ASSOC_PROP(array, zval, "headers", headers);
686 ASSOC_STRINGL(array, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg));
687
688 MAKE_STD_ZVAL(parent);
689 if (msg->parent) {
690 ZVAL_OBJVAL(parent, obj->parent, 1);
691 } else {
692 ZVAL_NULL(parent);
693 }
694 ASSOC_PROP(array, zval, "parentMessage", parent);
695
696 return OBJ_PROP(obj);
697 }
698
699 /* ### USERLAND ### */
700
701 /* {{{ proto void HttpMessage::__construct([string message])
702 Create a new HttpMessage object instance. */
703 PHP_METHOD(HttpMessage, __construct)
704 {
705 int length = 0;
706 char *message = NULL;
707
708 getObject(http_message_object, obj);
709
710 SET_EH_THROW_HTTP();
711 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
712 http_message *msg = obj->message;
713
714 http_message_dtor(msg);
715 if ((obj->message = http_message_parse_ex(msg, message, length))) {
716 if (obj->message->parent) {
717 obj->parent = http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL);
718 }
719 } else {
720 obj->message = http_message_init(msg);
721 }
722 }
723 if (!obj->message) {
724 obj->message = http_message_new();
725 }
726 SET_EH_NORMAL();
727 }
728 /* }}} */
729
730 /* {{{ proto static HttpMessage HttpMessage::factory([string raw_message[, string class_name = "HttpMessage"]])
731 Create a new HttpMessage object instance. */
732 PHP_METHOD(HttpMessage, factory)
733 {
734 char *string = NULL, *cn = NULL;
735 int length = 0, cl = 0;
736 http_message *msg = NULL;
737 zend_object_value ov;
738 http_message_object *obj = NULL;
739
740 RETVAL_NULL();
741
742 SET_EH_THROW_HTTP();
743 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &string, &length, &cn, &cl)) {
744 if (length) {
745 msg = http_message_parse(string, length);
746 }
747 if ((msg || !length) && SUCCESS == http_object_new(&ov, cn, cl, _http_message_object_new_ex, http_message_object_ce, msg, &obj)) {
748 RETVAL_OBJVAL(ov, 0);
749 }
750 if (obj && !obj->message) {
751 obj->message = http_message_new();
752 }
753 }
754 SET_EH_NORMAL();
755 }
756 /* }}} */
757
758 /* {{{ proto static HttpMessage HttpMessage::fromEnv(int type[, string class_name = "HttpMessage"])
759 Create a new HttpMessage object from environment representing either current request or response */
760 PHP_METHOD(HttpMessage, fromEnv)
761 {
762 char *cn = NULL;
763 int cl = 0;
764 long type;
765 http_message_object *obj = NULL;
766 zend_object_value ov;
767
768 RETVAL_NULL();
769 SET_EH_THROW_HTTP();
770 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|s", &type, &cn, &cl)) {
771 if (SUCCESS == http_object_new(&ov, cn, cl, _http_message_object_new_ex, http_message_object_ce, http_message_init_env(NULL, type), &obj)) {
772 RETVAL_OBJVAL(ov, 0);
773 }
774 if (obj && !obj->message) {
775 obj->message = http_message_new();
776 }
777 }
778 SET_EH_NORMAL();
779 }
780 /* }}} */
781
782 /* {{{ proto string HttpMessage::getBody()
783 Get the body of the parsed HttpMessage. */
784 PHP_METHOD(HttpMessage, getBody)
785 {
786 NO_ARGS;
787
788 if (return_value_used) {
789 getObject(http_message_object, obj);
790 RETURN_PHPSTR(&obj->message->body, PHPSTR_FREE_NOT, 1);
791 }
792 }
793 /* }}} */
794
795 /* {{{ proto void HttpMessage::setBody(string body)
796 Set the body of the HttpMessage. NOTE: Don't forget to update any headers accordingly. */
797 PHP_METHOD(HttpMessage, setBody)
798 {
799 char *body;
800 int len;
801 getObject(http_message_object, obj);
802
803 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &body, &len)) {
804 phpstr_dtor(PHPSTR(obj->message));
805 phpstr_from_string_ex(PHPSTR(obj->message), body, len);
806 }
807 }
808 /* }}} */
809
810 /* {{{ proto string HttpMessage::getHeader(string header)
811 Get message header. */
812 PHP_METHOD(HttpMessage, getHeader)
813 {
814 zval *header;
815 char *orig_header, *nice_header;
816 int header_len;
817 getObject(http_message_object, obj);
818
819 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &orig_header, &header_len)) {
820 RETURN_FALSE;
821 }
822
823 nice_header = pretty_key(estrndup(orig_header, header_len), header_len, 1, 1);
824 if ((header = http_message_header_ex(obj->message, nice_header, header_len + 1, 0))) {
825 RETVAL_ZVAL(header, 1, 1);
826 }
827 efree(nice_header);
828 }
829 /* }}} */
830
831 /* {{{ proto array HttpMessage::getHeaders()
832 Get Message Headers. */
833 PHP_METHOD(HttpMessage, getHeaders)
834 {
835 NO_ARGS;
836
837 if (return_value_used) {
838 getObject(http_message_object, obj);
839
840 array_init(return_value);
841 array_copy(&obj->message->hdrs, Z_ARRVAL_P(return_value));
842 }
843 }
844 /* }}} */
845
846 /* {{{ proto void HttpMessage::setHeaders(array headers)
847 Sets new headers. */
848 PHP_METHOD(HttpMessage, setHeaders)
849 {
850 zval *new_headers = NULL;
851 getObject(http_message_object, obj);
852
853 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &new_headers)) {
854 return;
855 }
856
857 zend_hash_clean(&obj->message->hdrs);
858 if (new_headers) {
859 array_copy(Z_ARRVAL_P(new_headers), &obj->message->hdrs);
860 }
861 }
862 /* }}} */
863
864 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
865 Add headers. If append is true, headers with the same name will be separated, else overwritten. */
866 PHP_METHOD(HttpMessage, addHeaders)
867 {
868 zval *new_headers;
869 zend_bool append = 0;
870 getObject(http_message_object, obj);
871
872 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
873 return;
874 }
875
876 array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, append, ARRAY_JOIN_STRONLY|ARRAY_JOIN_PRETTIFY);
877 }
878 /* }}} */
879
880 /* {{{ proto int HttpMessage::getType()
881 Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE) */
882 PHP_METHOD(HttpMessage, getType)
883 {
884 NO_ARGS;
885
886 if (return_value_used) {
887 getObject(http_message_object, obj);
888 RETURN_LONG(obj->message->type);
889 }
890 }
891 /* }}} */
892
893 /* {{{ proto void HttpMessage::setType(int type)
894 Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE) */
895 PHP_METHOD(HttpMessage, setType)
896 {
897 long type;
898 getObject(http_message_object, obj);
899
900 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
901 return;
902 }
903 http_message_set_type(obj->message, type);
904 }
905 /* }}} */
906
907 /* {{{ proto string HttpMessage::getInfo(void)
908 Get the HTTP request/response line */
909 PHP_METHOD(HttpMessage, getInfo)
910 {
911 NO_ARGS;
912
913 if (return_value_used) {
914 getObject(http_message_object, obj);
915
916 switch (obj->message->type) {
917 case HTTP_MSG_REQUEST:
918 Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, HTTP_INFO_REQUEST_FMT_ARGS(&obj->message->http, ""));
919 break;
920 case HTTP_MSG_RESPONSE:
921 Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, HTTP_INFO_RESPONSE_FMT_ARGS(&obj->message->http, ""));
922 break;
923 default:
924 RETURN_NULL();
925 break;
926 }
927 Z_TYPE_P(return_value) = IS_STRING;
928 }
929 }
930 /* }}} */
931
932 /* {{{ proto bool HttpMessage::setInfo(string http_info)
933 Set type and request or response info with a standard HTTP request or response line */
934 PHP_METHOD(HttpMessage, setInfo)
935 {
936 char *str;
937 int len;
938 http_info inf;
939
940 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) && SUCCESS == http_info_parse_ex(str, &inf, 0)) {
941 getObject(http_message_object, obj);
942
943 http_message_set_info(obj->message, &inf);
944 http_info_dtor(&inf);
945 RETURN_TRUE;
946 }
947 RETURN_FALSE;
948 }
949 /* }}} */
950
951 /* {{{ proto int HttpMessage::getResponseCode()
952 Get the Response Code of the Message. */
953 PHP_METHOD(HttpMessage, getResponseCode)
954 {
955 NO_ARGS;
956
957 if (return_value_used) {
958 getObject(http_message_object, obj);
959 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
960 RETURN_LONG(obj->message->http.info.response.code);
961 }
962 }
963 /* }}} */
964
965 /* {{{ proto bool HttpMessage::setResponseCode(int code)
966 Set the response code of an HTTP Response Message. */
967 PHP_METHOD(HttpMessage, setResponseCode)
968 {
969 long code;
970 getObject(http_message_object, obj);
971
972 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
973
974 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
975 RETURN_FALSE;
976 }
977 if (code < 100 || code > 599) {
978 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid response code (100-599): %ld", code);
979 RETURN_FALSE;
980 }
981
982 obj->message->http.info.response.code = code;
983 RETURN_TRUE;
984 }
985 /* }}} */
986
987 /* {{{ proto string HttpMessage::getResponseStatus()
988 Get the Response Status of the message (i.e. the string following the response code). */
989 PHP_METHOD(HttpMessage, getResponseStatus)
990 {
991 NO_ARGS;
992
993 if (return_value_used) {
994 getObject(http_message_object, obj);
995 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
996 if (obj->message->http.info.response.status) {
997 RETURN_STRING(obj->message->http.info.response.status, 1);
998 } else {
999 RETURN_EMPTY_STRING();
1000 }
1001 }
1002 }
1003 /* }}} */
1004
1005 /* {{{ proto bool HttpMessage::setResponseStatus(string status)
1006 Set the Response Status of the HTTP message (i.e. the string following the response code). */
1007 PHP_METHOD(HttpMessage, setResponseStatus)
1008 {
1009 char *status;
1010 int status_len;
1011 getObject(http_message_object, obj);
1012
1013 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
1014
1015 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len)) {
1016 RETURN_FALSE;
1017 }
1018 STR_SET(obj->message->http.info.response.status, estrndup(status, status_len));
1019 RETURN_TRUE;
1020 }
1021 /* }}} */
1022
1023 /* {{{ proto string HttpMessage::getRequestMethod()
1024 Get the Request Method of the Message. */
1025 PHP_METHOD(HttpMessage, getRequestMethod)
1026 {
1027 NO_ARGS;
1028
1029 if (return_value_used) {
1030 getObject(http_message_object, obj);
1031 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
1032 if (obj->message->http.info.request.method) {
1033 RETURN_STRING(obj->message->http.info.request.method, 1);
1034 } else {
1035 RETURN_EMPTY_STRING();
1036 }
1037 }
1038 }
1039 /* }}} */
1040
1041 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
1042 Set the Request Method of the HTTP Message. */
1043 PHP_METHOD(HttpMessage, setRequestMethod)
1044 {
1045 char *method;
1046 int method_len;
1047 getObject(http_message_object, obj);
1048
1049 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
1050
1051 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1052 RETURN_FALSE;
1053 }
1054 if (method_len < 1) {
1055 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestMethod to an empty string");
1056 RETURN_FALSE;
1057 }
1058 if (!http_request_method_exists(1, 0, method)) {
1059 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unknown request method: %s", method);
1060 RETURN_FALSE;
1061 }
1062
1063 STR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
1064 RETURN_TRUE;
1065 }
1066 /* }}} */
1067
1068 /* {{{ proto string HttpMessage::getRequestUrl()
1069 Get the Request URL of the Message. */
1070 PHP_METHOD(HttpMessage, getRequestUrl)
1071 {
1072 NO_ARGS;
1073
1074 if (return_value_used) {
1075 getObject(http_message_object, obj);
1076 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
1077 if (obj->message->http.info.request.url) {
1078 RETURN_STRING(obj->message->http.info.request.url, 1);
1079 } else {
1080 RETURN_EMPTY_STRING();
1081 }
1082 }
1083 }
1084 /* }}} */
1085
1086 /* {{{ proto bool HttpMessage::setRequestUrl(string url)
1087 Set the Request URL of the HTTP Message. */
1088 PHP_METHOD(HttpMessage, setRequestUrl)
1089 {
1090 char *URI;
1091 int URIlen;
1092 getObject(http_message_object, obj);
1093
1094 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
1095 RETURN_FALSE;
1096 }
1097 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
1098 if (URIlen < 1) {
1099 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestUrl to an empty string");
1100 RETURN_FALSE;
1101 }
1102
1103 STR_SET(obj->message->http.info.request.url, estrndup(URI, URIlen));
1104 RETURN_TRUE;
1105 }
1106 /* }}} */
1107
1108 /* {{{ proto string HttpMessage::getHttpVersion()
1109 Get the HTTP Protocol Version of the Message. */
1110 PHP_METHOD(HttpMessage, getHttpVersion)
1111 {
1112 NO_ARGS;
1113
1114 if (return_value_used) {
1115 char ver[4] = {0};
1116 getObject(http_message_object, obj);
1117
1118 sprintf(ver, "%1.1lf", obj->message->http.version);
1119 RETURN_STRINGL(ver, 3, 1);
1120 }
1121 }
1122 /* }}} */
1123
1124 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
1125 Set the HTTP Protocol version of the Message. */
1126 PHP_METHOD(HttpMessage, setHttpVersion)
1127 {
1128 char v[4];
1129 zval *zv;
1130 getObject(http_message_object, obj);
1131
1132 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &zv)) {
1133 return;
1134 }
1135
1136 convert_to_double(zv);
1137 snprintf(v, sizeof(v), "%1.1f", Z_DVAL_P(zv));
1138 if (strcmp(v, "1.0") && strcmp(v, "1.1")) {
1139 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %g", Z_DVAL_P(zv));
1140 RETURN_FALSE;
1141 }
1142
1143 obj->message->http.version = Z_DVAL_P(zv);
1144 RETURN_TRUE;
1145 }
1146 /* }}} */
1147
1148 /* {{{ proto string HttpMessage::guessContentType(string magic_file[, int magic_mode = MAGIC_MIME])
1149 Attempts to guess the content type of supplied payload through libmagic. */
1150 PHP_METHOD(HttpMessage, guessContentType)
1151 {
1152 #ifdef HTTP_HAVE_MAGIC
1153 char *magic_file, *ct = NULL;
1154 int magic_file_len;
1155 long magic_mode = MAGIC_MIME;
1156
1157 RETVAL_FALSE;
1158 SET_EH_THROW_HTTP();
1159 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &magic_file, &magic_file_len, &magic_mode)) {
1160 getObject(http_message_object, obj);
1161 if ((ct = http_guess_content_type(magic_file, magic_mode, PHPSTR_VAL(&obj->message->body), PHPSTR_LEN(&obj->message->body), SEND_DATA))) {
1162 RETVAL_STRING(ct, 0);
1163 }
1164 }
1165 SET_EH_NORMAL();
1166 #else
1167 http_error(HE_THROW, HTTP_E_RUNTIME, "Cannot guess Content-Type; libmagic not available");
1168 RETURN_FALSE;
1169 #endif
1170 }
1171 /* }}} */
1172
1173 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1174 Get parent Message. */
1175 PHP_METHOD(HttpMessage, getParentMessage)
1176 {
1177 SET_EH_THROW_HTTP();
1178 NO_ARGS {
1179 getObject(http_message_object, obj);
1180
1181 if (obj->message->parent) {
1182 RETVAL_OBJVAL(obj->parent, 1);
1183 } else {
1184 http_error(HE_WARNING, HTTP_E_RUNTIME, "HttpMessage does not have a parent message");
1185 }
1186 }
1187 SET_EH_NORMAL();
1188 }
1189 /* }}} */
1190
1191 /* {{{ proto bool HttpMessage::send()
1192 Send the Message according to its type as Response or Request. */
1193 PHP_METHOD(HttpMessage, send)
1194 {
1195 getObject(http_message_object, obj);
1196
1197 NO_ARGS;
1198
1199 RETURN_SUCCESS(http_message_send(obj->message));
1200 }
1201 /* }}} */
1202
1203 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
1204 Get the string representation of the Message. */
1205 PHP_METHOD(HttpMessage, toString)
1206 {
1207 if (return_value_used) {
1208 char *string;
1209 size_t length;
1210 zend_bool include_parent = 0;
1211 getObject(http_message_object, obj);
1212
1213 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
1214 RETURN_FALSE;
1215 }
1216
1217 if (include_parent) {
1218 http_message_serialize(obj->message, &string, &length);
1219 } else {
1220 http_message_tostring(obj->message, &string, &length);
1221 }
1222 RETURN_STRINGL(string, length, 0);
1223 }
1224 }
1225 /* }}} */
1226
1227 /* {{{ proto HttpRequest|HttpResponse HttpMessage::toMessageTypeObject(void)
1228 Creates an object regarding to the type of the message. Returns either an HttpRequest or HttpResponse object on success, or NULL on failure. */
1229 PHP_METHOD(HttpMessage, toMessageTypeObject)
1230 {
1231 SET_EH_THROW_HTTP();
1232
1233 NO_ARGS;
1234
1235 if (return_value_used) {
1236 getObject(http_message_object, obj);
1237
1238 switch (obj->message->type) {
1239 case HTTP_MSG_REQUEST:
1240 {
1241 #ifdef HTTP_HAVE_CURL
1242 int method;
1243 char *url;
1244 zval post, body, *array, *headers, *host = http_message_header(obj->message, "Host");
1245 php_url hurl, *purl = php_url_parse(STR_PTR(obj->message->http.info.request.url));
1246
1247 MAKE_STD_ZVAL(array);
1248 array_init(array);
1249
1250 memset(&hurl, 0, sizeof(php_url));
1251 if (host) {
1252 hurl.host = Z_STRVAL_P(host);
1253 zval_ptr_dtor(&host);
1254 }
1255 http_build_url(HTTP_URL_REPLACE, purl, &hurl, NULL, &url, NULL);
1256 php_url_free(purl);
1257 add_assoc_string(array, "url", url, 0);
1258
1259 if ( obj->message->http.info.request.method &&
1260 ((method = http_request_method_exists(1, 0, obj->message->http.info.request.method)) ||
1261 (method = http_request_method_register(obj->message->http.info.request.method, strlen(obj->message->http.info.request.method))))) {
1262 add_assoc_long(array, "method", method);
1263 }
1264
1265 if (10 == (int) (obj->message->http.version * 10)) {
1266 add_assoc_long(array, "protocol", CURL_HTTP_VERSION_1_0);
1267 }
1268
1269 MAKE_STD_ZVAL(headers);
1270 array_init(headers);
1271 array_copy(&obj->message->hdrs, Z_ARRVAL_P(headers));
1272 add_assoc_zval(array, "headers", headers);
1273
1274 object_init_ex(return_value, http_request_object_ce);
1275 zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setoptions", NULL, array);
1276 zval_ptr_dtor(&array);
1277
1278 if (PHPSTR_VAL(obj->message) && PHPSTR_LEN(obj->message)) {
1279 phpstr_fix(PHPSTR(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 zval_dtor(&post);
1292 }
1293 }
1294 #else
1295 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
1296 #endif
1297 break;
1298 }
1299
1300 case HTTP_MSG_RESPONSE:
1301 {
1302 #ifndef WONKY
1303 HashPosition pos1, pos2;
1304 HashKey key = initHashKey(0);
1305 zval **header, **h, *body;
1306
1307 if (obj->message->http.info.response.code) {
1308 http_send_status(obj->message->http.info.response.code);
1309 }
1310
1311 object_init_ex(return_value, http_response_object_ce);
1312
1313 FOREACH_HASH_KEYVAL(pos1, &obj->message->hdrs, key, header) {
1314 if (key.type == HASH_KEY_IS_STRING) {
1315 zval *zkey;
1316
1317 MAKE_STD_ZVAL(zkey);
1318 ZVAL_STRINGL(zkey, key.str, key.len - 1, 1);
1319
1320 switch (Z_TYPE_PP(header)) {
1321 case IS_ARRAY:
1322 case IS_OBJECT:
1323 FOREACH_HASH_VAL(pos2, HASH_OF(*header), h) {
1324 ZVAL_ADDREF(*h);
1325 zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, zkey, *h);
1326 zval_ptr_dtor(h);
1327 }
1328 break;
1329
1330 default:
1331 ZVAL_ADDREF(*header);
1332 zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, zkey, *header);
1333 zval_ptr_dtor(header);
1334 break;
1335 }
1336 zval_ptr_dtor(&zkey);
1337 }
1338 }
1339
1340 MAKE_STD_ZVAL(body);
1341 ZVAL_STRINGL(body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 1);
1342 zend_call_method_with_1_params(&return_value, http_response_object_ce, NULL, "setdata", NULL, body);
1343 zval_ptr_dtor(&body);
1344 #else
1345 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpResponse (need PHP 5.1+)");
1346 #endif
1347 break;
1348 }
1349
1350 default:
1351 http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type HttpMessage::TYPE_REQUEST nor HttpMessage::TYPE_RESPONSE");
1352 break;
1353 }
1354 }
1355 SET_EH_NORMAL();
1356 }
1357 /* }}} */
1358
1359 /* {{{ proto int HttpMessage::count()
1360 Implements Countable::count(). Returns the number of parent messages + 1. */
1361 PHP_METHOD(HttpMessage, count)
1362 {
1363 NO_ARGS {
1364 long i;
1365 getObject(http_message_object, obj);
1366
1367 http_message_count(i, obj->message);
1368 RETURN_LONG(i);
1369 }
1370 }
1371 /* }}} */
1372
1373 /* {{{ proto string HttpMessage::serialize()
1374 Implements Serializable::serialize(). Returns the serialized representation of the HttpMessage. */
1375 PHP_METHOD(HttpMessage, serialize)
1376 {
1377 NO_ARGS {
1378 char *string;
1379 size_t length;
1380 getObject(http_message_object, obj);
1381
1382 http_message_serialize(obj->message, &string, &length);
1383 RETURN_STRINGL(string, length, 0);
1384 }
1385 }
1386 /* }}} */
1387
1388 /* {{{ proto void HttpMessage::unserialize(string serialized)
1389 Implements Serializable::unserialize(). Re-constructs the HttpMessage based upon the serialized string. */
1390 PHP_METHOD(HttpMessage, unserialize)
1391 {
1392 int length;
1393 char *serialized;
1394 getObject(http_message_object, obj);
1395
1396 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &length)) {
1397 http_message *msg;
1398
1399 http_message_dtor(obj->message);
1400 if ((msg = http_message_parse_ex(obj->message, serialized, (size_t) length))) {
1401 obj->message = msg;
1402 } else {
1403 http_message_init(obj->message);
1404 http_error(HE_ERROR, HTTP_E_RUNTIME, "Could not unserialize HttpMessage");
1405 }
1406 }
1407 }
1408 /* }}} */
1409
1410 /* {{{ proto HttpMessage HttpMessage::detach(void)
1411 Returns a clone of an HttpMessage object detached from any parent messages. */
1412 PHP_METHOD(HttpMessage, detach)
1413 {
1414 http_info info;
1415 http_message *msg;
1416 getObject(http_message_object, obj);
1417
1418 NO_ARGS;
1419
1420 info.type = obj->message->type;
1421 memcpy(&HTTP_INFO(&info), &HTTP_INFO(obj->message), sizeof(struct http_info));
1422
1423 msg = http_message_new();
1424 http_message_set_info(msg, &info);
1425
1426 zend_hash_copy(&msg->hdrs, &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1427 phpstr_append(&msg->body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message));
1428
1429 RETVAL_OBJVAL(http_message_object_new_ex(Z_OBJCE_P(getThis()), msg, NULL), 0);
1430 }
1431 /* }}} */
1432
1433 /* {{{ proto void HttpMessage::prepend(HttpMessage message[, bool top = true])
1434 Prepends message(s) to the HTTP message. Throws HttpInvalidParamException if the message is located within the same message chain. */
1435 PHP_METHOD(HttpMessage, prepend)
1436 {
1437 zval *prepend;
1438 zend_bool top = 1;
1439
1440 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &prepend, http_message_object_ce, &top)) {
1441 http_message *msg[2];
1442 getObject(http_message_object, obj);
1443 getObjectEx(http_message_object, prepend_obj, prepend);
1444
1445 /* safety check */
1446 for (msg[0] = obj->message; msg[0]; msg[0] = msg[0]->parent) {
1447 for (msg[1] = prepend_obj->message; msg[1]; msg[1] = msg[1]->parent) {
1448 if (msg[0] == msg[1]) {
1449 http_error(HE_THROW, HTTP_E_INVALID_PARAM, "Cannot prepend a message located within the same message chain");
1450 return;
1451 }
1452 }
1453 }
1454
1455 http_message_object_prepend_ex(getThis(), prepend, top);
1456 }
1457 }
1458 /* }}} */
1459
1460 /* {{{ proto HttpMessage HttpMessage::reverse()
1461 Reorders the message chain in reverse order. Returns the most parent HttpMessage object. */
1462 PHP_METHOD(HttpMessage, reverse)
1463 {
1464 NO_ARGS {
1465 http_message_object_reverse(getThis(), return_value);
1466 }
1467 }
1468 /* }}} */
1469
1470 /* {{{ proto void HttpMessage::rewind(void)
1471 Implements Iterator::rewind(). */
1472 PHP_METHOD(HttpMessage, rewind)
1473 {
1474 NO_ARGS {
1475 getObject(http_message_object, obj);
1476
1477 if (obj->iterator) {
1478 zval_ptr_dtor(&obj->iterator);
1479 }
1480 ZVAL_ADDREF(getThis());
1481 obj->iterator = getThis();
1482 }
1483 }
1484 /* }}} */
1485
1486 /* {{{ proto bool HttpMessage::valid(void)
1487 Implements Iterator::valid(). */
1488 PHP_METHOD(HttpMessage, valid)
1489 {
1490 NO_ARGS {
1491 getObject(http_message_object, obj);
1492
1493 RETURN_BOOL(obj->iterator != NULL);
1494 }
1495 }
1496 /* }}} */
1497
1498 /* {{{ proto void HttpMessage::next(void)
1499 Implements Iterator::next(). */
1500 PHP_METHOD(HttpMessage, next)
1501 {
1502 NO_ARGS {
1503 getObject(http_message_object, obj);
1504 if (obj->iterator) {
1505 getObjectEx(http_message_object, itr, obj->iterator);
1506
1507 if (itr && itr->parent.handle) {
1508 zval *old = obj->iterator;
1509 MAKE_STD_ZVAL(obj->iterator);
1510 ZVAL_OBJVAL(obj->iterator, itr->parent, 1);
1511 zval_ptr_dtor(&old);
1512 } else {
1513 zval_ptr_dtor(&obj->iterator);
1514 obj->iterator = NULL;
1515 }
1516 }
1517 }
1518 }
1519 /* }}} */
1520
1521 /* {{{ proto int HttpMessage::key(void)
1522 Implements Iterator::key(). */
1523 PHP_METHOD(HttpMessage, key)
1524 {
1525 NO_ARGS {
1526 getObject(http_message_object, obj);
1527
1528 RETURN_LONG(obj->iterator ? obj->iterator->value.obj.handle:0);
1529 }
1530 }
1531 /* }}} */
1532
1533 /* {{{ proto HttpMessage HttpMessage::current(void)
1534 Implements Iterator::current(). */
1535 PHP_METHOD(HttpMessage, current)
1536 {
1537 NO_ARGS {
1538 getObject(http_message_object, obj);
1539
1540 if (obj->iterator) {
1541 RETURN_ZVAL(obj->iterator, 1, 0);
1542 }
1543 }
1544 }
1545 /* }}} */
1546
1547 #endif /* ZEND_ENGINE_2 */
1548
1549 /*
1550 * Local variables:
1551 * tab-width: 4
1552 * c-basic-offset: 4
1553 * End:
1554 * vim600: noet sw=4 ts=4 fdm=marker
1555 * vim<600: noet sw=4 ts=4
1556 */
1557