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