Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 38... Línea 38...
38
        // Sleep if there is a delay specified.
38
        // Sleep if there is a delay specified.
39
        if (isset($options['delay'])) {
39
        if (isset($options['delay'])) {
40
            \usleep($options['delay'] * 1000);
40
            \usleep($options['delay'] * 1000);
41
        }
41
        }
Línea -... Línea 42...
-
 
42
 
-
 
43
        $protocolVersion = $request->getProtocolVersion();
-
 
44
 
-
 
45
        if ('1.0' !== $protocolVersion && '1.1' !== $protocolVersion) {
-
 
46
            throw new ConnectException(sprintf('HTTP/%s is not supported by the stream handler.', $protocolVersion), $request);
-
 
47
        }
42
 
48
 
Línea 43... Línea 49...
43
        $startTime = isset($options['on_stats']) ? Utils::currentTime() : null;
49
        $startTime = isset($options['on_stats']) ? Utils::currentTime() : null;
44
 
50
 
45
        try {
51
        try {
Línea 65... Línea 71...
65
            $message = $e->getMessage();
71
            $message = $e->getMessage();
66
            // This list can probably get more comprehensive.
72
            // This list can probably get more comprehensive.
67
            if (false !== \strpos($message, 'getaddrinfo') // DNS lookup failed
73
            if (false !== \strpos($message, 'getaddrinfo') // DNS lookup failed
68
                || false !== \strpos($message, 'Connection refused')
74
                || false !== \strpos($message, 'Connection refused')
69
                || false !== \strpos($message, "couldn't connect to host") // error on HHVM
75
                || false !== \strpos($message, "couldn't connect to host") // error on HHVM
70
                || false !== \strpos($message, "connection attempt failed")
76
                || false !== \strpos($message, 'connection attempt failed')
71
            ) {
77
            ) {
72
                $e = new ConnectException($e->getMessage(), $request, $e);
78
                $e = new ConnectException($e->getMessage(), $request, $e);
73
            } else {
79
            } else {
74
                $e = RequestException::wrapException($request, $e);
80
                $e = RequestException::wrapException($request, $e);
75
            }
81
            }
Línea 81... Línea 87...
81
 
87
 
82
    private function invokeStats(
88
    private function invokeStats(
83
        array $options,
89
        array $options,
84
        RequestInterface $request,
90
        RequestInterface $request,
85
        ?float $startTime,
91
        ?float $startTime,
86
        ResponseInterface $response = null,
92
        ?ResponseInterface $response = null,
87
        \Throwable $error = null
93
        ?\Throwable $error = null
88
    ): void {
94
    ): void {
89
        if (isset($options['on_stats'])) {
95
        if (isset($options['on_stats'])) {
90
            $stats = new TransferStats($request, $response, Utils::currentTime() - $startTime, $error, []);
96
            $stats = new TransferStats($request, $response, Utils::currentTime() - $startTime, $error, []);
91
            ($options['on_stats'])($stats);
97
            ($options['on_stats'])($stats);
Línea 229... Línea 235...
229
    {
235
    {
230
        $errors = [];
236
        $errors = [];
231
        \set_error_handler(static function ($_, $msg, $file, $line) use (&$errors): bool {
237
        \set_error_handler(static function ($_, $msg, $file, $line) use (&$errors): bool {
232
            $errors[] = [
238
            $errors[] = [
233
                'message' => $msg,
239
                'message' => $msg,
234
                'file'    => $file,
240
                'file' => $file,
235
                'line'    => $line
241
                'line' => $line,
236
            ];
242
            ];
-
 
243
 
237
            return true;
244
            return true;
238
        });
245
        });
Línea 239... Línea 246...
239
 
246
 
240
        try {
247
        try {
Línea 245... Línea 252...
245
 
252
 
246
        if (!$resource) {
253
        if (!$resource) {
247
            $message = 'Error creating resource: ';
254
            $message = 'Error creating resource: ';
248
            foreach ($errors as $err) {
255
            foreach ($errors as $err) {
249
                foreach ($err as $key => $value) {
256
                foreach ($err as $key => $value) {
250
                    $message .= "[$key] $value" . \PHP_EOL;
257
                    $message .= "[$key] $value".\PHP_EOL;
251
                }
258
                }
252
            }
259
            }
253
            throw new \RuntimeException(\trim($message));
260
            throw new \RuntimeException(\trim($message));
Línea 270... Línea 277...
270
            throw new RequestException(\sprintf("The scheme '%s' is not supported.", $request->getUri()->getScheme()), $request);
277
            throw new RequestException(\sprintf("The scheme '%s' is not supported.", $request->getUri()->getScheme()), $request);
271
        }
278
        }
Línea 272... Línea 279...
272
 
279
 
273
        // HTTP/1.1 streams using the PHP stream wrapper require a
280
        // HTTP/1.1 streams using the PHP stream wrapper require a
274
        // Connection: close header
281
        // Connection: close header
275
        if ($request->getProtocolVersion() == '1.1'
282
        if ($request->getProtocolVersion() === '1.1'
276
            && !$request->hasHeader('Connection')
283
            && !$request->hasHeader('Connection')
277
        ) {
284
        ) {
278
            $request = $request->withHeader('Connection', 'close');
285
            $request = $request->withHeader('Connection', 'close');
Línea 348... Línea 355...
348
            if ('v4' === $options['force_ip_resolve']) {
355
            if ('v4' === $options['force_ip_resolve']) {
349
                $records = \dns_get_record($uri->getHost(), \DNS_A);
356
                $records = \dns_get_record($uri->getHost(), \DNS_A);
350
                if (false === $records || !isset($records[0]['ip'])) {
357
                if (false === $records || !isset($records[0]['ip'])) {
351
                    throw new ConnectException(\sprintf("Could not resolve IPv4 address for host '%s'", $uri->getHost()), $request);
358
                    throw new ConnectException(\sprintf("Could not resolve IPv4 address for host '%s'", $uri->getHost()), $request);
352
                }
359
                }
-
 
360
 
353
                return $uri->withHost($records[0]['ip']);
361
                return $uri->withHost($records[0]['ip']);
354
            }
362
            }
355
            if ('v6' === $options['force_ip_resolve']) {
363
            if ('v6' === $options['force_ip_resolve']) {
356
                $records = \dns_get_record($uri->getHost(), \DNS_AAAA);
364
                $records = \dns_get_record($uri->getHost(), \DNS_AAAA);
357
                if (false === $records || !isset($records[0]['ipv6'])) {
365
                if (false === $records || !isset($records[0]['ipv6'])) {
358
                    throw new ConnectException(\sprintf("Could not resolve IPv6 address for host '%s'", $uri->getHost()), $request);
366
                    throw new ConnectException(\sprintf("Could not resolve IPv6 address for host '%s'", $uri->getHost()), $request);
359
                }
367
                }
-
 
368
 
360
                return $uri->withHost('[' . $records[0]['ipv6'] . ']');
369
                return $uri->withHost('['.$records[0]['ipv6'].']');
361
            }
370
            }
362
        }
371
        }
Línea 363... Línea 372...
363
 
372
 
364
        return $uri;
373
        return $uri;
Línea 373... Línea 382...
373
            }
382
            }
374
        }
383
        }
Línea 375... Línea 384...
375
 
384
 
376
        $context = [
385
        $context = [
377
            'http' => [
386
            'http' => [
378
                'method'           => $request->getMethod(),
387
                'method' => $request->getMethod(),
379
                'header'           => $headers,
388
                'header' => $headers,
380
                'protocol_version' => $request->getProtocolVersion(),
389
                'protocol_version' => $request->getProtocolVersion(),
381
                'ignore_errors'    => true,
390
                'ignore_errors' => true,
382
                'follow_location'  => 0,
391
                'follow_location' => 0,
383
            ],
392
            ],
384
            'ssl' => [
393
            'ssl' => [
385
                'peer_name' => $request->getUri()->getHost(),
394
                'peer_name' => $request->getUri()->getHost(),
386
            ],
395
            ],
Línea 387... Línea 396...
387
        ];
396
        ];
Línea 388... Línea 397...
388
 
397
 
389
        $body = (string) $request->getBody();
398
        $body = (string) $request->getBody();
390
 
399
 
391
        if (!empty($body)) {
400
        if ('' !== $body) {
392
            $context['http']['content'] = $body;
401
            $context['http']['content'] = $body;
393
            // Prevent the HTTP handler from adding a Content-Type header.
402
            // Prevent the HTTP handler from adding a Content-Type header.
Línea 473... Línea 482...
473
    }
482
    }
Línea 474... Línea 483...
474
 
483
 
475
    /**
484
    /**
476
     * @param mixed $value as passed via Request transfer options.
485
     * @param mixed $value as passed via Request transfer options.
-
 
486
     */
-
 
487
    private function add_crypto_method(RequestInterface $request, array &$options, $value, array &$params): void
-
 
488
    {
-
 
489
        if (
-
 
490
            $value === \STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT
-
 
491
            || $value === \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT
-
 
492
            || $value === \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
-
 
493
            || (defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') && $value === \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT)
-
 
494
        ) {
-
 
495
            $options['http']['crypto_method'] = $value;
-
 
496
 
-
 
497
            return;
-
 
498
        }
-
 
499
 
-
 
500
        throw new \InvalidArgumentException('Invalid crypto_method request option: unknown version provided');
-
 
501
    }
-
 
502
 
-
 
503
    /**
-
 
504
     * @param mixed $value as passed via Request transfer options.
477
     */
505
     */
478
    private function add_verify(RequestInterface $request, array &$options, $value, array &$params): void
506
    private function add_verify(RequestInterface $request, array &$options, $value, array &$params): void
479
    {
507
    {
480
        if ($value === false) {
508
        if ($value === false) {
481
            $options['ssl']['verify_peer'] = false;
509
            $options['ssl']['verify_peer'] = false;
Línea 540... Línea 568...
540
        if ($value === false) {
568
        if ($value === false) {
541
            return;
569
            return;
542
        }
570
        }
Línea 543... Línea 571...
543
 
571
 
544
        static $map = [
572
        static $map = [
545
            \STREAM_NOTIFY_CONNECT       => 'CONNECT',
573
            \STREAM_NOTIFY_CONNECT => 'CONNECT',
546
            \STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED',
574
            \STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED',
547
            \STREAM_NOTIFY_AUTH_RESULT   => 'AUTH_RESULT',
575
            \STREAM_NOTIFY_AUTH_RESULT => 'AUTH_RESULT',
548
            \STREAM_NOTIFY_MIME_TYPE_IS  => 'MIME_TYPE_IS',
576
            \STREAM_NOTIFY_MIME_TYPE_IS => 'MIME_TYPE_IS',
549
            \STREAM_NOTIFY_FILE_SIZE_IS  => 'FILE_SIZE_IS',
577
            \STREAM_NOTIFY_FILE_SIZE_IS => 'FILE_SIZE_IS',
550
            \STREAM_NOTIFY_REDIRECTED    => 'REDIRECTED',
578
            \STREAM_NOTIFY_REDIRECTED => 'REDIRECTED',
551
            \STREAM_NOTIFY_PROGRESS      => 'PROGRESS',
579
            \STREAM_NOTIFY_PROGRESS => 'PROGRESS',
552
            \STREAM_NOTIFY_FAILURE       => 'FAILURE',
580
            \STREAM_NOTIFY_FAILURE => 'FAILURE',
553
            \STREAM_NOTIFY_COMPLETED     => 'COMPLETED',
581
            \STREAM_NOTIFY_COMPLETED => 'COMPLETED',
554
            \STREAM_NOTIFY_RESOLVE       => 'RESOLVE',
582
            \STREAM_NOTIFY_RESOLVE => 'RESOLVE',
555
        ];
583
        ];
Línea 556... Línea 584...
556
        static $args = ['severity', 'message', 'message_code', 'bytes_transferred', 'bytes_max'];
584
        static $args = ['severity', 'message', 'message_code', 'bytes_transferred', 'bytes_max'];
557
 
585
 
558
        $value = Utils::debugResource($value);
586
        $value = Utils::debugResource($value);
559
        $ident = $request->getMethod() . ' ' . $request->getUri()->withFragment('');
587
        $ident = $request->getMethod().' '.$request->getUri()->withFragment('');
560
        self::addNotification(
588
        self::addNotification(
561
            $params,
589
            $params,
562
            static function (int $code, ...$passed) use ($ident, $value, $map, $args): void {
590
            static function (int $code, ...$passed) use ($ident, $value, $map, $args): void {
563
                \fprintf($value, '<%s> [%s] ', $ident, $map[$code]);
591
                \fprintf($value, '<%s> [%s] ', $ident, $map[$code]);
564
                foreach (\array_filter($passed) as $i => $v) {
592
                foreach (\array_filter($passed) as $i => $v) {
565
                    \fwrite($value, $args[$i] . ': "' . $v . '" ');
593
                    \fwrite($value, $args[$i].': "'.$v.'" ');
566
                }
594
                }
567
                \fwrite($value, "\n");
595
                \fwrite($value, "\n");
568
            }
596
            }
Línea 575... Línea 603...
575
        if (!isset($params['notification'])) {
603
        if (!isset($params['notification'])) {
576
            $params['notification'] = $notify;
604
            $params['notification'] = $notify;
577
        } else {
605
        } else {
578
            $params['notification'] = self::callArray([
606
            $params['notification'] = self::callArray([
579
                $params['notification'],
607
                $params['notification'],
580
                $notify
608
                $notify,
581
            ]);
609
            ]);
582
        }
610
        }
583
    }
611
    }
Línea 584... Línea 612...
584
 
612