Search in sources :

Example 1 with EofException

use of org.eclipse.jetty.io.EofException in project jetty.project by eclipse.

the class HttpServerTestBase method testInterruptedRequest.

@Test
public void testInterruptedRequest() throws Exception {
    final AtomicBoolean fourBytesRead = new AtomicBoolean(false);
    final AtomicBoolean earlyEOFException = new AtomicBoolean(false);
    configureServer(new AbstractHandler.ErrorDispatchHandler() {

        @Override
        public void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            int contentLength = request.getContentLength();
            ServletInputStream inputStream = request.getInputStream();
            for (int i = 0; i < contentLength; i++) {
                try {
                    inputStream.read();
                } catch (EofException e) {
                    earlyEOFException.set(true);
                    throw new QuietServletException(e);
                }
                if (i == 3)
                    fourBytesRead.set(true);
            }
        }
    });
    StringBuffer request = new StringBuffer("GET / HTTP/1.0\n");
    request.append("Host: localhost\n");
    request.append("Content-length: 6\n\n");
    request.append("foo");
    Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
    OutputStream os = client.getOutputStream();
    os.write(request.toString().getBytes());
    os.flush();
    client.shutdownOutput();
    String response = readResponse(client);
    client.close();
    assertThat("response contains 500", response, Matchers.containsString(" 500 "));
    assertThat("The 4th byte (-1) has not been passed to the handler", fourBytesRead.get(), is(false));
    assertThat("EofException has been caught", earlyEOFException.get(), is(true));
}
Also used : EofException(org.eclipse.jetty.io.EofException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletInputStream(javax.servlet.ServletInputStream) Socket(java.net.Socket) Test(org.junit.Test)

Example 2 with EofException

use of org.eclipse.jetty.io.EofException in project dropwizard by dropwizard.

the class EarlyEofExceptionMapperTest method testToReponse.

@Test
public void testToReponse() {
    final Response reponse = mapper.toResponse(new EofException());
    Assert.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), reponse.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) EofException(org.eclipse.jetty.io.EofException) Test(org.junit.Test)

Example 3 with EofException

use of org.eclipse.jetty.io.EofException in project jetty.project by eclipse.

the class HttpOutput method write.

@Override
public void write(byte[] b, int off, int len) throws IOException {
    // Async or Blocking ?
    while (true) {
        switch(_state.get()) {
            case OPEN:
                // process blocking below
                break;
            case ASYNC:
                throw new IllegalStateException("isReady() not called");
            case READY:
                if (!_state.compareAndSet(OutputState.READY, OutputState.PENDING))
                    continue;
                // Should we aggregate?
                boolean last = isLastContentToWrite(len);
                if (!last && len <= _commitSize) {
                    if (_aggregate == null)
                        _aggregate = _channel.getByteBufferPool().acquire(getBufferSize(), _interceptor.isOptimizedForDirectBuffers());
                    // YES - fill the aggregate with content from the buffer
                    int filled = BufferUtil.fill(_aggregate, b, off, len);
                    // return if we are not complete, not full and filled all the content
                    if (filled == len && !BufferUtil.isFull(_aggregate)) {
                        if (!_state.compareAndSet(OutputState.PENDING, OutputState.ASYNC))
                            throw new IllegalStateException();
                        return;
                    }
                    // adjust offset/length
                    off += filled;
                    len -= filled;
                }
                // Do the asynchronous writing from the callback
                new AsyncWrite(b, off, len, last).iterate();
                return;
            case PENDING:
            case UNREADY:
                throw new WritePendingException();
            case ERROR:
                throw new EofException(_onError);
            case CLOSED:
                throw new EofException("Closed");
            default:
                throw new IllegalStateException();
        }
        break;
    }
    // handle blocking write
    // Should we aggregate?
    int capacity = getBufferSize();
    boolean last = isLastContentToWrite(len);
    if (!last && len <= _commitSize) {
        if (_aggregate == null)
            _aggregate = _channel.getByteBufferPool().acquire(capacity, _interceptor.isOptimizedForDirectBuffers());
        // YES - fill the aggregate with content from the buffer
        int filled = BufferUtil.fill(_aggregate, b, off, len);
        // return if we are not complete, not full and filled all the content
        if (filled == len && !BufferUtil.isFull(_aggregate))
            return;
        // adjust offset/length
        off += filled;
        len -= filled;
    }
    // flush any content from the aggregate
    if (BufferUtil.hasContent(_aggregate)) {
        write(_aggregate, last && len == 0);
        // should we fill aggregate again from the buffer?
        if (len > 0 && !last && len <= _commitSize && len <= BufferUtil.space(_aggregate)) {
            BufferUtil.append(_aggregate, b, off, len);
            return;
        }
    }
    // write any remaining content in the buffer directly
    if (len > 0) {
        // write a buffer capacity at a time to avoid JVM pooling large direct buffers
        // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6210541
        ByteBuffer view = ByteBuffer.wrap(b, off, len);
        while (len > getBufferSize()) {
            int p = view.position();
            int l = p + getBufferSize();
            view.limit(p + getBufferSize());
            write(view, false);
            len -= getBufferSize();
            view.limit(l + Math.min(len, getBufferSize()));
            view.position(l);
        }
        write(view, last);
    } else if (last) {
        write(BufferUtil.EMPTY_BUFFER, true);
    }
    if (last)
        closed();
}
Also used : EofException(org.eclipse.jetty.io.EofException) WritePendingException(java.nio.channels.WritePendingException) ByteBuffer(java.nio.ByteBuffer)

