This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Client.php
182 lines (150 loc) · 4.96 KB
/
Client.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
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
<?php
/*
* This file is part of the Goutte package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Goutte;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\BrowserKit\Client as BaseClient;
use Symfony\Component\BrowserKit\Response;
/**
* Client.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Michael Dowling <michael@guzzlephp.org>
* @author Charles Sarrazin <charles@sarraz.in>
*/
class Client extends BaseClient
{
protected $client;
private $headers = array();
private $auth = null;
public function setClient(GuzzleClientInterface $client)
{
$this->client = $client;
return $this;
}
public function getClient()
{
if (!$this->client) {
$this->client = new GuzzleClient(array('defaults' => array('allow_redirects' => false, 'cookies' => true)));
}
return $this->client;
}
public function setHeader($name, $value)
{
$this->headers[$name] = $value;
return $this;
}
public function removeHeader($name)
{
unset($this->headers[$name]);
}
public function setAuth($user, $password = '', $type = 'basic')
{
$this->auth = array($user, $password, $type);
return $this;
}
public function resetAuth()
{
$this->auth = null;
return $this;
}
protected function doRequest($request)
{
$headers = array();
foreach ($request->getServer() as $key => $val) {
$key = strtolower(str_replace('_', '-', $key));
$contentHeaders = array('content-length' => true, 'content-md5' => true, 'content-type' => true);
if (0 === strpos($key, 'http-')) {
$headers[substr($key, 5)] = $val;
}
// CONTENT_* are not prefixed with HTTP_
elseif (isset($contentHeaders[$key])) {
$headers[$key] = $val;
}
}
$cookies = CookieJar::fromArray(
$this->getCookieJar()->allRawValues($request->getUri()),
$request->getServer()['HTTP_HOST']
);
$requestOptions = array(
'cookies' => $cookies,
'allow_redirects' => false,
'auth' => $this->auth,
);
if (!in_array($request->getMethod(), array('GET', 'HEAD'))) {
if (null !== $request->getContent()) {
$requestOptions['body'] = $request->getContent();
} else {
$requestOptions['form_params'] = $request->getParameters();
if ($files = $request->getFiles()) {
$requestOptions['multipart'] = [];
$this->addPostFiles($files, $requestOptions['multipart']);
}
}
}
if (!empty($headers)) {
$requestOptions['headers'] = $headers;
}
$method = $request->getMethod();
$uri = $request->getUri();
foreach ($this->headers as $name => $value) {
$requestOptions['headers'][$name] = $value;
}
// Let BrowserKit handle redirects
try {
$response = $this->getClient()->request($method, $uri, $requestOptions);
} catch (RequestException $e) {
$response = $e->getResponse();
if (null === $response) {
throw $e;
}
}
return $this->createResponse($response);
}
protected function addPostFiles(array $files, array &$multipart, $arrayName = '')
{
if (empty($files)) {
return;
}
foreach ($files as $name => $info) {
if (!empty($arrayName)) {
$name = $arrayName.'['.$name.']';
}
$file = [
'name' => $name,
];
if (is_array($info)) {
if (isset($info['tmp_name'])) {
if ('' !== $info['tmp_name']) {
$file['contents'] = fopen($info['tmp_name'], 'r');
if (isset($info['name'])) {
$file['filename'] = $info['name'];
}
} else {
continue;
}
} else {
$this->addPostFiles($info, $multipart, $name);
continue;
}
} else {
$file['contents'] = fopen($info, 'r');
}
$multipart[] = $file;
}
}
protected function createResponse(ResponseInterface $response)
{
return new Response((string) $response->getBody(), $response->getStatusCode(), $response->getHeaders());
}
}