/** * Applies request headers to a request based on the POST state * * @param RequestInterface $request Request to update */ public function applyRequestHeaders(RequestInterface $request) { if ($this->files || $this->forceMultipart) { $request->setHeader('Content-Type', 'multipart/form-data; boundary=' . $this->getBody()->getBoundary()); } elseif ($this->fields) { $request->setHeader('Content-Type', 'application/x-www-form-urlencoded'); } if ($size = $this->getSize()) { $request->setHeader('Content-Length', $size); } }
public function signRequest(RequestInterface $request, CredentialsInterface $credentials) { // Ensure that the signable query string parameters are sorted sort($this->signableQueryString); // Add the security token header if one is being used by the credentials if ($token = $credentials->getSecurityToken()) { $request->setHeader('X-Amz-Security-Token', $token); } // Add a date header if one is not set $request->removeHeader('X-Amz-Date'); $request->setHeader('Date', gmdate(\DateTime::RFC2822)); $stringToSign = $this->createCanonicalizedString($request); $request->getConfig()['aws.signature'] = $stringToSign; $request->setHeader('Authorization', 'AWS ' . $credentials->getAccessKeyId() . ':' . $this->signString($stringToSign, $credentials)); }
/** * Always add a x-amz-content-sha-256 for data integrity. */ public function signRequest(RequestInterface $request, CredentialsInterface $credentials) { if (!$request->hasHeader('x-amz-content-sha256')) { $request->setHeader('X-Amz-Content-Sha256', $this->getPayload($request)); } parent::signRequest($request, $credentials); }
public function after(GuzzleCommandInterface $command, RequestInterface $request, Operation $operation, array $context) { foreach ($this->buffered as $param) { $this->visitWithValue($command[$param->getName()], $param, $command); } $this->buffered = array(); $additional = $operation->getAdditionalParameters(); if ($additional && $additional->getLocation() == $this->locationName) { foreach ($command->toArray() as $key => $value) { if (!$operation->hasParam($key)) { $additional->setName($key); $this->visitWithValue($value, $additional, $command); } } $additional->setName(null); } // If data was found that needs to be serialized, then do so $xml = null; if ($this->writer) { $xml = $this->finishDocument($this->writer); } elseif ($operation->getData('xmlAllowEmpty')) { // Check if XML should always be sent for the command $writer = $this->createRootElement($operation); $xml = $this->finishDocument($writer); } if ($xml) { $request->setBody(Stream::factory($xml)); // Don't overwrite the Content-Type if one is set if ($this->contentType && !$request->hasHeader('Content-Type')) { $request->setHeader('Content-Type', $this->contentType); } } $this->writer = null; }
public function signRequest(RequestInterface $request, CredentialsInterface $credentials) { $ldt = gmdate(self::ISO8601_BASIC); $sdt = substr($ldt, 0, 8); $request->removeHeader('Authorization'); $request->removeHeader('x-amz-date'); $request->setHeader('Date', $ldt); if ($token = $credentials->getSecurityToken()) { $request->setHeader('x-amz-security-token', $token); } $cs = $this->createScope($sdt, $this->region, $this->service); $payload = $this->getPayload($request); $context = $this->createContext($request, $payload); $context['string_to_sign'] = $this->createStringToSign($ldt, $cs, $context['creq']); $signingKey = $this->getSigningKey($sdt, $this->region, $this->service, $credentials->getSecretKey()); $signature = hash_hmac('sha256', $context['string_to_sign'], $signingKey); $request->setHeader('Authorization', "AWS4-HMAC-SHA256 " . "Credential={$credentials->getAccessKeyId()}/{$cs}, " . "SignedHeaders={$context['headers']}, Signature={$signature}"); $request->getConfig()['aws.signature'] = $context; }
/** * Set Headers for a Request specified by $headers. * * @param RequestInterface $request a Guzzle Request * @param array $headers headers to set (should be an assoc array). */ private function setGuzzleHeaders(RequestInterface $request, array $headers) { //iterate over the headers array and set each item foreach ($headers as $key => $value) { //Sets Header $request->setHeader($key, $value); } //return the request return $request; }
public function after(CommandInterface $command, RequestInterface $request, Operation $operation, array $context) { $additional = $operation->getAdditionalParameters(); if ($additional && $additional->getLocation() == $this->locationName) { foreach ($command->toArray() as $key => $value) { if (!$operation->hasParam($key)) { $request->setHeader($key, $additional->filter($value)); } } } }
public function after(GuzzleCommandInterface $command, RequestInterface $request, Operation $operation, array $context) { $data = $this->jsonData; $this->jsonData = null; // Add additional parameters to the JSON document $additional = $operation->getAdditionalParameters(); if ($additional && $additional->getLocation() == $this->locationName) { foreach ($command->toArray() as $key => $value) { if (!$operation->hasParam($key)) { $data[$key] = $this->prepareValue($value, $additional); } } } // Don't overwrite the Content-Type if one is set if ($this->jsonContentType && !$request->hasHeader('Content-Type')) { $request->setHeader('Content-Type', $this->jsonContentType); } $request->setBody(Stream::factory(json_encode($data))); }
private function add_decode_content(RequestInterface $request, $value) { if ($value === false) { return; } if ($value !== true) { $request->setHeader('Accept-Encoding', $value); } $request->getConfig()['decode_content'] = true; }
public function sign(RequestInterface $request, $accessToken) { $request->setHeader('Authorization', 'Bearer ' . $accessToken); }
/** * @param RequestInterface $request * @param string $name * @param mixed $args * * @return \GuzzleHttp\Stream\StreamInterface|void */ protected function payload(RequestInterface $request, $name, $args) { $request->setHeader('Content-Type', 'application/xml'); $request->setBody(Stream::factory($this->serializer->serialize($args))); }
protected function applyOptions(RequestInterface $request, array $options = []) { $config = $request->getConfig(); $emitter = $request->getEmitter(); foreach ($options as $key => $value) { if (isset(self::$configMap[$key])) { $config[$key] = $value; continue; } switch ($key) { case 'allow_redirects': if ($value === false) { continue; } if ($value === true) { $value = self::$defaultRedirect; } elseif (!isset($value['max'])) { throw new Iae('allow_redirects must be true, false, or an ' . 'array that contains the \'max\' key'); } else { // Merge the default settings with the provided settings $value += self::$defaultRedirect; } $config['redirect'] = $value; $emitter->attach($this->redirectPlugin); break; case 'decode_content': if ($value === false) { continue; } $config['decode_content'] = true; if ($value !== true) { $request->setHeader('Accept-Encoding', $value); } break; case 'headers': if (!is_array($value)) { throw new Iae('header value must be an array'); } // Do not overwrite existing headers foreach ($value as $k => $v) { if (!$request->hasHeader($k)) { $request->setHeader($k, $v); } } break; case 'exceptions': if ($value === true) { $emitter->attach($this->errorPlugin); } break; case 'body': if (is_array($value)) { $this->addPostData($request, $value); } elseif ($value !== null) { $request->setBody(Stream::factory($value)); } break; case 'auth': if (!$value) { continue; } if (is_array($value)) { $type = isset($value[2]) ? strtolower($value[2]) : 'basic'; } else { $type = strtolower($value); } $config['auth'] = $value; if ($type == 'basic') { $request->setHeader('Authorization', 'Basic ' . base64_encode("{$value['0']}:{$value['1']}")); } elseif ($type == 'digest') { // @todo: Do not rely on curl $config->setPath('curl/' . CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); $config->setPath('curl/' . CURLOPT_USERPWD, "{$value['0']}:{$value['1']}"); } break; case 'query': if ($value instanceof Query) { $original = $request->getQuery(); // Do not overwrite existing query string variables by // overwriting the object with the query string data passed // in the URL $value->overwriteWith($original->toArray()); $request->setQuery($value); } elseif (is_array($value)) { // Do not overwrite existing query string variables $query = $request->getQuery(); foreach ($value as $k => $v) { if (!isset($query[$k])) { $query[$k] = $v; } } } else { throw new Iae('query must be an array or Query object'); } break; case 'cookies': if ($value === true) { static $cookie = null; if (!$cookie) { $cookie = new Cookie(); } $emitter->attach($cookie); } elseif (is_array($value)) { $emitter->attach(new Cookie(CookieJar::fromArray($value, $request->getHost()))); } elseif ($value instanceof CookieJarInterface) { $emitter->attach(new Cookie($value)); } elseif ($value !== false) { throw new Iae('cookies must be an array, true, or CookieJarInterface'); } break; case 'events': if (!is_array($value)) { throw new Iae('events must be an array'); } $this->attachListeners($request, $this->prepareListeners($value, ['before', 'complete', 'error', 'progress', 'end'])); break; case 'subscribers': if (!is_array($value)) { throw new Iae('subscribers must be an array'); } foreach ($value as $subscribers) { $emitter->attach($subscribers); } break; case 'json': $request->setBody(Stream::factory(json_encode($value))); if (!$request->hasHeader('Content-Type')) { $request->setHeader('Content-Type', 'application/json'); } break; default: // Check for custom handler functions. if (isset($this->customOptions[$key])) { $fn = $this->customOptions[$key]; $fn($request, $value); continue; } throw new Iae("No method can handle the {$key} config key"); } } }
public function after(CommandInterface $command, RequestInterface $request, Operation $operation, array $context) { $request->setHeader('Content-Type', 'application/x-www-form-urlencoded'); }
/** * @param RequestInterface $request * @return RequestInterface */ public function setupRequest(RequestInterface $request) { $identitiy = $this->getCurrentIdentity(); if ($identitiy->getCookieJar() != null) { //todo // this seems pretty hacky... is there a better way to replace the cookie container of a request? // > Currently not @see https://github.com/guzzle/guzzle/issues/1028#issuecomment-96253542 - maybe with Guzzle 6 // remove current cookie subscribers $emitter = $request->getEmitter(); foreach ($emitter->listeners("complete") as $listener) { if (is_array($listener) && $listener[0] instanceof Cookie) { $emitter->detach($listener[0]); } } // set new Cookie subscriber $cookie = new Cookie($identitiy->getCookieJar()); $emitter->attach($cookie); } if ($identitiy->getUserAgent() != null) { $request->setHeader("user-agent", $identitiy->getUserAgent()); } $headers = $identitiy->getDefaultRequestHeaders(); if ($headers != null) { foreach ($headers as $key => $val) { $request->setHeader($key, $val); } } if ($identitiy->getReferer() != null && trim($identitiy->getReferer()) != "") { $request->setHeader("referer", $identitiy->getReferer()); } $request = parent::setupRequest($request); return $request; }
/** * Note: This is currently only present in the Amazon S3 model. */ private function applyHeaderMap(RequestInterface $request, $name, Shape $member, array $value) { $prefix = $member['locationName']; foreach ($value as $k => $v) { $request->setHeader($prefix . $k, $v); } }
public function after(CommandInterface $command, RequestInterface $request, Operation $operation, array $context) { if ($this->jsonContentType && !$request->hasHeader('Content-Type')) { $request->setHeader('Content-Type', $this->jsonContentType); } }
protected function payload(RequestInterface $request, StructureShape $member, array $value) { $request->setHeader('Content-Type', 'application/xml'); $request->setBody(Stream::factory($this->xmlBody->build($member, $value))); }
private function add_json(RequestInterface $request, $value) { if (!$request->hasHeader('Content-Type')) { $request->setHeader('Content-Type', 'application/json'); } $request->setBody(Stream\create(json_encode($value))); }
/** * @param InputInterface $input * @param RequestInterface $request * * @return Stream * @throws Exception */ protected function addRequestContent(InputInterface $input, RequestInterface $request) { $type = $input->getOption('request_type'); if (!is_string($type)) { throw new Exception('request_type (-t) is a required parameter when request_content (-c) is used'); } $body = Stream::factory($input->getOption('request_content')); $request->setBody($body); $request->setHeader('Content-Type', $this->parseContentType($type)); }
/** * Add compression to the request. * * @param RequestInterface $request */ protected function addCompression(RequestInterface $request) { $request->setHeader('Accept-Encoding', 'gzip'); }
private function add_headers(RequestInterface $request, $value) { if (!is_array($value)) { throw new \InvalidArgumentException('header value must be an array'); } // Do not overwrite existing headers foreach ($value as $k => $v) { if (!$request->hasHeader($k)) { $request->setHeader($k, $v); } } }
public function sign(RequestInterface $request, $client_id, $client_secret) { $request->getConfig()->set('auth', 'basic'); $request->setHeader('Authorization', 'Basic ' . base64_encode("{$client_id}:{$client_secret}")); }
protected function payload(RequestInterface $request, StructureShape $member, array $value) { $request->setHeader('Content-Type', $this->contentType); $request->setBody(Stream::factory($this->jsonFormatter->build($member, $value))); }
/** * @param RequestInterface $request */ private function prepareRequest(RequestInterface $request) { $request->setHeader('Content-Type', 'application/json'); $this->addAuthTokenHeader($request); }
private function addExpectHeader(RequestInterface $request, StreamInterface $body) { // Determine if the Expect header should be used if ($request->hasHeader('Expect')) { return; } $expect = $request->getConfig()['expect']; // Return if disabled or if you're not using HTTP/1.1 if ($expect === false || $request->getProtocolVersion() !== '1.1') { return; } // The expect header is unconditionally enabled if ($expect === true) { $request->setHeader('Expect', '100-Continue'); return; } // By default, send the expect header when the payload is > 1mb if ($expect === null) { $expect = 1048576; } // Always add if the body cannot be rewound, the size cannot be // determined, or the size is greater than the cutoff threshold $size = $body->getSize(); if ($size === null || $size >= (int) $expect || !$body->isSeekable()) { $request->setHeader('Expect', '100-Continue'); } }
public function addCookieHeader(RequestInterface $request) { $values = array(); $scheme = $request->getScheme(); $host = $request->getHost(); $path = $request->getPath(); foreach ($this->cookies as $cookie) { if ($cookie->matchesPath($path) && $cookie->matchesDomain($host) && !$cookie->isExpired() && (!$cookie->getSecure() || $scheme == 'https')) { $values[] = $cookie->getName() . '=' . self::getCookieValue($cookie->getValue()); } } if ($values) { $request->setHeader('Cookie', implode('; ', $values)); } }