Skip to content
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

Store HTTP response headers in case-insensitive array #531

Merged
merged 1 commit into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

// Utilities
require(dirname(__FILE__) . '/lib/Util/AutoPagingIterator.php');
require(dirname(__FILE__) . '/lib/Util/CaseInsensitiveArray.php');
require(dirname(__FILE__) . '/lib/Util/LoggerInterface.php');
require(dirname(__FILE__) . '/lib/Util/DefaultLogger.php');
require(dirname(__FILE__) . '/lib/Util/RandomGenerator.php');
Expand Down
2 changes: 1 addition & 1 deletion lib/HttpClient/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function request($method, $absUrl, $headers, $params, $hasFile)
}

// Create a callback to capture HTTP headers for the response
$rheaders = [];
$rheaders = new Util\CaseInsensitiveArray();
$headerCallback = function ($curl, $header_line) use (&$rheaders) {
// Ignore the HTTP request line (HTTP/1.1 200 OK)
if (strpos($header_line, ":") === false) {
Expand Down
62 changes: 62 additions & 0 deletions lib/Util/CaseInsensitiveArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Stripe\Util;

use ArrayAccess;

/**
* CaseInsensitiveArray is an array-like class that ignores case for keys.
*
* It is used to store HTTP headers. Per RFC 2616, section 4.2:
* Each header field consists of a name followed by a colon (":") and the field value. Field names
* are case-insensitive.
*
* In the context of stripe-php, this is useful because the API will return headers with different
* case depending on whether HTTP/2 is used or not (with HTTP/2, headers are always in lowercase).
*/
class CaseInsensitiveArray implements ArrayAccess
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we could put a comment block in here that mentions that this exists to provide case insensitive access to response headers because their case may vary depending on whether the request came back over HTTP or HTTP/2 (which mandates lowercase)? I think it may be helpful in reasoning about why it's here in the future.

{
private $container = array();

public function __construct($initial_array = array())
{
$this->container = array_map("strtolower", $initial_array);
}

public function offsetSet($offset, $value)
{
$offset = static::maybeLowercase($offset);
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
}
}

public function offsetExists($offset)
{
$offset = static::maybeLowercase($offset);
return isset($this->container[$offset]);
}

public function offsetUnset($offset)
{
$offset = static::maybeLowercase($offset);
unset($this->container[$offset]);
}

public function offsetGet($offset)
{
$offset = static::maybeLowercase($offset);
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}

private static function maybeLowercase($v)
{
if (is_string($v)) {
return strtolower($v);
} else {
return $v;
}
}
}
9 changes: 9 additions & 0 deletions tests/Stripe/HttpClient/CurlClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,13 @@ public function testSleepTimeShouldAddSomeRandomness()
$this->assertEquals($baseValue * 4, $this->sleepTimeMethod->invoke($curlClient, 3));
$this->assertEquals($baseValue * 8, $this->sleepTimeMethod->invoke($curlClient, 4));
}

public function testResponseHeadersCaseInsensitive()
{
$charge = Charge::all();

$headers = $charge->getLastResponse()->headers;
$this->assertNotNull($headers['request-id']);
$this->assertEquals($headers['request-id'], $headers['Request-Id']);
}
}