Changeset 60771 for trunk/src/wp-includes/SimplePie/src/HTTP/Parser.php
- Timestamp:
- 09/16/2025 10:45:37 PM (2 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/SimplePie/src/HTTP/Parser.php
r59141 r60771 1 1 <?php 2 2 3 /** 4 * SimplePie 5 * 6 * A PHP-Based RSS and Atom Feed Framework. 7 * Takes the hard work out of managing a complete RSS/Atom solution. 8 * 9 * Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without modification, are 13 * permitted provided that the following conditions are met: 14 * 15 * * Redistributions of source code must retain the above copyright notice, this list of 16 * conditions and the following disclaimer. 17 * 18 * * Redistributions in binary form must reproduce the above copyright notice, this list 19 * of conditions and the following disclaimer in the documentation and/or other materials 20 * provided with the distribution. 21 * 22 * * Neither the name of the SimplePie Team nor the names of its contributors may be used 23 * to endorse or promote products derived from this software without specific prior 24 * written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 27 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 28 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS 29 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 * 36 * @package SimplePie 37 * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue 38 * @author Ryan Parman 39 * @author Sam Sneddon 40 * @author Ryan McCue 41 * @link http://simplepie.org/ SimplePie 42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License 43 */ 3 // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue 4 // SPDX-License-Identifier: BSD-3-Clause 5 6 declare(strict_types=1); 44 7 45 8 namespace SimplePie\HTTP; … … 47 10 /** 48 11 * HTTP Response Parser 49 * 50 * @package SimplePie 51 * @subpackage HTTP 12 * @template Psr7Compatible of bool 52 13 */ 53 14 class Parser … … 75 36 76 37 /** 38 * @var Psr7Compatible whether headers are compatible with PSR-7 format. 39 */ 40 private $psr7Compatible; 41 42 /** 77 43 * Key/value pairs of the headers 78 44 * 79 * @var array45 * @var (Psr7Compatible is true ? array<string, non-empty-array<string>> : array<string, string>) 80 46 */ 81 47 public $headers = []; … … 145 111 146 112 /** 147 * Name of the he daer currently being parsed113 * Name of the header currently being parsed 148 114 * 149 115 * @var string … … 152 118 153 119 /** 154 * Value of the he daer currently being parsed120 * Value of the header currently being parsed 155 121 * 156 122 * @var string … … 162 128 * 163 129 * @param string $data Input data 164 */ 165 public function __construct($data) 130 * @param Psr7Compatible $psr7Compatible Whether the data types are in format compatible with PSR-7. 131 */ 132 public function __construct(string $data, bool $psr7Compatible = false) 166 133 { 167 134 $this->data = $data; 168 135 $this->data_length = strlen($this->data); 136 $this->psr7Compatible = $psr7Compatible; 169 137 } 170 138 … … 185 153 } 186 154 187 $this->http_version = ''; 155 // Reset the parser state. 156 $this->http_version = 0.0; 188 157 $this->status_code = 0; 189 158 $this->reason = ''; … … 219 188 /** 220 189 * Parse the HTTP version 190 * @return void 221 191 */ 222 192 protected function http_version() … … 224 194 if (strpos($this->data, "\x0A") !== false && strtoupper(substr($this->data, 0, 5)) === 'HTTP/') { 225 195 $len = strspn($this->data, '0123456789.', 5); 226 $ this->http_version = substr($this->data, 5, $len);196 $http_version = substr($this->data, 5, $len); 227 197 $this->position += 5 + $len; 228 if (substr_count($ this->http_version, '.') <= 1) {229 $this->http_version = (float) $ this->http_version;198 if (substr_count($http_version, '.') <= 1) { 199 $this->http_version = (float) $http_version; 230 200 $this->position += strspn($this->data, "\x09\x20", $this->position); 231 201 $this->state = self::STATE_STATUS; … … 240 210 /** 241 211 * Parse the status code 212 * @return void 242 213 */ 243 214 protected function status() … … 254 225 /** 255 226 * Parse the reason phrase 227 * @return void 256 228 */ 257 229 protected function reason() … … 263 235 } 264 236 237 private function add_header(string $name, string $value): void 238 { 239 if ($this->psr7Compatible) { 240 // For PHPStan: should be enforced by template parameter but PHPStan is not smart enough. 241 /** @var array<string, non-empty-array<string>> */ 242 $headers = &$this->headers; 243 $headers[$name][] = $value; 244 } else { 245 // For PHPStan: should be enforced by template parameter but PHPStan is not smart enough. 246 /** @var array<string, string>) */ 247 $headers = &$this->headers; 248 $headers[$name] .= ', ' . $value; 249 } 250 } 251 252 private function replace_header(string $name, string $value): void 253 { 254 if ($this->psr7Compatible) { 255 // For PHPStan: should be enforced by template parameter but PHPStan is not smart enough. 256 /** @var array<string, non-empty-array<string>> */ 257 $headers = &$this->headers; 258 $headers[$name] = [$value]; 259 } else { 260 // For PHPStan: should be enforced by template parameter but PHPStan is not smart enough. 261 /** @var array<string, string>) */ 262 $headers = &$this->headers; 263 $headers[$name] = $value; 264 } 265 } 266 265 267 /** 266 268 * Deal with a new line, shifting data around as needed 269 * @return void 267 270 */ 268 271 protected function new_line() … … 273 276 // We should only use the last Content-Type header. c.f. issue #1 274 277 if (isset($this->headers[$this->name]) && $this->name !== 'content-type') { 275 $this-> headers[$this->name] .= ', ' . $this->value;278 $this->add_header($this->name, $this->value); 276 279 } else { 277 $this-> headers[$this->name] = $this->value;280 $this->replace_header($this->name, $this->value); 278 281 } 279 282 } … … 293 296 /** 294 297 * Parse a header name 298 * @return void 295 299 */ 296 300 protected function name() … … 313 317 /** 314 318 * Parse LWS, replacing consecutive LWS characters with a single space 319 * @return void 315 320 */ 316 321 protected function linear_whitespace() … … 329 334 /** 330 335 * See what state to move to while within non-quoted header values 336 * @return void 331 337 */ 332 338 protected function value() … … 363 369 /** 364 370 * Parse a header value while outside quotes 371 * @return void 365 372 */ 366 373 protected function value_char() … … 374 381 /** 375 382 * See what state to move to while within quoted header values 383 * @return void 376 384 */ 377 385 protected function quote() … … 405 413 /** 406 414 * Parse a header value while within quotes 415 * @return void 407 416 */ 408 417 protected function quote_char() … … 416 425 /** 417 426 * Parse an escaped character within quotes 427 * @return void 418 428 */ 419 429 protected function quote_escaped() … … 426 436 /** 427 437 * Parse the body 438 * @return void 428 439 */ 429 440 protected function body() … … 440 451 /** 441 452 * Parsed a "Transfer-Encoding: chunked" body 453 * @return void 442 454 */ 443 455 protected function chunked() … … 460 472 461 473 $length = hexdec(trim($matches[1])); 474 // For PHPStan: this will only be float when larger than PHP_INT_MAX. 475 // But even on 32-bit systems, it would mean 2GiB chunk, which sounds unlikely. 476 \assert(\is_int($length), "Length needs to be shorter than PHP_INT_MAX"); 462 477 if ($length === 0) { 463 478 // Ignore trailer headers … … 486 501 * 487 502 * @param string $headers Raw headers 488 * @param integer $countRedirection count. Default to 1.503 * @param non-negative-int $count Redirection count. Default to 1. 489 504 * 490 505 * @return string 491 506 */ 492 public static function prepareHeaders( $headers,$count = 1)507 public static function prepareHeaders(string $headers, int $count = 1) 493 508 { 494 509 $data = explode("\r\n\r\n", $headers, $count);
Note: See TracChangeset
for help on using the changeset viewer.