forked from evaisse/SimpleHttpBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteHttpKernel.php
More file actions
192 lines (161 loc) · 5.37 KB
/
RemoteHttpKernel.php
File metadata and controls
192 lines (161 loc) · 5.37 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/*
* (c) Darrell Hamilton <darrell.noice@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace evaisse\SimpleHttpBundle\Http\Kernel;
use evaisse\SimpleHttpBundle\Curl\Collector\ContentCollector;
use evaisse\SimpleHttpBundle\Curl\Collector\HeaderCollector;
use evaisse\SimpleHttpBundle\Curl\CurlErrorException;
use evaisse\SimpleHttpBundle\Curl\Request as CurlRequest;
use evaisse\SimpleHttpBundle\Curl\RequestGenerator;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* RemoteHttpKernel utilizes curl to convert a Request object into a Response
*
* @author Darrell Hamilton <darrell.noice@gmail.com>
*/
class RemoteHttpKernel implements HttpKernelInterface
{
/**
* An instance of Curl\RequestGenerator for getting preconfigured
* Curl\Request objects
*
* @var RequestGenerator
*/
private $generator;
private $lastCurlRequest;
public function __construct(?RequestGenerator $generator = null)
{
$this->generator = $generator;
}
/**
* Handles a Request to convert it to a Response.
*
* When $catch is true, the implementation must catch all exceptions
* and do its best to convert them to a Response instance.
*
* @param Request $request A Request instance
* @param integer $type The type of the request
* (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
* @param Boolean $catch Whether to catch exceptions or not
*
* @return Response A Response instance
*
* @throws \Exception When an Exception occurs during processing
*/
public function handle(Request $request, int $type = HttpKernelInterface::SUB_REQUEST, bool $catch = true): Response
{
try {
return $this->handleRaw($request);
} catch (\Exception $e) {
if (false === $catch) {
throw $e;
}
return $this->handleException($e, $request);
}
}
private function handleException(\Exception $e, Request $request)
{
return new Response(
$e->getMessage(),
500
);
}
private function getCurlRequest()
{
if (isset($this->generator)) {
return $this->generator->getRequest();
}
return new CurlRequest();
}
/**
* Execute a Request object via cURL
*
* @param Request $request the request to execute
* @param array $options additional curl options to set/override
*
* @return Response
*
* @throws CurlErrorException
*/
private function handleRaw(Request $request)
{
$curl = $this->lastCurlRequest = $this->getCurlRequest();
$curl->setOptionArray(
array(
CURLOPT_URL => $request->getUri(),
CURLOPT_HTTPHEADER => $this->buildHeadersArray($request->headers),
CURLINFO_HEADER_OUT => true
)
);
$curl->setMethod($request->getMethod());
if ("POST" === $request->getMethod()) {
$this->setPostFields($curl, $request);
}
if ("PUT" === $request->getMethod() && count($request->files->all()) > 0) {
$file = current($request->files->all());
$curl->setOptionArray(
array(
CURLOPT_INFILE => '@' . $file->getRealPath(),
CURLOPT_INFILESIZE => $file->getSize()
)
);
}
$content = new ContentCollector();
$headers = new HeaderCollector();
// These options must not be tampered with to ensure proper functionality
$curl->setOptionArray(
array(
CURLOPT_HEADERFUNCTION => array($headers, "collect"),
CURLOPT_WRITEFUNCTION => array($content, "collect"),
)
);
$curl->execute();
$response = new Response(
$content->retrieve(),
$headers->getCode(),
$headers->retrieve()
);
$response->setProtocolVersion($headers->getVersion());
$response->setStatusCode($headers->getCode(), $headers->getMessage());
return $response;
}
/**
* Populate the POSTFIELDS option
*
* @param CurlRequest $curl cURL request object
* @param Request $request the Request object we're populating
*/
private function setPostFields(CurlRequest $curl, Request $request)
{
$postfields = null;
$content = $request->getContent();
if (!empty($content)) {
$postfields = $content;
} else if (count($request->request->all()) > 0) {
$postfields = $request->request->all();
}
$curl->setOption(CURLOPT_POSTFIELDS, $postfields);
}
/**
* Convert a HeaderBag into an array of headers appropriate for cURL
*
* @param HeaderBag $headerBag headers to parse
*
* @return array An array of header strings
*/
private function buildHeadersArray(HeaderBag $headerBag)
{
return explode("\r\n", $headerBag);
}
public function getLastCurlRequest()
{
return $this->lastCurlRequest;
}
}