Skip to content

Commit c8a8363

Browse files
committed
update URIBuilder
1 parent 7f81988 commit c8a8363

File tree

1 file changed

+55
-52
lines changed

1 file changed

+55
-52
lines changed

library/src/com/lidroid/xutils/http/client/util/URIBuilder.java

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,26 @@
3131
public class URIBuilder {
3232

3333
private String scheme;
34+
private String encodedSchemeSpecificPart;
3435
private String encodedAuthority;
3536
private String userInfo;
3637
private String encodedUserInfo;
3738
private String host;
3839
private int port;
3940
private String path;
41+
private String encodedPath;
42+
private String encodedQuery;
4043
private List<NameValuePair> queryParams;
4144
private String fragment;
45+
private String encodedFragment;
4246

4347
public URIBuilder() {
4448
this.port = -1;
4549
}
4650

4751
public URIBuilder(final String uri) {
4852
try {
49-
_digestURI(new URI(uri.replace(" ", "%20")));
53+
digestURI(new URI(uri));
5054
} catch (URISyntaxException e) {
5155
LogUtils.e(e.getMessage(), e);
5256
}
@@ -56,30 +60,19 @@ public URIBuilder(final URI uri) {
5660
digestURI(uri);
5761
}
5862

59-
private void _digestURI(final URI uri) {
60-
this.scheme = uri.getScheme();
61-
this.encodedAuthority = uri.getRawAuthority();
62-
this.host = uri.getHost();
63-
this.port = uri.getPort();
64-
this.encodedUserInfo = uri.getRawUserInfo();
65-
this.userInfo = uri.getUserInfo();
66-
this.path = uri.getPath();
67-
if (path != null) path = path.replace("%20", " ");
68-
String query = uri.getRawQuery();
69-
if (query != null) query = query.replace("%20", " ");
70-
this.queryParams = parseQuery(query);
71-
this.fragment = uri.getFragment();
72-
}
73-
7463
private void digestURI(final URI uri) {
7564
this.scheme = uri.getScheme();
65+
this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
7666
this.encodedAuthority = uri.getRawAuthority();
7767
this.host = uri.getHost();
7868
this.port = uri.getPort();
7969
this.encodedUserInfo = uri.getRawUserInfo();
8070
this.userInfo = uri.getUserInfo();
71+
this.encodedPath = uri.getRawPath();
8172
this.path = uri.getPath();
73+
this.encodedQuery = uri.getRawQuery();
8274
this.queryParams = parseQuery(uri.getRawQuery());
75+
this.encodedFragment = uri.getRawFragment();
8376
this.fragment = uri.getFragment();
8477
}
8578

@@ -100,39 +93,45 @@ public URI build(Charset charset) throws URISyntaxException {
10093
}
10194

10295
private String buildString(Charset charset) {
103-
StringBuilder sb = new StringBuilder();
96+
final StringBuilder sb = new StringBuilder();
10497
if (this.scheme != null) {
10598
sb.append(this.scheme).append(':');
10699
}
107-
108-
if (this.encodedAuthority != null) {
109-
sb.append("//").append(this.encodedAuthority);
110-
} else if (this.host != null) {
111-
sb.append("//");
112-
if (this.encodedUserInfo != null) {
113-
sb.append(this.encodedUserInfo).append("@");
114-
} else if (this.userInfo != null) {
115-
sb.append(encodeUserInfo(this.userInfo, charset)).append("@");
100+
if (this.encodedSchemeSpecificPart != null) {
101+
sb.append(this.encodedSchemeSpecificPart);
102+
} else {
103+
if (this.encodedAuthority != null) {
104+
sb.append("//").append(this.encodedAuthority);
105+
} else if (this.host != null) {
106+
sb.append("//");
107+
if (this.encodedUserInfo != null) {
108+
sb.append(this.encodedUserInfo).append("@");
109+
} else if (this.userInfo != null) {
110+
sb.append(encodeUserInfo(this.userInfo, charset)).append("@");
111+
}
112+
if (InetAddressUtils.isIPv6Address(this.host)) {
113+
sb.append("[").append(this.host).append("]");
114+
} else {
115+
sb.append(this.host);
116+
}
117+
if (this.port >= 0) {
118+
sb.append(":").append(this.port);
119+
}
116120
}
117-
if (InetAddressUtils.isIPv6Address(this.host)) {
118-
sb.append("[").append(this.host).append("]");
119-
} else {
120-
sb.append(this.host);
121+
if (this.encodedPath != null) {
122+
sb.append(normalizePath(this.encodedPath));
123+
} else if (this.path != null) {
124+
sb.append(encodePath(normalizePath(this.path), charset));
121125
}
122-
if (this.port >= 0) {
123-
sb.append(":").append(this.port);
126+
if (this.encodedQuery != null) {
127+
sb.append("?").append(this.encodedQuery);
128+
} else if (this.queryParams != null) {
129+
sb.append("?").append(encodeQuery(this.queryParams, charset));
124130
}
125131
}
126-
127-
if (this.path != null) {
128-
sb.append(encodePath(normalizePath(this.path), charset));
129-
}
130-
131-
if (this.queryParams != null) {
132-
sb.append("?").append(encodeQuery(this.queryParams, charset));
133-
}
134-
135-
if (this.fragment != null) {
132+
if (this.encodedFragment != null) {
133+
sb.append("#").append(this.encodedFragment);
134+
} else if (this.fragment != null) {
136135
sb.append("#").append(encodeFragment(this.fragment, charset));
137136
}
138137
return sb.toString();
@@ -168,6 +167,7 @@ public URIBuilder setScheme(final String scheme) {
168167
*/
169168
public URIBuilder setUserInfo(final String userInfo) {
170169
this.userInfo = userInfo;
170+
this.encodedSchemeSpecificPart = null;
171171
this.encodedAuthority = null;
172172
this.encodedUserInfo = null;
173173
return this;
@@ -186,6 +186,7 @@ public URIBuilder setUserInfo(final String username, final String password) {
186186
*/
187187
public URIBuilder setHost(final String host) {
188188
this.host = host;
189+
this.encodedSchemeSpecificPart = null;
189190
this.encodedAuthority = null;
190191
return this;
191192
}
@@ -195,6 +196,7 @@ public URIBuilder setHost(final String host) {
195196
*/
196197
public URIBuilder setPort(final int port) {
197198
this.port = port < 0 ? -1 : port;
199+
this.encodedSchemeSpecificPart = null;
198200
this.encodedAuthority = null;
199201
return this;
200202
}
@@ -204,14 +206,8 @@ public URIBuilder setPort(final int port) {
204206
*/
205207
public URIBuilder setPath(final String path) {
206208
this.path = path;
207-
return this;
208-
}
209-
210-
/**
211-
* Removes URI query.
212-
*/
213-
public URIBuilder removeQuery() {
214-
this.queryParams = null;
209+
this.encodedSchemeSpecificPart = null;
210+
this.encodedPath = null;
215211
return this;
216212
}
217213

@@ -222,6 +218,8 @@ public URIBuilder removeQuery() {
222218
*/
223219
public URIBuilder setQuery(final String query) {
224220
this.queryParams = parseQuery(query);
221+
this.encodedQuery = null;
222+
this.encodedSchemeSpecificPart = null;
225223
return this;
226224
}
227225

@@ -234,6 +232,8 @@ public URIBuilder addParameter(final String param, final String value) {
234232
this.queryParams = new ArrayList<NameValuePair>();
235233
}
236234
this.queryParams.add(new BasicNameValuePair(param, value));
235+
this.encodedQuery = null;
236+
this.encodedSchemeSpecificPart = null;
237237
return this;
238238
}
239239

@@ -246,14 +246,16 @@ public URIBuilder setParameter(final String param, final String value) {
246246
this.queryParams = new ArrayList<NameValuePair>();
247247
}
248248
if (!this.queryParams.isEmpty()) {
249-
for (Iterator<NameValuePair> it = this.queryParams.iterator(); it.hasNext(); ) {
250-
NameValuePair nvp = it.next();
249+
for (final Iterator<NameValuePair> it = this.queryParams.iterator(); it.hasNext(); ) {
250+
final NameValuePair nvp = it.next();
251251
if (nvp.getName().equals(param)) {
252252
it.remove();
253253
}
254254
}
255255
}
256256
this.queryParams.add(new BasicNameValuePair(param, value));
257+
this.encodedQuery = null;
258+
this.encodedSchemeSpecificPart = null;
257259
return this;
258260
}
259261

@@ -263,6 +265,7 @@ public URIBuilder setParameter(final String param, final String value) {
263265
*/
264266
public URIBuilder setFragment(final String fragment) {
265267
this.fragment = fragment;
268+
this.encodedFragment = null;
266269
return this;
267270
}
268271

0 commit comments

Comments
 (0)