All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.smartboot.http.client.impl.HttpResponseProtocol Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show newest version
/*******************************************************************************
 * Copyright (c) 2017-2021, org.smartboot. All rights reserved.
 * project name: smart-http
 * file name: HttpResponseProtocol.java
 * Date: 2021-02-04
 * Author: sandao ([email protected])
 ******************************************************************************/

package org.smartboot.http.client.impl;

import org.smartboot.http.client.decode.HeaderDecoder;
import org.smartboot.http.client.decode.HttpProtocolDecoder;
import org.smartboot.socket.Protocol;
import org.smartboot.socket.transport.AioSession;

import java.nio.ByteBuffer;

/**
 * @author 三刀([email protected])
 * @version V1.0 , 2021/2/2
 */
public class HttpResponseProtocol implements Protocol {

    public static final HeaderDecoder BODY_READY_DECODER = (byteBuffer, aioSession, response) -> null;
    public static final HeaderDecoder BODY_CONTINUE_DECODER = (byteBuffer, aioSession, response) -> null;

    private final HttpProtocolDecoder httpMethodDecoder = new HttpProtocolDecoder();

    @Override
    public Response decode(ByteBuffer buffer, AioSession session) {
        ResponseAttachment attachment = session.getAttachment();
        Response response = attachment.getResponse();
        HeaderDecoder decodeChain = attachment.getDecoder();
        if (decodeChain == null) {
            decodeChain = httpMethodDecoder;
        }

        // 数据还未就绪,继续读
        if (decodeChain == BODY_CONTINUE_DECODER) {
            attachment.setDecoder(BODY_READY_DECODER);
            return null;
        } else if (decodeChain == BODY_READY_DECODER) {
            return response;
        }

        decodeChain = decodeChain.decode(buffer, session, response);
        attachment.setDecoder(decodeChain);
        // 响应式流
        if (decodeChain == BODY_READY_DECODER) {
            return response;
        }
        if (buffer.remaining() == buffer.capacity()) {
            throw new RuntimeException("buffer is too small when decode " + decodeChain.getClass().getName() + " ," + response);
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy