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

Fix URL encoding and decoding #160

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
85 changes: 58 additions & 27 deletions src/main/java/com/github/packageurl/PackageURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package com.github.packageurl;

import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -441,24 +442,49 @@ private String percentEncode(final String input) {
}

private static String uriEncode(String source, Charset charset) {
if (source == null || source.length() == 0) {
if (source == null || source.isEmpty()) {
return source;
}

StringBuilder builder = new StringBuilder();
for (byte b : source.getBytes(charset)) {
byte[] bytes = source.getBytes(charset);
int length = bytes.length;
int pos = indexOfFirstUnreservedChar(bytes);

if (pos == -1) {
return source;
}

StringBuilder builder = new StringBuilder(source.substring(0, pos));

for (int i = pos; i < length; i++) {
byte b = bytes[i];

if (isUnreserved(b)) {
builder.append((char) b);
}
else {
// Substitution: A '%' followed by the hexadecimal representation of the ASCII value of the replaced character
} else {
builder.append('%');
builder.append(Integer.toHexString(b).toUpperCase());
builder.append(Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16)));
builder.append(Character.toUpperCase(Character.forDigit(b & 0xF, 16)));
}
}

return builder.toString();
}

private static int indexOfFirstUnreservedChar(final byte[] bytes) {
final int length = bytes.length;
int pos = -1;

for (int i = 0; i < length; i++) {
if (!isUnreserved(bytes[i])) {
pos = i;
break;
}
}

return pos;
}

private static boolean isUnreserved(int c) {
return (isAlpha(c) || isDigit(c) || '-' == c || '.' == c || '_' == c || '~' == c);
}
Expand All @@ -479,34 +505,39 @@ private static boolean isDigit(int c) {
* @return a decoded String
*/
private String percentDecode(final String input) {
if (input == null) {
return null;
}
final String decoded = uriDecode(input);
if (!decoded.equals(input)) {
return decoded;
}
return input;
return uriDecode(input);
}

public static String uriDecode(String source) {
if (source == null) {
if (source == null || source.isEmpty()) {
return source;
}
int length = source.length();
StringBuilder builder = new StringBuilder();

if (source.indexOf('%') == -1) {
return source;
}

byte[] bytes = source.getBytes(StandardCharsets.UTF_8);
int length = bytes.length;
ByteArrayOutputStream buffer = new ByteArrayOutputStream(length);

for (int i = 0; i < length; i++) {
if (source.charAt(i) == '%') {
String str = source.substring(i + 1, i + 3);
char c = (char) Integer.parseInt(str, 16);
builder.append(c);
i += 2;
}
else {
builder.append(source.charAt(i));
int b = bytes[i];

if (b == '%') {
if (i + 2 >= length) {
return null;
}

int b1 = Character.digit(bytes[++i], 16);
int b2 = Character.digit(bytes[++i], 16);
buffer.write((char) ((b1 << 4) + b2));
} else {
buffer.write(b);
}
}
return builder.toString();

return new String(buffer.toByteArray(), StandardCharsets.UTF_8);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/github/packageurl/PackageURLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ public static void setup() throws IOException {
json = new JSONArray(jsonTxt);
}

@Test
public void testEncoding1() throws MalformedPackageURLException {
PackageURL purl = new PackageURL("maven", "com.google.summit", "summit-ast", "2.2.0\n", null, null);
Assert.assertEquals("pkg:maven/com.google.summit/[email protected]%0A", purl.toString());
}

@Test
public void testEncoding2() throws MalformedPackageURLException {
PackageURL purl = new PackageURL("pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5");
Assert.assertEquals("Мicrosоft.ЕntitуFramеworkСоrе", purl.getName());
Assert.assertEquals("pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5", purl.toString());
}

@Test
public void testConstructorParsing() throws Exception {
exception = ExpectedException.none();
Expand Down