4
4
5
5
use Closure ;
6
6
use GuzzleHttp \Client as Guzzle ;
7
+ use GuzzleHttp \Exception \ConnectException ;
7
8
use GuzzleHttp \Exception \RequestException ;
8
9
use Illuminate \Http \Request ;
9
10
use Illuminate \Support \Arr ;
@@ -71,15 +72,15 @@ public function __construct(Guzzle $client)
71
72
{
72
73
$ this ->returnSoftHttpCodes = config ('prerender.prerender_soft_http_codes ' );
73
74
74
- if ($ this ->returnSoftHttpCodes ) {
75
- $ this ->client = $ client ;
76
- } else {
77
- // Workaround to avoid following redirects
78
- $ config = $ client ->getConfig ();
79
- $ config ['allow_redirects ' ] = false ;
80
- $ this ->client = new Guzzle ($ config );
75
+ $ guzzleConfig = $ client ->getConfig ();
76
+ $ guzzleConfig ['timeout ' ] = config ('prerender.timeout ' );
77
+
78
+ if (!$ this ->returnSoftHttpCodes ) {
79
+ $ guzzleConfig ['allow_redirects ' ] = false ;
81
80
}
82
81
82
+ $ this ->client = new Guzzle ($ guzzleConfig );
83
+
83
84
$ config = config ('prerender ' );
84
85
85
86
$ this ->prerenderUri = $ config ['prerender_url ' ];
@@ -102,7 +103,7 @@ public function handle(Request $request, Closure $next)
102
103
if ($ prerenderedResponse ) {
103
104
$ statusCode = $ prerenderedResponse ->getStatusCode ();
104
105
105
- if (! $ this ->returnSoftHttpCodes && $ statusCode >= 300 && $ statusCode < 400 ) {
106
+ if (!$ this ->returnSoftHttpCodes && $ statusCode >= 300 && $ statusCode < 400 ) {
106
107
$ headers = $ prerenderedResponse ->getHeaders ();
107
108
108
109
return Redirect::to (array_change_key_case ($ headers , CASE_LOWER )['location ' ][0 ], $ statusCode );
@@ -128,11 +129,11 @@ private function shouldShowPrerenderedPage(Request $request): bool
128
129
129
130
$ isRequestingPrerenderedPage = false ;
130
131
131
- if (! $ userAgent ) {
132
+ if (!$ userAgent ) {
132
133
return false ;
133
134
}
134
135
135
- if (! $ request ->isMethod ('GET ' )) {
136
+ if (!$ request ->isMethod ('GET ' )) {
136
137
return false ;
137
138
}
138
139
@@ -152,13 +153,13 @@ private function shouldShowPrerenderedPage(Request $request): bool
152
153
$ isRequestingPrerenderedPage = true ;
153
154
}
154
155
155
- if (! $ isRequestingPrerenderedPage ) {
156
+ if (!$ isRequestingPrerenderedPage ) {
156
157
return false ;
157
158
}
158
159
159
160
// only check whitelist if it is not empty
160
161
if ($ this ->whitelist ) {
161
- if (! $ this ->isListed ($ requestUri , $ this ->whitelist )) {
162
+ if (!$ this ->isListed ($ requestUri , $ this ->whitelist )) {
162
163
return false ;
163
164
}
164
165
}
@@ -207,19 +208,21 @@ private function getPrerenderedPageResponse(Request $request): ?ResponseInterfac
207
208
208
209
return $ this ->client ->get ($ this ->prerenderUri .'/ ' .urlencode ($ protocol .':// ' .$ host .'/ ' .$ path ), compact ('headers ' ));
209
210
} catch (RequestException $ exception ) {
210
- if (! $ this ->returnSoftHttpCodes && ! empty ($ exception ->getResponse ()) && $ exception ->getResponse ()->getStatusCode () === 404 ) {
211
+ if (!$ this ->returnSoftHttpCodes && !empty ($ exception ->getResponse ()) && $ exception ->getResponse ()->getStatusCode () === 404 ) {
211
212
abort (404 );
212
213
}
214
+ } catch (ConnectException $ exception ) {
215
+ //
216
+ }
213
217
214
- // In case of an exception, we only throw the exception if we are in debug mode. Otherwise,
215
- // we return null and the handle() method will just pass the request to the next middleware
216
- // and we do not show a prerendered page.
217
- if (config ('app.debug ' )) {
218
- throw $ exception ;
219
- }
220
-
221
- return null ;
218
+ // In case of an exception, we only throw the exception if we are in debug mode. Otherwise,
219
+ // we return null and the handle() method will just pass the request to the next middleware
220
+ // and we do not show a prerendered page.
221
+ if (config ('app.debug ' )) {
222
+ throw $ exception ;
222
223
}
224
+
225
+ return null ;
223
226
}
224
227
225
228
/**
0 commit comments