Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[HttpFoundation] Add HeaderUtils class#24699

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:masterfromc960657:header-utils
Apr 22, 2018

Conversation

@c960657
Copy link
Contributor

@c960657c960657 commentedOct 26, 2017
edited by fabpot
Loading

QA
Branch?master
Bug fix?yes
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets
LicenseMIT
Doc PR

In several places in HttpFoundation we parse HTTP header values using a variety of regular expressions. Some of them fail in various corner cases.

Parsing HTTP headers is not entirely trivial. We must be able to parse quoted strings with backslash escaping properly and ignore white-space in certain places.

In practice, our limitations in this respect may not be a big problem. We only care about a few different HTTP request headers, and they are usually restricted to a simple values without quoted strings etc. However, this is no excuse for not doing it right :-)

This PR introduces a new utility class for parsing headers. This allows Symfony itself and third-party code to parse HTTP headers in a robust way without using complex regular expressions that are difficult to write and error prone.

ro0NL, ostrolucky, Koc, and alamirault reacted with thumbs up emoji
@c960657c960657force-pushed theheader-utils branch 5 times, most recently fromeee2a20 to9b08a46CompareOctober 26, 2017 20:04
@nicolas-grekas
Copy link
Member

This is the kind of change that should be really fine, but is risky until proven right. I'm not sure this can be merged in 3.4 as this is a new feature, even if it fixes edge cases.
About these edge-cases, are they solvable, testable, important to fix now?

@nicolas-grekasnicolas-grekas added this to the4.1 milestoneOct 28, 2017
@c960657c960657force-pushed theheader-utils branch 2 times, most recently from7b3d34c to3674a7dCompareOctober 28, 2017 22:47
@c960657
Copy link
ContributorAuthor

At least some of the edge cases are easy to fix, e.g. those dealing with missing case-insensitivity.

I don't think these problems are particularly important to fix in 3.x. They hardly occur in practice. So I wouldn't mind postponing this to 4.x.

@xabbuhxabbuh changed the base branch from3.4 tomasterNovember 15, 2017 17:23
@c960657
Copy link
ContributorAuthor

Rebased to master.

*
* @author Christian Schmidt <github@chsc.dk>
*/
class HeaderUtils
Copy link
Contributor

Choose a reason for hiding this comment

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

Shall this class be marked@internal?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I don't think so. It is generally useful when parsing HTTP headers, including those not supported directly by HttpFoundation. It may even be used in other contexts than parsing incoming HTTP request headers.

ro0NL reacted with thumbs up emoji
@c960657
Copy link
ContributorAuthor

Rebased to master.

* added`CannotWriteFileException`,`ExtensionFileException`,`FormSizeFileException`,
`IniSizeFileException`,`NoFileException`,`NoTmpDirFileException`,`PartialFileException` to
handle failed`UploadedFile`.
* added`HeaderUtility`.
Copy link
Contributor

Choose a reason for hiding this comment

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

HeaderUtils :)

