Skip to content

Commit fda6590

Browse files
committed
Merge pull request #202 from DanielPlainview/feature-http-header-collection
Add HttpHeaderCollection
2 parents 5e8cfbd + 14eaba0 commit fda6590

File tree

5 files changed

+218
-12
lines changed

5 files changed

+218
-12
lines changed

doc/ChangeLog

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
2013-10-17 Nikita V. Konstantinov
2+
3+
* main/Flow/HttpRequest.class.php
4+
* main/Net/Http/HttpHeaderCollection.class.php
5+
* test/AllTests.php
6+
* test/main/Net/Http/HttpHeaderCollectionTest.class.php:
7+
added HttpHeaderCollection
8+
9+
2013-10-15 Nikita V. Konstantinov
10+
11+
* main/Flow/HttpRequest.class.php:
12+
added HttpRequest::createFromGlobals()
13+
114
2013-09-03 Vasily M. Stashko
215

316
* core/DB/MySQL.class.php
@@ -253,7 +266,7 @@
253266

254267
Introducing Uncachers
255268

256-
2012-06-29 Nikita Konstantinov
269+
2012-06-29 Nikita V. Konstantinov
257270

258271
* core/DB/PgSQL.class.php
259272
test/core/DbConnectionTest.class.php:

main/Flow/HttpRequest.class.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class HttpRequest
3535
// all other sh1t
3636
private $attached = array();
3737

38-
private $headers = array();
38+
private $headers = null;
3939

4040
/**
4141
* @var HttpMethod
@@ -47,7 +47,6 @@ final class HttpRequest
4747
*/
4848
private $url = null;
4949

50-
//for CurlHttpClient if you need to send raw CURLOPT_POSTFIELDS
5150
private $body = null;
5251

