org.apache.tomcat.jni.Status Java Examples

The following examples show how to use org.apache.tomcat.jni.Status. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Remove specified socket from the pollers. Must only be called from
 * {@link Poller#run()}.
 */
private void removeFromPoller(long socket) {
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("endpoint.debug.pollerRemove",
                Long.valueOf(socket)));
    }
    int rv = -1;
    for (int i = 0; i < pollers.length; i++) {
        if (pollerSpace[i] < actualPollerSize) {
            rv = Poll.remove(pollers[i], socket);
            if (rv != Status.APR_NOTFOUND) {
                pollerSpace[i]++;
                connectionCount.decrementAndGet();
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.debug.pollerRemoved",
                            Long.valueOf(socket)));
                }
                break;
            }
        }
    }
    timeouts.remove(socket);
}
 
Example #2
Source File: AprEndpoint.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Remove specified socket from the pollers. Must only be called from
 * {@link Poller#run()}.
 */
private boolean removeFromPoller(long socket) {
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("endpoint.debug.pollerRemove",
                Long.valueOf(socket)));
    }
    int rv = -1;
    for (int i = 0; i < pollers.length; i++) {
        if (pollerSpace[i] < actualPollerSize) {
            rv = Poll.remove(pollers[i], socket);
            if (rv != Status.APR_NOTFOUND) {
                pollerSpace[i]++;
                connectionCount.decrementAndGet();
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.debug.pollerRemoved",
                            Long.valueOf(socket)));
                }
                break;
            }
        }
    }
    timeouts.remove(socket);
    return (rv == Status.APR_SUCCESS);
}
 
Example #3
Source File: UpgradeAprProcessor.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public int read(boolean block, byte[] bytes, int off, int len)
        throws IOException {
    if (!block) {
        Socket.optSet(socket, Socket.APR_SO_NONBLOCK, -1);
    }
    try {
        int result = Socket.recv(socket, bytes, off, len);
        if (result > 0) {
            return result;
        } else if (-result == Status.EAGAIN) {
            return 0;
        } else {
            throw new IOException(sm.getString("apr.read.error",
                    Integer.valueOf(-result)));
        }
    } finally {
        if (!block) {
            Socket.optSet(socket, Socket.APR_SO_NONBLOCK, 0);
        }
    }
}
 
Example #4
Source File: UpgradeAprProcessor.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public int read(boolean block, byte[] bytes, int off, int len)
        throws IOException {
    if (!block) {
        Socket.optSet(socket, Socket.APR_SO_NONBLOCK, -1);
    }
    try {
        int result = Socket.recv(socket, bytes, off, len);
        if (result > 0) {
            return result;
        } else if (-result == Status.EAGAIN) {
            return 0;
        } else {
            throw new IOException(sm.getString("apr.read.error",
                    Integer.valueOf(-result)));
        }
    } finally {
        if (!block) {
            Socket.optSet(socket, Socket.APR_SO_NONBLOCK, 0);
        }
    }
}
 
Example #5
Source File: AprEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Remove specified socket from the pollers. Must only be called from
 * {@link Poller#run()}.
 */
private boolean removeFromPoller(long socket) {
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("endpoint.debug.pollerRemove",
                Long.valueOf(socket)));
    }
    int rv = -1;
    for (int i = 0; i < pollers.length; i++) {
        if (pollerSpace[i] < actualPollerSize) {
            rv = Poll.remove(pollers[i], socket);
            if (rv != Status.APR_NOTFOUND) {
                pollerSpace[i]++;
                connectionCount.decrementAndGet();
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.debug.pollerRemoved",
                            Long.valueOf(socket)));
                }
                break;
            }
        }
    }
    timeouts.remove(socket);
    return (rv == Status.APR_SUCCESS);
}
 