Example 4 with EofException

use of org.eclipse.jetty.io.EofException in project jetty.project by eclipse.

the class HttpOutput method sendContent.

/**
     * Asynchronous send of HTTP content.
     *
     * @param httpContent The HTTP content to send
     * @param callback    The callback to use to notify success or failure
     */
public void sendContent(HttpContent httpContent, Callback callback) {
    if (LOG.isDebugEnabled())
        LOG.debug("sendContent(http={},{})", httpContent, callback);
    if (BufferUtil.hasContent(_aggregate)) {
        callback.failed(new IOException("cannot sendContent() after write()"));
        return;
    }
    if (_channel.isCommitted()) {
        callback.failed(new IOException("cannot sendContent(), output already committed"));
        return;
    }
    while (true) {
        switch(_state.get()) {
            case OPEN:
                if (!_state.compareAndSet(OutputState.OPEN, OutputState.PENDING))
                    continue;
                break;
            case ERROR:
                callback.failed(new EofException(_onError));
                return;
            case CLOSED:
                callback.failed(new EofException("Closed"));
                return;
            default:
                throw new IllegalStateException();
        }
        break;
    }
    ByteBuffer buffer = _channel.useDirectBuffers() ? httpContent.getDirectBuffer() : null;
    if (buffer == null)
        buffer = httpContent.getIndirectBuffer();
    if (buffer != null) {
        sendContent(buffer, callback);
        return;
    }
    try {
        ReadableByteChannel rbc = httpContent.getReadableByteChannel();
        if (rbc != null) {
            // Close of the rbc is done by the async sendContent
            sendContent(rbc, callback);
            return;
        }
        InputStream in = httpContent.getInputStream();
        if (in != null) {
            sendContent(in, callback);
            return;
        }
        throw new IllegalArgumentException("unknown content for " + httpContent);
    } catch (Throwable th) {
        abort(th);
        callback.failed(th);
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) EofException(org.eclipse.jetty.io.EofException) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

EofException (org.eclipse.jetty.io.EofException)4 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 WritePendingException (java.nio.channels.WritePendingException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ServletException (javax.servlet.ServletException)1 ServletInputStream (javax.servlet.ServletInputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Response (javax.ws.rs.core.Response)1 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1