Permalink
Browse files

Lower case all header keys, which fixes tests and makes folks code be…

…tter.
  • Loading branch information...
1 parent be1ceb8 commit 6c02c2860dc122d4473eef05328bb75eea89f896 @sbisbee committed Jul 20, 2012
View
7 CHANGELOG
@@ -6,6 +6,13 @@ Version 0.8.0
Breaking Changes
+ * Header keys are automatically lower cased now, whereas they previously kept
+ their capitalization. Unfortunately different API endpoints would sometimes
+ capitalize header names different (ex., Etag vs. ETag). Additionally,
+ BigCouch and Cloudant sometimes have different header capitalization. So,
+ whereas previously you might have done `$response->headers->Cookie` you
+ must now do `$response->headers->cookie`.
+
* We no longer check the PHP install's configuration for a our supported
error_reporting level. This is because too many different types of installs
were causing false positives and false negatives, needlessly breaking
View
7 src/Sag.php
@@ -257,10 +257,13 @@ public function get($url) {
$prevResponse = $this->cache->get($url);
if($prevResponse) {
- $response = $this->procPacket('GET', $url, null, array('If-None-Match' => $prevResponse->headers->Etag));
+ $response = $this->procPacket('GET', $url, null, array('If-None-Match' => $prevResponse->headers->etag));
if($response->headers->_HTTP->status == 304) {
- return $prevResponse; //cache hit
+ //cache hit
+ $response->fromCache = true;
+
+ return $prevResponse;
}
$this->cache->remove($url);
View
4 src/SagCache.php
@@ -153,8 +153,8 @@ protected function mayCache($item) {
isset($item) &&
is_object($item) &&
isset($item->headers) &&
- is_string($item->headers->Etag) &&
- !empty($item->headers->Etag) &&
+ is_string($item->headers->etag) &&
+ !empty($item->headers->etag) &&
isset($item->body) &&
is_object($item->body)
);
View
1 src/httpAdapters/SagCURLHTTPAdapter.php
@@ -121,6 +121,7 @@ public function procPacket($method, $url, $data = null, $headers = array(), $spe
}
else {
$line = explode(':', $headers[$i], 2);
+ $line[0] = strtolower($line[0]);
$response->headers->$line[0] = ltrim($line[1]);
if($line[0] == 'Set-Cookie') {
View
4 src/httpAdapters/SagHTTPAdapter.php
@@ -37,8 +37,8 @@ protected function makeResult($response, $method) {
//Make sure we got the complete response.
if(
$method != 'HEAD' &&
- isset($response->headers->{'Content-Length'}) &&
- strlen($response->body) != $response->headers->{'Content-Length'}
+ isset($response->headers->{'content-length'}) &&
+ strlen($response->body) != $response->headers->{'content-length'}
) {
throw new SagException('Unexpected end of packet.');
}
View
21 src/httpAdapters/SagNativeHTTPAdapter.php
@@ -135,11 +135,11 @@ public function procPacket($method, $url, $data = null, $headers = array(), $spe
!$isHeader &&
$method != 'HEAD' &&
(
- isset($response->headers->{'Transfer-Encoding'}) == 'chunked' ||
- !isset($response->headers->{'Content-Length'}) ||
+ isset($response->headers->{'transfer-encoding'}) == 'chunked' ||
+ !isset($response->headers->{'content-length'}) ||
(
- isset($response->headers->{'Content-Length'}) &&
- strlen($response->body) < $response->headers->{'Content-Length'}
+ isset($response->headers->{'content-length'}) &&
+ strlen($response->body) < $response->headers->{'content-length'}
)
)
)
@@ -160,9 +160,9 @@ public function procPacket($method, $url, $data = null, $headers = array(), $spe
*
* And we can't use a ternary because fgets() wants an int or undefined.
*/
- if(!$isHeader && $response->headers->{'Transfer-Encoding'} !== 'chunked') {
+ if(!$isHeader && $response->headers->{'transfer-encoding'} !== 'chunked') {
//the +1 is because fgets() reads (length - 1) bytes
- $line = fgets($sock, $response->headers->{'Content-Length'} - strlen($response->body) + 1);
+ $line = fgets($sock, $response->headers->{'content-length'} - strlen($response->body) + 1);
}
else {
$line = fgets($sock);
@@ -202,10 +202,11 @@ public function procPacket($method, $url, $data = null, $headers = array(), $spe
}
else {
$line = explode(':', $line, 2);
+ $line[0] = strtolower($line[0]);
$response->headers->$line[0] = $line[1] = ltrim($line[1]);
switch($line[0]) {
- case 'Set-Cookie':
+ case 'set-cookie':
$response->cookies = new stdClass();
foreach(explode('; ', $line[1]) as $cookie) {
@@ -215,7 +216,7 @@ public function procPacket($method, $url, $data = null, $headers = array(), $spe
break;
- case 'Location':
+ case 'location':
//only follow Location header on 3xx status codes
if($response->headers->_HTTP->status >= 300 && $response->headers->_HTTP->status < 400) {
$line[1] = parse_url($line[1]);
@@ -235,7 +236,7 @@ public function procPacket($method, $url, $data = null, $headers = array(), $spe
}
}
}
- else if($response->headers->{'Transfer-Encoding'}) {
+ else if($response->headers->{'transfer-encoding'}) {
/*
* Parse the response's body, which is being sent in chunks. Welcome to
* HTTP/1.1 land.
@@ -300,7 +301,7 @@ public function procPacket($method, $url, $data = null, $headers = array(), $spe
}
// HTTP/1.1 assumes persisted connections, but proxies might close them.
- if(strtolower($response->headers->Connection) != 'close') {
+ if(strtolower($response->headers->connection) != 'close') {
$this->connPool[] = $sock;
}
View
4 tests/SagFileCacheTest.php
@@ -36,7 +36,7 @@ public function test_createNew()
$item->body = new stdClass();
$item->body->foo = "bar";
$item->headers = new stdClass();
- $item->headers->Etag = "\"asdfasfsadfsadf\"";
+ $item->headers->etag = "\"asdfasfsadfsadf\"";
$res = $this->cache->set($url, $item);
$this->assertTrue($res === true || is_object($res));
@@ -71,7 +71,7 @@ public function test_newVersion()
$new->body = new stdClass();
$new->body->titFor = "tat";
$new->headers = new stdClass();
- $new->headers->Etag = "\"asdfasdfasdfasdf\"";
+ $new->headers->etag = "\"asdfasdfasdfasdf\"";
$file = $this->cache->makeFilename('/bwah');
View
10 tests/SagMemoryCacheTest.php
@@ -32,7 +32,7 @@ public function test_createAndGet()
$item->body = new stdClass();
$item->body->foo = "bar";
$item->headers = new stdClass();
- $item->headers->Etag = "\"asdfasfsadfsadf\"";
+ $item->headers->etag = "\"asdfasfsadfsadf\"";
$res = $this->cache->set($url, $item);
$this->assertTrue($res === true || is_object($res));
@@ -55,7 +55,7 @@ public function test_remove()
$item->body = new stdClass();
$item->body->foo = "bar";
$item->headers = new stdClass();
- $item->headers->Etag = "\"asdfasfsadfsadf\"";
+ $item->headers->etag = "\"asdfasfsadfsadf\"";
$this->cache->set($url, $item);
$this->assertTrue(is_object($this->cache->get($url)));
@@ -71,7 +71,7 @@ public function test_overwrite()
$item->body = new stdClass();
$item->body->foo = "bar";
$item->headers = new stdClass();
- $item->headers->Etag = "\"asdfasfsadfsadf\"";
+ $item->headers->etag = "\"asdfasfsadfsadf\"";
$this->cache->set($url, $item);
@@ -97,7 +97,7 @@ public function test_clear()
$item->body = new stdClass();
$item->body->foo = "bar";
$item->headers = new stdClass();
- $item->headers->Etag = "\"asdfasfsadfsadf\"";
+ $item->headers->etag = "\"asdfasfsadfsadf\"";
$this->cache->set('/123', $item);
@@ -121,7 +121,7 @@ public function test_cacheBadBody()
{
$item = new stdClass();
$item->headers = new stdClass();
- $item->headers->Etag = "\"asdfasfsadfsadf\"";
+ $item->headers->etag = "\"asdfasfsadfsadf\"";
//try without a body
$this->assertFalse($this->cache->set('/hi', $item));
View
1 tests/SagTest.php
@@ -534,6 +534,7 @@ public function test_getFromCache()
$cFileName = $cache->makeFilename("/{$this->couch->currentDatabase()}/$id");
$this->assertFalse(is_file($cFileName));
+ //get the file, putting it in the cache
$fromDB = $this->couch->get("/$id");
$this->assertEquals($fromDB->headers->_HTTP->status, 200);

0 comments on commit 6c02c28

Please sign in to comment.