Example #6
Source File: AprEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Add specified socket to one of the pollers. Must only be called from
 * {@link Poller#run()}.
 */
protected boolean addToPoller(long socket, int events) {
    int rv = -1;
    for (int i = 0; i < pollers.length; i++) {
        if (pollerSpace[i] > 0) {
            rv = Poll.add(pollers[i], socket, events);
            if (rv == Status.APR_SUCCESS) {
                pollerSpace[i]--;
                connectionCount.incrementAndGet();
                return true;
            }
        }
    }
    return false;
}
 
Example #7
Source File: AprEndpoint.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Remove socket from the poller.
 *
 * @param data the sendfile data which should be removed
 */
protected void remove(SendfileData data) {
    int rv = Poll.remove(sendfilePollset, data.socket);
    if (rv == Status.APR_SUCCESS) {
        sendfileCount--;
    }
    sendfileData.remove(Long.valueOf(data.socket));
}
 
Example #8
Source File: AprEndpoint.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Add specified socket to one of the pollers. Must only be called from
 * {@link Poller#run()}.
 */
protected boolean addToPoller(long socket, int events) {
    int rv = -1;
    for (int i = 0; i < pollers.length; i++) {
        if (pollerSpace[i] > 0) {
            rv = Poll.add(pollers[i], socket, events);
            if (rv == Status.APR_SUCCESS) {
                pollerSpace[i]--;
                connectionCount.incrementAndGet();
                return true;
            }
        }
    }
    return false;
}
 
Example #9
Source File: AprEndpoint.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Allocate a new poller of the specified size.
 */
protected long allocatePoller(int size, long pool, int timeout) {
    try {
        return Poll.create(size, pool, 0, timeout * 1000);
    } catch (Error e) {
        if (Status.APR_STATUS_IS_EINVAL(e.getError())) {
            log.info(sm.getString("endpoint.poll.limitedpollsize", "" + size));
            return 0;
        } else {
            log.error(sm.getString("endpoint.poll.initfail"), e);
            return -1;
        }
    }
}
 
Example #10
Source File: AjpAprProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Read at least the specified amount of bytes, and place them
 * in the input buffer.
 */
protected boolean readt(int n, boolean useAvailableData)
    throws IOException {

    if (useAvailableData && inputBuffer.remaining() == 0) {
        return false;
    }
    if (inputBuffer.capacity() - inputBuffer.limit() <=
            n - inputBuffer.remaining()) {
        inputBuffer.compact();
        inputBuffer.limit(inputBuffer.position());
        inputBuffer.position(0);
    }
    int nRead;
    while (inputBuffer.remaining() < n) {
        nRead = Socket.recvbb
            (socketWrapper.getSocket().longValue(), inputBuffer.limit(),
                inputBuffer.capacity() - inputBuffer.limit());
        if (nRead > 0) {
            inputBuffer.limit(inputBuffer.limit() + nRead);
        } else {
            if ((-nRead) == Status.ETIMEDOUT || (-nRead) == Status.TIMEUP) {
                return false;
            } else {
                throw new IOException(sm.getString("ajpprocessor.failedread"));
            }
        }
    }

    return true;

}
 
Example #11
Source File: AprEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Remove socket from the poller.
 *
 * @param data the sendfile data which should be removed
 */
protected void remove(SendfileData data) {
    int rv = Poll.remove(sendfilePollset, data.socket);
    if (rv == Status.APR_SUCCESS) {
        sendfileCount--;
    }
    sendfileData.remove(Long.valueOf(data.socket));
}
 
Example #12
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Allocate a new poller of the specified size.
 * @param size The size
 * @param pool The pool from which the poller will be allocated
 * @param timeout The timeout
 * @return the poller pointer
 */
protected long allocatePoller(int size, long pool, int timeout) {
    try {
        return Poll.create(size, pool, 0, timeout * 1000);
    } catch (Error e) {
        if (Status.APR_STATUS_IS_EINVAL(e.getError())) {
            log.info(sm.getString("endpoint.poll.limitedpollsize", "" + size));
            return 0;
        } else {
            log.error(sm.getString("endpoint.poll.initfail"), e);
            return -1;
        }
    }
}
 
Example #13
Source File: AprEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Allocate a new poller of the specified size.
 */
protected long allocatePoller(int size, long pool, int timeout) {
    try {
        return Poll.create(size, pool, 0, timeout * 1000);
    } catch (Error e) {
        if (Status.APR_STATUS_IS_EINVAL(e.getError())) {
            log.info(sm.getString("endpoint.poll.limitedpollsize", "" + size));
            return 0;
        } else {
            log.error(sm.getString("endpoint.poll.initfail"), e);
            return -1;
        }
    }
}
 
Example #14
Source File: AjpAprProcessor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Read at least the specified amount of bytes, and place them
 * in the input buffer.
 */
protected boolean readt(int n, boolean useAvailableData)
    throws IOException {

    if (useAvailableData && inputBuffer.remaining() == 0) {
        return false;
    }
    if (inputBuffer.capacity() - inputBuffer.limit() <=
            n - inputBuffer.remaining()) {
        inputBuffer.compact();
        inputBuffer.limit(inputBuffer.position());
        inputBuffer.position(0);
    }
    int nRead;
    while (inputBuffer.remaining() < n) {
        nRead = Socket.recvbb
            (socketWrapper.getSocket().longValue(), inputBuffer.limit(),
                inputBuffer.capacity() - inputBuffer.limit());
        if (nRead > 0) {
            inputBuffer.limit(inputBuffer.limit() + nRead);
        } else {
            if ((-nRead) == Status.ETIMEDOUT || (-nRead) == Status.TIMEUP) {
                return false;
            } else {
                throw new IOException(sm.getString("ajpprocessor.failedread"));
            }
        }
    }

    return true;

}
 
Example #15
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Remove socket from the poller.
 *
 * @param data the sendfile data which should be removed
 */
protected void remove(SendfileData data) {
    int rv = Poll.remove(sendfilePollset, data.socket);
    if (rv == Status.APR_SUCCESS) {
        sendfileCount--;
    }
    sendfileData.remove(Long.valueOf(data.socket));
}
 
Example #16
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Add specified socket to one of the pollers. Must only be called from
 * {@link Poller#run()}.
 */
private boolean addToPoller(long socket, int events) {
    int rv = -1;
    for (int i = 0; i < pollers.length; i++) {
        if (pollerSpace[i] > 0) {
            rv = Poll.add(pollers[i], socket, events);
            if (rv == Status.APR_SUCCESS) {
                pollerSpace[i]--;
                connectionCount.incrementAndGet();
                return true;
            }
        }
    }
    return false;
}
 
Example #17
Source File: AprServletInputStream.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
protected int doRead(boolean block, byte[] b, int off, int len)
        throws IOException {

    if (closed) {
        throw new IOException(sm.getString("apr.closed", Long.valueOf(socket)));
    }

    Lock readLock = wrapper.getBlockingStatusReadLock();
    WriteLock writeLock = wrapper.getBlockingStatusWriteLock();

    boolean readDone = false;
    int result = 0;
    try {
        readLock.lock();
        if (wrapper.getBlockingStatus() == block) {
            result = Socket.recv(socket, b, off, len);
            readDone = true;
        }
    } finally {
        readLock.unlock();
    }

    if (!readDone) {
        try {
            writeLock.lock();
            wrapper.setBlockingStatus(block);
            // Set the current settings for this socket
            Socket.optSet(socket, Socket.APR_SO_NONBLOCK, (block ? 0 : 1));
            // Downgrade the lock
            try {
                readLock.lock();
                writeLock.unlock();
                result = Socket.recv(socket, b, off, len);
            } finally {
                readLock.unlock();
            }
        } finally {
            // Should have been released above but may not have been on some
            // exception paths
            if (writeLock.isHeldByCurrentThread()) {
                writeLock.unlock();
            }
        }
    }

    if (result > 0) {
        eagain = false;
        return result;
    } else if (-result == Status.EAGAIN) {
        eagain = true;
        return 0;
    } else if (-result == Status.APR_EGENERAL && wrapper.isSecure()) {
        // Not entirely sure why this is necessary. Testing to date has not
        // identified any issues with this but log it so it can be tracked
        // if it is suspected of causing issues in the future.
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("apr.read.sslGeneralError",
                    Long.valueOf(socket), wrapper));
        }
        eagain = true;
        return 0;
    } else if (-result == Status.APR_EOF) {
        throw new EOFException(sm.getString("apr.clientAbort"));
    } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
            (-result == Status.APR_OS_START_SYSERR + 10053)) {
        // 10053 on Windows is connection aborted
        throw new EOFException(sm.getString("apr.clientAbort"));
    } else {
        throw new IOException(sm.getString("apr.read.error",
                Integer.valueOf(-result), Long.valueOf(socket), wrapper));
    }
}
 
