From b9036ac208b0e92b3c41e17dcad9c4aab8689592 Mon Sep 17 00:00:00 2001 From: DevSnaith <65865554+DevSnaith@users.noreply.github.com> Date: Wed, 19 Apr 2023 10:45:25 +0300 Subject: [PATCH] Change file name --- .../eu/lumiere/net/http/HTTPController.java | 55 +++++++++++++++ src/org/eu/lumiere/net/http/HTTPRequest.java | 51 ++++++++++++++ src/org/eu/lumiere/net/http/HTTPResponse.java | 69 +++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 src/org/eu/lumiere/net/http/HTTPController.java create mode 100644 src/org/eu/lumiere/net/http/HTTPRequest.java create mode 100644 src/org/eu/lumiere/net/http/HTTPResponse.java diff --git a/src/org/eu/lumiere/net/http/HTTPController.java b/src/org/eu/lumiere/net/http/HTTPController.java new file mode 100644 index 0000000..27501fa --- /dev/null +++ b/src/org/eu/lumiere/net/http/HTTPController.java @@ -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 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]); + } + +} diff --git a/src/org/eu/lumiere/net/http/HTTPRequest.java b/src/org/eu/lumiere/net/http/HTTPRequest.java new file mode 100644 index 0000000..f15ce41 --- /dev/null +++ b/src/org/eu/lumiere/net/http/HTTPRequest.java @@ -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; + } +} diff --git a/src/org/eu/lumiere/net/http/HTTPResponse.java b/src/org/eu/lumiere/net/http/HTTPResponse.java new file mode 100644 index 0000000..8d63f0e --- /dev/null +++ b/src/org/eu/lumiere/net/http/HTTPResponse.java @@ -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 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 getKeySet() { + return httph.keySet(); + } +}