@@ -340,10 +340,12 @@ ngx_http_flv_live_send_header(ngx_rtmp_session_t *s)
340340 ngx_http_request_t * r ;
341341 ngx_rtmp_live_ctx_t * live_ctx ;
342342 ngx_rtmp_codec_ctx_t * codec_ctx ;
343- ngx_table_elt_t * e ;
343+ ngx_list_part_t * part ;
344+ ngx_table_elt_t * e , * header ;
344345 u_char * p ;
345346 ngx_chain_t cl_flv_hdr , * pkt ;
346347 ngx_buf_t buf_flv_hdr ;
348+ ngx_uint_t i ;
347349 ngx_str_t chunked_flv_header ;
348350 ngx_str_t consec_flv_header ;
349351 u_char chunked_flv_header_data [18 ];
@@ -360,8 +362,37 @@ ngx_http_flv_live_send_header(ngx_rtmp_session_t *s)
360362
361363 ngx_str_set (& r -> headers_out .content_type , "video/x-flv" );
362364
363- /* force HTTP header Connection to be close */
364- r -> keepalive = 0 ;
365+ /* fill HTTP header 'Connection' according to headers_in */
366+ part = & r -> headers_in .headers .part ;
367+ header = part -> elts ;
368+
369+ for (i = 0 ; /* void */ ; i ++ ) {
370+ if (i >= part -> nelts ) {
371+ if (part -> next == NULL ) {
372+ break ;
373+ }
374+
375+ part = part -> next ;
376+ header = part -> elts ;
377+ i = 0 ;
378+ }
379+
380+ if (header [i ].hash == 0 ) {
381+ continue ;
382+ }
383+
384+ if (ngx_strcasecmp (header [i ].key .data , (u_char * ) "connection" ) == 0 ) {
385+ if (ngx_strcasecmp (header [i ].value .data , (u_char * ) "keep-alive" )
386+ == 0 )
387+ {
388+ r -> keepalive = 1 ;
389+ } else {
390+ r -> keepalive = 0 ;
391+ }
392+
393+ break ;
394+ }
395+ }
365396
366397 live_ctx = ngx_rtmp_get_module_ctx (s , ngx_rtmp_live_module );
367398 if (live_ctx && !live_ctx -> active ) {
@@ -1129,6 +1160,18 @@ ngx_http_flv_live_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
11291160 r -> main -> blocked ++ ;
11301161 }
11311162
1163+ #if (nginx_version >= 1013001 )
1164+ /**
1165+ * when playing from pull, the downstream requests on the most
1166+ * of time return before the upstream requests, flv.js always
1167+ * sends HTTP header 'Connection: keep-alive', but Nginx has
1168+ * deleted r->blocked in ngx_http_finalize_request, that causes
1169+ * ngx_http_set_keepalive to run the cleanup handlers to close
1170+ * the connection between downstream and server, so play fails
1171+ **/
1172+ r -> keepalive = 0 ;
1173+ #endif
1174+
11321175 if (s -> wait_notify_play ) {
11331176 goto next ;
11341177 }
0 commit comments