-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogoutHandler.java
More file actions
30 lines (23 loc) · 986 Bytes
/
LogoutHandler.java
File metadata and controls
30 lines (23 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.util.Map;
import static util.ServerUtil.parseQueryParams;
public class LogoutHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
exchange.sendResponseHeaders(405, -1);
return;
}
String query = exchange.getRequestURI().getQuery();
Map<String, String> params = parseQueryParams(query);
String username = params.get("user");
if (username == null || !RegisterHandler.isAuthorized(exchange, username)) {
VirtualThreadHttpServer.sendResponse(exchange, 401, "Unauthorized");
return;
}
RegisterHandler.authStore.remove(username);
VirtualThreadHttpServer.sendResponse(exchange, 200, "Logged out " + username);
}
}