Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 10... Línea 10...
10
/**
10
/**
11
 * Trait implementing functionality common to requests and responses.
11
 * Trait implementing functionality common to requests and responses.
12
 */
12
 */
13
trait MessageTrait
13
trait MessageTrait
14
{
14
{
15
    /** @var array<string, string[]> Map of all registered headers, as original name => array of values */
15
    /** @var string[][] Map of all registered headers, as original name => array of values */
16
    private $headers = [];
16
    private $headers = [];
Línea 17... Línea 17...
17
 
17
 
18
    /** @var array<string, string> Map of lowercase header name => original name at registration */
18
    /** @var string[] Map of lowercase header name => original name at registration */
Línea 19... Línea 19...
19
    private $headerNames  = [];
19
    private $headerNames = [];
20
 
20
 
Línea 21... Línea 21...
21
    /** @var string */
21
    /** @var string */
Línea 35... Línea 35...
35
            return $this;
35
            return $this;
36
        }
36
        }
Línea 37... Línea 37...
37
 
37
 
38
        $new = clone $this;
38
        $new = clone $this;
-
 
39
        $new->protocol = $version;
39
        $new->protocol = $version;
40
 
40
        return $new;
41
        return $new;
Línea 41... Línea 42...
41
    }
42
    }
42
 
43
 
Línea 133... Línea 134...
133
            return $this;
134
            return $this;
134
        }
135
        }
Línea 135... Línea 136...
135
 
136
 
136
        $new = clone $this;
137
        $new = clone $this;
-
 
138
        $new->stream = $body;
137
        $new->stream = $body;
139
 
138
        return $new;
140
        return $new;
Línea 139... Línea 141...
139
    }
141
    }
140
 
142
 
141
    /**
143
    /**
142
     * @param array<string|int, string|string[]> $headers
144
     * @param (string|string[])[] $headers
143
     */
145
     */
144
    private function setHeaders(array $headers): void
146
    private function setHeaders(array $headers): void
145
    {
147
    {
Línea 189... Línea 191...
189
     *
191
     *
190
     * @param mixed[] $values Header values
192
     * @param mixed[] $values Header values
191
     *
193
     *
192
     * @return string[] Trimmed header values
194
     * @return string[] Trimmed header values
193
     *
195
     *
194
     * @see https://tools.ietf.org/html/rfc7230#section-3.2.4
196
     * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4
195
     */
197
     */
196
    private function trimAndValidateHeaderValues(array $values): array
198
    private function trimAndValidateHeaderValues(array $values): array
197
    {
199
    {
198
        return array_map(function ($value) {
200
        return array_map(function ($value) {
199
            if (!is_scalar($value) && null !== $value) {
201
            if (!is_scalar($value) && null !== $value) {
Línea 209... Línea 211...
209
            return $trimmed;
211
            return $trimmed;
210
        }, array_values($values));
212
        }, array_values($values));
211
    }
213
    }
Línea 212... Línea 214...
212
 
214
 
213
    /**
215
    /**
214
     * @see https://tools.ietf.org/html/rfc7230#section-3.2
216
     * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2
215
     *
217
     *
216
     * @param mixed $header
218
     * @param mixed $header
217
     */
219
     */
218
    private function assertHeader($header): void
220
    private function assertHeader($header): void
Línea 222... Línea 224...
222
                'Header name must be a string but %s provided.',
224
                'Header name must be a string but %s provided.',
223
                is_object($header) ? get_class($header) : gettype($header)
225
                is_object($header) ? get_class($header) : gettype($header)
224
            ));
226
            ));
225
        }
227
        }
Línea 226... Línea 228...
226
 
228
 
227
        if (! preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/', $header)) {
229
        if (!preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $header)) {
228
            throw new \InvalidArgumentException(
-
 
229
                sprintf(
230
            throw new \InvalidArgumentException(
230
                    '"%s" is not valid header name',
-
 
231
                    $header
-
 
232
                )
231
                sprintf('"%s" is not valid header name.', $header)
233
            );
232
            );
234
        }
233
        }
Línea 235... Línea 234...
235
    }
234
    }
236
 
235
 
237
    /**
236
    /**
238
     * @see https://tools.ietf.org/html/rfc7230#section-3.2
237
     * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2
239
     *
238
     *
240
     * field-value    = *( field-content / obs-fold )
239
     * field-value    = *( field-content / obs-fold )
241
     * field-content  = field-vchar [ 1*( SP / HTAB ) field-vchar ]
240
     * field-content  = field-vchar [ 1*( SP / HTAB ) field-vchar ]
Línea 255... Línea 254...
255
        // within the message/http media type.
254
        // within the message/http media type.
256
        //
255
        //
257
        // Clients must not send a request with line folding and a server sending folded headers is
256
        // Clients must not send a request with line folding and a server sending folded headers is
258
        // likely very rare. Line folding is a fairly obscure feature of HTTP/1.1 and thus not accepting
257
        // likely very rare. Line folding is a fairly obscure feature of HTTP/1.1 and thus not accepting
259
        // folding is not likely to break any legitimate use case.
258
        // folding is not likely to break any legitimate use case.
260
        if (! preg_match('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/', $value)) {
259
        if (!preg_match('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/D', $value)) {
-
 
260
            throw new \InvalidArgumentException(
261
            throw new \InvalidArgumentException(sprintf('"%s" is not valid header value', $value));
261
                sprintf('"%s" is not valid header value.', $value)
-
 
262
            );
262
        }
263
        }
263
    }
264
    }
264
}
265
}