Skip to content
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public static List<Pair<String, List<String>>> getCanonicalHeaders(Map<String, L
* Each header-value pair is separated by a newline.
*/
public static String getCanonicalHeadersString(List<Pair<String, List<String>>> canonicalHeaders) {
StringBuilder result = new StringBuilder(512);
StringBuilder result = new StringBuilder(2048);

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.

Can we add a note about why we chose this number?

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.

Done

canonicalHeaders.forEach(header -> {
result.append(header.left());
result.append(":");
Expand Down Expand Up @@ -246,35 +246,42 @@ private static String getCanonicalRequestString(String httpMethod, String canoni
* Matcher object as well.
*/
private static void addAndTrim(StringBuilder result, String value) {
int lengthBefore = result.length();
boolean isStart = true;
boolean previousIsWhiteSpace = false;

for (int i = 0; i < value.length(); i++) {
char ch = value.charAt(i);
if (isWhiteSpace(ch)) {
if (previousIsWhiteSpace || isStart) {
continue;
}
result.append(' ');
previousIsWhiteSpace = true;
} else {
result.append(ch);
isStart = false;
previousIsWhiteSpace = false;
int start = 0;
int valueLength = value.length();

// Find first non-whitespace
while (isWhiteSpace(value.charAt(start))) {
++start;
if (start > valueLength) {
return;
}
}

if (lengthBefore == result.length()) {
return;
// Add things word-by-word
int lastWordStart = start;
boolean lastWasWhitespace = false;
for (int i = start; i < valueLength; i++) {
char c = value.charAt(i);

if (isWhiteSpace(c)) {
if (!lastWasWhitespace) {
// End of word, add word
result.append(value, lastWordStart, i);
lastWasWhitespace = true;
}
} else {
if (lastWasWhitespace) {
// Start of new word, add space
result.append(' ');
lastWordStart = i;
lastWasWhitespace = false;
}
}
}

int lastNonWhitespaceChar = result.length() - 1;
while (isWhiteSpace(result.charAt(lastNonWhitespaceChar))) {
--lastNonWhitespaceChar;
if (!lastWasWhitespace) {
result.append(value, lastWordStart, valueLength);
}

result.setLength(lastNonWhitespaceChar + 1);
}

/**
Expand Down Expand Up @@ -365,7 +372,17 @@ private static String getCanonicalQueryString(SortedMap<String, List<String>> ca
}

private static boolean isWhiteSpace(char ch) {
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\u000b' || ch == '\r' || ch == '\f';
switch (ch) {
case ' ':
case '\t':
case '\n':
case '\u000b':
case '\r':
case '\f':
return true;
default:
return false;
}
}

/**
Expand Down