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
, setHeaders
, NULL
, ZEND_ACC_PUBLIC
)
47 PHP_ME(HttpMessage
, addHeaders
, NULL
, ZEND_ACC_PUBLIC
)
48 PHP_ME(HttpMessage
, getType
, NULL
, ZEND_ACC_PUBLIC
)
49 PHP_ME(HttpMessage
, setType
, NULL
, ZEND_ACC_PUBLIC
)
50 PHP_ME(HttpMessage
, getResponseCode
, NULL
, ZEND_ACC_PUBLIC
)
51 PHP_ME(HttpMessage
, setResponseCode
, NULL
, ZEND_ACC_PUBLIC
)
52 PHP_ME(HttpMessage
, getRequestMethod
, NULL
, ZEND_ACC_PUBLIC
)
53 PHP_ME(HttpMessage
, setRequestMethod
, NULL
, ZEND_ACC_PUBLIC
)
54 PHP_ME(HttpMessage
, getRequestUri
, NULL
, ZEND_ACC_PUBLIC
)
55 PHP_ME(HttpMessage
, setRequestUri
, NULL
, ZEND_ACC_PUBLIC
)
56 PHP_ME(HttpMessage
, getHttpVersion
, NULL
, ZEND_ACC_PUBLIC
)
57 PHP_ME(HttpMessage
, setHttpVersion
, NULL
, ZEND_ACC_PUBLIC
)
58 PHP_ME(HttpMessage
, getNestedMessage
, NULL
, ZEND_ACC_PUBLIC
)
59 PHP_ME(HttpMessage
, toString
, NULL
, ZEND_ACC_PUBLIC
)
61 ZEND_MALIAS(HttpMessage
, __toString
, toString
, NULL
, ZEND_ACC_PUBLIC
)
63 PHP_ME(HttpMessage
, fromString
, NULL
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
66 static zend_object_handlers http_message_object_handlers
;
68 void _http_message_object_init(INIT_FUNC_ARGS
)
70 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
72 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
73 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
74 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
76 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
77 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
78 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
81 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
83 return http_message_object_new_ex(ce
, NULL
);
86 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg TSRMLS_DC
)
89 http_message_object
*o
;
91 o
= ecalloc(1, sizeof(http_message_object
));
93 o
->message
= msg
? msg
: http_message_new();
95 ALLOC_HASHTABLE(OBJ_PROP(o
));
96 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
98 ov
.handle
= zend_objects_store_put(o
, (zend_objects_store_dtor_t
) zend_objects_destroy_object
, http_message_object_free
, NULL TSRMLS_CC
);
99 ov
.handlers
= &http_message_object_handlers
;
104 static inline void _http_message_object_declare_default_properties(TSRMLS_D
)
106 zend_class_entry
*ce
= http_message_object_ce
;
108 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
110 DCL_PROP(PROTECTED
, string
, body
, "");
112 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
113 DCL_PROP(PROTECTED
, string
, requestUri
, "");
114 DCL_PROP(PROTECTED
, long, responseCode
, 0);
116 DCL_PROP_N(PROTECTED
, httpVersion
);
117 DCL_PROP_N(PROTECTED
, headers
);
118 DCL_PROP_N(PROTECTED
, nestedMessage
);
121 static void _http_message_object_free(zend_object
*object TSRMLS_DC
)
123 http_message_object
*o
= (http_message_object
*) object
;
126 zend_hash_destroy(OBJ_PROP(o
));
127 FREE_HASHTABLE(OBJ_PROP(o
));
130 http_message_free(o
->message
);
132 zval_dtor(&o
->_tmp_property
);
136 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
138 getObjectEx(http_message_object
, obj
, object
);
139 http_message
*msg
= obj
->message
;
140 zval
*return_value
= &obj
->_tmp_property
;
142 if (!EG(scope
) || !instanceof_function(EG(scope
), obj
->zo
.ce TSRMLS_CC
)) {
143 zend_error(E_WARNING
, "Cannot access protected property %s::$%s", obj
->zo
.ce
->name
, Z_STRVAL_P(member
));
144 return EG(uninitialized_zval_ptr
);
147 zval_ptr_dtor(&return_value
);
150 fprintf(stderr
, "Reading property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member
), Z_STRLEN_P(member
), strlen(Z_STRVAL_P(member
)),
151 zend_get_hash_value(Z_STRVAL_P(member
), strlen(Z_STRVAL_P(member
)) + 1)
155 switch (zend_get_hash_value(Z_STRVAL_P(member
), strlen(Z_STRVAL_P(member
)) + 1))
157 case HTTP_MSG_PROPHASH_TYPE
:
158 RETVAL_LONG(msg
->type
);
161 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
168 case HTTP_MSG_REQUEST
:
169 RETVAL_DOUBLE(msg
->info
.request
.http_version
);
172 case HTTP_MSG_RESPONSE
:
173 RETVAL_DOUBLE(msg
->info
.response
.http_version
);
178 case HTTP_MSG_PROPHASH_BODY
:
179 phpstr_fix(PHPSTR(msg
));
180 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
183 case HTTP_MSG_PROPHASH_HEADERS
:
184 array_init(return_value
);
185 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
188 case HTTP_MSG_PROPHASH_NESTED_MESSAGE
:
190 Z_TYPE_P(return_value
) = IS_OBJECT
;
191 return_value
->value
.obj
= http_message_object_from_msg(msg
->nested
);
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 if (Z_LVAL_P(value
) != msg
->type
) {
248 if (msg
->type
== HTTP_MSG_REQUEST
) {
249 if (msg
->info
.request
.method
) {
250 efree(msg
->info
.request
.method
);
252 if (msg
->info
.request
.URI
) {
253 efree(msg
->info
.request
.URI
);
256 msg
->type
= Z_LVAL_P(value
);
257 if (msg
->type
== HTTP_MSG_REQUEST
) {
258 msg
->info
.request
.method
= NULL
;
259 msg
->info
.request
.URI
= NULL
;
265 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
268 case HTTP_MSG_REQUEST
:
269 msg
->info
.request
.http_version
= (float) Z_DVAL_P(value
);
272 case HTTP_MSG_RESPONSE
:
273 msg
->info
.response
.http_version
= (float) Z_DVAL_P(value
);
278 case HTTP_MSG_PROPHASH_BODY
:
279 phpstr_dtor(PHPSTR(msg
));
280 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(value
), Z_STRLEN_P(value
));
283 case HTTP_MSG_PROPHASH_HEADERS
:
284 zend_hash_clean(&msg
->hdrs
);
285 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(value
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
288 case HTTP_MSG_PROPHASH_NESTED_MESSAGE
:
291 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
292 if (msg
->type
== HTTP_MSG_REQUEST
) {
293 if (msg
->info
.request
.method
) {
294 efree(msg
->info
.request
.method
);
296 msg
->info
.request
.method
= estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
));
300 case HTTP_MSG_PROPHASH_REQUEST_URI
:
301 if (msg
->type
== HTTP_MSG_REQUEST
) {
302 if (msg
->info
.request
.URI
) {
303 efree(msg
->info
.request
.URI
);
305 msg
->info
.request
.URI
= estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
));
309 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
310 if (msg
->type
== HTTP_MSG_RESPONSE
) {
311 msg
->info
.response
.code
= Z_LVAL_P(value
);
319 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
322 getObjectEx(http_message_object
, obj
, object
);
323 http_message
*msg
= obj
->message
;
325 #define ASSOC_PROP(obj, ptype, name, val) \
330 Z_ARRVAL(array) = OBJ_PROP(obj); \
331 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
332 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
334 #define ASSOC_STRING(obj, name, val) ASSOC_STRINGL(obj, name, val, strlen(val))
335 #define ASSOC_STRINGL(obj, name, val, len) \
340 Z_ARRVAL(array) = OBJ_PROP(obj); \
341 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
342 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
345 zend_hash_clean(OBJ_PROP(obj
));
347 ASSOC_PROP(obj
, long, "type", msg
->type
);
348 ASSOC_STRINGL(obj
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
350 MAKE_STD_ZVAL(headers
);
353 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
354 ASSOC_PROP(obj
, zval
, "headers", headers
);
358 case HTTP_MSG_REQUEST
:
359 ASSOC_PROP(obj
, double, "httpVersion", msg
->info
.request
.http_version
);
360 ASSOC_PROP(obj
, long, "responseCode", 0);
361 ASSOC_STRING(obj
, "requestMethod", msg
->info
.request
.method
);
362 ASSOC_STRING(obj
, "requestUri", msg
->info
.request
.URI
);
365 case HTTP_MSG_RESPONSE
:
366 ASSOC_PROP(obj
, double, "httpVersion", msg
->info
.response
.http_version
);
367 ASSOC_PROP(obj
, long, "responseCode", msg
->info
.response
.code
);
368 ASSOC_STRING(obj
, "requestMethod", "");
369 ASSOC_STRING(obj
, "requestUri", "");
374 ASSOC_PROP(obj
, double, "httpVersion", 0.0);
375 ASSOC_PROP(obj
, long, "responseCode", 0);
376 ASSOC_STRING(obj
, "requestMethod", "");
377 ASSOC_STRING(obj
, "requestUri", "");
381 return OBJ_PROP(obj
);
384 #endif /* ZEND_ENGINE_2 */
391 * vim600: noet sw=4 ts=4 fdm=marker
392 * vim<600: noet sw=4 ts=4