2929final class RequestBuilder {
3030 private static final char [] HEX_DIGITS =
3131 { '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 = " \" <>^`{}|\\ ?#" ;
3333
3434 private final String method ;
3535
@@ -85,20 +85,20 @@ void addPathParam(String name, String value, boolean encoded) {
8585 // The relative URL is cleared when the first query parameter is set.
8686 throw new AssertionError ();
8787 }
88- relativeUrl = relativeUrl .replace ("{" + name + "}" , canonicalize (value , encoded ));
88+ relativeUrl = relativeUrl .replace ("{" + name + "}" , canonicalizeForPath (value , encoded ));
8989 }
9090
91- private static String canonicalize (String input , boolean alreadyEncoded ) {
91+ private static String canonicalizeForPath (String input , boolean alreadyEncoded ) {
9292 int codePoint ;
9393 for (int i = 0 , limit = input .length (); i < limit ; i += Character .charCount (codePoint )) {
9494 codePoint = input .codePointAt (i );
9595 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 == '%' ) )) {
9898 // Slow path: the character at i requires encoding!
9999 Buffer out = new Buffer ();
100100 out .writeUtf8 (input , 0 , i );
101- canonicalize (out , input , i , limit , alreadyEncoded );
101+ canonicalizeForPath (out , input , i , limit , alreadyEncoded );
102102 return out .readUtf8 ();
103103 }
104104 }
@@ -107,7 +107,7 @@ private static String canonicalize(String input, boolean alreadyEncoded) {
107107 return input ;
108108 }
109109
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 ,
111111 boolean alreadyEncoded ) {
112112 Buffer utf8Buffer = null ; // Lazily allocated.
113113 int codePoint ;
@@ -116,10 +116,9 @@ private static void canonicalize(Buffer out, String input, int pos, int limit,
116116 if (alreadyEncoded
117117 && (codePoint == '\t' || codePoint == '\n' || codePoint == '\f' || codePoint == '\r' )) {
118118 // 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 == '%' ))) {
123122 // Percent encode this character.
124123 if (utf8Buffer == null ) {
125124 utf8Buffer = new Buffer ();
0 commit comments