2023-04-13 06:27:32 +03:00
|
|
|
package org.eu.lumiere;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.Socket;
|
2023-04-14 02:32:17 +03:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
2023-04-14 02:59:36 +03:00
|
|
|
import java.util.NoSuchElementException;
|
2023-04-14 02:32:17 +03:00
|
|
|
import java.util.TimeZone;
|
2023-04-13 06:27:32 +03:00
|
|
|
|
|
|
|
import org.eu.lumiere.loggers.GlobalLogger;
|
|
|
|
import org.eu.lumiere.loggers.GlobalLogger.LogLevel;
|
2023-04-14 02:32:17 +03:00
|
|
|
import org.eu.lumiere.net.RequestListener;
|
2023-04-13 06:27:32 +03:00
|
|
|
import org.eu.lumiere.net.ServerEvents;
|
|
|
|
import org.eu.lumiere.net.http.HttpRequest;
|
|
|
|
import org.eu.lumiere.net.http.HttpRequestHandler;
|
|
|
|
import org.eu.lumiere.net.http.HttpResponse;
|
2023-04-14 02:59:36 +03:00
|
|
|
import org.eu.lumiere.utils.SimpleResponse;
|
2023-04-13 06:27:32 +03:00
|
|
|
|
|
|
|
public class Lumiere implements ServerEvents{
|
|
|
|
|
|
|
|
private GlobalLogger l = GlobalLogger.getLogger();
|
|
|
|
|
|
|
|
private int server_port = 8080;
|
2023-04-14 02:32:17 +03:00
|
|
|
private RequestListener server;
|
2023-04-13 06:27:32 +03:00
|
|
|
|
|
|
|
private HttpResponse header;
|
|
|
|
private HttpRequestHandler request;
|
|
|
|
|
|
|
|
public Lumiere(HttpRequestHandler rHandler) {
|
|
|
|
if((this.request = rHandler) == null) {
|
|
|
|
l.printf(LogLevel.ERROR, "Lumiere 'requestHandler' CANNOT BE NULL", null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
header = new HttpResponse(null, "HTTP/1.1 200 OK", "text/html");
|
|
|
|
header.setProperty("Server", "Lumiere Server");
|
2023-04-14 02:32:17 +03:00
|
|
|
header.setProperty("Accept-Ranges", "bytes");
|
2023-04-13 06:27:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onConnection(Socket socket) {
|
|
|
|
|
|
|
|
if(header == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
HttpResponse httpH = new HttpResponse(socket, header.getStatusLine(), header.getContentType());
|
|
|
|
header.getKeySet().forEach(key -> {
|
|
|
|
httpH.setProperty(key, header.getProperty(key));
|
|
|
|
});
|
|
|
|
|
2023-04-14 02:32:17 +03:00
|
|
|
// Add Date to the header
|
|
|
|
SimpleDateFormat gmtDate = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
|
|
|
|
gmtDate.setTimeZone(TimeZone.getTimeZone("GMT"));
|
|
|
|
httpH.setProperty("Date", gmtDate.format(new Date()));
|
2023-04-14 02:59:36 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
request.onRequestReceived(new HttpRequest(socket), httpH);
|
|
|
|
}catch (NoSuchElementException e) {
|
|
|
|
httpH.setStatus("HTTP/1.1 400 Bad Request");
|
|
|
|
new SimpleResponse("Bad Request", false).onRequestReceived(null, httpH);
|
|
|
|
}
|
|
|
|
|
2023-04-13 06:27:32 +03:00
|
|
|
try {
|
|
|
|
if(socket.isConnected())
|
|
|
|
socket.close();
|
|
|
|
}catch(IOException ex) {
|
|
|
|
l.printf(LogLevel.ERROR, ex.getMessage(), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void bootServer(int port) {
|
|
|
|
if(server != null) {
|
|
|
|
l.printf(LogLevel.ERROR, "Lumiere is already running", null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
server_port = port;
|
2023-04-14 02:32:17 +03:00
|
|
|
server = new RequestListener(this.getClass().getSimpleName(), this, server_port);
|
2023-04-13 06:27:32 +03:00
|
|
|
server.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public HttpResponse getSharedResponseHeader() {
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getCurrentPort() {
|
|
|
|
return server_port;
|
|
|
|
}
|
2023-04-14 05:34:38 +03:00
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append("Lumiere-server version 1.0").append("\n");
|
|
|
|
sb.append("lumiere-server at github https://github.com/DevSnaith/lumiere-server").append("\n");
|
|
|
|
System.out.write(sb.toString().getBytes());
|
|
|
|
}
|
2023-04-13 06:27:32 +03:00
|
|
|
}
|