Ejemplo n.º 1
0
 function Request($req, $details = false)
 {
     $crlf = "\r\n";
     //Fetch the data
     if (!$this->keepalive || !$this->fp || !is_resource($this->fp)) {
         $this->fp = fsockopen(($this->protocol == 'https' ? 'ssl://' : '') . $this->host, $this->port);
         stream_set_timeout($this->fp, 0, $this->timeout * 1000);
     }
     fwrite($this->fp, $req);
     $response = '';
     if ($this->keepalive) {
         $line = '';
         $lastbyte = null;
         $contentlength = 0;
         $contentstart = false;
         while ($this->fp && is_resource($this->fp) && !feof($this->fp)) {
             if (!$contentstart) {
                 $byte = fread($this->fp, 1);
                 $response .= $byte;
                 $line .= $byte;
                 if ($lastbyte == "\r" && $byte == "\n") {
                     if ($line == $crlf) {
                         $contentstart = true;
                     } elseif (strpos(strtolower($line), 'content-length') !== false) {
                         $contentlength = trim(substr($line, strpos($line, ':') + 1));
                     }
                     $line = '';
                 }
                 $lastbyte = $byte;
             } else {
                 if ($contentlength > 0) {
                     $response .= fread($this->fp, $contentlength);
                 }
                 break;
             }
         }
     } else {
         //Read till termination of connection
         while ($this->fp && is_resource($this->fp) && !feof($this->fp)) {
             $response .= fread($this->fp, 1024);
         }
         $this->Close();
     }
     //Split header and body
     $pos = strpos($response, $crlf . $crlf);
     if ($pos === false) {
         return $response;
     }
     $header = substr($response, 0, $pos);
     $body = substr($response, $pos + 2 * strlen($crlf));
     //Parse headers
     $headers = array();
     $lines = explode($crlf, $header);
     $firsttime = true;
     foreach ($lines as $line) {
         if ($firsttime) {
             $codes = explode(" ", $line);
             $code['version'] = $codes[0];
             $code['code'] = intval($codes[1]);
             $code['message'] = $codes[2];
             $firsttime = false;
         }
         if (($pos = strpos($line, ':')) !== false) {
             $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos + 1));
         }
     }
     //Redirection?
     if (isset($headers['location'])) {
         //TODO: eventually handle keep alive here too
         $http = new HTTPRequest($headers['location']);
         return $http->Request($req, $details);
     } else {
         if (strtolower($headers['transfer-encoding']) == 'chunked') {
             $body = $this->unchunkHttp11($body);
         }
         if ($details) {
             $result[HTTPRequest::HTTP] = $code;
             $result[HTTPRequest::HEADER] = $headers;
             $result[HTTPRequest::BODY] = $body;
             return $result;
         } else {
             return $body;
         }
     }
 }
Ejemplo n.º 2
0
 function Request($req, $post = false, $details = false)
 {
     $crlf = "\r\n";
     // fetch
     $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
     fwrite($this->_fp, $req);
     while (is_resource($this->_fp) && $this->_fp && !feof($this->_fp)) {
         $response .= fread($this->_fp, 1024);
     }
     fclose($this->_fp);
     // split header and body
     $pos = strpos($response, $crlf . $crlf);
     if ($pos === false) {
         return $response;
     }
     $header = substr($response, 0, $pos);
     $body = substr($response, $pos + 2 * strlen($crlf));
     // parse headers
     $headers = array();
     $lines = explode($crlf, $header);
     $firsttime = true;
     foreach ($lines as $line) {
         if ($firsttime) {
             $codes = explode(" ", $line);
             $code['version'] = $codes[0];
             $code['code'] = intval($codes[1]);
             $code['message'] = $codes[2];
             $firsttime = false;
         }
         if (($pos = strpos($line, ':')) !== false) {
             $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos + 1));
         }
     }
     // redirection?
     if (isset($headers['location'])) {
         $http = new HTTPRequest($headers['location']);
         return $http->Request($req, $post, $details);
     } else {
         if ($details) {
             $result['http'] = $code;
             $result['header'] = $headers;
             $result['body'] = $body;
             return $result;
         } else {
             return $body;
         }
     }
 }