-
Notifications
You must be signed in to change notification settings - Fork 301
/
Guzzle.php
150 lines (131 loc) · 4.56 KB
/
Guzzle.php
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
<?php
namespace Solarium\Core\Client\Adapter;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\RequestOptions;
use Solarium\Core\Client\Endpoint;
use Solarium\Core\Client\Request;
use Solarium\Core\Client\Response;
use Solarium\Core\Configurable;
use Solarium\Exception\HttpException;
/**
* Guzzle HTTP adapter.
*/
class Guzzle extends Configurable implements AdapterInterface
{
/**
* The Guzzle HTTP client instance.
*
* @var GuzzleClient
*/
private $guzzleClient;
/**
* Execute a Solr request using the cURL Http.
*
* @param Request $request the incoming Solr request
* @param Endpoint $endpoint the configured Solr endpoint
*
* @throws HttpException thrown if solr request connot be made
*
* @return Response
*
*
* @codingStandardsIgnoreStart AdapterInterface does not declare type-hints
*/
public function execute($request, $endpoint)
{
//@codingStandardsIgnoreEnd
$requestOptions = [
RequestOptions::HEADERS => $this->getRequestHeaders($request),
RequestOptions::BODY => $this->getRequestBody($request),
RequestOptions::TIMEOUT => $endpoint->getTimeout(),
RequestOptions::CONNECT_TIMEOUT => $endpoint->getTimeout(),
];
// Try endpoint authentication first, fallback to request for backwards compatibility
$authData = $endpoint->getAuthentication();
if (empty($authData['username'])) {
$authData = $request->getAuthentication();
}
if (!empty($authData['username']) && !empty($authData['password'])) {
$requestOptions[RequestOptions::AUTH] = [$authData['username'], $authData['password']];
}
try {
$baseUri = $request->getIsServerRequest() ? $endpoint->getServerUri() : $endpoint->getCoreBaseUri();
$uri = $baseUri.$request->getUri();
$guzzleResponse = $this->getGuzzleClient()->request(
$request->getMethod(),
$uri,
$requestOptions
);
$responseHeaders = [
"HTTP/{$guzzleResponse->getProtocolVersion()} {$guzzleResponse->getStatusCode()} "
.$guzzleResponse->getReasonPhrase(),
];
foreach ($guzzleResponse->getHeaders() as $key => $value) {
$responseHeaders[] = "{$key}: ".implode(', ', $value);
}
return new Response((string) $guzzleResponse->getBody(), $responseHeaders);
} catch (\GuzzleHttp\Exception\RequestException $e) {
$error = $e->getMessage();
throw new HttpException("HTTP request failed, {$error}");
}
}
/**
* Gets the Guzzle HTTP client instance.
*
* @return GuzzleClient
*/
public function getGuzzleClient()
{
if (null === $this->guzzleClient) {
$this->guzzleClient = new GuzzleClient($this->options);
}
return $this->guzzleClient;
}
/**
* Helper method to create a request body suitable for a guzzle 3 request.
*
* @param Request $request the incoming solarium request
*
* @return null|resource|string
*/
private function getRequestBody(Request $request)
{
if (Request::METHOD_PUT == $request->getMethod()) {
return $request->getRawData();
}
if (Request::METHOD_POST !== $request->getMethod()) {
return null;
}
if ($request->getFileUpload()) {
$helper = new AdapterHelper();
return $helper->buildUploadBodyFromRequest($request);
}
return $request->getRawData();
}
/**
* Helper method to extract headers from the incoming solarium request and put them in a format
* suitable for a guzzle 3 request.
*
* @param Request $request the incoming solarium request
*
* @return array
*/
private function getRequestHeaders(Request $request)
{
$headers = [];
foreach ($request->getHeaders() as $headerLine) {
list($header, $value) = explode(':', $headerLine);
if ($header = trim($header)) {
$headers[$header] = trim($value);
}
}
if (!isset($headers['Content-Type'])) {
if (Request::METHOD_GET == $request->getMethod()) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
} else {
$headers['Content-Type'] = 'application/xml; charset=utf-8';
}
}
return $headers;
}
}