Completed
Push — master ( e52a94...a1c4a7 )
by Márk
02:18
created

StreamHandler   D

Complexity

Total Complexity 82

Size/Duplication

Total Lines 491
Duplicated Lines 2.24 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 11
loc 491
rs 4.5142
c 0
b 0
f 0
wmc 82
lcom 1
cbo 11

17 Methods

Rating   Name   Duplication   Size   Complexity  
A invokeStats() 0 18 2
A createSink() 0 14 4
B createResource() 0 27 4
D __invoke() 0 43 9
B createResponse() 0 47 6
C checkDecode() 11 33 7
A drain() 0 20 3
C createStream() 0 78 17
B getDefaultContext() 0 33 5
B add_proxy() 0 18 5
A add_timeout() 0 6 2
B add_verify() 0 25 6
A add_cert() 0 13 3
A add_progress() 0 11 2
B add_debug() 0 36 3
A addNotification() 0 12 2
A callArray() 0 9 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like StreamHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use StreamHandler, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace GuzzleHttp\Handler;
3
4
use GuzzleHttp\Exception\RequestException;
5
use GuzzleHttp\Exception\ConnectException;
6
use GuzzleHttp\Promise\FulfilledPromise;
7
use GuzzleHttp\Promise\RejectedPromise;
8
use GuzzleHttp\Promise\PromiseInterface;
9
use GuzzleHttp\Psr7;
10
use GuzzleHttp\TransferStats;
11
use Psr\Http\Message\RequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\StreamInterface;
14
15
/**
16
 * HTTP handler that uses PHP's HTTP stream wrapper.
17
 */
