diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..9b0e3f3 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../../../../../:\Users\georg\OneDrive\FH\SWE\RestServer\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..217af47 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/.idea/description.html b/.idea/description.html new file mode 100644 index 0000000..cc10d56 --- /dev/null +++ b/.idea/description.html @@ -0,0 +1,2 @@ +Simple JavaFX 2.0 application that includes simple .fxml file with attached controller and Main class to quick start. Artifact to build JavaFX application is provided. + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..3e3960b --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b8ef383 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1c90ddd --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..3b00020 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..def6a6a --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/RestServer.iml b/RestServer.iml new file mode 100644 index 0000000..d5c0743 --- /dev/null +++ b/RestServer.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/out/production/RestServer/at/reisinger/Main.class b/out/production/RestServer/at/reisinger/Main.class new file mode 100644 index 0000000..0f39e02 Binary files /dev/null and b/out/production/RestServer/at/reisinger/Main.class differ diff --git a/out/production/RestServer/at/reisinger/server/Main.class b/out/production/RestServer/at/reisinger/server/Main.class new file mode 100644 index 0000000..aaf5b13 Binary files /dev/null and b/out/production/RestServer/at/reisinger/server/Main.class differ diff --git a/out/production/RestServer/at/reisinger/server/thread/ClientThread.class b/out/production/RestServer/at/reisinger/server/thread/ClientThread.class new file mode 100644 index 0000000..6981913 Binary files /dev/null and b/out/production/RestServer/at/reisinger/server/thread/ClientThread.class differ diff --git a/src/at/reisinger/server/Main.java b/src/at/reisinger/server/Main.java new file mode 100644 index 0000000..abdd46a --- /dev/null +++ b/src/at/reisinger/server/Main.java @@ -0,0 +1,27 @@ +package at.reisinger.server; + +import at.reisinger.server.thread.ClientThread; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + +public class Main { + static final int port = 80; + + public static void main(String[] args) { + System.out.println("Starte Server auf Port 80"); + ServerSocket serverSocket = null; + try { + serverSocket = new ServerSocket(port); + + int id = 0; + while (true){ + Socket socket = serverSocket.accept(); + new ClientThread(id, socket).start(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/at/reisinger/server/thread/ClientThread.java b/src/at/reisinger/server/thread/ClientThread.java new file mode 100644 index 0000000..2f39de0 --- /dev/null +++ b/src/at/reisinger/server/thread/ClientThread.java @@ -0,0 +1,126 @@ +package at.reisinger.server.thread; + +import java.io.*; +import java.net.Socket; +import java.util.ArrayList; +import java.util.List; + +public class ClientThread extends Thread{ + private Socket socket; + private int id; + private PrintStream out; + private InputStream in; + private String cmd; + private String url; + private String httpversion; + private StringBuilder rqBuilder; + + /** + * Neuer Client wird erstellt + * @param id Id des Clients + * @param socket Socket des Clients + */ + public ClientThread(int id, Socket socket) { + this.id = id; + this.socket = socket; + this.rqBuilder = new StringBuilder(); + } + + /** + * Hauptmethode des ClientThreads + */ + @Override + public void run() { + try{ + System.out.println("Socket von Client #" + this.id + " wurde gestartet!"); + out = new PrintStream(socket.getOutputStream()); + in = socket.getInputStream(); + getRequest(); + createResponse(); + socket.close(); + System.out.println("Socket von Client #" + this.id + " wurde geschlossen!"); + }catch (IOException e){ + e.printStackTrace(); + } + + } + + /** + * Erstellt eine Respone Message anhand des gegebenen Requests + */ + private void createResponse() { + if (this.url != null) { + if (this.cmd.equals("GET")) { + if (this.url.startsWith("/test")) { + out.print("HTTP/1.0 200 OK\r\n"); + out.print("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n"); + out.print("Server: Apache/0.8.4\r\n"); + out.print("Content-Type: text/html\r\n"); + out.print("Content-Length: 59\r\n"); + out.print("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n"); + out.print("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n"); + out.print("\r\n"); + out.print("Test"); + out.print("

Das ist ein Test

"); + } else if (this.url.startsWith("/")) { + out.print("HTTP/1.0 200 OK\r\n"); + out.print("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n"); + out.print("Server: Apache/0.8.4\r\n"); + out.print("Content-Type: text/html\r\n"); + out.print("Content-Length: 59\r\n"); + out.print("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n"); + out.print("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n"); + out.print("\r\n"); + out.print("Hello World!"); + out.print("

Hallo Welt!

"); + } + }else if (this.cmd.equals("POST")){ + + }else if (this.cmd.equals("PUT")){ + + }else if (this.cmd.equals("DELETE")){ + + }else{ + /* + + ERROR senden + + + */ + } + } + } + + /** + * Holt den HTTP Request + */ + private void getRequest() throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); + String line; + while (!(line = bufferedReader.readLine()).isBlank()) { + rqBuilder.append(line + "\r\n"); + } + String request = rqBuilder.toString(); + String[] requestsLines = request.split("\r\n"); + String[] requestLine = requestsLines[0].split(" "); + String method = requestLine[0]; + String path = requestLine[1]; + String version = requestLine[2]; + String host = requestsLines[1].split(" ")[1]; + + this.url = path; + this.httpversion = version; + this.cmd = method; + + List headers = new ArrayList<>(); + for (int h = 2; h < requestsLines.length; h++) { + String header = requestsLines[h]; + headers.add(header); + } + + String accessLog = String.format("Client %s, method %s, path %s, version %s, host %s, headers %s", + socket.toString(), method, path, version, host, headers.toString()); + System.out.println(accessLog); + + } +}