mirror of
https://github.com/php/php-src.git
synced 2025-01-24 12:43:38 +08:00
@ Made the User-Agent that php's fopen wrappers send, configurable via
@ php.ini or via a stream context. (Sterling) The stream context is untested, but it should/could work :) Either way it doesn't make the rest of the code bad. Wez -- please take a looksie for me :)
This commit is contained in:
parent
c0ed6edf41
commit
91df221ab7
@ -141,7 +141,10 @@ typedef struct {
|
||||
|
||||
HashTable sm_protected_env_vars;
|
||||
char *sm_allowed_env_vars;
|
||||
|
||||
|
||||
/* file.c */
|
||||
char *user_agent;
|
||||
|
||||
/* pageinfo.c */
|
||||
long page_uid;
|
||||
long page_gid;
|
||||
|
@ -34,6 +34,7 @@
|
||||
#include "ext/standard/php_filestat.h"
|
||||
#include "php_open_temporary_file.h"
|
||||
#include "ext/standard/basic_functions.h"
|
||||
#include "php_ini.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -155,6 +156,10 @@ static void file_globals_dtor(php_file_globals *file_globals_p TSRMLS_DC)
|
||||
}
|
||||
|
||||
|
||||
PHP_INI_BEGIN()
|
||||
STD_PHP_INI_ENTRY("user_agent", NULL, PHP_INI_ALL, OnUpdateString, user_agent, php_basic_globals, basic_globals)
|
||||
PHP_INI_END()
|
||||
|
||||
PHP_MINIT_FUNCTION(file)
|
||||
{
|
||||
le_stream = zend_register_list_destructors_ex(_file_stream_dtor, NULL, "stream", module_number);
|
||||
@ -166,6 +171,8 @@ PHP_MINIT_FUNCTION(file)
|
||||
file_globals_ctor(&file_globals TSRMLS_CC);
|
||||
#endif
|
||||
|
||||
REGISTER_INI_ENTRIES();
|
||||
|
||||
REGISTER_LONG_CONSTANT("SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT);
|
||||
REGISTER_LONG_CONSTANT("SEEK_CUR", SEEK_CUR, CONST_CS | CONST_PERSISTENT);
|
||||
REGISTER_LONG_CONSTANT("SEEK_END", SEEK_END, CONST_CS | CONST_PERSISTENT);
|
||||
|
@ -88,6 +88,8 @@ php_stream *php_stream_url_wrap_http(php_stream_wrapper *wrapper, char *path, ch
|
||||
int use_ssl;
|
||||
char *scratch = NULL;
|
||||
char *tmp = NULL;
|
||||
char *ua_str = NULL;
|
||||
zval **ua_zval;
|
||||
int scratch_len = 0;
|
||||
int body = 0;
|
||||
char location[HTTP_HEADER_BLOCK_SIZE];
|
||||
@ -196,7 +198,34 @@ php_stream *php_stream_url_wrap_http(php_stream_wrapper *wrapper, char *path, ch
|
||||
else if (snprintf(scratch, scratch_len, "Host: %s\r\n", resource->host) > 0)
|
||||
php_stream_write(stream, scratch, strlen(scratch));
|
||||
|
||||
php_stream_write_string(stream, "User-Agent: PHP/" PHP_VERSION "\r\n\r\n");
|
||||
if (context &&
|
||||
php_stream_context_get_option(context, "http", "user_agent", (zval **) &ua_zval) == FAILURE &&
|
||||
php_stream_context_get_option(context, "https", "user_agent", (zval **) &ua_zval) == FAILURE) {
|
||||
ua_str = Z_STRVAL_PP(ua_zval);
|
||||
} else if (BG(user_agent)) {
|
||||
ua_str = BG(user_agent);
|
||||
}
|
||||
|
||||
if (ua_str) {
|
||||
#define _UA_HEADER "User-Agent: %s\r\n"
|
||||
char *ua;
|
||||
size_t ua_len;
|
||||
|
||||
ua_len = sizeof(_UA_HEADER) + strlen(ua_str);
|
||||
ua = emalloc(ua_len + 1);
|
||||
if ((ua_len = snprintf(ua, ua_len, _UA_HEADER, ua_str)) > 0) {
|
||||
ua[ua_len] = 0;
|
||||
php_stream_write(stream, ua, ua_len);
|
||||
} else {
|
||||
php_error(E_WARNING, "Cannot construct User-agent header");
|
||||
}
|
||||
|
||||
if (ua) {
|
||||
efree(ua);
|
||||
}
|
||||
}
|
||||
|
||||
php_stream_write_string(stream, "\r\n");
|
||||
|
||||
location[0] = '\0';
|
||||
|
||||
@ -241,66 +270,64 @@ php_stream *php_stream_url_wrap_http(php_stream_wrapper *wrapper, char *path, ch
|
||||
}
|
||||
}
|
||||
|
||||
if( reqok ) {
|
||||
/* read past HTTP headers */
|
||||
/* read past HTTP headers */
|
||||
|
||||
http_header_line = emalloc(HTTP_HEADER_BLOCK_SIZE);
|
||||
http_header_line = emalloc(HTTP_HEADER_BLOCK_SIZE);
|
||||
|
||||
while (!body && !php_stream_eof(stream)) {
|
||||
while (!body && !php_stream_eof(stream)) {
|
||||
|
||||
if (php_stream_gets(stream, http_header_line, HTTP_HEADER_BLOCK_SIZE-1) != NULL) {
|
||||
char *p;
|
||||
int found_eol = 0;
|
||||
int http_header_line_length;
|
||||
if (php_stream_gets(stream, http_header_line, HTTP_HEADER_BLOCK_SIZE-1) != NULL) {
|
||||
char *p;
|
||||
int found_eol = 0;
|
||||
int http_header_line_length;
|
||||
|
||||
http_header_line[HTTP_HEADER_BLOCK_SIZE-1] = '\0';
|
||||
p = http_header_line;
|
||||
while(*p) {
|
||||
while(*p == '\n' || *p == '\r') {
|
||||
*p = '\0';
|
||||
p--;
|
||||
found_eol = 1;
|
||||
}
|
||||
if (found_eol)
|
||||
break;
|
||||
p++;
|
||||
}
|
||||
http_header_line_length = p-http_header_line+1;
|
||||
|
||||
if (!strncasecmp(http_header_line, "Location: ", 10)) {
|
||||
strlcpy(location, http_header_line + 10, sizeof(location));
|
||||
} else if (!strncasecmp(http_header_line, "Content-Type: ", 14)) {
|
||||
php_stream_notify_info(context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, http_header_line + 14, 0);
|
||||
} else if (!strncasecmp(http_header_line, "Content-Length: ", 16)) {
|
||||
file_size = atoi(http_header_line + 16);
|
||||
php_stream_notify_file_size(context, file_size, http_header_line, 0);
|
||||
}
|
||||
|
||||
if (http_header_line[0] == '\0')
|
||||
body = 1;
|
||||
else {
|
||||
zval *http_header;
|
||||
|
||||
MAKE_STD_ZVAL(http_header);
|
||||
|
||||
ZVAL_STRINGL(http_header, http_header_line, http_header_line_length, 1);
|
||||
|
||||
zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_header, sizeof(zval *), NULL);
|
||||
http_header_line[HTTP_HEADER_BLOCK_SIZE-1] = '\0';
|
||||
p = http_header_line;
|
||||
while(*p) {
|
||||
while(*p == '\n' || *p == '\r') {
|
||||
*p = '\0';
|
||||
p--;
|
||||
found_eol = 1;
|
||||
}
|
||||
if (found_eol)
|
||||
break;
|
||||
p++;
|
||||
}
|
||||
http_header_line_length = p-http_header_line+1;
|
||||
|
||||
if (!strncasecmp(http_header_line, "Location: ", 10)) {
|
||||
strlcpy(location, http_header_line + 10, sizeof(location));
|
||||
} else if (!strncasecmp(http_header_line, "Content-Type: ", 14)) {
|
||||
php_stream_notify_info(context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, http_header_line + 14, 0);
|
||||
} else if (!strncasecmp(http_header_line, "Content-Length: ", 16)) {
|
||||
file_size = atoi(http_header_line + 16);
|
||||
php_stream_notify_file_size(context, file_size, http_header_line, 0);
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!reqok) {
|
||||
|
||||
if (http_header_line[0] == '\0')
|
||||
body = 1;
|
||||
else {
|
||||
zval *http_header;
|
||||
|
||||
MAKE_STD_ZVAL(http_header);
|
||||
|
||||
ZVAL_STRINGL(http_header, http_header_line, http_header_line_length, 1);
|
||||
|
||||
zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_header, sizeof(zval *), NULL);
|
||||
}
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
if (!reqok) {
|
||||
|
||||
if (location[0] != '\0')
|
||||
php_stream_notify_info(context, PHP_STREAM_NOTIFY_REDIRECTED, location, 0);
|
||||
|
||||
php_stream_close(stream);
|
||||
stream = NULL;
|
||||
zval_dtor(response_header);
|
||||
FREE_ZVAL(response_header);
|
||||
|
||||
if (location[0] != '\0') {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user