@@ -56,30 +56,38 @@ public function __construct(private PersistentState &$state)
5656 * @param string $method The HTTP method of the request (e.g., 'GET', 'POST').
5757 * @param ServerRequestInterface|string $request The request payload, typically used for 'POST' requests.
5858 * @param string &$response The variable to store the generated response.
59- * @param array &$content_type The variable to store the content type of the response.
59+ * @param array &$headers The variable to store the headers of the response.
6060 * @param string &$body The variable to store the body of the response.
6161 * @param bool $bypass_token Whether to bypass the token check. Default is false.
6262 */
6363 public function handle (
6464 string $ method ,
6565 ServerRequestInterface |string $ request ,
6666 int |string &$ response ,
67- array &$ content_type ,
67+ array &$ headers ,
6868 string &$ body ,
6969 bool $ bypass_token = false
7070 ): void
7171 {
7272 switch ($ method ) {
7373 case 'GET ' :
74- $ this ->get ($ response , $ content_type , $ body );
74+ $ this ->get ($ response , $ headers , $ body );
75+ break ;
76+ case 'HEAD ' :
77+ $ this ->head ($ response , $ headers );
7578 break ;
7679 case 'POST ' :
80+ case 'PUT ' :
7781 case 'DELETE ' :
78- $ this ->post ($ request , $ response , $ content_type , $ body , $ bypass_token );
82+ $ this ->post ($ request , $ response , $ headers , $ body , $ bypass_token );
7983 break ;
84+ case 'PATCH ' :
85+ case 'OPTIONS ' :
86+ case 'CONNECT ' :
87+ case 'TRACE ' :
8088 default :
8189 $ response = Response::STATUS_METHOD_NOT_ALLOWED ;
82- $ content_type = ['Content-Type ' => 'text/plain ' ];
90+ $ headers = ['Content-Type ' => 'text/plain ' ];
8391 $ body = 'Method Not Allowed ' ;
8492 break ;
8593 }
@@ -88,26 +96,53 @@ public function handle(
8896 /**
8997 * Handles the GET request and prepares the response.
9098 *
91- * @param string &$response The response string to be sent back to the client.
92- * @param array &$content_type The variable to store the content type of the response.
93- * @param string &$body The variable to store the body of the response.
99+ * @param int|string &$response The response string to be sent back to the client.
100+ * @param array &$headers The variable to store the headers of the response.
101+ * @param string &$body The variable to store the body of the response.
102+ *
103+ * It appends the JSON-encoded verification list to the body of the response.
104+ */
105+ private function get (int |string &$ response , array &$ headers , string &$ body ): void
106+ {
107+ $ body = $ this ->head ($ response , $ headers );
108+ }
109+
110+ /**
111+ * Sets the HTTP response status and content type for the HEAD request.
94112 *
95- * This method sets the HTTP status code to 200 OK and the Content-Type to application/json.
96- * It then appends the JSON-encoded verification list to the response.
113+ * @param int|string &$response The response string to be sent back to the client.
114+ * @param array &$headers The variable to store the headers of the response.
115+ *
116+ * @return string The content to be sent in the response body.
117+ *
118+ * This method sets the HTTP status code and headers.
97119 */
98- private function get (int |string &$ response , array &$ content_type , string & $ body ): void
120+ private function head (int |string &$ response , array &$ headers ): string
99121 {
100122 $ response = Response::STATUS_OK ;
101- $ content_type = ['Content-Type ' => 'application/json ' ];
102- $ body = json_encode ($ this ->state ->getVerifyList ());
123+ $ headers = ['Content-Type ' => 'application/json ' ];
124+ $ headers ['Content-Length ' ] = ($ content = $ this ->__content ())
125+ ? strlen ($ content )
126+ : 0 ;
127+ return $ content ;
128+ }
129+
130+ /**
131+ * Encodes the verification list retrieved from the state into a JSON string.
132+ *
133+ * @return string|false Returns the JSON-encoded string on success, or false on failure.
134+ */
135+ private function __content (): string |false
136+ {
137+ return json_encode ($ this ->state ->getVerifyList ());
103138 }
104139
105140 /**
106141 * Handles POST requests by parsing the request data and performing actions based on the method type.
107142 *
108143 * @param ServerRequestInterface|string $request The raw HTTP request string.
109144 * @param string &$response The response string to be modified based on the request handling.
110- * @param array &$content_type The variable to store the content type of the response.
145+ * @param array &$headers The variable to store the headers of the response.
111146 * @param string &$body The variable to store the body of the response.
112147 *
113148 * The function performs the following steps:
@@ -118,7 +153,7 @@ private function get(int|string &$response, array &$content_type, string &$body)
118153 * 5. Retrieves the verification list from the state.
119154 * 6. Based on the method type, either deletes an entry from the list or handles the default case.
120155 */
121- private function post (ServerRequestInterface |string $ request , int |string &$ response , array &$ content_type , string &$ body , bool $ bypass_token = false ): void
156+ private function post (ServerRequestInterface |string $ request , int |string &$ response , array &$ headers , string &$ body , bool $ bypass_token = false ): void
122157 {
123158 $ formData = $ request instanceof ServerRequestInterface
124159 ? $ request ->getHeaders ()
@@ -141,7 +176,7 @@ private function post(ServerRequestInterface|string $request, int|string &$respo
141176
142177 if (!$ bypass_token && ($ this ->state ->getToken () === 'changeme ' || $ token !== $ this ->state ->getToken ())) {
143178 $ response = Response::STATUS_UNAUTHORIZED ;
144- $ content_type = ['Content-Type ' => 'text/plain ' ];
179+ $ headers = ['Content-Type ' => 'text/plain ' ];
145180 $ body = 'Unauthorized ' ;
146181 return ;
147182 }
@@ -156,7 +191,7 @@ private function post(ServerRequestInterface|string $request, int|string &$respo
156191 $ existingIndex ,
157192 $ list ,
158193 $ response ,
159- $ content_type ,
194+ $ headers ,
160195 $ body
161196 );
162197 break ;
@@ -166,7 +201,7 @@ private function post(ServerRequestInterface|string $request, int|string &$respo
166201 $ ckey ,
167202 $ discord ,
168203 $ response ,
169- $ content_type ,
204+ $ headers ,
170205 $ body
171206 );
172207 break ;
@@ -185,17 +220,17 @@ private function post(ServerRequestInterface|string $request, int|string &$respo
185220 * @param string $ckey The ckey to be verified.
186221 * @param string $discord The discord identifier to be verified.
187222 * @param string &$response The response message to be set based on the verification result.
188- * @param array &$content_type The variable to store the content type of the response.
223+ * @param array &$headers The variable to store the headers of the response.
189224 * @param string &$body The variable to store the body of the response.
190225 */
191- private function __post (array &$ list , string $ ckey , string $ discord , int |string &$ response , array &$ content_type , string &$ body ): void
226+ private function __post (array &$ list , string $ ckey , string $ discord , int |string &$ response , array &$ headers , string &$ body ): void
192227 {
193228 $ existingCkeyIndex = array_search ($ ckey , array_column ($ list , 'ss13 ' ));
194229 $ existingDiscordIndex = array_search ($ discord , array_column ($ list , 'discord ' ));
195230
196231 if ($ existingCkeyIndex !== false || $ existingDiscordIndex !== false ) {
197232 $ response = Response::STATUS_FORBIDDEN ;
198- $ content_type = ['Content-Type ' => 'text/plain ' ];
233+ $ headers = ['Content-Type ' => 'text/plain ' ];
199234 $ body = 'Forbidden ' ;
200235 return ;
201236 }
@@ -206,9 +241,7 @@ private function __post(array &$list, string $ckey, string $discord, int|string
206241 ];
207242 PersistentState::writeJson ($ this ->state ->getJsonPath (), $ list );
208243 $ this ->state ->setVerifyList ($ list );
209- $ response = Response::STATUS_OK ;
210- $ content_type = ['Content-Type ' => 'application/json ' ];
211- $ body = json_encode ($ list );
244+ $ body = json_encode ($ this ->head ($ response , $ headers ));
212245 }
213246
214247 /**
@@ -217,22 +250,24 @@ private function __post(array &$list, string $ckey, string $discord, int|string
217250 * @param int|string|false $existingIndex The index of the item to delete, or false if the item does not exist.
218251 * @param array &$list The list from which the item will be deleted.
219252 * @param string &$response The HTTP response message to be returned.
220- * @param array &$content_type The variable to store the content type of the response.
253+ * @param array &$headers The variable to store the headers of the response.
221254 * @param string &$body The variable to store the body of the response.
222255 */
223- private function delete (int |string |false $ existingIndex , array &$ list , int |string &$ response , array &$ content_type , string &$ body ): void
256+ private function delete (int |string |false $ existingIndex , array &$ list , int |string &$ response , array &$ headers , string &$ body ): void
224257 {
225258 if ($ existingIndex === false ) {
226259 $ response = Response::STATUS_NOT_FOUND ;
227- $ content_type = ['Content-Type ' => 'text/plain ' ];
260+ $ headers = ['Content-Type ' => 'text/plain ' ];
228261 $ body = 'Not Found ' ;
229262 return ;
230263 }
231264 $ splice = array_splice ($ list , $ existingIndex , 1 );
232265 PersistentState::writeJson ($ this ->state ->getJsonPath (), $ list );
233266 $ this ->state ->setVerifyList ($ list );
234267 $ response = Response::STATUS_OK ;
235- $ content_type = ['Content-Type ' => 'application/json ' ];
236- $ body = json_encode ($ splice );
268+ $ headers = ['Content-Type ' => 'application/json ' ];
269+ $ headers ['Content-Length ' ] = ($ content = json_encode ($ splice ))
270+ ? strlen ($ body = $ content )
271+ : 0 ;
237272 }
238273}
0 commit comments