Completed
Push — master ( 221921...32345e )
by Bhanu
58:02
created

functions.php ➔ stream_for()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 29
nc 11
nop 2
dl 0
loc 38
rs 5.2653
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace GuzzleHttp\Psr7;
3
4
use Psr\Http\Message\MessageInterface;
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\StreamInterface;
8
use Psr\Http\Message\UriInterface;
9
10
/**
11
 * Returns the string representation of an HTTP message.
12
 *
13
 * @param MessageInterface $message Message to convert to a string.
14
 *
15
 * @return string
16
 */
17
function str(MessageInterface $message)
18
{
19
    if ($message instanceof RequestInterface) {
20
        $msg = trim($message->getMethod() . ' '
21
                . $message->getRequestTarget())
22
            . ' HTTP/' . $message->getProtocolVersion();
23
        if (!$message->hasHeader('host')) {
24
            $msg .= "\r\nHost: " . $message->getUri()->getHost();
25
        }
26
    } elseif ($message instanceof ResponseInterface) {
27
        $msg = 'HTTP/' . $message->getProtocolVersion() . ' '
28
            . $message->getStatusCode() . ' '
29
            . $message->getReasonPhrase();
30
    } else {
31
        throw new \InvalidArgumentException('Unknown message type');
32
    }
33
34
    foreach ($message->getHeaders() as $name => $values) {
35
        $msg .= "\r\n{$name}: " . implode(', ', $values);
36
    }
37
38
    return "{$msg}\r\n\r\n" . $message->getBody();
39
}
40
41
/**
42
 * Returns a UriInterface for the given value.
43
 *
44
 * This function accepts a string or {@see Psr\Http\Message\UriInterface} and
45
 * returns a UriInterface for the given value. If the value is already a
46
 * `UriInterface`, it is returned as-is.
47
 *
48
 * @param string|UriInterface $uri
49
 *
50
 * @return UriInterface
51
 * @throws \InvalidArgumentException
52
 */
53
function uri_for($uri)
54
{
55
    if ($uri instanceof UriInterface) {
56
        return $uri;
57
    } elseif (is_string($uri)) {
58
        return new Uri($uri);
59
    }
60
61
    throw new \InvalidArgumentException('URI must be a string or UriInterface');
62
}
63
64
/**
65
 * Create a new stream based on the input type.
66
 *
67
 * Options is an associative array that can contain the following keys:
68
 * - metadata: Array of custom metadata.
69
 * - size: Size of the stream.
70
 *
71
 * @param resource|string|StreamInterface $resource Entity body data
72
 * @param array                           $options  Additional options
73
 *
74
 * @return Stream
75
 * @throws \InvalidArgumentException if the $resource arg is not valid.
76
 */
77
function stream_for($resource = '', array $options = [])
78
{
79
    switch (gettype($resource)) {
80
        case 'string':
81
            $stream = fopen('php://temp', 'r+');
82
            if ($resource !== '') {
83
                fwrite($stream, $resource);
84
                fseek($stream, 0);
85
            }
86
            return new Stream($stream, $options);
87
        case 'resource':
88
            return new Stream($resource, $options);
89
        case 'object':
90
            if ($resource instanceof StreamInterface) {
91
                return $resource;
92
            } elseif ($resource instanceof \Iterator) {
93
                return new PumpStream(function () use ($resource) {
94
                    if (!$resource->valid()) {
95
                        return false;
96
                    }
97
                    $result = $resource->current();
98
                    $resource->next();
99
                    return $result;
100
                }, $options);
101
            } elseif (method_exists($resource, '__toString')) {
102
                return stream_for((string) $resource, $options);
103
            }
104
            break;
105
        case 'NULL':
106
            return new Stream(fopen('php://temp', 'r+'), $options);
107
    }
108
109
    if (is_callable($resource)) {
110
        return new PumpStream($resource, $options);
111
    }
112
113
    throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource));
114
}
115
116
/**
117
 * Parse an array of header values containing ";" separated data into an
118
 * array of associative arrays representing the header key value pair
119
 * data of the header. When a parameter does not contain a value, but just
120
 * contains a key, this function will inject a key with a '' string value.
121
 *
122
 * @param string|array $header Header to parse into components.
123
 *
124
 * @return array Returns the parsed header values.
125
 */
