Skip to content

Commit

Permalink
Fixes #4305 - Fix getRequestTarget in Grizzly HTTP engine (#4306)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Nov 30, 2024
1 parent 8f21379 commit 8bd9543
Show file tree
Hide file tree
Showing 9 changed files with 478 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ public int getRemotePort() {

@Override
public String getRequestTarget() {
return request.getRequestURI();
String requestTarget;
if (request.getQueryString() != null) {
requestTarget = request.getRequestURI() + "?" + request.getQueryString();
} else {
requestTarget = request.getRequestURI();
}
return requestTarget;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.http.grizzly;

import cloud.piranha.http.api.HttpServer;
import cloud.piranha.http.api.HttpServerProcessor;
import cloud.piranha.http.tests.HttpServerRequestTest;

/**
* The JUnit tests for the DefaultHttpServer class.
*
* @author Manfred Riem ([email protected])
*/
public class GrizzlyHttpServerRequestTest extends HttpServerRequestTest {

/**
* Create the Netty HTTP server.
*
* @param portNumber the port number.
* @param processor the HTTP server processor.
* @return the Netty HTTP server.
*/
@Override
protected HttpServer createServer(int portNumber, HttpServerProcessor processor) {
return new GrizzlyHttpServer(portNumber, processor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.http.tests;

import cloud.piranha.http.api.HttpServer;
import cloud.piranha.http.api.HttpServerProcessor;
import static cloud.piranha.http.api.HttpServerProcessorEndState.COMPLETED;
import cloud.piranha.http.api.HttpServerRequest;
import cloud.piranha.http.api.HttpServerResponse;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
import me.alexpanov.net.FreePortFinder;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

/**
* The JUnit tests for the HttpServerRequest class.
*
* @author Manfred Riem ([email protected])
*/
public abstract class HttpServerRequestTest {

/**
* Constructor.
*/
public HttpServerRequestTest() {
}

/**
* Create server with a port and processor.
*
* @param portNumber the port number.
* @param processor the HTTP processor.
* @return the HTTP server.
*/
protected abstract HttpServer createServer(int portNumber, HttpServerProcessor processor);

/**
* Test getRequestTarget method.
*/
@Test
void testGetRequestTarget() {
System.clearProperty("requestTarget");
int port = FreePortFinder.findFreeLocalPort();
HttpServer server = createServer(port,
(HttpServerRequest request, HttpServerResponse response) -> {
System.setProperty("requestTarget", request.getRequestTarget());
return COMPLETED;
});
server.start();
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(
URI.create("http://localhost:" + port)).build();
client.send(request, BodyHandlers.discarding());
} catch (Exception e) {
// do nothing
} finally {
server.stop();
}
assertNotNull(System.getProperty("requestTarget"));
System.clearProperty("requestTarget");
}

/**
* Test getRequestTarget method (looking for query string marker).
*/
@Test
void testGetRequestTarget2() {
System.clearProperty("requestTarget");
int port = FreePortFinder.findFreeLocalPort();
HttpServer server = createServer(port,
(HttpServerRequest request, HttpServerResponse response) -> {
System.setProperty("requestTarget", request.getRequestTarget());
return COMPLETED;
});
server.start();
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(
URI.create("http://localhost:" + port + "?queryParam=queryParam")).build();
client.send(request, BodyHandlers.discarding());
} catch (Exception e) {
// do nothing
} finally {
server.stop();
}
assertNotNull(System.getProperty("requestTarget"));
assertTrue(System.getProperty("requestTarget").indexOf("?") > 0);
System.clearProperty("requestTarget");
}
}
63 changes: 63 additions & 0 deletions test/coreprofile/integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@
<build>
<finalName>piranha-test-coreprofile-integration</finalName>
<plugins>
<plugin>
<groupId>cloud.piranha.maven</groupId>
<artifactId>piranha-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<distribution>coreprofile</distribution>
<httpPort>${httpPort}</httpPort>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
Expand All @@ -68,6 +93,9 @@
</executions>
<configuration>
<rerunFailingTestsCount>10</rerunFailingTestsCount>
<systemPropertyVariables>
<httpPort>${httpPort}</httpPort>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
Expand All @@ -77,6 +105,41 @@
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>reserve-network-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>validate</phase>
<configuration>
<portNames>
<portName>httpPort</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>debug</id>
<build>
<plugins>
<plugin>
<groupId>cloud.piranha.maven</groupId>
<artifactId>piranha-maven-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<jvmArguments>-Xdebug -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=9009</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.test.coreprofile.distribution;

import jakarta.ws.rs.BeanParam;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import static jakarta.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
import static jakarta.ws.rs.core.MediaType.TEXT_PLAIN;
import jakarta.ws.rs.core.Response;

/**
* The BeanParam bean to test the BeanParam annotation integration.
*
* @author Manfred Riem ([email protected])
*/
@Path("/beanParam")
public class BeanParamBean {

/**
* Process BeanParam annotated input.
*
* @param input the input.
* @return the response.
*/
@POST
@Consumes(APPLICATION_FORM_URLENCODED)
@Produces(TEXT_PLAIN)
public Response beanParamInput(@BeanParam BeanParamInput input) {
return Response.ok(input.toString()).build();
}
}
Loading

0 comments on commit 8bd9543

Please sign in to comment.