Example #18
Source File: AprServletOutputStream.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private int doWriteInternal(byte[] b, int off, int len) throws IOException {

        int start = off;
        int left = len;
        int written;

        do {
            if (endpoint.isSSLEnabled()) {
                if (sslOutputBuffer.remaining() == 0) {
                    // Buffer was fully written last time around
                    sslOutputBuffer.clear();
                    if (left < SSL_OUTPUT_BUFFER_SIZE) {
                        sslOutputBuffer.put(b, start, left);
                    } else {
                        sslOutputBuffer.put(b, start, SSL_OUTPUT_BUFFER_SIZE);
                    }
                    sslOutputBuffer.flip();
                } else {
                    // Buffer still has data from previous attempt to write
                    // APR + SSL requires that exactly the same parameters are
                    // passed when re-attempting the write
                }
                written = Socket.sendb(socket, sslOutputBuffer,
                        sslOutputBuffer.position(), sslOutputBuffer.limit());
                if (written > 0) {
                    sslOutputBuffer.position(
                            sslOutputBuffer.position() + written);
                }
            } else {
                written = Socket.send(socket, b, start, left);
            }
            if (Status.APR_STATUS_IS_EAGAIN(-written)) {
                written = 0;
            } else if (-written == Status.APR_EOF) {
                throw new EOFException(sm.getString("apr.clientAbort"));
            } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
                    (-written == Status.APR_OS_START_SYSERR + 10053)) {
                // 10053 on Windows is connection aborted
                throw new EOFException(sm.getString("apr.clientAbort"));
            } else if (written < 0) {
                throw new IOException(sm.getString("apr.write.error",
                        Integer.valueOf(-written), Long.valueOf(socket), wrapper));
            }
            start += written;
            left -= written;
        } while (written > 0 && left > 0);

        if (left > 0) {
            endpoint.getPoller().add(socket, -1, false, true);
        }
        return len - left;
    }
 
