-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.java
More file actions
98 lines (85 loc) · 2.4 KB
/
server.java
File metadata and controls
98 lines (85 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
//import java.nio.file.FileSystems;
//import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class Server {
private static final String CLOSE_COMMAND = "CLOSE";
private static final int PORT = 4444;
public static void main(String[] args) throws IOException {
boolean closing = false;
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Started server on port " + PORT);
while (!closing) {
try (Socket clientSocket = serverSocket.accept()) {
System.out.println("Accepted connection from client: "+ clientSocket);
InputStream inStream = clientSocket.getInputStream();
OutputStream outStream = clientSocket.getOutputStream();
try (BufferedReader in = new BufferedReader(new InputStreamReader(inStream));
PrintWriter out = new PrintWriter(outStream, true); {
String s = in.readLine();
LinkedList<String> lines = new LinkedList<String>();
while (!s.equals("")) {
lines.add(s);
s = in.readLine();
}
String[] parts = lines.get(0).split(" ");
System.out.println(parts[0]);
boolean file_exists = new File(".\\sources_html\\"+ parts[1]).exists();
switch (parts[0]) {
case parts[0] == "GET":
if (parts[1] == "index.html") {
/*List<String> file = (ArrayList<String>) Files.readAllLines(FileSystems.getDefault().getPath("sources_html", parts[1]), Charset.defaultCharset());
Iterator<String> it = file.iterator();
while (it.hasNext()) {
out.write((String) it.next());*/
out.write("200 OK");
}
else {
out.write("404 NOT FOUND");
}
break;
case "PUT":
file_exists = new File(".\\sources_html\\" + parts[1]).exists();
if (file_exists) {
out.write("403 FORBIDDEN");
} else {
out.write("404 NOT FOUND");
}
break;
case "POST":
file_exists = new File(".\\sources_html\\" + parts[1]).exists();
if (file_exists) {
out.write("403 FORBIDDEN");
} else {
out.write("404 NOT FOUND");
}
break;
case "DELETE":
file_exists = new File(".\\sources_html\\" + parts[1]).exists();
if (file_exists) {
out.write("403 FORBIDDEN");
} else {
out.write("404 NOT FOUND");
}
break;
default:
out.write("501 Not Implemented");
}
}
}
}
}
}
}