Skip to content

[http-foudation] Better accept header parsing #5841

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

Closed
wants to merge 12 commits into from

Conversation

jfsimon
Copy link
Contributor

@jfsimon jfsimon commented Oct 26, 2012

Bug fix: no
Feature addition: yes
Backwards compatibility break: yes
Symfony2 tests pass: yes

Quality:
The special q item attribute represents its quality. I had to make some choices:

  • if I set q attribute, it's assigned to quality property, but not to attributes
  • the __toString() method only render q attribute if quality is less than 1

BC break:
The return of Request::splitHttpAcceptHeader() has changed. It's result was an array of qualities indexed by an accept value, it now returns an array of AcceptHeaderItem indexed by its value.

@jfsimon
Copy link
Contributor Author

jfsimon commented Oct 26, 2012

As dicussed in #5711.

public function __construct(array $items)
{
$this->sorted = true;
$this->items = array();
Copy link
Member

Choose a reason for hiding this comment

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

you should move these to the property declarations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay, done

@Seldaek
Copy link
Member

Seldaek commented Oct 27, 2012

Maybe you can pull Seldaek@5e8a526 into your branch (for some reason I can't send a PR to your repo, it doesn't show up in github's repo selector.. looks like they don't like projects with too many forks). It allows you to use usort() which hopefully is faster than your merge sort, though I did not bench it. I also added tests to confirm the functionality.

@Seldaek
Copy link
Member

Seldaek commented Oct 27, 2012

Sorry please check Seldaek@376dd93 instead, I missed a few tests in the RequestTest class.

@jfsimon
Copy link
Contributor Author

jfsimon commented Oct 29, 2012

@fabpot do you think the introduced BC break is acceptable?

@fabpot
Copy link
Member

fabpot commented Oct 29, 2012

@jfsimon Are all getAccept*() method BC?

@jfsimon
Copy link
Contributor Author

jfsimon commented Oct 29, 2012

@fabpot nope, just Request::splitHttpAcceptHeader()

@jfsimon
Copy link
Contributor Author

jfsimon commented Oct 29, 2012

@fabpot I think missunderstood... only Request::splitHttpAcceptHeader() breaks BC.

@fabpot
Copy link
Member

fabpot commented Oct 29, 2012

So, a BC break on just splitHttpAcceptHeader is possible... but should be documented properly. Another option would be to deprecate the current method (and keep it as is), and just use the new version everywhere. Sounds better as it won"t introduce any BC breaks.

@jfsimon
Copy link
Contributor Author

jfsimon commented Oct 29, 2012

@fabpot Okay, I'll update this PR according to your second option.

@jfsimon
Copy link
Contributor Author

jfsimon commented Oct 29, 2012

@fabpot done.

As you can see here: https://github.com/symfony/symfony/pull/5841/files#L5L1029 value returned by Request::splitHttpAcceptHeader() is not exactly the same as before because all attributes are present (not only those before the q one).

@@ -14,6 +14,8 @@
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\AcceptHeader;
use Symfony\Component\HttpFoundation\AcceptHeaderItem;
Copy link
Member

Choose a reason for hiding this comment

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

These 2 lines seems unneeded.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, removed

*/
public function first()
{
$this->ensureSorted();
Copy link
Member

Choose a reason for hiding this comment

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

I would just name it sort().

@fabpot
Copy link
Member

fabpot commented Oct 30, 2012

The last thing missing before I can merge is a PR to update the documentation (should probably be just a note somewhere with the example you have in the UPGRADE file).

@jfsimon
Copy link
Contributor Author

jfsimon commented Oct 30, 2012

@fabpot I could add this example here: http://symfony.com/doc/current/components/http_foundation/introduction.html#request after Accessing the session, what do you think?

@fabpot
Copy link
Member

fabpot commented Oct 30, 2012

Yes, looks good to me.

/**
* Constructor.
*
* @param array $items
Copy link
Contributor

Choose a reason for hiding this comment

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

should also be AcceptHeaderItem[]

return $qA > $qB ? -1 : 1;
});
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't it set $this->sorted = true; after sorting?!

Copy link
Member

Choose a reason for hiding this comment

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

Indeed it should :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

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.

5 participants