preface :

  HTTP2 The binary framing layer is introduced , Will normal request / response , Decomposing into frames to realize the concurrency of request and response .HTTP2 Yes 10 Frames of type in . This article will focus on this 10 Summarize the types of frames .

Frame structure :
    All frames contain a 9 byte Frame header of + Text that can be long is different . Depending on the type of frame , The structure of the text is also different .
    Frame header :
       Length(3
bypte): Represents the body part of the frame Payload Length of . Initially set to 2^14(16384), If you want to send a larger frame length , Must receive settings with SETTINGS_MAX_FRAME_SIZE. Of
    SETTING frame. Frame header 9 byte It doesn't count length Within the calculation range of .   Type(1
byte): only 0h-Ah Effective , If you do type Out of range , This frame must be ignored .   Flags(1 bype): Marker bit , frequently-used flag
Yes END_STREAM, Indicates that the frame is the last frame of the data stream .   Stream Identifier(31 bit): Frame id, Indicates which data stream the frame belongs to

  DATA frame :
 

  DATA: Variable length , It's application data . Length is affected by Length, Window Size Control of .   Pad Length:
  One 8bit field , It means Padding The length of the field .   Padding
:*padding Fill in several 0x0 Bit , from padding To control whether to verify . If verification is required , On the contrary 0 Part of the response PROTOCAL_ERROR abnormal .padding The length of the flow
controll within      DATA frame Of flag There can be :   END_STREAM
(0x1): When the setting is changed to ,stream Will be transformed into half-closed perhaps closed state .   PADDED: This bit is set , that bit 3 Indicates that Pad
Length field And the text needs to be filled in . payload = Padding Length + Data + Padding
  Abnormal situation :   1, every last DATA All frames must be subordinate to a data stream . If you receive one DATA frame its stream id
by 0x0, So throw out one PROTOCAL_ERROR error .  
2, If padding Is longer than payload, Then it has to be seen as a mistake , And throw it out PROTOCAL_ERROR   
  HEADER frame :
  
  Pad Length:1 btye, indicate padding The length of the field . stay flag Set in PADDED(00001000) It works when it's a bit .   E: 1
bit, Indicates whether the stream dependency is private , Only if the PRIORITY Only when it works .   Stream Dependency:
Indicates which data flow the requested resource depends on . The receiver responds preferentially to the data stream of the dependent resource .   Weight: No stream
dependency When , When the resource is already dependent or responded to .weight The bigger it is , More priority response .   Header Block Fragment:
Header information for transmitting differences
  header Of flag There can be :    END_STREAM:
(0000001B), Used to close the data stream , If the receiver receives the flag bit , Then flow in half-close
perhaps close Status of . But it can still be accepted CONTINUATION frame . Because theoretically ,CONTIUATION Frame belongs to HEADER Part of .  
 END_HEADERS:(00000100B), express header block fragment Contains all of them header
block. Not after that CONTIUATION frame.    PADDED:(00001000B), Indicates whether the body needs to be filled padding*    
PRIORITY: (00010100B), The fifth digit indicates E, So it needs to be stream dependency and weight Bit valid .
  Abnormal situation :   1, Received header No frames stream id, Then throw it out PROTOCAL_ERROR  
 2,padding If the domain exceeds length Length of , Then it will be thrown out PROTOCAL_ERROR

 PRIORITY frame
 
   E: indicate stream dependency and weight Effective .   Stream Dependency:
Indicates which data flow the requested resource depends on . The receiver responds preferentially to the data stream of the dependent resource .   Weight: No stream
dependency When , Or when the dependent resource has been responded to .weight The bigger it is , More priority response .
  priority There is no flag bit . Can be in stream idle perhaps closed When to send . It is used to determine the receiving order at the receiving end . however PRIORITY
Frames may be in stream Received when it has been processed . This is the time , this PRIORITY Will not be valid .      Abnormal situation :   1,PRIORITY
Frames must be subordinate to a stream, without steam id, Then it will be thrown out PROTOCAL_ERROR   2,PRIORITY If the frame length exceeds 5
byte, Then it will be thrown out FRAME_SIZE_ERROR

  RST_STREAM frame
       
  received RST_STREAM The frame will close immediately stream. And contains the ending error code , The error code contains why to end the data stream . After receiving the frame , Cannot send any stream. But the sender can accept the frame still on the channel . Because it is necessary to ensure that the frames that have been sent out are processed correctly .
      Abnormal situation :   1, If RST_STREAM No, stream id, Then it will be thrown out PROTOCAL_ERROR error .  