126
function parse_header($header)
127
{
128
    static $trimmed = "\"'  \n\t\r";
129
    $params = $matches = [];
130
131
    foreach (normalize_header($header) as $val) {
132
        $part = [];
133
        foreach (preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) as $kvp) {
134
            if (preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) {
135
                $m = $matches[0];
136
                if (isset($m[1])) {
137
                    $part[trim($m[0], $trimmed)] = trim($m[1], $trimmed);
138
                } else {
139
                    $part[] = trim($m[0], $trimmed);
140
                }
141
            }
142
        }
143
        if ($part) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $part of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
144
            $params[] = $part;
145
        }
146
    }
147
148
    return $params;
149
}
150
151
/**
152
 * Converts an array of header values that may contain comma separated
153
 * headers into an array of headers with no comma separated values.
154
 *
155
 * @param string|array $header Header to normalize.
156
 *
157
 * @return array Returns the normalized header field values.
158
 */
159
function normalize_header($header)
160
{
161
    if (!is_array($header)) {
162
        return array_map('trim', explode(',', $header));
163
    }
164
165
    $result = [];
166
    foreach ($header as $value) {
167
        foreach ((array) $value as $v) {
168
            if (strpos($v, ',') === false) {
169
                $result[] = $v;
170
                continue;
171
            }
172
            foreach (preg_split('/,(?=([^"]*"[^"]*")*[^"]*$)/', $v) as $vv) {
173
                $result[] = trim($vv);
174
            }
175
        }
176
    }
177
178
    return $result;
179
}
180
181
/**
182
 * Clone and modify a request with the given changes.
183
 *
184
 * The changes can be one of:
185
 * - method: (string) Changes the HTTP method.
186
 * - set_headers: (array) Sets the given headers.
187
 * - remove_headers: (array) Remove the given headers.
188
 * - body: (mixed) Sets the given body.
189
 * - uri: (UriInterface) Set the URI.
190
 * - query: (string) Set the query string value of the URI.
191
 * - version: (string) Set the protocol version.
192
 *
193
 * @param RequestInterface $request Request to clone and modify.
194
 * @param array            $changes Changes to apply.
195
 *
196
 * @return RequestInterface
197
 */
198
function modify_request(RequestInterface $request, array $changes)
199
{
200
    if (!$changes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $changes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
201
        return $request;
202
    }
203
204
    $headers = $request->getHeaders();
205
206
    if (!isset($changes['uri'])) {
207
        $uri = $request->getUri();
208
    } else {
209
        // Remove the host header if one is on the URI
210
        if ($host = $changes['uri']->getHost()) {
211
            $changes['set_headers']['Host'] = $host;
212
213
            if ($port = $changes['uri']->getPort()) {
214
                $standardPorts = ['http' => 80, 'https' => 443];
215
                $scheme = $changes['uri']->getScheme();
216
                if (isset($standardPorts[$scheme]) && $port != $standardPorts[$scheme]) {
217
                    $changes['set_headers']['Host'] .= ':'.$port;
218
                }
219
            }
220
        }
221
        $uri = $changes['uri'];
222
    }
223
224
    if (!empty($changes['remove_headers'])) {
225
        $headers = _caseless_remove($changes['remove_headers'], $headers);
226
    }
227
228
    if (!empty($changes['set_headers'])) {
229
        $headers = _caseless_remove(array_keys($changes['set_headers']), $headers);
230
        $headers = $changes['set_headers'] + $headers;
231
    }
232
233
    if (isset($changes['query'])) {
234
        $uri = $uri->withQuery($changes['query']);
235
    }
236
237
    return new Request(
238
        isset($changes['method']) ? $changes['method'] : $request->getMethod(),
239
        $uri,
240
        $headers,
241
        isset($changes['body']) ? $changes['body'] : $request->getBody(),
242
        isset($changes['version'])
243
            ? $changes['version']
244
            : $request->getProtocolVersion()
245
    );
246
}
247
248
/**
249
 * Attempts to rewind a message body and throws an exception on failure.
250
 *
251
 * The body of the message will only be rewound if a call to `tell()` returns a
252
 * value other than `0`.
253
 *
254
 * @param MessageInterface $message Message to rewind
255
 *
256
 * @throws \RuntimeException
257
 */
258
function rewind_body(MessageInterface $message)
259
{
260
    $body = $message->getBody();
261
262
    if ($body->tell()) {
263
        $body->rewind();
264
    }
265
}
266
267
/**
268
 * Safely opens a PHP stream resource using a filename.
269
 *
270
 * When fopen fails, PHP normally raises a warning. This function adds an
271
 * error handler that checks for errors and throws an exception instead.
272
 *
273
 * @param string $filename File to open
274
 * @param string $mode     Mode used to open the file
275
 *
276
 * @return resource
277
 * @throws \RuntimeException if the file cannot be opened
278
 */
