-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.php
74 lines (56 loc) · 2.13 KB
/
proxy.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
<?php
/**
* Proxies API requests to the public packagist repositor API
* by injecting a `type=roundcube-plugin` query parameter.
*/
require_once './vendor/autoload.php';
use Proxy\Proxy;
use Proxy\Adapter\Guzzle\GuzzleAdapter;
use Proxy\Filter\RemoveEncodingFilter;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response\Serializer as ResponseSerializer;
use Zend\Diactoros\Request\Serializer as RequestSerializer;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;
use GuzzleHttp\Exception\RequestException;
use Zend\HttpHandlerRunner\Emitter\SapiEmitter;
// create a log channel
$logger = new Logger('proxy');
$logger->pushHandler((new StreamHandler('./log/proxy.log', Logger::WARNING))
->setFormatter(new LineFormatter("[%datetime%] %level_name%: %message%\n\n", null, true))
);
// create a PSR7 request based on the current browser request.
$request = ServerRequestFactory::fromGlobals();
// create a guzzle client
$guzzle = new GuzzleHttp\Client();
// create the proxy instance
$proxy = new Proxy(new GuzzleAdapter($guzzle));
$proxy->filter(new RemoveEncodingFilter());
$proxy->filter(function ($request, $response, $next) use ($logger) {
// manipulate the request object
$uri = $request->getUri();
$path = urldecode($uri->getPath());
// inject a type=roundcube-plugin query param
if ($path === '/search.json' || $path === '/packages/list.json') {
parse_str($uri->getQuery(), $query);
$query['type'] = 'roundcube-plugin';
$uri = $uri->withQuery(http_build_query($query));
}
$request = $request->withUri($uri);
$logger->info('>> ' . RequestSerializer::toString($request));
// call the next item in the middleware
try {
$response = $next($request, $response);
} catch (RequestException $e) {
// pipe error response to client
$response = $e->getResponse();
}
$logger->info('<< ' . ResponseSerializer::toString($response));
return $response;
});
// forward the request and get the response.
$response = $proxy->forward($request)->to('https://packagist.org');
// output response to the browser.
$emitter = new SapiEmitter();
$emitter->emit($response);