-
Notifications
You must be signed in to change notification settings - Fork 52
Add HttpHeaderCollection #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dovg
merged 1 commit into
onPHP:master
from
DanielPlainview:feature-http-header-collection
Oct 21, 2013
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
/*************************************************************************** | ||
* Copyright (C) 2013 by Nikita V. Konstantinov * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU Lesser General Public License as * | ||
* published by the Free Software Foundation; either version 3 of the * | ||
* License, or (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
/** | ||
* @ingroup Http | ||
**/ | ||
class HttpHeaderCollection implements IteratorAggregate | ||
{ | ||
private $headers = array(); | ||
|
||
public function __construct(array $headers = array()) | ||
{ | ||
foreach ($headers as $name => $value) | ||
$this->set($name, $value); | ||
} | ||
|
||
public function set($name, $value) | ||
{ | ||
$this->headers[$this->normalizeName($name)]= | ||
array_values((array) $value); | ||
|
||
return $this; | ||
} | ||
|
||
public function add($name, $value) | ||
{ | ||
$name = $this->normalizeName($name); | ||
|
||
if (array_key_exists($name, $this->headers)) | ||
$this->headers[$name][] = $value; | ||
else | ||
$this->set($name, $value); | ||
|
||
return $this; | ||
} | ||
|
||
public function remove($name) | ||
{ | ||
if (!$this->has($name)) { | ||
throw new MissingElementException( | ||
sprintf('Header "%s" does not exist', $name) | ||
); | ||
} | ||
|
||
unset($this->headers[$this->normalizeName($name)]); | ||
|
||
return $this; | ||
} | ||
|
||
public function get($name) | ||
{ | ||
$valueList = $this->getRaw($name); | ||
|
||
return count($valueList) > 1 ? $valueList : $valueList[0]; | ||
} | ||
|
||
public function has($name) | ||
{ | ||
return | ||
array_key_exists( | ||
$this->normalizeName($name), | ||
$this->headers | ||
); | ||
} | ||
|
||
public function getRaw($name) | ||
{ | ||
if (!$this->has($name)) { | ||
throw new MissingElementException( | ||
sprintf('Header "%s" does not exist', $name) | ||
); | ||
} | ||
|
||
return $this->headers[$this->normalizeName($name)]; | ||
} | ||
|
||
public function getAll() | ||
{ | ||
return $this->headers; | ||
} | ||
|
||
public function getIterator() | ||
{ | ||
return new ArrayIterator($this->headers); | ||
} | ||
|
||
private function normalizeName($name) | ||
{ | ||
return | ||
preg_replace_callback( | ||
'/(?<name>[^-]+)/', | ||
function ($match) { | ||
return | ||
strtoupper(substr($match['name'], 0, 1)) | ||
.strtolower(substr($match['name'], 1)) | ||
; | ||
}, | ||
$name | ||
); | ||
} | ||
} | ||
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/*************************************************************************** | ||
* Copyright (C) 2013 by Nikita V. Konstantinov * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU Lesser General Public License as * | ||
* published by the Free Software Foundation; either version 3 of the * | ||
* License, or (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
class HttpHeaderCollectionTest extends TestCase | ||
{ | ||
public function testSetter() | ||
{ | ||
$collection = | ||
new HttpHeaderCollection( | ||
array('Content-Length' => 42) | ||
); | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* @depends testSetter | ||
*/ | ||
public function testAddition(HttpHeaderCollection $collection) | ||
{ | ||
$collection->add('x-foo', 'bar')->add('x-foo', 'baz'); | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* @depends testAddition | ||
*/ | ||
public function testGetter(HttpHeaderCollection $collection) | ||
{ | ||
$this->assertEquals(42, $collection->get('content-LeNgTh')); | ||
$this->assertEquals(array(42), $collection->getRaw('content-LeNgTh')); | ||
$this->assertEquals(array('bar', 'baz'), $collection->get('x-foo')); | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* @depends testGetter | ||
*/ | ||
public function testRemoving(HttpHeaderCollection $collection) | ||
{ | ||
$collection->remove('x-foo'); | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* @depends testRemoving | ||
* @expectedException MissingElementException | ||
*/ | ||
public function testFailedRemoving(HttpHeaderCollection $collection) | ||
{ | ||
$collection->remove('x-foo'); | ||
|
||
return $collection; | ||
} | ||
} | ||
?> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http://www.ietf.org/rfc/rfc2616.txt p.4.2 последний абзац.
Это не обязывает нас при получении $value делать explode по \r\n и ";" ?
Насколько я понимаю, с точки зрения протокола
x-foo: bar;baz
равнозначно с
x-foo: bar
x-foo: baz
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Там речь про comma (","), а не semicolon (";"). Мне неясен это абзац совершенно. Если это так, то как различать значение и список значений? Экранирования нет, запятая не запрещена.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тесты показали, что популярным веб-серверам и языкам разработки пофиг на это требование.
Если возражений от сообщества нет, то предлагаю вливать.