Search in sources :

Example 36 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class AsyncMiddleManServletTest method testDownstreamTransformationKnownContentLengthDroppingLastChunk.

@Test
public void testDownstreamTransformationKnownContentLengthDroppingLastChunk() throws Exception {
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            byte[] chunk = new byte[1024];
            int contentLength = 2 * chunk.length;
            response.setContentLength(contentLength);
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk);
            output.flush();
            sleep(1000);
            output.write(chunk);
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            return new ContentTransformer() {

                @Override
                public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
                    if (!finished)
                        output.add(input);
                }
            };
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 37 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class RequestTest method testHostPort.

@Test
public void testHostPort() throws Exception {
    final ArrayList<String> results = new ArrayList<>();
    _handler._checker = new RequestTester() {

        @Override
        public boolean check(HttpServletRequest request, HttpServletResponse response) {
            results.add(request.getRequestURL().toString());
            results.add(request.getRemoteAddr());
            results.add(request.getServerName());
            results.add(String.valueOf(request.getServerPort()));
            return true;
        }
    };
    results.clear();
    String response = _connector.getResponse("GET / HTTP/1.1\n" + "Host: myhost\n" + "Connection: close\n" + "\n");
    int i = 0;
    assertThat(response, Matchers.containsString("200 OK"));
    assertEquals("http://myhost/", results.get(i++));
    assertEquals("0.0.0.0", results.get(i++));
    assertEquals("myhost", results.get(i++));
    assertEquals("80", results.get(i++));
    results.clear();
    response = _connector.getResponse("GET / HTTP/1.1\n" + "Host: myhost:8888\n" + "Connection: close\n" + "\n");
    i = 0;
    assertThat(response, Matchers.containsString("200 OK"));
    assertEquals("http://myhost:8888/", results.get(i++));
    assertEquals("0.0.0.0", results.get(i++));
    assertEquals("myhost", results.get(i++));
    assertEquals("8888", results.get(i++));
    results.clear();
    response = _connector.getResponse("GET http://myhost:8888/ HTTP/1.0\n" + "\n");
    i = 0;
    assertThat(response, Matchers.containsString("200 OK"));
    assertEquals("http://myhost:8888/", results.get(i++));
    assertEquals("0.0.0.0", results.get(i++));
    assertEquals("myhost", results.get(i++));
    assertEquals("8888", results.get(i++));
    results.clear();
    response = _connector.getResponse("GET http://myhost:8888/ HTTP/1.1\n" + "Host: wrong:666\n" + "Connection: close\n" + "\n");
    i = 0;
    assertThat(response, Matchers.containsString("200 OK"));
    assertEquals("http://myhost:8888/", results.get(i++));
    assertEquals("0.0.0.0", results.get(i++));
    assertEquals("myhost", results.get(i++));
    assertEquals("8888", results.get(i++));
    results.clear();
    response = _connector.getResponse("GET / HTTP/1.1\n" + "Host: 1.2.3.4\n" + "Connection: close\n" + "\n");
    i = 0;
    assertThat(response, Matchers.containsString("200 OK"));
    assertEquals("http://1.2.3.4/", results.get(i++));
    assertEquals("0.0.0.0", results.get(i++));
    assertEquals("1.2.3.4", results.get(i++));
    assertEquals("80", results.get(i++));
    results.clear();
    response = _connector.getResponse("GET / HTTP/1.1\n" + "Host: 1.2.3.4:8888\n" + "Connection: close\n" + "\n");
    i = 0;
    assertThat(response, Matchers.containsString("200 OK"));
    assertEquals("http://1.2.3.4:8888/", results.get(i++));
    assertEquals("0.0.0.0", results.get(i++));
    assertEquals("1.2.3.4", results.get(i++));
    assertEquals("8888", results.get(i++));
    results.clear();
    response = _connector.getResponse("GET / HTTP/1.1\n" + "Host: [::1]\n" + "Connection: close\n" + "\n");
    i = 0;
    assertThat(response, Matchers.containsString("200 OK"));
    assertEquals("http://[::1]/", results.get(i++));
    assertEquals("0.0.0.0", results.get(i++));
    assertEquals("[::1]", results.get(i++));
    assertEquals("80", results.get(i++));
    results.clear();
    response = _connector.getResponse("GET / HTTP/1.1\n" + "Host: [::1]:8888\n" + "Connection: close\n" + "\n");
    i = 0;
    assertThat(response, Matchers.containsString("200 OK"));
    assertEquals("http://[::1]:8888/", results.get(i++));
    assertEquals("0.0.0.0", results.get(i++));
    assertEquals("[::1]", results.get(i++));
    assertEquals("8888", results.get(i++));
    results.clear();
    response = _connector.getResponse("GET / HTTP/1.1\n" + "Host: [::1]\n" + "x-forwarded-for: remote\n" + "x-forwarded-proto: https\n" + "Connection: close\n" + "\n");
    i = 0;
    assertThat(response, Matchers.containsString("200 OK"));
    assertEquals("https://[::1]/", results.get(i++));
    assertEquals("remote", results.get(i++));
    assertEquals("[::1]", results.get(i++));
    assertEquals("443", results.get(i++));
    results.clear();
    response = _connector.getResponse("GET / HTTP/1.1\n" + "Host: [::1]:8888\n" + "Connection: close\n" + "x-forwarded-for: remote\n" + "x-forwarded-proto: https\n" + "\n");
    i = 0;
    assertThat(response, Matchers.containsString("200 OK"));
    assertEquals("https://[::1]:8888/", results.get(i++));
    assertEquals("remote", results.get(i++));
    assertEquals("[::1]", results.get(i++));
    assertEquals("8888", results.get(i++));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) LocalEndPoint(org.eclipse.jetty.server.LocalConnector.LocalEndPoint) Test(org.junit.Test)

