예제 #1
0
 public function testReloadExtractsURIFromEditLink()
 {
     $expectedUri = 'http://www.example.com:81';
     $etag = Etag::fromString('Etag: ABCD1234');
     $this->service->setMajorProtocolVersion(2);
     $this->adapter->setResponse($this->httpEntrySample);
     $entry = $this->service->newEntry();
     $entry->link = array(new Extension\Link($expectedUri, 'edit', 'application/atom+xml'));
     $entry->setEtag($etag);
     $newEntry = $entry->reload();
     $requestUri = $this->adapter->popRequest()->uri;
     $expectedUriObject = new Uri\Http($expectedUri);
     $expectedUriObject->setPort('81');
     $this->assertEquals($expectedUriObject, $requestUri);
 }
예제 #2
0
    /**
     * Provide an alternate Parameter Container implementation for server parameters in this object, (this is NOT the
     * primary API for value setting, for that see server())
     *
     * @param \Zend\Stdlib\ParametersInterface $server
     * @return Request
     */
    public function setServer(ParametersInterface $server)
    {
        $this->serverParams = $server;

        // This seems to be the only way to get the Authorization header on Apache
        if (function_exists('apache_request_headers')) {
            $apacheRequestHeaders = apache_request_headers();
            if (isset($apacheRequestHeaders['Authorization'])) {
                if (!$this->serverParams->get('HTTP_AUTHORIZATION')) {
                    $this->serverParams->set('HTTP_AUTHORIZATION', $apacheRequestHeaders['Authorization']);
                }
            }
        }

        $this->headers()->addHeaders($this->serverToHeaders($this->serverParams));

        if (isset($this->serverParams['REQUEST_METHOD'])) {
            $this->setMethod($this->serverParams['REQUEST_METHOD']);
        }

        if (isset($this->serverParams['SERVER_PROTOCOL']) 
            && strpos($this->serverParams['SERVER_PROTOCOL'], '1.0') !== false) {
            $this->setVersion('1.0');
        }

        $this->setUri($uri = new HttpUri());

        if (isset($this->serverParams['HTTPS']) && $this->serverParams['HTTPS'] === 'on') { 
            $uri->setScheme('https');
        } else {
            $uri->setScheme('http');
        }

        if (isset($this->serverParams['QUERY_STRING'])) {
            $uri->setQuery($this->serverParams['QUERY_STRING']);
        }

        if ($this->headers()->get('host')) {
            //TODO handle IPv6 with port
            if (preg_match('|^([^:]+):([^:]+)$|', $this->headers()->get('host')->getFieldValue(), $match)) {
                $uri->setHost($match[1]);
                $uri->setPort($match[2]);
            } else {
                $uri->setHost($this->headers()->get('host')->getFieldValue());
            }
        } elseif (isset($this->serverParams['SERVER_NAME'])) {
            $uri->setHost($this->serverParams['SERVER_NAME']);
            if (isset($this->serverParams['SERVER_PORT'])) {
                $uri->setPort($this->serverParams['SERVER_PORT']);
            }
        }

        $requestUri = $this->getRequestUri();
        $uri->setPath(substr($requestUri, 0, strpos($requestUri, '?') ?: strlen($requestUri)));

        return $this;
    }
    /**
     * Provide an alternate Parameter Container implementation for server parameters in this object,
     * (this is NOT the primary API for value setting, for that see getServer())
     *
     * @param  ParametersInterface $server
     * @return Request
     */
    public function setServer(ParametersInterface $server)
    {
        $this->serverParams = $server;

        // This seems to be the only way to get the Authorization header on Apache
        if (function_exists('apache_request_headers')) {
            $apacheRequestHeaders = apache_request_headers();
            if (!isset($this->serverParams['HTTP_AUTHORIZATION'])) {
                if (isset($apacheRequestHeaders['Authorization'])) {
                    $this->serverParams->set('HTTP_AUTHORIZATION', $apacheRequestHeaders['Authorization']);
                } elseif (isset($apacheRequestHeaders['authorization'])) {
                    $this->serverParams->set('HTTP_AUTHORIZATION', $apacheRequestHeaders['authorization']);
                }
            }
        }

        // set headers
        $headers = array();

        foreach ($server as $key => $value) {
            if ($value && strpos($key, 'HTTP_') === 0) {
                if (strpos($key, 'HTTP_COOKIE') === 0) {
                    // Cookies are handled using the $_COOKIE superglobal
                    continue;
                }
                $name = strtr(substr($key, 5), '_', ' ');
                $name = strtr(ucwords(strtolower($name)), ' ', '-');
            } elseif ($value && strpos($key, 'CONTENT_') === 0) {
                $name = substr($key, 8); // Content-
                $name = 'Content-' . (($name == 'MD5') ? $name : ucfirst(strtolower($name)));
            } else {
                continue;
            }

            $headers[$name] = $value;
        }

        $this->getHeaders()->addHeaders($headers);

        // set method
        if (isset($this->serverParams['REQUEST_METHOD'])) {
            $this->setMethod($this->serverParams['REQUEST_METHOD']);
        }

        // set HTTP version
        if (isset($this->serverParams['SERVER_PROTOCOL'])
            && strpos($this->serverParams['SERVER_PROTOCOL'], self::VERSION_10) !== false
        ) {
            $this->setVersion(self::VERSION_10);
        }

        // set URI
        $uri = new HttpUri();

        // URI scheme
        $scheme = (!empty($this->serverParams['HTTPS'])
                   && $this->serverParams['HTTPS'] !== 'off') ? 'https' : 'http';
        $uri->setScheme($scheme);

        // URI host & port
        $host = null;
        $port = null;
        if (isset($this->serverParams['SERVER_NAME'])) {
            $host = $this->serverParams['SERVER_NAME'];
            if (isset($this->serverParams['SERVER_PORT'])) {
                $port = (int) $this->serverParams['SERVER_PORT'];
            }
            // Check for missinterpreted IPv6-Address
            // Reported at least for Safari on Windows
            if (isset($this->serverParams['SERVER_ADDR']) && preg_match('/^\[[0-9a-fA-F\:]+\]$/', $host)) {
                $host = '[' . $this->serverParams['SERVER_ADDR'] . ']';
                if ($port . ']' == substr($host, strrpos($host, ':')+1)) {
                    // The last digit of the IPv6-Address has been taken as port
                    // Unset the port so the default port can be used
                    $port = null;
                }
            }
        } elseif ($this->getHeaders()->get('host')) {
            $host = $this->getHeaders()->get('host')->getFieldValue();
            // works for regname, IPv4 & IPv6
            if (preg_match('|\:(\d+)$|', $host, $matches)) {
                $host = substr($host, 0, -1 * (strlen($matches[1]) + 1));
                $port = (int) $matches[1];
            }
        }
        $uri->setHost($host);
        $uri->setPort($port);

        // URI path
        $requestUri = $this->getRequestUri();
        if (($qpos = strpos($requestUri, '?')) !== false) {
            $requestUri = substr($requestUri, 0, $qpos);
        }

        $uri->setPath($requestUri);

        // URI query
        if (isset($this->serverParams['QUERY_STRING'])) {
            $uri->setQuery($this->serverParams['QUERY_STRING']);
        }

        $this->setUri($uri);

        return $this;
    }