$assoc =array();
foreach ($partsas$part) {
$name =strtolower($part[0]);
$value =isset($part[1]) ?$part[1] :true;
Copy link
Contributor

@ro0NLro0NLApr 19, 2018
edited
Loading

Choose a reason for hiding this comment

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

$part[1] ?? true

* @return array Nested array with as many levels as there are characters in
* $separators
*/
publicstaticfunctionsplit($header,$separators)
Copy link
Contributor

Choose a reason for hiding this comment

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

in general type declarations / return types are missing


publicfunctiontestQuote()
{
$this->assertEquals('foo', HeaderUtils::quote('foo'));
Copy link
Contributor

@ro0NLro0NLApr 19, 2018
edited
Loading

Choose a reason for hiding this comment

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

alsoassertSame as of here..

@c960657c960657 changed the title[HttpFoundation] Add HeaderUtility class[HttpFoundation] Add HeaderUtils classApr 20, 2018
@c960657c960657force-pushed theheader-utils branch 2 times, most recently from2bb54da to1ef098aCompareApril 20, 2018 05:28
@c960657
Copy link
ContributorAuthor

@ro0NL I have addresses your comments. Thanks!

*
* @param array $parts Array of arrays
*
* @return array Associative array

Choose a reason for hiding this comment

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

I think many docblocks here could be removed. We prefer not adding them when they are of low added value.
While doing so, could you please squash your commits also?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I assume you mean the individual@param and@return lines, not the entire docblock, right?

Copy link
Member

Choose a reason for hiding this comment

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

That's correct.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Sure I can squash, but doesn't Github offer to do this automatically? I'm just trying to understand why it is relevant :-)

Choose a reason for hiding this comment

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

we don't use github to merge
and the squashing feature of the tool we're using is broken right now :)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Ok, cool :-) I have removed the unnecessary docblock lines and squashed my commits (it took a few attempts to get it right).

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

(with minor comment)

returnself::groupParts($matches,$separators);
}

privatestaticfunctiongroupParts(array$matches,string$separators):array
Copy link
Member

Choose a reason for hiding this comment

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

private methods should be afterpublic ones

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Fixed.

@c960657c960657force-pushed theheader-utils branch 2 times, most recently fromc92e14b to96f7606CompareApril 20, 2018 14:29
@fabpot
Copy link
Member

Thank you@c960657.

@fabpotfabpot merged commitb435e80 intosymfony:masterApr 22, 2018
fabpot added a commit that referenced this pull requestApr 22, 2018
This PR was merged into the 4.1-dev branch.Discussion----------[HttpFoundation] Add HeaderUtils class| Q             | A| ------------- | ---| Branch?       | 3.4| Bug fix?      | yes| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets || License       | MIT| Doc PR        |In several places in HttpFoundation we parse HTTP header values using a variety of regular expressions. Some of them fail in various corner cases.Parsing HTTP headers is not entirely trivial. We must be able to parse quoted strings with backslash escaping properly and ignore white-space in certain places.In practice, our limitations in this respect may not be a big problem. We only care about a few different HTTP request headers, and they are usually restricted to a simple values without quoted strings etc. However, this is no excuse for not doing it right :-)This PR introduces a new utility class for parsing headers. This allows Symfony itself and third-party code to parse HTTP headers in a robust way without using complex regular expressions that are difficult to write and error prone.Commits-------b435e80 [HttpFoundation] Add HeaderUtility class
*/
publicstaticfunctionsplit(string$header,string$separators):array
{
$quotedSeparators =preg_quote($separators);
Copy link
Member

Choose a reason for hiding this comment

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

this should include the regex delimiter (/ as second argument of the call, in case it is one of the separators

* Combines an array of arrays into one associative array.
*
* Each of the nested arrays should have one or two elements. The first
* value will be used as the keys in the associative array, and the second
Copy link
Member

Choose a reason for hiding this comment

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

that's not what is done. The key is the associative array is thelowercased first value, not the first value.

* Example:
*
* HeaderUtils::split("da, en-gb;q=0.8", ",;")
* // => array(array("da"), array("en-gb"), array("q", "0.8"))
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure about this example ? Why is the header split on= when giving,; as separator ?

@c960657c960657 deleted the header-utils branchApril 23, 2018 14:51
@c960657
Copy link
ContributorAuthor

c960657 commentedApr 23, 2018
edited
Loading

@stof You are right x 3 :-) I have created a follow-up PR:#27019

fabpot added a commit that referenced this pull requestApr 27, 2018
This PR was squashed before being merged into the 4.1-dev branch (closes#27019).Discussion----------[HttpFoundation] Fixes to new HeaderUtils class| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | yes| New feature?  | no| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets || License       | MIT| Doc PR        |A follow-up to#24699 with a few code and documentation fixes for post-merge review comments by@stof.Commits-------d7c3c79 [HttpFoundation] Fixes to new HeaderUtils class
@fabpotfabpot mentioned this pull requestMay 7, 2018
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@stofstofstof left review comments

@xabbuhxabbuhxabbuh left review comments

+2 more reviewers

@hhamonhhamonhhamon left review comments

@ro0NLro0NLro0NL left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.1

Development

Successfully merging this pull request may close these issues.

9 participants

@c960657@nicolas-grekas@fabpot@hhamon@stof@ro0NL@xabbuh@javiereguiluz@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp