|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\HttpAdapter\Extractor\ProtocolVersionExtractor; |
|
15
|
|
|
use Ivory\HttpAdapter\Extractor\StatusCodeExtractor; |
|
16
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
|
17
|
|
|
use Ivory\HttpAdapter\Normalizer\BodyNormalizer; |
|
18
|
|
|
use Ivory\HttpAdapter\Normalizer\HeadersNormalizer; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author GeLo <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractStreamHttpAdapter extends AbstractHttpAdapter |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function sendInternalRequest(InternalRequestInterface $internalRequest) |
|
29
|
|
|
{ |
|
30
|
|
|
$context = stream_context_create([ |
|
31
|
|
|
'http' => [ |
|
32
|
|
|
'follow_location' => false, |
|
33
|
|
|
'max_redirects' => 1, |
|
34
|
|
|
'ignore_errors' => true, |
|
35
|
|
|
'timeout' => $this->getConfiguration()->getTimeout(), |
|
36
|
|
|
'protocol_version' => $internalRequest->getProtocolVersion(), |
|
37
|
|
|
'method' => $internalRequest->getMethod(), |
|
38
|
|
|
'header' => $this->prepareHeaders($internalRequest, false), |
|
39
|
|
|
'content' => $this->prepareBody($internalRequest), |
|
40
|
|
|
], |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
list($body, $headers) = $this->process($uri = (string) $internalRequest->getUri(), $context); |
|
44
|
|
|
|
|
45
|
|
|
if ($body === false) { |
|
46
|
|
|
$error = error_get_last(); |
|
47
|
|
|
throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $error['message']); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $this->getConfiguration()->getMessageFactory()->createResponse( |
|
51
|
|
|
StatusCodeExtractor::extract($headers), |
|
52
|
|
|
ProtocolVersionExtractor::extract($headers), |
|
53
|
|
|
HeadersNormalizer::normalize($headers), |
|
54
|
|
|
BodyNormalizer::normalize($body, $internalRequest->getMethod()) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $uri |
|
60
|
|
|
* @param resource $context |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
abstract protected function process($uri, $context); |
|
65
|
|
|
} |
|
66
|
|
|
|