63 * @param string $name |
63 * @param string $name |
64 * @param string $value |
64 * @param string $value |
65 * @param array|Requests_Utility_CaseInsensitiveDictionary $attributes Associative array of attribute data |
65 * @param array|Requests_Utility_CaseInsensitiveDictionary $attributes Associative array of attribute data |
66 */ |
66 */ |
67 public function __construct($name, $value, $attributes = array(), $flags = array(), $reference_time = null) { |
67 public function __construct($name, $value, $attributes = array(), $flags = array(), $reference_time = null) { |
68 $this->name = $name; |
68 $this->name = $name; |
69 $this->value = $value; |
69 $this->value = $value; |
70 $this->attributes = $attributes; |
70 $this->attributes = $attributes; |
71 $default_flags = array( |
71 $default_flags = array( |
72 'creation' => time(), |
72 'creation' => time(), |
73 'last-access' => time(), |
73 'last-access' => time(), |
74 'persistent' => false, |
74 'persistent' => false, |
75 'host-only' => true, |
75 'host-only' => true, |
76 ); |
76 ); |
77 $this->flags = array_merge($default_flags, $flags); |
77 $this->flags = array_merge($default_flags, $flags); |
78 |
78 |
79 $this->reference_time = time(); |
79 $this->reference_time = time(); |
80 if ($reference_time !== null) { |
80 if ($reference_time !== null) { |
81 $this->reference_time = $reference_time; |
81 $this->reference_time = $reference_time; |
82 } |
82 } |
226 * @return boolean Whether the cookie was successfully normalized |
226 * @return boolean Whether the cookie was successfully normalized |
227 */ |
227 */ |
228 public function normalize() { |
228 public function normalize() { |
229 foreach ($this->attributes as $key => $value) { |
229 foreach ($this->attributes as $key => $value) { |
230 $orig_value = $value; |
230 $orig_value = $value; |
231 $value = $this->normalize_attribute($key, $value); |
231 $value = $this->normalize_attribute($key, $value); |
232 if ($value === null) { |
232 if ($value === null) { |
233 unset($this->attributes[$key]); |
233 unset($this->attributes[$key]); |
234 continue; |
234 continue; |
235 } |
235 } |
236 |
236 |
383 * |
383 * |
384 * @param string Cookie header value (from a Set-Cookie header) |
384 * @param string Cookie header value (from a Set-Cookie header) |
385 * @return Requests_Cookie Parsed cookie object |
385 * @return Requests_Cookie Parsed cookie object |
386 */ |
386 */ |
387 public static function parse($string, $name = '', $reference_time = null) { |
387 public static function parse($string, $name = '', $reference_time = null) { |
388 $parts = explode(';', $string); |
388 $parts = explode(';', $string); |
389 $kvparts = array_shift($parts); |
389 $kvparts = array_shift($parts); |
390 |
390 |
391 if (!empty($name)) { |
391 if (!empty($name)) { |
392 $value = $string; |
392 $value = $string; |
393 } |
393 } |
395 // Some sites might only have a value without the equals separator. |
395 // Some sites might only have a value without the equals separator. |
396 // Deviate from RFC 6265 and pretend it was actually a blank name |
396 // Deviate from RFC 6265 and pretend it was actually a blank name |
397 // (`=foo`) |
397 // (`=foo`) |
398 // |
398 // |
399 // https://bugzilla.mozilla.org/show_bug.cgi?id=169091 |
399 // https://bugzilla.mozilla.org/show_bug.cgi?id=169091 |
400 $name = ''; |
400 $name = ''; |
401 $value = $kvparts; |
401 $value = $kvparts; |
402 } |
402 } |
403 else { |
403 else { |
404 list($name, $value) = explode('=', $kvparts, 2); |
404 list($name, $value) = explode('=', $kvparts, 2); |
405 } |
405 } |
406 $name = trim($name); |
406 $name = trim($name); |
407 $value = trim($value); |
407 $value = trim($value); |
408 |
408 |
409 // Attribute key are handled case-insensitively |
409 // Attribute key are handled case-insensitively |
410 $attributes = new Requests_Utility_CaseInsensitiveDictionary(); |
410 $attributes = new Requests_Utility_CaseInsensitiveDictionary(); |
411 |
411 |
412 if (!empty($parts)) { |
412 if (!empty($parts)) { |
413 foreach ($parts as $part) { |
413 foreach ($parts as $part) { |
414 if (strpos($part, '=') === false) { |
414 if (strpos($part, '=') === false) { |
415 $part_key = $part; |
415 $part_key = $part; |
416 $part_value = true; |
416 $part_value = true; |
417 } |
417 } |
418 else { |
418 else { |
419 list($part_key, $part_value) = explode('=', $part, 2); |
419 list($part_key, $part_value) = explode('=', $part, 2); |
420 $part_value = trim($part_value); |
420 $part_value = trim($part_value); |
421 } |
421 } |
422 |
422 |
423 $part_key = trim($part_key); |
423 $part_key = trim($part_key); |
424 $attributes[$part_key] = $part_value; |
424 $attributes[$part_key] = $part_value; |
425 } |
425 } |
426 } |
426 } |
427 |
427 |
428 return new Requests_Cookie($name, $value, $attributes, array(), $reference_time); |
428 return new Requests_Cookie($name, $value, $attributes, array(), $reference_time); |
447 $parsed = self::parse($header, '', $time); |
447 $parsed = self::parse($header, '', $time); |
448 |
448 |
449 // Default domain/path attributes |
449 // Default domain/path attributes |
450 if (empty($parsed->attributes['domain']) && !empty($origin)) { |
450 if (empty($parsed->attributes['domain']) && !empty($origin)) { |
451 $parsed->attributes['domain'] = $origin->host; |
451 $parsed->attributes['domain'] = $origin->host; |
452 $parsed->flags['host-only'] = true; |
452 $parsed->flags['host-only'] = true; |
453 } |
453 } |
454 else { |
454 else { |
455 $parsed->flags['host-only'] = false; |
455 $parsed->flags['host-only'] = false; |
456 } |
456 } |
457 |
457 |
495 /** |
495 /** |
496 * Parse all Set-Cookie headers from request headers |
496 * Parse all Set-Cookie headers from request headers |
497 * |
497 * |
498 * @codeCoverageIgnore |
498 * @codeCoverageIgnore |
499 * @deprecated Use {@see Requests_Cookie::parse_from_headers} |
499 * @deprecated Use {@see Requests_Cookie::parse_from_headers} |
500 * @return string |
500 * @return array |
501 */ |
501 */ |
502 public static function parseFromHeaders(Requests_Response_Headers $headers) { |
502 public static function parseFromHeaders(Requests_Response_Headers $headers) { |
503 return self::parse_from_headers($headers); |
503 return self::parse_from_headers($headers); |
504 } |
504 } |
505 } |
505 } |