1 | <?php |
||
23 | class RedirectPlugin implements Plugin |
||
24 | { |
||
25 | /** |
||
26 | * Rule on how to redirect, change method for the new request. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $redirectCodes = [ |
||
31 | 300 => [ |
||
32 | 'switch' => [ |
||
33 | 'unless' => ['GET', 'HEAD'], |
||
34 | 'to' => 'GET', |
||
35 | ], |
||
36 | 'multiple' => true, |
||
37 | 'permanent' => false, |
||
38 | ], |
||
39 | 301 => [ |
||
40 | 'switch' => [ |
||
41 | 'unless' => ['GET', 'HEAD'], |
||
42 | 'to' => 'GET', |
||
43 | ], |
||
44 | 'multiple' => false, |
||
45 | 'permanent' => true, |
||
46 | ], |
||
47 | 302 => [ |
||
48 | 'switch' => [ |
||
49 | 'unless' => ['GET', 'HEAD'], |
||
50 | 'to' => 'GET', |
||
51 | ], |
||
52 | 'multiple' => false, |
||
53 | 'permanent' => false, |
||
54 | ], |
||
55 | 303 => [ |
||
56 | 'switch' => [ |
||
57 | 'unless' => ['GET', 'HEAD'], |
||
58 | 'to' => 'GET', |
||
59 | ], |
||
60 | 'multiple' => false, |
||
61 | 'permanent' => false, |
||
62 | ], |
||
63 | 307 => [ |
||
64 | 'switch' => false, |
||
65 | 'multiple' => false, |
||
66 | 'permanent' => false, |
||
67 | ], |
||
68 | 308 => [ |
||
69 | 'switch' => false, |
||
70 | 'multiple' => false, |
||
71 | 'permanent' => true, |
||
72 | ], |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * Determine how header should be preserved from old request. |
||
77 | * |
||
78 | * @var bool|array |
||
79 | * |
||
80 | * true will keep all previous headers (default value) |
||
81 | * false will ditch all previous headers |
||
82 | * string[] will keep only headers with the specified names |
||
83 | */ |
||
84 | protected $preserveHeader; |
||
85 | |||
86 | /** |
||
87 | * Store all previous redirect from 301 / 308 status code. |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | protected $redirectStorage = []; |
||
92 | |||
93 | /** |
||
94 | * Whether the location header must be directly used for a multiple redirection status code (300). |
||
95 | * |
||
96 | * @var bool |
||
97 | */ |
||
98 | protected $useDefaultForMultiple; |
||
99 | |||
100 | /** |
||
101 | * @var array |
||
102 | */ |
||
103 | protected $circularDetection = []; |
||
104 | |||
105 | /** |
||
106 | * @param array $config { |
||
107 | * |
||
108 | * @var bool|string[] $preserve_header true keeps all headers, false remove all of them, an array is interpreted as a list of header names to keep |
||
109 | * @var bool $use_default_for_multiple Whether the location header must be directly used for a multiple redirection status code (300). |
||
110 | * } |
||
111 | */ |
||
112 | 12 | public function __construct(array $config = []) |
|
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | 11 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
182 | |||
183 | /** |
||
184 | * Builds the redirect request. |
||
185 | * |
||
186 | * @param RequestInterface $request Original request |
||
187 | * @param UriInterface $uri New uri |
||
188 | * @param int $statusCode Status code from the redirect response |
||
189 | * |
||
190 | * @return MessageInterface|RequestInterface |
||
191 | */ |
||
192 | 7 | protected function buildRedirectRequest(RequestInterface $request, UriInterface $uri, $statusCode) |
|
212 | |||
213 | /** |
||
214 | * Creates a new Uri from the old request and the location header. |
||
215 | * |
||
216 | * @param ResponseInterface $response The redirect response |
||
217 | * @param RequestInterface $request The original request |
||
218 | * |
||
219 | * @throws HttpException If location header is not usable (missing or incorrect) |
||
220 | * @throws MultipleRedirectionException If a 300 status code is received and default location cannot be resolved (doesn't use the location header or not present) |
||
221 | * |
||
222 | * @return UriInterface |
||
223 | */ |
||
224 | 10 | private function createUri(ResponseInterface $response, RequestInterface $request) |
|
273 | } |
||
274 |
If you suppress an error, we recommend checking for the error condition explicitly: