1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Remote\Internals; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\UploadPerPartes\UploadException; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Client |
11
|
|
|
* @package kalanis\UploadPerPartes\Target\Remote\Internals |
12
|
|
|
* Remote query itself |
13
|
|
|
* @link https://www.php.net/manual/en/reserved.variables.httpresponseheader.php |
14
|
|
|
*/ |
15
|
|
|
class Client |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param RequestData $data |
19
|
|
|
* @throws UploadException |
20
|
|
|
* @return ResponseData |
21
|
|
|
* @link https://wiki.php.net/rfc/http-last-response-headers |
22
|
|
|
*/ |
23
|
2 |
|
public function request(RequestData $data): ResponseData |
24
|
|
|
{ |
25
|
2 |
|
$response = @file_get_contents($data->path, false, stream_context_create($data->context)); |
26
|
2 |
|
if (false === $response) { |
27
|
1 |
|
throw new UploadException('Bad request content', 503); |
28
|
|
|
} |
29
|
|
|
if ( |
30
|
1 |
|
function_exists('http_get_last_response_headers') |
31
|
1 |
|
&& function_exists('http_clear_last_response_headers') |
32
|
|
|
) { // 8.4+ |
33
|
|
|
// @codeCoverageIgnoreStart |
34
|
|
|
$http_response_header = http_get_last_response_headers(); |
35
|
|
|
http_clear_last_response_headers(); |
36
|
|
|
} |
37
|
1 |
|
if (false !== strpos($data->path, '://')) { |
38
|
|
|
if (empty($http_response_header)) { |
39
|
|
|
// @codeCoverageIgnoreStart |
40
|
|
|
throw new UploadException('Bad response headers', 503); |
41
|
|
|
} |
42
|
|
|
// @codeCoverageIgnoreEnd |
43
|
|
|
} else { |
44
|
|
|
$http_response_header = [ |
45
|
1 |
|
'HTTP/0.0 999', |
46
|
1 |
|
'local-file', |
47
|
1 |
|
'path:'.$data->path |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
// @codeCoverageIgnoreEnd |
51
|
1 |
|
return new ResponseData( |
52
|
1 |
|
empty($http_response_header) ? [] : $this->parseHeaders($http_response_header) |
53
|
|
|
, $response); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array<string> $headers |
58
|
|
|
* @return array{ |
|
|
|
|
59
|
|
|
* response_code?: int, |
60
|
|
|
* last-modified?: string, |
61
|
|
|
* accept-ranges?: string, |
62
|
|
|
* cache-control?: string, |
63
|
|
|
* expires?: string, |
64
|
|
|
* content-length?: int, |
65
|
|
|
* content-type?: string, |
66
|
|
|
* server?: string |
67
|
|
|
* }|array<string|int, string|int> |
68
|
|
|
*/ |
69
|
1 |
|
private function parseHeaders(array $headers): array |
70
|
|
|
{ |
71
|
1 |
|
$head = []; |
72
|
1 |
|
foreach ($headers as $k => $v) { |
73
|
1 |
|
$t = explode(':', $v, 2); |
74
|
1 |
|
if (isset($t[1])) { |
75
|
1 |
|
$head[trim($t[0])] = trim($t[1]); |
76
|
|
|
} else { |
77
|
1 |
|
$head[] = $v; |
78
|
1 |
|
if (preg_match("#HTTP/[0-9\.]+\s+([0-9]+)#", $v, $out)) { |
79
|
1 |
|
$head['response_code'] = intval($out[1]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
1 |
|
return $head; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|