Change file name

This commit is contained in:
DevSnaith 2023-04-19 10:45:25 +03:00
parent f9cceb68a0
commit b9036ac208
3 changed files with 175 additions and 0 deletions

View File

@ -0,0 +1,55 @@
package org.eu.lumiere.net.http;
import java.net.Socket;
import java.util.HashMap;
import org.eu.lumiere.error.ErrorHandler;
import org.eu.lumiere.error.HTTPException;
import org.eu.lumiere.loggers.GlobalLogger;
import org.eu.lumiere.loggers.GlobalLogger.LogLevel;
import org.eu.lumiere.net.RequestHandler;
public class HTTPController implements RequestHandler {
private GlobalLogger l = GlobalLogger.getLogger();
private HashMap<String, RequestHandler> handlers = new HashMap<>();
private HTTPException hException = ErrorHandler.getHandler();
public boolean addHandler(String path, RequestHandler handler) {
if(handler == null)
return (boolean) l.printf(LogLevel.WARNING, "The handler ["+path+"] is null", false);
if(handler.equals(this))
return (boolean) l.printf(LogLevel.WARNING, "The handler cannot be the HttpController", false);
l.printf(LogLevel.INFO, "A Handler for " + path + " has been created", null);
handlers.put(path, handler);
return true;
}
public void setHTTPException(HTTPException ex) {
hException = ex == null ? ErrorHandler.getHandler() : ex;
}
@Override
public void onRequestReceived(Socket socket, HTTPRequest request, HTTPResponse response) {
String clientUrl = request.getURL();
if(handlers.containsKey(clientUrl)) {
handlers.get(clientUrl).onRequestReceived(socket, request, response);
} else {
hException.onException(response, 404, "where are you going dude?");
}
}
public RequestHandler getHandler(String path) {
return handlers.get(path);
}
public RequestHandler[] getHandlers() {
return handlers.values().toArray(new RequestHandler[0]);
}
public String[] getPaths() {
return handlers.keySet().toArray(new String[0]);
}
}

View File

@ -0,0 +1,51 @@
package org.eu.lumiere.net.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.StringTokenizer;
public class HTTPRequest {
private String reqMethod, reqPath, reqVersion;
private Properties properties;
public HTTPRequest(InputStream os) throws IOException {
InputStreamReader stream = new InputStreamReader(os);
BufferedReader reader = new BufferedReader(stream);
StringTokenizer t = new StringTokenizer(reader.readLine());
reqMethod = t.nextToken();
reqPath = t.nextToken();
reqVersion = t.nextToken();
String line;
properties = new Properties();
while((line = reader.readLine()) != null) {
String[] split;
if((split = line.split(":", 2)).length >= 2) {
properties.setProperty(split[0], split[1].replaceFirst ("^ *", ""));
}else {
break;
}
}
}
public String[] getKeys() {
return properties.keySet().toArray(new String[0]);
}
public String getProperty(String key) {
return properties.getProperty(key);
}
public String getMethod() {
return reqMethod;
}
public String getURL() {
return reqPath;
}
public String getHttpVersion() {
return reqVersion;
}
}

View File

@ -0,0 +1,69 @@
package org.eu.lumiere.net.http;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.Set;
import org.eu.lumiere.loggers.GlobalLogger;
import org.eu.lumiere.loggers.GlobalLogger.LogLevel;
public class HTTPResponse {
private GlobalLogger l = GlobalLogger.getLogger();
private String status_line = "HTTP/1.0 200 OK";
private String content_type = "text/html";
private LinkedHashMap<String, String> httph;
private OutputStream os;
public HTTPResponse(OutputStream os, String status_line, String content_type) {
this.content_type = content_type == null ? this.content_type : content_type;
if(status_line != null && !status_line.isEmpty())
this.status_line = status_line;
httph = httph == null ? new LinkedHashMap<>() : httph;
this.os = os;
}
public void push(String body) {
try {
StringBuilder build = new StringBuilder(status_line+"\r\n");
build.append("Server: Lumiere Server").append("\r\n");
for(String key : httph.keySet()) {
build.append(String.format("%s: %s\r\n", key, getProperty(key)));
}
build.append("Content-Length: " + body.length()).append("\r\n");
os.write(build.append("\r\n").append(body).toString().getBytes());
} catch (IOException e) {
l.printf(LogLevel.ERROR, e.getMessage(), null);
}
}
public void setProperty(String key, String value) {
if(!httph.containsKey(key))
httph.put(key, value);
}
public void setStatus(String sl) {
this.status_line = sl;
}
public void setContentType(String ct) {
this.content_type = ct;
}
public String getStatus() {
return this.status_line;
}
public String getContentType() {
return this.content_type;
}
public String getProperty(String key) {
return httph.get(key);
}
public Set<String> getKeySet() {
return httph.keySet();
}
}