Example 38 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class RequestTest method testContentTypeEncoding.

@Test
public void testContentTypeEncoding() throws Exception {
    final ArrayList<String> results = new ArrayList<>();
    _handler._checker = new RequestTester() {

        @Override
        public boolean check(HttpServletRequest request, HttpServletResponse response) {
            results.add(request.getContentType());
            results.add(request.getCharacterEncoding());
            return true;
        }
    };
    LocalEndPoint endp = _connector.executeRequest("GET / HTTP/1.1\n" + "Host: whatever\n" + "Content-Type: text/test\n" + "\n" + "GET / HTTP/1.1\n" + "Host: whatever\n" + "Content-Type: text/html;charset=utf8\n" + "\n" + "GET / HTTP/1.1\n" + "Host: whatever\n" + "Content-Type: text/html; charset=\"utf8\"\n" + "\n" + "GET / HTTP/1.1\n" + "Host: whatever\n" + "Content-Type: text/html; other=foo ; blah=\"charset=wrong;\" ; charset =   \" x=z; \"   ; more=values \n" + "Connection: close\n" + "\n");
    endp.getResponse();
    endp.getResponse();
    endp.getResponse();
    endp.getResponse();
    int i = 0;
    assertEquals("text/test", results.get(i++));
    assertEquals(null, results.get(i++));
    assertEquals("text/html;charset=utf8", results.get(i++));
    assertEquals("utf-8", results.get(i++));
    assertEquals("text/html; charset=\"utf8\"", results.get(i++));
    assertEquals("utf-8", results.get(i++));
    assertTrue(results.get(i++).startsWith("text/html"));
    assertEquals(" x=z; ", results.get(i++));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) LocalEndPoint(org.eclipse.jetty.server.LocalConnector.LocalEndPoint) LocalEndPoint(org.eclipse.jetty.server.LocalConnector.LocalEndPoint) Test(org.junit.Test)

Example 39 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class RequestTest method testHashDOSKeys.

@Test
public void testHashDOSKeys() throws Exception {
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        LOG.info("Expecting maxFormKeys limit and Closing HttpParser exceptions...");
        _server.setAttribute("org.eclipse.jetty.server.Request.maxFormContentSize", -1);
        _server.setAttribute("org.eclipse.jetty.server.Request.maxFormKeys", 1000);
        StringBuilder buf = new StringBuilder(4000000);
        buf.append("a=b");
        // The evil keys file is not distributed - as it is dangerous
        File evil_keys = new File("/tmp/keys_mapping_to_zero_2m");
        if (evil_keys.exists()) {
            LOG.info("Using real evil keys!");
            try (BufferedReader in = new BufferedReader(new FileReader(evil_keys))) {
                String key = null;
                while ((key = in.readLine()) != null) buf.append("&").append(key).append("=").append("x");
            }
        } else {
            // we will just create a lot of keys and make sure the limit is applied
            for (int i = 0; i < 2000; i++) buf.append("&").append("K").append(i).append("=").append("x");
        }
        buf.append("&c=d");
        _handler._checker = new RequestTester() {

            @Override
            public boolean check(HttpServletRequest request, HttpServletResponse response) {
                return "b".equals(request.getParameter("a")) && request.getParameter("c") == null;
            }
        };
        String request = "POST / HTTP/1.1\r\n" + "Host: whatever\r\n" + "Content-Type: " + MimeTypes.Type.FORM_ENCODED.asString() + "\r\n" + "Content-Length: " + buf.length() + "\r\n" + "Connection: close\r\n" + "\r\n" + buf;
        long start = System.currentTimeMillis();
        String response = _connector.getResponse(request);
        assertThat(response, Matchers.containsString("IllegalStateException"));
        long now = System.currentTimeMillis();
        assertTrue((now - start) < 5000);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BufferedReader(java.io.BufferedReader) HttpServletResponse(javax.servlet.http.HttpServletResponse) FileReader(java.io.FileReader) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) File(java.io.File) LocalEndPoint(org.eclipse.jetty.server.LocalConnector.LocalEndPoint) Test(org.junit.Test)

