note 110086 deleted from features.commandline.webserver by danbrown

From: Date: Thu, 06 Dec 2012 15:20:58 +0000
Subject: note 110086 deleted from features.commandline.webserver by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192367@lists.php.net to get a copy of this message
Note Submitter: matthewkastor at gmail dot com ---- 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']; } } ?>

« previous php.notes (#192367) next »