279
function try_fopen($filename, $mode)
280
{
281
    $ex = null;
282
    set_error_handler(function () use ($filename, $mode, &$ex) {
283
        $ex = new \RuntimeException(sprintf(
284
            'Unable to open %s using mode %s: %s',
285
            $filename,
286
            $mode,
287
            func_get_args()[1]
288
        ));
289
    });
290
291
    $handle = fopen($filename, $mode);
292
    restore_error_handler();
293
294
    if ($ex) {
295
        /** @var $ex \RuntimeException */
296
        throw $ex;
297
    }
298
299
    return $handle;
300
}
301
302
/**
303
 * Copy the contents of a stream into a string until the given number of
304
 * bytes have been read.
305
 *
306
 * @param StreamInterface $stream Stream to read
307
 * @param int             $maxLen Maximum number of bytes to read. Pass -1
308
 *                                to read the entire stream.
309
 * @return string
310
 * @throws \RuntimeException on error.
311
 */
312
function copy_to_string(StreamInterface $stream, $maxLen = -1)
313
{
314
    $buffer = '';
315
316
    if ($maxLen === -1) {
317
        while (!$stream->eof()) {
318
            $buf = $stream->read(1048576);
319
            // Using a loose equality here to match on '' and false.
320
            if ($buf == null) {
321
                break;
322
            }
323
            $buffer .= $buf;
324
        }
325
        return $buffer;
326
    }
327
328
    $len = 0;
329
    while (!$stream->eof() && $len < $maxLen) {
330
        $buf = $stream->read($maxLen - $len);
331
        // Using a loose equality here to match on '' and false.
332
        if ($buf == null) {
333
            break;
334
        }
335
        $buffer .= $buf;
336
        $len = strlen($buffer);
337
    }
338
339
    return $buffer;
340
}
341
342
/**
343
 * Copy the contents of a stream into another stream until the given number
344
 * of bytes have been read.
345
 *
346
 * @param StreamInterface $source Stream to read from
347
 * @param StreamInterface $dest   Stream to write to
348
 * @param int             $maxLen Maximum number of bytes to read. Pass -1
349
 *                                to read the entire stream.
350
 *
351
 * @throws \RuntimeException on error.
352
 */
353
function copy_to_stream(
354
    StreamInterface $source,
355
    StreamInterface $dest,
356
    $maxLen = -1
357
) {
358
    if ($maxLen === -1) {
359
        while (!$source->eof()) {
360
            if (!$dest->write($source->read(1048576))) {
361
                break;
362
            }
363
        }
364
        return;
365
    }
366
367
    $bytes = 0;
368
    while (!$source->eof()) {
369
        $buf = $source->read($maxLen - $bytes);
370
        if (!($len = strlen($buf))) {
371
            break;
372
        }
373
        $bytes += $len;
374
        $dest->write($buf);
375
        if ($bytes == $maxLen) {
376
            break;
377
        }
378
    }
379
}
380
381
/**
382
 * Calculate a hash of a Stream
383
 *
384
 * @param StreamInterface $stream    Stream to calculate the hash for
385
 * @param string          $algo      Hash algorithm (e.g. md5, crc32, etc)
386
 * @param bool            $rawOutput Whether or not to use raw output
387
 *
388
 * @return string Returns the hash of the stream
389
 * @throws \RuntimeException on error.
390
 */
391
function hash(
392
    StreamInterface $stream,
393
    $algo,
394
    $rawOutput = false
395
) {
396
    $pos = $stream->tell();
397
398
    if ($pos > 0) {
399
        $stream->rewind();
400
    }
401
402
    $ctx = hash_init($algo);
403
    while (!$stream->eof()) {
404
        hash_update($ctx, $stream->read(1048576));
405
    }
406
407
    $out = hash_final($ctx, (bool) $rawOutput);
408
    $stream->seek($pos);
409
410
    return $out;
411
}
412
413
/**
414
 * Read a line from the stream up to the maximum allowed buffer length
415
 *
416
 * @param StreamInterface $stream    Stream to read from
417
 * @param int             $maxLength Maximum buffer length
418
 *
419
 * @return string|bool
420
 */
