Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
0d7a7152c4 | |||
490815efad | |||
ad099a4f0c |
5
.idea/vcs.xml
generated
5
.idea/vcs.xml
generated
@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="" />
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
|
||||
</project>
|
Binary file not shown.
Binary file not shown.
@ -6,14 +6,12 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ClientThread extends Thread{
|
||||
private Socket socket;
|
||||
private int id;
|
||||
private final Socket socket;
|
||||
private final int id;
|
||||
private PrintStream out;
|
||||
private InputStream in;
|
||||
private String cmd;
|
||||
private String url;
|
||||
private String httpversion;
|
||||
private StringBuilder rqBuilder;
|
||||
private final StringBuilder rqBuilder;
|
||||
|
||||
/**
|
||||
* Neuer Client wird erstellt
|
||||
@ -34,7 +32,7 @@ public class ClientThread extends Thread{
|
||||
try{
|
||||
System.out.println("Socket von Client #" + this.id + " wurde gestartet!");
|
||||
out = new PrintStream(socket.getOutputStream());
|
||||
in = socket.getInputStream();
|
||||
InputStream in = socket.getInputStream();
|
||||
getRequest();
|
||||
createResponse();
|
||||
socket.close();
|
||||
@ -45,52 +43,153 @@ public class ClientThread extends Thread{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt alle Nachrichten aus
|
||||
*/
|
||||
private void listAllMsg(){
|
||||
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("<TITLE>GET /messages</TITLE>");
|
||||
out.print("<P>Listet alle Nachrichten</P>");
|
||||
}
|
||||
|
||||
/**
|
||||
* gibt die startseite aus
|
||||
*/
|
||||
private void startseite(){
|
||||
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("<TITLE>Startseite</TITLE>");
|
||||
out.print("<p>lists all messages: GET /messages</p>" +
|
||||
"<p>add message: POST /messages (Payload: the message; Response an id like 1)</p>" +
|
||||
"<p>show first message: GET /messages/1</p> " +
|
||||
"<p>show third message: GET /messages/3</p>" +
|
||||
"<p>update first message: PUT /messages/1 (Payload: the message)</p>" +
|
||||
"<p>remove first message: DELETE /messages/1</p>");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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("<TITLE>Test</TITLE>");
|
||||
out.print("<P>Das ist ein Test</P>");
|
||||
if (this.url.startsWith("/messages")) {
|
||||
String lastBit = this.url.substring(this.url.lastIndexOf('/') + 1);
|
||||
System.out.println("Last Bit: " + lastBit);
|
||||
if(lastBit.equals("messages")){
|
||||
listAllMsg();
|
||||
}else{
|
||||
getMsg(Integer.parseInt(lastBit));
|
||||
}
|
||||
} 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("<TITLE>Hello World!</TITLE>");
|
||||
out.print("<P>Hallo Welt!</P>");
|
||||
startseite();
|
||||
}
|
||||
}else if (this.cmd.equals("POST")){
|
||||
|
||||
if (this.url.startsWith("/messages")) {
|
||||
addMsg(id);
|
||||
}
|
||||
}else if (this.cmd.equals("PUT")){
|
||||
|
||||
if (this.url.startsWith("/messages")) {
|
||||
editMsg(id);
|
||||
}
|
||||
}else if (this.cmd.equals("DELETE")){
|
||||
|
||||
if (this.url.startsWith("/messages")) {
|
||||
delMsg(id);
|
||||
}
|
||||
}else{
|
||||
/*
|
||||
|
||||
ERROR senden
|
||||
|
||||
|
||||
*/
|
||||
sendError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Holt eine Nachricht
|
||||
* @param id ID der nachricht die geholt werden soll
|
||||
*/
|
||||
private void getMsg(int id) {
|
||||
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("<TITLE>Get MSG</TITLE>");
|
||||
out.print("<p> Aktuelle MSG Number: " + id + "</p>");
|
||||
}
|
||||
|
||||
/**
|
||||
* sendet eine Fehlermeldung zurück
|
||||
*/
|
||||
private void sendError() {
|
||||
out.print("HTTP/1.0 500 ERR\r\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Löscht eine Nachricht
|
||||
* @param id Nachricht die zu löschen ist
|
||||
*/
|
||||
private void delMsg(int id) {
|
||||
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("<TITLE>Del MSG</TITLE>");
|
||||
out.print("<p> Aktuelle MSG Number: " + id + "</p>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Bearbeitet eine Nachricht
|
||||
* @param id Nachricht die zu bearbeiten ist
|
||||
*/
|
||||
private void editMsg(int id) {
|
||||
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("<TITLE>Edit MSG</TITLE>");
|
||||
out.print("<p> Aktuelle MSG Number: " + id + "</p>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt eine Nachricht hinzu
|
||||
* @param id Id der neuen Nachricht
|
||||
*/
|
||||
private void addMsg(int id) {
|
||||
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("<TITLE>Add MSG</TITLE>");
|
||||
out.print("<p> Aktuelle MSG Number: " + id + "</p>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Holt den HTTP Request
|
||||
*/
|
||||
@ -109,7 +208,6 @@ public class ClientThread extends Thread{
|
||||
String host = requestsLines[1].split(" ")[1];
|
||||
|
||||
this.url = path;
|
||||
this.httpversion = version;
|
||||
this.cmd = method;
|
||||
|
||||
List<String> headers = new ArrayList<>();
|
||||
|
Reference in New Issue
Block a user