PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/YASE/HTTPClient.php

https://github.com/engel/yase
PHP | 172 lines | 147 code | 23 blank | 2 comment | 17 complexity | df830a907093cd13bb4ad8297174ade5 MD5 | raw file
  1. <?php
  2. class YASE_HTTPClient {
  3. public $sStatus;
  4. public $sFinalUrl;
  5. public $sContentType;
  6. protected $oSocket;
  7. protected $sHost;
  8. protected $iPort;
  9. protected $sErrNo;
  10. protected $sErrStr;
  11. protected $iRedirects;
  12. protected $sUrl;
  13. protected $sReply;
  14. protected $aHeaders;
  15. public function __construct(){
  16. $this->iPort=80;
  17. $this->iRedirects=0;
  18. }
  19. public function Connect($sHost ){
  20. $this->sHost=$sHost;
  21. if($this->sHost==""){
  22. die("missing host name!\r\n");
  23. }
  24. $this->oSocket = fsockopen( $this->sHost,
  25. $this->iPort,
  26. $this->sErrNo,
  27. $this->sErrStr,
  28. 30
  29. );
  30. $this->aHeaders=array();
  31. }
  32. public function Close(){
  33. fclose($this->oSocket);
  34. }
  35. private function SendRequest( $sRequest ){
  36. $sRequest.=" HTTP/1.0";
  37. $sRequest.="\r\nUser-Agent: YASE alpha2";
  38. $sRequest.="\r\nHost: ".$this->sHost;
  39. $sRequest.="\r\nAccept-Charset: iso-8859-1";
  40. $sRequest.="\r\nConnection: close\r\n\r\n";
  41. if($this->oSocket)
  42. fputs( $this->oSocket, $sRequest."\r\n");
  43. }
  44. public function extractHost($sUrl) {
  45. preg_match("@(http\s?\://([^\/].*?))(\/|$)@", $sUrl, $aMatch);
  46. if ( count($aMatch) > 1 ){
  47. $sHost = $aMatch[2];
  48. }
  49. if(isset($sHost))
  50. $this->sHost=$sHost;
  51. return($this->sHost);
  52. }
  53. public function extractRelativeUrl($sUrl) {
  54. $sUrl=preg_replace("/http\:\/\//i","", $sUrl);
  55. $sUrl=str_replace($this->sHost, "", $sUrl);
  56. if($sUrl==""){
  57. $sUrl="/";
  58. }
  59. return($sUrl);
  60. }
  61. protected function getHeaders () {
  62. while( !feof($this->oSocket ) ) {
  63. $sLine=fgets($this->oSocket,512);
  64. $indx=strpos($sLine,":");
  65. $sKey=substr($sLine, 0, $indx);
  66. $sKey=strtolower($sKey);
  67. $sValue=substr($sLine,$indx+1, strlen($sLine)-$indx);
  68. if ( $sKey=="content-type" ){
  69. $this->sContentType=$sValue;
  70. }
  71. $sValue=trim($sValue);
  72. $this->aHeaders[$sKey]=$sValue;
  73. if($sLine=="\r\n") break;
  74. }
  75. }
  76. protected function Redirect() {
  77. $this->iRedirects++;
  78. print "redirects#:".$this->iRedirects."\r\n";
  79. if($this->iRedirects<5){
  80. $sNewUrl=chop($this->aHeaders['location']);
  81. print "redirecting to:".$sNewUrl."\r\n";
  82. $this->Connect($this->sHost);
  83. //make sure we have a full url
  84. if(!(strpos($sNewUrl, $this->sHost)) &&
  85. !(strpos($sNewUrl, "/"))){
  86. print "EXPAND:".$this->sHost."\r\n";
  87. $sNewUrl="http://".$this->sHost.$sNewUrl;
  88. print "NEW URL:".$sNewUrl."\r\n";
  89. }
  90. $this->sFinalUrl=$sNewUrl;
  91. print "[".$this->sFinalUrl."]\r\n";
  92. return($this->Get($sNewUrl));
  93. }else{
  94. print "too many redirects \r\n";
  95. return("");
  96. }
  97. }
  98. protected function getReply () {
  99. $this->sReply="";
  100. if(!$this->oSocket){ return(""); }
  101. $sStatus=fgets($this->oSocket,24);
  102. $aStatus=split(" ",$sStatus, 3);
  103. if( preg_match("/http/i",$aStatus[0])){
  104. if($aStatus[1]!="200"){
  105. //handle redirects
  106. if( $aStatus[1]=="301" || $aStatus[1]=="302"){
  107. $this->getHeaders();
  108. $this->sStatus="301";
  109. return($this->Redirect());
  110. }
  111. if($aStatus[1]=="400"){
  112. print "[".$this->sUrl."] was not found!\r\n";
  113. return("");
  114. }
  115. }else{
  116. $this->getHeaders();
  117. $this->sReply="";
  118. try{
  119. while( !feof($this->oSocket ) ) {
  120. $sLine=fgets($this->oSocket,512);
  121. if(strlen($this->sReply) < 1500000){
  122. $this->sReply.=$sLine;
  123. }
  124. }
  125. }catch(Exception $e){
  126. print "failed retrieving:".$this->ssUrl."\r\n";
  127. }
  128. }
  129. }
  130. return($this->sReply);
  131. }
  132. public function Get ($sIncomingUrl) {
  133. $sHost = $this->extractHost($sIncomingUrl);
  134. if($sHost!=""){
  135. $this->sHost=$sHost;
  136. }
  137. $sUrl=$this->extractRelativeUrl($sIncomingUrl);
  138. $this->sUrl=$sUrl;
  139. $this->SendRequest("GET $sUrl");
  140. return($this->getReply());
  141. }
  142. public function Post ($sUrl, $sParams) {
  143. throw new Exception("not implemented yet");
  144. }
  145. public function Delete ($sUrl, $sParams) {
  146. throw new Exception("not implemented yet");
  147. }
  148. };
  149. ?>