-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStreamClient.php
More file actions
168 lines (133 loc) · 3.99 KB
/
Copy pathStreamClient.php
File metadata and controls
168 lines (133 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Class StreamClient
*
* @created 23.02.2019
* @author smiley <[email protected]>
* @copyright 2019 smiley
* @license MIT
*/
declare(strict_types=1);
namespace chillerlan\HTTP;
use chillerlan\HTTP\Utils\HeaderUtil;
use Psr\Http\Message\{RequestInterface, ResponseInterface};
use Exception, Throwable;
use function explode, file_get_contents, get_headers, in_array, intval, is_file, restore_error_handler,
set_error_handler, sprintf, stream_context_create, strtolower, str_starts_with, trim;
/**
* A http client via PHP streams
*
* (I'm not exactly sure why I'm keeping this - use CurlClient in production)
*
* @see \file_get_contents()
* @see \stream_context_create()
*/
class StreamClient extends HTTPClientAbstract{
/**
* @inheritDoc
* @throws \Exception|\chillerlan\HTTP\ClientException
*/
public function sendRequest(RequestInterface $request):ResponseInterface{
$errorHandler = function(int $errno, string $errstr):bool{
$this->logger->error(sprintf('StreamClient error #%s: %s', $errno, $errstr));
throw new Exception($errstr, $errno);
};
set_error_handler($errorHandler);
$exception = null;
try{
$context = stream_context_create($this->getContextOptions($request));
$requestUri = (string)$request->getUri()->withFragment('');
$responseBody = file_get_contents($requestUri, false, $context);
$response = $this->createResponse(get_headers($requestUri, true, $context));
}
catch(Throwable $e){
$exception = $e;
}
restore_error_handler();
if($exception !== null){
throw new ClientException($exception->getMessage());
}
$body = $this->streamFactory !== null
? $this->streamFactory->createStream()
: $response->getBody()
;
$body->write($responseBody);
$body->rewind();
return $response->withBody($body);
}
/**
*
*/
protected function getContextOptions(RequestInterface $request):array{
$method = $request->getMethod();
$body = null;
if(in_array($method, ['DELETE', 'PATCH', 'POST', 'PUT'], true)){
$body = $request->getBody()->getContents();
}
$options = [
'http' => [
'method' => $method,
'header' => $this->getRequestHeaders($request),
'content' => $body,
'protocol_version' => $request->getProtocolVersion(),
'user_agent' => $this->options->user_agent,
'max_redirects' => 0,
'timeout' => 5,
],
'ssl' => [
'verify_peer' => $this->options->ssl_verifypeer,
'verify_depth' => 3,
'peer_name' => $request->getUri()->getHost(),
'ciphers' => 'HIGH:!SSLv2:!SSLv3',
'disable_compression' => true,
],
];
if($this->options->ca_info){
$ca = (is_file($this->options->ca_info)) ? 'capath' : 'cafile';
$options['ssl'][$ca] = $this->options->ca_info;
}
return $options;
}
/**
*
*/
protected function getRequestHeaders(RequestInterface $request):array{
$headers = [];
foreach($request->getHeaders() as $name => $values){
$name = strtolower($name);
foreach($values as $value){
// cURL requires a special format for empty headers.
// See https://github.com/guzzle/guzzle/issues/1882 for more details.
$headers[] = ($value === '') ? $name.';' : $name.': '.$value;
}
}
return $headers;
}
/**
* @param string[] $headers
*/
protected function createResponse(array $headers):ResponseInterface{
$h = [];
$httpversion = '';
$statuscode = 0;
$statustext = '';
foreach($headers as $k => $v){
if($k === 0 && str_starts_with($v, 'HTTP')){
$status = explode(' ', $v, 3);
$httpversion = explode('/', $status[0], 2)[1];
$statuscode = intval($status[1]);
$statustext = trim($status[2]);
continue;
}
$h[$k] = $v;
}
$response = $this->responseFactory
->createResponse($statuscode, $statustext)
->withProtocolVersion($httpversion)
;
foreach(HeaderUtil::normalize($h) as $k => $v){
$response = $response->withAddedHeader($k, $v);
}
return $response;
}
}