@@ -24,38 +24,61 @@ class AddHttp2ServerPush
2424 *
2525 * @return mixed
2626 */
27- public function handle (Request $ request , Closure $ next , $ limit = null )
27+ public function handle (Request $ request , Closure $ next , $ limit = null , $ sizeLimit = null , $ excludeKeywords = null )
2828 {
2929 $ response = $ next ($ request );
3030
3131 if ($ response ->isRedirection () || !$ response instanceof Response || $ request ->isJson ()) {
3232 return $ response ;
3333 }
3434
35- $ this ->generateAndAttachLinkHeaders ($ response , $ limit );
35+ $ this ->generateAndAttachLinkHeaders ($ response , $ limit, $ sizeLimit , $ excludeKeywords );
3636
3737 return $ response ;
3838 }
3939
40+ public function getConfig ($ key , $ default =false ) {
41+ if (!function_exists ('config ' )) { // for tests..
42+ return $ default ;
43+ }
44+ return config ('http2serverpush. ' .$ key , $ default );
45+ }
46+
4047 /**
4148 * @param \Illuminate\Http\Response $response
4249 *
4350 * @return $this
4451 */
45- protected function generateAndAttachLinkHeaders (Response $ response , $ limit = null )
52+ protected function generateAndAttachLinkHeaders (Response $ response , $ limit = null , $ sizeLimit = null , $ excludeKeywords = null )
4653 {
54+ $ excludeKeywords ?? $ this ->getConfig ('exclude_keywords ' , []);
4755 $ headers = $ this ->fetchLinkableNodes ($ response )
4856 ->flatten (1 )
4957 ->map (function ($ url ) {
5058 return $ this ->buildLinkHeaderString ($ url );
5159 })
52- ->filter ()
5360 ->unique ()
54- ->take ($ limit )
55- ->implode (', ' );
61+ ->filter (function ($ value , $ key ) use ($ excludeKeywords ){
62+ if (!$ value ) return false ;
63+ $ exclude_keywords = collect ($ excludeKeywords )->map (function ($ keyword ) {
64+ return preg_quote ($ keyword );
65+ });
66+ if ($ exclude_keywords ->count () <= 0 ) {
67+ return true ;
68+ }
69+ return !preg_match ('%( ' .$ exclude_keywords ->implode ('| ' ).')%i ' , $ value );
70+ })
71+ ->take ($ limit );
72+
73+ $ sizeLimit = $ sizeLimit ?? max (1 , intval ($ this ->getConfig ('size_limit ' , 32 *1024 )));
74+ $ headersText = trim ($ headers ->implode (', ' ));
75+ while (strlen ($ headersText ) > $ sizeLimit ) {
76+ $ headers ->pop ();
77+ $ headersText = trim ($ headers ->implode (', ' ));
78+ }
5679
57- if (!empty (trim ( $ headers ) )) {
58- $ this ->addLinkHeader ($ response , $ headers );
80+ if (!empty ($ headersText )) {
81+ $ this ->addLinkHeader ($ response , $ headersText );
5982 }
6083
6184 return $ this ;
@@ -115,6 +138,12 @@ private function buildLinkHeaderString($url)
115138 $ type = collect ($ linkTypeMap )->first (function ($ type , $ extension ) use ($ url ) {
116139 return str_contains (strtoupper ($ url ), $ extension );
117140 });
141+
142+
143+ if (!preg_match ('%^https?://%i ' , $ url )) {
144+ $ basePath = $ this ->getConfig ('base_path ' , '/ ' );
145+ $ url = $ basePath . ltrim ($ url , $ basePath );
146+ }
118147
119148 return is_null ($ type ) ? null : "< {$ url }>; rel=preload; as= {$ type }" ;
120149 }
0 commit comments