当前位置:网站首页>Simply test the two different data transmission methods of content length and chunked
Simply test the two different data transmission methods of content length and chunked
2022-07-02 07:59:00 【Silent discrimination】
List of articles
Content-Length: Data transmission according to data length
Chunk: Data transmission by block
The sample request shown in the following figure , It's just at this time Content-length Is in the response header

One 、 Code testing
This article will use ApiPost Test two different methods of carrying data
1、 Preparation
1、 Prepare one ApiPost( Their own use postman When testing , Unable to test successfully )

2、 Write the corresponding test class
public class TestServerSocket {
public static void main(String[] args) throws IOException {
ServerSocket socket = new ServerSocket(8089);
Socket accept = socket.accept();
InputStream inputStream = accept.getInputStream();
while (true) {
byte[] bytes = new byte[100000];
int readLength = inputStream.read(bytes);
System.out.println(new String(bytes, 0, readLength));
System.out.println("---- Complete a data reception over----");
}
}
}
2、 test Content-length
1、 Send a message to 8089 port , The carried parameter is name and age

2、 View the data information of the console

We will find that the default parameter carried is content-length, And the length is 269 Bytes .
3、 test Transfer-Encoding:chunked
1、 modify ApiPost Request header for , Add corresponding Transfer-Encoding Parameters

2、 The parameters carried and remain unchanged , Again using ApiPost test , View console data

At this time, the requested parameter information , It includes Content-length, Also contains the Transfer-Encoding:chunked. But the data below seems different , Use chunked after , The data consists of the previous single line string mobian, It becomes multi row data . And mobian Ahead 6 just mobian Length of string ,23 Ahead 2 just 23 The length of , Finally 0 ending .
summary :
namely content-length It is transmitted based on the total length of data , and chunked It is transmitted in blocks , Each piece is internally transmitted according to the previous length , Until the length is 0 after , Stop data transfer . If two exist at the same time , be content-length invalid .
Two 、tomcat Source code
tomcat Chinese vs Content-length and Transfer-Encoding Data processing of .
1、Request The process
A request is in tomcat The internal call logic of is not repeated here , For those who don't know, please refer to my previous blog : Talking about Tomcat The internal execution process after receiving a request ( Source code )
It will eventually call prepareRequest() Method
1、prepareRequest Method until after the position in the figure below
- First, I will get transfer-encoding attribute
- Then according to the parameters configured later , Pass to addInputFilter Method

2、 If the following parameter is chunked, It will be contentDelimitation Set to true

3、 The analysis is finished transfer-encoding after
- First of all get contentLength
- When the content length is greater than 0 after , Will judge contentDelimitation The state of , If we configure it chunked Then change the value to true
- contentDelimitation by true after , Will remove content-length node , That is, both exist at the same time ,contentlength invalid

2、 other
If you want to know contentlength and chunked There are two ways to read the logic code of data , You can check InputFilter Two implementation classes of the interface , namely IdentityInputFilter(contentlength) and ChunkedInputFilter(chunked) Two kinds of doRead Method .

边栏推荐
- 【Paper Reading】
- C#与MySQL数据库连接
- 【Wing Loss】《Wing Loss for Robust Facial Landmark Localisation with Convolutional Neural Networks》
- Daily practice (19): print binary tree from top to bottom
- C # connect to MySQL database
- Comparison of chat Chinese corpus (attach links to various resources)
- 【Batch】learning notes
- Look for we media materials from four aspects to ensure your creative inspiration
- Meta Learning 简述
- E-R画图明确内容
猜你喜欢
Semi supervised mixpatch
[binocular vision] binocular correction
Replace convolution with full connection layer -- repmlp
Sequence problem for tqdm and print
Open3d learning note 4 [surface reconstruction]
Replace self attention with MLP
Mmdetection trains its own data set -- export coco format of cvat annotation file and related operations
【Batch】learning notes
Jetson nano installation tensorflow stepping pit record (scipy1.4.1)
【AutoAugment】《AutoAugment:Learning Augmentation Policies from Data》
随机推荐
【BiSeNet】《BiSeNet:Bilateral Segmentation Network for Real-time Semantic Segmentation》
SQLyog远程连接centos7系统下的MySQL数据库
TimeCLR: A self-supervised contrastive learning framework for univariate time series representation
I'll show you why you don't need to log in every time you use Taobao, jd.com, etc?
【TCDCN】《Facial landmark detection by deep multi-task learning》
联邦学习下的数据逆向攻击 -- GradInversion
静态库和动态库
将恶意软件嵌入到神经网络中
Where do you find the materials for those articles that have read 10000?
【FastDepth】《FastDepth:Fast Monocular Depth Estimation on Embedded Systems》
【Sparse-to-Dense】《Sparse-to-Dense:Depth Prediction from Sparse Depth Samples and a Single Image》
Open3d learning notes II [file reading and writing]
The hystrix dashboard reported an error hystrix Stream is not in the allowed list of proxy host names solution
MoCO ——Momentum Contrast for Unsupervised Visual Representation Learning
It's great to save 10000 pictures of girls
w10升级至W11系统,黑屏但鼠标与桌面快捷方式能用,如何解决
CONDA common commands
【Hide-and-Seek】《Hide-and-Seek: A Data Augmentation Technique for Weakly-Supervised Localization xxx》
How to turn on night mode on laptop
【MnasNet】《MnasNet:Platform-Aware Neural Architecture Search for Mobile》