77 * 编码解码类,负责对目标请求进行编码解码
88 */
99public class Transfer {
10+ public static final byte http_0 = 48 ;
11+ public static final byte http_r = 13 ; // \r
12+ public static final byte http_n = 10 ; // \n
1013 /**
1114 * 对请求包进行chunked编码
1215 * @param requestResponse 要处理的请求响应对象
@@ -110,7 +113,7 @@ public static byte[] splitReqBody(byte[] reqBody,int minChunkedLen,int maxChunke
110113 byte_encoding_body = joinByteArray (byte_encoding_body ,b );
111114 byte_encoding_body = joinByteArray (byte_encoding_body ,"\r \n " .getBytes ());
112115 }
113- byte_encoding_body = joinByteArray (byte_encoding_body ,"0\n \n " .getBytes ());
116+ byte_encoding_body = joinByteArray (byte_encoding_body ,"0\r \n \r \n " .getBytes ());
114117 return byte_encoding_body ;
115118 }
116119
@@ -134,7 +137,14 @@ public static byte[] mergeReqBody(byte[] chunkedReqBody){
134137 byte [] chunkedLen = new byte [length ];
135138 System .arraycopy (chunkedReqBody , j , chunkedLen , 0 , length );
136139 j = i + 2 ;
137- int cLen = Util .hexToDecimal (new String (chunkedLen ));
140+ int cLen ;
141+ String strChunkedLen = new String (chunkedLen );
142+ if (strChunkedLen .contains (";" )){// 如果存在注释
143+ cLen = Util .hexToDecimal (strChunkedLen .substring (0 ,strChunkedLen .indexOf (";" )));
144+ }else {
145+ cLen = Util .hexToDecimal (strChunkedLen );
146+ }
147+
138148 // 根据分块长度获取分块内容
139149 byte [] chunked = new byte [cLen ];
140150 System .arraycopy (chunkedReqBody , j , chunked , 0 , cLen );
@@ -145,9 +155,11 @@ public static byte[] mergeReqBody(byte[] chunkedReqBody){
145155 }
146156
147157 // 处理结尾0\n\n
148- if (chunkedReqBody [i ] == "0" .getBytes ()[0 ]
149- && chunkedReqBody [i +1 ] == "\n " .getBytes ()[0 ]
150- && chunkedReqBody [i +2 ] == "\n " .getBytes ()[0 ]){
158+ if (chunkedReqBody [i ] == http_0
159+ && chunkedReqBody [i +1 ] == http_r
160+ && chunkedReqBody [i +2 ] == http_n
161+ && chunkedReqBody [i +2 ] == http_r
162+ && chunkedReqBody [i +3 ] == http_n ){
151163 break ;
152164 }
153165 }
0 commit comments