Example #1
0
 public function testGetHeaders()
 {
     $response = EasyRdf_Http_Response::fromString(readFixture('http_response_200'));
     $this->assertEquals(8, count($response->getHeaders()), 'Header count is not as expected');
     $this->assertEquals('Apache/2.2.9 (Unix) PHP/5.2.6', $response->getHeader('Server'), 'Server header is not as expected');
     $this->assertEquals('text/plain', $response->getHeader('Content-Type'), 'Content-type header is not as expected');
     $this->assertEquals(array('foo', 'bar'), $response->getHeader('X-Multiple'), 'Header with multiple values is not as expected');
 }
Example #2
0
 /**
  * Send the HTTP request and return an HTTP response object
  *
  * @return EasyRdf_Http_Response
  * @throws EasyRdf_Exception
  */
 public function request($method = null)
 {
     if (!$this->_uri) {
         throw new EasyRdf_Exception("Set URI before calling EasyRdf_Http_Client->request()");
     }
     if ($method) {
         $this->setMethod($method);
     }
     $this->_redirectCounter = 0;
     $response = null;
     // Send the first request. If redirected, continue.
     do {
         // Clone the URI and add the additional GET parameters to it
         $uri = parse_url($this->_uri);
         if ($uri['scheme'] === 'http') {
             $host = $uri['host'];
         } else {
             if ($uri['scheme'] === 'https') {
                 $host = 'ssl://' . $uri['host'];
             } else {
                 throw new EasyRdf_Exception("Unsupported URI scheme: " . $uri['scheme']);
             }
         }
         if (isset($uri['port'])) {
             $port = $uri['port'];
         } else {
             if ($uri['scheme'] === 'https') {
                 $port = 443;
             } else {
                 $port = 80;
             }
         }
         if (!empty($this->_paramsGet)) {
             if (!empty($uri['query'])) {
                 $uri['query'] .= '&';
             } else {
                 $uri['query'] = '';
             }
             $uri['query'] .= http_build_query($this->_paramsGet, null, '&');
         }
         $headers = $this->_prepareHeaders($uri['host'], $port);
         // Open socket to remote server
         $socket = @fsockopen($host, $port, $errno, $errstr, $this->_config['timeout']);
         if (!$socket) {
             throw new EasyRdf_Exception("Unable to connect to {$host}:{$port} ({$errstr})");
         }
         // Write the request
         $path = $uri['path'];
         if (isset($uri['query'])) {
             $path .= '?' . $uri['query'];
         }
         fwrite($socket, "{$this->_method} {$path} HTTP/1.1\r\n");
         foreach ($headers as $k => $v) {
             if (is_string($k)) {
                 $v = ucfirst($k) . ": {$v}";
             }
             fwrite($socket, "{$v}\r\n");
         }
         fwrite($socket, "\r\n");
         // Send the request body, if there is one set
         if (isset($this->_rawPostData)) {
             fwrite($socket, $this->_rawPostData);
         }
         // Read in the response
         $content = '';
         while (!feof($socket)) {
             $content .= fgets($socket);
         }
         // FIXME: support HTTP/1.1 100 Continue
         // Close the socket
         @fclose($socket);
         // Parse the response string
         $response = EasyRdf_Http_Response::fromString($content);
         // If we got redirected, look for the Location header
         if ($response->isRedirect() && ($location = $response->getHeader('location'))) {
             // Avoid problems with buggy servers that add whitespace at the
             // end of some headers (See ZF-11283)
             $location = trim($location);
             // If it is a 303 then drop the parameters and send a GET request
             if ($response->getStatus() == 303) {
                 $this->resetParameters();
                 $this->setMethod('GET');
             }
             // If we got a well formed absolute URI
             if (parse_url($location)) {
                 $this->setHeaders('host', null);
                 $this->setUri($location);
             } else {
                 throw new EasyRdf_Exception("Failed to parse Location header returned by " . $this->_uri);
             }
             ++$this->_redirectCounter;
         } else {
             // If we didn't get any location, stop redirecting
             break;
         }
     } while ($this->_redirectCounter < $this->_config['maxredirects']);
     return $response;
 }
 public function testAsString()
 {
     $responseStr = readFixture('http_response_404');
     $response = EasyRdf_Http_Response::fromString($responseStr);
     $this->assertSame(strtolower($responseStr), strtolower($response->asString()), 'Response convertion to string does not match original string');
     $this->assertSame(strtolower($responseStr), strtolower((string) $response), 'Response convertion to string does not match original string');
 }
Example #4
0
 /**
  * Send the HTTP request and return an HTTP response object
  *
  * @return EasyRdf_Http_Response
  * @throws EasyRdf_Exception
  */
 public function request($method = 'GET')
 {
     if (!$this->_uri) {
         throw new EasyRdf_Exception("Set URI before calling EasyRdf_Http_Client->request()");
     }
     $this->_redirectCounter = 0;
     $response = null;
     // Send the first request. If redirected, continue.
     do {
         // Clone the URI and add the additional GET parameters to it
         $uri = parse_url($this->_uri);
         $host = $uri['host'];
         if (isset($uri['port'])) {
             $port = $uri['port'];
         } else {
             $port = 80;
         }
         $headers = $this->_prepareHeaders($host, $port);
         // Open socket to remote server
         $socket = fsockopen($host, $port, $errno, $errstr, $this->_config['timeout']);
         if (!$socket) {
             throw new EasyRdf_Exception($errstr);
         }
         // Write the request
         fwrite($socket, "{$method} {$uri['path']} HTTP/1.1\r\n");
         foreach ($headers as $k => $v) {
             if (is_string($k)) {
                 $v = ucfirst($k) . ": {$v}";
             }
             fwrite($socket, "{$v}\r\n");
         }
         fwrite($socket, "\r\n");
         // Read in the response
         $content = '';
         while (!feof($socket)) {
             $content .= fgets($socket);
         }
         // FIXME: support HTTP/1.1 100 Continue
         // Close the socket
         fclose($socket);
         // Parse the response string
         $response = EasyRdf_Http_Response::fromString($content);
         // If we got redirected, look for the Location header
         if ($response->isRedirect() && ($location = $response->getHeader('location'))) {
             // If we got a well formed absolute URI
             if (parse_url($location)) {
                 $this->setHeaders('host', null);
                 $this->setUri($location);
             } else {
                 throw new EasyRdf_Exception("Failed to parse Location header returned by " . $this->_uri);
             }
             ++$this->_redirectCounter;
         } else {
             // If we didn't get any location, stop redirecting
             break;
         }
     } while ($this->_redirectCounter < $this->_config['maxredirects']);
     return $response;
 }