|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.felix.http.jetty.it; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.ops4j.pax.exam.CoreOptions.mavenBundle; |
| 22 | +import static org.ops4j.pax.exam.cm.ConfigurationAdminOptions.newConfiguration; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.URI; |
| 26 | +import java.util.Hashtable; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import javax.inject.Inject; |
| 30 | +import jakarta.servlet.Servlet; |
| 31 | +import jakarta.servlet.ServletException; |
| 32 | +import jakarta.servlet.http.HttpServlet; |
| 33 | +import jakarta.servlet.http.HttpServletRequest; |
| 34 | +import jakarta.servlet.http.HttpServletResponse; |
| 35 | + |
| 36 | +import org.eclipse.jetty.client.ContentResponse; |
| 37 | +import org.eclipse.jetty.client.HttpClient; |
| 38 | +import org.eclipse.jetty.client.transport.HttpClientTransportOverHTTP; |
| 39 | +import org.eclipse.jetty.util.Fields; |
| 40 | +import org.junit.Before; |
| 41 | +import org.junit.Test; |
| 42 | +import org.junit.runner.RunWith; |
| 43 | +import org.ops4j.pax.exam.Option; |
| 44 | +import org.ops4j.pax.exam.junit.PaxExam; |
| 45 | +import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; |
| 46 | +import org.ops4j.pax.exam.spi.reactors.PerClass; |
| 47 | +import org.osgi.framework.BundleContext; |
| 48 | +import org.osgi.service.http.HttpService; |
| 49 | +import org.osgi.service.servlet.whiteboard.HttpWhiteboardConstants; |
| 50 | + |
| 51 | +@RunWith(PaxExam.class) |
| 52 | +@ExamReactorStrategy(PerClass.class) |
| 53 | +public class JettySizeLimitHandlerIT extends AbstractJettyTestSupport { |
| 54 | + private static final int LIMIT_IN_BYTES = 10; |
| 55 | + |
| 56 | + @Inject |
| 57 | + protected BundleContext bundleContext; |
| 58 | + |
| 59 | + @Override |
| 60 | + protected Option[] additionalOptions() throws IOException { |
| 61 | + String jettyVersion = System.getProperty("jetty.version", JETTY_VERSION); |
| 62 | + return new Option[] { |
| 63 | + spifly(), |
| 64 | + |
| 65 | + // bundles for the server side |
| 66 | + mavenBundle().groupId("org.eclipse.jetty.ee10").artifactId("jetty-ee10-webapp").version(jettyVersion), |
| 67 | + mavenBundle().groupId("org.eclipse.jetty").artifactId("jetty-ee").version(jettyVersion), |
| 68 | + mavenBundle().groupId("org.eclipse.jetty.ee10").artifactId("jetty-ee10-servlet").version(jettyVersion), |
| 69 | + mavenBundle().groupId("org.eclipse.jetty").artifactId("jetty-xml").version(jettyVersion), |
| 70 | + |
| 71 | + // additional bundles for the client side |
| 72 | + mavenBundle().groupId("org.eclipse.jetty").artifactId("jetty-alpn-client").version(jettyVersion), |
| 73 | + mavenBundle().groupId("org.eclipse.jetty").artifactId("jetty-client").version(jettyVersion) |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected Option felixHttpConfig(int httpPort) { |
| 79 | + return newConfiguration("org.apache.felix.http") |
| 80 | + .put("org.osgi.service.http.port", httpPort) |
| 81 | + .put("org.apache.felix.http.jetty.requestSizeLimit", LIMIT_IN_BYTES) // 10 bytes limit for the request |
| 82 | + .put("org.apache.felix.http.jetty.responseSizeLimit", LIMIT_IN_BYTES) // 10 bytes limit for the response |
| 83 | + .asOption(); |
| 84 | + } |
| 85 | + |
| 86 | + @Before |
| 87 | + public void setup(){ |
| 88 | + assertNotNull(bundleContext); |
| 89 | + bundleContext.registerService(Servlet.class, new HelloWorldServletWithinLimit(), new Hashtable<>(Map.of( |
| 90 | + HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/withinlimit/*" |
| 91 | + ))); |
| 92 | + bundleContext.registerService(Servlet.class, new HelloWorldServletExceedingLimit(), new Hashtable<>(Map.of( |
| 93 | + HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/exceedinglimit/*" |
| 94 | + ))); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testRequestResponseLimits() throws Exception { |
| 100 | + HttpClientTransportOverHTTP transport = new HttpClientTransportOverHTTP(); |
| 101 | + HttpClient httpClient = new HttpClient(transport); |
| 102 | + httpClient.start(); |
| 103 | + |
| 104 | + Object value = bundleContext.getServiceReference(HttpService.class).getProperty("org.osgi.service.http.port"); |
| 105 | + int httpPort = Integer.parseInt((String) value); |
| 106 | + |
| 107 | + Fields formFields = new Fields(); |
| 108 | + formFields.add(new Fields.Field("key", "value")); // under 10 bytes |
| 109 | + ContentResponse responseWithinLimit = httpClient.FORM(new URI(String.format("http://localhost:%d/withinlimit/a", httpPort)), formFields); |
| 110 | + |
| 111 | + // Request limit ok, response limit ok |
| 112 | + assertEquals(200, responseWithinLimit.getStatus()); |
| 113 | + assertEquals("OK", responseWithinLimit.getContentAsString()); |
| 114 | + |
| 115 | + // Request limit ok, response limit exceeded |
| 116 | + // org.eclipse.jetty.http.HttpException$RuntimeException: 500: Response body is too large: 17>10 |
| 117 | + ContentResponse responseExceedingLimit = httpClient.FORM(new URI(String.format("http://localhost:%d/exceedinglimit/a", httpPort)), formFields); |
| 118 | + assertEquals(500, responseExceedingLimit.getStatus()); |
| 119 | + |
| 120 | + Fields formFieldsLimitExceeded = new Fields(); |
| 121 | + formFieldsLimitExceeded.add(new Fields.Field("key", "valueoverlimit")); // over limit of 10 bytes |
| 122 | + ContentResponse responseExceeded = httpClient.FORM(new URI(String.format("http://localhost:%d/withinlimit/a", httpPort)), formFieldsLimitExceeded); |
| 123 | + |
| 124 | + // Request limit exceeded, HTTP 413 directly from Jetty |
| 125 | + assertEquals(413, responseExceeded.getStatus()); |
| 126 | + |
| 127 | + httpClient.close(); |
| 128 | + } |
| 129 | + |
| 130 | + static final class HelloWorldServletWithinLimit extends HttpServlet { |
| 131 | + @Override |
| 132 | + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 133 | + resp.setStatus(200); |
| 134 | + resp.getWriter().write("OK"); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + static final class HelloWorldServletExceedingLimit extends HttpServlet { |
| 139 | + @Override |
| 140 | + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 141 | + resp.setStatus(200); |
| 142 | + resp.getWriter().write("responseoverlimit"); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments