Package io.undertow.server.protocol.framed

Examples of io.undertow.server.protocol.framed.SendFrameHeader


        //first deal with flow control
        if (newFrame instanceof Http2StreamSinkChannel) {
            if(newFrame.isBroken()) {
                return true; //just quietly drop the frame
            }
            SendFrameHeader header = ((Http2StreamSinkChannel) newFrame).generateSendFrameHeader();
            //if no header is generated then flow control means we can't send anything
            if (header.getByteBuffer() == null) {
                //we clear the header, as we want to generate a new real header when the flow control window is updated
                ((Http2StreamSinkChannel) newFrame).clearHeader();
                return false;
            }
        }
View Full Code Here


    public void frameAdded(AbstractHttp2StreamSinkChannel addedFrame, List<AbstractHttp2StreamSinkChannel> pendingFrames, Deque<AbstractHttp2StreamSinkChannel> holdFrames) {
        Iterator<AbstractHttp2StreamSinkChannel> it = holdFrames.iterator();
        while (it.hasNext()) {
            AbstractHttp2StreamSinkChannel pending = it.next();
            if (pending instanceof Http2StreamSinkChannel) {
                SendFrameHeader header = ((Http2StreamSinkChannel) pending).generateSendFrameHeader();
                if (header.getByteBuffer() != null) {
                    pendingFrames.add(pending);
                    it.remove();
                } else {
                    //we clear the header, as we want to generate a new real header when the flow control window is updated
                    ((Http2StreamSinkChannel) pending).clearHeader();
View Full Code Here

    public static AjpClientFramePriority INSTANCE = new AjpClientFramePriority();

    @Override
    public boolean insertFrame(AbstractAjpClientStreamSinkChannel newFrame, List<AbstractAjpClientStreamSinkChannel> pendingFrames) {
        if(newFrame instanceof AjpClientRequestClientStreamSinkChannel) {
            SendFrameHeader header = ((AjpClientRequestClientStreamSinkChannel) newFrame).generateSendFrameHeader();
            if(header.getByteBuffer() == null) {
                //we clear the header, as we want to generate a new real header when the flow control window is updated
                ((AjpClientRequestClientStreamSinkChannel) newFrame).clearHeader();
                return false;
            }
        }
View Full Code Here

    public void frameAdded(AbstractAjpClientStreamSinkChannel addedFrame, List<AbstractAjpClientStreamSinkChannel> pendingFrames, Deque<AbstractAjpClientStreamSinkChannel> holdFrames) {
        Iterator<AbstractAjpClientStreamSinkChannel> it = holdFrames.iterator();
        while (it.hasNext()){
            AbstractAjpClientStreamSinkChannel pending = it.next();
            if(pending instanceof AjpClientRequestClientStreamSinkChannel) {
                SendFrameHeader header = ((AjpClientRequestClientStreamSinkChannel) pending).generateSendFrameHeader();
                if(header.getByteBuffer() != null) {
                    pendingFrames.add(pending);
                    it.remove();
                } else {
                    //we clear the header, as we want to generate a new real header when the flow control window is updated
                    ((AjpClientRequestClientStreamSinkChannel) pending).clearHeader();
View Full Code Here

    @Override
    protected SendFrameHeader createFrameHeaderImpl() {

        int fcWindow = grabFlowControlBytes(getBuffer().remaining());
        if (fcWindow == 0 && getBuffer().hasRemaining()) {
            return new SendFrameHeader(getBuffer().remaining(), null);
        }
        final boolean finalFrame = isWritesShutdown() && fcWindow >= getBuffer().remaining();
        Pooled<ByteBuffer> firstHeaderBuffer = getChannel().getBufferPool().allocate();
        Pooled<ByteBuffer>[] allHeaderBuffers = null;
        ByteBuffer firstBuffer = firstHeaderBuffer.getResource();
        boolean firstFrame = false;
        if (first) {
            firstFrame = true;
            first = false;
            int firstInt = SpdyChannel.CONTROL_FRAME | (getChannel().getSpdyVersion() << 16) | 1;
            SpdyProtocolUtils.putInt(firstBuffer, firstInt);
            SpdyProtocolUtils.putInt(firstBuffer, 0); //we back fill the length
            HeaderMap headers = this.headers;

            SpdyProtocolUtils.putInt(firstBuffer, getStreamId());
            SpdyProtocolUtils.putInt(firstBuffer, 0);
            firstBuffer.put((byte) 0);
            firstBuffer.put((byte) 0);


            headers.remove(Headers.CONNECTION); //todo: should this be here?
            headers.remove(Headers.KEEP_ALIVE);
            headers.remove(Headers.TRANSFER_ENCODING);

            allHeaderBuffers = createHeaderBlock(firstHeaderBuffer, allHeaderBuffers, firstBuffer, headers);
        }
        Pooled<ByteBuffer> currentPooled = allHeaderBuffers == null ? firstHeaderBuffer : allHeaderBuffers[allHeaderBuffers.length - 1];
        ByteBuffer currentBuffer = currentPooled.getResource();
        int remainingInBuffer = 0;
        if (getBuffer().remaining() > 0) {
            remainingInBuffer = getBuffer().remaining() - fcWindow;
            getBuffer().limit(getBuffer().position() + fcWindow);
            if (currentBuffer.remaining() < 8) {
                allHeaderBuffers = allocateAll(allHeaderBuffers, currentPooled);
                currentPooled = allHeaderBuffers[allHeaderBuffers.length - 1];
                currentBuffer = currentPooled.getResource();
            }
            SpdyProtocolUtils.putInt(currentBuffer, getStreamId());
            SpdyProtocolUtils.putInt(currentBuffer, ((finalFrame ? SpdyChannel.FLAG_FIN : 0) << 24) + fcWindow);
        } else if(finalFrame && !firstFrame) {
            SpdyProtocolUtils.putInt(currentBuffer, getStreamId());
            SpdyProtocolUtils.putInt(currentBuffer, SpdyChannel.FLAG_FIN  << 24);
        }
        if (allHeaderBuffers == null) {
            //only one buffer required
            currentBuffer.flip();
            return new SendFrameHeader(remainingInBuffer, currentPooled);
        } else {
            //headers were too big to fit in one buffer
            //for now we will just copy them into a big buffer
            int length = 0;
            for (int i = 0; i < allHeaderBuffers.length; ++i) {
                length += allHeaderBuffers[i].getResource().position();
                allHeaderBuffers[i].getResource().flip();
            }
            try {
                ByteBuffer newBuf = ByteBuffer.allocate(length);
                for (int i = 0; i < allHeaderBuffers.length; ++i) {
                    newBuf.put(allHeaderBuffers[i].getResource());
                }
                newBuf.flip();
                return new SendFrameHeader(remainingInBuffer, new ImmediatePooled<ByteBuffer>(newBuf));
            } finally {
                //the allocate can oome
                for (int i = 0; i < allHeaderBuffers.length; ++i) {
                    allHeaderBuffers[i].free();
                }
View Full Code Here

    private SendFrameHeader createFrameHeaderImpl() {
        if(discardMode) {
            getBuffer().clear();
            getBuffer().flip();
            return new SendFrameHeader(new ImmediatePooled<>(ByteBuffer.wrap(new byte[0])));
        }
        Pooled<ByteBuffer> pooledHeaderBuffer = getChannel().getBufferPool().allocate();
        final ByteBuffer buffer = pooledHeaderBuffer.getResource();
        ByteBuffer dataBuffer = getBuffer();
        int dataInBuffer = dataBuffer.remaining();
        if (!firstFrameWritten && requestedChunkSize == 0) {
            //we are waiting on a read body chunk
            return new SendFrameHeader(dataInBuffer, null);
        }

        if (!firstFrameWritten) {
            String contentLength = headers.getFirst(Headers.CONTENT_LENGTH);
            if (contentLength != null) {
                dataSize = Long.parseLong(contentLength);
                requestedChunkSize = MAX_DATA_SIZE;
                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 = MAX_DATA_SIZE;
            } 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);
            buffer.put((byte) 0); //we fill the size in later
            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) {
                buffer.put((byte) ATTR_QUERY_STRING); //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, MAX_DATA_SIZE);
            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.free();
                return new SendFrameHeader(dataInBuffer, null, true);
            }
            dataBuffer.limit(dataBuffer.position());
            return new SendFrameHeader(dataInBuffer, pooledHeaderBuffer, true);
        }
    }
View Full Code Here

        }
    }

    @Override
    protected final SendFrameHeader createFrameHeader() {
        SendFrameHeader header = this.header;
        this.header = null;
        return header;
    }
View Full Code Here

        SpdyProtocolUtils.putInt(buf, firstInt);
        SpdyProtocolUtils.putInt(buf, 8);
        SpdyProtocolUtils.putInt(buf, streamId);
        SpdyProtocolUtils.putInt(buf, statusCode);
        buf.flip();
        return new SendFrameHeader(new ImmediatePooled<>(buf));
    }
View Full Code Here

        SpdyProtocolUtils.putInt(buf, firstInt);
        SpdyProtocolUtils.putInt(buf, 8);
        SpdyProtocolUtils.putInt(buf, streamId);
        SpdyProtocolUtils.putInt(buf, deltaWindowSize);
        buf.flip();
        return new SendFrameHeader(new ImmediatePooled<>(buf));
    }
View Full Code Here

        }
    }

    @Override
    protected final SendFrameHeader createFrameHeader() {
        SendFrameHeader header = this.header;
        this.header = null;
        return header;
    }
View Full Code Here

TOP

Related Classes of io.undertow.server.protocol.framed.SendFrameHeader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.