add stream support for http\Env\Response
[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 /* return bool (v == SUCCESS) */
204 #define RETVAL_SUCCESS(v) RETVAL_BOOL(SUCCESS == (v))
205 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
206 /* return object(values) */
207 #define RETVAL_OBJECT(o, addref) \
208 RETVAL_OBJVAL((o)->value.obj, addref)
209 #define RETURN_OBJECT(o, addref) \
210 RETVAL_OBJECT(o, addref); \
211 return
212 #define RETVAL_OBJVAL(ov, addref) \
213 ZVAL_OBJVAL(return_value, ov, addref)
214 #define RETURN_OBJVAL(ov, addref) \
215 RETVAL_OBJVAL(ov, addref); \
216 return
217 #define ZVAL_OBJVAL(zv, ov, addref) \
218 (zv)->type = IS_OBJECT; \
219 (zv)->value.obj = (ov);\
220 if (addref && Z_OBJ_HT_P(zv)->add_ref) { \
221 Z_OBJ_HT_P(zv)->add_ref((zv) TSRMLS_CC); \
222 }
223 /* return property */
224 #define RETVAL_PROP(CE, n) RETVAL_PROP_EX(CE, getThis(), n)
225 #define RETURN_PROP(CE, n) RETURN_PROP_EX(CE, getThis(), n)
226 #define RETVAL_PROP_EX(CE, this, n) \
227 { \
228 zval *__prop = zend_read_property(CE, this, ZEND_STRL(n), 0 TSRMLS_CC); \
229 RETVAL_ZVAL(__prop, 1, 0); \
230 }
231 #define RETURN_PROP_EX(CE, this, n) \
232 { \
233 zval *__prop = zend_read_property(CE, this, ZEND_STRL(n), 0 TSRMLS_CC); \
234 RETURN_ZVAL(__prop, 1, 0); \
235 }
236 #define RETVAL_SPROP(CE, n) \
237 { \
238 zval *__prop = zend_read_static_property(CE, ZEND_STRL(n), 0 TSRMLS_CC); \
239 RETVAL_ZVAL(__prop, 1, 0); \
240 }
241 #define RETURN_SPROP(CE, n) \
242 { \
243 zval *__prop = zend_read_static_property(CE, ZEND_STRL(n), 0 TSRMLS_CC); \
244 RETURN_ZVAL(__prop, 1, 0); \
245 }
246
247 #define Z_OBJ_DELREF(z) \
248 if (Z_OBJ_HT(z)->del_ref) { \
249 Z_OBJ_HT(z)->del_ref(&(z) TSRMLS_CC); \
250 }
251 #define Z_OBJ_ADDREF(z) \
252 if (Z_OBJ_HT(z)->add_ref) { \
253 Z_OBJ_HT(z)->add_ref(&(z) TSRMLS_CC); \
254 }
255 #define Z_OBJ_DELREF_P(z) \
256 if (Z_OBJ_HT_P(z)->del_ref) { \
257 Z_OBJ_HT_P(z)->del_ref((z) TSRMLS_CC); \
258 }
259 #define Z_OBJ_ADDREF_P(z) \
260 if (Z_OBJ_HT_P(z)->add_ref) { \
261 Z_OBJ_HT_P(z)->add_ref((z) TSRMLS_CC); \
262 }
263 #define Z_OBJ_DELREF_PP(z) \
264 if (Z_OBJ_HT_PP(z)->del_ref) { \
265 Z_OBJ_HT_PP(z)->del_ref(*(z) TSRMLS_CC); \
266 }
267 #define Z_OBJ_ADDREF_PP(z) \
268 if (Z_OBJ_HT_PP(z)->add_ref) { \
269 Z_OBJ_HT_PP(z)->add_ref(*(z) TSRMLS_CC); \
270 }
271
272 #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)
273 #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)
274 #define PHP_HTTP_END_ARGS }
275 #define PHP_HTTP_EMPTY_ARGS_EX(class, method, ret_ref) PHP_HTTP_BEGIN_ARGS_EX(class, method, ret_ref, 0) PHP_HTTP_END_ARGS
276 #define PHP_HTTP_ARGS(class, method) args_for_ ##class## _ ##method
277 #define PHP_HTTP_ARG_VAL(name, pass_ref) ZEND_ARG_INFO(pass_ref, name)
278 #define PHP_HTTP_ARG_OBJ(class, name, allow_null) ZEND_ARG_OBJ_INFO(0, name, class, allow_null)
279 #define PHP_HTTP_ARG_ARR(name, allow_null, pass_ref) ZEND_ARG_ARRAY_INFO(pass_ref, name, allow_null)
280
281 #define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
282
283 #define PHP_MINIT_CALL(func) PHP_MINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
284 #define PHP_RINIT_CALL(func) PHP_RINIT(func)(INIT_FUNC_ARGS_PASSTHRU)
285 #define PHP_MSHUTDOWN_CALL(func) PHP_MSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
286 #define PHP_RSHUTDOWN_CALL(func) PHP_RSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU)
287
288
289 #define PHP_HTTP_INI_ENTRY(entry, default, scope, updater, global) \
290 STD_PHP_INI_ENTRY(entry, default, scope, updater, global, zend_php_http_globals, php_http_globals)
291 #define PHP_HTTP_INI_ENTRY_EX(entry, default, scope, updater, displayer, global) \
292 STD_PHP_INI_ENTRY_EX(entry, default, scope, updater, global, zend_php_http_globals, php_http_globals, displayer)
293
294 #define PHP_HTTP_REGISTER_CLASS(ns, classname, name, parent, flags) \
295 { \
296 zend_class_entry ce; \
297 memset(&ce, 0, sizeof(zend_class_entry)); \
298 INIT_NS_CLASS_ENTRY(ce, #ns, #classname, php_ ##name## _method_entry); \
299 php_ ##name## _class_entry = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
300 php_ ##name## _class_entry->ce_flags |= flags; \
301 php_http_register_class(php_ ##name## _get_class_entry); \
302 }
303
304 #define PHP_HTTP_REGISTER_INTERFACE(ns, ifacename, name, flags) \
305 { \
306 zend_class_entry ce; \
307 memset(&ce, 0, sizeof(zend_class_entry)); \
308 INIT_NS_CLASS_ENTRY(ce, #ns, #ifacename, php_ ##name## _method_entry); \
309 php_ ##name## _class_entry = zend_register_internal_interface(&ce TSRMLS_CC); \
310 php_ ##name## _class_entry->ce_flags |= flags; \
311 php_http_register_class(php_ ##name## _get_class_entry); \
312 }
313
314 #define PHP_HTTP_REGISTER_EXCEPTION(classname, cename, parent) \
315 { \
316 zend_class_entry ce; \
317 memset(&ce, 0, sizeof(zend_class_entry)); \
318 INIT_NS_CLASS_ENTRY(ce, "http", #classname, NULL); \
319 ce.create_object = NULL; \
320 cename = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
321 }
322
323 /* ARRAYS */
324 PHP_HTTP_API unsigned php_http_array_list(HashTable *ht TSRMLS_DC, unsigned argc, ...);
325
326 typedef struct php_http_array_hashkey {
327 char *str;
328 uint len;
329 ulong num;
330 uint dup:1;
331 uint type:31;
332 } php_http_array_hashkey_t;
333 #define php_http_array_hashkey_init(dup) {NULL, 0, 0, (dup), 0}
334
335 static inline void php_http_array_hashkey_stringify(php_http_array_hashkey_t *key)
336 {
337 if (key->type != HASH_KEY_IS_STRING) {
338 key->len = spprintf(&key->str, 0, "%lu", key->num) + 1;
339 }
340 }
341
342 static inline void php_http_array_hashkey_stringfree(php_http_array_hashkey_t *key)
343 {
344 if (key->type != HASH_KEY_IS_STRING || key->dup) {
345 STR_FREE(key->str);
346 }
347 }
348
349 #define FOREACH_VAL(pos, array, val) FOREACH_HASH_VAL(pos, HASH_OF(array), val)
350 #define FOREACH_HASH_VAL(pos, hash, val) \
351 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
352 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
353 zend_hash_move_forward_ex(hash, &pos))
354
355 #define FOREACH_KEY(pos, array, key) FOREACH_HASH_KEY(pos, HASH_OF(array), key)
356 #define FOREACH_HASH_KEY(pos, hash, _key) \
357 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
358 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT; \
359 zend_hash_move_forward_ex(hash, &pos)) \
360
361 #define FOREACH_KEYVAL(pos, array, key, val) FOREACH_HASH_KEYVAL(pos, HASH_OF(array), key, val)
362 #define FOREACH_HASH_KEYVAL(pos, hash, _key, val) \
363 for ( zend_hash_internal_pointer_reset_ex(hash, &pos); \
364 ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT && \
365 zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
366 zend_hash_move_forward_ex(hash, &pos))
367
368 #define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
369 #define ARRAY_JOIN_STRONLY 1
370 #define ARRAY_JOIN_PRETTIFY 2
371 #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)
372
373 int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
374 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
375
376 /* PASS CALLBACK */
377
378 typedef size_t (*php_http_pass_callback_t)(void *cb_arg, const char *str, size_t len);
379 typedef size_t (*php_http_pass_php_http_buffer_callback_t)(void *cb_arg, php_http_buffer_t *str);
380 typedef size_t (*php_http_pass_format_callback_t)(void *cb_arg, const char *fmt, ...);
381
382 typedef struct php_http_pass_fcall_arg {
383 zval *fcz;
384 zend_fcall_info fci;
385 zend_fcall_info_cache fcc;
386 #ifdef ZTS
387 void ***ts;
388 #endif
389 } php_http_pass_fcall_arg_t;
390
391 PHP_HTTP_API size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len);
392
393 /* ERROR */
394
395 extern void php_http_error(long type TSRMLS_DC, long code, const char *format, ...);
396
397 #define with_error_handling(eh, ec) \
398 { \
399 zend_error_handling __eh; \
400 zend_replace_error_handling((eh), (ec), &__eh TSRMLS_CC);
401
402 #define end_error_handling() \
403 zend_restore_error_handling(&__eh TSRMLS_CC); \
404 }
405
406 #ifndef E_THROW
407 # define E_THROW -1
408 #endif
409 #define HE_THROW E_THROW TSRMLS_CC
410 #define HE_NOTICE E_NOTICE TSRMLS_CC
411 #define HE_WARNING E_WARNING TSRMLS_CC
412 #define HE_ERROR E_ERROR TSRMLS_CC
413
414 typedef enum php_http_error {
415 PHP_HTTP_E_UNKNOWN = 0,
416 PHP_HTTP_E_RUNTIME,
417 PHP_HTTP_E_INVALID_PARAM,
418 PHP_HTTP_E_HEADER,
419 PHP_HTTP_E_MALFORMED_HEADERS,
420 PHP_HTTP_E_REQUEST_METHOD,
421 PHP_HTTP_E_MESSAGE,
422 PHP_HTTP_E_MESSAGE_TYPE,
423 PHP_HTTP_E_MESSAGE_BODY,
424 PHP_HTTP_E_ENCODING,
425 PHP_HTTP_E_CLIENT,
426 PHP_HTTP_E_CLIENT_POOL,
427 PHP_HTTP_E_CLIENT_DATASHARE,
428 PHP_HTTP_E_REQUEST_FACTORY,
429 PHP_HTTP_E_SOCKET,
430 PHP_HTTP_E_RESPONSE,
431 PHP_HTTP_E_URL,
432 PHP_HTTP_E_QUERYSTRING,
433 PHP_HTTP_E_COOKIE,
434 } php_http_error_t;
435
436 #endif
437 /*
438 * Local variables:
439 * tab-width: 4
440 * c-basic-offset: 4
441 * End:
442 * vim600: noet sw=4 ts=4 fdm=marker
443 * vim<600: noet sw=4 ts=4
444 */