1 | <?php |
||
17 | class Client implements HttpClient |
||
18 | { |
||
19 | /** @var ZendClient */ |
||
20 | private $client; |
||
21 | |||
22 | /** @var ResponseFactory */ |
||
23 | private $responseFactory; |
||
24 | |||
25 | 82 | public function __construct(ZendClient $client = null, ResponseFactory $responseFactory = null) |
|
26 | { |
||
27 | 82 | $this->client = $client ?: new ZendClient(); |
|
28 | 82 | $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
|
29 | 82 | } |
|
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | 82 | public function sendRequest(RequestInterface $request): ResponseInterface |
|
35 | { |
||
36 | 82 | $request = $this->sanitizeRequest($request); |
|
37 | 82 | $headers = new Headers(); |
|
38 | |||
39 | 82 | foreach ($request->getHeaders() as $key => $value) { |
|
40 | 82 | $headers->addHeader(new GenericHeader($key, $request->getHeaderLine($key))); |
|
41 | } |
||
42 | |||
43 | 82 | $zendRequest = new Request(); |
|
44 | 82 | $zendRequest->setMethod($request->getMethod()); |
|
45 | 82 | $zendRequest->setUri((string) $request->getUri()); |
|
46 | 82 | $zendRequest->setHeaders($headers); |
|
47 | 82 | $zendRequest->setContent($request->getBody()->getContents()); |
|
48 | |||
49 | $options = [ |
||
50 | 82 | 'httpversion' => $request->getProtocolVersion(), |
|
51 | ]; |
||
52 | |||
53 | 82 | if (extension_loaded('curl')) { |
|
54 | 82 | $options['curloptions'] = [ |
|
55 | 82 | CURLOPT_HTTP_VERSION => $this->getProtocolVersion($request->getProtocolVersion()), |
|
56 | ]; |
||
57 | } |
||
58 | |||
59 | 82 | $this->client->setOptions($options); |
|
60 | |||
61 | 82 | if ($this->client->getAdapter() instanceof ZendClient\Adapter\Curl && $request->getMethod()) { |
|
62 | 31 | $request = $request->withHeader('Content-Length', '0'); |
|
63 | } |
||
64 | |||
65 | try { |
||
66 | 82 | $zendResponse = $this->client->send($zendRequest); |
|
67 | 2 | } catch (RuntimeException $exception) { |
|
68 | 2 | throw new NetworkException($exception->getMessage(), $request, $exception); |
|
69 | } |
||
70 | |||
71 | 80 | return $this->responseFactory->createResponse( |
|
72 | 80 | $zendResponse->getStatusCode(), |
|
73 | 80 | $zendResponse->getReasonPhrase(), |
|
74 | 80 | $zendResponse->getHeaders()->toArray(), |
|
75 | 80 | $zendResponse->getContent(), |
|
76 | 80 | $zendResponse->getVersion() |
|
77 | ); |
||
78 | } |
||
79 | |||
80 | 82 | private function sanitizeRequest(RequestInterface $request) |
|
81 | { |
||
82 | 82 | $request = $this->sanitizeWithTrace($request); |
|
83 | 82 | $request = $this->sanitizeWithCurl($request); |
|
84 | |||
85 | 82 | return $request; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Zend request remove the body if it's a trace but does not rewrite the content length header, |
||
90 | * This can lead to error from the server has it can expect a specific content length, but don't have |
||
91 | * a body, so we set content-length to 0 to avoid bad reading from the server. |
||
92 | * |
||
93 | * @param RequestInterface $request |
||
94 | * |
||
95 | * @return RequestInterface |
||
96 | */ |
||
97 | 82 | private function sanitizeWithTrace(RequestInterface $request) |
|
105 | |||
106 | /** |
||
107 | * On cUrl Adapter, zend does not include the body if it's not a POST, PUT or PATCH request but does not |
||
108 | * rewrite the content length header. |
||
109 | * This can lead to error from the server has it can expect a specific content length, but don't have |
||
110 | * a body, so we set content-length to 0 to avoid bad reading from the server. |
||
111 | * |
||
112 | * @param RequestInterface $request |
||
113 | * |
||
114 | * @return RequestInterface |
||
115 | */ |
||
116 | 82 | private function sanitizeWithCurl(RequestInterface $request) |
|
128 | |||
129 | /** |
||
130 | * Return cURL constant for specified HTTP version. |
||
131 | * |
||
132 | * @param string $requestVersion |
||
133 | * |
||
134 | * @throws \UnexpectedValueException if unsupported version requested |
||
135 | * |
||
136 | * @return int |
||
137 | */ |
||
138 | 82 | private function getProtocolVersion($requestVersion) |
|
155 | } |
||
156 |