2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
26 #include "php_http_std_defs.h"
27 #include "php_http_message_object.h"
31 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
32 static inline void _http_message_object_declare_default_properties(TSRMLS_D
);
33 #define http_message_object_read_prop _http_message_object_read_prop
34 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
35 #define http_message_object_write_prop _http_message_object_write_prop
36 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
37 #define http_message_object_get_props _http_message_object_get_props
38 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
40 zend_class_entry
*http_message_object_ce
;
41 zend_function_entry http_message_object_fe
[] = {
42 PHP_ME(HttpMessage
, __construct
, NULL
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
43 PHP_ME(HttpMessage
, setRaw
, NULL
, ZEND_ACC_PUBLIC
)
44 PHP_ME(HttpMessage
, getBody
, NULL
, ZEND_ACC_PUBLIC
)
45 PHP_ME(HttpMessage
, getHeaders
, NULL
, ZEND_ACC_PUBLIC
)
46 PHP_ME(HttpMessage
, getType
, NULL
, ZEND_ACC_PUBLIC
)
47 PHP_ME(HttpMessage
, getResponseCode
, NULL
, ZEND_ACC_PUBLIC
)
48 PHP_ME(HttpMessage
, getRequestMethod
, NULL
, ZEND_ACC_PUBLIC
)
49 PHP_ME(HttpMessage
, getRequestUri
, NULL
, ZEND_ACC_PUBLIC
)
50 PHP_ME(HttpMessage
, getHttpVersion
, NULL
, ZEND_ACC_PUBLIC
)
51 PHP_ME(HttpMessage
, toString
, NULL
, ZEND_ACC_PUBLIC
)
53 ZEND_MALIAS(HttpMessage
, __toString
, toString
, NULL
, ZEND_ACC_PUBLIC
)
55 PHP_ME(HttpMessage
, fromString
, NULL
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
58 static zend_object_handlers http_message_object_handlers
;
60 void _http_message_object_init(INIT_FUNC_ARGS
)
62 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
64 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
65 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
66 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
68 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
69 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
70 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
73 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
75 return http_message_object_new_ex(ce
, NULL
);
78 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg TSRMLS_DC
)
81 http_message_object
*o
;
83 o
= ecalloc(1, sizeof(http_message_object
));
85 o
->message
= msg
? msg
: http_message_new();
87 ALLOC_HASHTABLE(OBJ_PROP(o
));
88 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
90 ov
.handle
= zend_objects_store_put(o
, (zend_objects_store_dtor_t
) zend_objects_destroy_object
, http_message_object_free
, NULL TSRMLS_CC
);
91 ov
.handlers
= &http_message_object_handlers
;
96 static inline void _http_message_object_declare_default_properties(TSRMLS_D
)
98 zend_class_entry
*ce
= http_message_object_ce
;
100 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
102 DCL_PROP(PROTECTED
, string
, raw
, "");
103 DCL_PROP(PROTECTED
, string
, body
, "");
105 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
106 DCL_PROP(PROTECTED
, string
, requestUri
, "");
107 DCL_PROP(PROTECTED
, long, responseCode
, 0);
109 DCL_PROP_N(PROTECTED
, httpVersion
);
110 DCL_PROP_N(PROTECTED
, headers
);
111 DCL_PROP_N(PROTECTED
, nestedMessage
);
114 static void _http_message_object_free(zend_object
*object TSRMLS_DC
)
116 http_message_object
*o
= (http_message_object
*) object
;
119 zend_hash_destroy(OBJ_PROP(o
));
120 FREE_HASHTABLE(OBJ_PROP(o
));
123 http_message_free(o
->message
);
125 zval_dtor(&o
->_tmp_property
);
129 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
131 getObjectEx(http_message_object
, obj
, object
);
132 http_message
*msg
= obj
->message
;
133 zval
*return_value
= &obj
->_tmp_property
;
135 if (!EG(scope
) || !instanceof_function(EG(scope
), obj
->zo
.ce TSRMLS_CC
)) {
136 zend_error(E_WARNING
, "Cannot access protected property %s::$%s", obj
->zo
.ce
->name
, Z_STRVAL_P(member
));
137 return EG(uninitialized_zval_ptr
);
140 zval_dtor(return_value
);
143 fprintf(stderr
, "Reading property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member
), Z_STRLEN_P(member
), strlen(Z_STRVAL_P(member
)),
144 zend_get_hash_value(Z_STRVAL_P(member
), strlen(Z_STRVAL_P(member
)) + 1)
148 switch (zend_get_hash_value(Z_STRVAL_P(member
), strlen(Z_STRVAL_P(member
)) + 1))
150 case HTTP_MSG_PROPHASH_TYPE
:
151 RETVAL_LONG(msg
->type
);
154 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
161 case HTTP_MSG_REQUEST
:
162 RETVAL_DOUBLE(msg
->info
.request
.http_version
);
165 case HTTP_MSG_RESPONSE
:
166 RETVAL_DOUBLE(msg
->info
.response
.http_version
);
171 case HTTP_MSG_PROPHASH_RAW
:
174 RETVAL_STRINGL(msg
->raw
, msg
->len
, 1);
176 RETVAL_STRINGL("", 0, 1);
183 case HTTP_MSG_PROPHASH_BODY
:
184 phpstr_fix(PHPSTR(msg
));
185 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
188 case HTTP_MSG_PROPHASH_HEADERS
:
189 array_init(return_value
);
190 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
193 case HTTP_MSG_PROPHASH_NESTED_MESSAGE
:
197 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
198 if (msg
->type
== HTTP_MSG_REQUEST
&& msg
->info
.request
.method
) {
199 RETVAL_STRING(msg
->info
.request
.method
, 1);
205 case HTTP_MSG_PROPHASH_REQUEST_URI
:
206 if (msg
->type
== HTTP_MSG_REQUEST
&& msg
->info
.request
.URI
) {
207 RETVAL_STRING(msg
->info
.request
.URI
, 1);
213 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
214 if (msg
->type
== HTTP_MSG_RESPONSE
) {
215 RETVAL_LONG(msg
->info
.response
.code
);
229 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
231 getObjectEx(http_message_object
, obj
, object
);
232 http_message
*msg
= obj
->message
;
234 if (!EG(scope
) || !instanceof_function(EG(scope
), obj
->zo
.ce TSRMLS_CC
)) {
235 zend_error(E_WARNING
, "Cannot access protected property %s::$%s", obj
->zo
.ce
->name
, Z_STRVAL_P(member
));
239 fprintf(stderr
, "Writing property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member
), Z_STRLEN_P(member
), strlen(Z_STRVAL_P(member
)),
240 zend_get_hash_value(Z_STRVAL_P(member
), strlen(Z_STRVAL_P(member
)) + 1)
244 switch (zend_get_hash_value(Z_STRVAL_P(member
), strlen(Z_STRVAL_P(member
)) + 1))
246 case HTTP_MSG_PROPHASH_TYPE
:
247 msg
->type
= Z_LVAL_P(value
);
250 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
253 case HTTP_MSG_REQUEST
:
254 msg
->info
.request
.http_version
= (float) Z_DVAL_P(value
);
257 case HTTP_MSG_RESPONSE
:
258 msg
->info
.response
.http_version
= (float) Z_DVAL_P(value
);
263 case HTTP_MSG_PROPHASH_RAW
:
264 http_message_dtor(msg
);
265 http_message_parse_ex(msg
, Z_STRVAL_P(value
), Z_STRLEN_P(value
), 1);
268 case HTTP_MSG_PROPHASH_BODY
:
269 phpstr_dtor(PHPSTR(msg
));
270 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(value
), Z_STRLEN_P(value
));
273 case HTTP_MSG_PROPHASH_HEADERS
:
274 zend_hash_clean(&msg
->hdrs
);
275 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(value
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
278 case HTTP_MSG_PROPHASH_NESTED_MESSAGE
:
281 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
282 if (msg
->type
== HTTP_MSG_REQUEST
) {
283 if (msg
->info
.request
.method
) {
284 efree(msg
->info
.request
.method
);
286 msg
->info
.request
.method
= estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
));
290 case HTTP_MSG_PROPHASH_REQUEST_URI
:
291 if (msg
->type
== HTTP_MSG_REQUEST
) {
292 if (msg
->info
.request
.URI
) {
293 efree(msg
->info
.request
.URI
);
295 msg
->info
.request
.URI
= estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
));
299 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
300 if (msg
->type
== HTTP_MSG_RESPONSE
) {
301 msg
->info
.response
.code
= Z_LVAL_P(value
);
307 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
310 getObjectEx(http_message_object
, obj
, object
);
311 http_message
*msg
= obj
->message
;
313 #define ASSOC_PROP(obj, ptype, name, val) \
318 Z_ARRVAL(array) = OBJ_PROP(obj); \
319 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
320 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
322 #define ASSOC_STRING(obj, name, val) ASSOC_STRINGL(obj, name, val, strlen(val))
323 #define ASSOC_STRINGL(obj, name, val, len) \
328 Z_ARRVAL(array) = OBJ_PROP(obj); \
329 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
330 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
333 zend_hash_clean(OBJ_PROP(obj
));
335 ASSOC_PROP(obj
, long, "type", msg
->type
);
336 ASSOC_STRINGL(obj
, "raw", msg
->raw
, msg
->len
)
337 ASSOC_STRINGL(obj
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
339 MAKE_STD_ZVAL(headers
);
342 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
343 ASSOC_PROP(obj
, zval
, "headers", headers
);
347 case HTTP_MSG_REQUEST
:
348 ASSOC_PROP(obj
, double, "httpVersion", msg
->info
.request
.http_version
);
349 ASSOC_PROP(obj
, long, "responseCode", 0);
350 ASSOC_STRING(obj
, "requestMethod", msg
->info
.request
.method
);
351 ASSOC_STRING(obj
, "requestUri", msg
->info
.request
.URI
);
354 case HTTP_MSG_RESPONSE
:
355 ASSOC_PROP(obj
, double, "httpVersion", msg
->info
.response
.http_version
);
356 ASSOC_PROP(obj
, long, "responseCode", msg
->info
.response
.code
);
357 ASSOC_STRING(obj
, "requestMethod", "");
358 ASSOC_STRING(obj
, "requestUri", "");
363 ASSOC_PROP(obj
, double, "httpVersion", 0.0);
364 ASSOC_PROP(obj
, long, "responseCode", 0);
365 ASSOC_STRING(obj
, "requestMethod", "");
366 ASSOC_STRING(obj
, "requestUri", "");
370 return OBJ_PROP(obj
);
373 #endif /* ZEND_ENGINE_2 */
380 * vim600: noet sw=4 ts=4 fdm=marker
381 * vim<600: noet sw=4 ts=4