コード例 #1
0
ファイル: UrlTest.php プロジェクト: alab1001101/zf2
 public function testAllParts()
 {
     $url = new URL('http://*****:*****@www.zend.com:8080/path/to/file?a=1&b=2#top');
     $this->assertEquals('http', $url->getScheme());
     $this->assertEquals('andi', $url->getUsername());
     $this->assertEquals('password', $url->getPassword());
     $this->assertEquals('www.zend.com', $url->getHost());
     $this->assertEquals('8080', $url->getPort());
     $this->assertEquals('/path/to/file', $url->getPath());
     $this->assertEquals('a=1&b=2', $url->getQuery());
     $this->assertEquals('top', $url->getFragment());
     $this->assertTrue($url->isValid());
 }
コード例 #2
0
ファイル: Socket.php プロジェクト: bradley-holt/zf2
 /**
  * Send request to the remote server
  *
  * @param string        $method
  * @param \Zend\URI\URL $uri
  * @param string        $http_ver
  * @param array         $headers
  * @param string        $body
  * @return string Request as string
  */
 public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
 {
     // Make sure we're properly connected
     if (!$this->socket) {
         throw new Exception('Trying to write but we are not connected');
     }
     $host = $uri->getHost();
     $host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
     if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) {
         throw new Exception('Trying to write but we are connected to the wrong host');
     }
     // Save request method for later
     $this->method = $method;
     // Build request headers
     $path = $uri->getPath();
     if ($uri->getQuery()) {
         $path .= '?' . $uri->getQuery();
     }
     $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
     foreach ($headers as $k => $v) {
         if (is_string($k)) {
             $v = ucfirst($k) . ": {$v}";
         }
         $request .= "{$v}\r\n";
     }
     if (is_resource($body)) {
         $request .= "\r\n";
     } else {
         // Add the request body
         $request .= "\r\n" . $body;
     }
     // Send the request
     if (!@fwrite($this->socket, $request)) {
         throw new Exception('Error writing request to server');
     }
     if (is_resource($body)) {
         if (stream_copy_to_stream($body, $this->socket) == 0) {
             throw new Exception('Error writing request to server');
         }
     }
     return $request;
 }
コード例 #3
0
ファイル: Curl.php プロジェクト: bradley-holt/zf2
 /**
  * Send request to the remote server
  *
  * @param  string        $method
  * @param  \Zend\URI\URL $uri
  * @param  float         $http_ver
  * @param  array         $headers
  * @param  string        $body
  * @return string        $request
  * @throws \Zend\HTTP\Client\Adapter\Exception If connection fails, connected to wrong host, no PUT file defined, unsupported method, or unsupported cURL option
  */
 public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $body = '')
 {
     // Make sure we're properly connected
     if (!$this->_curl) {
         throw new Exception("Trying to write but we are not connected");
     }
     if ($this->_connected_to[0] != $uri->getHost() || $this->_connected_to[1] != $uri->getPort()) {
         throw new Exception("Trying to write but we are connected to the wrong host");
     }
     // set URL
     curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString());
     // ensure correct curl call
     $curlValue = true;
     switch ($method) {
         case Client\Client::GET:
             $curlMethod = CURLOPT_HTTPGET;
             break;
         case Client\Client::POST:
             $curlMethod = CURLOPT_POST;
             break;
         case Client\Client::PUT:
             // There are two different types of PUT request, either a Raw Data string has been set
             // or CURLOPT_INFILE and CURLOPT_INFILESIZE are used.
             if (is_resource($body)) {
                 $this->_config['curloptions'][CURLOPT_INFILE] = $body;
             }
             if (isset($this->_config['curloptions'][CURLOPT_INFILE])) {
                 // Now we will probably already have Content-Length set, so that we have to delete it
                 // from $headers at this point:
                 foreach ($headers as $k => $header) {
                     if (preg_match('/Content-Length:\\s*(\\d+)/i', $header, $m)) {
                         if (is_resource($body)) {
                             $this->_config['curloptions'][CURLOPT_INFILESIZE] = (int) $m[1];
                         }
                         unset($headers[$k]);
                     }
                 }
                 if (!isset($this->_config['curloptions'][CURLOPT_INFILESIZE])) {
                     throw new Exception("Cannot set a file-handle for cURL option CURLOPT_INFILE without also setting its size in CURLOPT_INFILESIZE.");
                 }
                 if (is_resource($body)) {
                     $body = '';
                 }
                 $curlMethod = CURLOPT_PUT;
             } else {
                 $curlMethod = CURLOPT_CUSTOMREQUEST;
                 $curlValue = "PUT";
             }
             break;
         case Client\Client::DELETE:
             $curlMethod = CURLOPT_CUSTOMREQUEST;
             $curlValue = "DELETE";
             break;
         case Client\Client::OPTIONS:
             $curlMethod = CURLOPT_CUSTOMREQUEST;
             $curlValue = "OPTIONS";
             break;
         case Client\Client::TRACE:
             $curlMethod = CURLOPT_CUSTOMREQUEST;
             $curlValue = "TRACE";
             break;
         default:
             // For now, through an exception for unsupported request methods
             throw new Exception("Method currently not supported");
     }
     if (is_resource($body) && $curlMethod != CURLOPT_PUT) {
         throw new Exception("Streaming requests are allowed only with PUT");
     }
     // get http version to use
     $curlHttp = $httpVersion == 1.1 ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0;
     // mark as HTTP request and set HTTP method
     curl_setopt($this->_curl, $curlHttp, true);
     curl_setopt($this->_curl, $curlMethod, $curlValue);
     if ($this->out_stream) {
         // headers will be read into the response
         curl_setopt($this->_curl, CURLOPT_HEADER, false);
         curl_setopt($this->_curl, CURLOPT_HEADERFUNCTION, array($this, "readHeader"));
         // and data will be written into the file
         curl_setopt($this->_curl, CURLOPT_FILE, $this->out_stream);
     } else {
         // ensure headers are also returned
         curl_setopt($this->_curl, CURLOPT_HEADER, true);
         // ensure actual response is returned
         curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, true);
     }
     // set additional headers
     $headers['Accept'] = '';
     curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $headers);
     /**
      * Make sure POSTFIELDS is set after $curlMethod is set:
      * @link http://de2.php.net/manual/en/function.curl-setopt.php#81161
      */
     if ($method == Client\Client::POST) {
         curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
     } elseif ($curlMethod == CURLOPT_PUT) {
         // this covers a PUT by file-handle:
         // Make the setting of this options explicit (rather than setting it through the loop following a bit lower)
         // to group common functionality together.
         curl_setopt($this->_curl, CURLOPT_INFILE, $this->_config['curloptions'][CURLOPT_INFILE]);
         curl_setopt($this->_curl, CURLOPT_INFILESIZE, $this->_config['curloptions'][CURLOPT_INFILESIZE]);
         unset($this->_config['curloptions'][CURLOPT_INFILE]);
         unset($this->_config['curloptions'][CURLOPT_INFILESIZE]);
     } elseif ($method == Client\Client::PUT) {
         // This is a PUT by a setRawData string, not by file-handle
         curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
     }
     // set additional curl options
     if (isset($this->_config['curloptions'])) {
         foreach ((array) $this->_config['curloptions'] as $k => $v) {
             if (!in_array($k, $this->_invalidOverwritableCurlOptions)) {
                 if (curl_setopt($this->_curl, $k, $v) == false) {
                     throw new Client\Exception(sprintf("Unknown or erroreous cURL option '%s' set", $k));
                 }
             }
         }
     }
     // send the request
     $response = curl_exec($this->_curl);
     // if we used streaming, headers are already there
     if (!is_resource($this->out_stream)) {
         $this->_response = $response;
     }
     $request = curl_getinfo($this->_curl, CURLINFO_HEADER_OUT);
     $request .= $body;
     if (empty($this->_response)) {
         throw new Client\Exception("Error in cURL request: " . curl_error($this->_curl));
     }
     // cURL automatically decodes chunked-messages, this means we have to disallow the Zend_Http_Response to do it again
     if (stripos($this->_response, "Transfer-Encoding: chunked\r\n")) {
         $this->_response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $this->_response);
     }
     // Eliminate multiple HTTP responses.
     do {
         $parts = preg_split('|(?:\\r?\\n){2}|m', $this->_response, 2);
         $again = false;
         if (isset($parts[1]) && preg_match("|^HTTP/1\\.[01](.*?)\r\n|mi", $parts[1])) {
             $this->_response = $parts[1];
             $again = true;
         }
     } while ($again);
     // cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string:
     if (stripos($this->_response, "HTTP/1.0 200 Connection established\r\n\r\n") !== false) {
         $this->_response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $this->_response);
     }
     return $request;
 }