예제 #4
0
파일: Client.php 프로젝트: tillk/vufind
 /**
  * Send HTTP request
  *
  * @param  Request $request
  * @return Response
  * @throws Exception\RuntimeException
  * @throws Client\Exception\RuntimeException
  */
 public function send(Request $request = null)
 {
     if ($request !== null) {
         $this->setRequest($request);
     }
     $this->redirectCounter = 0;
     $response = null;
     $adapter = $this->getAdapter();
     // Send the first request. If redirected, continue.
     do {
         // uri
         $uri = $this->getUri();
         // query
         $query = $this->getRequest()->getQuery();
         if (!empty($query)) {
             $queryArray = $query->toArray();
             if (!empty($queryArray)) {
                 $newUri = $uri->toString();
                 $queryString = http_build_query($queryArray, null, $this->getArgSeparator());
                 if ($this->config['rfc3986strict']) {
                     $queryString = str_replace('+', '%20', $queryString);
                 }
                 if (strpos($newUri, '?') !== false) {
                     $newUri .= $this->getArgSeparator() . $queryString;
                 } else {
                     $newUri .= '?' . $queryString;
                 }
                 $uri = new Http($newUri);
             }
         }
         // If we have no ports, set the defaults
         if (!$uri->getPort()) {
             $uri->setPort($uri->getScheme() == 'https' ? 443 : 80);
         }
         // method
         $method = $this->getRequest()->getMethod();
         // this is so the correct Encoding Type is set
         $this->setMethod($method);
         // body
         $body = $this->prepareBody();
         // headers
         $headers = $this->prepareHeaders($body, $uri);
         $secure = $uri->getScheme() == 'https';
         // cookies
         $cookie = $this->prepareCookies($uri->getHost(), $uri->getPath(), $secure);
         if ($cookie->getFieldValue()) {
             $headers['Cookie'] = $cookie->getFieldValue();
         }
         // check that adapter supports streaming before using it
         if (is_resource($body) && !$adapter instanceof Client\Adapter\StreamInterface) {
             throw new Client\Exception\RuntimeException('Adapter does not support streaming');
         }
         // calling protected method to allow extending classes
         // to wrap the interaction with the adapter
         $response = $this->doRequest($uri, $method, $secure, $headers, $body);
         if (!$response) {
             throw new Exception\RuntimeException('Unable to read response, or response is empty');
         }
         if ($this->config['storeresponse']) {
             $this->lastRawResponse = $response;
         } else {
             $this->lastRawResponse = null;
         }
         if ($this->config['outputstream']) {
             $stream = $this->getStream();
             if (!is_resource($stream) && is_string($stream)) {
                 $stream = fopen($stream, 'r');
             }
             $streamMetaData = stream_get_meta_data($stream);
             if ($streamMetaData['seekable']) {
                 rewind($stream);
             }
             // cleanup the adapter
             $adapter->setOutputStream(null);
             $response = Response\Stream::fromStream($response, $stream);
             $response->setStreamName($this->streamName);
             if (!is_string($this->config['outputstream'])) {
                 // we used temp name, will need to clean up
                 $response->setCleanup(true);
             }
         } else {
             $response = $this->getResponse()->fromString($response);
         }
         // Get the cookies from response (if any)
         $setCookies = $response->getCookie();
         if (!empty($setCookies)) {
             $this->addCookie($setCookies);
         }
         // If we got redirected, look for the Location header
         if ($response->isRedirect() && $response->getHeaders()->has('Location')) {
             // Avoid problems with buggy servers that add whitespace at the
             // end of some headers
             $location = trim($response->getHeaders()->get('Location')->getFieldValue());
             // Check whether we send the exact same request again, or drop the parameters
             // and send a GET request
             if ($response->getStatusCode() == 303 || !$this->config['strictredirects'] && ($response->getStatusCode() == 302 || $response->getStatusCode() == 301)) {
                 $this->resetParameters(false, false);
                 $this->setMethod(Request::METHOD_GET);
             }
             // If we got a well formed absolute URI
             if (($scheme = substr($location, 0, 6)) && ($scheme == 'http:/' || $scheme == 'https:')) {
                 // setURI() clears parameters if host changed, see #4215
                 $this->setUri($location);
             } else {
                 // Split into path and query and set the query
                 if (strpos($location, '?') !== false) {
                     list($location, $query) = explode('?', $location, 2);
                 } else {
                     $query = '';
                 }
                 $this->getUri()->setQuery($query);
                 // Else, if we got just an absolute path, set it
                 if (strpos($location, '/') === 0) {
                     $this->getUri()->setPath($location);
                     // Else, assume we have a relative path
                 } else {
                     // Get the current path directory, removing any trailing slashes
                     $path = $this->getUri()->getPath();
                     $path = rtrim(substr($path, 0, strrpos($path, '/')), "/");
                     $this->getUri()->setPath($path . '/' . $location);
                 }
             }
             ++$this->redirectCounter;
         } else {
             // If we didn't get any location, stop redirecting
             break;
         }
     } while ($this->redirectCounter <= $this->config['maxredirects']);
     $this->response = $response;
     return $response;
 }
