1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Scrawler package. |
4
|
|
|
* |
5
|
|
|
* (c) Pranjal Pandey <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Scrawler\Http; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* RedirectResponse on Scrawler namespace. |
15
|
|
|
*/ |
16
|
|
|
class RedirectResponse extends Response |
17
|
|
|
{ |
18
|
|
|
protected $targetUrl; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Creates a redirect response so that it conforms to the rules defined for a redirect status code. |
22
|
|
|
* |
23
|
|
|
* @param string $url The URL to redirect to. The URL should be a full URL, with schema etc., |
24
|
|
|
* but practically every browser redirects on paths only as well |
25
|
|
|
* @param int $status The HTTP status code (302 "Found" by default) |
26
|
|
|
* @param array $headers The headers (Location is always set to the given URL) |
27
|
|
|
* |
28
|
|
|
* @throws \InvalidArgumentException |
29
|
|
|
* |
30
|
|
|
* @see https://tools.ietf.org/html/rfc2616#section-10.3 |
31
|
|
|
*/ |
32
|
|
|
public function __construct(string $url, int $status = 302, array $headers = []) |
33
|
|
|
{ |
34
|
|
|
parent::__construct('', $status, $headers); |
35
|
|
|
|
36
|
|
|
$this->setTargetUrl($url); |
37
|
|
|
|
38
|
|
|
if (!$this->isRedirect()) { |
39
|
|
|
throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (301 == $status && !\array_key_exists('cache-control', array_change_key_case($headers, \CASE_LOWER))) { |
43
|
|
|
$this->headers->remove('cache-control'); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns the target URL. |
49
|
|
|
*/ |
50
|
|
|
public function getTargetUrl(): string |
51
|
|
|
{ |
52
|
|
|
return $this->targetUrl; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Sets the redirect target of this response. |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
* |
60
|
|
|
* @throws \InvalidArgumentException |
61
|
|
|
*/ |
62
|
|
|
public function setTargetUrl(string $url): static |
63
|
|
|
{ |
64
|
|
|
if ('' === $url) { |
65
|
|
|
throw new \InvalidArgumentException('Cannot redirect to an empty URL.'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->targetUrl = $url; |
69
|
|
|
|
70
|
|
|
$this->setContent( |
71
|
|
|
sprintf('<!DOCTYPE html> |
72
|
|
|
<html> |
73
|
|
|
<head> |
74
|
|
|
<meta charset="UTF-8" /> |
75
|
|
|
<meta http-equiv="refresh" content="0;url=\'%1$s\'" /> |
76
|
|
|
|
77
|
|
|
<title>Redirecting to %1$s</title> |
78
|
|
|
</head> |
79
|
|
|
<body> |
80
|
|
|
Redirecting to <a href="%1$s">%1$s</a>. |
81
|
|
|
</body> |
82
|
|
|
</html>', htmlspecialchars($url, \ENT_QUOTES, 'UTF-8')) |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$this->headers->set('Location', $url); |
86
|
|
|
$this->headers->set('Content-Type', 'text/html; charset=utf-8'); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|