<commit_before>package com.firefly.client.http2;

import com.firefly.codec.http2.frame.DataFrame;
import com.firefly.codec.http2.frame.ErrorCode;
import com.firefly.codec.http2.frame.HeadersFrame;
import com.firefly.codec.http2.frame.ResetFrame;
import com.firefly.codec.http2.model.HttpStatus;
import com.firefly.codec.http2.model.MetaData;
import com.firefly.codec.http2.model.MetaData.Request;
import com.firefly.codec.http2.stream.AbstractHTTP2OutputStream;
import com.firefly.codec.http2.stream.HTTPOutputStream;
import com.firefly.codec.http2.stream.Stream;
import com.firefly.utils.concurrent.Callback;
import com.firefly.utils.concurrent.Promise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HTTP2ClientResponseHandler extends Stream.Listener.Adapter {

    private static Logger log = LoggerFactory.getLogger("firefly-system");

    public static final String OUTPUT_STREAM_KEY = "_outputStream";
    public static final String RESPONSE_KEY = "_response";

    private final Request request;
    private final ClientHTTPHandler handler;
    private final HTTPClientConnection connection;

    public HTTP2ClientResponseHandler(Request request, ClientHTTPHandler handler, HTTPClientConnection connection) {
        this.request = request;
        this.handler = handler;
        this.connection = connection;
    }


    @Override
    public void onHeaders(final Stream stream, final HeadersFrame headersFrame) {
        // TODO wait local stream creating success.
        // System.out.println("Client received header: " + stream.toString() + ", " + headersFrame.toString());
        if (headersFrame.getMetaData() == null) {
            // System.out.println("Client received meta data is null");
            throw new IllegalArgumentException("the stream " + stream.getId() + " received a null meta data");
        }

        if (headersFrame.getMetaData().isResponse()) {
            final HTTPOutputStream output = getOutputStream(stream);
            final MetaData.Response response = (MetaData.Response) headersFrame.getMetaData();

            if (response.getStatus() == HttpStatus.CONTINUE_100) {
                handler.continueToSendData(request, response, output, connection);
            } else {
                stream.setAttribute(RESPONSE_KEY, response);
                handler.headerComplete(request, response, output, connection);
                if (headersFrame.isEndStream()) {
                    handler.messageComplete(request, response, output, connection);
                }
            }
        } else {
            if (headersFrame.isEndStream()) {
                final HTTPOutputStream output = getOutputStream(stream);
                final MetaData.Response response = getResponse(stream);
                response.setTrailerSupplier(() -> headersFrame.getMetaData().getFields());
                handler.contentComplete(request, response, output, connection);
                handler.messageComplete(request, response, output, connection);
            } else {
                throw new IllegalArgumentException("the stream " + stream.getId() + " received illegal meta data");
            }
        }

    }

    private HTTPOutputStream getOutputStream(Stream stream) {
        return (HTTPOutputStream) stream.getAttribute(OUTPUT_STREAM_KEY);
    }

    @Override
    public void onData(Stream stream, DataFrame dataFrame, Callback callback) {
        final HTTPOutputStream output = getOutputStream(stream);
        final MetaData.Response response = getResponse(stream);

        try {
            handler.content(dataFrame.getData(), request, response, output, connection);
            callback.succeeded();
        } catch (Throwable t) {
            callback.failed(t);
        }

        if (dataFrame.isEndStream()) {
            handler.contentComplete(request, response, output, connection);
            handler.messageComplete(request, response, output, connection);
        }
    }

    private MetaData.Response getResponse(Stream stream) {
        return (MetaData.Response) stream.getAttribute(RESPONSE_KEY);
    }

    @Override
    public void onReset(Stream stream, ResetFrame frame) {
        // System.out.println("Client received reset frame: " + stream + ", " + frame);
        final HTTPOutputStream output = getOutputStream(stream);
        final MetaData.Response response = getResponse(stream);

        ErrorCode errorCode = ErrorCode.from(frame.getError());
        String reason = errorCode == null ? "error=" + frame.getError() : errorCode.name().toLowerCase();
        int status = HttpStatus.INTERNAL_SERVER_ERROR_500;<commit_msg>[test]: test h2c<commit_after>package com.firefly.client.http2;

import com.firefly.codec.http2.frame.DataFrame;
import com.firefly.codec.http2.frame.ErrorCode;
import com.firefly.codec.http2.frame.HeadersFrame;
import com.firefly.codec.http2.frame.ResetFrame;
import com.firefly.codec.http2.model.HttpStatus;
import com.firefly.codec.http2.model.MetaData;
import com.firefly.codec.http2.model.MetaData.Request;
import com.firefly.codec.http2.stream.AbstractHTTP2OutputStream;
import com.firefly.codec.http2.stream.HTTPOutputStream;
import com.firefly.codec.http2.stream.Stream;
import com.firefly.utils.concurrent.Callback;
import com.firefly.utils.concurrent.Promise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HTTP2ClientResponseHandler extends Stream.Listener.Adapter {

    private static Logger log = LoggerFactory.getLogger("firefly-system");

    public static final String OUTPUT_STREAM_KEY = "_outputStream";
    public static final String RESPONSE_KEY = "_response";

    private final Request request;
    private final ClientHTTPHandler handler;
    private final HTTPClientConnection connection;

    public HTTP2ClientResponseHandler(Request request, ClientHTTPHandler handler, HTTPClientConnection connection) {
        this.request = request;
        this.handler = handler;
        this.connection = connection;
    }


    @Override
    public void onHeaders(final Stream stream, final HeadersFrame headersFrame) {
        // TODO wait local stream creating success.
         System.out.println("Client received header: " + stream.toString() + ", " + headersFrame.toString());
        if (headersFrame.getMetaData() == null) {
            // System.out.println("Client received meta data is null");
            throw new IllegalArgumentException("the stream " + stream.getId() + " received a null meta data");
        }

        if (headersFrame.getMetaData().isResponse()) {
            final HTTPOutputStream output = getOutputStream(stream);
            final MetaData.Response response = (MetaData.Response) headersFrame.getMetaData();

            if (response.getStatus() == HttpStatus.CONTINUE_100) {
                handler.continueToSendData(request, response, output, connection);
            } else {
                stream.setAttribute(RESPONSE_KEY, response);
                handler.headerComplete(request, response, output, connection);
                if (headersFrame.isEndStream()) {
                    handler.messageComplete(request, response, output, connection);
                }
            }
        } else {
            if (headersFrame.isEndStream()) {
                final HTTPOutputStream output = getOutputStream(stream);
                final MetaData.Response response = getResponse(stream);
                response.setTrailerSupplier(() -> headersFrame.getMetaData().getFields());
                handler.contentComplete(request, response, output, connection);
                handler.messageComplete(request, response, output, connection);
            } else {
                throw new IllegalArgumentException("the stream " + stream.getId() + " received illegal meta data");
            }
        }

    }

    private HTTPOutputStream getOutputStream(Stream stream) {
        return (HTTPOutputStream) stream.getAttribute(OUTPUT_STREAM_KEY);
    }

    @Override
    public void onData(Stream stream, DataFrame dataFrame, Callback callback) {
        System.out.println("Client received: " + stream + ", " + dataFrame);
        final HTTPOutputStream output = getOutputStream(stream);
        final MetaData.Response response = getResponse(stream);

        try {
            handler.content(dataFrame.getData(), request, response, output, connection);
            callback.succeeded();
        } catch (Throwable t) {
            callback.failed(t);
        }

        if (dataFrame.isEndStream()) {
            handler.contentComplete(request, response, output, connection);
            handler.messageComplete(request, response, output, connection);
        }
    }

    private MetaData.Response getResponse(Stream stream) {
        return (MetaData.Response) stream.getAttribute(RESPONSE_KEY);
    }

    @Override
    public void onReset(Stream stream, ResetFrame frame) {
        // System.out.println("Client received reset frame: " + stream + ", " + frame);
        final HTTPOutputStream output = getOutputStream(stream);
        final MetaData.Response response = getResponse(stream);

        ErrorCode errorCode = ErrorCode.from(frame.getError());
        String reason = errorCode == null ? "error=" + frame.getError() : errorCode.name().toLowerCase();
        int status = HttpStatus.INTERNAL_SERVER_ERROR_500;
{'fraction_non_alphanumeric': 0.09875882730579927, 'fraction_numerical': 0.004814894072330409, 'mean_word_length': 3.0236762806715456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7062375', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2306, 'n_words': 533}