Page 1 of 1

Custom Application Proxy

Posted: Fri Jan 05, 2007 12:23 pm
by programmingjeff
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?

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

Posted: Fri Jan 05, 2007 12:37 pm
by feyd
What about using cURL or Snoopy?

Posted: Fri Jan 05, 2007 1:11 pm
by timvw
What about using mod_proxy? (eg: http://www.apachetutor.org/admin/reverseproxies).

Posted: Fri Jan 05, 2007 1:14 pm
by feyd
I just realized this was in Critique. Moved to PHP - Code.

Posted: Fri Jan 05, 2007 4:19 pm
by programmingjeff
timvw wrote:What about using mod_proxy? (eg: http://www.apachetutor.org/admin/reverseproxies).
mod_proxy does exactly what I need it to do. The only problem is, how do I configure it on a 3rd party hosting site? Most of them don't allow you to mess around with httpd.conf...



@feyd: I apologize for placing this topic in the wrong category.

Posted: Fri Jan 05, 2007 4:59 pm
by feyd
Talk to the host to see what they recommend.