Exemplo n.º 1
0
 /**
  * Sorts and normalizes request data and oauthValues.
  *
  * Section 9.1.1 of Oauth spec.
  *
  * - URL encode keys + values.
  * - Sort keys & values by byte value.
  *
  * @param \Cake\Network\Http\Request $request The request object.
  * @param array $oauthValues Oauth values.
  *
  * @return string sorted and normalized values
  */
 protected function _normalizedParams($request, $oauthValues)
 {
     $query = parse_url($request->url(), PHP_URL_QUERY);
     parse_str($query, $queryArgs);
     $post = [];
     $body = $request->body();
     if (is_string($body) && $request->getHeaderLine('content-type') === 'application/x-www-form-urlencoded') {
         parse_str($body, $post);
     }
     if (is_array($body)) {
         $post = $body;
     }
     $args = array_merge($queryArgs, $oauthValues, $post);
     uksort($args, 'strcmp');
     $pairs = [];
     foreach ($args as $k => $val) {
         if (is_array($val)) {
             sort($val, SORT_STRING);
             foreach ($val as $nestedVal) {
                 $pairs[] = "{$k}={$nestedVal}";
             }
         } else {
             $val = rawurlencode($val);
             // Required for Twitter signatures
             $pairs[] = "{$k}={$val}";
         }
     }
     return implode('&', $pairs);
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 protected function sendInternalRequest(InternalRequestInterface $internalRequest)
 {
     $request = new Request();
     foreach ($this->prepareHeaders($internalRequest) as $name => $value) {
         $request->header($name, $value);
     }
     $request->method($internalRequest->getMethod());
     $request->body($this->prepareBody($internalRequest));
     $request->url($uri = (string) $internalRequest->getUri());
     $request->version($this->getConfiguration()->getProtocolVersion());
     try {
         $response = $this->client->send($request, array('timeout' => $this->getConfiguration()->getTimeout(), 'redirect' => false));
     } catch (\Exception $e) {
         throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
     }
     return $this->getConfiguration()->getMessageFactory()->createResponse((int) $response->statusCode(), $response->version(), $response->headers(), $response->body());
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function sendRequest(RequestInterface $request)
 {
     $cakeRequest = new Request();
     $cakeRequest->method($request->getMethod());
     $cakeRequest->url((string) $request->getUri());
     $cakeRequest->version($request->getProtocolVersion());
     $cakeRequest->body($request->getBody()->getContents());
     foreach ($request->getHeaders() as $header => $values) {
         $cakeRequest->header($header, $request->getHeaderLine($header));
     }
     if (null === $cakeRequest->header('Content-Type')) {
         $cakeRequest->header('Content-Type', 'application/x-www-form-urlencoded');
     }
     try {
         $cakeResponse = $this->client->send($cakeRequest, $this->client->config());
     } catch (Exception $exception) {
         throw new NetworkException('Failed to send request', $request, $exception);
     }
     return $this->responseFactory->createResponse($cakeResponse->statusCode(), null, $cakeResponse->headers(), $cakeResponse->body(), $cakeResponse->version());
 }
Exemplo n.º 4
0
 /**
  * Builds the request content based on the request object.
  *
  * If the $request->body() is a string, it will be used as is.
  * Array data will be processed with Cake\Network\Http\FormData
  *
  * @param \Cake\Network\Http\Request $request The request being sent.
  * @param array $options Array of options to use.
  * @return void
  */
 protected function _buildContent(Request $request, $options)
 {
     $content = $request->body();
     if (empty($content)) {
         return;
     }
     if (is_string($content)) {
         $this->_contextOptions['content'] = $content;
         return;
     }
     if (is_array($content)) {
         $formData = new FormData();
         $formData->addMany($content);
         $type = 'multipart/form-data; boundary="' . $formData->boundary() . '"';
         $request->header('Content-Type', $type);
         $this->_contextOptions['content'] = (string) $formData;
         return;
     }
     $this->_contextOptions['content'] = $content;
 }
Exemplo n.º 5
0
    /**
     * Sorts and normalizes request data and oauthValues
     *
     * Section 9.1.1 of Oauth spec.
     *
     * - URL encode keys + values.
     * - Sort keys & values by byte value.
     *
     * @param \Cake\Network\Http\Request $request The request object.
     * @param array $oauthValues Oauth values.
     * @return string sorted and normalized values
     */
    protected function _normalizedParams($request, $oauthValues)
    {
        $query = parse_url($request->url(), PHP_URL_QUERY);
        parse_str($query, $queryArgs);

        $post = [];
        $body = $request->body();

        if (is_array($body)) {
            $post = $body;
        }

        $args = array_merge($queryArgs, $oauthValues, $post);
        uksort($args, 'strcmp');

        $pairs = [];
        foreach ($args as $k => $val) {
            if (is_array($val)) {
                sort($val, SORT_STRING);
                foreach ($val as $nestedVal) {
                    $pairs[] = "$k=$nestedVal";
                }
            } else {
                $pairs[] = "$k=$val";
            }
        }

        return implode('&', $pairs);
    }