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
, getBody
, NULL
, ZEND_ACC_PUBLIC
)
43 PHP_ME(HttpMessage
, getHeaders
, NULL
, ZEND_ACC_PUBLIC
)
44 PHP_ME(HttpMessage
, setHeaders
, NULL
, ZEND_ACC_PUBLIC
)
45 PHP_ME(HttpMessage
, addHeaders
, NULL
, ZEND_ACC_PUBLIC
)
46 PHP_ME(HttpMessage
, getType
, NULL
, ZEND_ACC_PUBLIC
)
47 PHP_ME(HttpMessage
, setType
, NULL
, ZEND_ACC_PUBLIC
)
48 PHP_ME(HttpMessage
, getResponseCode
, NULL
, ZEND_ACC_PUBLIC
)
49 PHP_ME(HttpMessage
, setResponseCode
, NULL
, ZEND_ACC_PUBLIC
)
50 PHP_ME(HttpMessage
, getRequestMethod
, NULL
, ZEND_ACC_PUBLIC
)
51 PHP_ME(HttpMessage
, setRequestMethod
, NULL
, ZEND_ACC_PUBLIC
)
52 PHP_ME(HttpMessage
, getRequestUri
, NULL
, ZEND_ACC_PUBLIC
)
53 PHP_ME(HttpMessage
, setRequestUri
, NULL
, ZEND_ACC_PUBLIC
)
54 PHP_ME(HttpMessage
, getHttpVersion
, NULL
, ZEND_ACC_PUBLIC
)
55 PHP_ME(HttpMessage
, setHttpVersion
, NULL
, ZEND_ACC_PUBLIC
)
56 PHP_ME(HttpMessage
, getNestedMessage
, NULL
, ZEND_ACC_PUBLIC
)
57 PHP_ME(HttpMessage
, toString
, NULL
, ZEND_ACC_PUBLIC
)
59 ZEND_MALIAS(HttpMessage
, __toString
, toString
, NULL
, ZEND_ACC_PUBLIC
)
61 PHP_ME(HttpMessage
, fromString
, NULL
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
64 static zend_object_handlers http_message_object_handlers
;
66 void _http_message_object_init(INIT_FUNC_ARGS
)
68 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
70 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
71 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
72 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
74 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
75 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
76 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
79 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
81 return http_message_object_new_ex(ce
, NULL
);
84 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg TSRMLS_DC
)
87 http_message_object
*o
;
89 o
= ecalloc(1, sizeof(http_message_object
));
96 o
->nested
= http_message_object_from_msg(msg
->nested
);
100 ALLOC_HASHTABLE(OBJ_PROP(o
));
101 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
103 ov
.handle
= zend_objects_store_put(o
, (zend_objects_store_dtor_t
) zend_objects_destroy_object
, http_message_object_free
, NULL TSRMLS_CC
);
104 ov
.handlers
= &http_message_object_handlers
;
109 static inline void _http_message_object_declare_default_properties(TSRMLS_D
)
111 zend_class_entry
*ce
= http_message_object_ce
;
113 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
114 DCL_PROP(PROTECTED
, string
, body
, "");
115 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
116 DCL_PROP(PROTECTED
, string
, requestUri
, "");
117 DCL_PROP(PROTECTED
, long, responseCode
, 0);
118 DCL_PROP_N(PROTECTED
, httpVersion
);
119 DCL_PROP_N(PROTECTED
, headers
);
121 DCL_PROP_N(PUBLIC
, nestedMessage
);
124 static void _http_message_object_free(zend_object
*object TSRMLS_DC
)
126 http_message_object
*o
= (http_message_object
*) object
;
129 zend_hash_destroy(OBJ_PROP(o
));
130 FREE_HASHTABLE(OBJ_PROP(o
));
133 http_message_dtor(o
->message
);
139 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
141 getObjectEx(http_message_object
, obj
, object
);
142 http_message
*msg
= obj
->message
;
146 ALLOC_ZVAL(return_value
);
147 return_value
->refcount
= 0;
149 if (!EG(scope
) || !instanceof_function(EG(scope
), obj
->zo
.ce TSRMLS_CC
)) {
150 zend_error(E_WARNING
, "Cannot access protected property %s::$%s", obj
->zo
.ce
->name
, Z_STRVAL_P(member
));
151 return EG(uninitialized_zval_ptr
);
154 zval_dtor(return_value
);
157 fprintf(stderr
, "Reading property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member
), Z_STRLEN_P(member
), strlen(Z_STRVAL_P(member
)),
158 zend_get_hash_value(Z_STRVAL_P(member
), strlen(Z_STRVAL_P(member
)) + 1)
162 switch (zend_get_hash_value(Z_STRVAL_P(member
), strlen(Z_STRVAL_P(member
)) + 1))
164 case HTTP_MSG_PROPHASH_TYPE
:
165 RETVAL_LONG(msg
->type
);
168 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
175 case HTTP_MSG_REQUEST
:
176 RETVAL_DOUBLE(msg
->info
.request
.http_version
);
179 case HTTP_MSG_RESPONSE
:
180 RETVAL_DOUBLE(msg
->info
.response
.http_version
);
185 case HTTP_MSG_PROPHASH_BODY
:
186 phpstr_fix(PHPSTR(msg
));
187 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
190 case HTTP_MSG_PROPHASH_HEADERS
:
191 array_init(return_value
);
192 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
195 case HTTP_MSG_PROPHASH_NESTED_MESSAGE
:
197 Z_TYPE_P(return_value
) = IS_OBJECT
;
198 return_value
->value
.obj
= obj
->nested
;
199 zend_objects_store_add_ref(return_value TSRMLS_CC
);
205 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
206 if (msg
->type
== HTTP_MSG_REQUEST
&& msg
->info
.request
.method
) {
207 RETVAL_STRING(msg
->info
.request
.method
, 1);
213 case HTTP_MSG_PROPHASH_REQUEST_URI
:
214 if (msg
->type
== HTTP_MSG_REQUEST
&& msg
->info
.request
.URI
) {
215 RETVAL_STRING(msg
->info
.request
.URI
, 1);
221 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
222 if (msg
->type
== HTTP_MSG_RESPONSE
) {
223 RETVAL_LONG(msg
->info
.response
.code
);
237 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
239 getObjectEx(http_message_object
, obj
, object
);
240 http_message
*msg
= obj
->message
;
242 if (!EG(scope
) || !instanceof_function(EG(scope
), obj
->zo
.ce TSRMLS_CC
)) {
243 zend_error(E_WARNING
, "Cannot access protected property %s::$%s", obj
->zo
.ce
->name
, Z_STRVAL_P(member
));
247 fprintf(stderr
, "Writing property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member
), Z_STRLEN_P(member
), strlen(Z_STRVAL_P(member
)),
248 zend_get_hash_value(Z_STRVAL_P(member
), strlen(Z_STRVAL_P(member
)) + 1)
252 switch (zend_get_hash_value(Z_STRVAL_P(member
), strlen(Z_STRVAL_P(member
)) + 1))
254 case HTTP_MSG_PROPHASH_TYPE
:
255 if (Z_LVAL_P(value
) != msg
->type
) {
256 if (msg
->type
== HTTP_MSG_REQUEST
) {
257 if (msg
->info
.request
.method
) {
258 efree(msg
->info
.request
.method
);
260 if (msg
->info
.request
.URI
) {
261 efree(msg
->info
.request
.URI
);
264 msg
->type
= Z_LVAL_P(value
);
265 if (msg
->type
== HTTP_MSG_REQUEST
) {
266 msg
->info
.request
.method
= NULL
;
267 msg
->info
.request
.URI
= NULL
;
273 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
276 case HTTP_MSG_REQUEST
:
277 msg
->info
.request
.http_version
= (float) Z_DVAL_P(value
);
280 case HTTP_MSG_RESPONSE
:
281 msg
->info
.response
.http_version
= (float) Z_DVAL_P(value
);
286 case HTTP_MSG_PROPHASH_BODY
:
287 phpstr_dtor(PHPSTR(msg
));
288 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(value
), Z_STRLEN_P(value
));
291 case HTTP_MSG_PROPHASH_HEADERS
:
292 zend_hash_clean(&msg
->hdrs
);
293 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(value
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
296 case HTTP_MSG_PROPHASH_NESTED_MESSAGE
:
299 tmp
.value
.obj
= obj
->nested
;
300 zend_objects_store_del_ref(&tmp TSRMLS_CC
);
302 zend_objects_store_add_ref(value TSRMLS_CC
);
303 obj
->nested
= value
->value
.obj
;
306 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
307 if (msg
->type
== HTTP_MSG_REQUEST
) {
308 if (msg
->info
.request
.method
) {
309 efree(msg
->info
.request
.method
);
311 msg
->info
.request
.method
= estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
));
315 case HTTP_MSG_PROPHASH_REQUEST_URI
:
316 if (msg
->type
== HTTP_MSG_REQUEST
) {
317 if (msg
->info
.request
.URI
) {
318 efree(msg
->info
.request
.URI
);
320 msg
->info
.request
.URI
= estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
));
324 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
325 if (msg
->type
== HTTP_MSG_RESPONSE
) {
326 msg
->info
.response
.code
= Z_LVAL_P(value
);
332 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
335 getObjectEx(http_message_object
, obj
, object
);
336 http_message
*msg
= obj
->message
;
338 #define ASSOC_PROP(obj, ptype, name, val) \
343 Z_ARRVAL(array) = OBJ_PROP(obj); \
344 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
345 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
347 #define ASSOC_STRING(obj, name, val) ASSOC_STRINGL(obj, name, val, strlen(val))
348 #define ASSOC_STRINGL(obj, name, val, len) \
353 Z_ARRVAL(array) = OBJ_PROP(obj); \
354 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
355 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
358 zend_hash_clean(OBJ_PROP(obj
));
360 ASSOC_PROP(obj
, long, "type", msg
->type
);
361 ASSOC_STRINGL(obj
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
363 MAKE_STD_ZVAL(headers
);
366 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
367 ASSOC_PROP(obj
, zval
, "headers", headers
);
371 case HTTP_MSG_REQUEST
:
372 ASSOC_PROP(obj
, double, "httpVersion", msg
->info
.request
.http_version
);
373 ASSOC_PROP(obj
, long, "responseCode", 0);
374 ASSOC_STRING(obj
, "requestMethod", msg
->info
.request
.method
);
375 ASSOC_STRING(obj
, "requestUri", msg
->info
.request
.URI
);
378 case HTTP_MSG_RESPONSE
:
379 ASSOC_PROP(obj
, double, "httpVersion", msg
->info
.response
.http_version
);
380 ASSOC_PROP(obj
, long, "responseCode", msg
->info
.response
.code
);
381 ASSOC_STRING(obj
, "requestMethod", "");
382 ASSOC_STRING(obj
, "requestUri", "");
387 ASSOC_PROP(obj
, double, "httpVersion", 0.0);
388 ASSOC_PROP(obj
, long, "responseCode", 0);
389 ASSOC_STRING(obj
, "requestMethod", "");
390 ASSOC_STRING(obj
, "requestUri", "");
394 return OBJ_PROP(obj
);
397 #endif /* ZEND_ENGINE_2 */
404 * vim600: noet sw=4 ts=4 fdm=marker
405 * vim<600: noet sw=4 ts=4