Middleware that adds cookies to requests.

The options array must be set to a CookieJarInterface in order to use
cookies. This is typically handled for you by a client.

@return callable Returns a function that accepts the next handler.
434448
public static function cookies()
    {
        return function (callable $handler) {
            return function ($request, array $options) use ($handler) {
                if (empty($options['cookies'])) {
                    return $handler($request, $options);
                } elseif (!($options['cookies'] instanceof CookieJarInterface)) {
                    throw new \InvalidArgumentException('cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface');
                }
                $cookieJar = $options['cookies'];
                $request = $cookieJar->withCookieHeader($request);
                return $handler($request, $options)
                    ->then(
                        function ($response) use ($cookieJar, $request) {
                            $cookieJar->extractCookies($request, $response);
                            return $response;
                        }
                );
            };
        };
    }
1225165
public function setCookie($cookies = [])
    {
        $this->cookies = $cookies;
        $this->debug->info(__FUNCTION__, 'setCookie: ', $this->cookies);

        return $this;
    }
189739
protected function usingCookieSessions()
    {
        if (! $this->sessionConfigured()) {
            return false;
        }

        $handler = $this->manager->driver()->getHandler();

        if ($handler instanceof LockingSessionHandler) {
            return $handler->usingCookieSessions();
        }

        return parent::usingCookieSessions();
    }
319903
protected function curlInit()
    {
        $curlHandle = curl_init();
        curl_setopt($curlHandle, CURLOPT_URL, $this->requestUrl);
        $this->requestUrl = $this->url;  // Reset request URL
        // Set Curl Options
        foreach ($this->curlOptions as $key => $value) {
            curl_setopt($curlHandle, $key, $value);
        }

        // Check if have header
        if (count($this->requestHeader) > 0) {
            curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $this->requestHeader);
            $this->requestHeader = []; // Reset request Header
        }

        // Add Cookies
        if (count($this->cookies) > 0) {
            curl_setopt($curlHandle, CURLOPT_COOKIE, implode(";", $this->cookies));
            $this->cookies = []; // Reset request Header
        }

        // Set last fetched URL
        $this->lastFetchedUrl = null;

        return $curlHandle;
    }
451304
private function decorate($middleware): MiddlewareInterface
    {
        if ($middleware instanceof MiddlewareInterface) {
            return $middleware;
        } elseif ($middleware instanceof RequestHandlerInterface) {
            return new RequestHandlerMiddleware($middleware);
        } elseif ($middleware instanceof ResponseInterface) {
            return new FixedResponseMiddleware($middleware);
        } elseif (is_callable($middleware)) {
            return new CallableMiddleware($middleware);
        } elseif (is_string($middleware)) {
            return new LazyLoadingMiddleware($middleware, $this->container);
        } else {
            throw new InvalidArgumentException(sprintf(
                'Middleware "%s" is neither a string service name, an autoloadable class name, a PHP callable, or an instance of %s/%s/%s',
                is_object($middleware) ? get_class($middleware) : gettype($middleware),
                MiddlewareInterface::class, ResponseInterface::class, RequestHandlerInterface::class
            ));
        }
    }
754693
public function getCookieJar(array &$options, $base_uri = null)
    {
        if ($this->coockieJar->count() <= 0 || !$this->loginMade) {
            $this->obtainCookies($options, $base_uri);
        }

        if (isset($options['auth-cookie'])) {
            unset($options['auth-cookie']);
        }

        return $this->coockieJar;
    }
375681
public static function httpErrors()
    {
        return function (callable $handler) {
            return function ($request, array $options) use ($handler) {
                if (empty($options['http_errors'])) {
                    return $handler($request, $options);
                }
                return $handler($request, $options)->then(
                    function (ResponseInterface $response) use ($request) {
                        $code = $response->getStatusCode();
                        if ($code < 400) {
                            return $response;
                        }
                        throw RequestException::create($request, $response);
                    }
                );
            };
        };
    }
