-
Notifications
You must be signed in to change notification settings - Fork 8
/
ProxyHelper.php
200 lines (170 loc) · 7.32 KB
/
ProxyHelper.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace App\Helpers;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile as HttpUploadedFile;
use Illuminate\Support\Facades\Http;
class ProxyHelper {
//Original request
private Request $originalRequest;
//Params from multipart requests
private $multipartParams;
//Custom Headers
private $headers;
//Custom Authorization
private $authorization;
//Custom Method
private $customMethod;
private $addQuery;
//If needed add cookies support with:
// - PendingRequest->withCookies
// - Request->hasCookie
// - or custom
//Settings
private $useDefaultAuth;
//It's recommandable check manually (for multipart exceptions and other things)
// private $useDefaultHeaders;
public function CreateProxy(Request $request, $useDefaultAuth = true /*, $useDefaultHeaders = false*/){
$this->originalRequest = $request;
$this->multipartParams = $this->GetMultipartParams();
$this->useDefaultAuth = $useDefaultAuth;
// $this->useDefaultHeaders = $useDefaultHeaders;
return $this;
}
public function withHeaders($headers){ $this->headers = $headers; return $this; }
public function withBasicAuth($user, $secret){ $this->authorization = ['type' => 'basic', 'user' => $user, 'secret' => $secret ]; return $this; }
public function withDigestAuth($user, $secret){ $this->authorization = ['type' => 'digest', 'user' => $user, 'secret' => $secret ]; return $this; }
public function withToken($token){ $this->authorization = ['type' => 'token', 'token' => $token ]; return $this; }
public function withMethod($method = 'POST'){ $this->customMethod = $method; return $this; }
public function preserveQuery($preserve){ $this->addQuery = $preserve; return $this; }
public function getResponse($url){
$info = $this->getRequestInfo();
$http = $this->createHttp($info['type']);
$http = $this->setAuth($http, $info['token']);
$http = $this->setHeaders($http);
if($this->addQuery && $info['query'])
$url = $url.'?'.http_build_query($info['query']);
$response = $this->call($http, $info['method'], $url, $this->getParams($info));
return response($this->isJson($response) ? $response->json() : $response->body(), $response->status());
}
public function toUrl($url){ return $this->getResponse($url); }
public function toHost($host, $proxyController){
return $this->getResponse($host.str_replace($proxyController, '', $this->originalRequest->path()));
}
private function getParams($info){
$defaultParams = [];
if($info['method'] == 'GET')
return $info['params'];
if($info['type'] == 'multipart')
$defaultParams = $this->multipartParams;
else
$defaultParams = $info['params'];
if($info['query'])
foreach ($info['query'] as $key => $value)
unset($defaultParams[array_search(['name' => $key,'contents' => $value], $defaultParams)]);
return $defaultParams;
}
private function setAuth(PendingRequest $request, $currentAuth = null){
if(!$this->authorization)
return $request;
switch ($this->authorization['type']) {
case 'basic':
return $request->withBasicAuth($this->authorization['user'],$this->authorization['secret']);
case 'digest':
return $request->withDigestAuth($this->authorization['user'],$this->authorization['secret']);
case 'token':
return $request->withToken($this->authorization['token']);
default:
if($currentAuth && $this->useDefaultAuth)
return $request->withToken($currentAuth);
return $request;
}
}
private function setHeaders(PendingRequest $request){
if(!$this->headers)
return $request;
return $request->withHeaders($this->headers);
}
private function createHttp($type){
switch ($type) {
case 'multipart':
return Http::asMultipart();
case 'form':
return Http::asForm();
case 'json':
return Http::asJson();
case null:
return new PendingRequest();
default:
return Http::contentType($type);
}
}
private function call(PendingRequest $request, $method, $url, $params){
if($this->customMethod)
$method = $this->customMethod;
switch ($method) {
case 'GET':
return $request->get($url, $params);
case 'HEAD':
return $request->head($url, $params);
default:
case 'POST':
return $request->post($url, $params);
case 'PATCH':
return $request->patch($url, $params);
case 'PUT':
return $request->put($url, $params);
case 'DELETE':
return $request->delete($url, $params);
}
}
private function getRequestInfo(){
return [
'type' => ($this->originalRequest->isJson() ? 'json' :
(strpos($this->originalRequest->header('Content-Type'),'multipart') !== false ? 'multipart' :
($this->originalRequest->header('Content-Type') == 'application/x-www-form-urlencoded' ? 'form' : $this->originalRequest->header('Content-Type')))),
'agent' => $this->originalRequest->userAgent(),
'method' => $this->originalRequest->method(),
'token' => $this->originalRequest->bearerToken(),
'full_url'=>$this->originalRequest->fullUrl(),
'url'=>$this->originalRequest->url(),
'format'=>$this->originalRequest->format(),
'query' =>$this->originalRequest->query(),
'params' => $this->originalRequest->all(),
];
}
private function GetMultipartParams(){
$multipartParams = [];
if ($this->originalRequest->isMethod('post')) {
$formParams = $this->originalRequest->all();
$fileUploads = [];
foreach ($formParams as $key => $param)
if ($param instanceof HttpUploadedFile) {
$fileUploads[$key] = $param;
unset($formParams[$key]);
}
if (count($fileUploads) > 0){
$multipartParams = [];
foreach ($formParams as $key => $value)
$multipartParams[] = [
'name' => $key,
'contents' => $value
];
foreach ($fileUploads as $key => $value)
$multipartParams[] = [
'name' => $key,
'contents' => fopen($value->getRealPath(), 'r'),
'filename' => $value->getClientOriginalName(),
'headers' => [
'Content-Type' => $value->getMimeType()
]
];
}
}
return $multipartParams;
}
private function isJson(Response $response){
return strpos($response->header('Content-Type'),'json') !== false;
}
}