29
29
final class RequestBuilder {
30
30
private static final char [] HEX_DIGITS =
31
31
{ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' };
32
- private static final String PATH_SEGMENT_ENCODE_SET = " \" <>^`{}|/ \\ ?#" ;
32
+ private static final String PATH_SEGMENT_ALWAYS_ENCODE_SET = " \" <>^`{}|\\ ?#" ;
33
33
34
34
private final String method ;
35
35
@@ -85,20 +85,20 @@ void addPathParam(String name, String value, boolean encoded) {
85
85
// The relative URL is cleared when the first query parameter is set.
86
86
throw new AssertionError ();
87
87
}
88
- relativeUrl = relativeUrl .replace ("{" + name + "}" , canonicalize (value , encoded ));
88
+ relativeUrl = relativeUrl .replace ("{" + name + "}" , canonicalizeForPath (value , encoded ));
89
89
}
90
90
91
- private static String canonicalize (String input , boolean alreadyEncoded ) {
91
+ private static String canonicalizeForPath (String input , boolean alreadyEncoded ) {
92
92
int codePoint ;
93
93
for (int i = 0 , limit = input .length (); i < limit ; i += Character .charCount (codePoint )) {
94
94
codePoint = input .codePointAt (i );
95
95
if (codePoint < 0x20 || codePoint >= 0x7f
96
- || PATH_SEGMENT_ENCODE_SET .indexOf (codePoint ) != -1
97
- || (codePoint == '%' && ! alreadyEncoded )) {
96
+ || PATH_SEGMENT_ALWAYS_ENCODE_SET .indexOf (codePoint ) != -1
97
+ || (! alreadyEncoded && ( codePoint == '/' || codePoint == '%' ) )) {
98
98
// Slow path: the character at i requires encoding!
99
99
Buffer out = new Buffer ();
100
100
out .writeUtf8 (input , 0 , i );
101
- canonicalize (out , input , i , limit , alreadyEncoded );
101
+ canonicalizeForPath (out , input , i , limit , alreadyEncoded );
102
102
return out .readUtf8 ();
103
103
}
104
104
}
@@ -107,7 +107,7 @@ private static String canonicalize(String input, boolean alreadyEncoded) {
107
107
return input ;
108
108
}
109
109
110
- private static void canonicalize (Buffer out , String input , int pos , int limit ,
110
+ private static void canonicalizeForPath (Buffer out , String input , int pos , int limit ,
111
111
boolean alreadyEncoded ) {
112
112
Buffer utf8Buffer = null ; // Lazily allocated.
113
113
int codePoint ;
@@ -116,10 +116,9 @@ private static void canonicalize(Buffer out, String input, int pos, int limit,
116
116
if (alreadyEncoded
117
117
&& (codePoint == '\t' || codePoint == '\n' || codePoint == '\f' || codePoint == '\r' )) {
118
118
// Skip this character.
119
- } else if (codePoint < 0x20
120
- || codePoint >= 0x7f
121
- || PATH_SEGMENT_ENCODE_SET .indexOf (codePoint ) != -1
122
- || (codePoint == '%' && !alreadyEncoded )) {
119
+ } else if (codePoint < 0x20 || codePoint >= 0x7f
120
+ || PATH_SEGMENT_ALWAYS_ENCODE_SET .indexOf (codePoint ) != -1
121
+ || (!alreadyEncoded && (codePoint == '/' || codePoint == '%' ))) {
123
122
// Percent encode this character.
124
123
if (utf8Buffer == null ) {
125
124
utf8Buffer = new Buffer ();
0 commit comments