Search in sources :

Example 6 with SendFrameHeader

use of io.undertow.server.protocol.framed.SendFrameHeader in project undertow by undertow-io.

the class AjpClientRequestClientStreamSinkChannel method createFrameHeaderImpl.

private SendFrameHeader createFrameHeaderImpl() {
    if (discardMode) {
        getBuffer().clear();
        getBuffer().flip();
        return new SendFrameHeader(new ImmediatePooledByteBuffer(ByteBuffer.wrap(new byte[0])));
    }
    PooledByteBuffer pooledHeaderBuffer = getChannel().getBufferPool().allocate();
    try {
        final ByteBuffer buffer = pooledHeaderBuffer.getBuffer();
        ByteBuffer dataBuffer = getBuffer();
        int dataInBuffer = dataBuffer.remaining();
        if (!firstFrameWritten && requestedChunkSize == 0) {
            //we are waiting on a read body chunk
            return new SendFrameHeader(dataInBuffer, null);
        }
        int maxData = getChannel().getSettings().get(UndertowOptions.MAX_AJP_PACKET_SIZE, DEFAULT_MAX_DATA_SIZE) - 6;
        if (!firstFrameWritten) {
            String contentLength = headers.getFirst(Headers.CONTENT_LENGTH);
            if (contentLength != null) {
                dataSize = Long.parseLong(contentLength);
                requestedChunkSize = maxData;
                if (dataInBuffer > dataSize) {
                    throw UndertowMessages.MESSAGES.fixedLengthOverflow();
                }
            } else if (isWritesShutdown() && !headers.contains(Headers.TRANSFER_ENCODING)) {
                //writes are shut down, go to fixed length
                headers.put(Headers.CONTENT_LENGTH, dataInBuffer);
                dataSize = dataInBuffer;
                requestedChunkSize = maxData;
            } else {
                headers.put(Headers.TRANSFER_ENCODING, Headers.CHUNKED.toString());
                dataSize = -1;
                requestedChunkSize = 0;
            }
            firstFrameWritten = true;
            final String path;
            final String queryString;
            int qsIndex = this.path.indexOf('?');
            if (qsIndex == -1) {
                path = this.path;
                queryString = null;
            } else {
                path = this.path.substring(0, qsIndex);
                queryString = this.path.substring(qsIndex + 1);
            }
            buffer.put((byte) 0x12);
            buffer.put((byte) 0x34);
            //we fill the size in later
            buffer.put((byte) 0);
            buffer.put((byte) 0);
            buffer.put((byte) 2);
            boolean storeMethod = false;
            Integer methodNp = AjpConstants.HTTP_METHODS_MAP.get(method);
            if (methodNp == null) {
                methodNp = 0xFF;
                storeMethod = true;
            }
            buffer.put((byte) (int) methodNp);
            AjpUtils.putHttpString(buffer, protocol);
            putString(buffer, path);
            putString(buffer, notNull(attachable.getAttachment(ProxiedRequestAttachments.REMOTE_ADDRESS)));
            putString(buffer, notNull(attachable.getAttachment(ProxiedRequestAttachments.REMOTE_HOST)));
            putString(buffer, notNull(attachable.getAttachment(ProxiedRequestAttachments.SERVER_NAME)));
            AjpUtils.putInt(buffer, notNull(attachable.getAttachment(ProxiedRequestAttachments.SERVER_PORT)));
            buffer.put((byte) (notNull(attachable.getAttachment(ProxiedRequestAttachments.IS_SSL)) ? 1 : 0));
            int headers = 0;
            //we need to count the headers
            final HeaderMap responseHeaders = this.headers;
            for (HttpString name : responseHeaders.getHeaderNames()) {
                headers += responseHeaders.get(name).size();
            }
            AjpUtils.putInt(buffer, headers);
            for (final HttpString header : responseHeaders.getHeaderNames()) {
                for (String headerValue : responseHeaders.get(header)) {
                    Integer headerCode = AjpConstants.HEADER_MAP.get(header);
                    if (headerCode != null) {
                        AjpUtils.putInt(buffer, headerCode);
                    } else {
                        AjpUtils.putHttpString(buffer, header);
                    }
                    putString(buffer, headerValue);
                }
            }
            if (queryString != null) {
                //query_string
                buffer.put((byte) ATTR_QUERY_STRING);
                putString(buffer, queryString);
            }
            String remoteUser = attachable.getAttachment(ProxiedRequestAttachments.REMOTE_USER);
            if (remoteUser != null) {
                buffer.put((byte) ATTR_REMOTE_USER);
                putString(buffer, remoteUser);
            }
            String authType = attachable.getAttachment(ProxiedRequestAttachments.AUTH_TYPE);
            if (authType != null) {
                buffer.put((byte) ATTR_AUTH_TYPE);
                putString(buffer, authType);
            }
            String route = attachable.getAttachment(ProxiedRequestAttachments.ROUTE);
            if (route != null) {
                buffer.put((byte) ATTR_ROUTE);
                putString(buffer, route);
            }
            String sslCert = attachable.getAttachment(ProxiedRequestAttachments.SSL_CERT);
            if (sslCert != null) {
                buffer.put((byte) ATTR_SSL_CERT);
                putString(buffer, sslCert);
            }
            String sslCypher = attachable.getAttachment(ProxiedRequestAttachments.SSL_CYPHER);
            if (sslCypher != null) {
                buffer.put((byte) ATTR_SSL_CIPHER);
                putString(buffer, sslCypher);
            }
            byte[] sslSession = attachable.getAttachment(ProxiedRequestAttachments.SSL_SESSION_ID);
            if (sslSession != null) {
                buffer.put((byte) ATTR_SSL_SESSION);
                putString(buffer, FlexBase64.encodeString(sslSession, false));
            }
            Integer sslKeySize = attachable.getAttachment(ProxiedRequestAttachments.SSL_KEY_SIZE);
            if (sslKeySize != null) {
                buffer.put((byte) ATTR_SSL_KEY_SIZE);
                putString(buffer, sslKeySize.toString());
            }
            String secret = attachable.getAttachment(ProxiedRequestAttachments.SECRET);
            if (secret != null) {
                buffer.put((byte) ATTR_SECRET);
                putString(buffer, secret);
            }
            if (storeMethod) {
                buffer.put((byte) ATTR_STORED_METHOD);
                putString(buffer, method.toString());
            }
            buffer.put((byte) 0xFF);
            int dataLength = buffer.position() - 4;
            buffer.put(2, (byte) ((dataLength >> 8) & 0xFF));
            buffer.put(3, (byte) (dataLength & 0xFF));
        }
        if (dataSize == 0) {
            //no data, just write out this frame and we are done
            buffer.flip();
            return new SendFrameHeader(pooledHeaderBuffer);
        } else if (requestedChunkSize > 0) {
            if (isWritesShutdown() && dataInBuffer == 0) {
                buffer.put((byte) 0x12);
                buffer.put((byte) 0x34);
                buffer.put((byte) 0x00);
                buffer.put((byte) 0x02);
                buffer.put((byte) 0x00);
                buffer.put((byte) 0x00);
                buffer.flip();
                return new SendFrameHeader(pooledHeaderBuffer);
            }
            int remaining = dataInBuffer;
            remaining = Math.min(remaining, maxData);
            remaining = Math.min(remaining, requestedChunkSize);
            int bodySize = remaining + 2;
            buffer.put((byte) 0x12);
            buffer.put((byte) 0x34);
            buffer.put((byte) ((bodySize >> 8) & 0xFF));
            buffer.put((byte) (bodySize & 0xFF));
            buffer.put((byte) ((remaining >> 8) & 0xFF));
            buffer.put((byte) (remaining & 0xFF));
            requestedChunkSize = 0;
            if (remaining < dataInBuffer) {
                dataBuffer.limit(getBuffer().position() + remaining);
                buffer.flip();
                return new SendFrameHeader(dataInBuffer - remaining, pooledHeaderBuffer, dataSize < 0);
            } else {
                buffer.flip();
                return new SendFrameHeader(0, pooledHeaderBuffer, dataSize < 0);
            }
        } else {
            //chunked. We just write the headers, and leave all the data in the buffer
            //they need to send us a read body chunk in order to get any data
            buffer.flip();
            if (buffer.remaining() == 0) {
                pooledHeaderBuffer.close();
                return new SendFrameHeader(dataInBuffer, null, true);
            }
            dataBuffer.limit(dataBuffer.position());
            return new SendFrameHeader(dataInBuffer, pooledHeaderBuffer, true);
        }
    } catch (BufferOverflowException e) {
        //TODO: UNDERTOW-901
        pooledHeaderBuffer.close();
        markBroken();
        throw e;
    }
}
Also used : HeaderMap(io.undertow.util.HeaderMap) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) AjpUtils.putString(io.undertow.protocols.ajp.AjpUtils.putString) HttpString(io.undertow.util.HttpString) BufferOverflowException(java.nio.BufferOverflowException) SendFrameHeader(io.undertow.server.protocol.framed.SendFrameHeader) ByteBuffer(java.nio.ByteBuffer) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) HttpString(io.undertow.util.HttpString)

