public function download($url) { $cookies = $this->getSession()->getCookie($this->sessionName); $cookie = SetCookie::fromString($cookies); $jar = new CookieJar(); $jar->setCookie($cookie); $client = new Client(['base_url' => $this->getSession()->getCurrentUrl()]); $this->response = $client->get($url, ['cookies' => $jar]); }
public function __construct(\GuzzleHttp\Client $guzzle, SerializerInterface $serializer, CookieJar $cookieJar) { $this->guzzle = $guzzle; // @TODO temporary solution, as authentication does not work ATM. $cookieJar->setCookie(SetCookie::fromString('SID="AuthKey 23ec5d03-86db-4d80-a378-6059139a7ead"; expires=Thu, 24 Nov 2016 13:52:20 GMT; path=/; domain=.sketchup.com')); $this->serializer = $serializer; }
/** * Create a new CookieAuth plugin. * * @param string $uri Login url * @param array|\Traversable $fields Array containing all fields that should * be send upon login, where the key is the field name and the value is the * field value * @param string $method Resquest method POST or GET * @param string|array|\Traversable|CookieJar $cookies */ public function __construct($uri, $fields, $method = 'POST', $cookies = null) { if ($cookies instanceof CookieJar) { $this->coockieJar = $cookies; } else { $this->coockieJar = new CookieJar(); if (!is_null($cookies)) { if (is_string($cookies)) { $this->coockieJar->setCookie(SetCookie::fromString($cookies)); $this->loginMade = true; } else { if (is_array($cookies) || $cookies instanceof \Traversable) { foreach ($cookies as $cookie) { if (is_string($cookie)) { $this->coockieJar->setCookie(SetCookie::fromString($cookie)); } else { if (is_array($cookie)) { $this->coockieJar->setCookie(new SetCookie($cookie)); } } } $this->loginMade = true; } } } } $this->config = ['uri' => $uri, 'method' => $method, 'fields' => $fields]; }
protected function getCookies(array $headers) { $cookies = []; if (isset($headers['Cookie'])) { $cookies_tmp = explode('; ', $headers['Cookie']); foreach ($cookies_tmp as $cookie) { $data = \GuzzleHttp\Cookie\SetCookie::fromString($cookie)->toArray(); $cookies[$data['Name']] = rawurldecode($data['Value']); } } return $cookies; }
/** * Returns only the final results. To enable interim results, * use session-based requests or Websockets API. Endianness * of the incoming audio is autodetected. Audio files larger * than 4 MB are required to be sent in streaming mode * (chunked transfer-encoding). * * Streaming audio size limit is 100 MB. In streaming mode, * the connection is closed by the server if no data chunk * is received for more than 30 seconds and the last chunk * has not been sent yet (if all data has been sent, it can * take more than 30 seconds to generate the response and the * request will not time out). * * Use getUrlWithSession() to prevent the session from expiring. * The connection is also closed by the server if no speech is * detected for "inactivity_timeout" seconds of audio (not * processing time). This time can be set by "inactivity_timeout" * parameter; the default is 30 seconds. * * @param string $pathToFile * @param string $contentType * @param bool $uri * * @return $this */ public function recognize($pathToFile, $contentType = self::CONTENT_TYPE, $uri = false) { $uri = $uri ?: $this->getUrlWithSession('/recognize'); foreach ($this->getSetCookieHeader() as $cookie) { $cookie = SetCookie::fromString($cookie); $cookie->setDomain('stream.watsonplatform.net'); $this->cookieJar->setCookie($cookie); } $data[] = ['name' => 'audio_file', 'contents' => fopen($pathToFile, 'r')]; $this->recognize = $this->request($uri, 'POST', ['multipart' => $data, 'query' => ['continuous' => 'true', 'inactivity_timeout' => -1], 'cookies' => $this->cookieJar, 'headers' => ['content-type' => $contentType]]); return $this; }
public function testOnReceive() { // mock request $request = new Request('GET', $this->config['uri']); // mock response $response = new Response(200, ['Cookie' => SetCookie::fromString($this->config['cookies'])], null); // middleware $m = new CookieAuth($this->config['uri'], $this->config['fields'], $this->config['method']); $result = $m->onReceive($request, $response); $this->assertInstanceOf('\\Psr\\Http\\Message\\ResponseInterface', $result); $this->assertTrue($result->hasHeader('Cookie')); $cookieHeader = $result->getHeader('Cookie'); $cookieHeader = $cookieHeader[0]; $this->assertEquals($this->config['cookies'], $cookieHeader); }
protected function getCookies(array $headers) { $cookies = []; if (isset($headers['Cookie'])) { if (function_exists('http_parse_cookie')) { $cookie_data = http_parse_cookie($headers['Cookie']); if ($cookie_data) { $cookies = $cookie_data->cookies; } } else { if (class_exists("\\Guzzle\\Parser\\Cookie\\CookieParser")) { $cookies = array_get(with(new \Guzzle\Parser\Cookie\CookieParser())->parseCookie($headers['Cookie']), 'cookies', []); } else { if (class_exists("\\GuzzleHttp\\Cookie\\SetCookie")) { foreach (\GuzzleHttp\Cookie\SetCookie::fromString($headers['Cookie'])->toArray() as $data) { $cookies[$data['Name']] = $data['Value']; } } } } } return $cookies; }
public function extractCookies(RequestInterface $request, ResponseInterface $response) { if ($cookieHeader = $response->getHeader('Set-Cookie')) { foreach ($cookieHeader as $cookie) { $sc = SetCookie::fromString($cookie); if (!$sc->getDomain()) { $sc->setDomain($request->getUri()->getHost()); } $this->setCookie($sc); } } }
protected function extractCookies($host) { $jar = []; $cookies = $this->getCookieJar()->all(); foreach ($cookies as $cookie) { /** @var $cookie Cookie **/ $setCookie = SetCookie::fromString((string) $cookie); if (!$setCookie->getDomain()) { $setCookie->setDomain($host); } $jar[] = $setCookie; } return new CookieJar(false, $jar); }
/** * @dataProvider cookieParserDataProvider */ public function testParseCookie($cookie, $parsed) { foreach ((array) $cookie as $v) { $c = SetCookie::fromString($v); $p = $c->toArray(); if (isset($p['Expires'])) { // Remove expires values from the assertion if they are relatively equal if (abs($p['Expires'] != strtotime($parsed['Expires'])) < 40) { unset($p['Expires']); unset($parsed['Expires']); } } if (!empty($parsed)) { foreach ($parsed as $key => $value) { $this->assertEquals($parsed[$key], $p[$key], 'Comparing ' . $key . ' ' . var_export($value, true) . ' : ' . var_export($parsed, true) . ' | ' . var_export($p, true)); } foreach ($p as $key => $value) { $this->assertEquals($p[$key], $parsed[$key], 'Comparing ' . $key . ' ' . var_export($value, true) . ' : ' . var_export($parsed, true) . ' | ' . var_export($p, true)); } } else { $this->assertEquals(['Name' => null, 'Value' => null, 'Domain' => null, 'Path' => '/', 'Max-Age' => null, 'Expires' => null, 'Secure' => false, 'Discard' => false, 'HttpOnly' => false], $p); } } }
/** * Create cookie and store it. * * @param string $session */ public function storeCookie($session) { $cookie = SetCookie::fromString("PHPSESSID={$session}; path=/"); $cookie->setDomain('ecourse.ccu.edu.tw'); $cookie->setDiscard(true); session()->put('COOKIE', new CookieJar(false, [$cookie])); }
/** * Regenerate CSFR Token */ public function generateCSRFToken() { $response = $this->httpclient->get('/users/' . $this->userId, ['cookies' => true]); foreach ($response->getHeaders()['Set-Cookie'] as $item) { $cookie = SetCookie::fromString($item); if ($cookie->getName() === 'CSRF_TOKEN') { $this->csrf = $cookie->getValue(); } } }
/** * @param Request $request * * @return Response */ protected function doRequest($request) { $headers = array(); foreach ($request->getServer() as $key => $val) { $key = strtolower(str_replace('_', '-', $key)); $contentHeaders = array('content-length' => true, 'content-md5' => true, 'content-type' => true); if (0 === strpos($key, 'http-')) { $headers[substr($key, 5)] = $val; } elseif (isset($contentHeaders[$key])) { $headers[$key] = $val; } } $parts = array_replace(array('path' => '/'), parse_url($request->getUri())); $cookies = $this->getCookieJar()->all(); foreach ($cookies as $cookie) { $domain = $cookie->getDomain(); if ($domain) { $domain = '.' . ltrim($domain, '.'); if ($domain != substr('.' . $parts['host'], -strlen($domain))) { continue; } } $path = $cookie->getPath(); if ($path != substr($parts['path'], 0, strlen($path))) { continue; } if ($cookie->isSecure() && 'https' != $parts['scheme']) { continue; } $setCookie = SetCookie::fromString((string) $cookie); if (empty($setCookie->getDomain())) { $setCookie->setDomain($parts['host']); } $this->getGuzzleCookieJar()->setCookie($setCookie); } $requestOptions = array('cookies' => $this->getGuzzleCookieJar(), 'allow_redirects' => false, 'auth' => $this->auth); if (!in_array($request->getMethod(), array('GET', 'HEAD'))) { if (null !== ($content = $request->getContent())) { $requestOptions['body'] = $content; } else { if ($files = $request->getFiles()) { $requestOptions['multipart'] = []; $this->addPostFields($request->getParameters(), $requestOptions['multipart']); $this->addPostFiles($files, $requestOptions['multipart']); } else { $requestOptions['form_params'] = $request->getParameters(); } } } if (!empty($headers)) { $requestOptions['headers'] = $headers; } $method = $request->getMethod(); $uri = $request->getUri(); foreach ($this->headers as $name => $value) { $requestOptions['headers'][$name] = $value; } // Let BrowserKit handle redirects try { $response = $this->getClient()->request($method, $uri, $requestOptions); } catch (RequestException $e) { $response = $e->getResponse(); if (null === $response) { throw $e; } } return $this->createResponse($response); }