]> granicus.if.org Git - php/commitdiff
Added get_browser() function. This function can be used to fetch the headers
authorIlia Alshanetsky <iliaa@php.net>
Fri, 7 Feb 2003 21:33:35 +0000 (21:33 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Fri, 7 Feb 2003 21:33:35 +0000 (21:33 +0000)
sent by the server when a request is made for a given URL.

ext/standard/basic_functions.c
ext/standard/url.c
ext/standard/url.h
main/streams.c

index 448326294e6fd8c711d4f23892049d2a031b8495..855453764555e1f105d509af97425c83c11cd55a 100644 (file)
@@ -682,6 +682,7 @@ function_entry basic_functions[] = {
        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)
index 8752a6a201b1a07e3b5c26ba6b7be54d08e0d98c..8bd2a99d00a60454cb7903bf23ba07a2f4aa0bd8 100644 (file)
@@ -552,6 +552,63 @@ PHPAPI int php_raw_url_decode(char *str, int len)
 }
 /* }}} */
 
+/* {{{ 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);
+}
+/* }}} */
+
 /*
  * Local variables:
  * tab-width: 4
index 0fce2214842a04642324253d2ccf53540a5175d1..2a3b980be05d7c73041b432f9fd797f55586d462 100644 (file)
@@ -43,6 +43,7 @@ PHP_FUNCTION(urlencode);
 PHP_FUNCTION(urldecode);
 PHP_FUNCTION(rawurlencode);
 PHP_FUNCTION(rawurldecode);
+PHP_FUNCTION(get_headers);
 
 #endif /* URL_H */
 
index a1fa323df4f547f957288ac103984dcdb89d3bca..5ed18c6d1106b318ced4e2e15e58a3af50250ac3 100755 (executable)
@@ -2379,6 +2379,10 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(char *path, char *mode, int optio
        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)    {