Select Git revision
MainActivity2.java
InternalWebServer.java 7.53 KiB
package com.slprojects.slcraftplugin.parallelTasks;
import com.slprojects.slcraftplugin.Main;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;
import java.net.URLEncoder;
public class InternalWebServer {
@SuppressWarnings({ "unchecked", "InfiniteLoopStatement" })
public static void startServer(Main plugin){
int serverPort = plugin.getConfig().getInt("internal-webserver-port");
plugin.getServer().getConsoleSender().sendMessage("["+ plugin.getName() +"] Lancement du serveur web intégré sur le port " + ChatColor.GOLD + serverPort);
plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "["+ plugin.getName() +"] Attention! Le serveur ne fonctionne pas avec les requêtes https!");
// On fait un thread pour écouter le port
Runnable serverThread = () -> {
try {
ServerSocket serverSocket = new ServerSocket(serverPort);
while (true) {
Socket client = serverSocket.accept();
//plugin.getServer().getConsoleSender().sendMessage("Nouvelle connexion sur le port " + ChatColor.GOLD + serverPort);
// Get input and output streams to talk to the client
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream());
// Start sending our reply, using the HTTP 1.1 protocol
out.print("HTTP/1.1 200 \r\n"); // Version & status code
out.print("Content-Type: application/json\r\n"); // The type of data
out.print("Connection: close\r\n"); // Will close stream
out.print("\r\n"); // End of headers
// Now, read the HTTP request from the client, and send it
// right back to the client as part of the body of our
// response. The client doesn't disconnect, so we never get
// an EOF. It does sends an empty line at the end of the
// headers, though. So when we see the empty line, we stop
// reading. This means we don't mirror the contents of POST
// requests, for example. Note that the readLine() method
// works with Unix, Windows, and Mac line terminators.
String line, commandName = "";
String[] aliases = new String[0];
while ((line = in.readLine()) != null) {
if (line.equals("")) {
break;
}
// On va regarder si la ligne commence par GET
if (line.startsWith("GET")) {
// On split par les espaces
String[] split = line.split(" ");
// Et on récupère le nom de la commande
String command = split[1];
// On split par des /
aliases = command.split("/");
// On récupère le nom de la commande
commandName = aliases[1];
// On ne process pas la commande ici car ça cause des problèmes vu qu'on va renvoyer le résultat avant que le client n'écoute
}
}