10
* Cookie holder object
15
class Requests_Cookie_Jar implements ArrayAccess, IteratorAggregate {
21
protected $cookies = array();
26
* @param array $cookies Existing cookie values
28
public function __construct($cookies = array()) {
29
$this->cookies = $cookies;
33
* Normalise cookie data into a Requests_Cookie
35
* @param string|Requests_Cookie $cookie
36
* @return Requests_Cookie
38
public function normalize_cookie($cookie, $key = null) {
39
if ($cookie instanceof Requests_Cookie) {
43
return Requests_Cookie::parse($cookie, $key);
47
* Normalise cookie data into a Requests_Cookie
50
* @deprecated Use {@see Requests_Cookie_Jar::normalize_cookie}
51
* @return Requests_Cookie
53
public function normalizeCookie($cookie, $key = null) {
54
return $this->normalize_cookie($cookie, $key);
58
* Check if the given item exists
60
* @param string $key Item key
61
* @return boolean Does the item exist?
63
public function offsetExists($key) {
64
return isset($this->cookies[$key]);
68
* Get the value for the item
70
* @param string $key Item key
71
* @return string Item value
73
public function offsetGet($key) {
74
if (!isset($this->cookies[$key])) {
78
return $this->cookies[$key];
84
* @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
86
* @param string $key Item name
87
* @param string $value Item value
89
public function offsetSet($key, $value) {
91
throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
94
$this->cookies[$key] = $value;
98
* Unset the given header
102
public function offsetUnset($key) {
103
unset($this->cookies[$key]);
107
* Get an iterator for the data
109
* @return ArrayIterator
111
public function getIterator() {
112
return new ArrayIterator($this->cookies);
116
* Register the cookie handler with the request's hooking system
118
* @param Requests_Hooker $hooks Hooking system
120
public function register(Requests_Hooker $hooks) {
121
$hooks->register('requests.before_request', array($this, 'before_request'));
122
$hooks->register('requests.before_redirect_check', array($this, 'before_redirect_check'));
126
* Add Cookie header to a request if we have any
128
* As per RFC 6265, cookies are separated by '; '
131
* @param array $headers
133
* @param string $type
134
* @param array $options
136
public function before_request($url, &$headers, &$data, &$type, &$options) {
137
if (!$url instanceof Requests_IRI) {
138
$url = new Requests_IRI($url);
141
if (!empty($this->cookies)) {
143
foreach ($this->cookies as $key => $cookie) {
144
$cookie = $this->normalize_cookie($cookie, $key);
146
// Skip expired cookies
147
if ($cookie->is_expired()) {
151
if ($cookie->domain_matches($url->host)) {
152
$cookies[] = $cookie->format_for_header();
156
$headers['Cookie'] = implode('; ', $cookies);
161
* Parse all cookies from a response and attach them to the response
163
* @var Requests_Response $response
165
public function before_redirect_check(Requests_Response &$return) {
167
if (!$url instanceof Requests_IRI) {
168
$url = new Requests_IRI($url);
171
$cookies = Requests_Cookie::parse_from_headers($return->headers, $url);
172
$this->cookies = array_merge($this->cookies, $cookies);
173
$return->cookies = $this;
b'\\ No newline at end of file'