/** * {@inheritDoc} */ public function preSend(RequestInterface $request) { $content = $request->getContent(); if (is_array($content) || $content instanceof \JsonSerializable) { $request->setContent(Json::encode($content)); $request->addHeader('Content-Type: application/json'); } }
/** * Processes Set-Cookie headers from a request/response pair. * * @param RequestInterface $request A request object * @param MessageInterface $response A response object */ public function processSetCookieHeaders(RequestInterface $request, MessageInterface $response) { foreach ($response->getHeader('Set-Cookie', false) as $header) { $cookie = new Cookie(); $cookie->fromSetCookieHeader($header, parse_url($request->getHost(), PHP_URL_HOST)); $this->addCookie($cookie); } }
/** * Converts a request into an array for stream_context_create(). * * @param RequestInterface $request A request object * * @return array An array for stream_context_create() */ public function getStreamContextArray(RequestInterface $request) { $options = array('http' => array('method' => $request->getMethod(), 'header' => implode("\r\n", $request->getHeaders()), 'content' => $request->getContent(), 'protocol_version' => $request->getProtocolVersion(), 'ignore_errors' => $this->getIgnoreErrors(), 'max_redirects' => $this->getMaxRedirects(), 'timeout' => $this->getTimeout()), 'ssl' => array('verify_peer' => $this->getVerifyPeer())); if ($this->proxy) { $options['http']['proxy'] = $this->proxy; $options['http']['request_fulluri'] = true; } return $options; }
public function send(BuzzRequest $buzzRequest, BuzzResponse $buzzResponse) { $session = session_id(); $request = Request::create($buzzRequest->getUrl(), $buzzRequest->getMethod()); $response = static::$kernel->handle($request); $buzzResponse->setContent($response->getContent()); // kernel handling set session_id to empty string session_id($session); }
/** * Populates uri, method and entityBody used to generate the Authentication header using the specified request object. * Appends the Authentication header if it is present and has been able to be calculated. * * @param RequestInterface $request A request object */ public function preSend(RequestInterface $request) { $this->setUri($request->getResource()); $this->setMethod($request->getMethod()); $this->setEntityBody($request->getContent()); $header = $this->getHeader(); if ($header) { $request->addHeader($header); } }
/** * @param string|null $message */ public function __construct(RequestInterface $request, Response $response, $message = null) { $this->request = $request; $this->response = $response; if ($message === null) { $curlError = $response->getHeader('X-Curl-Error-Result'); $message = sprintf('HTTP %s request to "%s%s" failed: %d - "%s".', $request->getMethod(), $request->getHost(), $request->getResource(), $curlError ?: $response->getStatusCode(), $curlError ? curl_strerror($curlError) : $response->getReasonPhrase()); } parent::__construct($message); }
/** * PreSend event * @param RequestInterface $request Request object */ public function preSend(RequestInterface $request) { $url = $request->getUrl(); $pos = strpos($url, '?'); if ($pos !== false) { $url .= '&' . utf8_encode(http_build_query($this->params, '', '&')); } else { $url .= '?' . utf8_encode(http_build_query($this->params, '', '&')); } $request->fromUrl(new Url($url)); }
/** * @param RequestInterface $request */ public function preSend(RequestInterface $request) { // Skip by default if (null === $this->method) { return; } switch ($this->method) { case Client::AUTH_BASIC_TOKEN: $request->addHeader('Authorization: Basic ' . base64_encode($this->username . ':' . $this->password)); break; } }
public function preSend(RequestInterface $request) { if ($this->username === null) { throw new \RuntimeException("You have to setCredentials before using WsseListener with Buzz"); } $username = $this->username; $created = date('c'); $nonce = substr(md5(uniqid('nonce_', true)), 0, 16); $nonce64 = base64_encode($nonce); $passwordDigest = base64_encode(sha1($nonce . $created . $this->password, true)); $request->addHeader('Authorization: WSSE profile="UsernameToken"'); $header = "X-WSSE: UsernameToken Username=\"{$username}\", PasswordDigest=\"{$passwordDigest}\", Nonce=\"{$nonce64}\", Created=\"{$created}\""; $request->addHeader($header); }
public function preSend(RequestInterface $request) { $raw = $request->getContent(); $url = new Url($this->apiBaseUrl . $request->getResource() . '.json'); $request->setContent(json_encode($raw)); $request->setHost($url->getHost()); $request->setResource($url->getResource()); $request->addHeader('Authorization: Bearer ' . $this->token); $request->addHeader('Accept: application/json'); $request->addHeader('Content-Type: application/json'); }
public function applyContent(RequestInterface $request, $content) { if (empty($content)) { return $this; } if (is_array($content)) { $content = $this->encode((object) $content); } else { if (is_object($content)) { $content = $this->encode($content); } } $request->setContent($content); return $this; }
/** * {@inheritDoc} * * @throws ForbiddenAccessException * @throws \InvalidArgumentException */ public function preSend(RequestInterface $request) { if (($oauth2Header = $request->getHeader('Authorization')) && strpos($oauth2Header, 'Bearer') !== false) { return; } if (false === array_key_exists('access_token', self::$config)) { try { $data = $this->getAccessToken(); self::$config['token_type'] = $data['token_type']; self::$config['access_token'] = $data['access_token']; } catch (HttpResponseException $e) { throw new ForbiddenAccessException("Can't fetch access_token.", 0, $e); } } $request->addHeader(sprintf('Authorization: %s %s', ucfirst(strtolower(self::$config['token_type'])), self::$config['access_token'])); }
/** * Returns true if the current cookie matches the supplied request. * * @return boolean */ public function matchesRequest(RequestInterface $request) { // domain if (!$this->matchesDomain(parse_url($request->getHost(), PHP_URL_HOST))) { return false; } // path if (!$this->matchesPath($request->getResource())) { return false; } // secure if ($this->hasAttribute(static::ATTR_SECURE) && !$request->isSecure()) { return false; } return true; }
/** * Returns a value for the CURLOPT_POSTFIELDS option. * * @return string|array A post fields value */ private static function getPostFields(RequestInterface $request) { if (!$request instanceof FormRequestInterface) { return $request->getContent(); } $fields = $request->getFields(); $multipart = false; foreach ($fields as $name => $value) { if ($value instanceof FormUploadInterface) { $multipart = true; if ($file = $value->getFile()) { // replace value with upload string $fields[$name] = '@' . $file; if ($contentType = $value->getContentType()) { $fields[$name] .= ';type=' . $contentType; } if (basename($file) != $value->getFilename()) { $fields[$name] .= ';filename=' . $value->getFilename(); } } else { return $request->getContent(); } } } return $multipart ? $fields : http_build_query($fields, '', '&'); }
/** * {@inheritdoc} */ public function authenticate(RequestInterface $request) { if (strpos($this->authHeader, 'OAuth') === false) { $this->authHeader = 'OAuth ' . $this->authHeader; } $request->addHeader('Authorization: ' . $this->authHeader); return $request; }
public function preSend(RequestInterface $request) { if ($request::METHOD_GET !== $request->getMethod() || null === $this->page) { return; } $resource = $request->getResource(); // Already configured, properly a secondary request if (false !== strpos($resource, 'pagelen=') xor false !== strpos($resource, 'limit=')) { return; } $urlComponents = parse_url($resource); if (!isset($urlComponents['query'])) { $urlComponents['query'] = []; } else { parse_str($urlComponents['query'], $query); $urlComponents['query'] = $query; } /* * Page limiting works as follow (only when perPage is higher then maximum): * * Divide perPage by two, so each page covers exactly two request. * We have a maximum of 100 (unless the limit is disabled) so no crazy math or truncating is required. */ if ('2.0' === $this->client->getApiVersion()) { $urlComponents['query']['pagelen'] = null === $this->perPage ? 100 : $this->perPage; $urlComponents['query']['page'] = $this->page; // BitBucket please fix your API.. // https://bitbucket.org/site/master/issue/9659/pagelen-sometimes-limited-to-100 if (false !== strpos($urlComponents['path'], '/pullrequests') && $urlComponents['query']['pagelen'] > 50) { $urlComponents['query']['pagelen'] = $urlComponents['query']['pagelen'] / 2; } } elseif ('1.0' === $this->client->getApiVersion()) { if (null === $this->perPage) { // Use the maximum $limit = 50; } elseif ($this->perPage > 50) { // Divide perPage by two, so each page covers exactly two request (no crazy math or truncating). $limit = $this->perPage / 2; } else { $limit = $this->perPage; } $urlComponents['query']['limit'] = $limit; $urlComponents['query']['start'] = abs($this->page - 1) * $limit; } $request->setResource($urlComponents['path'] . '?' . http_build_query($urlComponents['query'], '', '&')); }
/** * {@inheritDoc} * * @throws InvalidArgumentException */ public function preSend(RequestInterface $request) { // Skip by default if (null === $this->method) { return; } switch ($this->method) { case Client::AUTH_HTTP_TOKEN: $request->addHeader('PRIVATE-TOKEN: ' . $this->token); if (!is_null($this->sudo)) { $request->addHeader('SUDO: ' . $this->sudo); } break; case Client::AUTH_URL_TOKEN: $url = $request->getUrl(); $query = array('private_token' => $this->token); if (!is_null($this->sudo)) { $query['sudo'] = $this->sudo; } $url .= (false === strpos($url, '?') ? '?' : '&') . utf8_encode(http_build_query($query, '', '&')); $request->fromUrl(new Url($url)); break; case Client::AUTH_OAUTH_TOKEN: $request->addHeader('Authorization: Bearer ' . $this->token); if (!is_null($this->sudo)) { $request->addHeader('SUDO: ' . $this->sudo); } break; } }
/** * {@inheritDoc} * * @throws InvalidArgumentException */ public function preSend(RequestInterface $request) { $parameters = array('login' => $this->login, 'password' => $this->password); $content_out = ""; foreach ($parameters as $key => $param) { $content_out .= $key . ":" . $param; $content_out .= "\n"; } $content = $request->getContent(); parse_str($content, $content); $content = $content['SimpleRequest']; $content = iconv('KOI8-R', 'UTF-8', $content); $content = $content_out . $content; $content = iconv('UTF-8', 'KOI8-R', $content); $content = array('SimpleRequest' => $content); // var_dump($content);die; $request->setContent(http_build_query($content)); }
/** * {@inheritDoc} * * @throws InvalidArgumentException */ public function preSend(RequestInterface $request) { $requestId = date('YmdHis.') . getmypid() . '@' . $this->partnerWebSite; $parameters = array('request-id' => $requestId); $content_out = ""; foreach ($parameters as $key => $param) { $content_out .= $key . ":" . $param; $content_out .= "\n"; } $content = $request->getContent(); parse_str($content, $content); $content = $content['SimpleRequest']; $content = iconv('KOI8-R', 'UTF-8', $content); $content = $content_out . $content; $content = iconv('UTF-8', 'KOI8-R', $content); $content = array('SimpleRequest' => $content); // var_dump($content);die; $request->setContent(http_build_query($content)); }
/** * {@inheritDoc} * * @throws InvalidArgumentException */ public function preSend(RequestInterface $request) { if (null === $this->method) { return; } switch ($this->method) { case Client::AUTH_HTTP_PASSWORD: if (!isset($this->options['login'], $this->options['password'])) { throw new InvalidArgumentException('You need to set username with password!'); } $request->addHeader('Authorization: Basic ' . base64_encode($this->options['login'] . ':' . $this->options['password'])); break; case Client::AUTH_HTTP_TOKEN: if (!isset($this->options['token'])) { throw new InvalidArgumentException('You need to set OAuth token!'); } $request->addHeader('Authorization: token ' . $this->options['token']); break; case Client::AUTH_URL_CLIENT_ID: if (!isset($this->options['login'], $this->options['password'])) { throw new InvalidArgumentException('You need to set client_id and client_secret!'); } if ('GET' === $request->getMethod()) { $url = $request->getUrl(); $parameters = array('client_id' => $this->options['login'], 'client_secret' => $this->options['password']); $url .= (false === strpos($url, '?') ? '?' : '&') . utf8_encode(http_build_query($parameters, '', '&')); $request->fromUrl(new Url($url)); } break; case Client::AUTH_URL_TOKEN: if (!isset($this->options['token'])) { throw new InvalidArgumentException('You need to set OAuth token!'); } if ('GET' === $request->getMethod()) { $url = $request->getUrl(); $parameters = array('access_token' => $this->options['token']); $url .= (false === strpos($url, '?') ? '?' : '&') . utf8_encode(http_build_query($parameters, '', '&')); $request->fromUrl(new Url($url)); } break; } }
protected function call() { $this->request->setMethod($this->call->getMethod()); $this->request->setHost($this->call->getRequestPartBase()); $this->request->setResource($this->call->getRequestPartUri()); $this->request->setHeaders($this->headers->getHeaders()); if ($this->call->getFormattedBody()) { $this->request->setContent($this->call->getFormattedBody()); } $this->client->setIgnoreErrors(true); $this->client->setVerifyPeer($this->configuration->getVerifyPeer()); $this->client->setMaxRedirects(0); $this->client->setTimeout(60); $this->client->send($this->request, $this->response); }
/** * Returns a value for the CURLOPT_POSTFIELDS option. * * @return string|array A post fields value */ private static function getPostFields(RequestInterface $request) { if (!$request instanceof FormRequestInterface) { return $request->getContent(); } $fields = $request->getFields(); $multipart = false; foreach ($fields as $name => $value) { if (!$value instanceof FormUploadInterface) { continue; } if (!($file = $value->getFile())) { return $request->getContent(); } $multipart = true; if (version_compare(PHP_VERSION, '5.5', '>=')) { $curlFile = new \CURLFile($file); if ($contentType = $value->getContentType()) { $curlFile->setMimeType($contentType); } if (basename($file) != $value->getFilename()) { $curlFile->setPostFilename($value->getFilename()); } $fields[$name] = $curlFile; } else { // replace value with upload string $fields[$name] = '@' . $file; if ($contentType = $value->getContentType()) { $fields[$name] .= ';type=' . $contentType; } if (basename($file) != $value->getFilename()) { $fields[$name] .= ';filename=' . $value->getFilename(); } } } return $multipart ? $fields : http_build_query($fields, '', '&'); }
/** * {@inheritDoc} */ public function preSend(RequestInterface $request) { switch ($this->method) { case Client::AUTH_HTTP_PASSWORD: $request->addHeader('Authorization: Basic ' . base64_encode($this->options['login'] . ':' . $this->options['password'])); break; case Client::AUTH_HTTP_TOKEN: $request->addHeader('Authorization: token ' . $this->options['token']); break; case Client::AUTH_URL_TOKEN: default: $url = $request->getUrl(); if ('GET' === $request->getMethod()) { $parameters = array('access_token' => $this->options['token']); $url .= '?' . utf8_encode(http_build_query($parameters, '', '&')); } $request->fromUrl(new Url($url)); break; } }
/** * @param \Buzz\Message\RequestInterface $oRequest */ public function preSend(\Buzz\Message\RequestInterface $oRequest) { // Skip by default if (!$this->hasMethod()) { return; } switch ($this->getMethod()) { case \GitlabCI\Client::AUTH_HTTP_TOKEN: $oRequest->addHeader('PRIVATE-TOKEN: ' . $this->getToken()); $sUrl = $oRequest->getUrl(); $aQuery = array('url' => $this->getGitlabUrl()); $sUrl .= (false === strpos($sUrl, '?') ? '?' : '&') . utf8_encode(http_build_query($aQuery, '', '&')); $oRequest->fromUrl(new \Buzz\Util\Url($sUrl)); break; case \GitlabCI\Client::AUTH_URL_TOKEN: $aQuery = array('private_token' => $this->getToken(), 'url' => $this->getGitlabUrl()); $sUrl = $oRequest->getUrl(); $sUrl .= (false === strpos($sUrl, '?') ? '?' : '&') . utf8_encode(http_build_query($aQuery, '', '&')); $oRequest->fromUrl(new \Buzz\Util\Url($sUrl)); break; } }
public function preSend(RequestInterface $request) { $request->addHeader(sprintf('%s: %s', self::AUTH_TOKEN_NAME, $this->token)); }
/** * {@inheritDoc} */ public function preSend(RequestInterface $request) { $url = $request->getUrl(); $url .= (false === strpos($url, '?') ? '?' : '&') . utf8_encode(http_build_query($this->options, '', '&')); $request->fromUrl(new Url($url)); }
/** * Execute before send request. * * @param RequestInterface $request Request instance */ public function preSend(RequestInterface $request) { $request->addHeader('access_token: ' . $this->token); }
/** * {@inheritDoc} */ public function preSend(RequestInterface $request) { $request->setContent(preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $request->getContent())); $request->setResource(preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $request->getResource())); }
/** * Reduces the request to its normal form * Which means: strip all information that does not contribute to its uniqueness * This will prevent cache misses, when effectively indifferent requests are made * * @param \Buzz\Message\RequestInterface $request * @return \Buzz\Message\RequestInterface */ private function getNormalizedRequest(RequestInterface $request) { $normalizedRequest = clone $request; $headers = $request->getHeaders(); $normalizedHeaders = $this->normalizeHeaders($headers); asort($normalizedHeaders); $normalizedRequest->setHeaders($normalizedHeaders); return $normalizedRequest; }
/** * @param RequestInterface $request * @param array $parameters * * @return Url */ private function setRequestUrl(RequestInterface $request, array $parameters = array()) { $url = $request->getUrl(); $url .= (false === strpos($url, '?') ? '?' : '&') . utf8_encode(http_build_query($parameters, '', '&')); $request->fromUrl(new Url($url)); }