lopez86100 Posted July 29, 2007 Share Posted July 29, 2007 Hi all !! I'm trying to write simple proxy server, I get header but I can't send a response, I'm trying to fix it for 3 days with no result. I tried to use socket_sendto() but there was an error (I think PHP BUG) .I don't have any more ideas how to solve it. Can anybody help me. ?Here is the code: <?php error_reporting(E_ALL); set_time_limit(0); $value1=""; $addr = my_ip(); $local_port=81; function my_ip($dest='64.0.0.0', $port=80) { $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_connect($socket, $dest, $port); socket_getsockname($socket, $addr, $port); socket_close($socket); return $addr; } while(1){ $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); socket_bind($socket,NULL,81); socket_listen($socket); #echo "Waiting for a connection\n"; $conn = false; switch(socket_select($r = array($socket), $w = array($socket), $e = array($socket), 60)) { case 2: # echo "Connection refused\n"; break; case 1: # echo "Connection accepted\n"; $conn = socket_accept($socket); break; case 0: ## echo "Connection timed out\n"; break; } if ($conn !== false) { $buffer=""; $buffer1=""; while($buffer=socket_read($conn,512,PHP_BINARY_READ)){ $buffer1.=$buffer; } echo $buffer=$buffer1; preg_match("@\s*([post|GET])\s*(http[^\s]*).*@si",$buffer,$address); $address=$address[2]; if(preg_match("@\s*POST\s*http@si",$buffer,$new)){ $method="POST"; } if(preg_match("@\s*GET\s*http@si",$buffer,$new)){ $method="GET"; } if($method=="GET"){ $ch = curl_init(); curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiejar21".""); curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiejar21".""); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_MAXREDIRS, 15); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); @curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_URL,$url=$address); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 25); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $echo = curl_exec($ch); $addr1=""; $port1=""; socket_getpeername ($conn,&$addr1,&$port1); /*it doesnt't work if I am giving a handle "$socket" there is an error , when I am giving a "$conn" there is no error but in a browser nothing happens. As you can see I gave in curl HEADER included so it should be sent with all content. It seems that this function doesn't word properly in this case.*/ socket_write ($conn, $echo, strlen ($echo)); curl_close($ch); }elseif($method=="POST"){ }else{ } } socket_close($socket); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.