2626import java .util .Base64 ;
2727import java .util .Arrays ;
2828import java .util .HashMap ;
29+ import java .nio .charset .StandardCharsets ;
2930
3031public class WebServer {
3132 private HttpServer server ;
@@ -229,9 +230,10 @@ public void start() throws IOException {
229230 server .createContext ("/api/ping" , exchange -> {
230231 String resp = "{\" msg\" :\" pong\" }" ;
231232 exchange .getResponseHeaders ().add ("Content-Type" , "application/json; charset=utf-8" );
232- exchange .sendResponseHeaders (200 , resp .getBytes ().length );
233+ byte [] data = resp .getBytes (StandardCharsets .UTF_8 );
234+ exchange .sendResponseHeaders (200 , data .length );
233235 OutputStream os = exchange .getResponseBody ();
234- os .write (resp . getBytes () );
236+ os .write (data );
235237 os .close ();
236238 });
237239
@@ -330,7 +332,7 @@ public void start() throws IOException {
330332 return ;
331333 }
332334
333- JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes ()));
335+ JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes (), StandardCharsets . UTF_8 ));
334336 String email = req .optString ("email" , "" ).trim ().toLowerCase ();
335337 String language = req .optString ("language" , "en" );
336338
@@ -381,7 +383,9 @@ public void start() throws IOException {
381383 String code = codeService .generateCode (email );
382384 debugLog ("Generated verification code for email: " + email + ", code: " + code );
383385
384- boolean sent = mailService .sendCode (email , getMsg ("email.subject" , language ), code );
386+ // Get email subject from config.yml, fallback to default if not set
387+ String emailSubject = plugin .getConfig ().getString ("email_subject" , "VerifyMC Verification Code" );
388+ boolean sent = mailService .sendCode (email , emailSubject , code );
385389 JSONObject resp = new JSONObject ();
386390 resp .put ("success" , sent );
387391 resp .put ("msg" , sent ? getMsg ("email.sent" , language ) : getMsg ("email.failed" , language ));
@@ -399,7 +403,7 @@ public void start() throws IOException {
399403 server .createContext ("/api/register" , exchange -> {
400404 debugLog ("/api/register called" );
401405 if (!"POST" .equals (exchange .getRequestMethod ())) { exchange .sendResponseHeaders (405 , 0 ); exchange .close (); return ; }
402- JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes ()));
406+ JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes (), StandardCharsets . UTF_8 ));
403407 String email = req .optString ("email" , "" ).trim ().toLowerCase ();
404408 String code = req .optString ("code" );
405409 String uuid = req .optString ("uuid" );
@@ -557,7 +561,7 @@ public void start() throws IOException {
557561 exchange .close ();
558562 return ;
559563 }
560- JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes ()));
564+ JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes (), StandardCharsets . UTF_8 ));
561565 String password = req .optString ("password" );
562566 String language = req .optString ("language" , "en" );
563567
@@ -649,7 +653,7 @@ public void start() throws IOException {
649653 return ;
650654 }
651655
652- JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes ()));
656+ JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes (), StandardCharsets . UTF_8 ));
653657 String uuid = req .optString ("uuid" );
654658 String action = req .optString ("action" );
655659 String language = req .optString ("language" , "en" );
@@ -868,7 +872,7 @@ public void start() throws IOException {
868872 return ;
869873 }
870874
871- JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes ()));
875+ JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes (), StandardCharsets . UTF_8 ));
872876 String uuid = req .optString ("uuid" );
873877 String language = req .optString ("language" , "en" );
874878
@@ -936,7 +940,7 @@ public void start() throws IOException {
936940 return ;
937941 }
938942
939- JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes ()));
943+ JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes (), StandardCharsets . UTF_8 ));
940944 String uuid = req .optString ("uuid" );
941945 String language = req .optString ("language" , "en" );
942946
@@ -999,7 +1003,7 @@ public void start() throws IOException {
9991003 return ;
10001004 }
10011005
1002- JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes ()));
1006+ JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes (), StandardCharsets . UTF_8 ));
10031007 String uuid = req .optString ("uuid" );
10041008 String language = req .optString ("language" , "en" );
10051009
@@ -1062,7 +1066,7 @@ public void start() throws IOException {
10621066 return ;
10631067 }
10641068
1065- JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes ()));
1069+ JSONObject req = new JSONObject (new String (exchange .getRequestBody ().readAllBytes (), StandardCharsets . UTF_8 ));
10661070 String uuid = req .optString ("uuid" );
10671071 String username = req .optString ("username" );
10681072 String newPassword = req .optString ("newPassword" );
@@ -1287,8 +1291,38 @@ public void handle(HttpExchange exchange) throws IOException {
12871291 return ;
12881292 }
12891293 String mime = Files .probeContentType (file );
1290- if (mime == null ) mime = "application/octet-stream" ;
1291- exchange .getResponseHeaders ().add ("Content-Type" , mime );
1294+ if (mime == null ) {
1295+ // Try to determine MIME type from file extension
1296+ String fileName = file .getFileName ().toString ().toLowerCase ();
1297+ if (fileName .endsWith (".html" ) || fileName .endsWith (".htm" )) {
1298+ mime = "text/html" ;
1299+ } else if (fileName .endsWith (".css" )) {
1300+ mime = "text/css" ;
1301+ } else if (fileName .endsWith (".js" )) {
1302+ mime = "application/javascript" ;
1303+ } else if (fileName .endsWith (".json" )) {
1304+ mime = "application/json" ;
1305+ } else if (fileName .endsWith (".svg" )) {
1306+ mime = "image/svg+xml" ;
1307+ } else if (fileName .endsWith (".xml" )) {
1308+ mime = "text/xml" ;
1309+ } else {
1310+ mime = "application/octet-stream" ;
1311+ }
1312+ }
1313+
1314+ // Add charset=utf-8 for text-based content types
1315+ String contentType = mime ;
1316+ if (mime .startsWith ("text/" ) ||
1317+ mime .equals ("application/javascript" ) ||
1318+ mime .equals ("application/json" ) ||
1319+ mime .equals ("application/xml" ) ||
1320+ mime .equals ("text/xml" ) ||
1321+ mime .equals ("image/svg+xml" )) {
1322+ contentType = mime + "; charset=utf-8" ;
1323+ }
1324+
1325+ exchange .getResponseHeaders ().add ("Content-Type" , contentType );
12921326 byte [] data = Files .readAllBytes (file );
12931327 exchange .sendResponseHeaders (200 , data .length );
12941328 OutputStream os = exchange .getResponseBody ();
@@ -1312,7 +1346,7 @@ public void handle(HttpExchange exchange) throws IOException {
13121346
13131347 private void sendJson (HttpExchange exchange , JSONObject resp ) throws IOException {
13141348 JSONObject withCopy = withCopyright (resp );
1315- byte [] data = withCopy .toString ().getBytes ();
1349+ byte [] data = withCopy .toString ().getBytes (StandardCharsets . UTF_8 );
13161350 exchange .getResponseHeaders ().add ("Content-Type" , "application/json; charset=utf-8" );
13171351 exchange .sendResponseHeaders (200 , data .length );
13181352 exchange .getResponseBody ().write (data );
0 commit comments