client test and fixes
[m6w6/ext-http] / php_http_misc.h
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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #ifndef PHP_HTTP_MISC_H
14 #define PHP_HTTP_MISC_H
15
16 /* DEFAULTS */
17
18 /* DATE FORMAT RFC1123 */
19 #define PHP_HTTP_DATE_FORMAT "D, d M Y H:i:s \\G\\M\\T"
20
21 /* CR LF */
22 #define PHP_HTTP_CRLF "\r\n"
23
24 /* max URL length */
25 #define PHP_HTTP_URL_MAXLEN 4096
26
27 /* def URL arg separator */
28 #define PHP_HTTP_URL_ARGSEP "&"
29
30 /* send buffer size */
31 #define PHP_HTTP_SENDBUF_SIZE 40960
32
33 /* SLEEP */
34
35 #define PHP_HTTP_DIFFSEC (0.001)
36 #define PHP_HTTP_MLLISEC (1000)
37 #define PHP_HTTP_MCROSEC (1000 * 1000)
38 #define PHP_HTTP_NANOSEC (1000 * 1000 * 1000)
39 #define PHP_HTTP_MSEC(s) ((long)(s * PHP_HTTP_MLLISEC))
40 #define PHP_HTTP_USEC(s) ((long)(s * PHP_HTTP_MCROSEC))
41 #define PHP_HTTP_NSEC(s) ((long)(s * PHP_HTTP_NANOSEC))
42
43 PHP_HTTP_API void php_http_sleep(double s);
44
45 /* STRING UTILITIES */
46
47 #define PHP_HTTP_CHECK_CONTENT_TYPE(ct, action) \
48 if (!strchr((ct), '/')) { \
49 php_http_error(HE_WARNING, PHP_HTTP_E_INVALID_PARAM, \
50 "Content type \"%s\" does not seem to contain a primary and a secondary part", (ct)); \
51 action; \
52 }
53
54
55 #ifndef STR_SET
56 # define STR_SET(STR, SET) \
57 { \
58 STR_FREE(STR); \
59 STR = SET; \
60 }
61 #endif
62
63 #define STR_PTR(s) (s?s:"")
64
65 #define lenof(S) (sizeof(S) - 1)
66
67 #define PHP_HTTP_MATCH_LOOSE 0
68 #define PHP_HTTP_MATCH_CASE 0x01
69 #define PHP_HTTP_MATCH_WORD 0x10
70 #define PHP_HTTP_MATCH_FULL 0x20
71 #define PHP_HTTP_MATCH_STRICT (PHP_HTTP_MATCH_CASE|PHP_HTTP_MATCH_FULL)
72
73 int php_http_match(const char *haystack, const char *needle, int flags);
74 char *php_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen);
75 size_t php_http_boundary(char *buf, size_t len TSRMLS_DC);
76 int php_http_select_str(const char *cmp, int argc, ...);
77
78 static inline const char *php_http_locate_str(const char *h, size_t h_len, const char *n, size_t n_len)
79 {
80 const char *p, *e;
81
82 if (n_len && h_len) {
83 e = h + h_len;
84 do {
85 if (*h == *n) {
86 for (p = n; *p == h[p-n]; ++p) {
87 if (p == n+n_len-1) {
88 return h;
89 }
90 }
91 }
92 } while (h++ != e);
93 }
94
95 return NULL;
96 }
97
98 static inline const char *php_http_locate_eol(const char *line, int *eol_len)
99 {
100 const char *eol = strpbrk(line, "\r\n");
101
102 if (eol_len) {
103 *eol_len = eol ? ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1) : 0;
104 }
105 return eol;
106 }
107
108 static inline const char *php_http_locate_bin_eol(const char *bin, size_t len, int *eol_len)
109 {
110 const char *eol;
111
112 for (eol = bin; eol - bin < len; ++eol) {
113 if (*eol == '\r' || *eol == '\n') {
114 if (eol_len) {
115 *eol_len = ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1);
116 }
117 return eol;
118 }
119 }
120
121 return NULL;
122 }
123
124 /* ZEND */
125
126 #if PHP_VERSION_ID < 50400
127 # define object_properties_init(o, ce) zend_hash_copy(((zend_object *) o)->properties, &(ce->default_properties), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*))
128 # define PHP_HTTP_ZEND_LITERAL_DC
129 # define PHP_HTTP_ZEND_LITERAL_CC
130 # define PHP_HTTP_ZEND_LITERAL_CCN
131 # define ZVAL_COPY_VALUE(zv, arr) do { \
132 (zv)->value = (arr)->value; \
133 Z_TYPE_P(zv) = Z_TYPE_P(arr); \
134 } while (0)
135 #else
136 # define PHP_HTTP_ZEND_LITERAL_DC , const zend_literal *literal_key
137 # define PHP_HTTP_ZEND_LITERAL_CC , (literal_key)
138 # define PHP_HTTP_ZEND_LITERAL_CCN , NULL
139 #endif
140
141 #define INIT_PZVAL_ARRAY(zv, ht) \
142 { \
143 INIT_PZVAL((zv)); \
144 Z_TYPE_P(zv) = IS_ARRAY; \
145 Z_ARRVAL_P(zv) = (ht); \
146 }
147
148 static inline zval *php_http_ztyp(int type, zval *z)
149 {
150 SEPARATE_ARG_IF_REF(z);
151 if (Z_TYPE_P(z) != type) {
152 switch (type) {
153 case IS_NULL: convert_to_null_ex(&z); break;
154 case IS_BOOL: convert_to_boolean_ex(&z); break;
155 case IS_LONG: convert_to_long_ex(&z); break;
156 case IS_DOUBLE: convert_to_double_ex(&z); break;
157 case IS_STRING: convert_to_string_ex(&z); break;
158 case IS_ARRAY: convert_to_array_ex(&z); break;
159 case IS_OBJECT: convert_to_object_ex(&z); break;
160 }
161 }
162 return z;
163 }
164
165 static inline zval *php_http_zsep(zend_bool add_ref, int type, zval *z)
166 {
167 if (add_ref) {
168 Z_ADDREF_P(z);
169 }
170 if (Z_TYPE_P(z) != type) {
171 switch (type) {
172 case IS_NULL: convert_to_null_ex(&z); break;
173 case IS_BOOL: convert_to_boolean_ex(&z); break;
174 case IS_LONG: convert_to_long_ex(&z); break;
175 case IS_DOUBLE: convert_to_double_ex(&z); break;
176 case IS_STRING: convert_to_string_ex(&z); break;
177 case IS_ARRAY: convert_to_array_ex(&z); break;
178 case IS_OBJECT: convert_to_object_ex(&z); break;
179 }
180 } else {
181 SEPARATE_ZVAL_IF_NOT_REF(&z);
182 }
183 return z;
184 }
185
186 static inline STATUS php_http_ini_entry(const char *name_str, size_t name_len, const char **value_str, size_t *value_len, zend_bool orig TSRMLS_DC)
187 {
188 zend_ini_entry *ini_entry;
189
190 if (SUCCESS == zend_hash_find(EG(ini_directives), name_str, name_len + 1, (void *) &ini_entry)) {
191 if (orig && ini_entry->modified) {
192 *value_str = ini_entry->orig_value;
193 *value_len = (size_t) ini_entry->orig_value_length;
194 } else {
195 *value_str = ini_entry->value;
196 *value_len = (size_t) ini_entry->value_length;
197 }
198 return SUCCESS;
199 }
200 return FAILURE;
201 }
202
203 STATUS php_http_method_call(zval *object, const char *method_str, size_t method_len, int argc, zval **argv[], zval **retval_ptr TSRMLS_DC);
204
205 /* return bool (v == SUCCESS) */
206 #define RETVAL_SUCCESS(v) RETVAL_BOOL(SUCCESS == (v))
207 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
208 /* return object(values) */
209 #define RETVAL_OBJECT(o, addref) \
210 RETVAL_OBJVAL((o)->value.obj, addref)
211 #define RETURN_OBJECT(o, addref) \
212 RETVAL_OBJECT(o, addref); \
213 return
214 #define RETVAL_OBJVAL(ov, addref) \
215 ZVAL_OBJVAL(return_value, ov, addref)
216 #define RETURN_OBJVAL(ov, addref) \
217 RETVAL_OBJVAL(ov, addref); \
218 return
219 #define ZVAL_OBJVAL(zv, ov, addref) \
220 (zv)->type = IS_OBJECT; \
221 (zv)->value.obj = (ov);\
222 if (addref && Z_OBJ_HT_P(zv)->add_ref) { \
223 Z_OBJ_HT_P(zv)->add_ref((zv) TSRMLS_CC); \
224 }
225 /* return property */
226 #define RETVAL_PROP(CE, n) RETVAL_PROP_EX(CE, getThis(), n)
227 #define RETURN_PROP(CE, n) RETURN_PROP_EX(CE, getThis(), n)
228 #define RETVAL_PROP_EX(CE, this, n) \
229 { \
230 zval *__prop = zend_read_property(CE, this, ZEND_STRL(n), 0 TSRMLS_CC); \
231 RETVAL_ZVAL(__prop, 1, 0); \
232 }
233 #define RETURN_PROP_EX(CE, this, n) \
234 { \
235 zval *__prop = zend_read_property(CE, this, ZEND_STRL(n), 0 TSRMLS_CC); \
236 RETURN_ZVAL(__prop, 1, 0); \
237 }
238 #define RETVAL_SPROP(CE, n) \
239 { \
240 zval *__prop = zend_read_static_property(CE, ZEND_STRL(n), 0 TSRMLS_CC); \
241 RETVAL_ZVAL(__prop, 1, 0); \
242 }
243 #define RETURN_SPROP(CE, n) \
244 { \
245 zval *__prop = zend_read_static_property(CE, ZEND_STRL(n), 0 TSRMLS_CC); \
246 RETURN_ZVAL(__prop, 1, 0); \
247 }
248
249 #define Z_OBJ_DELREF(z) \
250 if (Z_OBJ_HT(z)->del_ref) { \
251 Z_OBJ_HT(z)->del_ref(&(z) TSRMLS_CC); \
252 }
253 #define Z_OBJ_ADDREF(z) \
254 if (Z_OBJ_HT(z)->add_ref) { \
255 Z_OBJ_HT(z)->add_ref(&(z) TSRMLS_CC); \
256 }
257 #define Z_OBJ_DELREF_P(z) \
258 if (Z_OBJ_HT_P(z)->del_ref) { \
259 Z_OBJ_HT_P(z)->del_ref((z) TSRMLS_CC); \
260 }
261 #define Z_OBJ_ADDREF_P(z) \
262 if (Z_OBJ_HT_P(z)->add_ref) { \
263 Z_OBJ_HT_P(z)->add_ref((z) TSRMLS_CC); \
264 }
265 #define Z_OBJ_DELREF_PP(z) \
266 if (Z_OBJ_HT_PP(z)->del_ref) { \
267 Z_OBJ_HT_PP(z)->del_ref(*(z) TSRMLS_CC); \
268 }
269 #define Z_OBJ_ADDREF_PP(z) \
270 if (Z_OBJ_HT_PP(z)->add_ref) { \
271 Z_OBJ_HT_PP(z)->add_ref(*(z) TSRMLS_CC); \
272 }
273
274 #define PHP_HTTP_BEGIN_ARGS_EX(class, method, ret_ref, req_args) ZEND_BEGIN_ARG_INFO_EX(args_for_ ##class## _ ##method , 0, ret_ref, req_args)
275 #define PHP_HTTP_BEGIN_ARGS_AR(class, method, ret_ref, req_args) ZEND_BEGIN_ARG_INFO_EX(args_for_ ##class## _ ##method , 1, ret_ref, req_args)
276 #define PHP_HTTP_END_ARGS }
277 #define PHP_HTTP_EMPTY_ARGS_EX(class, method, ret_ref) PHP_HTTP_BEGIN_ARGS_EX(class, method, ret_ref, 0) PHP_HTTP_END_ARGS
278 #define PHP_HTTP_ARGS(class, method) args_for_ ##class## _ ##method
279 #define PHP_HTTP_ARG_VAL(name, pass_ref) ZEND_ARG_INFO(pass_ref, name)
280 #define PHP_HTTP_ARG_OBJ(class, name, allow_null) ZEND_ARG_OBJ_INFO(0, name, class, allow_null)
281 #define PHP_HTTP_ARG_ARR(name, allow_null, pass_ref) ZEND_ARG_ARRAY_INFO(pass_ref, name, allow_null)
282
283 #define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
284
285 #define PHP_MINIT_CALL(func) PHP_MINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
286 #define PHP_RINIT_CALL(func) PHP_RINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
287 #define PHP_MSHUTDOWN_CALL(func) PHP_MSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
288 #define PHP_RSHUTDOWN_CALL(func) PHP_RSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
289
290
291 #define PHP_HTTP_INI_ENTRY(entry, default, scope, updater, global) \
292 STD_PHP_INI_ENTRY(entry, default, scope, updater, global, zend_php_http_globals, php_http_globals)
293 #define PHP_HTTP_INI_ENTRY_EX(entry, default, scope, updater, displayer, global) \
294 STD_PHP_INI_ENTRY_EX(entry, default, scope, updater, global, zend_php_http_globals, php_http_globals, displayer)
295
296 #define PHP_HTTP_REGISTER_CLASS(ns, classname, name, parent, flags) \
297 { \
298 zend_class_entry ce; \
299 memset(&ce, 0, sizeof(zend_class_entry)); \
300 INIT_NS_CLASS_ENTRY(ce, #ns, #classname, php_ ##name## _method_entry); \
301 php_ ##name## _class_entry = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
302 php_ ##name## _class_entry->ce_flags |= flags; \
303 php_http_register_class(php_ ##name## _get_class_entry); \
304 }
305
306 #define PHP_HTTP_REGISTER_INTERFACE(ns, ifacename, name, flags) \
307 { \
308 zend_class_entry ce; \
309 memset(&ce, 0, sizeof(zend_class_entry)); \
310 INIT_NS_CLASS_ENTRY(ce, #ns, #ifacename, php_ ##name## _method_entry); \
311 php_ ##name## _class_entry = zend_register_internal_interface(&ce TSRMLS_CC); \
312 php_ ##name## _class_entry->ce_flags |= flags; \
313 php_http_register_class(php_ ##name## _get_class_entry); \
314 }
315
316 #define PHP_HTTP_REGISTER_EXCEPTION(classname, cename, parent) \
317 { \
318 zend_class_entry ce; \
319 memset(&ce, 0, sizeof(zend_class_entry)); \
320 INIT_NS_CLASS_ENTRY(ce, "http", #classname, NULL); \
321 ce.create_object = NULL; \
322 cename = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
323 }
324
325 /* ARRAYS */
326 PHP_HTTP_API unsigned php_http_array_list(HashTable *ht TSRMLS_DC, unsigned argc, ...);
327
328 typedef struct php_http_array_hashkey {
329 char *str;
330 uint len;
331 ulong num;
332 uint dup:1;
333 uint type:31;
334 } php_http_array_hashkey_t;
335 #define php_http_array_hashkey_init(dup) {NULL, 0, 0, (dup), 0}
336
337 static inline void php_http_array_hashkey_stringify(php_http_array_hashkey_t *key)
338 {
339 if (key->type != HASH_KEY_IS_STRING) {
340 key->len = spprintf(&key->str, 0, "%lu", key->num) + 1;
341 }
342 }
343
344 static inline void php_http_array_hashkey_stringfree(php_http_array_hashkey_t *key)
345 {
346 if (key->type != HASH_KEY_IS_STRING || key->dup) {
347 STR_FREE(key->str);
348 }
349 }
350
351 #define FOREACH_VAL(pos, array, val) FOREACH_HASH_VAL(pos, HASH_OF(array), val)
352 #define FOREACH_HASH_VAL(pos, hash, val) \
353 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
354 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
355 zend_hash_move_forward_ex(hash, &pos))
356
357 #define FOREACH_KEY(pos, array, key) FOREACH_HASH_KEY(pos, HASH_OF(array), key)
358 #define FOREACH_HASH_KEY(pos, hash, _key) \
359 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
360 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT; \
361 zend_hash_move_forward_ex(hash, &pos)) \
362
363 #define FOREACH_KEYVAL(pos, array, key, val) FOREACH_HASH_KEYVAL(pos, HASH_OF(array), key, val)
364 #define FOREACH_HASH_KEYVAL(pos, hash, _key, val) \
365 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
366 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT && \
367 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
368 zend_hash_move_forward_ex(hash, &pos))
369
370 #define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
371 #define ARRAY_JOIN_STRONLY 1
372 #define ARRAY_JOIN_PRETTIFY 2
373 #define array_join(src, dst, append, flags) zend_hash_apply_with_arguments(src TSRMLS_CC, (append)?php_http_array_apply_append_func:php_http_array_apply_merge_func, 2, dst, (int)flags)
374
375 int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
376 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
377
378 /* PASS CALLBACK */
379
380 typedef size_t (*php_http_pass_callback_t)(void *cb_arg, const char *str, size_t len);
381 typedef size_t (*php_http_pass_php_http_buffer_callback_t)(void *cb_arg, php_http_buffer_t *str);
382 typedef size_t (*php_http_pass_format_callback_t)(void *cb_arg, const char *fmt, ...);
383
384 typedef struct php_http_pass_fcall_arg {
385 zval *fcz;
386 zend_fcall_info fci;
387 zend_fcall_info_cache fcc;
388 #ifdef ZTS
389 void ***ts;
390 #endif
391 } php_http_pass_fcall_arg_t;
392
393 PHP_HTTP_API size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len);
394
395 /* ERROR */
396
397 extern void php_http_error(long type TSRMLS_DC, long code, const char *format, ...);
398
399 #define with_error_handling(eh, ec) \
400 { \
401 zend_error_handling __eh; \
402 zend_replace_error_handling((eh), (ec), &__eh TSRMLS_CC);
403
404 #define end_error_handling() \
405 zend_restore_error_handling(&__eh TSRMLS_CC); \
406 }
407
408 #ifndef E_THROW
409 # define E_THROW -1
410 #endif
411 #define HE_THROW E_THROW TSRMLS_CC
412 #define HE_NOTICE E_NOTICE TSRMLS_CC
413 #define HE_WARNING E_WARNING TSRMLS_CC
414 #define HE_ERROR E_ERROR TSRMLS_CC
415
416 typedef enum php_http_error {
417 PHP_HTTP_E_UNKNOWN = 0,
418 PHP_HTTP_E_RUNTIME,
419 PHP_HTTP_E_INVALID_PARAM,
420 PHP_HTTP_E_HEADER,
421 PHP_HTTP_E_MALFORMED_HEADERS,
422 PHP_HTTP_E_REQUEST_METHOD,
423 PHP_HTTP_E_MESSAGE,
424 PHP_HTTP_E_MESSAGE_TYPE,
425 PHP_HTTP_E_MESSAGE_BODY,
426 PHP_HTTP_E_ENCODING,
427 PHP_HTTP_E_CLIENT,
428 PHP_HTTP_E_CLIENT_POOL,
429 PHP_HTTP_E_CLIENT_DATASHARE,
430 PHP_HTTP_E_REQUEST_FACTORY,
431 PHP_HTTP_E_SOCKET,
432 PHP_HTTP_E_RESPONSE,
433 PHP_HTTP_E_URL,
434 PHP_HTTP_E_QUERYSTRING,
435 PHP_HTTP_E_COOKIE,
436 } php_http_error_t;
437
438 #endif
439 /*
440 * Local variables:
441 * tab-width: 4
442 * c-basic-offset: 4
443 * End:
444 * vim600: noet sw=4 ts=4 fdm=marker
445 * vim<600: noet sw=4 ts=4
446 */