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