1111#include <nginx.h>
1212#include <ngx_http_v2_module.h>
1313
14-
14+ //这里的ngx_http_v2_integer_octets ngx_http_v2_integer_octets index索引编码过程和ngx_http_v2_state_header_block中的解码过程对应
15+ //ngx_http_v2_integer_octets ngx_http_v2_indexed进行整数编码,ngx_http_v2_literal_size进行字符串编码
1516#define ngx_http_v2_integer_octets (v ) (((v) + 127) / 128)
16-
1717#define ngx_http_v2_literal_size (h ) \
1818 (ngx_http_v2_integer_octets(sizeof(h) - 1) + sizeof(h) - 1)
1919
20+ /* 128也就是位操作1000 0000,也就是该index在索引表中,如果i为1表示索引表的0,i=2对应索引表的1,i=3对应索引表的2,i=4对应索引表的3 */
2021#define ngx_http_v2_indexed (i ) (128 + (i))
2122#define ngx_http_v2_inc_indexed (i ) (64 + (i))
2223
23-
24+ /* 和ngx_http_v2_static_table数组下表对应,相差1 */
2425#define NGX_HTTP_V2_STATUS_INDEX 8
2526#define NGX_HTTP_V2_STATUS_200_INDEX 8
2627#define NGX_HTTP_V2_STATUS_204_INDEX 9
@@ -230,7 +231,9 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
230231 }
231232 }
232233
233- /* NGINX在ngx_http_v2_state_header_block对接收到的头部帧进行解码解包,在ngx_http_v2_header_filter中对头部帧进行编码组包 */
234+ /* NGINX在ngx_http_v2_state_header_block对接收到的头部帧进行解码解包,在ngx_http_v2_header_filter中对头部帧进行编码组包
235+ 静态映射表在ngx_http_v2_static_table
236+ */
234237
235238 /* 头部9字节 + status响应长度(1字节为什么可以表示status响应码,因为一个字节就可以表示静态表的那个成员,见ngx_http_v2_static_table) */
236239 len = NGX_HTTP_V2_FRAME_HEADER_SIZE
@@ -379,6 +382,7 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
379382 part = & r -> headers_out .headers .part ;
380383 header = part -> elts ;
381384
385+ /* header_out数组列表中的所有NAME:VALUE长度加进来 */
382386 for (i = 0 ; /* void */ ; i ++ ) {
383387
384388 if (i >= part -> nelts ) {
@@ -415,6 +419,7 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
415419
416420 stream = r -> stream ;
417421
422+ /* 如果整个头部帧内容超过了最大frame_size大小,则可能需要拆分到多个帧 */
418423 len += NGX_HTTP_V2_FRAME_HEADER_SIZE
419424 * (len / stream -> connection -> frame_size );
420425
@@ -429,7 +434,6 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
429434
430435 if (status ) {
431436 * b -> last ++ = status ;
432-
433437 } else {
434438 * b -> last ++ = ngx_http_v2_inc_indexed (NGX_HTTP_V2_STATUS_INDEX );
435439 * b -> last ++ = 3 ;
@@ -617,13 +621,15 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
617621 cl -> buf = b ;
618622 cl -> next = NULL ;
619623
624+ /* 针对前面的header帧封包,组一个frame结构,挂到h2c->last_out队列,通过ngx_http_v2_filter_send触发发送出去 */
620625 frame = ngx_palloc (r -> pool , sizeof (ngx_http_v2_out_frame_t ));
621626 if (frame == NULL ) {
622627 return NGX_ERROR ;
623628 }
624629
625630 frame -> first = cl ;
626631 frame -> last = cl ;
632+ //该frame上对应的数据发送完毕后,会调用ngx_http_v2_headers_frame_handler
627633 frame -> handler = ngx_http_v2_headers_frame_handler ;
628634 frame -> stream = stream ;
629635 frame -> length = b -> last - b -> pos - NGX_HTTP_V2_FRAME_HEADER_SIZE ;
@@ -641,11 +647,14 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
641647 return NGX_ERROR ;
642648 }
643649
650+
644651 cln -> handler = ngx_http_v2_filter_cleanup ;
645652 cln -> data = stream ;
646653
647654 stream -> queued = 1 ;
648655
656+ //把ngx_http_v2_send_chain.send_chain=ngx_http_v2_header_filter,后面的数据帧就通过该函数发送,
657+ //在读取到后端数据后开始走out filter流程,然后调用ngx_http_output_filter,最终执行该ngx_http_v2_send_chain
649658 fc -> send_chain = ngx_http_v2_send_chain ;
650659 fc -> need_last_buf = 1 ;
651660
@@ -711,7 +720,12 @@ ngx_http_v2_write_continuation_head(u_char *pos, size_t length, ngx_uint_t sid,
711720 (void ) ngx_http_v2_write_sid (pos , sid );
712721}
713722
723+ /*
724+ 当http2头部帧发送的时候,会在ngx_http_v2_header_filter把ngx_http_v2_send_chain.send_chain=ngx_http_v2_send_chain
714725
726+ 该函数发送数据帧
727+ 在读取到后端数据后开始走out filter流程,然后调用ngx_http_output_filter,最终执行该ngx_http_v2_send_chain
728+ */
715729static ngx_chain_t *
716730ngx_http_v2_send_chain (ngx_connection_t * fc , ngx_chain_t * in , off_t limit )
717731{
@@ -726,6 +740,8 @@ ngx_http_v2_send_chain(ngx_connection_t *fc, ngx_chain_t *in, off_t limit)
726740
727741 r = fc -> data ;
728742 stream = r -> stream ;
743+ ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
744+ "ngx_http_v2_send_chain" );
729745
730746#if (NGX_SUPPRESS_WARN )
731747 size = 0 ;
@@ -741,7 +757,7 @@ ngx_http_v2_send_chain(ngx_connection_t *fc, ngx_chain_t *in, off_t limit)
741757 in = in -> next ;
742758 }
743759
744- if (in == NULL ) {
760+ if (in == NULL ) { /* chain链上没有要发送的数据 */
745761
746762 if (stream -> queued ) {
747763 fc -> write -> delayed = 1 ;
@@ -754,6 +770,7 @@ ngx_http_v2_send_chain(ngx_connection_t *fc, ngx_chain_t *in, off_t limit)
754770
755771 h2c = stream -> connection ;
756772
773+ /* 窗口 */
757774 if (size && ngx_http_v2_flow_control (h2c , stream ) == NGX_DECLINED ) {
758775 fc -> write -> delayed = 1 ;
759776 return in ;
@@ -779,31 +796,33 @@ ngx_http_v2_send_chain(ngx_connection_t *fc, ngx_chain_t *in, off_t limit)
779796 offset = 0 ;
780797 }
781798
799+ /* 发送limit不能超过h2c->send_window和stream->send_window的最小值 */
782800 if (limit == 0 || limit > (off_t ) h2c -> send_window ) {
783801 limit = h2c -> send_window ;
784802 }
785-
786803 if (limit > stream -> send_window ) {
787804 limit = (stream -> send_window > 0 ) ? stream -> send_window : 0 ;
788805 }
789806
790807 h2lcf = ngx_http_get_module_loc_conf (r , ngx_http_v2_module );
791808
809+ /* frame_size为本地配置chunk_size和对端通知的frame_size的最小值 */
792810 frame_size = (h2lcf -> chunk_size < h2c -> frame_size )
793811 ? h2lcf -> chunk_size : h2c -> frame_size ;
794812
795813#if (NGX_SUPPRESS_WARN )
796814 cl = NULL ;
797815#endif
798816
799- for ( ;; ) {
817+ for ( ;; ) {
800818 if ((off_t ) frame_size > limit ) {
801819 frame_size = (size_t ) limit ;
802820 }
803821
804822 ln = & out ;
805823 rest = frame_size ;
806824
825+ /* 把chain链中的buf组包到新的cl chain(即out链)链中,但是数据总大小不能超过rest限制 */
807826 while ((off_t ) rest >= size ) {
808827
809828 if (offset ) {
@@ -831,8 +850,8 @@ ngx_http_v2_send_chain(ngx_connection_t *fc, ngx_chain_t *in, off_t limit)
831850 in = in -> next ;
832851
833852 if (in == NULL ) {
834- frame_size -= rest ;
835- rest = 0 ;
853+ frame_size -= rest ; //in链中的数据已经全部移到out链,这时候的frame_size就是out中的数据大小
854+ rest = 0 ; //清0,说明所有的in链中的数据都可以全部发送出去
836855 break ;
837856 }
838857
@@ -861,6 +880,7 @@ ngx_http_v2_send_chain(ngx_connection_t *fc, ngx_chain_t *in, off_t limit)
861880
862881 ngx_http_v2_queue_frame (h2c , frame );
863882
883+ /* 发送了这么多数据,则窗口减少 */
864884 h2c -> send_window -= frame_size ;
865885
866886 stream -> send_window -= frame_size ;
@@ -931,7 +951,7 @@ ngx_http_v2_filter_get_shadow(ngx_http_v2_stream_t *stream, ngx_buf_t *buf,
931951 return cl ;
932952}
933953
934-
954+ //获取data帧frame结构
935955static ngx_http_v2_out_frame_t *
936956ngx_http_v2_filter_get_data_frame (ngx_http_v2_stream_t * stream ,
937957 size_t len , ngx_chain_t * first , ngx_chain_t * last )
@@ -1031,7 +1051,7 @@ ngx_http_v2_filter_send(ngx_connection_t *fc, ngx_http_v2_stream_t *stream)
10311051 return NGX_OK ;
10321052}
10331053
1034-
1054+ /* 查看发送窗口是不是大于0,大于0则发送 */
10351055static ngx_inline ngx_int_t
10361056ngx_http_v2_flow_control (ngx_http_v2_connection_t * h2c ,
10371057 ngx_http_v2_stream_t * stream )
0 commit comments