421
function readline(StreamInterface $stream, $maxLength = null)
422
{
423
    $buffer = '';
424
    $size = 0;
425
426
    while (!$stream->eof()) {
427
        // Using a loose equality here to match on '' and false.
428
        if (null == ($byte = $stream->read(1))) {
429
            return $buffer;
430
        }
431
        $buffer .= $byte;
432
        // Break when a new line is found or the max length - 1 is reached
433
        if ($byte == PHP_EOL || ++$size == $maxLength - 1) {
434
            break;
435
        }
436
    }
437
438
    return $buffer;
439
}
440
441
/**
442
 * Parses a request message string into a request object.
443
 *
444
 * @param string $message Request message string.
445
 *
446
 * @return Request
447
 */
448
function parse_request($message)
449
{
450
    $data = _parse_message($message);
451
    $matches = [];
452
    if (!preg_match('/^[a-zA-Z]+\s+([a-zA-Z]+:\/\/|\/).*/', $data['start-line'], $matches)) {
453
        throw new \InvalidArgumentException('Invalid request string');
454
    }
455
    $parts = explode(' ', $data['start-line'], 3);
456
    $version = isset($parts[2]) ? explode('/', $parts[2])[1] : '1.1';
457
458
    $request = new Request(
459
        $parts[0],
460
        $matches[1] === '/' ? _parse_request_uri($parts[1], $data['headers']) : $parts[1],
461
        $data['headers'],
462
        $data['body'],
463
        $version
464
    );
465
466
    return $matches[1] === '/' ? $request : $request->withRequestTarget($parts[1]);
467
}
468
469
/**
470
 * Parses a response message string into a response object.
471
 *
472
 * @param string $message Response message string.
473
 *
474
 * @return Response
475
 */
476
function parse_response($message)
477
{
478
    $data = _parse_message($message);
479
    if (!preg_match('/^HTTP\/.* [0-9]{3} .*/', $data['start-line'])) {
480
        throw new \InvalidArgumentException('Invalid response string');
481
    }
482
    $parts = explode(' ', $data['start-line'], 3);
483
484
    return new Response(
485
        $parts[1],
486
        $data['headers'],
487
        $data['body'],
488
        explode('/', $parts[0])[1],
489
        isset($parts[2]) ? $parts[2] : null
490
    );
491
}
492
493
/**
494
 * Parse a query string into an associative array.
495
 *
496
 * If multiple values are found for the same key, the value of that key
497
 * value pair will become an array. This function does not parse nested
498
 * PHP style arrays into an associative array (e.g., foo[a]=1&foo[b]=2 will
499
 * be parsed into ['foo[a]' => '1', 'foo[b]' => '2']).
500
 *
501
 * @param string      $str         Query string to parse
502
 * @param bool|string $urlEncoding How the query string is encoded
503
 *
504
 * @return array
505
 */
506
function parse_query($str, $urlEncoding = true)
507
{
508
    $result = [];
509
510
    if ($str === '') {
511
        return $result;
512
    }
513
514
    if ($urlEncoding === true) {
515
        $decoder = function ($value) {
516
            return rawurldecode(str_replace('+', ' ', $value));
517
        };
518
    } elseif ($urlEncoding == PHP_QUERY_RFC3986) {
519
        $decoder = 'rawurldecode';
520
    } elseif ($urlEncoding == PHP_QUERY_RFC1738) {
521
        $decoder = 'urldecode';
522
    } else {
523
        $decoder = function ($str) { return $str; };
524
    }
525
526
    foreach (explode('&', $str) as $kvp) {
527
        $parts = explode('=', $kvp, 2);
528
        $key = $decoder($parts[0]);
529
        $value = isset($parts[1]) ? $decoder($parts[1]) : null;
530 View Code Duplication
        if (!isset($result[$key])) {
531
            $result[$key] = $value;
532
        } else {
533
            if (!is_array($result[$key])) {
534
                $result[$key] = [$result[$key]];
535
            }
536
            $result[$key][] = $value;
537
        }
538
    }
539
540
    return $result;
541
}
542
543
/**
544
 * Build a query string from an array of key value pairs.
545
 *
546
 * This function can use the return value of parseQuery() to build a query
547
 * string. This function does not modify the provided keys when an array is
548
 * encountered (like http_build_query would).
549
 *
550
 * @param array     $params   Query string parameters.
551
 * @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
552
 *                            to encode using RFC3986, or PHP_QUERY_RFC1738
553
 *                            to encode using RFC1738.
554
 * @return string
555
 */
