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