Edit File by line
/home/barbar84/public_h.../wp-inclu.../SimplePi.../HTTP
File: Parser.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* SimplePie
[2] Fix | Delete
*
[3] Fix | Delete
* A PHP-Based RSS and Atom Feed Framework.
[4] Fix | Delete
* Takes the hard work out of managing a complete RSS/Atom solution.
[5] Fix | Delete
*
[6] Fix | Delete
* Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
[7] Fix | Delete
* All rights reserved.
[8] Fix | Delete
*
[9] Fix | Delete
* Redistribution and use in source and binary forms, with or without modification, are
[10] Fix | Delete
* permitted provided that the following conditions are met:
[11] Fix | Delete
*
[12] Fix | Delete
* * Redistributions of source code must retain the above copyright notice, this list of
[13] Fix | Delete
* conditions and the following disclaimer.
[14] Fix | Delete
*
[15] Fix | Delete
* * Redistributions in binary form must reproduce the above copyright notice, this list
[16] Fix | Delete
* of conditions and the following disclaimer in the documentation and/or other materials
[17] Fix | Delete
* provided with the distribution.
[18] Fix | Delete
*
[19] Fix | Delete
* * Neither the name of the SimplePie Team nor the names of its contributors may be used
[20] Fix | Delete
* to endorse or promote products derived from this software without specific prior
[21] Fix | Delete
* written permission.
[22] Fix | Delete
*
[23] Fix | Delete
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
[24] Fix | Delete
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
[25] Fix | Delete
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
[26] Fix | Delete
* AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
[27] Fix | Delete
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
[28] Fix | Delete
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
[29] Fix | Delete
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
[30] Fix | Delete
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
[31] Fix | Delete
* POSSIBILITY OF SUCH DAMAGE.
[32] Fix | Delete
*
[33] Fix | Delete
* @package SimplePie
[34] Fix | Delete
* @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
[35] Fix | Delete
* @author Ryan Parman
[36] Fix | Delete
* @author Sam Sneddon
[37] Fix | Delete
* @author Ryan McCue
[38] Fix | Delete
* @link http://simplepie.org/ SimplePie
[39] Fix | Delete
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
[40] Fix | Delete
*/
[41] Fix | Delete
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* HTTP Response Parser
[45] Fix | Delete
*
[46] Fix | Delete
* @package SimplePie
[47] Fix | Delete
* @subpackage HTTP
[48] Fix | Delete
*/
[49] Fix | Delete
class SimplePie_HTTP_Parser
[50] Fix | Delete
{
[51] Fix | Delete
/**
[52] Fix | Delete
* HTTP Version
[53] Fix | Delete
*
[54] Fix | Delete
* @var float
[55] Fix | Delete
*/
[56] Fix | Delete
public $http_version = 0.0;
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Status code
[60] Fix | Delete
*
[61] Fix | Delete
* @var int
[62] Fix | Delete
*/
[63] Fix | Delete
public $status_code = 0;
[64] Fix | Delete
[65] Fix | Delete
/**
[66] Fix | Delete
* Reason phrase
[67] Fix | Delete
*
[68] Fix | Delete
* @var string
[69] Fix | Delete
*/
[70] Fix | Delete
public $reason = '';
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* Key/value pairs of the headers
[74] Fix | Delete
*
[75] Fix | Delete
* @var array
[76] Fix | Delete
*/
[77] Fix | Delete
public $headers = array();
[78] Fix | Delete
[79] Fix | Delete
/**
[80] Fix | Delete
* Body of the response
[81] Fix | Delete
*
[82] Fix | Delete
* @var string
[83] Fix | Delete
*/
[84] Fix | Delete
public $body = '';
[85] Fix | Delete
[86] Fix | Delete
/**
[87] Fix | Delete
* Current state of the state machine
[88] Fix | Delete
*
[89] Fix | Delete
* @var string
[90] Fix | Delete
*/
[91] Fix | Delete
protected $state = 'http_version';
[92] Fix | Delete
[93] Fix | Delete
/**
[94] Fix | Delete
* Input data
[95] Fix | Delete
*
[96] Fix | Delete
* @var string
[97] Fix | Delete
*/
[98] Fix | Delete
protected $data = '';
[99] Fix | Delete
[100] Fix | Delete
/**
[101] Fix | Delete
* Input data length (to avoid calling strlen() everytime this is needed)
[102] Fix | Delete
*
[103] Fix | Delete
* @var int
[104] Fix | Delete
*/
[105] Fix | Delete
protected $data_length = 0;
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* Current position of the pointer
[109] Fix | Delete
*
[110] Fix | Delete
* @var int
[111] Fix | Delete
*/
[112] Fix | Delete
protected $position = 0;
[113] Fix | Delete
[114] Fix | Delete
/**
[115] Fix | Delete
* Name of the hedaer currently being parsed
[116] Fix | Delete
*
[117] Fix | Delete
* @var string
[118] Fix | Delete
*/
[119] Fix | Delete
protected $name = '';
[120] Fix | Delete
[121] Fix | Delete
/**
[122] Fix | Delete
* Value of the hedaer currently being parsed
[123] Fix | Delete
*
[124] Fix | Delete
* @var string
[125] Fix | Delete
*/
[126] Fix | Delete
protected $value = '';
[127] Fix | Delete
[128] Fix | Delete
/**
[129] Fix | Delete
* Create an instance of the class with the input data
[130] Fix | Delete
*
[131] Fix | Delete
* @param string $data Input data
[132] Fix | Delete
*/
[133] Fix | Delete
public function __construct($data)
[134] Fix | Delete
{
[135] Fix | Delete
$this->data = $data;
[136] Fix | Delete
$this->data_length = strlen($this->data);
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* Parse the input data
[141] Fix | Delete
*
[142] Fix | Delete
* @return bool true on success, false on failure
[143] Fix | Delete
*/
[144] Fix | Delete
public function parse()
[145] Fix | Delete
{
[146] Fix | Delete
while ($this->state && $this->state !== 'emit' && $this->has_data())
[147] Fix | Delete
{
[148] Fix | Delete
$state = $this->state;
[149] Fix | Delete
$this->$state();
[150] Fix | Delete
}
[151] Fix | Delete
$this->data = '';
[152] Fix | Delete
if ($this->state === 'emit' || $this->state === 'body')
[153] Fix | Delete
{
[154] Fix | Delete
return true;
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
$this->http_version = '';
[158] Fix | Delete
$this->status_code = '';
[159] Fix | Delete
$this->reason = '';
[160] Fix | Delete
$this->headers = array();
[161] Fix | Delete
$this->body = '';
[162] Fix | Delete
return false;
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
/**
[166] Fix | Delete
* Check whether there is data beyond the pointer
[167] Fix | Delete
*
[168] Fix | Delete
* @return bool true if there is further data, false if not
[169] Fix | Delete
*/
[170] Fix | Delete
protected function has_data()
[171] Fix | Delete
{
[172] Fix | Delete
return (bool) ($this->position < $this->data_length);
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
/**
[176] Fix | Delete
* See if the next character is LWS
[177] Fix | Delete
*
[178] Fix | Delete
* @return bool true if the next character is LWS, false if not
[179] Fix | Delete
*/
[180] Fix | Delete
protected function is_linear_whitespace()
[181] Fix | Delete
{
[182] Fix | Delete
return (bool) ($this->data[$this->position] === "\x09"
[183] Fix | Delete
|| $this->data[$this->position] === "\x20"
[184] Fix | Delete
|| ($this->data[$this->position] === "\x0A"
[185] Fix | Delete
&& isset($this->data[$this->position + 1])
[186] Fix | Delete
&& ($this->data[$this->position + 1] === "\x09" || $this->data[$this->position + 1] === "\x20")));
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/**
[190] Fix | Delete
* Parse the HTTP version
[191] Fix | Delete
*/
[192] Fix | Delete
protected function http_version()
[193] Fix | Delete
{
[194] Fix | Delete
if (strpos($this->data, "\x0A") !== false && strtoupper(substr($this->data, 0, 5)) === 'HTTP/')
[195] Fix | Delete
{
[196] Fix | Delete
$len = strspn($this->data, '0123456789.', 5);
[197] Fix | Delete
$this->http_version = substr($this->data, 5, $len);
[198] Fix | Delete
$this->position += 5 + $len;
[199] Fix | Delete
if (substr_count($this->http_version, '.') <= 1)
[200] Fix | Delete
{
[201] Fix | Delete
$this->http_version = (float) $this->http_version;
[202] Fix | Delete
$this->position += strspn($this->data, "\x09\x20", $this->position);
[203] Fix | Delete
$this->state = 'status';
[204] Fix | Delete
}
[205] Fix | Delete
else
[206] Fix | Delete
{
[207] Fix | Delete
$this->state = false;
[208] Fix | Delete
}
[209] Fix | Delete
}
[210] Fix | Delete
else
[211] Fix | Delete
{
[212] Fix | Delete
$this->state = false;
[213] Fix | Delete
}
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
/**
[217] Fix | Delete
* Parse the status code
[218] Fix | Delete
*/
[219] Fix | Delete
protected function status()
[220] Fix | Delete
{
[221] Fix | Delete
if ($len = strspn($this->data, '0123456789', $this->position))
[222] Fix | Delete
{
[223] Fix | Delete
$this->status_code = (int) substr($this->data, $this->position, $len);
[224] Fix | Delete
$this->position += $len;
[225] Fix | Delete
$this->state = 'reason';
[226] Fix | Delete
}
[227] Fix | Delete
else
[228] Fix | Delete
{
[229] Fix | Delete
$this->state = false;
[230] Fix | Delete
}
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
/**
[234] Fix | Delete
* Parse the reason phrase
[235] Fix | Delete
*/
[236] Fix | Delete
protected function reason()
[237] Fix | Delete
{
[238] Fix | Delete
$len = strcspn($this->data, "\x0A", $this->position);
[239] Fix | Delete
$this->reason = trim(substr($this->data, $this->position, $len), "\x09\x0D\x20");
[240] Fix | Delete
$this->position += $len + 1;
[241] Fix | Delete
$this->state = 'new_line';
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
/**
[245] Fix | Delete
* Deal with a new line, shifting data around as needed
[246] Fix | Delete
*/
[247] Fix | Delete
protected function new_line()
[248] Fix | Delete
{
[249] Fix | Delete
$this->value = trim($this->value, "\x0D\x20");
[250] Fix | Delete
if ($this->name !== '' && $this->value !== '')
[251] Fix | Delete
{
[252] Fix | Delete
$this->name = strtolower($this->name);
[253] Fix | Delete
// We should only use the last Content-Type header. c.f. issue #1
[254] Fix | Delete
if (isset($this->headers[$this->name]) && $this->name !== 'content-type')
[255] Fix | Delete
{
[256] Fix | Delete
$this->headers[$this->name] .= ', ' . $this->value;
[257] Fix | Delete
}
[258] Fix | Delete
else
[259] Fix | Delete
{
[260] Fix | Delete
$this->headers[$this->name] = $this->value;
[261] Fix | Delete
}
[262] Fix | Delete
}
[263] Fix | Delete
$this->name = '';
[264] Fix | Delete
$this->value = '';
[265] Fix | Delete
if (substr($this->data[$this->position], 0, 2) === "\x0D\x0A")
[266] Fix | Delete
{
[267] Fix | Delete
$this->position += 2;
[268] Fix | Delete
$this->state = 'body';
[269] Fix | Delete
}
[270] Fix | Delete
elseif ($this->data[$this->position] === "\x0A")
[271] Fix | Delete
{
[272] Fix | Delete
$this->position++;
[273] Fix | Delete
$this->state = 'body';
[274] Fix | Delete
}
[275] Fix | Delete
else
[276] Fix | Delete
{
[277] Fix | Delete
$this->state = 'name';
[278] Fix | Delete
}
[279] Fix | Delete
}
[280] Fix | Delete
[281] Fix | Delete
/**
[282] Fix | Delete
* Parse a header name
[283] Fix | Delete
*/
[284] Fix | Delete
protected function name()
[285] Fix | Delete
{
[286] Fix | Delete
$len = strcspn($this->data, "\x0A:", $this->position);
[287] Fix | Delete
if (isset($this->data[$this->position + $len]))
[288] Fix | Delete
{
[289] Fix | Delete
if ($this->data[$this->position + $len] === "\x0A")
[290] Fix | Delete
{
[291] Fix | Delete
$this->position += $len;
[292] Fix | Delete
$this->state = 'new_line';
[293] Fix | Delete
}
[294] Fix | Delete
else
[295] Fix | Delete
{
[296] Fix | Delete
$this->name = substr($this->data, $this->position, $len);
[297] Fix | Delete
$this->position += $len + 1;
[298] Fix | Delete
$this->state = 'value';
[299] Fix | Delete
}
[300] Fix | Delete
}
[301] Fix | Delete
else
[302] Fix | Delete
{
[303] Fix | Delete
$this->state = false;
[304] Fix | Delete
}
[305] Fix | Delete
}
[306] Fix | Delete
[307] Fix | Delete
/**
[308] Fix | Delete
* Parse LWS, replacing consecutive LWS characters with a single space
[309] Fix | Delete
*/
[310] Fix | Delete
protected function linear_whitespace()
[311] Fix | Delete
{
[312] Fix | Delete
do
[313] Fix | Delete
{
[314] Fix | Delete
if (substr($this->data, $this->position, 2) === "\x0D\x0A")
[315] Fix | Delete
{
[316] Fix | Delete
$this->position += 2;
[317] Fix | Delete
}
[318] Fix | Delete
elseif ($this->data[$this->position] === "\x0A")
[319] Fix | Delete
{
[320] Fix | Delete
$this->position++;
[321] Fix | Delete
}
[322] Fix | Delete
$this->position += strspn($this->data, "\x09\x20", $this->position);
[323] Fix | Delete
} while ($this->has_data() && $this->is_linear_whitespace());
[324] Fix | Delete
$this->value .= "\x20";
[325] Fix | Delete
}
[326] Fix | Delete
[327] Fix | Delete
/**
[328] Fix | Delete
* See what state to move to while within non-quoted header values
[329] Fix | Delete
*/
[330] Fix | Delete
protected function value()
[331] Fix | Delete
{
[332] Fix | Delete
if ($this->is_linear_whitespace())
[333] Fix | Delete
{
[334] Fix | Delete
$this->linear_whitespace();
[335] Fix | Delete
}
[336] Fix | Delete
else
[337] Fix | Delete
{
[338] Fix | Delete
switch ($this->data[$this->position])
[339] Fix | Delete
{
[340] Fix | Delete
case '"':
[341] Fix | Delete
// Workaround for ETags: we have to include the quotes as
[342] Fix | Delete
// part of the tag.
[343] Fix | Delete
if (strtolower($this->name) === 'etag')
[344] Fix | Delete
{
[345] Fix | Delete
$this->value .= '"';
[346] Fix | Delete
$this->position++;
[347] Fix | Delete
$this->state = 'value_char';
[348] Fix | Delete
break;
[349] Fix | Delete
}
[350] Fix | Delete
$this->position++;
[351] Fix | Delete
$this->state = 'quote';
[352] Fix | Delete
break;
[353] Fix | Delete
[354] Fix | Delete
case "\x0A":
[355] Fix | Delete
$this->position++;
[356] Fix | Delete
$this->state = 'new_line';
[357] Fix | Delete
break;
[358] Fix | Delete
[359] Fix | Delete
default:
[360] Fix | Delete
$this->state = 'value_char';
[361] Fix | Delete
break;
[362] Fix | Delete
}
[363] Fix | Delete
}
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
/**
[367] Fix | Delete
* Parse a header value while outside quotes
[368] Fix | Delete
*/
[369] Fix | Delete
protected function value_char()
[370] Fix | Delete
{
[371] Fix | Delete
$len = strcspn($this->data, "\x09\x20\x0A\"", $this->position);
[372] Fix | Delete
$this->value .= substr($this->data, $this->position, $len);
[373] Fix | Delete
$this->position += $len;
[374] Fix | Delete
$this->state = 'value';
[375] Fix | Delete
}
[376] Fix | Delete
[377] Fix | Delete
/**
[378] Fix | Delete
* See what state to move to while within quoted header values
[379] Fix | Delete
*/
[380] Fix | Delete
protected function quote()
[381] Fix | Delete
{
[382] Fix | Delete
if ($this->is_linear_whitespace())
[383] Fix | Delete
{
[384] Fix | Delete
$this->linear_whitespace();
[385] Fix | Delete
}
[386] Fix | Delete
else
[387] Fix | Delete
{
[388] Fix | Delete
switch ($this->data[$this->position])
[389] Fix | Delete
{
[390] Fix | Delete
case '"':
[391] Fix | Delete
$this->position++;
[392] Fix | Delete
$this->state = 'value';
[393] Fix | Delete
break;
[394] Fix | Delete
[395] Fix | Delete
case "\x0A":
[396] Fix | Delete
$this->position++;
[397] Fix | Delete
$this->state = 'new_line';
[398] Fix | Delete
break;
[399] Fix | Delete
[400] Fix | Delete
case '\\':
[401] Fix | Delete
$this->position++;
[402] Fix | Delete
$this->state = 'quote_escaped';
[403] Fix | Delete
break;
[404] Fix | Delete
[405] Fix | Delete
default:
[406] Fix | Delete
$this->state = 'quote_char';
[407] Fix | Delete
break;
[408] Fix | Delete
}
[409] Fix | Delete
}
[410] Fix | Delete
}
[411] Fix | Delete
[412] Fix | Delete
/**
[413] Fix | Delete
* Parse a header value while within quotes
[414] Fix | Delete
*/
[415] Fix | Delete
protected function quote_char()
[416] Fix | Delete
{
[417] Fix | Delete
$len = strcspn($this->data, "\x09\x20\x0A\"\\", $this->position);
[418] Fix | Delete
$this->value .= substr($this->data, $this->position, $len);
[419] Fix | Delete
$this->position += $len;
[420] Fix | Delete
$this->state = 'value';
[421] Fix | Delete
}
[422] Fix | Delete
[423] Fix | Delete
/**
[424] Fix | Delete
* Parse an escaped character within quotes
[425] Fix | Delete
*/
[426] Fix | Delete
protected function quote_escaped()
[427] Fix | Delete
{
[428] Fix | Delete
$this->value .= $this->data[$this->position];
[429] Fix | Delete
$this->position++;
[430] Fix | Delete
$this->state = 'quote';
[431] Fix | Delete
}
[432] Fix | Delete
[433] Fix | Delete
/**
[434] Fix | Delete
* Parse the body
[435] Fix | Delete
*/
[436] Fix | Delete
protected function body()
[437] Fix | Delete
{
[438] Fix | Delete
$this->body = substr($this->data, $this->position);
[439] Fix | Delete
if (!empty($this->headers['transfer-encoding']))
[440] Fix | Delete
{
[441] Fix | Delete
unset($this->headers['transfer-encoding']);
[442] Fix | Delete
$this->state = 'chunked';
[443] Fix | Delete
}
[444] Fix | Delete
else
[445] Fix | Delete
{
[446] Fix | Delete
$this->state = 'emit';
[447] Fix | Delete
}
[448] Fix | Delete
}
[449] Fix | Delete
[450] Fix | Delete
/**
[451] Fix | Delete
* Parsed a "Transfer-Encoding: chunked" body
[452] Fix | Delete
*/
[453] Fix | Delete
protected function chunked()
[454] Fix | Delete
{
[455] Fix | Delete
if (!preg_match('/^([0-9a-f]+)[^\r\n]*\r\n/i', trim($this->body)))
[456] Fix | Delete
{
[457] Fix | Delete
$this->state = 'emit';
[458] Fix | Delete
return;
[459] Fix | Delete
}
[460] Fix | Delete
[461] Fix | Delete
$decoded = '';
[462] Fix | Delete
$encoded = $this->body;
[463] Fix | Delete
[464] Fix | Delete
while (true)
[465] Fix | Delete
{
[466] Fix | Delete
$is_chunked = (bool) preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', $encoded, $matches );
[467] Fix | Delete
if (!$is_chunked)
[468] Fix | Delete
{
[469] Fix | Delete
// Looks like it's not chunked after all
[470] Fix | Delete
$this->state = 'emit';
[471] Fix | Delete
return;
[472] Fix | Delete
}
[473] Fix | Delete
[474] Fix | Delete
$length = hexdec(trim($matches[1]));
[475] Fix | Delete
if ($length === 0)
[476] Fix | Delete
{
[477] Fix | Delete
// Ignore trailer headers
[478] Fix | Delete
$this->state = 'emit';
[479] Fix | Delete
$this->body = $decoded;
[480] Fix | Delete
return;
[481] Fix | Delete
}
[482] Fix | Delete
[483] Fix | Delete
$chunk_length = strlen($matches[0]);
[484] Fix | Delete
$decoded .= $part = substr($encoded, $chunk_length, $length);
[485] Fix | Delete
$encoded = substr($encoded, $chunk_length + $length + 2);
[486] Fix | Delete
[487] Fix | Delete
if (trim($encoded) === '0' || empty($encoded))
[488] Fix | Delete
{
[489] Fix | Delete
$this->state = 'emit';
[490] Fix | Delete
$this->body = $decoded;
[491] Fix | Delete
return;
[492] Fix | Delete
}
[493] Fix | Delete
}
[494] Fix | Delete
}
[495] Fix | Delete
[496] Fix | Delete
/**
[497] Fix | Delete
* Prepare headers (take care of proxies headers)
[498] Fix | Delete
*
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function