Skip to content

Commit f4ecd11

Browse files
committed
HTTP: move classes into their own files, http.php loads the new files, so this is 100% BC if someone is loading http.php directly. New files created using svn cp.
`class-http.php` requires functions from `http.php`, so loading it by itself wouldn't have worked. Creates: `class-wp-http-cookie.php` `class-wp-http-curl.php` `class-wp-http-encoding.php` `class-wp-http-proxy.php` `class-wp-http-streams.php` `http-functions.php` `WP_Http` remains in `class-http.php`. `http.php` contains only top-level code. Class files only contain classes. Functions file only contains functions. See #33413. git-svn-id: https://develop.svn.wordpress.org/trunk@33748 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2b67add commit f4ecd11

9 files changed

Lines changed: 1995 additions & 1987 deletions

src/wp-includes/class-http.php

Lines changed: 0 additions & 1440 deletions
Large diffs are not rendered by default.
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<?php
2+
/**
3+
* Internal representation of a single cookie.
4+
*
5+
* Returned cookies are represented using this class, and when cookies are set, if they are not
6+
* already a WP_Http_Cookie() object, then they are turned into one.
7+
*
8+
* @todo The WordPress convention is to use underscores instead of camelCase for function and method
9+
* names. Need to switch to use underscores instead for the methods.
10+
*
11+
* @package WordPress
12+
* @subpackage HTTP
13+
* @since 2.8.0
14+
*/
15+
class WP_Http_Cookie {
16+
17+
/**
18+
* Cookie name.
19+
*
20+
* @since 2.8.0
21+
* @var string
22+
*/
23+
public $name;
24+
25+
/**
26+
* Cookie value.
27+
*
28+
* @since 2.8.0
29+
* @var string
30+
*/
31+
public $value;
32+
33+
/**
34+
* When the cookie expires.
35+
*
36+
* @since 2.8.0
37+
* @var string
38+
*/
39+
public $expires;
40+
41+
/**
42+
* Cookie URL path.
43+
*
44+
* @since 2.8.0
45+
* @var string
46+
*/
47+
public $path;
48+
49+
/**
50+
* Cookie Domain.
51+
*
52+
* @since 2.8.0
53+
* @var string
54+
*/
55+
public $domain;
56+
57+
/**
58+
* Sets up this cookie object.
59+
*
60+
* The parameter $data should be either an associative array containing the indices names below
61+
* or a header string detailing it.
62+
*
63+
* @since 2.8.0
64+
* @access public
65+
*
66+
* @param string|array $data {
67+
* Raw cookie data as header string or data array.
68+
*
69+
* @type string $name Cookie name.
70+
* @type mixed $value Value. Should NOT already be urlencoded.
71+
* @type string|int $expires Optional. Unix timestamp or formatted date. Default null.
72+
* @type string $path Optional. Path. Default '/'.
73+
* @type string $domain Optional. Domain. Default host of parsed $requested_url.
74+
* @type int $port Optional. Port. Default null.
75+
* }
76+
* @param string $requested_url The URL which the cookie was set on, used for default $domain
77+
* and $port values.
78+
*/
79+
public function __construct( $data, $requested_url = '' ) {
80+
if ( $requested_url )
81+
$arrURL = @parse_url( $requested_url );
82+
if ( isset( $arrURL['host'] ) )
83+
$this->domain = $arrURL['host'];
84+
$this->path = isset( $arrURL['path'] ) ? $arrURL['path'] : '/';
85+
if ( '/' != substr( $this->path, -1 ) )
86+
$this->path = dirname( $this->path ) . '/';
87+
88+
if ( is_string( $data ) ) {
89+
// Assume it's a header string direct from a previous request.
90+
$pairs = explode( ';', $data );
91+
92+
// Special handling for first pair; name=value. Also be careful of "=" in value.
93+
$name = trim( substr( $pairs[0], 0, strpos( $pairs[0], '=' ) ) );
94+
$value = substr( $pairs[0], strpos( $pairs[0], '=' ) + 1 );
95+
$this->name = $name;
96+
$this->value = urldecode( $value );
97+
98+
// Removes name=value from items.
99+
array_shift( $pairs );
100+
101+
// Set everything else as a property.
102+
foreach ( $pairs as $pair ) {
103+
$pair = rtrim($pair);
104+
105+
// Handle the cookie ending in ; which results in a empty final pair.
106+
if ( empty($pair) )
107+
continue;
108+
109+
list( $key, $val ) = strpos( $pair, '=' ) ? explode( '=', $pair ) : array( $pair, '' );
110+
$key = strtolower( trim( $key ) );
111+
if ( 'expires' == $key )
112+
$val = strtotime( $val );
113+
$this->$key = $val;
114+
}
115+
} else {
116+
if ( !isset( $data['name'] ) )
117+
return;
118+
119+
// Set properties based directly on parameters.
120+
foreach ( array( 'name', 'value', 'path', 'domain', 'port' ) as $field ) {
121+
if ( isset( $data[ $field ] ) )
122+
$this->$field = $data[ $field ];
123+
}
124+
125+
if ( isset( $data['expires'] ) )
126+
$this->expires = is_int( $data['expires'] ) ? $data['expires'] : strtotime( $data['expires'] );
127+
else
128+
$this->expires = null;
129+
}
130+
}
131+
132+
/**
133+
* Confirms that it's OK to send this cookie to the URL checked against.
134+
*
135+
* Decision is based on RFC 2109/2965, so look there for details on validity.
136+
*
137+
* @access public
138+
* @since 2.8.0
139+
*
140+
* @param string $url URL you intend to send this cookie to
141+
* @return bool true if allowed, false otherwise.
142+
*/
143+
public function test( $url ) {
144+
if ( is_null( $this->name ) )
145+
return false;
146+
147+
// Expires - if expired then nothing else matters.
148+
if ( isset( $this->expires ) && time() > $this->expires )
149+
return false;
150+
151+
// Get details on the URL we're thinking about sending to.
152+
$url = parse_url( $url );
153+
$url['port'] = isset( $url['port'] ) ? $url['port'] : ( 'https' == $url['scheme'] ? 443 : 80 );
154+
$url['path'] = isset( $url['path'] ) ? $url['path'] : '/';
155+
156+
// Values to use for comparison against the URL.
157+
$path = isset( $this->path ) ? $this->path : '/';
158+
$port = isset( $this->port ) ? $this->port : null;
159+
$domain = isset( $this->domain ) ? strtolower( $this->domain ) : strtolower( $url['host'] );
160+
if ( false === stripos( $domain, '.' ) )
161+
$domain .= '.local';
162+
163+
// Host - very basic check that the request URL ends with the domain restriction (minus leading dot).
164+
$domain = substr( $domain, 0, 1 ) == '.' ? substr( $domain, 1 ) : $domain;
165+
if ( substr( $url['host'], -strlen( $domain ) ) != $domain )
166+
return false;
167+
168+
// Port - supports "port-lists" in the format: "80,8000,8080".
169+
if ( !empty( $port ) && !in_array( $url['port'], explode( ',', $port) ) )
170+
return false;
171+
172+
// Path - request path must start with path restriction.
173+
if ( substr( $url['path'], 0, strlen( $path ) ) != $path )
174+
return false;
175+
176+
return true;
177+
}
178+
179+
/**
180+
* Convert cookie name and value back to header string.
181+
*
182+
* @access public
183+
* @since 2.8.0
184+
*
185+
* @return string Header encoded cookie name and value.
186+
*/
187+
public function getHeaderValue() {
188+
if ( ! isset( $this->name ) || ! isset( $this->value ) )
189+
return '';
190+
191+
/**
192+
* Filter the header-encoded cookie value.
193+
*
194+
* @since 3.4.0
195+
*
196+
* @param string $value The cookie value.
197+
* @param string $name The cookie name.
198+
*/
199+
return $this->name . '=' . apply_filters( 'wp_http_cookie_value', $this->value, $this->name );
200+
}
201+
202+
/**
203+
* Retrieve cookie header for usage in the rest of the WordPress HTTP API.
204+
*
205+
* @access public
206+
* @since 2.8.0
207+
*
208+
* @return string
209+
*/
210+
public function getFullHeader() {
211+
return 'Cookie: ' . $this->getHeaderValue();
212+
}
213+
}

0 commit comments

Comments
 (0)