2, No more flow idle When you accept it RST_STREAM, Otherwise, it will be thrown PROTOCAL_ERROR error .  
3, If RST_STREAM Is longer than 4byte, Then it will be thrown out FRAME_SIZE_ERROR error .

  SETTINGS frame
       This frame carries the configuration information , These information will affect the communication process of the data stream . In general, the HEADERS frame Send before sending SETTINGS
frame , But it is also allowed to be sent during communication SETTINGS frame . Can send multiple SETTINGS frame , The frames are processed in order , The value set later will override the previous value .  
because SETTINGS Frames are configuration information , With high priority , So it needs to be sent by the receiver ack Guarantee each SETTINGS Frames arrive correctly .  
A lot of times SETTINGS Frames are oriented to the entire connection , Instead of a single stream . under these circumstances ,Stream id Set to 0.
  Identifier-value: Setting parameters - Setting value . Here's the situation :( every last setting Sets a set of values for each frame )  
SETTINGS_HEADER_TABLE_SIZE(0x1): Allow sender to notify receiver header Maximum length of . The initial value is 4096 byte  
SETTINGS_ENABLE_PUSH(0x2): Allow the server to actively push messages to the client . If the disable, Then the server cannot send PUSH_PROMISE
frame.0 Yes ,1 It's not allowed  
SETTINGS_MAX_CONCURRENT_STREAM(0x3): Indicates the current connection Can be established at the same time stream Maximum number of . Initially 100  
SETTINGS_INITIAL_WINDOW_SIZE(0x4): Indicates the initial window size of the sender , The initial value is 2^16 -1  
SETTINGS_MAX_FRAME_SIZE(0x5): Maximum frame length . set up DATA Maximum allowed length of frame , Maximum cannot exceed 2^24-1  
SETTINGS_MAX_HEADER_LIST_SIZE(0x6): Indicates what the sender wants to accept header Maximum length of . And this length is not compressed before . include name,value
+ For each header domain 32byte.       flags:   ACK:
SETTINGS Must be processed in the order in which they were sent . Once all the value It's all dealt with , The recipient must send a SETTINGS, Contains a ack. After that, the sender can continue to send .  
  Abnormal situation :  1,SETTINGS It's about the overall situation connection Of , therefore stream id It has to be 0, Otherwise, it will be thrown PROTOCAL_ERROR error .
 2,SETTINGS Will affect connection state , An unfinished one , Or something wrong SETTINGS Must be regarded as error, Throw out PROTOCAL_ERROR
 3,SETTINGS If the length exceeds 6 byte, Then a FRAME_SIZE_ERROR
 4, If SETTINGS_ENABLE_PUSH Set to 1, But the server sent it PUSH_PROMISE, Then it will be thrown out PROTOCAL_ERROR
 5, If SETTINGS_INITIAL_WINDOW_SIZE: More than 2^31-1, Then it will be thrown out FLOW_CONTROL_ERROR
 6, If sent SETTINGS, Never received it ack, Then it will be thrown out SETTINGS_TIMEOUT

PUSH_PROMISE frame
     PUSH_PROMISE Frame is when the server sends data to the client actively , Used to open this one stream Request frame for . The effect is similar to that of HEADERS
frame. Only when SETTING_ENABLE_PUSH Set to 0 When , Can be sent PUSH_PROMISE.      Pad Length:padding Length of
 R: Reserved bit   Promise Stream Id: Open this group stream Time stream id. This is the frame header stream
id It refers to which group of requests is this group of active push ( data stream ) Active messaging .   Header Block Fragment: Difference part of request header field   Padding:
Filling bit      flags:   END_HEADERS:(0x4) When the flag, So the second one bit express Header Block
Fragment Contains all the request headers .   PADDED:(0x8), third bit express Pad Length and Padding Effective
 
PUSH_PROMISE Must be in stream It can only be sent when it has been opened or half closed . If the receiver wants to reject PUSH_PROMISE, Can be sent RST_STREAM frame . One PUSH_PROMISE The frame changes in two ways stream. The first is PUSH_PROMISE
Of header block fragment It will change header
compress . The second is sending PUSH_PROMISE A new flow is created . The sender must confirm that the flow has been established , After sending DATA frame.
  Abnormal state :   1,PUSH_PROMISE Must be subordinate to a data stream , So if stream id by 0, Then it must be reported PROTOCAL_ERROR error  
