Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
0d7a7152c4 | |||
490815efad | |||
ad099a4f0c |
3
.idea/vcs.xml
generated
3
.idea/vcs.xml
generated
@ -1,7 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="VcsDirectoryMappings">
|
<component name="VcsDirectoryMappings">
|
||||||
<mapping directory="" vcs="" />
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
|
|
||||||
|
Binary file not shown.
Binary file not shown.
@ -6,14 +6,12 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ClientThread extends Thread{
|
public class ClientThread extends Thread{
|
||||||
private Socket socket;
|
private final Socket socket;
|
||||||
private int id;
|
private final int id;
|
||||||
private PrintStream out;
|
private PrintStream out;
|
||||||
private InputStream in;
|
|
||||||
private String cmd;
|
private String cmd;
|
||||||
private String url;
|
private String url;
|
||||||
private String httpversion;
|
private final StringBuilder rqBuilder;
|
||||||
private StringBuilder rqBuilder;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Neuer Client wird erstellt
|
* Neuer Client wird erstellt
|
||||||
@ -34,7 +32,7 @@ public class ClientThread extends Thread{
|
|||||||
try{
|
try{
|
||||||
System.out.println("Socket von Client #" + this.id + " wurde gestartet!");
|
System.out.println("Socket von Client #" + this.id + " wurde gestartet!");
|
||||||
out = new PrintStream(socket.getOutputStream());
|
out = new PrintStream(socket.getOutputStream());
|
||||||
in = socket.getInputStream();
|
InputStream in = socket.getInputStream();
|
||||||
getRequest();
|
getRequest();
|
||||||
createResponse();
|
createResponse();
|
||||||
socket.close();
|
socket.close();
|
||||||
@ -45,50 +43,151 @@ 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
|
* Erstellt eine Respone Message anhand des gegebenen Requests
|
||||||
*/
|
*/
|
||||||
private void createResponse() {
|
private void createResponse() {
|
||||||
if (this.url != null) {
|
if (this.url != null) {
|
||||||
if (this.cmd.equals("GET")) {
|
if (this.cmd.equals("GET")) {
|
||||||
if (this.url.startsWith("/test")) {
|
if (this.url.startsWith("/messages")) {
|
||||||
out.print("HTTP/1.0 200 OK\r\n");
|
String lastBit = this.url.substring(this.url.lastIndexOf('/') + 1);
|
||||||
out.print("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
|
System.out.println("Last Bit: " + lastBit);
|
||||||
out.print("Server: Apache/0.8.4\r\n");
|
if(lastBit.equals("messages")){
|
||||||
out.print("Content-Type: text/html\r\n");
|
listAllMsg();
|
||||||
out.print("Content-Length: 59\r\n");
|
}else{
|
||||||
out.print("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
|
getMsg(Integer.parseInt(lastBit));
|
||||||
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>");
|
|
||||||
} else if (this.url.startsWith("/")) {
|
} else if (this.url.startsWith("/")) {
|
||||||
out.print("HTTP/1.0 200 OK\r\n");
|
startseite();
|
||||||
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>");
|
|
||||||
}
|
}
|
||||||
}else if (this.cmd.equals("POST")){
|
}else if (this.cmd.equals("POST")){
|
||||||
|
if (this.url.startsWith("/messages")) {
|
||||||
|
addMsg(id);
|
||||||
|
}
|
||||||
}else if (this.cmd.equals("PUT")){
|
}else if (this.cmd.equals("PUT")){
|
||||||
|
if (this.url.startsWith("/messages")) {
|
||||||
|
editMsg(id);
|
||||||
|
}
|
||||||
}else if (this.cmd.equals("DELETE")){
|
}else if (this.cmd.equals("DELETE")){
|
||||||
|
if (this.url.startsWith("/messages")) {
|
||||||
|
delMsg(id);
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
/*
|
sendError();
|
||||||
|
}
|
||||||
ERROR senden
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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>");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -109,7 +208,6 @@ public class ClientThread extends Thread{
|
|||||||
String host = requestsLines[1].split(" ")[1];
|
String host = requestsLines[1].split(" ")[1];
|
||||||
|
|
||||||
this.url = path;
|
this.url = path;
|
||||||
this.httpversion = version;
|
|
||||||
this.cmd = method;
|
this.cmd = method;
|
||||||
|
|
||||||
List<String> headers = new ArrayList<>();
|
List<String> headers = new ArrayList<>();
|
||||||
|
Reference in New Issue
Block a user