Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing query string conversion in TomcatService #74

Merged
merged 1 commit into from
Dec 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.AsciiString;
Expand Down Expand Up @@ -248,14 +249,27 @@ public long getBytesWritten() {

private static Request convertRequest(FullHttpRequest req, String mappedPath) {
final Request coyoteReq = new Request();
coyoteReq.method().setString(req.method().name());

// Set the method.
final HttpMethod method = req.method();
coyoteReq.method().setString(method.name());

// Set the request URI.
final byte[] uriBytes = mappedPath.getBytes(StandardCharsets.US_ASCII);
coyoteReq.requestURI().setBytes(uriBytes, 0, uriBytes.length);

// Set the query string if any.
final int queryIndex = req.uri().indexOf('?');
if (queryIndex >= 0) {
coyoteReq.queryString().setString(req.uri().substring(queryIndex + 1));
}

// Set the headers.
final MimeHeaders cHeaders = coyoteReq.getMimeHeaders();
convertHeaders(req.headers(), cHeaders);
convertHeaders(req.trailingHeaders(), cHeaders);

// Set the content.
final ByteBuf content = req.content();
if (content.isReadable()) {
coyoteReq.setInputBuffer(new InputBuffer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.regex.Pattern;

import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import com.linecorp.armeria.server.AbstractServerTest;
import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.server.VirtualHostBuilder;
import com.linecorp.armeria.server.logging.LoggingService;

import io.netty.handler.codec.http.HttpHeaderNames;
Expand Down Expand Up @@ -65,4 +70,46 @@ public void testJsp() throws Exception {
}
}
}

@Test
public void testGetQueryString() throws Exception {
try (CloseableHttpClient hc = HttpClients.createMinimal()) {
try (CloseableHttpResponse res = hc.execute(
new HttpGet(uri("/tc/query_string.jsp?foo=%31&bar=%32")))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These URL encoded values are intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, to test if they are decoded properly.


assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
assertThat(res.getFirstHeader(HttpHeaderNames.CONTENT_TYPE.toString()).getValue(),
startsWith("text/html"));
final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity()))
.replaceAll("");
assertThat(actualContent, is(
"<html><body>" +
"<p>foo is 1</p>" +
"<p>bar is 2</p>" +
"</body></html>"));
}
}
}

@Test
public void testPostQueryString() throws Exception {
try (CloseableHttpClient hc = HttpClients.createMinimal()) {
final HttpPost post = new HttpPost(uri("/tc/query_string.jsp?foo=3"));
post.setEntity(new UrlEncodedFormEntity(
Collections.singletonList(new BasicNameValuePair("bar", "4")), StandardCharsets.UTF_8));

try (CloseableHttpResponse res = hc.execute(post)) {
assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
assertThat(res.getFirstHeader(HttpHeaderNames.CONTENT_TYPE.toString()).getValue(),
startsWith("text/html"));
final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity()))
.replaceAll("");
assertThat(actualContent, is(
"<html><body>" +
"<p>foo is 3</p>" +
"<p>bar is 4</p>" +
"</body></html>"));
}
}
}
}
7 changes: 7 additions & 0 deletions src/test/resources/tomcat_service/query_string.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%@ page contentType="text/html; ISO-8859-1" %>
<html>
<body>
<p>foo is <%= request.getParameter("foo") %></p>
<p>bar is <%= request.getParameter("bar") %></p>
</body>
</html>