|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Slim Framework (https://slimframework.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/slimphp/Slim |
|
6
|
|
|
* @copyright Copyright (c) 2011-2017 Josh Lockhart |
|
7
|
|
|
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License) |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Slim\Handlers; |
|
10
|
|
|
|
|
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
use Slim\Http\Body; |
|
14
|
|
|
use UnexpectedValueException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Default Slim application not allowed handler |
|
18
|
|
|
* |
|
19
|
|
|
* It outputs a simple message in either JSON, XML or HTML based on the |
|
20
|
|
|
* Accept header. |
|
21
|
|
|
*/ |
|
22
|
|
|
class NotAllowed extends AbstractHandler |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Invoke error handler |
|
26
|
|
|
* |
|
27
|
|
|
* @param ServerRequestInterface $request The most recent Request object |
|
28
|
|
|
* @param ResponseInterface $response The most recent Response object |
|
29
|
|
|
* @param string[] $methods Allowed HTTP methods |
|
30
|
|
|
* |
|
31
|
|
|
* @return ResponseInterface |
|
32
|
|
|
* @throws UnexpectedValueException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($request->getMethod() === 'OPTIONS') { |
|
37
|
|
|
$status = 200; |
|
38
|
|
|
$contentType = 'text/plain'; |
|
39
|
|
|
$output = $this->renderPlainOptionsMessage($methods); |
|
40
|
|
|
} else { |
|
41
|
|
|
$status = 405; |
|
42
|
|
|
$contentType = $this->determineContentType($request); |
|
43
|
|
|
switch ($contentType) { |
|
44
|
|
|
case 'application/json': |
|
45
|
|
|
$output = $this->renderJsonNotAllowedMessage($methods); |
|
46
|
|
|
break; |
|
47
|
|
|
|
|
48
|
|
|
case 'text/xml': |
|
49
|
|
|
case 'application/xml': |
|
50
|
|
|
$output = $this->renderXmlNotAllowedMessage($methods); |
|
51
|
|
|
break; |
|
52
|
|
|
|
|
53
|
|
|
case 'text/html': |
|
54
|
|
|
$output = $this->renderHtmlNotAllowedMessage($methods); |
|
55
|
|
|
break; |
|
56
|
|
|
default: |
|
57
|
|
|
throw new UnexpectedValueException('Cannot render unknown content type ' . $contentType); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$body = new Body(fopen('php://temp', 'r+')); |
|
62
|
|
|
$body->write($output); |
|
63
|
|
|
$allow = implode(', ', $methods); |
|
64
|
|
|
|
|
65
|
|
|
return $response |
|
66
|
|
|
->withStatus($status) |
|
67
|
|
|
->withHeader('Content-type', $contentType) |
|
68
|
|
|
->withHeader('Allow', $allow) |
|
69
|
|
|
->withBody($body); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Render PLAIN message for OPTIONS response |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $methods |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function renderPlainOptionsMessage($methods) |
|
79
|
|
|
{ |
|
80
|
|
|
$allow = implode(', ', $methods); |
|
81
|
|
|
|
|
82
|
|
|
return 'Allowed methods: ' . $allow; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Render JSON not allowed message |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $methods |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function renderJsonNotAllowedMessage($methods) |
|
92
|
|
|
{ |
|
93
|
|
|
$allow = implode(', ', $methods); |
|
94
|
|
|
|
|
95
|
|
|
return '{"message":"Method not allowed. Must be one of: ' . $allow . '"}'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Render XML not allowed message |
|
100
|
|
|
* |
|
101
|
|
|
* @param array $methods |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function renderXmlNotAllowedMessage($methods) |
|
105
|
|
|
{ |
|
106
|
|
|
$allow = implode(', ', $methods); |
|
107
|
|
|
|
|
108
|
|
|
return "<root><message>Method not allowed. Must be one of: $allow</message></root>"; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Render HTML not allowed message |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $methods |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function renderHtmlNotAllowedMessage($methods) |
|
118
|
|
|
{ |
|
119
|
|
|
$allow = implode(', ', $methods); |
|
120
|
|
|
$output = <<<END |
|
121
|
|
|
<html> |
|
122
|
|
|
<head> |
|
123
|
|
|
<title>Method not allowed</title> |
|
124
|
|
|
<style> |
|
125
|
|
|
body{ |
|
126
|
|
|
margin:0; |
|
127
|
|
|
padding:30px; |
|
128
|
|
|
font:12px/1.5 Helvetica,Arial,Verdana,sans-serif; |
|
129
|
|
|
} |
|
130
|
|
|
h1{ |
|
131
|
|
|
margin:0; |
|
132
|
|
|
font-size:48px; |
|
133
|
|
|
font-weight:normal; |
|
134
|
|
|
line-height:48px; |
|
135
|
|
|
} |
|
136
|
|
|
</style> |
|
137
|
|
|
</head> |
|
138
|
|
|
<body> |
|
139
|
|
|
<h1>Method not allowed</h1> |
|
140
|
|
|
<p>Method not allowed. Must be one of: <strong>$allow</strong></p> |
|
141
|
|
|
</body> |
|
142
|
|
|
</html> |
|
143
|
|
|
END; |
|
144
|
|
|
|
|
145
|
|
|
return $output; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|