package com.firefly.codec.http2.encode;

import com.firefly.codec.http2.model.*;
import com.firefly.utils.io.BufferUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * @author Pengtao Qiu
 */
abstract public class PredefinedHTTP1Response {

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

    public static final MetaData.Response H2C_RESPONSE = new MetaData.Response(HttpVersion.HTTP_1_1, 101, new HttpFields());
    public static byte[] H2C_BYTES;
    public static byte[] CONTINUE_100_BYTES;

    static {
        H2C_RESPONSE.getFields().put(HttpHeader.CONNECTION, HttpHeaderValue.UPGRADE);
        H2C_RESPONSE.getFields().put(HttpHeader.UPGRADE, "h2c");


        try {
            ByteBuffer header = BufferUtils.allocate(128);
            HttpGenerator gen = new HttpGenerator(true, true);
            HttpGenerator.Result result = gen.generateResponse(H2C_RESPONSE, false, header, null, null, true);
            if (result == HttpGenerator.Result.FLUSH && gen.getState() == HttpGenerator.State.COMPLETING) {
                H2C_BYTES = BufferUtils.toArray(header);
            } else {
                log.error("generate h2c error. {}, {}", result, gen.getState());
            }
        } catch (IOException e) {
            log.error("generate h2c response exception", e);
        }

        try {
            ByteBuffer header = BufferUtils.allocate(128);
            HttpGenerator gen = new HttpGenerator(true, true);
            HttpGenerator.Result result = gen.generateResponse(HttpGenerator.CONTINUE_100_INFO, false, header, null, null, false);
            if (result == HttpGenerator.Result.FLUSH && gen.getState() == HttpGenerator.State.COMPLETING_1XX) {
                CONTINUE_100_BYTES = BufferUtils.toArray(header);
            } else {
                log.error("generate 100-continue error. {}, {}", result, gen.getState());
            }
        } catch (IOException e) {
            log.error("generate 100-continue response exception", e);
        }
    }
}

@@ -74,16 +74,108 @@
 odel.*;%0A
+import com.firefly.utils.Assert;%0Aimport com.firefly.utils.exception.CommonRuntimeException;%0A
 import c
@@ -508,16 +508,107 @@
 c final 
+byte%5B%5D H2C_BYTES;%0A    public static final byte%5B%5D CONTINUE_100_BYTES;%0A%0A    static %7B%0A        
 MetaData
@@ -704,103 +704,8 @@
 ));%0A
-    public static byte%5B%5D H2C_BYTES;%0A    public static byte%5B%5D CONTINUE_100_BYTES;%0A%0A    static %7B%0A
     
@@ -852,17 +852,16 @@
 h2c%22);%0A%0A
-%0A
         
@@ -1103,35 +1103,44 @@
 e);%0A            
-if 
+Assert.state
 (result == HttpG
@@ -1212,19 +1212,17 @@
 MPLETING
-) %7B
+,
 %0A       
@@ -1234,307 +1234,106 @@
     
-H2C_BYTES = BufferUtils.toArray(header);%0A            %7D else %7B%0A                log.error(%22generate h2c error. %7B%7D, %7B%7D%22, result, gen.getState());%0A            %7D%0A        %7D catch (IOException e) %7B%0A            log.error(%22generate h2c response exception%22, e);%0A        %7D%0A%0A        try %7B%0A            ByteBuffer
+    %22generate h2c bytes error%22);%0A            H2C_BYTES = BufferUtils.toArray(header);%0A%0A           
  hea
@@ -1373,38 +1373,24 @@
             
-HttpGenerator 
 gen = new Ht
@@ -1426,37 +1426,16 @@
         
-HttpGenerator.Result 
 result =
@@ -1540,11 +1540,20 @@
     
-if 
+Assert.state
 (res
@@ -1641,24 +1641,70 @@
 TING_1XX
-) %7B
+,
 %0A    
+                %22generate continue 100 error%22);%0A
         
@@ -1769,25 +1769,34 @@
     
-    %7D else %7B%0A    
+%7D catch (IOException e) %7B%0A
     
@@ -1827,104 +1827,30 @@
 ate 
-100-continue error. %7B%7D, %7B%7D%22, result, gen.getState());%0A            %7D%0A        %7D catch (IOE
+h2c response e
 xception
  e) 
@@ -1837,37 +1837,38 @@
 sponse exception
+%22,
  e)
- %7B
+;
 %0A            log
@@ -1868,59 +1868,40 @@
     
-log.error(%22generate 100-continue response e
+throw new CommonRuntimeE
 xception
 %22, e
@@ -1888,35 +1888,33 @@
 RuntimeException
-%22, 
+(
 e);%0A        %7D%0A  

remove redudant codes