From 195a208f67b4d3d6c61edf0529c78a76c9ad8b3c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 15 Jun 2017 22:12:02 +0200 Subject: [PATCH] User mgmnt fixes, login preparation --- .../website/ButtonWebsiteModule.java | 1 + src/buttondevteam/website/io/IOHelper.java | 31 +++++++++++++++++++ src/buttondevteam/website/page/LoginPage.java | 29 +++++++++++++++++ .../website/page/ProfilePage.java | 19 ++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 src/buttondevteam/website/page/LoginPage.java create mode 100644 src/buttondevteam/website/page/ProfilePage.java diff --git a/src/buttondevteam/website/ButtonWebsiteModule.java b/src/buttondevteam/website/ButtonWebsiteModule.java index 02b22d5..f232878 100644 --- a/src/buttondevteam/website/ButtonWebsiteModule.java +++ b/src/buttondevteam/website/ButtonWebsiteModule.java @@ -111,6 +111,7 @@ public class ButtonWebsiteModule extends JavaPlugin { @Override public void onEnable() { addPage(new IndexPage()); + TBMCCoreAPI.RegisterUserClass(WebUser.class); Bukkit.getScheduler().runTaskAsynchronously(this, () -> { this.getLogger().info("Starting webserver..."); server.setExecutor( diff --git a/src/buttondevteam/website/io/IOHelper.java b/src/buttondevteam/website/io/IOHelper.java index 4b65d8a..156983c 100644 --- a/src/buttondevteam/website/io/IOHelper.java +++ b/src/buttondevteam/website/io/IOHelper.java @@ -3,6 +3,8 @@ package buttondevteam.website.io; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -148,4 +150,33 @@ public class IOHelper { SendLogoutHeaders(exchange); return null; } + + public static Map parseQueryString(HttpExchange exchange) { + String qs = exchange.getRequestURI().getRawQuery(); + Map result = new HashMap<>(); + if (qs == null) + return result; + + int last = 0, next, l = qs.length(); + while (last < l) { + next = qs.indexOf('&', last); + if (next == -1) + next = l; + + if (next > last) { + int eqPos = qs.indexOf('=', last); + try { + if (eqPos < 0 || eqPos > next) + result.put(URLDecoder.decode(qs.substring(last, next), "utf-8"), ""); + else + result.put(URLDecoder.decode(qs.substring(last, eqPos), "utf-8"), + URLDecoder.decode(qs.substring(eqPos + 1, next), "utf-8")); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); // will never happen, utf-8 support is mandatory for java + } + } + last = next + 1; + } + return result; + } } diff --git a/src/buttondevteam/website/page/LoginPage.java b/src/buttondevteam/website/page/LoginPage.java new file mode 100644 index 0000000..3214d65 --- /dev/null +++ b/src/buttondevteam/website/page/LoginPage.java @@ -0,0 +1,29 @@ +package buttondevteam.website.page; + +import java.util.Map; + +import com.sun.net.httpserver.HttpExchange; + +import buttondevteam.website.io.IOHelper; +import buttondevteam.website.io.Response; + +public class LoginPage extends Page { + + @Override + public String GetName() { + return "login"; + } + + @Override + public Response handlePage(HttpExchange exchange) { + Map q = IOHelper.parseQueryString(exchange); + if (q == null || !q.containsKey("type")) + return new Response(400, "400 Bad request", exchange); + String type = q.get("type"); + /*if (type.equalsIgnoreCase("getstate")) + return new Response(200, "TODO", exchange); // TO!DO: Store and return a random state and check on other types + String state = q.get("state"), code = q.get("code");*/ + return new Response(200, "", exchange); + } + +} diff --git a/src/buttondevteam/website/page/ProfilePage.java b/src/buttondevteam/website/page/ProfilePage.java new file mode 100644 index 0000000..22467e9 --- /dev/null +++ b/src/buttondevteam/website/page/ProfilePage.java @@ -0,0 +1,19 @@ +package buttondevteam.website.page; + +import com.sun.net.httpserver.HttpExchange; + +import buttondevteam.website.io.Response; + +public class ProfilePage extends Page { + + @Override + public String GetName() { + return "profile"; + } + + @Override + public Response handlePage(HttpExchange exchange) { + return new Response(200, "Under construction", exchange); + } + +}