Example 7 with SendFrameHeader

use of io.undertow.server.protocol.framed.SendFrameHeader in project undertow by undertow-io.

the class Http2GoAwayStreamSinkChannel method createFrameHeader.

@Override
protected SendFrameHeader createFrameHeader() {
    ByteBuffer buf = ByteBuffer.allocate(17);
    Http2ProtocolUtils.putInt(buf, HEADER_FIRST_LINE);
    buf.put((byte) 0);
    //stream id
    Http2ProtocolUtils.putInt(buf, 0);
    Http2ProtocolUtils.putInt(buf, lastGoodStreamId);
    Http2ProtocolUtils.putInt(buf, status);
    buf.flip();
    return new SendFrameHeader(new ImmediatePooledByteBuffer(buf));
}
Also used : ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) ByteBuffer(java.nio.ByteBuffer) SendFrameHeader(io.undertow.server.protocol.framed.SendFrameHeader) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer)

Example 8 with SendFrameHeader

use of io.undertow.server.protocol.framed.SendFrameHeader in project undertow by undertow-io.

the class Http2PingStreamSinkChannel method createFrameHeader.

@Override
protected SendFrameHeader createFrameHeader() {
    ByteBuffer buf = ByteBuffer.allocate(17);
    Http2ProtocolUtils.putInt(buf, HEADER);
    buf.put((byte) (ack ? Http2Channel.PING_FLAG_ACK : 0));
    //stream id, must be zero
    Http2ProtocolUtils.putInt(buf, 0);
    for (int i = 0; i < PING_FRAME_LENGTH; ++i) {
        buf.put(data[i]);
    }
    buf.flip();
    return new SendFrameHeader(new ImmediatePooledByteBuffer(buf));
}
Also used : ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) ByteBuffer(java.nio.ByteBuffer) SendFrameHeader(io.undertow.server.protocol.framed.SendFrameHeader) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer)

