Make WordPress Core


Ignore:
Timestamp:
09/16/2025 10:45:37 PM (2 months ago)
Author:
SergeyBiryukov
Message:

External Libraries: Update the SimplePie library to version 1.9.0.

References:

Follow-up to [59141], [60490].

Props swissspidy, TobiasBg, SergeyBiryukov.
Fixes #63961.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/SimplePie/src/HTTP/Parser.php

    r59141 r60771  
    11<?php
    22
    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
     6declare(strict_types=1);
    447
    458namespace SimplePie\HTTP;
     
    4710/**
    4811 * HTTP Response Parser
    49  *
    50  * @package SimplePie
    51  * @subpackage HTTP
     12 * @template Psr7Compatible of bool
    5213 */
    5314class Parser
     
    7536
    7637    /**
     38     * @var Psr7Compatible whether headers are compatible with PSR-7 format.
     39     */
     40    private $psr7Compatible;
     41
     42    /**
    7743     * Key/value pairs of the headers
    7844     *
    79      * @var array
     45     * @var (Psr7Compatible is true ? array<string, non-empty-array<string>> : array<string, string>)
    8046     */
    8147    public $headers = [];
     
    145111
    146112    /**
    147      * Name of the hedaer currently being parsed
     113     * Name of the header currently being parsed
    148114     *
    149115     * @var string
     
    152118
    153119    /**
    154      * Value of the hedaer currently being parsed
     120     * Value of the header currently being parsed
    155121     *
    156122     * @var string
     
    162128     *
    163129     * @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)
    166133    {
    167134        $this->data = $data;
    168135        $this->data_length = strlen($this->data);
     136        $this->psr7Compatible = $psr7Compatible;
    169137    }
    170138
     
    185153        }
    186154
    187         $this->http_version = '';
     155        // Reset the parser state.
     156        $this->http_version = 0.0;
    188157        $this->status_code = 0;
    189158        $this->reason = '';
     
    219188    /**
    220189     * Parse the HTTP version
     190     * @return void
    221191     */
    222192    protected function http_version()
     
    224194        if (strpos($this->data, "\x0A") !== false && strtoupper(substr($this->data, 0, 5)) === 'HTTP/') {
    225195            $len = strspn($this->data, '0123456789.', 5);
    226             $this->http_version = substr($this->data, 5, $len);
     196            $http_version = substr($this->data, 5, $len);
    227197            $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;
    230200                $this->position += strspn($this->data, "\x09\x20", $this->position);
    231201                $this->state = self::STATE_STATUS;
     
    240210    /**
    241211     * Parse the status code
     212     * @return void
    242213     */
    243214    protected function status()
     
    254225    /**
    255226     * Parse the reason phrase
     227     * @return void
    256228     */
    257229    protected function reason()
     
    263235    }
    264236
     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
    265267    /**
    266268     * Deal with a new line, shifting data around as needed
     269     * @return void
    267270     */
    268271    protected function new_line()
     
    273276            // We should only use the last Content-Type header. c.f. issue #1
    274277            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);
    276279            } else {
    277                 $this->headers[$this->name] = $this->value;
     280                $this->replace_header($this->name, $this->value);
    278281            }
    279282        }
     
    293296    /**
    294297     * Parse a header name
     298     * @return void
    295299     */
    296300    protected function name()
     
    313317    /**
    314318     * Parse LWS, replacing consecutive LWS characters with a single space
     319     * @return void
    315320     */
    316321    protected function linear_whitespace()
     
    329334    /**
    330335     * See what state to move to while within non-quoted header values
     336     * @return void
    331337     */
    332338    protected function value()
     
    363369    /**
    364370     * Parse a header value while outside quotes
     371     * @return void
    365372     */
    366373    protected function value_char()
     
    374381    /**
    375382     * See what state to move to while within quoted header values
     383     * @return void
    376384     */
    377385    protected function quote()
     
    405413    /**
    406414     * Parse a header value while within quotes
     415     * @return void
    407416     */
    408417    protected function quote_char()
     
    416425    /**
    417426     * Parse an escaped character within quotes
     427     * @return void
    418428     */
    419429    protected function quote_escaped()
     
    426436    /**
    427437     * Parse the body
     438     * @return void
    428439     */
    429440    protected function body()
     
    440451    /**
    441452     * Parsed a "Transfer-Encoding: chunked" body
     453     * @return void
    442454     */
    443455    protected function chunked()
     
    460472
    461473            $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");
    462477            if ($length === 0) {
    463478                // Ignore trailer headers
     
    486501     *
    487502     * @param string  $headers Raw headers
    488      * @param integer $count  Redirection count. Default to 1.
     503     * @param non-negative-int $count Redirection count. Default to 1.
    489504     *
    490505     * @return string
    491506     */
    492     public static function prepareHeaders($headers, $count = 1)
     507    public static function prepareHeaders(string $headers, int $count = 1)
    493508    {
    494509        $data = explode("\r\n\r\n", $headers, $count);
Note: See TracChangeset for help on using the changeset viewer.