Example #19
Source File: AprEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Add the sendfile data to the sendfile poller. Note that in most cases,
 * the initial non blocking calls to sendfile will return right away, and
 * will be handled asynchronously inside the kernel. As a result,
 * the poller will never be used.
 *
 * @param data containing the reference to the data which should be snet
 * @return true if all the data has been sent right away, and false
 *              otherwise
 */
public boolean add(SendfileData data) {
    // Initialize fd from data given
    try {
        data.fdpool = Socket.pool(data.socket);
        data.fd = File.open
            (data.fileName, File.APR_FOPEN_READ
             | File.APR_FOPEN_SENDFILE_ENABLED | File.APR_FOPEN_BINARY,
             0, data.fdpool);
        data.pos = data.start;
        // Set the socket to nonblocking mode
        Socket.timeoutSet(data.socket, 0);
        while (true) {
            long nw = Socket.sendfilen(data.socket, data.fd,
                                       data.pos, data.end - data.pos, 0);
            if (nw < 0) {
                if (!(-nw == Status.EAGAIN)) {
                    Pool.destroy(data.fdpool);
                    data.socket = 0;
                    return false;
                } else {
                    // Break the loop and add the socket to poller.
                    break;
                }
            } else {
                data.pos = data.pos + nw;
                if (data.pos >= data.end) {
                    // Entire file has been sent
                    Pool.destroy(data.fdpool);
                    // Set back socket to blocking mode
                    Socket.timeoutSet(
                            data.socket, getSoTimeout() * 1000);
                    return true;
                }
            }
        }
    } catch (Exception e) {
        log.warn(sm.getString("endpoint.sendfile.error"), e);
        return false;
    }
    // Add socket to the list. Newly added sockets will wait
    // at most for pollTime before being polled
    synchronized (this) {
        addS.add(data);
        this.notify();
    }
    return false;
}
 
