Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 16... Línea 16...
16
     * @param MessageInterface $message Message to convert to a string.
16
     * @param MessageInterface $message Message to convert to a string.
17
     */
17
     */
18
    public static function toString(MessageInterface $message): string
18
    public static function toString(MessageInterface $message): string
19
    {
19
    {
20
        if ($message instanceof RequestInterface) {
20
        if ($message instanceof RequestInterface) {
21
            $msg = trim($message->getMethod() . ' '
21
            $msg = trim($message->getMethod().' '
22
                    . $message->getRequestTarget())
22
                    .$message->getRequestTarget())
23
                . ' HTTP/' . $message->getProtocolVersion();
23
                .' HTTP/'.$message->getProtocolVersion();
24
            if (!$message->hasHeader('host')) {
24
            if (!$message->hasHeader('host')) {
25
                $msg .= "\r\nHost: " . $message->getUri()->getHost();
25
                $msg .= "\r\nHost: ".$message->getUri()->getHost();
26
            }
26
            }
27
        } elseif ($message instanceof ResponseInterface) {
27
        } elseif ($message instanceof ResponseInterface) {
28
            $msg = 'HTTP/' . $message->getProtocolVersion() . ' '
28
            $msg = 'HTTP/'.$message->getProtocolVersion().' '
29
                . $message->getStatusCode() . ' '
29
                .$message->getStatusCode().' '
30
                . $message->getReasonPhrase();
30
                .$message->getReasonPhrase();
31
        } else {
31
        } else {
32
            throw new \InvalidArgumentException('Unknown message type');
32
            throw new \InvalidArgumentException('Unknown message type');
33
        }
33
        }
Línea 34... Línea 34...
34
 
34
 
35
        foreach ($message->getHeaders() as $name => $values) {
35
        foreach ($message->getHeaders() as $name => $values) {
36
            if (strtolower($name) === 'set-cookie') {
36
            if (is_string($name) && strtolower($name) === 'set-cookie') {
37
                foreach ($values as $value) {
37
                foreach ($values as $value) {
38
                    $msg .= "\r\n{$name}: " . $value;
38
                    $msg .= "\r\n{$name}: ".$value;
39
                }
39
                }
40
            } else {
40
            } else {
41
                $msg .= "\r\n{$name}: " . implode(', ', $values);
41
                $msg .= "\r\n{$name}: ".implode(', ', $values);
42
            }
42
            }
Línea 43... Línea 43...
43
        }
43
        }
44
 
44
 
Línea 45... Línea 45...
45
        return "{$msg}\r\n\r\n" . $message->getBody();
45
        return "{$msg}\r\n\r\n".$message->getBody();
46
    }
46
    }
47
 
47
 
Línea 75... Línea 75...
75
            $summary .= ' (truncated...)';
75
            $summary .= ' (truncated...)';
76
        }
76
        }
Línea 77... Línea 77...
77
 
77
 
78
        // Matches any printable character, including unicode characters:
78
        // Matches any printable character, including unicode characters:
79
        // letters, marks, numbers, punctuation, spacing, and separators.
79
        // letters, marks, numbers, punctuation, spacing, and separators.
80
        if (preg_match('/[^\pL\pM\pN\pP\pS\pZ\n\r\t]/u', $summary)) {
80
        if (preg_match('/[^\pL\pM\pN\pP\pS\pZ\n\r\t]/u', $summary) !== 0) {
81
            return null;
81
            return null;
Línea 82... Línea 82...
82
        }
82
        }
83
 
83
 
Línea 144... Línea 144...
144
        /** @var array[] $headerLines */
144
        /** @var array[] $headerLines */
145
        $count = preg_match_all(Rfc7230::HEADER_REGEX, $rawHeaders, $headerLines, PREG_SET_ORDER);
145
        $count = preg_match_all(Rfc7230::HEADER_REGEX, $rawHeaders, $headerLines, PREG_SET_ORDER);
Línea 146... Línea 146...
146
 
146
 
147
        // If these aren't the same, then one line didn't match and there's an invalid header.
147
        // If these aren't the same, then one line didn't match and there's an invalid header.
148
        if ($count !== substr_count($rawHeaders, "\n")) {
148
        if ($count !== substr_count($rawHeaders, "\n")) {
149
            // Folding is deprecated, see https://tools.ietf.org/html/rfc7230#section-3.2.4
149
            // Folding is deprecated, see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4
150
            if (preg_match(Rfc7230::HEADER_FOLD_REGEX, $rawHeaders)) {
150
            if (preg_match(Rfc7230::HEADER_FOLD_REGEX, $rawHeaders)) {
151
                throw new \InvalidArgumentException('Invalid header syntax: Obsolete line folding');
151
                throw new \InvalidArgumentException('Invalid header syntax: Obsolete line folding');
Línea 152... Línea 152...
152
            }
152
            }
Línea 188... Línea 188...
188
        }
188
        }
Línea 189... Línea 189...
189
 
189
 
190
        $host = $headers[reset($hostKey)][0];
190
        $host = $headers[reset($hostKey)][0];
Línea 191... Línea 191...
191
        $scheme = substr($host, -4) === ':443' ? 'https' : 'http';
191
        $scheme = substr($host, -4) === ':443' ? 'https' : 'http';
192
 
192
 
Línea 193... Línea 193...
193
        return $scheme . '://' . $host . '/' . ltrim($path, '/');
193
        return $scheme.'://'.$host.'/'.ltrim($path, '/');
194
    }
194
    }
195
 
195
 
Línea 225... Línea 225...
225
     * @param string $message Response message string.
225
     * @param string $message Response message string.
226
     */
226
     */
227
    public static function parseResponse(string $message): ResponseInterface
227
    public static function parseResponse(string $message): ResponseInterface
228
    {
228
    {
229
        $data = self::parseMessage($message);
229
        $data = self::parseMessage($message);
230
        // According to https://tools.ietf.org/html/rfc7230#section-3.1.2 the space
230
        // According to https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.2
231
        // between status-code and reason-phrase is required. But browsers accept
231
        // the space between status-code and reason-phrase is required. But
232
        // responses without space and reason as well.
232
        // browsers accept responses without space and reason as well.
233
        if (!preg_match('/^HTTP\/.* [0-9]{3}( .*|$)/', $data['start-line'])) {
233
        if (!preg_match('/^HTTP\/.* [0-9]{3}( .*|$)/', $data['start-line'])) {
234
            throw new \InvalidArgumentException('Invalid response string: ' . $data['start-line']);
234
            throw new \InvalidArgumentException('Invalid response string: '.$data['start-line']);
235
        }
235
        }
236
        $parts = explode(' ', $data['start-line'], 3);
236
        $parts = explode(' ', $data['start-line'], 3);
Línea 237... Línea 237...
237
 
237
 
238
        return new Response(
238
        return new Response(