예제 #5
0
 /**
  * Sets the Request Uri object
  *
  * @param array $parameters Url construction parameter array
  *
  * @return void
  */
 protected function setRequestUri(array $parameters = array())
 {
     // Getting base uri according to the uri construction method
     $uri = $this->constructActionUrl($parameters);
     // Converting it into a Zend Http Uri object
     $httpUri = new Http($uri);
     $httpUri->setPort($this->port);
     // Setting request uri object
     $this->setUri($httpUri);
 }
예제 #6
0
 /**
  * Provide an alternate Parameter Container implementation for server parameters in this object, (this is NOT the
  * primary API for value setting, for that see server())
  *
  * @param \Zend\Stdlib\ParametersDescription $server
  * @return Request
  */
 public function setServer(ParametersDescription $server)
 {
     $this->serverParams = $server;
     $this->headers()->addHeaders($this->serverToHeaders($this->serverParams));
     if (isset($this->serverParams['REQUEST_METHOD'])) {
         $this->setMethod($this->serverParams['REQUEST_METHOD']);
     }
     if (isset($this->serverParams['SERVER_PROTOCOL']) && strpos($this->serverParams['SERVER_PROTOCOL'], '1.0') !== false) {
         $this->setVersion('1.0');
     }
     $this->setUri($uri = new HttpUri());
     if (isset($this->serverParams['HTTPS']) && $this->serverParams['HTTPS'] === 'on') {
         $uri->setScheme('https');
     } else {
         $uri->setScheme('http');
     }
     if (isset($this->serverParams['QUERY_STRING'])) {
         $uri->setQuery($this->serverParams['QUERY_STRING']);
     }
     if ($this->headers()->get('host')) {
         //TODO handle IPv6 with port
         if (preg_match('|^([^:]+):([^:]+)$|', $this->headers()->get('host')->getFieldValue(), $match)) {
             $uri->setHost($match[1]);
             $uri->setPort($match[2]);
         } else {
             $uri->setHost($this->headers()->get('host')->getFieldValue());
         }
     } elseif (isset($this->serverParams['SERVER_NAME'])) {
         $uri->setHost($this->serverParams['SERVER_NAME']);
         if (isset($this->serverParams['SERVER_PORT'])) {
             $uri->setPort($this->serverParams['SERVER_PORT']);
         }
     }
     $requestUri = $this->getRequestUri();
     $uri->setPath(substr($requestUri, 0, strpos($requestUri, '?') ?: strlen($requestUri)));
     return $this;
 }