Example #20
Source File: InternalAprInputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Fill the internal buffer using data from the underlying input stream.
 * 
 * @return false if at end of stream
 */
protected boolean fill()
    throws IOException {

    int nRead = 0;

    if (parsingHeader) {

        if (lastValid == buf.length) {
            throw new IllegalArgumentException
                (sm.getString("iib.requestheadertoolarge.error"));
        }

        bbuf.clear();
        nRead = Socket.recvbb(socket, 0, buf.length - lastValid);
        if (nRead > 0) {
            bbuf.limit(nRead);
            bbuf.get(buf, pos, nRead);
            lastValid = pos + nRead;
        } else {
            if ((-nRead) == Status.EAGAIN) {
                return false;
            } else {
                throw new IOException(sm.getString("iib.failedread"));
            }
        }

    } else {

        if (buf.length - end < 4500) {
            // In this case, the request header was really large, so we allocate a 
            // brand new one; the old one will get GCed when subsequent requests
            // clear all references
            buf = new byte[buf.length];
            end = 0;
        }
        pos = end;
        lastValid = pos;
        bbuf.clear();
        nRead = Socket.recvbb(socket, 0, buf.length - lastValid);
        if (nRead > 0) {
            bbuf.limit(nRead);
            bbuf.get(buf, pos, nRead);
            lastValid = pos + nRead;
        } else {
            if ((-nRead) == Status.ETIMEDOUT || (-nRead) == Status.TIMEUP) {
                throw new SocketTimeoutException(sm.getString("iib.failedread"));
            } else if (nRead == 0) {
                // APR_STATUS_IS_EOF, since native 1.1.22
                return false;
            } else if (-nRead == Status.APR_EGENERAL && wrapper.isSecure()) {
                // Not entirely sure why this is necessary. Testing to date has not
                // identified any issues with this but log it so it can be tracked
                // if it is suspected of causing issues in the future.
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("iib.apr.sslGeneralError",
                            Long.valueOf(socket), wrapper));
                }
            } else {
                throw new IOException(sm.getString("iib.failedread"));
            }
        }

    }

    return (nRead > 0);
}
 
Example #21
Source File: InternalAprInputBuffer.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Fill the internal buffer using data from the underlying input stream.
 *
 * @return false if at end of stream
 */
protected boolean fill()
    throws IOException {

    int nRead = 0;

    if (parsingHeader) {

        if (lastValid == buf.length) {
            throw new IllegalArgumentException
                (sm.getString("iib.requestheadertoolarge.error"));
        }

        bbuf.clear();
        nRead = Socket.recvbb(socket, 0, buf.length - lastValid);
        if (nRead > 0) {
            bbuf.limit(nRead);
            bbuf.get(buf, pos, nRead);
            lastValid = pos + nRead;
        } else {
            if ((-nRead) == Status.EAGAIN) {
                return false;
            } else {
                throw new IOException(sm.getString("iib.failedread"));
            }
        }

    } else {

        if (buf.length - end < 4500) {
            // In this case, the request header was really large, so we allocate a
            // brand new one; the old one will get GCed when subsequent requests
            // clear all references
            buf = new byte[buf.length];
            end = 0;
        }
        pos = end;
        lastValid = pos;
        bbuf.clear();
        nRead = Socket.recvbb(socket, 0, buf.length - lastValid);
        if (nRead > 0) {
            bbuf.limit(nRead);
            bbuf.get(buf, pos, nRead);
            lastValid = pos + nRead;
        } else {
            if ((-nRead) == Status.ETIMEDOUT || (-nRead) == Status.TIMEUP) {
                throw new SocketTimeoutException(sm.getString("iib.failedread"));
            } else if (nRead == 0) {
                // APR_STATUS_IS_EOF, since native 1.1.22
                return false;
            } else if (-nRead == Status.APR_EGENERAL && wrapper.isSecure()) {
                // Not entirely sure why this is necessary. Testing to date has not
                // identified any issues with this but log it so it can be tracked
                // if it is suspected of causing issues in the future.
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("iib.apr.sslGeneralError",
                            Long.valueOf(socket), wrapper));
                }
            } else {
                throw new IOException(sm.getString("iib.failedread"));
            }
        }

    }

    return (nRead > 0);
}
 
