iliaa Fri Feb 7 16:33:36 2003 EDT Modified files: /php4/main streams.c /php4/ext/standard url.c url.h basic_functions.c Log: Added get_browser() function. This function can be used to fetch the headers sent by the server when a request is made for a given URL. Index: php4/main/streams.c diff -u php4/main/streams.c:1.143 php4/main/streams.c:1.144 --- php4/main/streams.c:1.143 Thu Jan 30 16:06:34 2003 +++ php4/main/streams.c Fri Feb 7 16:33:35 2003 @@ -20,7 +20,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: streams.c,v 1.143 2003/01/30 21:06:34 sas Exp $ */ +/* $Id: streams.c,v 1.144 2003/02/07 21:33:35 iliaa Exp $ */ #define _GNU_SOURCE #include "php.h" @@ -2379,6 +2379,10 @@ path_to_open = path; wrapper = php_stream_locate_url_wrapper(path, &path_to_open, options TSRMLS_CC); + if (options & STREAM_USE_URL && (!wrapper || !wrapper->is_url)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "This function may only be +used against URLs."); + return NULL; + } if (wrapper) { Index: php4/ext/standard/url.c diff -u php4/ext/standard/url.c:1.62 php4/ext/standard/url.c:1.63 --- php4/ext/standard/url.c:1.62 Tue Dec 31 11:07:56 2002 +++ php4/ext/standard/url.c Fri Feb 7 16:33:35 2003 @@ -15,7 +15,7 @@ | Author: Jim Winstead <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ -/* $Id: url.c,v 1.62 2002/12/31 16:07:56 sebastian Exp $ */ +/* $Id: url.c,v 1.63 2003/02/07 21:33:35 iliaa Exp $ */ #include <stdlib.h> #include <string.h> @@ -549,6 +549,63 @@ } *dest = '\0'; return dest - str; +} +/* }}} */ + +/* {{{ proto array get_headers(string url) + fetches all the headers sent by the server in response to a HTTP request */ +PHP_FUNCTION(get_headers) +{ + char *url, *url_len; + php_stream_context *context = NULL; + php_stream *stream; + zval **prev_val, **hdr = NULL; + HashPosition pos; + long format = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &url, &url_len, +&format) == FAILURE) { + return; + } + + if (!(stream = php_stream_open_wrapper_ex(url, "r", REPORT_ERRORS | +STREAM_USE_URL | STREAM_ONLY_GET_HEADERS, NULL, context))) { + RETURN_FALSE; + } + + array_init(return_value); + + zend_hash_internal_pointer_reset_ex(HASH_OF(stream->wrapperdata), &pos); + while (zend_hash_get_current_data_ex(HASH_OF(stream->wrapperdata), +(void**)&hdr, &pos) != FAILURE) { + if (!format) { +no_name_header: + add_next_index_stringl(return_value, Z_STRVAL_PP(hdr), +Z_STRLEN_PP(hdr), 1); + } else { + char c; + char *s, *p; + + if ((p = strchr(Z_STRVAL_PP(hdr), ':'))) { + c = *p; + *p = '\0'; + s = p + 1; + while (isspace(*s)) { + s++; + } + + if (zend_hash_find(HASH_OF(return_value), +Z_STRVAL_PP(hdr), (p - Z_STRVAL_PP(hdr) + 1), (void **) &prev_val) == FAILURE) { + add_assoc_stringl_ex(return_value, +Z_STRVAL_PP(hdr), (p - Z_STRVAL_PP(hdr) + 1), s, (Z_STRLEN_PP(hdr) - (s - +Z_STRVAL_PP(hdr))), 1); + } else { /* some headers may occur more then once, +therefor we need to remake the string into an array */ + convert_to_array(*prev_val); + add_next_index_stringl(*prev_val, s, +(Z_STRLEN_PP(hdr) - (s - Z_STRVAL_PP(hdr))), 1); + } + + *p = c; + } else { + goto no_name_header; + } + } + zend_hash_move_forward_ex(HASH_OF(stream->wrapperdata), &pos); + } + + php_stream_close(stream); } /* }}} */ Index: php4/ext/standard/url.h diff -u php4/ext/standard/url.h:1.14 php4/ext/standard/url.h:1.15 --- php4/ext/standard/url.h:1.14 Tue Dec 31 11:07:56 2002 +++ php4/ext/standard/url.h Fri Feb 7 16:33:35 2003 @@ -15,7 +15,7 @@ | Author: Jim Winstead <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ -/* $Id: url.h,v 1.14 2002/12/31 16:07:56 sebastian Exp $ */ +/* $Id: url.h,v 1.15 2003/02/07 21:33:35 iliaa Exp $ */ #ifndef URL_H #define URL_H @@ -43,6 +43,7 @@ PHP_FUNCTION(urldecode); PHP_FUNCTION(rawurlencode); PHP_FUNCTION(rawurldecode); +PHP_FUNCTION(get_headers); #endif /* URL_H */ Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.571 php4/ext/standard/basic_functions.c:1.572 --- php4/ext/standard/basic_functions.c:1.571 Wed Feb 5 12:56:08 2003 +++ php4/ext/standard/basic_functions.c Fri Feb 7 16:33:35 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.571 2003/02/05 17:56:08 iliaa Exp $ */ +/* $Id: basic_functions.c,v 1.572 2003/02/07 21:33:35 iliaa Exp $ */ #include "php.h" #include "php_streams.h" @@ -682,6 +682,7 @@ PHP_FE(stream_get_meta_data, NULL) PHP_FE(stream_register_wrapper, NULL) PHP_FE(stream_get_wrappers, NULL) + PHP_FE(get_headers, + NULL) #if HAVE_SYS_TIME_H || defined(PHP_WIN32) PHP_FE(stream_set_timeout, NULL)
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php