Example 40 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class RequestTest method testMultiPartFormDataReadInputThenParams.

@Test
@Ignore("See issue #1175")
public void testMultiPartFormDataReadInputThenParams() throws Exception {
    final File tmpdir = MavenTestingUtils.getTargetTestingDir("multipart");
    FS.ensureEmpty(tmpdir);
    Handler handler = new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            if (baseRequest.getDispatcherType() != DispatcherType.REQUEST)
                return;
            // Fake a @MultiPartConfig'd servlet endpoint
            MultipartConfigElement multipartConfig = new MultipartConfigElement(tmpdir.getAbsolutePath());
            request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, multipartConfig);
            // Normal processing
            baseRequest.setHandled(true);
            // Fake the commons-fileupload behavior
            int length = request.getContentLength();
            InputStream in = request.getInputStream();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            // KEY STEP (Don't Change!) commons-fileupload does not read to EOF
            IO.copy(in, out, length);
            // Record what happened as servlet response headers
            response.setIntHeader("x-request-content-length", request.getContentLength());
            response.setIntHeader("x-request-content-read", out.size());
            // uri query parameter
            String foo = request.getParameter("foo");
            // form-data content parameter
            String bar = request.getParameter("bar");
            response.setHeader("x-foo", foo == null ? "null" : foo);
            response.setHeader("x-bar", bar == null ? "null" : bar);
        }
    };
    _server.stop();
    _server.setHandler(handler);
    _server.start();
    String multipart = "--AaBbCc\r\n" + "content-disposition: form-data; name=\"bar\"\r\n" + "\r\n" + "BarContent\r\n" + "--AaBbCc\r\n" + "content-disposition: form-data; name=\"stuff\"\r\n" + "Content-Type: text/plain;charset=ISO-8859-1\r\n" + "\r\n" + "000000000000000000000000000000000000000000000000000\r\n" + "--AaBbCc--\r\n";
    String request = "POST /?foo=FooUri HTTP/1.1\r\n" + "Host: whatever\r\n" + "Content-Type: multipart/form-data; boundary=\"AaBbCc\"\r\n" + "Content-Length: " + multipart.getBytes().length + "\r\n" + "Connection: close\r\n" + "\r\n" + multipart;
    HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request));
    // It should always be possible to read query string
    assertThat("response.x-foo", response.get("x-foo"), is("FooUri"));
    // Not possible to read request content parameters?
    // TODO: should this work?
    assertThat("response.x-bar", response.get("x-bar"), is("null"));
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) LocalEndPoint(org.eclipse.jetty.server.LocalConnector.LocalEndPoint) HttpTester(org.eclipse.jetty.http.HttpTester) HttpServletRequest(javax.servlet.http.HttpServletRequest) MultipartConfigElement(javax.servlet.MultipartConfigElement) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)3285 HttpServletRequest (javax.servlet.http.HttpServletRequest)2624 Test (org.junit.Test)1229 IOException (java.io.IOException)873 ServletException (javax.servlet.ServletException)671 PrintWriter (java.io.PrintWriter)286 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)250 FilterChain (javax.servlet.FilterChain)243 Request (org.eclipse.jetty.server.Request)223 HashMap (java.util.HashMap)204 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)195 HttpServlet (javax.servlet.http.HttpServlet)188 CountDownLatch (java.util.concurrent.CountDownLatch)183 ServletOutputStream (javax.servlet.ServletOutputStream)183 StringWriter (java.io.StringWriter)180 Test (org.testng.annotations.Test)176 HttpSession (javax.servlet.http.HttpSession)175 ServletContext (javax.servlet.ServletContext)173 InputStream (java.io.InputStream)141 Map (java.util.Map)137