2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 #include <ext/standard/php_lcg.h>
16 #include <zend_exceptions.h>
20 PHP_HTTP_API
void php_http_sleep(double s
)
22 #if defined(PHP_WIN32)
23 Sleep((DWORD
) PHP_HTTP_MSEC(s
));
24 #elif defined(HAVE_USLEEP)
25 usleep(PHP_HTTP_USEC(s
));
26 #elif defined(HAVE_NANOSLEEP)
27 struct timespec req
, rem
;
29 req
.tv_sec
= (time_t) s
;
30 req
.tv_nsec
= PHP_HTTP_NSEC(s
) % PHP_HTTP_NANOSEC
;
32 while (nanosleep(&req
, &rem
) && (errno
== EINTR
) && (PHP_HTTP_NSEC(rem
.tv_sec
) + rem
.tv_nsec
) > PHP_HTTP_NSEC(PHP_HTTP_DIFFSEC
))) {
33 req
.tv_sec
= rem
.tv_sec
;
34 req
.tv_nsec
= rem
.tv_nsec
;
37 struct timeval timeout
;
39 timeout
.tv_sec
= (time_t) s
;
40 timeout
.tv_usec
= PHP_HTTP_USEC(s
) % PHP_HTTP_MCROSEC
;
42 select(0, NULL
, NULL
, NULL
, &timeout
);
47 /* STRING UTILITIES */
49 int php_http_match(const char *haystack_str
, const char *needle_str
, int flags
)
53 if (!haystack_str
|| !needle_str
) {
57 if (flags
& PHP_HTTP_MATCH_FULL
) {
58 if (flags
& PHP_HTTP_MATCH_CASE
) {
59 result
= !strcmp(haystack_str
, needle_str
);
61 result
= !strcasecmp(haystack_str
, needle_str
);
64 char *found
, *haystack
= estrdup(haystack_str
), *needle
= estrdup(needle_str
);
66 if (flags
& PHP_HTTP_MATCH_CASE
) {
67 found
= zend_memnstr(haystack
, needle
, strlen(needle
), haystack
+strlen(haystack
));
69 found
= php_stristr(haystack
, needle
, strlen(haystack
), strlen(needle
));
73 if (!(flags
& PHP_HTTP_MATCH_WORD
)
74 || ( (found
== haystack
|| !PHP_HTTP_IS_CTYPE(alnum
, *(found
- 1)))
75 && (!*(found
+ strlen(needle
)) || !PHP_HTTP_IS_CTYPE(alnum
, *(found
+ strlen(needle
))))
89 char *php_http_pretty_key(char *key
, size_t key_len
, zend_bool uctitle
, zend_bool xhyphen
)
95 if ((wasalpha
= PHP_HTTP_IS_CTYPE(alpha
, key
[0]))) {
96 key
[0] = (char) (uctitle
? PHP_HTTP_TO_CTYPE(upper
, key
[0]) : PHP_HTTP_TO_CTYPE(lower
, key
[0]));
98 for (i
= 1; i
< key_len
; i
++) {
99 if (PHP_HTTP_IS_CTYPE(alpha
, key
[i
])) {
100 key
[i
] = (char) (((!wasalpha
) && uctitle
) ? PHP_HTTP_TO_CTYPE(upper
, key
[i
]) : PHP_HTTP_TO_CTYPE(lower
, key
[i
]));
103 if (xhyphen
&& (key
[i
] == '_')) {
114 size_t php_http_boundary(char *buf
, size_t buf_len TSRMLS_DC
)
116 return snprintf(buf
, buf_len
, "%15.15F", PHP_HTTP_G
->env
.request
.time
* php_combined_lcg(TSRMLS_C
));
119 int php_http_select_str(const char *cmp
, int argc
, ...)
124 if (cmp
&& argc
> 0) {
127 va_start(argv
, argc
);
128 for (i
= 0; i
< argc
; ++i
) {
129 const char *test
= va_arg(argv
, const char *);
131 if (!strcasecmp(cmp
, test
)) {
145 PHP_HTTP_API
unsigned php_http_array_list(zval
*hash TSRMLS_DC
, unsigned argc
, ...)
147 HashTable
*ht
= HASH_OF(hash
);
152 va_start(argv
, argc
);
153 for ( zend_hash_internal_pointer_reset_ex(ht
, &pos
);
154 zend_hash_has_more_elements_ex(ht
, &pos
) && (argl
< argc
);
155 zend_hash_move_forward_ex(ht
, &pos
))
157 zval
**data
, ***argp
= (zval
***) va_arg(argv
, zval
***);
159 if (SUCCESS
== zend_hash_get_current_data_ex(ht
, (void *) &data
, &pos
)) {
169 int php_http_array_apply_append_func(void *pDest TSRMLS_DC
, int num_args
, va_list args
, zend_hash_key
*hash_key
)
174 zval
**data
= NULL
, **value
= (zval
**) pDest
;
176 dst
= va_arg(args
, HashTable
*);
177 flags
= va_arg(args
, int);
179 if ((!(flags
& ARRAY_JOIN_STRONLY
)) || hash_key
->nKeyLength
) {
180 if ((flags
& ARRAY_JOIN_PRETTIFY
) && hash_key
->nKeyLength
) {
181 key
= php_http_pretty_key(estrndup(hash_key
->arKey
, hash_key
->nKeyLength
- 1), hash_key
->nKeyLength
- 1, 1, 1);
182 zend_hash_find(dst
, key
, hash_key
->nKeyLength
, (void *) &data
);
184 zend_hash_quick_find(dst
, hash_key
->arKey
, hash_key
->nKeyLength
, hash_key
->h
, (void *) &data
);
189 if (Z_TYPE_PP(data
) != IS_ARRAY
) {
190 convert_to_array(*data
);
192 add_next_index_zval(*data
, *value
);
194 zend_symtable_update(dst
, key
, hash_key
->nKeyLength
, value
, sizeof(zval
*), NULL
);
196 zend_hash_quick_add(dst
, hash_key
->arKey
, hash_key
->nKeyLength
, hash_key
->h
, value
, sizeof(zval
*), NULL
);
204 return ZEND_HASH_APPLY_KEEP
;
207 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC
, int num_args
, va_list args
, zend_hash_key
*hash_key
)
212 zval
**value
= (zval
**) pDest
;
214 dst
= va_arg(args
, HashTable
*);
215 flags
= va_arg(args
, int);
217 if ((!(flags
& ARRAY_JOIN_STRONLY
)) || hash_key
->nKeyLength
) {
219 if ((flags
& ARRAY_JOIN_PRETTIFY
) && hash_key
->nKeyLength
) {
220 key
= php_http_pretty_key(estrndup(hash_key
->arKey
, hash_key
->nKeyLength
- 1), hash_key
->nKeyLength
- 1, 1, 1);
221 zend_hash_update(dst
, key
, hash_key
->nKeyLength
, (void *) value
, sizeof(zval
*), NULL
);
224 zend_hash_quick_update(dst
, hash_key
->arKey
, hash_key
->nKeyLength
, hash_key
->h
, (void *) value
, sizeof(zval
*), NULL
);
228 return ZEND_HASH_APPLY_KEEP
;
233 PHP_HTTP_API
size_t php_http_pass_wrapper(php_http_pass_callback_arg_t
*cb
, const char *str
, size_t len
)
236 return cb
->cb_zts(cb
->cb_arg
, str
, len TSRMLS_CC
);
239 PHP_HTTP_API
size_t php_http_pass_fcall_callback(void *cb_arg
, const char *str
, size_t len
)
241 php_http_pass_fcall_arg_t
*fcd
= cb_arg
;
243 TSRMLS_FETCH_FROM_CTX(fcd
->ts
);
245 MAKE_STD_ZVAL(zdata
);
246 ZVAL_STRINGL(zdata
, str
, len
, 1);
247 if (SUCCESS
== zend_fcall_info_argn(&fcd
->fci TSRMLS_CC
, 2, &fcd
->fcz
, &zdata
)) {
248 zend_fcall_info_call(&fcd
->fci
, &fcd
->fcc
, NULL
, NULL TSRMLS_CC
);
249 zend_fcall_info_args_clear(&fcd
->fci
, 0);
251 zval_ptr_dtor(&zdata
);
257 static inline int scope_error_handling(long type TSRMLS_DC
)
259 if ((type
== E_THROW
) || (EG(error_handling
) == EH_THROW
)) {
263 if (EG(This
) && instanceof_function(Z_OBJCE_P(EG(This
)), php_http_object_get_class_entry() TSRMLS_CC
)) {
264 return php_http_object_get_error_handling(EG(This
) TSRMLS_CC
);
270 void php_http_error(long type TSRMLS_DC
, long code
, const char *format
, ...)
274 va_start(args
, format
);
275 switch (scope_error_handling(type TSRMLS_CC
)) {
278 zend_class_entry
*ce
= php_http_exception_get_class_entry();
281 if (0&& EG(exception_class
) && instanceof_function(EG(exception_class
), ce TSRMLS_CC
)) {
282 ce
= EG(exception_class
);
285 vspprintf(&message
, 0, format
, args
);
286 zend_throw_exception(ce
, message
, code TSRMLS_CC
);
291 php_verror(NULL
, "", type
, format
, args TSRMLS_CC
);
304 * vim600: noet sw=4 ts=4 fdm=marker
305 * vim<600: noet sw=4 ts=4