forked from Athlon1600/php-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractPlugin.php
More file actions
81 lines (60 loc) · 2.09 KB
/
AbstractPlugin.php
File metadata and controls
81 lines (60 loc) · 2.09 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
72
73
74
75
76
77
78
79
80
81
<?php
namespace Proxy\Plugin;
use Proxy\Event\ProxyEvent;
abstract class AbstractPlugin {
// apply these methods only to those events whose request URL passes this filter
protected $url_pattern;
public function onBeforeRequest(ProxyEvent $event){
// fired right before a request is being sent to a proxy
}
public function onHeadersReceived(ProxyEvent $event){
// fired right after response headers have been fully received - last chance to modify before sending it back to the user
}
public function onCurlWrite(ProxyEvent $event){
// fired as the data is being written piece by piece
}
public function onCompleted(ProxyEvent $event){
// fired after the full response=headers+body has been read - will only be called on "non-streaming" responses
}
final public function subscribe($dispatcher){
$dispatcher->addListener('request.before_send', function($event){
$this->route('request.before_send', $event);
});
$dispatcher->addListener('request.sent', function($event){
$this->route('request.sent', $event);
});
$dispatcher->addListener('curl.callback.write', function($event){
$this->route('curl.callback.write', $event);
});
$dispatcher->addListener('request.complete', function($event){
$this->route('request.complete', $event);
});
}
// dispatch based on filter
private function route($event_name, ProxyEvent $event){
$url = $event['request']->getUri();
// url filter provided and current request url does not match it
if($this->url_pattern){
if(starts_with($this->url_pattern, '/') && preg_match($this->url_pattern, $url) !== 1){
return;
} else if(stripos($url, $this->url_pattern) === false){
return;
}
}
switch($event_name){
case 'request.before_send':
$this->onBeforeRequest($event);
break;
case 'request.sent':
$this->onHeadersReceived($event);
break;
case 'curl.callback.write':
$this->onCurlWrite($event);
break;
case 'request.complete':
$this->onCompleted($event);
break;
}
}
}
?>