コード例 #4
0
ファイル: Client.php プロジェクト: bradley-holt/zf2
 /**
  * Prepare the request headers
  *
  * @return array
  */
 protected function _prepareHeaders()
 {
     $headers = array();
     // Set the host header
     if (!isset($this->headers['host'])) {
         $host = $this->uri->getHost();
         // If the port is not default, add it
         if (!($this->uri->getScheme() == 'http' && $this->uri->getPort() == 80 || $this->uri->getScheme() == 'https' && $this->uri->getPort() == 443)) {
             $host .= ':' . $this->uri->getPort();
         }
         $headers[] = "Host: {$host}";
     }
     // Set the connection header
     if (!isset($this->headers['connection'])) {
         if (!$this->config['keepalive']) {
             $headers[] = "Connection: close";
         }
     }
     // Set the Accept-encoding header if not set - depending on whether
     // zlib is available or not.
     if (!isset($this->headers['accept-encoding'])) {
         if (function_exists('gzinflate')) {
             $headers[] = 'Accept-encoding: gzip, deflate';
         } else {
             $headers[] = 'Accept-encoding: identity';
         }
     }
     // Set the Content-Type header
     if ($this->method == self::POST && (!isset($this->headers[strtolower(self::CONTENT_TYPE)]) && isset($this->enctype))) {
         $headers[] = self::CONTENT_TYPE . ': ' . $this->enctype;
     }
     // Set the user agent header
     if (!isset($this->headers['user-agent']) && isset($this->config['useragent'])) {
         $headers[] = "User-Agent: {$this->config['useragent']}";
     }
     // Set HTTP authentication if needed
     if (is_array($this->auth)) {
         $auth = self::encodeAuthHeader($this->auth['user'], $this->auth['password'], $this->auth['type']);
         $headers[] = "Authorization: {$auth}";
     }
     // Load cookies from cookie jar
     if (isset($this->cookiejar)) {
         $cookstr = $this->cookiejar->getMatchingCookies($this->uri, true, HTTP\CookieJar::COOKIE_STRING_CONCAT);
         if ($cookstr) {
             $headers[] = "Cookie: {$cookstr}";
         }
     }
     // Add all other user defined headers
     foreach ($this->headers as $header) {
         list($name, $value) = $header;
         if (is_array($value)) {
             $value = implode(', ', $value);
         }
         $headers[] = "{$name}: {$value}";
     }
     return $headers;
 }