HttpRequestEncoder extends HttpObjectEncoder<HttpRequest> { @Override protected void encodeInitialLine(ByteBuf buf, HttpRequest request) throws Exception { AsciiString method = request.method().asciiName(); ByteBufUtil.copy(method, method.arrayOffset(), buf, method.length()); buf.writeByte(SP); String uri = request.uri(); if (uri.length() == 0) { uri += SLASH; } else { int start = uri.indexOf(": if (start != -1 && uri.charAt(0) != SLASH) { int startIndex = start + 3; 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) .append(SLASH) .append(uri, index, len); uri = sb.toString(); } } } } buf.writeBytes(uri.getBytes(CharsetUtil.UTF_8)); buf.writeByte(SP); request.protocolVersion().encode(buf); buf.writeBytes(CRLF); }  @Override boolean acceptOutboundMessage(Object msg);  }
@Test public void testUriWithoutPath() throws Exception { HttpRequestEncoder encoder = new HttpRequestEncoder(); ByteBuf buffer = Unpooled.buffer(64); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http: String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET http: }
@Test public void testUriWithoutPath2() throws Exception { HttpRequestEncoder encoder = new HttpRequestEncoder(); ByteBuf buffer = Unpooled.buffer(64); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http: String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET http: }
@Test public void testUriWithPath() throws Exception { HttpRequestEncoder encoder = new HttpRequestEncoder(); ByteBuf buffer = Unpooled.buffer(64); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http: String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET http: }
@Test public void testAbsPath() throws Exception { HttpRequestEncoder encoder = new HttpRequestEncoder(); ByteBuf buffer = Unpooled.buffer(64); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/")); String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET / HTTP/1.1\r\n", req); }
@Test public void testEmptyAbsPath() throws Exception { HttpRequestEncoder encoder = new HttpRequestEncoder(); ByteBuf buffer = Unpooled.buffer(64); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "")); String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET / HTTP/1.1\r\n", req); }
@Test public void testQueryStringPath() throws Exception { HttpRequestEncoder encoder = new HttpRequestEncoder(); ByteBuf buffer = Unpooled.buffer(64); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/?url=http: String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET /?url=http: }