Search in sources :

Example 76 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class MultiPartContentProviderTest method testFieldWithOverridenContentType.

@Test
public void testFieldWithOverridenContentType() throws Exception {
    String name = "field";
    String value = "è";
    Charset encoding = StandardCharsets.ISO_8859_1;
    start(new AbstractMultiPartHandler() {

        @Override
        protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Collection<Part> parts = request.getParts();
            Assert.assertEquals(1, parts.size());
            Part part = parts.iterator().next();
            Assert.assertEquals(name, part.getName());
            String contentType = part.getContentType();
            Assert.assertNotNull(contentType);
            int equal = contentType.lastIndexOf('=');
            Charset charset = Charset.forName(contentType.substring(equal + 1));
            Assert.assertEquals(encoding, charset);
            Assert.assertEquals(value, IO.toString(part.getInputStream(), charset));
        }
    });
    MultiPartContentProvider multiPart = new MultiPartContentProvider();
    HttpFields fields = new HttpFields();
    fields.put(HttpHeader.CONTENT_TYPE, "text/plain;charset=" + encoding.name());
    BytesContentProvider content = new BytesContentProvider(value.getBytes(encoding));
    multiPart.addFieldPart(name, content, fields);
    multiPart.close();
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Charset(java.nio.charset.Charset) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Part(javax.servlet.http.Part) HttpFields(org.eclipse.jetty.http.HttpFields) Collection(java.util.Collection) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Example 77 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class MultiPartContentProviderTest method testFieldWithFile.

@Test
public void testFieldWithFile() throws Exception {
    // Prepare a file to upload.
    byte[] data = new byte[1024];
    new Random().nextBytes(data);
    Path tmpDir = MavenTestingUtils.getTargetTestingPath();
    Path tmpPath = Files.createTempFile(tmpDir, "multipart_", ".txt");
    try (OutputStream output = Files.newOutputStream(tmpPath, StandardOpenOption.CREATE)) {
        output.write(data);
    }
    String field = "field";
    String value = "€";
    String fileField = "file";
    Charset encoding = StandardCharsets.UTF_8;
    String contentType = "text/plain;charset=" + encoding.name();
    String headerName = "foo";
    String headerValue = "bar";
    start(new AbstractMultiPartHandler() {

        @Override
        protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            List<Part> parts = new ArrayList<>(request.getParts());
            Assert.assertEquals(2, parts.size());
            Part fieldPart = parts.get(0);
            Part filePart = parts.get(1);
            if (!field.equals(fieldPart.getName())) {
                Part swap = filePart;
                filePart = fieldPart;
                fieldPart = swap;
            }
            Assert.assertEquals(field, fieldPart.getName());
            Assert.assertEquals(contentType, fieldPart.getContentType());
            Assert.assertEquals(value, IO.toString(fieldPart.getInputStream(), encoding));
            Assert.assertEquals(headerValue, fieldPart.getHeader(headerName));
            Assert.assertEquals(fileField, filePart.getName());
            Assert.assertEquals("application/octet-stream", filePart.getContentType());
            Assert.assertEquals(tmpPath.getFileName().toString(), filePart.getSubmittedFileName());
            Assert.assertEquals(Files.size(tmpPath), filePart.getSize());
            Assert.assertArrayEquals(data, IO.readBytes(filePart.getInputStream()));
        }
    });
    MultiPartContentProvider multiPart = new MultiPartContentProvider();
    HttpFields fields = new HttpFields();
    fields.put(headerName, headerValue);
    multiPart.addFieldPart(field, new StringContentProvider(value, encoding), fields);
    multiPart.addFilePart(fileField, tmpPath.getFileName().toString(), new PathContentProvider(tmpPath), null);
    multiPart.close();
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
    Assert.assertEquals(200, response.getStatus());
    Files.delete(tmpPath);
}
Also used : Path(java.nio.file.Path) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) Part(javax.servlet.http.Part) HttpFields(org.eclipse.jetty.http.HttpFields) ArrayList(java.util.ArrayList) List(java.util.List) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Example 78 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class HttpChannelOverFCGI method exchangeTerminated.