Example 9 with SendFrameHeader

use of io.undertow.server.protocol.framed.SendFrameHeader in project undertow by undertow-io.

the class Http2RstStreamSinkChannel method createFrameHeader.

@Override
protected SendFrameHeader createFrameHeader() {
    ByteBuffer buf = ByteBuffer.allocate(13);
    Http2ProtocolUtils.putInt(buf, HEADER_FIRST_LINE);
    buf.put((byte) 0);
    Http2ProtocolUtils.putInt(buf, streamId);
    Http2ProtocolUtils.putInt(buf, errorCode);
    buf.flip();
    return new SendFrameHeader(new ImmediatePooledByteBuffer(buf));
}
Also used : ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) ByteBuffer(java.nio.ByteBuffer) SendFrameHeader(io.undertow.server.protocol.framed.SendFrameHeader) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer)

Example 10 with SendFrameHeader

use of io.undertow.server.protocol.framed.SendFrameHeader in project undertow by undertow-io.

the class Http2SettingsStreamSinkChannel method createFrameHeaderImpl.

@Override
protected SendFrameHeader createFrameHeaderImpl() {
    PooledByteBuffer pooled = getChannel().getBufferPool().allocate();
    ByteBuffer currentBuffer = pooled.getBuffer();
    if (settings != null) {
        int size = settings.size() * 6;
        currentBuffer.put((byte) ((size >> 16) & 0xFF));
        currentBuffer.put((byte) ((size >> 8) & 0xFF));
        currentBuffer.put((byte) (size & 0xFF));
        //type
        currentBuffer.put((byte) Http2Channel.FRAME_TYPE_SETTINGS);
        //flags
        currentBuffer.put((byte) 0);
        Http2ProtocolUtils.putInt(currentBuffer, getStreamId());
        for (Http2Setting setting : settings) {
            currentBuffer.put((byte) ((setting.getId() >> 8) & 0xFF));
            currentBuffer.put((byte) (setting.getId() & 0xFF));
            currentBuffer.put((byte) ((setting.getValue() >> 24) & 0xFF));
            currentBuffer.put((byte) ((setting.getValue() >> 16) & 0xFF));
            currentBuffer.put((byte) ((setting.getValue() >> 8) & 0xFF));
            currentBuffer.put((byte) (setting.getValue() & 0xFF));
        }
    } else {
        currentBuffer.put((byte) 0);
        currentBuffer.put((byte) 0);
        currentBuffer.put((byte) 0);
        //type
        currentBuffer.put((byte) Http2Channel.FRAME_TYPE_SETTINGS);
        //flags
        currentBuffer.put((byte) Http2Channel.SETTINGS_FLAG_ACK);
        Http2ProtocolUtils.putInt(currentBuffer, getStreamId());
    }
    currentBuffer.flip();
    return new SendFrameHeader(pooled);
}
Also used : PooledByteBuffer(io.undertow.connector.PooledByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ByteBuffer(java.nio.ByteBuffer) SendFrameHeader(io.undertow.server.protocol.framed.SendFrameHeader)

Aggregations

SendFrameHeader (io.undertow.server.protocol.framed.SendFrameHeader)13 ByteBuffer (java.nio.ByteBuffer)8 ImmediatePooledByteBuffer (io.undertow.util.ImmediatePooledByteBuffer)7 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)4 AjpUtils.putString (io.undertow.protocols.ajp.AjpUtils.putString)1 HeaderMap (io.undertow.util.HeaderMap)1 HttpString (io.undertow.util.HttpString)1 BufferOverflowException (java.nio.BufferOverflowException)1