556
function build_query(array $params, $encoding = PHP_QUERY_RFC3986)
557
{
558
    if (!$params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
559
        return '';
560
    }
561
562
    if ($encoding === false) {
563
        $encoder = function ($str) { return $str; };
564
    } elseif ($encoding == PHP_QUERY_RFC3986) {
565
        $encoder = 'rawurlencode';
566
    } elseif ($encoding == PHP_QUERY_RFC1738) {
567
        $encoder = 'urlencode';
568
    } else {
569
        throw new \InvalidArgumentException('Invalid type');
570
    }
571
572
    $qs = '';
573
    foreach ($params as $k => $v) {
574
        $k = $encoder($k);
575
        if (!is_array($v)) {
576
            $qs .= $k;
577
            if ($v !== null) {
578
                $qs .= '=' . $encoder($v);
579
            }
580
            $qs .= '&';
581
        } else {
582
            foreach ($v as $vv) {
583
                $qs .= $k;
584
                if ($vv !== null) {
585
                    $qs .= '=' . $encoder($vv);
586
                }
587
                $qs .= '&';
588
            }
589
        }
590
    }
591
592
    return $qs ? (string) substr($qs, 0, -1) : '';
593
}
594
595
/**
596
 * Determines the mimetype of a file by looking at its extension.
597
 *
598
 * @param $filename
599
 *
600
 * @return null|string
601
 */
602
function mimetype_from_filename($filename)
603
{
604
    return mimetype_from_extension(pathinfo($filename, PATHINFO_EXTENSION));
605
}
606
607
/**
608
 * Maps a file extensions to a mimetype.
609
 *
610
 * @param $extension string The file extension.
611
 *
612
 * @return string|null
613
 * @link http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types
614
 */
615
function mimetype_from_extension($extension)
616
{
617
    static $mimetypes = [
618
        '7z' => 'application/x-7z-compressed',
619
        'aac' => 'audio/x-aac',
620
        'ai' => 'application/postscript',
621
        'aif' => 'audio/x-aiff',
622
        'asc' => 'text/plain',
623
        'asf' => 'video/x-ms-asf',
624
        'atom' => 'application/atom+xml',
625
        'avi' => 'video/x-msvideo',
626
        'bmp' => 'image/bmp',
627
        'bz2' => 'application/x-bzip2',
628
        'cer' => 'application/pkix-cert',
629
        'crl' => 'application/pkix-crl',
630
        'crt' => 'application/x-x509-ca-cert',
631
        'css' => 'text/css',
632
        'csv' => 'text/csv',
633
        'cu' => 'application/cu-seeme',
634
        'deb' => 'application/x-debian-package',
635
        'doc' => 'application/msword',
636
        'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
637
        'dvi' => 'application/x-dvi',
638
        'eot' => 'application/vnd.ms-fontobject',
639
        'eps' => 'application/postscript',
640
        'epub' => 'application/epub+zip',
641
        'etx' => 'text/x-setext',
642
        'flac' => 'audio/flac',
643
        'flv' => 'video/x-flv',
644
        'gif' => 'image/gif',
645
        'gz' => 'application/gzip',
646
        'htm' => 'text/html',
647
        'html' => 'text/html',
648
        'ico' => 'image/x-icon',
649
        'ics' => 'text/calendar',
650
        'ini' => 'text/plain',
651
        'iso' => 'application/x-iso9660-image',
652
        'jar' => 'application/java-archive',
653
        'jpe' => 'image/jpeg',
654
        'jpeg' => 'image/jpeg',
655
        'jpg' => 'image/jpeg',
656
        'js' => 'text/javascript',
657
        'json' => 'application/json',
658
        'latex' => 'application/x-latex',
659
        'log' => 'text/plain',
660
        'm4a' => 'audio/mp4',
661
        'm4v' => 'video/mp4',
662
        'mid' => 'audio/midi',
663
        'midi' => 'audio/midi',
664
        'mov' => 'video/quicktime',
665
        'mp3' => 'audio/mpeg',
666
        'mp4' => 'video/mp4',
667
        'mp4a' => 'audio/mp4',
668
        'mp4v' => 'video/mp4',
669
        'mpe' => 'video/mpeg',
670
        'mpeg' => 'video/mpeg',
671
        'mpg' => 'video/mpeg',
672
        'mpg4' => 'video/mp4',
673
        'oga' => 'audio/ogg',
674
        'ogg' => 'audio/ogg',
675
        'ogv' => 'video/ogg',
676
        'ogx' => 'application/ogg',
677
        'pbm' => 'image/x-portable-bitmap',
678
        'pdf' => 'application/pdf',
679
        'pgm' => 'image/x-portable-graymap',
680
        'png' => 'image/png',
681
        'pnm' => 'image/x-portable-anymap',
682
        'ppm' => 'image/x-portable-pixmap',
683
        'ppt' => 'application/vnd.ms-powerpoint',
684
        'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
685
        'ps' => 'application/postscript',
686
        'qt' => 'video/quicktime',
687
        'rar' => 'application/x-rar-compressed',
688
        'ras' => 'image/x-cmu-raster',
689
        'rss' => 'application/rss+xml',
690
        'rtf' => 'application/rtf',
691
        'sgm' => 'text/sgml',
692
        'sgml' => 'text/sgml',
693
        'svg' => 'image/svg+xml',
694
        'swf' => 'application/x-shockwave-flash',
695
        'tar' => 'application/x-tar',
696
        'tif' => 'image/tiff',
697
        'tiff' => 'image/tiff',
698
        'torrent' => 'application/x-bittorrent',
699
        'ttf' => 'application/x-font-ttf',
700
        'txt' => 'text/plain',
701
        'wav' => 'audio/x-wav',
702
        'webm' => 'video/webm',
703
        'wma' => 'audio/x-ms-wma',
704
        'wmv' => 'video/x-ms-wmv',
705
        'woff' => 'application/x-font-woff',
706
        'wsdl' => 'application/wsdl+xml',
707
        'xbm' => 'image/x-xbitmap',
708
        'xls' => 'application/vnd.ms-excel',
709
        'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
710
        'xml' => 'application/xml',
711
        'xpm' => 'image/x-xpixmap',
712
        'xwd' => 'image/x-xwindowdump',
713
        'yaml' => 'text/yaml',
714
        'yml' => 'text/yaml',
715
        'zip' => 'application/zip',
716
    ];
717
718
    $extension = strtolower($extension);
719
720
    return isset($mimetypes[$extension])
721
        ? $mimetypes[$extension]
722
        : null;
723
}
724
725
/**
726
 * Parses an HTTP message into an associative array.
727
 *
728
 * The array contains the "start-line" key containing the start line of
729
 * the message, "headers" key containing an associative array of header
730
 * array values, and a "body" key containing the body of the message.
731
 *
732
 * @param string $message HTTP request or response to parse.
733
 *
734
 * @return array
735
 * @internal
736
 */