2, Only in SETTINGS_ENABLE_PUSH Set to 0 When , To allow sending PUSH_PROMISE, Otherwise, it will be reported PROTOCAL_ERROR error  
3, If the dependent stream is not open
perhaps half-closed state , But I did PUSH_PROMISE frame , Then the frame must be treated as an error , Throw out PROTOCAL_ERROR error . But send out RST_STREAM The end of the must first process the received PUSH_PROMISE, Because it might actually be sent RST Previously issued .
  4, If PUSH_PROMISE Of stream id The stream pointed to is illegal , Then it must be reported PROTOCAL_ERROR error .

  PING frame
         
PING Frames are used to test an idle connection Is there still a valid frame . It can be emitted from either end . Count Reg 8byte. If the receiver does not receive ACK Of PING frame , Must send a ACK Of PING
Back to sender . also PING Frames should have higher priority         flags:     1,ACK(0x1):bit 0 indicate PING frame It's a PING
response. The receiver must receive the PING After frame , Returns a set of ack Of PING frame
    Abnormal situation :    1,PING Frames cannot depend on any stream , If PING There is stream id, Then you have to report an error .Protocal_ERROR  
 2, If PING Frame length exceeds 9 byte, Then it must be reported FRAME_SIZE_ERROR error

  GOAWAY frame  
  
     
GOAWAY The role of the frame is for the whole connection Not individually stream Of . It acts as a notification to both parties to close one connection, After receiving the frame , No new data stream can be created , But the established data stream should be processed . Because there is a situation , It's one end of the line GOAWAY before , Another build was sent stream Of header
frame, So in the GOAWAY One is designed on the frame Last-Stream-ID field , This is used to identify the connection On the largest frame, If a party receives a higher stream
id Of frame Time , The frame is ignored . Received GOAWAY One end of the frame can no longer be in the conncetion Create a new stream, But you can build new ones connection.    
  The terminal is shutting down connection When , Should be sent GOAWAY frame , So the other person can know which one stream And its previous data streams are being processed . But if something goes wrong , Then the terminal will choose not to send GOAWAY Just set up one connection.
    A terminal can send back multiple messages GOAWAY
frame , If the environment changes . for instance , A terminal wants to close the link , But there was no error . It may send one first NO_ERROR Of frame. after , We have another mistake , It needs to be done immediately shutdown The frame , This is the time , Will send an error code GOAWAY
frame .     Last-Stream-ID:   Identify the connection The last thing you can handle stream. If you receive it again stream
id Higher frame, It will be ignored .     Error Code: If you close the link because of an error , Put the error code in this position .         flags
: No, flags position          Abnormal situation :     1, Must be specified Last-Stream-ID: If the bit is 0, Need to report PROTOCOL_ERROR    
2,GOAWAY Frame your own stream id Must be 0, Otherwise PROTOCOL_ERROR
  WINDOW_UPDATE frame
           The purpose of this frame is to tell the other party , How much more can you accept byte Data for . act on DATA
frame. If one end is receiving a sliding window , Found that the number of bytes that can be sent becomes negative , Then you have to wait until you receive one WINDOW_UPDATE
frame , Make the window size positive , To continue sending DATA
frame. This frame can also be used as ack, There is one delter position , How many signs have just been confirmed byte Data for , One end is sending DATA
frame after , Wait until you get it WINDOW_UPDATE, Confirm that the previous data has been received , To send the next one DATA frame             
Window Size Increment: Indicates the current acceptable byte size         Abnormal situation :    
 1, If the receiver cannot accept any frame , When the frame is received , To respond FLOW_CONTROL_ERROR      2, If the receiver receives window size
increment by 0, Then it must be reported PROTOCOL_ERROR error      3, If WINDOW_UPDATE
Is longer than 4type, Then it must be reported FRAME_SIZE_ERROR error

  CONTINUATION frame
       The frame is subordinate to a HEADER ,PUSH_PROMISE frame . Used to send in HEADER PUSH_PROMISE I can't put it in request
header field . There is no limit to the length .
    flags:     END_HEADERS: (00000010B), express stream id Corresponding data flow ,request header
It's already sent .         Abnormal situation :    1, If stream id Not current stream id Or is it 0, Then it must be reported PROTOCOL_ERROR error
   2, send out CONTUATION The frame must be sent before HEADERS, PUSH_PROMISE frame , And the previous frames are not set END_HEADERS
Marker bit , Otherwise, it will be reported PROTOCOL_ERROR error .

Technology
©2019-2020 Toolsou All rights reserved,
Final review of database : Summary of comprehensive application questions use Python Make simple games Laplance operator ( Second derivative ) Convert hard disk to GPT Partition format Python Implementation of Hanoi Tower code about String How to create objects vue3 Learning journey 1—— establish vue3 project java String from back to front _Java String String summary use Python Write a story about plants versus zombies 【 Greedy Algorithm 】 Huffman coding problem