Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » PHP
  • » [PHP-DEV] get_headers patch [RSS Feed]

#1 Nov. 25, 2005 04:07:59

Arnar M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] get_headers patch


This is a update of the get_headers patch i sent in a long time ago and wasignored. Patch is written with the latest cvs snapshot of 5.1. Hope it makes itway to 5.1.1Info:

mixed get_headers ( string url [, bool format | string header ])

2nd arg can eather be bool or string
bool:
true returns an numeric indexed array with headers
false returns key index array with header name as key
string:
returns only the header with the same name.

passing long will cast E_STRICT but still work the old way

3nd arg is a context to pass header/data to the url.Index: ext/standard/url.c
===================================================================
RCS file: /repository/php-src/ext/standard/url.c,v
retrieving revision 1.86.2.1
diff -u -r1.86.2.1 url.c
--- ext/standard/url.c 16 Aug 2005 14:20:41 -0000 1.86.2.1
+++ ext/standard/url.c 25 Nov 2005 03:47:30 -0000
@@ -36,6 +36,8 @@
#endif /*APACHE*/
#endif /*_OSD_POSIX*/

+#include "ext/standard/file.h"
+
/* {{{ free_url
*/
PHPAPI void php_url_free(php_url *theurl)
@@ -624,15 +626,51 @@
{
char *url;
int url_len;
+ zval *zcontext = NULL;
+ zval *format = NULL;
php_stream_context *context = NULL;
php_stream *stream;
zval **prev_val, **hdr = NULL;
HashPosition pos;
- long format = 0;
+ char *header = NULL;
+ int header_len;
+ int retformat = 0;

- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &url,
&url_len, &format) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zr", &url,
&url_len, &format, &zcontext) == FAILURE) {
return;
}
+ if (zcontext) {
+ context = php_stream_context_from_zval(zcontext, 1);
+ }
+ if (format) {
+ switch (Z_TYPE_P(format)) {
+ case IS_BOOL:
+ if (Z_LVAL_P(format)) {
+ retformat = 1;
+ }
+ break;
+
+ case IS_STRING:
+ header = Z_STRVAL_P(format);
+ header_len = Z_STRLEN_P(format);
+ break;
+
+ case IS_NULL:
+ // Use default
+ break;
+
+ case IS_LONG:
+ retformat = Z_LVAL_P(format);
+ php_error_docref(NULL TSRMLS_CC, E_STRICT,
"Using long for 2nd parameter is deprecated");
+ break;
+
+ default:
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
"The 2nd parameter should be either a string or bool");
+ break;
+ }
+ }
+
+

if (!(stream = php_stream_open_wrapper_ex(url, "r", REPORT_ERRORS |
STREAM_USE_URL | STREAM_ONLY_GET_HEADERS, NULL, context))) {
RETURN_FALSE;
@@ -642,7 +680,7 @@

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) {
+ if (!retformat && header == NULL) {
no_name_header:
add_next_index_stringl(return_value, Z_STRVAL_PP(hdr),
Z_STRLEN_PP(hdr), 1);
} else {
@@ -656,13 +694,19 @@
while (isspace((int)*(unsigned char *)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);
- }
+ if (header == NULL) {
+ 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);
+ }
+ } else {
+ if (!strncmp(header, Z_STRVAL_PP(hdr),
header_len)) {
+ RETVAL_STRING(s,1);
+ goto done;
+ }
+ }

*p = c;
} else {
@@ -671,7 +715,11 @@
}
zend_hash_move_forward_ex(HASH_OF(stream->wrapperdata), &pos);
}
+ if (header != NULL) {
+ RETVAL_FALSE;
+ }

+done:
php_stream_close(stream);
}
/* }}} */--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#2 Nov. 28, 2005 11:39:53

Hannes M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] get_headers patch


On 11/25/05, Arnar Mar SigurĂ°sson <> wrote:
> This is a update of the get_headers patch i sent in a long time ago and was
> ignored. Patch is written with the latest cvs snapshot of 5.1. Hope it makes
> it
> way to 5.1.1
>
> Info:
>
> mixed get_headers ( string url [, bool format | string header ])
>
> 2nd arg can eather be bool or string
> bool:
> true returns an numeric indexed array with headers
> false returns key index array with header name as key
> string:
> returns only the header with the same name.
>
> passing long will cast E_STRICT but still work the old way
>
> 3nd arg is a context to pass header/data to the url.

hmh. true, as 2nd parameter, returns associative array, false numeric.

The patch seems to apply cleanly to HEAD, to. Any change this could be
committed?

- Hannes

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

  • Root
  • » PHP
  • » [PHP-DEV] get_headers patch [RSS Feed]

Board footer

Moderator control

Enjoy the 20th of January
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support