Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Fix #14: Decoding fails when two encoded characters appear one after another#21

Open
tholu wants to merge 7 commits into
js-cookie:masterfrom
tholu:fix/issue-14-decoding-two-encoded-chars
Open

Fix #14: Decoding fails when two encoded characters appear one after another#21
tholu wants to merge 7 commits into
js-cookie:masterfrom
tholu:fix/issue-14-decoding-two-encoded-chars

Conversation

@tholu

@tholu tholu commented Feb 28, 2019

Copy link
Copy Markdown
Contributor

@FagnerMartinsBrack I accidentally did some small refactoring (simplifications) together with the fix, the actual fix for is in Cookies:316-323.

Please review and merge, thanks!

Resolves #14.

@FagnerMartinsBrack

Copy link
Copy Markdown
Member

The integration test with js-cookie fails when I checkout 9e305c9:

screen shot 2019-03-01 at 9 02 17 am

Are you getting the same error?

@tholu

tholu commented Mar 1, 2019

Copy link
Copy Markdown
Contributor Author

@FagnerMartinsBrack Thanks for checking that, I can reproduce it and need to look into it.

@JevinMenezes

Copy link
Copy Markdown
Contributor

It's been a while here for this PR, wish it could be closed any time soon.

@FagnerMartinsBrack

FagnerMartinsBrack commented Mar 10, 2021

Copy link
Copy Markdown
Member

@JevinMenezes If the OP is not continuing to work on this for more than a year then I believe it can be closed.

@tholu

tholu commented Mar 10, 2021

Copy link
Copy Markdown
Contributor Author

@JevinMenezes @FagnerMartinsBrack Thanks for the reminder! I'll try to finish it soon.

Comment on lines +30 to +36
@Test
public void character_with_2_bytes() {
Mockito.when( request.getHeader( "cookie" ) ).thenReturn( "c=%C3%A3" );
String actual = cookies.get( "c" );
String expected = "ã";
Assert.assertEquals( expected, actual );
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test case is failing.., (its also failing in Travis CI builds - https://travis-ci.org/github/js-cookie/java-cookie/builds/764105675)
java-cookie pr reivew faing junit

@tholu check this failing test case

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@JevinMenezes Yes, I explicitly added the test case here: 88b8327

Thanks for the reminder! Did not have time yet to fix the issue, will try to get this done over the weekend.

@tholu

tholu commented Oct 26, 2021

Copy link
Copy Markdown
Contributor Author

@JevinMenezes I fixed the test, but Travis is not running anymore?

@JevinMenezes

JevinMenezes commented Oct 30, 2021

Copy link
Copy Markdown
Contributor

@FagnerMartinsBrack Travis builds for this repository have stopped in travis-ci.org and this repo needs to be migrated to travis-ci.com. Can we migrate java-cookie repo to travis-ci.com ?

@FagnerMartinsBrack

Copy link
Copy Markdown
Member

@JevinMenezes yes what do you want me to do?

@JevinMenezes

JevinMenezes commented Oct 31, 2021

Copy link
Copy Markdown
Contributor

Just wanted to ask whether I can approve & request this for organization js-cookie,
Capture
@FagnerMartinsBrack
(I believed organization owner could approve this)

@FagnerMartinsBrack

FagnerMartinsBrack commented Oct 31, 2021 via email

Copy link
Copy Markdown
Member

@FagnerMartinsBrack

Copy link
Copy Markdown
Member

@JevinMenezes I received the email and just approved it, let me know if you need anything else

@JevinMenezes

JevinMenezes commented Oct 31, 2021

Copy link
Copy Markdown
Contributor

@FagnerMartinsBrack thanks, seems like an admin privilege user can migrate the java-cookie repo and I'm unable to select the repo for migrating to travis-ci.com, would you be able to do it, thanks again ,
image

@FagnerMartinsBrack

Copy link
Copy Markdown
Member

Done @JevinMenezes

@tholu

tholu commented Nov 9, 2021

Copy link
Copy Markdown
Contributor Author

@JevinMenezes Can this be merged now? I'll have to fix conflicts in #23 afterwards.

Comment on lines -328 to -347
// Decode characters with 3 bytes first, then with 1 byte to fix https://github.com/js-cookie/java-cookie/issues/14
return decode( decode( encoded, 3 ), 1 );
}

private String decode( String encoded, Integer bytesPerCharacter ) {
// Use URLDecoder to fix https://github.com/js-cookie/java-cookie/issues/14
String decoded = encoded;
Pattern pattern = Pattern.compile( "(%[0-9A-Z]{2}){" + bytesPerCharacter + "}" );
Matcher matcher = pattern.matcher( encoded );
while ( matcher.find() ) {
String encodedChar = matcher.group();
String[] encodedBytes = encodedChar.split( "%" );
byte[] bytes = new byte[ encodedBytes.length - 1 ];
for ( int i = 1; i < encodedBytes.length; i++ ) {
String encodedByte = encodedBytes[ i ];
bytes[ i - 1 ] = ( byte )Integer.parseInt( encodedByte, 16 );
}
try {
String decodedChar = new String( bytes, UTF_8 );
decoded = decoded.replace( encodedChar, decodedChar );
} catch ( UnsupportedEncodingException e ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@tholu I see that decode implementation with 3 bytes first and then with 1 byte is for fixing #14 , further the issue also mentions #14 (comment) . Will this change in implementation solve #14 (comment) ?

@JevinMenezes JevinMenezes Nov 9, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@tholu seems like my review wasn't submitted a few days ago, apologies for the same. Could you please update on this #21 (comment), I can update the PR post your reply, thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Decoding sometimes fails when two encoded characters appear one after another.

3 participants