18
class StreamHandler
19
{
20
    private $lastHeaders = [];
21
22
    /**
23
     * Sends an HTTP request.
24
     *
25
     * @param RequestInterface $request Request to send.
26
     * @param array            $options Request transfer options.
27
     *
28
     * @return PromiseInterface
29
     */
30
    public function __invoke(RequestInterface $request, array $options)
31
    {
32
        // Sleep if there is a delay specified.
33
        if (isset($options['delay'])) {
34
            usleep($options['delay'] * 1000);
35
        }
36
37
        $startTime = isset($options['on_stats']) ? microtime(true) : null;
38
39
        try {
40
            // Does not support the expect header.
41
            $request = $request->withoutHeader('Expect');
42
43
            // Append a content-length header if body size is zero to match
44
            // cURL's behavior.
45
            if (0 === $request->getBody()->getSize()) {
46
                $request = $request->withHeader('Content-Length', 0);
47
            }
48
49
            return $this->createResponse(
50
                $request,
51
                $options,
52
                $this->createStream($request, $options),
53
                $startTime
54
            );
55
        } catch (\InvalidArgumentException $e) {
56
            throw $e;
57
        } catch (\Exception $e) {
58
            // Determine if the error was a networking error.
59
            $message = $e->getMessage();
60
            // This list can probably get more comprehensive.
61
            if (strpos($message, 'getaddrinfo') // DNS lookup failed
62
                || strpos($message, 'Connection refused')
63
                || strpos($message, "couldn't connect to host") // error on HHVM
64
            ) {
65
                $e = new ConnectException($e->getMessage(), $request, $e);
66
            }
67
            $e = RequestException::wrapException($request, $e);
68
            $this->invokeStats($options, $request, $startTime, null, $e);
69
70
            return new RejectedPromise($e);
71
        }
72
    }
73
74
    private function invokeStats(
75
        array $options,
76
        RequestInterface $request,
77
        $startTime,
78
        ResponseInterface $response = null,
79
        $error = null
80
    ) {
81
        if (isset($options['on_stats'])) {
82
            $stats = new TransferStats(
83
                $request,
84
                $response,
85
                microtime(true) - $startTime,
86
                $error,
87
                []
88
            );
89
            call_user_func($options['on_stats'], $stats);
90
        }
91
    }
92
93
    private function createResponse(
94
        RequestInterface $request,
95
        array $options,
96
        $stream,
97
        $startTime
98
    ) {
99
        $hdrs = $this->lastHeaders;
100
        $this->lastHeaders = [];
101
        $parts = explode(' ', array_shift($hdrs), 3);
102
        $ver = explode('/', $parts[0])[1];
103
        $status = $parts[1];
104
        $reason = isset($parts[2]) ? $parts[2] : null;
105
        $headers = \GuzzleHttp\headers_from_lines($hdrs);
106
        list ($stream, $headers) = $this->checkDecode($options, $headers, $stream);
107
        $stream = Psr7\stream_for($stream);
108
        $sink = $stream;
109
110
        if (strcasecmp('HEAD', $request->getMethod())) {
111
            $sink = $this->createSink($stream, $options);
112
        }
113
114
        $response = new Psr7\Response($status, $headers, $sink, $ver, $reason);
115
116
        if (isset($options['on_headers'])) {
117
            try {
118
                $options['on_headers']($response);
119
            } catch (\Exception $e) {
120
                $msg = 'An error was encountered during the on_headers event';
121
                $ex = new RequestException($msg, $request, $response, $e);
122
                return new RejectedPromise($ex);
123
            }
124
        }
125
126
        // Do not drain when the request is a HEAD request because they have
127
        // no body.
128
        if ($sink !== $stream) {
129
            $this->drain(
130
                $stream,
131
                $sink,
132
                $response->getHeaderLine('Content-Length')
133
            );
134
        }
135
136
        $this->invokeStats($options, $request, $startTime, $response, null);
137
138
        return new FulfilledPromise($response);
139
    }
140
141
    private function createSink(StreamInterface $stream, array $options)
142
    {
143
        if (!empty($options['stream'])) {
144
            return $stream;
145
        }
146
147
        $sink = isset($options['sink'])
148
            ? $options['sink']
149
            : fopen('php://temp', 'r+');
150
151
        return is_string($sink)
152
            ? new Psr7\LazyOpenStream($sink, 'w+')
153
            : Psr7\stream_for($sink);
154
    }
155
156
    private function checkDecode(array $options, array $headers, $stream)
157
    {
158
        // Automatically decode responses when instructed.
159
        if (!empty($options['decode_content'])) {
160
            $normalizedKeys = \GuzzleHttp\normalize_header_keys($headers);
161
            if (isset($normalizedKeys['content-encoding'])) {
162
                $encoding = $headers[$normalizedKeys['content-encoding']];
163
                if ($encoding[0] === 'gzip' || $encoding[0] === 'deflate') {
164
                    $stream = new Psr7\InflateStream(
165
                        Psr7\stream_for($stream)
166
                    );
167
                    $headers['x-encoded-content-encoding']
168
                        = $headers[$normalizedKeys['content-encoding']];
169
                    // Remove content-encoding header
170
                    unset($headers[$normalizedKeys['content-encoding']]);
171
                    // Fix content-length header
172 View Code Duplication
                    if (isset($normalizedKeys['content-length'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
                        $headers['x-encoded-content-length']
174
                            = $headers[$normalizedKeys['content-length']];
175
176
                        $length = (int) $stream->getSize();
177
                        if ($length === 0) {
178
                            unset($headers[$normalizedKeys['content-length']]);
179
                        } else {
180
                            $headers[$normalizedKeys['content-length']] = [$length];
181
                        }
182
                    }
183
                }
184
            }
185
        }
186
187
        return [$stream, $headers];
188
    }
189
190
    /**
191
     * Drains the source stream into the "sink" client option.
192
     *
193
     * @param StreamInterface $source
194
     * @param StreamInterface $sink
195
     * @param string          $contentLength Header specifying the amount of
196
     *                                       data to read.
197
     *
198
     * @return StreamInterface
199
     * @throws \RuntimeException when the sink option is invalid.
200
     */
201
    private function drain(
202
        StreamInterface $source,
203
        StreamInterface $sink,
204
        $contentLength
205
    ) {
206
        // If a content-length header is provided, then stop reading once
207
        // that number of bytes has been read. This can prevent infinitely
208
        // reading from a stream when dealing with servers that do not honor
209
        // Connection: Close headers.
210
        Psr7\copy_to_stream(
211
            $source,
212
            $sink,
213
            (strlen($contentLength) > 0 && (int) $contentLength > 0) ? (int) $contentLength : -1
214
        );
215
216
        $sink->seek(0);
217
        $source->close();
218
219
        return $sink;
220
    }
221
222
    /**
223
     * Create a resource and check to ensure it was created successfully
224
     *
225
     * @param callable $callback Callable that returns stream resource
226
     *
227
     * @return resource
228
     * @throws \RuntimeException on error
229
     */
230
    private function createResource(callable $callback)
231
    {
232
        $errors = null;
233
        set_error_handler(function ($_, $msg, $file, $line) use (&$errors) {
234
            $errors[] = [
235
                'message' => $msg,
236
                'file'    => $file,
237
                'line'    => $line
238
            ];
239
            return true;
240
        });
241
242
        $resource = $callback();
243
        restore_error_handler();
244
245
        if (!$resource) {
246
            $message = 'Error creating resource: ';
247
            foreach ($errors as $err) {
0 ignored issues
show
Bug introduced by
The expression $errors of type null is not traversable.
Loading history...
248
                foreach ($err as $key => $value) {
249
                    $message .= "[$key] $value" . PHP_EOL;
250
                }
251
            }
252
            throw new \RuntimeException(trim($message));
253
        }
254
255
        return $resource;
256
    }
257
258
    private function createStream(RequestInterface $request, array $options)
259
    {
260
        static $methods;
261
        if (!$methods) {
262
            $methods = array_flip(get_class_methods(__CLASS__));
263
        }
264
265
        // HTTP/1.1 streams using the PHP stream wrapper require a
266
        // Connection: close header
267
        if ($request->getProtocolVersion() == '1.1'
268
            && !$request->hasHeader('Connection')
269
        ) {
270
            $request = $request->withHeader('Connection', 'close');
271
        }
272
273
        // Ensure SSL is verified by default
274
        if (!isset($options['verify'])) {
275
            $options['verify'] = true;
276
        }
277
278
        $params = [];
279
        $context = $this->getDefaultContext($request, $options);
0 ignored issues
show
Unused Code introduced by
The call to StreamHandler::getDefaultContext() has too many arguments starting with $options.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
280
281
        if (isset($options['on_headers']) && !is_callable($options['on_headers'])) {
282
            throw new \InvalidArgumentException('on_headers must be callable');
283
        }
284
285
        if (!empty($options)) {
286
            foreach ($options as $key => $value) {
287
                $method = "add_{$key}";
288
                if (isset($methods[$method])) {
289
                    $this->{$method}($request, $context, $value, $params);
290
                }
291
            }
292
        }
293
294
        if (isset($options['stream_context'])) {
295
            if (!is_array($options['stream_context'])) {
296
                throw new \InvalidArgumentException('stream_context must be an array');
297
            }
298
            $context = array_replace_recursive(
299
                $context,
300
                $options['stream_context']
301
            );
302
        }
303
304
        // Microsoft NTLM authentication only supported with curl handler
305
        if (isset($options['auth'])
306
            && is_array($options['auth'])
307
            && isset($options['auth'][2])
308
            && 'ntlm' == $options['auth'][2]
309
        ) {
310
311
            throw new \InvalidArgumentException('Microsoft NTLM authentication only supported with curl handler');
312
        }
313
314
        $context = $this->createResource(
315
            function () use ($context, $params) {
316
                return stream_context_create($context, $params);
317
            }
318
        );
319
320
        return $this->createResource(
321
            function () use ($request, &$http_response_header, $context, $options) {
322
                $resource = fopen((string) $request->getUri()->withFragment(''), 'r', null, $context);
323
                $this->lastHeaders = $http_response_header;
324
325
                if (isset($options['read_timeout'])) {
326
                    $readTimeout = $options['read_timeout'];
327
                    $sec = (int) $readTimeout;
328
                    $usec = ($readTimeout - $sec) * 100000;
329
                    stream_set_timeout($resource, $sec, $usec);
330
                }
331
332
                return $resource;
333
            }
334
        );
335
    }
336
337
    private function getDefaultContext(RequestInterface $request)
338
    {
339
        $headers = '';
340
        foreach ($request->getHeaders() as $name => $value) {
341
            foreach ($value as $val) {
342
                $headers .= "$name: $val\r\n";
343
            }
344
        }
345
346
        $context = [
347
            'http' => [
348
                'method'           => $request->getMethod(),
349
                'header'           => $headers,
350
                'protocol_version' => $request->getProtocolVersion(),
351
                'ignore_errors'    => true,
352
                'follow_location'  => 0,
353
            ],
354
        ];
355
356
        $body = (string) $request->getBody();
357
358
        if (!empty($body)) {
359
            $context['http']['content'] = $body;
360
            // Prevent the HTTP handler from adding a Content-Type header.
361
            if (!$request->hasHeader('Content-Type')) {
362
                $context['http']['header'] .= "Content-Type:\r\n";
363
            }
364
        }
365
366
        $context['http']['header'] = rtrim($context['http']['header']);
367
368
        return $context;
369
    }
370
371
    private function add_proxy(RequestInterface $request, &$options, $value, &$params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
372
    {
373
        if (!is_array($value)) {
374
            $options['http']['proxy'] = $value;
375
        } else {
376
            $scheme = $request->getUri()->getScheme();
377
            if (isset($value[$scheme])) {
378
                if (!isset($value['no'])
379
                    || !\GuzzleHttp\is_host_in_noproxy(
380
                        $request->getUri()->getHost(),
381
                        $value['no']
382
                    )
383
                ) {
384
                    $options['http']['proxy'] = $value[$scheme];
385
                }
386
            }
387
        }
388
    }
389
390
    private function add_timeout(RequestInterface $request, &$options, $value, &$params)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
391
    {
392
        if ($value > 0) {
393
            $options['http']['timeout'] = $value;
394
        }
395
    }
396
397
    private function add_verify(RequestInterface $request, &$options, $value, &$params)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
398
    {
399
        if ($value === true) {
400
            // PHP 5.6 or greater will find the system cert by default. When
401
            // < 5.6, use the Guzzle bundled cacert.
402
            if (PHP_VERSION_ID < 50600) {
403
                $options['ssl']['cafile'] = \GuzzleHttp\default_ca_bundle();
404
            }
405
        } elseif (is_string($value)) {
406
            $options['ssl']['cafile'] = $value;
407
            if (!file_exists($value)) {
408
                throw new \RuntimeException("SSL CA bundle not found: $value");
409
            }
410
        } elseif ($value === false) {
411
            $options['ssl']['verify_peer'] = false;
412
            $options['ssl']['verify_peer_name'] = false;
413
            return;
414
        } else {
415
            throw new \InvalidArgumentException('Invalid verify request option');
416
        }
417
418
        $options['ssl']['verify_peer'] = true;
419
        $options['ssl']['verify_peer_name'] = true;
420
        $options['ssl']['allow_self_signed'] = false;
421
    }
422
423
    private function add_cert(RequestInterface $request, &$options, $value, &$params)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
424
    {
425
        if (is_array($value)) {
426
            $options['ssl']['passphrase'] = $value[1];
427
            $value = $value[0];
428
        }
429
430
        if (!file_exists($value)) {
431
            throw new \RuntimeException("SSL certificate not found: {$value}");
432
        }
433
434
        $options['ssl']['local_cert'] = $value;
435
    }
436
437
    private function add_progress(RequestInterface $request, &$options, $value, &$params)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
438
    {
439
        $this->addNotification(
440
            $params,
441
            function ($code, $a, $b, $c, $transferred, $total) use ($value) {
442
                if ($code == STREAM_NOTIFY_PROGRESS) {
443
                    $value($total, $transferred, null, null);
444
                }
445
            }
446
        );
447
    }
448
449
    private function add_debug(RequestInterface $request, &$options, $value, &$params)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
450
    {
451
        if ($value === false) {
452
            return;
453
        }
454
455
        static $map = [
456
            STREAM_NOTIFY_CONNECT       => 'CONNECT',
457
            STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED',
458
            STREAM_NOTIFY_AUTH_RESULT   => 'AUTH_RESULT',
459
            STREAM_NOTIFY_MIME_TYPE_IS  => 'MIME_TYPE_IS',
460
            STREAM_NOTIFY_FILE_SIZE_IS  => 'FILE_SIZE_IS',
461
            STREAM_NOTIFY_REDIRECTED    => 'REDIRECTED',
462
            STREAM_NOTIFY_PROGRESS      => 'PROGRESS',
463
            STREAM_NOTIFY_FAILURE       => 'FAILURE',
464
            STREAM_NOTIFY_COMPLETED     => 'COMPLETED',
465
            STREAM_NOTIFY_RESOLVE       => 'RESOLVE',
466
        ];
467
        static $args = ['severity', 'message', 'message_code',
468
            'bytes_transferred', 'bytes_max'];
469
470
        $value = \GuzzleHttp\debug_resource($value);
471
        $ident = $request->getMethod() . ' ' . $request->getUri()->withFragment('');
472
        $this->addNotification(
473
            $params,
474
            function () use ($ident, $value, $map, $args) {
475
                $passed = func_get_args();
476
                $code = array_shift($passed);
477
                fprintf($value, '<%s> [%s] ', $ident, $map[$code]);
478
                foreach (array_filter($passed) as $i => $v) {
479
                    fwrite($value, $args[$i] . ': "' . $v . '" ');
480
                }
481
                fwrite($value, "\n");
482
            }
483
        );
484
    }
485
486
    private function addNotification(array &$params, callable $notify)
487
    {
488
        // Wrap the existing function if needed.
489
        if (!isset($params['notification'])) {
490
            $params['notification'] = $notify;
491
        } else {
492
            $params['notification'] = $this->callArray([
493
                $params['notification'],
494
                $notify
495
            ]);
496
        }
497
    }
498
499
    private function callArray(array $functions)
500
    {
501
        return function () use ($functions) {
502
            $args = func_get_args();
503
            foreach ($functions as $fn) {
504
                call_user_func_array($fn, $args);
505
            }
506
        };
507
    }
508
}
509