@Override
public void exchangeTerminated(HttpExchange exchange, Result result) {
    super.exchangeTerminated(exchange, result);
    idle.onClose();
    HttpFields responseHeaders = result.getResponse().getHeaders();
    if (result.isFailed())
        connection.close(result.getFailure());
    else if (!connection.closeByHTTP(responseHeaders))
        release();
}
Also used : HttpFields(org.eclipse.jetty.http.HttpFields)

Example 79 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class HttpReceiverOverHTTP2 method onHeaders.

@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
    HttpExchange exchange = getHttpExchange();
    if (exchange == null)
        return;
    HttpResponse response = exchange.getResponse();
    MetaData.Response metaData = (MetaData.Response) frame.getMetaData();
    response.version(metaData.getHttpVersion()).status(metaData.getStatus()).reason(metaData.getReason());
    if (responseBegin(exchange)) {
        HttpFields headers = metaData.getFields();
        for (HttpField header : headers) {
            if (!responseHeader(exchange, header))
                return;
        }
        if (responseHeaders(exchange)) {
            int status = metaData.getStatus();
            boolean informational = HttpStatus.isInformational(status) && status != HttpStatus.SWITCHING_PROTOCOLS_101;
            if (frame.isEndStream() || informational)
                responseSuccess(exchange);
        }
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpResponse(org.eclipse.jetty.client.HttpResponse) MetaData(org.eclipse.jetty.http.MetaData) HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) HttpExchange(org.eclipse.jetty.client.HttpExchange) HttpResponse(org.eclipse.jetty.client.HttpResponse)

Example 80 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class TrailersTest method testTrailersSentByServer.

@Test
public void testTrailersSentByServer() throws Exception {
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            HttpFields responseFields = new HttpFields();
            responseFields.put("X-Response", "true");
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, responseFields);
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), response, null, false);
            stream.headers(responseFrame, new Callback() {

                @Override
                public void succeeded() {
                    HttpFields trailerFields = new HttpFields();
                    trailerFields.put("X-Trailer", "true");
                    MetaData trailer = new MetaData(HttpVersion.HTTP_2, trailerFields);
                    HeadersFrame trailerFrame = new HeadersFrame(stream.getId(), trailer, null, true);
                    stream.headers(trailerFrame, NOOP);
                }
            });
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request request = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(request, null, true);
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(requestFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        private boolean responded;

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (!responded) {
                MetaData.Response response = (MetaData.Response) frame.getMetaData();
                Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
                Assert.assertTrue(response.getFields().containsKey("X-Response"));
                Assert.assertFalse(frame.isEndStream());
                responded = true;
            } else {
                MetaData trailer = frame.getMetaData();
                Assert.assertTrue(trailer.getFields().containsKey("X-Trailer"));
                Assert.assertTrue(frame.isEndStream());
                latch.countDown();
            }
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

HttpFields (org.eclipse.jetty.http.HttpFields)172 Test (org.junit.Test)142 MetaData (org.eclipse.jetty.http.MetaData)117 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)105 CountDownLatch (java.util.concurrent.CountDownLatch)96 Stream (org.eclipse.jetty.http2.api.Stream)93 Session (org.eclipse.jetty.http2.api.Session)89 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)79 FuturePromise (org.eclipse.jetty.util.FuturePromise)69 HttpServletResponse (javax.servlet.http.HttpServletResponse)59 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)55 Callback (org.eclipse.jetty.util.Callback)53 ByteBuffer (java.nio.ByteBuffer)52 Promise (org.eclipse.jetty.util.Promise)49 HttpServletRequest (javax.servlet.http.HttpServletRequest)46 IOException (java.io.IOException)42 ServletException (javax.servlet.ServletException)39 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)37 HttpServlet (javax.servlet.http.HttpServlet)33 HashMap (java.util.HashMap)32