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