Custom Application Proxy
Posted: Fri Jan 05, 2007 12:23 pm
I've been developing a Javascript/Ajax/PHP application for the past 9 months. The application basically has 2 primary functions: generate the primary page, and handle all ajax requests that come from the primary page. I've designed the application so that all requests go through a single file, index.php.
My goal is to keep the application on my own server, and allow my clients to log in remotely. Although they wouldn't mind doing this, they don't want their customers to see that a 3rd party is hosting their data. To avoid this, I've created a small proxy script that my clients can place on their servers. To any user using the proxy script, it seems like that file is the entire application, although it simply reroutes requests to my server.
The functions of the proxy are the following:
1. Redirect all GET requests to the server
2. Redirect data from the server back to the client, including redirects and cookies and body data.
I had this script working properly a while ago, but it doesn't work properly anymore. On some HTML pages that it serves, it inserts random text when there are more than 4 blank lines in a row., and at the beginning, and at the end. Any suggestions on why this would be happening? Any better suggestions?
Sample output:
My goal is to keep the application on my own server, and allow my clients to log in remotely. Although they wouldn't mind doing this, they don't want their customers to see that a 3rd party is hosting their data. To avoid this, I've created a small proxy script that my clients can place on their servers. To any user using the proxy script, it seems like that file is the entire application, although it simply reroutes requests to my server.
The functions of the proxy are the following:
1. Redirect all GET requests to the server
2. Redirect data from the server back to the client, including redirects and cookies and body data.
I had this script working properly a while ago, but it doesn't work properly anymore. On some HTML pages that it serves, it inserts random text when there are more than 4 blank lines in a row., and at the beginning, and at the end. Any suggestions on why this would be happening? Any better suggestions?
Code: Select all
<?php
// Name of this script
$name = $_SERVER['PHP_SELF'];
// Path to this script
$thispath = 'http://localhost/';
// Auto-generated password
$password = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// Host of the primary application
$host = 'PATH.TO.MY.SERVER.COM';
// Path to the primary application
$path = 'http://'.$host.'/';
$headers = array();
$header = 'GET '.$path.'index.php?__proxypwd='.$password.'&__proxypath='.urlencode($thispath).'&__proxyname='.urlencode($name)
.(isset($_POST['username'])&&isset($_POST['username']) ? '&__proxyusername='.urlencode($_POST['username']).'&__proxypassword='.urlencode($_POST['password']) : '')
.'&'.$_SERVER['QUERY_STRING']." HTTP/1.1\r\nHost: $host\r\n";
$header_lookup = array( 'HTTP_USER_AGENT' => 'User-Agent',
'HTTP_ACCEPT' => 'Accept',
'HTTP_ACCEPT_LANGUAGE' => 'Accept-Language',
'HTTP_ACCEPT_ENCODING' => 'Accept-Encoding',
'HTTP_ACCEPT_CHARSET' => 'Accept-Charset'
);
foreach($header_lookup as $key => $name)
if(isset($_SERVER[$key])) $header .= $name.': '.$_SERVER[$key]."\r\n";
// Add cookies
if(count($_COOKIE)) {
$cookies = array();
foreach($_COOKIE as $k => $v)
$cookies[] = $k.'='.urlencode($v);
$header .= 'Cookie: '.implode('; ',$cookies)."\r\n";
}
$header .= "Connection: Close\r\n\r\n";
// Open the socket and send it the request
$socket = fsockopen( $host, 80, $errno, $errstr);
if(!$socket) die("Socket Error");
fwrite($socket, $header);
// Get the response
$out = '';
while(!feof($socket)) {
$s = fgets($socket, 128);
$out .= $s;
}
fclose($socket);
// See if any cookies need to be set
preg_match("/Set-Cookie: ([^\r]+)/", $out, $match);
if(count($match) == 2) {
$kv = explode('=', $match[1]);
setcookie($kv[0],$kv[1]);
}
// Relocate
preg_match("/Location: ([^\r]+)/", $out, $match);
if(count($match) == 2) {
header($match[1]);
die();
}
// Remove the response headers
echo strchr($out, "\r\n\r\n");
?>
Sample output:
Code: Select all
bo57
<html>
...
</html>
0