244e29da292569359834051902169c13548f51e3
[m6w6/ext-http] / http_message_object.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
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 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "php.h"
24
25 #include "php_http.h"
26 #include "php_http_std_defs.h"
27 #include "php_http_message_object.h"
28
29 #ifdef ZEND_ENGINE_2
30
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);
39
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, toString, NULL, ZEND_ACC_PUBLIC)
57
58 ZEND_MALIAS(HttpMessage, __toString, toString, NULL, ZEND_ACC_PUBLIC)
59
60 PHP_ME(HttpMessage, fromString, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
61 {NULL, NULL, NULL}
62 };
63 static zend_object_handlers http_message_object_handlers;
64
65 void _http_message_object_init(INIT_FUNC_ARGS)
66 {
67 HTTP_REGISTER_CLASS_EX(HttpMessage, http_message_object, NULL, 0);
68
69 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE);
70 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST);
71 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE);
72
73 http_message_object_handlers.read_property = http_message_object_read_prop;
74 http_message_object_handlers.write_property = http_message_object_write_prop;
75 http_message_object_handlers.get_properties = http_message_object_get_props;
76 }
77
78 zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC)
79 {
80 return http_message_object_new_ex(ce, NULL);
81 }
82
83 zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message *msg TSRMLS_DC)
84 {
85 zend_object_value ov;
86 http_message_object *o;
87
88 o = ecalloc(1, sizeof(http_message_object));
89 o->zo.ce = ce;
90
91 MAKE_STD_ZVAL(o->nested);
92 Z_TYPE_P(o->nested) = IS_OBJECT;
93
94 if (msg) {
95 o->message = msg;
96 if (msg->nested) {
97 o->nested->value.obj = http_message_object_from_msg(msg->nested);
98 }
99 } else {
100 o->message = http_message_new();
101 }
102
103 ALLOC_HASHTABLE(OBJ_PROP(o));
104 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
105
106 ov.handle = zend_objects_store_put(o, (zend_objects_store_dtor_t) zend_objects_destroy_object, http_message_object_free, NULL TSRMLS_CC);
107 ov.handlers = &http_message_object_handlers;
108
109 return ov;
110 }
111
112 static inline void _http_message_object_declare_default_properties(TSRMLS_D)
113 {
114 zend_class_entry *ce = http_message_object_ce;
115
116 DCL_PROP(PROTECTED, long, type, HTTP_MSG_NONE);
117
118 DCL_PROP(PROTECTED, string, body, "");
119
120 DCL_PROP(PROTECTED, string, requestMethod, "");
121 DCL_PROP(PROTECTED, string, requestUri, "");
122 DCL_PROP(PROTECTED, long, responseCode, 0);
123
124 DCL_PROP_N(PROTECTED, httpVersion);
125 DCL_PROP_N(PROTECTED, headers);
126
127 DCL_PROP_N(PUBLIC, nestedMessage);
128 }
129
130 static void _http_message_object_free(zend_object *object TSRMLS_DC)
131 {
132 http_message_object *o = (http_message_object *) object;
133
134 if (OBJ_PROP(o)) {
135 zend_hash_destroy(OBJ_PROP(o));
136 FREE_HASHTABLE(OBJ_PROP(o));
137 }
138 if (o->message) {
139 http_message_dtor(o->message);
140 efree(o->message);
141 }
142 FREE_ZVAL(o->nested);
143 zval_dtor(&o->_tmp_property);
144 efree(o);
145 }
146
147 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC)
148 {
149 getObjectEx(http_message_object, obj, object);
150 http_message *msg = obj->message;
151 zval *return_value = &obj->_tmp_property;
152
153 /* public */
154 if (strcmp(Z_STRVAL_P(member), "nestedMessage")) {
155 if (!EG(scope) || !instanceof_function(EG(scope), obj->zo.ce TSRMLS_CC)) {
156 zend_error(E_WARNING, "Cannot access protected property %s::$%s", obj->zo.ce->name, Z_STRVAL_P(member));
157 return EG(uninitialized_zval_ptr);
158 }
159 }
160
161 zval_dtor(return_value);
162
163 #if 0
164 fprintf(stderr, "Reading property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member), Z_STRLEN_P(member), strlen(Z_STRVAL_P(member)),
165 zend_get_hash_value(Z_STRVAL_P(member), strlen(Z_STRVAL_P(member)) + 1)
166 );
167 #endif
168
169 switch (zend_get_hash_value(Z_STRVAL_P(member), strlen(Z_STRVAL_P(member)) + 1))
170 {
171 case HTTP_MSG_PROPHASH_TYPE:
172 RETVAL_LONG(msg->type);
173 break;
174
175 case HTTP_MSG_PROPHASH_HTTP_VERSION:
176 switch (msg->type)
177 {
178 case HTTP_MSG_NONE:
179 RETVAL_NULL();
180 break;
181
182 case HTTP_MSG_REQUEST:
183 RETVAL_DOUBLE(msg->info.request.http_version);
184 break;
185
186 case HTTP_MSG_RESPONSE:
187 RETVAL_DOUBLE(msg->info.response.http_version);
188 break;
189 }
190 break;
191
192 case HTTP_MSG_PROPHASH_BODY:
193 phpstr_fix(PHPSTR(msg));
194 RETVAL_PHPSTR(PHPSTR(msg), 0, 1);
195 break;
196
197 case HTTP_MSG_PROPHASH_HEADERS:
198 array_init(return_value);
199 zend_hash_copy(Z_ARRVAL_P(return_value), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
200 break;
201
202 case HTTP_MSG_PROPHASH_NESTED_MESSAGE:
203 RETVAL_NULL();
204 if (msg->nested) {
205 return obj->nested;
206 }
207 break;
208
209 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
210 if (msg->type == HTTP_MSG_REQUEST && msg->info.request.method) {
211 RETVAL_STRING(msg->info.request.method, 1);
212 } else {
213 RETVAL_NULL();
214 }
215 break;
216
217 case HTTP_MSG_PROPHASH_REQUEST_URI:
218 if (msg->type == HTTP_MSG_REQUEST && msg->info.request.URI) {
219 RETVAL_STRING(msg->info.request.URI, 1);
220 } else {
221 RETVAL_NULL();
222 }
223 break;
224
225 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
226 if (msg->type == HTTP_MSG_RESPONSE) {
227 RETVAL_LONG(msg->info.response.code);
228 } else {
229 RETVAL_NULL();
230 }
231 break;
232
233 default:
234 RETVAL_NULL();
235 break;
236 }
237
238 return return_value;
239 }
240
241 static void _http_message_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC)
242 {
243 getObjectEx(http_message_object, obj, object);
244 http_message *msg = obj->message;
245
246 /* public */
247 if (strcmp("nestedMessage", Z_STRVAL_P(member))) {
248 if (!EG(scope) || !instanceof_function(EG(scope), obj->zo.ce TSRMLS_CC)) {
249 zend_error(E_WARNING, "Cannot access protected property %s::$%s", obj->zo.ce->name, Z_STRVAL_P(member));
250 }
251 }
252
253 #if 0
254 fprintf(stderr, "Writing property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member), Z_STRLEN_P(member), strlen(Z_STRVAL_P(member)),
255 zend_get_hash_value(Z_STRVAL_P(member), strlen(Z_STRVAL_P(member)) + 1)
256 );
257 #endif
258
259 switch (zend_get_hash_value(Z_STRVAL_P(member), strlen(Z_STRVAL_P(member)) + 1))
260 {
261 case HTTP_MSG_PROPHASH_TYPE:
262 if (Z_LVAL_P(value) != msg->type) {
263 if (msg->type == HTTP_MSG_REQUEST) {
264 if (msg->info.request.method) {
265 efree(msg->info.request.method);
266 }
267 if (msg->info.request.URI) {
268 efree(msg->info.request.URI);
269 }
270 }
271 msg->type = Z_LVAL_P(value);
272 if (msg->type == HTTP_MSG_REQUEST) {
273 msg->info.request.method = NULL;
274 msg->info.request.URI = NULL;
275 }
276 }
277
278 break;
279
280 case HTTP_MSG_PROPHASH_HTTP_VERSION:
281 switch (msg->type)
282 {
283 case HTTP_MSG_REQUEST:
284 msg->info.request.http_version = (float) Z_DVAL_P(value);
285 break;
286
287 case HTTP_MSG_RESPONSE:
288 msg->info.response.http_version = (float) Z_DVAL_P(value);
289 break;
290 }
291 break;
292
293 case HTTP_MSG_PROPHASH_BODY:
294 phpstr_dtor(PHPSTR(msg));
295 phpstr_from_string_ex(PHPSTR(msg), Z_STRVAL_P(value), Z_STRLEN_P(value));
296 break;
297
298 case HTTP_MSG_PROPHASH_HEADERS:
299 zend_hash_clean(&msg->hdrs);
300 zend_hash_copy(&msg->hdrs, Z_ARRVAL_P(value), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
301 break;
302
303 case HTTP_MSG_PROPHASH_NESTED_MESSAGE:
304 zval_ptr_dtor(&obj->nested);
305 obj->nested->value.obj = value->value.obj;
306 zval_add_ref(&value);
307 break;
308
309 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
310 if (msg->type == HTTP_MSG_REQUEST) {
311 if (msg->info.request.method) {
312 efree(msg->info.request.method);
313 }
314 msg->info.request.method = estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value));
315 }
316 break;
317
318 case HTTP_MSG_PROPHASH_REQUEST_URI:
319 if (msg->type == HTTP_MSG_REQUEST) {
320 if (msg->info.request.URI) {
321 efree(msg->info.request.URI);
322 }
323 msg->info.request.URI = estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value));
324 }
325 break;
326
327 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
328 if (msg->type == HTTP_MSG_RESPONSE) {
329 msg->info.response.code = Z_LVAL_P(value);
330 }
331 break;
332 }
333 zval_dtor(value);
334 FREE_ZVAL(value);
335 }
336
337 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
338 {
339 zval *headers;
340 getObjectEx(http_message_object, obj, object);
341 http_message *msg = obj->message;
342
343 #define ASSOC_PROP(obj, ptype, name, val) \
344 { \
345 zval array; \
346 char *m_prop_name; \
347 int m_prop_len; \
348 Z_ARRVAL(array) = OBJ_PROP(obj); \
349 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
350 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
351 }
352 #define ASSOC_STRING(obj, name, val) ASSOC_STRINGL(obj, name, val, strlen(val))
353 #define ASSOC_STRINGL(obj, name, val, len) \
354 { \
355 zval array; \
356 char *m_prop_name; \
357 int m_prop_len; \
358 Z_ARRVAL(array) = OBJ_PROP(obj); \
359 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
360 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
361 }
362
363 zend_hash_clean(OBJ_PROP(obj));
364
365 ASSOC_PROP(obj, long, "type", msg->type);
366 ASSOC_STRINGL(obj, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg));
367
368 MAKE_STD_ZVAL(headers);
369 array_init(headers);
370
371 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
372 ASSOC_PROP(obj, zval, "headers", headers);
373
374 switch (msg->type)
375 {
376 case HTTP_MSG_REQUEST:
377 ASSOC_PROP(obj, double, "httpVersion", msg->info.request.http_version);
378 ASSOC_PROP(obj, long, "responseCode", 0);
379 ASSOC_STRING(obj, "requestMethod", msg->info.request.method);
380 ASSOC_STRING(obj, "requestUri", msg->info.request.URI);
381 break;
382
383 case HTTP_MSG_RESPONSE:
384 ASSOC_PROP(obj, double, "httpVersion", msg->info.response.http_version);
385 ASSOC_PROP(obj, long, "responseCode", msg->info.response.code);
386 ASSOC_STRING(obj, "requestMethod", "");
387 ASSOC_STRING(obj, "requestUri", "");
388 break;
389
390 case HTTP_MSG_NONE:
391 default:
392 ASSOC_PROP(obj, double, "httpVersion", 0.0);
393 ASSOC_PROP(obj, long, "responseCode", 0);
394 ASSOC_STRING(obj, "requestMethod", "");
395 ASSOC_STRING(obj, "requestUri", "");
396 break;
397 }
398
399 return OBJ_PROP(obj);
400 }
401
402 #endif /* ZEND_ENGINE_2 */
403
404 /*
405 * Local variables:
406 * tab-width: 4
407 * c-basic-offset: 4
408 * End:
409 * vim600: noet sw=4 ts=4 fdm=marker
410 * vim<600: noet sw=4 ts=4
411 */
412