407968
def request(self, method, url, **kwargs):
        '''
        '''
        # TODO should we raise an exception if auth or cookies are explicitly
        # mentioned in kwargs?
        kwargs['auth'] = self.auth()
        kwargs['cookies'] = self.cookies
        return requests.request(method=method, url=url, **kwargs)
1355723
public function withCookieParams(array $cookie_params): SessionMiddleware
    {
        return new SessionMiddleware(
            $this->save_handler,
            $this->name,
            $this->save_path,
            $this->cache_limiter,
            $this->cache_expire,
            $cookie_params
        );
    }
71462
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
        {
            $cookies  = $request->getCookieParams();
            $name     = $this->name ?: session_name();
            $id       = $cookies[$name] ?? bin2hex(random_bytes(16));
            $options  = $this->options;
            $session  = $this->session->start($id, $name, $options);
            
            $request  = $request->withAttribute($this->sessionRequestAttribute, $session);
            $response = $handler->handle($request);
            $session->save();
            
            return isset($cookies[$name]) ? $response : $response->withHeader(
                'Set-Cookie',
                sprintf(
                    "%s=%s; path=%s", 
                    $name, 
                    $id, 
                    ini_get('session.cookie_path')
                )
            );
        }
1272074
public static function SendCookie () {
		if (!static::GetStarted()) return;
		$maxExpiration = static::GetSessionMaxTime();
		$response = & \MvcCore\Application::GetInstance()->GetResponse();
		if (!$response->IsSent()) {
			$params = (object) session_get_cookie_params();
			$response->SetCookie(
				session_name(),
				session_id(),
				($maxExpiration > static::$sessionStartTime
					? (static::$sessionMaxTime - static::$sessionStartTime)
					: (isset($params->lifetime) ? $params->lifetime : 0)),
				$params->path,
				$params->domain,
				$params->secure,
				TRUE
			);
		}
	}
907795
private function onBeforeSend(RequestInterface $request, array &$options)
    {
        $base_uri = null;
        if (isset($options['base_uri']) && $options['base_uri']) {
            if ($options['base_uri'] instanceof UriInterface) {
                $base_uri = $options['base_uri'];
            }
        }

        $cookieJar = $this->getCookieJar($options, $base_uri);
        return $cookieJar->withCookieHeader($request);
    }
928149
protected function cookies()
        {
            $ref = new \ReflectionClass('Symfony\Component\HttpFoundation\Cookie');

            // All of the cookies for the response are actually stored on the
            // Cookie class until we're ready to send the response back to
            // the browser. This allows our cookies to be set easily.
            foreach (Cookie::$jar as $name => $cookie) {
                $config = array_values($cookie);
                $this->headers()->setCookie($ref->newInstanceArgs($config));
            }
        }
103211
public function cookie(Closure $callback = null)
    {
        if ($callback instanceof CookieInterface) {
            return Cookie::create($callback);
        }

        return Cookie::create();
    }
96650
public function setCookies($cookies = [])
    {

        $this->cookies = ($cookies instanceof Parameters) ? $cookies : new Parameters("cookies", (array)$cookies);

        return $this;
    }
739700
protected function createHandlerStack()
    {
        $stack = HandlerStack::create(new CurlHandler());

        $stack->push(ClientMiddlewares::cache($this->cache));
        $stack->push(
            ClientMiddlewares::setBaseHeaders(
                $this->api['token'], $this->config['basecamp.user-agent']
            )
        );
        $stack->push(
            ClientMiddlewares::retry($this->api, $this->config)
        );

        // Add custom middlewares.
        foreach ($this->middlewares as $middleware) {
            $stack->push($middleware);
        }

        return $stack;
    }
577241
public static function createFromIni(): CookieInterface
    {
        $params = session_get_cookie_params();

        return (new static())
            ->withLifetime($params["lifetime"])
            ->withPath($params["path"])
            ->withDomain($params["domain"])
            ->withSecure($params["secure"])
            ->withHttpOnly($params["httponly"]);
    }