note 110086 added to features.commandline.webserver
| From: | matthewkastor at gmail dot com | Date: | Tue, 18 Sep 2012 05:02:37 +0000 |
| Subject: | note 110086 added to features.commandline.webserver | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190855@lists.php.net to get a copy of this message | ||
PHPMonkey?
I have been experimenting with the built in server to see if I could get it to act as a proxy
running on localhost. What I wanted to do was manipulate pages before they were sent to my browser,
like Fiddler and other programs can. I got it to work for http traffic but not https. I see errors
in the console, when the built in server gets an https request, saying that the request is
malformed. Anyway, if you want to alter http traffic just before it hits your browser, then you can
extend the class in this router to override the external_resource_handler method. Right now all it
will do is transparently proxy pages but you can do whatever you want between getting the page and
showing the page. @see: DOMDocument in the manual http://php.net/manual/en/class.domdocument.php
There is a nice class contributed by PhilipWayneRollins at gmail dot com on 15-Aug-2009 06:32
<?php
/**
* Rev. 1 Atropa mod_proxy for php built in webserver
* â Hial Atropa!! â
*/
class atropa_mod_proxy
{
protected function is_external_resource() {
$host = parse_url($_SERVER['REQUEST_URI'], PHP_URL_HOST);
if(isset($host) && $host !== $_SERVER['SERVER_NAME']) {
return true;
} else {
return false;
}
}
protected function local_resource_handler() {
return false;
}
protected function external_resource_handler() {
$ext = $this->get_page();
$this->show_page($ext);
}
public function process_request() {
if($this->is_external_resource()) {
return $this->external_resource_handler();
} else {
return $this->local_resource_handler();
}
}
public function get_request_headers() {
$arr = array();
foreach($_SERVER as $svar => $sval) {
if(substr($svar, 0, 4) === 'HTTP') {
$svar = substr($svar, 5);
$svar = preg_replace('/_/', ' ', $svar);
$svar = ucwords(strtolower($svar));
$svar = preg_replace('/ /', '-', $svar);
$arr[$svar] = $sval;
}
}
return $arr;
}
public function pack_request_headers($headers_array) {
$packed = '';
foreach($headers_array as $header_name => $header_value) {
$packed .= $header_name . ': ' . $header_value . "\r\n";
}
return $packed;
}
public function echo_response_headers($http_response_header_array) {
foreach($http_response_header_array as $val) {
if(strpos(strtolower($val), 'connection') !== 0) {
header($val);
}
}
}
protected function get_page() {
$request_headers = $this->get_request_headers();
$request_headers = $this->pack_request_headers($request_headers);
$method = $_SERVER["REQUEST_METHOD"];
$scheme = parse_url($_SERVER['REQUEST_URI'], PHP_URL_SCHEME);
$opts = array(
$scheme => array(
'method' => $method,
'header' => $request_headers
)
);
if(count($_POST) > 0) {
$content = http_build_query($_POST);
$opts[$scheme]['content'] = $content;
}
$context = stream_context_create($opts);
$ext = array();
$ext['page'] = file_get_contents($_SERVER['REQUEST_URI'], false, $context);
$ext['http_response_header'] = $http_response_header;
return $ext;
}
protected function show_page($ext) {
header_remove();
$this->echo_response_headers($ext['http_response_header']);
echo $ext['page'];
}
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 71.205.34.219
----
Manual Page -- http://www.php.net/manual/en/features.commandline.webserver.php
Edit -- https://master.php.net/note/edit/110086
Del: integrated -- https://master.php.net/note/delete/110086/integrated
Del: useless -- https://master.php.net/note/delete/110086/useless
Del: bad code -- https://master.php.net/note/delete/110086/bad+code
Del: spam -- https://master.php.net/note/delete/110086/spam
Del: non-english -- https://master.php.net/note/delete/110086/non-english
Del: in docs -- https://master.php.net/note/delete/110086/in+docs
Del: other reasons-- https://master.php.net/note/delete/110086
Reject -- https://master.php.net/note/reject/110086
Search -- https://master.php.net/manage/user-notes.php