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

Trim optional white space in CombinedHttpHeaders values #6502

Closed
wants to merge 1 commit into from

Conversation

ddossot
Copy link
Contributor

@ddossot ddossot commented Mar 7, 2017

Motivation:

The updated HTTP/1.x RFC allows for header values to be CSV and separated by OWS [1]. CombinedHttpHeaders should remove this OWS on insertion.

[1] https://tools.ietf.org/html/rfc7230#section-7

Modification:

CombinedHttpHeaders doesn't account for the OWS and returns it back to the user as part of the value.

Result:

Fixes #6452

}

StringBuilder result = new StringBuilder(length + CSV_NUMBER_ESCAPE_CHARACTERS);
boolean quoted = isDoubleQuote(value.charAt(start)) && isDoubleQuote(value.charAt(last)) && length != 1;
Copy link
Contributor

@fenik17 fenik17 Mar 7, 2017

Choose a reason for hiding this comment

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

better make length != 1 as first condition bcz it has lower cost.

UPD: Although may be not. One-char params is not very likely..
sorry for noise.

Copy link
Contributor

Choose a reason for hiding this comment

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

If quoted==true, can we increment start and decrement last to exclude check i == start || i == last from the loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I honestly haven't found a way to make the code better while doing the suggested increment/decrement. There's more involved than just excluding the i == start || i == last from the loop, and I have tried to not alter too much the existing code in this PR.

@fenik17 please send a follow-up PR to implement the change you envisioned.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. I'll do it soon. Thanks for an effort!

Copy link
Contributor

Choose a reason for hiding this comment

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

@ddossot done in #6840

return start == 0 && end == length - 1 ? value : value.subSequence(start, end + 1);
}

private static int indexOfFirstNonOwsChar(CharSequence value, int length) {
Copy link
Contributor

@fenik17 fenik17 Mar 7, 2017

Choose a reason for hiding this comment

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

IMHO, make sense add comment that if the value don't contain non-OWS chars, it returns length, but not -1.

Copy link
Member

Choose a reason for hiding this comment

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

+1

@@ -34,6 +34,7 @@
public static final char LINE_FEED = '\n';
public static final char CARRIAGE_RETURN = '\r';
public static final char TAB = '\t';
public static final char SPACE = ' ';
Copy link
Contributor

Choose a reason for hiding this comment

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

' ' -> 0x20?

Copy link
Member

Choose a reason for hiding this comment

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

I think ' ' is easier to read...

Copy link
Contributor

Choose a reason for hiding this comment

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

I just wanted to emphasize specific unicode value.
Not everything can be distinguished in appearance )
Spaces may be different: https://www.cs.tut.fi/~jkorpela/chars/spaces.html

Copy link
Member

@Scottmitch Scottmitch left a comment

Choose a reason for hiding this comment

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

lgtm once comments addressed

@Scottmitch Scottmitch self-assigned this Mar 7, 2017
@ddossot
Copy link
Contributor Author

ddossot commented Mar 11, 2017

@normanmaurer @Scottmitch @fenik17 Sorry for the delay in addressing the comments. I've done a few basic changes and invited @fenik17 to submit a follow-up with the changes he has in mind.

* according to <a href="https://tools.ietf.org/html/rfc7230#section-7">RFC-7230</a>
* @return {@link CharSequence} the escaped value if necessary, or the value unchanged
*/
public static CharSequence escapeCsv(CharSequence value, boolean trimWhiteSpace) {
Copy link

Choose a reason for hiding this comment

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

MAJOR The Cyclomatic Complexity of this method "escapeCsv" is 28 which is greater than 25 authorized. rule

@ddossot ddossot force-pushed the 6452-trim-ows-in-header-values branch from e79442c to 5b62a2c Compare March 14, 2017 15:52
@normanmaurer
Copy link
Member

@ddossot sorry for the delay :( Just merged into 4.1 (9c1a191) . @ddossot would you mind port this over to 4.0 and open a new PR for it as well ?

@normanmaurer normanmaurer added this to the 4.1.10.Final milestone Mar 19, 2017
@ddossot
Copy link
Contributor Author

ddossot commented Mar 23, 2017

Just for closure here, after discussing with @normanmaurer in meatspace, we will not port to 4.0 as the CombinedHttpHeaders feature is lacking altogether there.

@normanmaurer
Copy link
Member

normanmaurer commented Mar 23, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants