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
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.10</version>
<version>2.10.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.2</version>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
40 changes: 11 additions & 29 deletions src/main/java/com/github/jscookie/javacookie/Cookies.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package com.github.jscookie.javacookie;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public final class Cookies implements CookiesDefinition {
private static String UTF_8 = "UTF-8";
Expand Down Expand Up @@ -325,28 +323,12 @@ private String encode( String decoded, Set<Integer> exceptions ) {
}

private String decode(String encoded) {
// 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
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) ?

Copy link
Contributor

@JevinMenezes JevinMenezes Nov 9, 2021

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.

try {
decoded = URLDecoder.decode(encoded, UTF_8);
} catch ( UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return decoded;
}
Expand Down