737
function _parse_message($message)
738
{
739
    if (!$message) {
740
        throw new \InvalidArgumentException('Invalid message');
741
    }
742
743
    // Iterate over each line in the message, accounting for line endings
744
    $lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
745
    $result = ['start-line' => array_shift($lines), 'headers' => [], 'body' => ''];
746
    array_shift($lines);
747
748
    for ($i = 0, $totalLines = count($lines); $i < $totalLines; $i += 2) {
749
        $line = $lines[$i];
750
        // If two line breaks were encountered, then this is the end of body
751
        if (empty($line)) {
752
            if ($i < $totalLines - 1) {
753
                $result['body'] = implode('', array_slice($lines, $i + 2));
754
            }
755
            break;
756
        }
757
        if (strpos($line, ':')) {
758
            $parts = explode(':', $line, 2);
759
            $key = trim($parts[0]);
760
            $value = isset($parts[1]) ? trim($parts[1]) : '';
761
            $result['headers'][$key][] = $value;
762
        }
763
    }
764
765
    return $result;
766
}
767
768
/**
769
 * Constructs a URI for an HTTP request message.
770
 *
771
 * @param string $path    Path from the start-line
772
 * @param array  $headers Array of headers (each value an array).
773
 *
774
 * @return string
775
 * @internal
776
 */
777
function _parse_request_uri($path, array $headers)
778
{
779
    $hostKey = array_filter(array_keys($headers), function ($k) {
780
        return strtolower($k) === 'host';
781
    });
782
783
    // If no host is found, then a full URI cannot be constructed.
784
    if (!$hostKey) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $hostKey of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
785
        return $path;
786
    }
787
788
    $host = $headers[reset($hostKey)][0];
789
    $scheme = substr($host, -4) === ':443' ? 'https' : 'http';
790
791
    return $scheme . '://' . $host . '/' . ltrim($path, '/');
792
}
793
794
/** @internal */
795
function _caseless_remove($keys, array $data)
796
{
797
    $result = [];
798
799
    foreach ($keys as &$key) {
800
        $key = strtolower($key);
801
    }
802
803
    foreach ($data as $k => $v) {
804
        if (!in_array(strtolower($k), $keys)) {
805
            $result[$k] = $v;
806
        }
807
    }
808
809
    return $result;
810
}
811