[#2732] HttpRequestEncoder may produce invalid uri if uri parameters are included.
Motivation: If the requests contains uri parameters but not path the HttpRequestEncoder does produce an invalid uri while try to add the missing path. Modifications: Correctly handle the case of uri with paramaters but no path. Result: HttpRequestEncoder produce correct uri in all cases.
This commit is contained in:
parent
9412ace4ee
commit
6c0429391e
|
@ -25,6 +25,7 @@ import static org.jboss.netty.handler.codec.http.HttpConstants.*;
|
|||
*/
|
||||
public class HttpRequestEncoder extends HttpMessageEncoder {
|
||||
private static final char SLASH = '/';
|
||||
private static final char QUESTION_MARK = '?';
|
||||
|
||||
@Override
|
||||
protected void encodeInitialLine(ChannelBuffer buf, HttpMessage message) throws Exception {
|
||||
|
@ -38,8 +39,22 @@ public class HttpRequestEncoder extends HttpMessageEncoder {
|
|||
int start = uri.indexOf("://");
|
||||
if (start != -1) {
|
||||
int startIndex = start + 3;
|
||||
if (uri.lastIndexOf(SLASH) <= startIndex) {
|
||||
uri += SLASH;
|
||||
// Correctly handle query params.
|
||||
// See https://github.com/netty/netty/issues/2732
|
||||
int index = uri.indexOf(QUESTION_MARK, startIndex);
|
||||
if (index == -1) {
|
||||
if (uri.lastIndexOf(SLASH) <= startIndex) {
|
||||
uri += SLASH;
|
||||
}
|
||||
} else {
|
||||
if (uri.lastIndexOf(SLASH, index) <= startIndex) {
|
||||
int len = uri.length();
|
||||
StringBuilder sb = new StringBuilder(len + 1);
|
||||
sb.append(uri, 0, index);
|
||||
sb.append(SLASH);
|
||||
sb.append(uri, index, len);
|
||||
uri = sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -45,4 +45,13 @@ public class HttpRequestEncoderTest {
|
|||
String req = buffer.toString(Charset.forName("US-ASCII"));
|
||||
assertEquals("GET http://localhost/ HTTP/1.1\r\n", req);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUriWithoutPath2() throws Exception {
|
||||
HttpRequestEncoder encoder = new HttpRequestEncoder();
|
||||
ChannelBuffer buffer = ChannelBuffers.buffer(64);
|
||||
encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost:9999?p1=v1"));
|
||||
String req = buffer.toString(Charset.forName("US-ASCII"));
|
||||
assertEquals("GET http://localhost:9999/?p1=v1 HTTP/1.1\r\n", req);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user