Example #22
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private void doWriteInternal(ByteBuffer from) throws IOException {
    int thisTime;

    do {
        thisTime = 0;
        if (getEndpoint().isSSLEnabled()) {
            if (sslOutputBuffer.remaining() == 0) {
                // Buffer was fully written last time around
                sslOutputBuffer.clear();
                transfer(from, sslOutputBuffer);
                sslOutputBuffer.flip();
            } else {
                // Buffer still has data from previous attempt to write
                // APR + SSL requires that exactly the same parameters are
                // passed when re-attempting the write
            }
            thisTime = Socket.sendb(getSocket().longValue(), sslOutputBuffer,
                    sslOutputBuffer.position(), sslOutputBuffer.limit());
            if (thisTime > 0) {
                sslOutputBuffer.position(sslOutputBuffer.position() + thisTime);
            }
        } else {
            thisTime = Socket.sendb(getSocket().longValue(), from, from.position(),
                    from.remaining());
            if (thisTime > 0) {
                from.position(from.position() + thisTime);
            }
        }
        if (Status.APR_STATUS_IS_EAGAIN(-thisTime)) {
            thisTime = 0;
        } else if (-thisTime == Status.APR_EOF) {
            throw new EOFException(sm.getString("socket.apr.clientAbort"));
        } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
                (-thisTime == Status.APR_OS_START_SYSERR + 10053)) {
            // 10053 on Windows is connection aborted
            throw new EOFException(sm.getString("socket.apr.clientAbort"));
        } else if (thisTime < 0) {
            throw new IOException(sm.getString("socket.apr.write.error",
                    Integer.valueOf(-thisTime), getSocket(), this));
        }
    } while ((thisTime > 0 || getBlockingStatus()) && from.hasRemaining());

    // If there is data left in the buffer the socket will be registered for
    // write further up the stack. This is to ensure the socket is only
    // registered for write once as both container and user code can trigger
    // write registration.
}
 
Example #23
Source File: AprServletOutputStream.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private int doWriteInternal(byte[] b, int off, int len) throws IOException {

        int start = off;
        int left = len;
        int written;

        do {
            if (endpoint.isSSLEnabled()) {
                if (sslOutputBuffer.remaining() == 0) {
                    // Buffer was fully written last time around
                    sslOutputBuffer.clear();
                    if (left < SSL_OUTPUT_BUFFER_SIZE) {
                        sslOutputBuffer.put(b, start, left);
                    } else {
                        sslOutputBuffer.put(b, start, SSL_OUTPUT_BUFFER_SIZE);
                    }
                    sslOutputBuffer.flip();
                } else {
                    // Buffer still has data from previous attempt to write
                    // APR + SSL requires that exactly the same parameters are
                    // passed when re-attempting the write
                }
                written = Socket.sendb(socket, sslOutputBuffer,
                        sslOutputBuffer.position(), sslOutputBuffer.limit());
                if (written > 0) {
                    sslOutputBuffer.position(
                            sslOutputBuffer.position() + written);
                }
            } else {
                written = Socket.send(socket, b, start, left);
            }
            if (Status.APR_STATUS_IS_EAGAIN(-written)) {
                written = 0;
            } else if (-written == Status.APR_EOF) {
                throw new EOFException(sm.getString("apr.clientAbort"));
            } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
                    (-written == Status.APR_OS_START_SYSERR + 10053)) {
                // 10053 on Windows is connection aborted
                throw new EOFException(sm.getString("apr.clientAbort"));
            } else if (written < 0) {
                throw new IOException(sm.getString("apr.write.error",
                        Integer.valueOf(-written), Long.valueOf(socket), wrapper));
            }
            start += written;
            left -= written;
        } while (written > 0 && left > 0);

        if (left > 0) {
            endpoint.getPoller().add(socket, -1, false, true);
        }
        return len - left;
    }
 
