HTTPCLIENT-1195: blanks in the query part of request URIs to be encoded as '+'

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1353593 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2012-06-25 15:28:41 +00:00
parent 9dcf1ebd67
commit 2e9220e97b
3 changed files with 38 additions and 26 deletions

View File

@ -310,7 +310,10 @@ public class URLEncodedUtils {
private static final int RADIX = 16;
private static String urlencode(
final String content, final Charset charset, final BitSet safechars) {
final String content,
final Charset charset,
final BitSet safechars,
final boolean blankAsPlus) {
if (content == null) {
return null;
}
@ -321,18 +324,24 @@ public class URLEncodedUtils {
if (safechars.get(b)) {
buf.append((char) b);
} else {
buf.append("%");
char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
buf.append(hex1);
buf.append(hex2);
if (b == ' ' && blankAsPlus) {
buf.append('+');
} else {
buf.append("%");
char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
buf.append(hex1);
buf.append(hex2);
}
}
}
return buf.toString();
}
private static String urldecode(
final String content, final Charset charset) {
final String content,
final Charset charset,
final boolean plusAsBlank) {
if (content == null) {
return null;
}
@ -352,6 +361,8 @@ public class URLEncodedUtils {
bb.put((byte) uc);
bb.put((byte) lc);
}
} else if (c == '+' && plusAsBlank) {
bb.put((byte) ' ');
} else {
bb.put((byte) c);
}
@ -364,50 +375,51 @@ public class URLEncodedUtils {
if (content == null) {
return null;
}
return urldecode(content, charset != null ? Charset.forName(charset) : Consts.UTF_8);
return urldecode(content, charset != null ? Charset.forName(charset) : Consts.UTF_8, true);
}
private static String decode (final String content, final Charset charset) {
if (content == null) {
return null;
}
return urldecode(content, charset != null ? charset : Consts.UTF_8);
return urldecode(content, charset != null ? charset : Consts.UTF_8, true);
}
private static String encode(final String content, final String charset) {
if (content == null) {
return null;
}
return urlencode(content, charset != null ? Charset.forName(charset) : Consts.UTF_8, UNRESERVED);
return urlencode(content, charset != null ? Charset.forName(charset) :
Consts.UTF_8, UNRESERVED, true);
}
private static String encode(final String content, final Charset charset) {
if (content == null) {
return null;
}
return urlencode(content, charset != null ? charset : Consts.UTF_8, UNRESERVED);
return urlencode(content, charset != null ? charset : Consts.UTF_8, UNRESERVED, true);
}
/**
* Encode a String using the {@link #SAFE} set of characters.
*
*
* @param content the string to encode
* @param charset the charset to use
* @return the encoded string
*/
static String enc(final String content, final Charset charset) {
return urlencode(content, charset, SAFE);
return urlencode(content, charset, SAFE, false);
}
/**
* Encode a String using the {@link #PATHSAFE} set of characters.
*
*
* @param content the string to encode
* @param charset the charset to use
* @return the encoded string
*/
static String encPath(final String content, final Charset charset) {
return urlencode(content, charset, PATHSAFE);
return urlencode(content, charset, PATHSAFE, false);
}
}

View File

@ -120,7 +120,7 @@ public class TestURIBuilder {
URIBuilder uribuilder = new URIBuilder(uri).setParameter("param", "some other stuff")
.setParameter("blah", "blah");
URI result = uribuilder.build();
Assert.assertEquals(new URI("http://localhost:80/?param=some%20other%20stuff&blah=blah"), result);
Assert.assertEquals(new URI("http://localhost:80/?param=some+other+stuff&blah=blah"), result);
}
@Test
@ -129,7 +129,7 @@ public class TestURIBuilder {
URIBuilder uribuilder = new URIBuilder(uri).addParameter("param", "1 + 1 = 2")
.addParameter("param", "blah&blah");
URI result = uribuilder.build();
Assert.assertEquals(new URI("http://localhost:80/?param=stuff&param=1%20%2B%201%20%3D%202&" +
Assert.assertEquals(new URI("http://localhost:80/?param=stuff&param=1+%2B+1+%3D+2&" +
"param=blah%26blah"), result);
}
@ -140,13 +140,13 @@ public class TestURIBuilder {
.addParameter("blah", "blah");
URI result = uribuilder.build();
Assert.assertEquals(new URI("http://localhost:80/?param=stuff&blah&blah&" +
"param=some%20other%20stuff&blah=blah"), result);
"param=some+other+stuff&blah=blah"), result);
}
@Test
public void testQueryEncoding() throws Exception {
URI uri1 = new URI("https://somehost.com/stuff?client_id=1234567890" +
"&redirect_uri=https%3A%2F%2Fsomehost.com%2Fblah%20blah%2F");
"&redirect_uri=https%3A%2F%2Fsomehost.com%2Fblah+blah%2F");
URI uri2 = new URIBuilder("https://somehost.com/stuff")
.addParameter("client_id","1234567890")
.addParameter("redirect_uri","https://somehost.com/blah blah/").build();