예제 #7
0
파일: Request.php 프로젝트: moln/gzfextra
 public function setServer(ParametersInterface $server)
 {
     $this->serverParams = $server;
     // This seems to be the only way to get the Authorization header on Apache
     if (function_exists('apache_request_headers')) {
         $apacheRequestHeaders = apache_request_headers();
         if (!isset($this->serverParams['HTTP_AUTHORIZATION'])) {
             if (isset($apacheRequestHeaders['Authorization'])) {
                 $this->serverParams->set('HTTP_AUTHORIZATION', $apacheRequestHeaders['Authorization']);
             } elseif (isset($apacheRequestHeaders['authorization'])) {
                 $this->serverParams->set('HTTP_AUTHORIZATION', $apacheRequestHeaders['authorization']);
             }
         }
     }
     // set headers
     $headers = array();
     foreach ($server as $key => $value) {
         if ($value && strpos($key, 'HTTP_') === 0) {
             if (strpos($key, 'HTTP_COOKIE') === 0) {
                 // Cookies are handled using the $_COOKIE superglobal
                 continue;
             }
             $name = strtr(substr($key, 5), '_', ' ');
             $name = strtr(ucwords(strtolower($name)), ' ', '-');
         } elseif ($value && strpos($key, 'CONTENT_') === 0) {
             $name = substr($key, 8);
             // Content-
             $name = 'Content-' . ($name == 'MD5' ? $name : ucfirst(strtolower($name)));
         } else {
             continue;
         }
         $headers[$name] = $value;
     }
     $this->getHeaders()->addHeaders($headers);
     // set method
     if (isset($this->serverParams['REQUEST_METHOD'])) {
         $this->setMethod($this->serverParams['REQUEST_METHOD']);
     }
     // set HTTP version
     if (isset($this->serverParams['SERVER_PROTOCOL']) && strpos($this->serverParams['SERVER_PROTOCOL'], self::VERSION_10) !== false) {
         $this->setVersion(self::VERSION_10);
     }
     // set URI
     $uri = new HttpUri();
     // URI scheme
     if (!empty($this->serverParams['HTTPS']) && $this->serverParams['HTTPS'] !== 'off' || !empty($this->serverParams['HTTP_X_FORWARDED_PROTO']) && $this->serverParams['HTTP_X_FORWARDED_PROTO'] == 'https') {
         $scheme = 'https';
     } else {
         $scheme = 'http';
     }
     $uri->setScheme($scheme);
     // URI host & port
     $uri->setHost($this->serverParams['SERVER_NAME']);
     $uri->setPort($this->serverParams['SERVER_PORT']);
     // URI path
     if (isset($this->serverParams['REQUEST_URI'])) {
         $this->setRequestUri($this->serverParams['REQUEST_URI']);
     }
     $requestUri = $this->getRequestUri();
     if (($qpos = strpos($requestUri, '?')) !== false) {
         $requestUri = substr($requestUri, 0, $qpos);
     }
     $uri->setPath($requestUri);
     // URI query
     if (isset($this->serverParams['QUERY_STRING'])) {
         $uri->setQuery($this->serverParams['QUERY_STRING']);
     }
     $this->setUri($uri);
     return $this;
 }