Example #24
Source File: AprServletInputStream.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
protected int doRead(boolean block, byte[] b, int off, int len)
        throws IOException {

    if (closed) {
        throw new IOException(sm.getString("apr.closed", Long.valueOf(socket)));
    }

    Lock readLock = wrapper.getBlockingStatusReadLock();
    WriteLock writeLock = wrapper.getBlockingStatusWriteLock();

    boolean readDone = false;
    int result = 0;
    try {
        readLock.lock();
        if (wrapper.getBlockingStatus() == block) {
            result = Socket.recv(socket, b, off, len);
            readDone = true;
        }
    } finally {
        readLock.unlock();
    }

    if (!readDone) {
        try {
            writeLock.lock();
            wrapper.setBlockingStatus(block);
            // Set the current settings for this socket
            Socket.optSet(socket, Socket.APR_SO_NONBLOCK, (block ? 0 : 1));
            // Downgrade the lock
            try {
                readLock.lock();
                writeLock.unlock();
                result = Socket.recv(socket, b, off, len);
            } finally {
                readLock.unlock();
            }
        } finally {
            // Should have been released above but may not have been on some
            // exception paths
            if (writeLock.isHeldByCurrentThread()) {
                writeLock.unlock();
            }
        }
    }

    if (result > 0) {
        eagain = false;
        return result;
    } else if (-result == Status.EAGAIN) {
        eagain = true;
        return 0;
    } else if (-result == Status.APR_EGENERAL && wrapper.isSecure()) {
        // Not entirely sure why this is necessary. Testing to date has not
        // identified any issues with this but log it so it can be tracked
        // if it is suspected of causing issues in the future.
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("apr.read.sslGeneralError",
                    Long.valueOf(socket), wrapper));
        }
        eagain = true;
        return 0;
    } else if (-result == Status.APR_EOF) {
        throw new EOFException(sm.getString("apr.clientAbort"));
    } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
            (-result == Status.APR_OS_START_SYSERR + 10053)) {
        // 10053 on Windows is connection aborted
        throw new EOFException(sm.getString("apr.clientAbort"));
    } else {
        throw new IOException(sm.getString("apr.read.error",
                Integer.valueOf(-result), Long.valueOf(socket), wrapper));
    }
}
 
Example #25
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private int fillReadBuffer(boolean block, ByteBuffer to) throws IOException {
    if (closed) {
        throw new IOException(sm.getString("socket.apr.closed", getSocket()));
    }

    Lock readLock = getBlockingStatusReadLock();
    WriteLock writeLock = getBlockingStatusWriteLock();

    boolean readDone = false;
    int result = 0;
    readLock.lock();
    try {
        if (getBlockingStatus() == block) {
            if (block) {
                Socket.timeoutSet(getSocket().longValue(), getReadTimeout() * 1000);
            }
            result = Socket.recvb(getSocket().longValue(), to, to.position(),
                    to.remaining());
            readDone = true;
        }
    } finally {
        readLock.unlock();
    }

    if (!readDone) {
        writeLock.lock();
        try {
            // Set the current settings for this socket
            setBlockingStatus(block);
            if (block) {
                Socket.timeoutSet(getSocket().longValue(), getReadTimeout() * 1000);
            } else {
                Socket.timeoutSet(getSocket().longValue(), 0);
            }
            // Downgrade the lock
            readLock.lock();
            try {
                writeLock.unlock();
                result = Socket.recvb(getSocket().longValue(), to, to.position(),
                        to.remaining());
            } finally {
                readLock.unlock();
            }
        } finally {
            // Should have been released above but may not have been on some
            // exception paths
            if (writeLock.isHeldByCurrentThread()) {
                writeLock.unlock();
            }
        }
    }

    if (result > 0) {
        to.position(to.position() + result);
        return result;
    } else if (result == 0 || -result == Status.EAGAIN) {
        return 0;
    } else if ((-result) == Status.ETIMEDOUT || (-result) == Status.TIMEUP) {
        if (block) {
            throw new SocketTimeoutException(sm.getString("iib.readtimeout"));
        } else {
            // Attempting to read from the socket when the poller
            // has not signalled that there is data to read appears
            // to behave like a blocking read with a short timeout
            // on OSX rather than like a non-blocking read. If no
            // data is read, treat the resulting timeout like a
            // non-blocking read that returned no data.
            return 0;
        }
    } else if (-result == Status.APR_EOF) {
        return -1;
    } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
            (-result == Status.APR_OS_START_SYSERR + 10053)) {
        // 10053 on Windows is connection aborted
        throw new EOFException(sm.getString("socket.apr.clientAbort"));
    } else {
        throw new IOException(sm.getString("socket.apr.read.error",
                Integer.valueOf(-result), getSocket(), this));
    }
}
 