View File

@ -192,7 +192,7 @@ public class TestURLEncodedUtils {
String s = URLEncodedUtils.format(parameters, HTTP.DEF_CONTENT_CHARSET);
Assert.assertEquals("english=hi%20there&swiss=Gr%FCezi_z%E4m%E4", s);
Assert.assertEquals("english=hi+there&swiss=Gr%FCezi_z%E4m%E4", s);
StringEntity entity = new StringEntity(s, ContentType.create(
URLEncodedUtils.CONTENT_TYPE, HTTP.DEF_CONTENT_CHARSET));
@ -235,7 +235,7 @@ public class TestURLEncodedUtils {
params.clear();
params.add(new BasicNameValuePair("Name4", "Value 4&"));
Assert.assertEquals("Name4=Value%204%26", URLEncodedUtils.format(params, Consts.ASCII));
Assert.assertEquals("Name4=Value+4%26", URLEncodedUtils.format(params, Consts.ASCII));
params.clear();
params.add(new BasicNameValuePair("Name4", "Value+4&"));
@ -243,7 +243,7 @@ public class TestURLEncodedUtils {
params.clear();
params.add(new BasicNameValuePair("Name4", "Value 4& =4"));
Assert.assertEquals("Name4=Value%204%26%20%3D4", URLEncodedUtils.format(params, Consts.ASCII));
Assert.assertEquals("Name4=Value+4%26+%3D4", URLEncodedUtils.format(params, Consts.ASCII));
params.clear();
params.add(new BasicNameValuePair("Name5", "aaa"));
@ -258,7 +258,7 @@ public class TestURLEncodedUtils {
params.clear();
params.add(new BasicNameValuePair("Name8", "xx, yy ,zz"));
Assert.assertEquals("Name8=xx%2C%20%20yy%20%20%2Czz", URLEncodedUtils.format(params, Consts.ASCII));
Assert.assertEquals("Name8=xx%2C++yy++%2Czz", URLEncodedUtils.format(params, Consts.ASCII));
}
@Test
@ -280,7 +280,7 @@ public class TestURLEncodedUtils {
params.clear();
params.add(new BasicNameValuePair("Name4", "Value 4&"));
Assert.assertEquals("Name4=Value%204%26", URLEncodedUtils.format(params, "US-ASCII"));
Assert.assertEquals("Name4=Value+4%26", URLEncodedUtils.format(params, "US-ASCII"));
params.clear();
params.add(new BasicNameValuePair("Name4", "Value+4&"));
@ -288,7 +288,7 @@ public class TestURLEncodedUtils {
params.clear();
params.add(new BasicNameValuePair("Name4", "Value 4& =4"));
Assert.assertEquals("Name4=Value%204%26%20%3D4", URLEncodedUtils.format(params, "US-ASCII"));
Assert.assertEquals("Name4=Value+4%26+%3D4", URLEncodedUtils.format(params, "US-ASCII"));
params.clear();
params.add(new BasicNameValuePair("Name5", "aaa"));
@ -303,7 +303,7 @@ public class TestURLEncodedUtils {
params.clear();
params.add(new BasicNameValuePair("Name8", "xx, yy ,zz"));
Assert.assertEquals("Name8=xx%2C%20%20yy%20%20%2Czz", URLEncodedUtils.format(params, "US-ASCII"));
Assert.assertEquals("Name8=xx%2C++yy++%2Czz", URLEncodedUtils.format(params, "US-ASCII"));
}
private List <NameValuePair> parse (final String params) {