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

Refactor Header Parsing #70

Merged
merged 5 commits into from
May 2, 2019
Merged

Conversation

l0gicgate
Copy link
Member

@l0gicgate l0gicgate commented May 1, 2019

Original Issue: slimphp/Slim-Http#61

Closes #11

The following components have been removed:

  • CollectionInterface & Collection
  • CookiesInterface

The following methods have been deprecated:

  • Headers::get() - Replaced by Headers::getHeader()
  • Headers::all() - Replaced by Headers::getHeaders()
  • Headers::set() - Replaced by Headers::setHeader()

Headers Provisioning
It's important to note that the original headers that will hydrate the Headers object are obtained via the getallheaders() function's which is dependent on the underlying infrastructure of your project. See https://www.php.net/manual/en/function.getallheaders.php. If you want a consistent behavior you should override the original function within the Slim\Psr7 namespace. You can use any existing implementation you'd like e.g.: https://github.com/ralouphie/getallheaders

The following behavior has changed:
The method Request::getHeaders() used to return a reconstructed version of the original headers based on assumptions that were unsafe and did not preserve the original case. As the spec says:

1.2 HTTP Headers
Case-insensitive header field names
HTTP messages include case-insensitive header field names. Headers are retrieved
by name from classes implementing the MessageInterface in a case-insensitive
manner. For example, retrieving the foo header will return the same result as
retrieving the FoO header. Similarly, setting the Foo header will overwrite
any previously set foo header value.

Despite that headers may be retrieved case-insensitively, the original case
MUST be preserved by the implementation, in particular when retrieved with
getHeaders().

Non-conforming HTTP applications may depend on a certain case, so it is useful
for a user to be able to dictate the case of the HTTP headers when creating a
request or response.

All the following methods are case-insensitive:

  • Request::getHeader()
  • Request::getHeaderLine()
  • Request::setHeader()
  • Request::withHeader()
  • Request::hasHeader()
  • Request::withAddedHeader();

Request::getHeaders() behavior:
The method now returns the hyphenated original case as per RFC2616 without the HTTP_ prefix.

// Example $_SERVER
$_SERVER = [
    'HTTP_ACCEPT' => 'application/json',
    'HTTP_CONTENT_TYPE' => 'application/json',
];

$headers = $request->getHeaders();

// The return value of $headers
[
    'ACCEPT' => 'application/json',
    'CONTENT-TYPE' => 'application/json',
]

It's worth to mention that typically the getallheaders() function will provision headers in the following fashion as seen in ralouphie's implementation:

$_SERVER = [
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
];

@l0gicgate l0gicgate added this to the 0.1 milestone May 1, 2019
@coveralls
Copy link

coveralls commented May 1, 2019

Coverage Status

Coverage decreased (-0.6%) to 95.652% when pulling 964107c on l0gicgate:RefactorHeaders into 8036a9c on slimphp:master.

@l0gicgate l0gicgate merged commit 3f9b6ae into slimphp:master May 2, 2019
@l0gicgate l0gicgate deleted the RefactorHeaders branch May 3, 2019 01:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ensure headers are consistent
2 participants