Example #26
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Add the sendfile data to the sendfile poller. Note that in most cases,
 * the initial non blocking calls to sendfile will return right away, and
 * will be handled asynchronously inside the kernel. As a result,
 * the poller will never be used.
 *
 * @param data containing the reference to the data which should be snet
 * @return true if all the data has been sent right away, and false
 *              otherwise
 */
public SendfileState add(SendfileData data) {
    // Initialize fd from data given
    try {
        data.fdpool = Socket.pool(data.socket);
        data.fd = File.open
            (data.fileName, File.APR_FOPEN_READ
             | File.APR_FOPEN_SENDFILE_ENABLED | File.APR_FOPEN_BINARY,
             0, data.fdpool);
        // Set the socket to nonblocking mode
        Socket.timeoutSet(data.socket, 0);
        while (sendfileRunning) {
            long nw = Socket.sendfilen(data.socket, data.fd,
                                       data.pos, data.length, 0);
            if (nw < 0) {
                if (!(-nw == Status.EAGAIN)) {
                    Pool.destroy(data.fdpool);
                    data.socket = 0;
                    return SendfileState.ERROR;
                } else {
                    // Break the loop and add the socket to poller.
                    break;
                }
            } else {
                data.pos += nw;
                data.length -= nw;
                if (data.length == 0) {
                    // Entire file has been sent
                    Pool.destroy(data.fdpool);
                    // Set back socket to blocking mode
                    Socket.timeoutSet(data.socket, getConnectionTimeout() * 1000);
                    return SendfileState.DONE;
                }
            }
        }
    } catch (Exception e) {
        log.warn(sm.getString("endpoint.sendfile.error"), e);
        return SendfileState.ERROR;
    }
    // Add socket to the list. Newly added sockets will wait
    // at most for pollTime before being polled
    synchronized (this) {
        addS.add(data);
        this.notify();
    }
    return SendfileState.PENDING;
}
 
Example #27
Source File: AprEndpoint.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Add the sendfile data to the sendfile poller. Note that in most cases,
 * the initial non blocking calls to sendfile will return right away, and
 * will be handled asynchronously inside the kernel. As a result,
 * the poller will never be used.
 *
 * @param data containing the reference to the data which should be snet
 * @return true if all the data has been sent right away, and false
 *              otherwise
 */
public boolean add(SendfileData data) {
    // Initialize fd from data given
    try {
        data.fdpool = Socket.pool(data.socket);
        data.fd = File.open
            (data.fileName, File.APR_FOPEN_READ
             | File.APR_FOPEN_SENDFILE_ENABLED | File.APR_FOPEN_BINARY,
             0, data.fdpool);
        data.pos = data.start;
        // Set the socket to nonblocking mode
        Socket.timeoutSet(data.socket, 0);
        while (true) {
            long nw = Socket.sendfilen(data.socket, data.fd,
                                       data.pos, data.end - data.pos, 0);
            if (nw < 0) {
                if (!(-nw == Status.EAGAIN)) {
                    Pool.destroy(data.fdpool);
                    data.socket = 0;
                    return false;
                } else {
                    // Break the loop and add the socket to poller.
                    break;
                }
            } else {
                data.pos = data.pos + nw;
                if (data.pos >= data.end) {
                    // Entire file has been sent
                    Pool.destroy(data.fdpool);
                    // Set back socket to blocking mode
                    Socket.timeoutSet(
                            data.socket, getSoTimeout() * 1000);
                    return true;
                }
            }
        }
    } catch (Exception e) {
        log.warn(sm.getString("endpoint.sendfile.error"), e);
        return false;
    }
    // Add socket to the list. Newly added sockets will wait
    // at most for pollTime before being polled
    synchronized (this) {
        addS.add(data);
        this.notify();
    }
    return false;
}