5352
/**
@@ -74,9 +73,12 @@ public static function createFromGlobals()
7473
if (isset($_SESSION))
7574
$request->setSession($_SESSION);
7675

77-
foreach ($_SERVER as $name => $value)
78-
if (substr($name, 0, 5) === 'HTTP_')
79-
$request->setHeaderVar(substr($name, 5), $value);
76+
foreach ($_SERVER as $name => $value) {
77+
if (strpos($name, 'HTTP_') === 0) {
78+
$name = str_replace('_', '-', substr($name, 5));
79+
$request->setHeaderVar($name, $value);
80+
}
81+
}
8082

8183
if (
8284
$request->hasServerVar('CONTENT_TYPE')
@@ -86,6 +88,11 @@ public static function createFromGlobals()
8688

8789
return $request;
8890
}
91+
92+
public function __construct()
93+
{
94+
$this->headers = new HttpHeaderCollection();
95+
}
8996

9097
public function &getGet()
9198
{
@@ -218,7 +225,11 @@ public function &getSession()
218225
{
219226
return $this->session;
220227
}
221-
228+
229+
/**
230+
* @param string $name
231+
* @return mixed
232+
*/
222233
public function getSessionVar($name)
223234
{
224235
return $this->session[$name];
@@ -278,7 +289,11 @@ public function &getAttached()
278289
{
279290
return $this->attached;
280291
}
281-
292+
293+
/**
294+
* @param string $name
295+
* @return mixed
296+
*/
282297
public function getAttachedVar($name)
283298
{
284299
return $this->attached[$name];
@@ -306,7 +321,7 @@ public function getByType(RequestType $type)
306321

307322
public function getHeaderList()
308323
{
309-
return $this->headers;
324+
return $this->headers->getAll();
310325
}
311326

312327
public function hasHeaderVar($name)
@@ -316,7 +331,7 @@ public function hasHeaderVar($name)
316331

317332
public function getHeaderVar($name)
318333
{
319-
return $this->headers[$name];
334+
return $this->headers->get($name);
320335
}
321336

322337
/**
@@ -333,7 +348,7 @@ public function unsetHeaderVar($name)
333348
**/
334349
public function setHeaderVar($name, $var)
335350
{
336-
$this->headers[$name] = $var;
351+
$this->headers->set($name, $var);
337352
return $this;
338353
}
339354

@@ -342,7 +357,7 @@ public function setHeaderVar($name, $var)
342357
**/
343358
public function setHeaders(array $headers)
344359
{
345-
$this->headers = $headers;
360+
$this->headers = new HttpHeaderCollection($headers);
346361
return $this;
347362
}
348363

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/***************************************************************************
3+
* Copyright (C) 2013 by Nikita V. Konstantinov *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify *
6+
* it under the terms of the GNU Lesser General Public License as *
7+
* published by the Free Software Foundation; either version 3 of the *
8+
* License, or (at your option) any later version. *
9+
* *
10+
***************************************************************************/
11+
12+
/**
13+
* @ingroup Http
14+
**/
15+
class HttpHeaderCollection implements IteratorAggregate
16+
{
17+
private $headers = array();
18+
19+
public function __construct(array $headers = array())
20+
{
21+
foreach ($headers as $name => $value)
22+
$this->set($name, $value);
23+
}
24+
25+
public function set($name, $value)
26+
{
27+
$this->headers[$this->normalizeName($name)]=
28+
array_values((array) $value);
29+
30+
return $this;
31+
}
32+
33+
public function add($name, $value)
34+
{
35+
$name = $this->normalizeName($name);
36+
37+
if (array_key_exists($name, $this->headers))
38+
$this->headers[$name][] = $value;
39+
else
40+
$this->set($name, $value);
41+
42+
return $this;
43+
}
44+
45+
public function remove($name)
46+
{
47+
if (!$this->has($name)) {
48+
throw new MissingElementException(
49+
sprintf('Header "%s" does not exist', $name)
50+
);
51+
}
52+
53+
unset($this->headers[$this->normalizeName($name)]);
54+
55+
return $this;
56+
}
57+
58+
public function get($name)
59+
{
60+
$valueList = $this->getRaw($name);
61+
62+
return count($valueList) > 1 ? $valueList : $valueList[0];
63+
}
64+
65+
public function has($name)
66+
{
67+
return
68+
array_key_exists(
69+
$this->normalizeName($name),
70+
$this->headers
71+
);
72+
}
73+
74+
public function getRaw($name)
75+
{
76+
if (!$this->has($name)) {
77+
throw new MissingElementException(
78+
sprintf('Header "%s" does not exist', $name)
79+
);
80+
}
81+
82+
return $this->headers[$this->normalizeName($name)];
83+
}
84+
85+
public function getAll()
86+
{
87+
return $this->headers;
88+
}
89+
90+
public function getIterator()
91+
{
92+
return new ArrayIterator($this->headers);
93+
}
94+
95+
private function normalizeName($name)
96+
{
97+
return
98+
preg_replace_callback(
99+
'/(?<name>[^-]+)/',
100+
function ($match) {
101+
return
102+
strtoupper(substr($match['name'], 0, 1))
103+
.strtolower(substr($match['name'], 1))
104+
;
105+
},
106+
$name
107+
);
108+
}
109+
}
110+
?>

test/AllTests.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Autoloader'.DIRECTORY_SEPARATOR,
2828
ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Ip'.DIRECTORY_SEPARATOR,
2929
ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Net'.DIRECTORY_SEPARATOR,
30+
ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Net'.DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR,
3031
ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Utils'.DIRECTORY_SEPARATOR,
3132
ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Utils'.DIRECTORY_SEPARATOR.'Routers'.DIRECTORY_SEPARATOR,
3233
ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Utils'.DIRECTORY_SEPARATOR.'AMQP'.DIRECTORY_SEPARATOR,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/***************************************************************************
3+
* Copyright (C) 2013 by Nikita V. Konstantinov *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify *
6+
* it under the terms of the GNU Lesser General Public License as *
7+
* published by the Free Software Foundation; either version 3 of the *
8+
* License, or (at your option) any later version. *
9+
* *
10+
***************************************************************************/
11+
12+
class HttpHeaderCollectionTest extends TestCase
13+
{
14+
public function testSetter()
15+
{
16+
$collection =
17+
new HttpHeaderCollection(
18+
array('Content-Length' => 42)
19+
);
20+
21+
return $collection;
22+
}
23+
24+
/**
25+
* @depends testSetter
26+
*/
27+
public function testAddition(HttpHeaderCollection $collection)
28+
{
29+
$collection->add('x-foo', 'bar')->add('x-foo', 'baz');
30+
31+
return $collection;
32+
}
33+
34+
/**
35+
* @depends testAddition
36+
*/
37+
public function testGetter(HttpHeaderCollection $collection)
38+
{
39+
$this->assertEquals(42, $collection->get('content-LeNgTh'));
40+
$this->assertEquals(array(42), $collection->getRaw('content-LeNgTh'));
41+
$this->assertEquals(array('bar', 'baz'), $collection->get('x-foo'));
42+
43+
return $collection;
44+
}
45+
46+
/**
47+
* @depends testGetter
48+
*/
49+
public function testRemoving(HttpHeaderCollection $collection)
50+
{
51+
$collection->remove('x-foo');
52+
53+
return $collection;
54+
}
55+
56+
/**
57+
* @depends testRemoving
58+
* @expectedException MissingElementException
59+
*/
60+
public function testFailedRemoving(HttpHeaderCollection $collection)
61+
{
62+
$collection->remove('x-foo');
63+
64+
return $collection;
65+
}
66+
}
67+
?>

0 commit comments

Comments
 (0)