-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathHeaderRewritePlugin.php
More file actions
71 lines (50 loc) · 2.3 KB
/
HeaderRewritePlugin.php
File metadata and controls
71 lines (50 loc) · 2.3 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
<?php
namespace Proxy\Plugin;
use Proxy\Plugin\AbstractPlugin;
use Proxy\Event\ProxyEvent;
use Proxy\Config;
class HeaderRewritePlugin extends AbstractPlugin {
function onBeforeRequest(ProxyEvent $event){
// tell target website that we only accept plain text without any transformations
$event['request']->headers->set('accept-encoding', 'identity');
// mask proxy referer
$event['request']->headers->remove('referer');
}
function onHeadersReceived(ProxyEvent $event){
// so stupid... onCompleted won't be called on "streaming" responses
$response = $event['response'];
$request_url = $event['request']->getUri();
// proxify header location value
if($response->headers->has('location')){
$location = $response->headers->get('location');
// just in case this is a relative url like: /en
$response->headers->set('location', proxify_url($location, $request_url));
}
$code = $response->getStatusCode();
$text = $response->getStatusText();
if($code >= 400 && $code <= 600){
throw new \Exception("Error accessing resource: {$code} - {$text}");
}
// we need content-encoding (in case server refuses to serve it in plain text)
// content-length: final size of content sent to user may change via plugins, so it makes no sense to send old content-length
$forward_headers = array('cache-control', 'content-type', 'zzzcontent-length', 'accept-ranges', 'content-range', 'content-disposition', 'location', 'set-cookie');
foreach($response->headers->all() as $name => $value){
// is this one of the headers we wish to forward back to the client?
if(!in_array($name, $forward_headers)){
$response->headers->remove($name);
}
}
if(!$response->headers->has('content-disposition')){
$url_path = parse_url($request_url, PHP_URL_PATH);
$filename = basename($url_path);
$response->headers->set('Content-Disposition', 'filename="'.$filename.'"');
}
if (!Config::get('allow_client_caching')) {
// do not ever cache our proxy pages!
$response->headers->set("cache-control", "no-cache, no-store, must-revalidate");
$response->headers->set("pragma", "no-cache");
$response->headers->set("expires", 0);
}
}
}
?>