Skip to content

Commit c564383

Browse files
committed
fix sign v2 bug.
1 parent fb08e35 commit c564383

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/main/java/com/aliyun/oss/internal/SignV2Utils.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,23 @@ private static String buildCanonicalizedResource(String resourcePath, Map<String
237237
builder.append(uriEncoding(resourcePath));
238238

239239
if (parameters != null) {
240-
String[] parameterNames = parameters.keySet().toArray(new String[parameters.size()]);
241-
Arrays.sort(parameterNames);
240+
TreeMap<String, String> canonicalizedParams = new TreeMap<String, String>();
241+
for (Map.Entry<String, String> param : parameters.entrySet()) {
242+
if (param.getValue() != null ) {
243+
canonicalizedParams.put(uriEncoding(param.getKey()), uriEncoding(param.getValue()));
244+
}
245+
else {
246+
canonicalizedParams.put(uriEncoding(param.getKey()), null);
247+
}
248+
}
242249

243250
char separator = '?';
244-
for (String paramName : parameterNames) {
251+
for (Map.Entry<String, String> entry : canonicalizedParams.entrySet()) {
245252
builder.append(separator);
246-
builder.append(uriEncoding(paramName));
247-
String paramValue = parameters.get(paramName);
248-
if (paramValue != null && !paramValue.isEmpty()) {
249-
builder.append("=").append(uriEncoding(paramValue));
253+
builder.append(entry.getKey());
254+
if (entry.getValue() != null && !entry.getValue().isEmpty()) {
255+
builder.append("=").append(entry.getValue());
250256
}
251-
252257
separator = '&';
253258
}
254259
}

src/test/java/com/aliyun/oss/integrationtests/SignTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public void testSignV2() {
5151
request.addAdditionalHeaderName("x-oss-head1");
5252
request.addAdditionalHeaderName("abc");
5353
request.addParameter("param1", "value1");
54+
request.addParameter("|param1", "value2");
55+
request.addParameter("+param1", "value3");
56+
request.addParameter("|param1", "value4");
57+
request.addParameter("+param2", "");
58+
request.addParameter("|param2", null);
59+
request.addParameter("param2", "");
5460

5561
ossClient.putObject(request);
5662
} catch (Exception e) {
@@ -77,7 +83,7 @@ public void testGenerateSignedV2URL() {
7783
String filePath;
7884

7985
try {
80-
filePath = genFixedLengthFile(1 * 1024 * 1024); //1MB
86+
filePath = genFixedLengthFile(100); //1MB
8187
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, new File(filePath));
8288

8389
ossClient.putObject(putObjectRequest);
@@ -114,7 +120,7 @@ public void testSwitchSignatureVersion() {
114120
String filePath;
115121

116122
try {
117-
filePath = genFixedLengthFile(1 * 1024 * 1024); //1MB
123+
filePath = genFixedLengthFile(100);
118124

119125
ossClient.putObject(bucket, key, new File(filePath));
120126

0 commit comments

Comments
 (0)