-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebServerThread.java
executable file
·117 lines (96 loc) · 5.03 KB
/
WebServerThread.java
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.util.StringTokenizer;
import java.util.Properties;
import java.nio.file.*;
import java.util.Date;
public class WebServerThread extends Thread {
private Socket connectionFromClient;
public WebServerThread(Socket connectionFromClient) {
this.connectionFromClient = connectionFromClient;
start();
}
public void run() {
try {
Properties props = new Properties();
props.load(new FileInputStream("./server.config"));
String root = props.getProperty("root");
InputStream in = connectionFromClient.getInputStream();
OutputStream out = connectionFromClient.getOutputStream();
BufferedReader headerReader = new BufferedReader(new InputStreamReader(in));
BufferedWriter headerWriter = new BufferedWriter(new OutputStreamWriter(out));
DataOutputStream dataOut = new DataOutputStream(out);
String header = headerReader.readLine();
StringTokenizer strk = new StringTokenizer(header, " ");
String verb = strk.nextToken();
if (verb.equals("GET") || verb.equals("HEAD")) {
String location = strk.nextToken();
String newLocation = props.getProperty(location);
try {
if (newLocation == null) {
String path = root + location;
FileInputStream fileIn = new FileInputStream(path);
int fileSize = fileIn.available();
String fileType = Files.probeContentType(Paths.get(path));
header = "HTTP/1.1 200 OK\nContent-Length: " + fileSize + "\nConnection: Close\nContent-Type: "
+ fileType + "\nDate: " + new Date() + "\n\n";
headerWriter.write(header, 0, header.length());
headerWriter.flush();
if (verb.equals("GET")) {
byte[] bytes = new byte[fileSize];
fileIn.read(bytes);
fileIn.close();
dataOut.write(bytes, 0, fileSize);
}
} else {
String path = root + newLocation;
FileInputStream fileIn = new FileInputStream(path);
int fileSize = fileIn.available();
String fileType = Files.probeContentType(Paths.get(path));
header = "HTTP/1.1 302 Found\nContent-Length: " + fileSize + "\nLocation: " + newLocation
+ "\nConnection: Close\nContent-Type: " + fileType + "\nDate: " + new Date() + "\n\n";
headerWriter.write(header, 0, header.length());
headerWriter.flush();
if (verb.equals("GET")) {
byte[] bytes = new byte[fileSize];
fileIn.read(bytes);
fileIn.close();
dataOut.write(bytes, 0, fileSize);
}
}
} catch (Exception ex) {
String path = root + "/error.html";
FileInputStream fileIn = new FileInputStream(path);
int fileSize = fileIn.available();
String fileType = Files.probeContentType(Paths.get(path));
header = "HTTP/1.1 404 NOT FOUND\nContent-Length: " + fileSize
+ "\nConnection: Close\nContent-Type: " + fileType + "\nDate: " + new Date() + "\n\n";
headerWriter.write(header, 0, header.length());
headerWriter.flush();
if (verb.equals("GET")) {
byte[] bytes = new byte[fileSize];
fileIn.read(bytes);
fileIn.close();
dataOut.write(bytes, 0, fileSize);
}
}
} else {
String path = root + "/notImplemented.html";
FileInputStream fileIn = new FileInputStream(path);
int fileSize = fileIn.available();
String fileType = Files.probeContentType(Paths.get(path));
header = "HTTP/1.1 501 NOT IMPLEMENTED\nContent-Length: " + fileSize
+ "\nConnection: Close\nContent-Type: " + fileType + "\nDate: " + new Date() + "\n\n";
headerWriter.write(header, 0, header.length());
headerWriter.flush();
byte[] bytes = new byte[fileSize];
fileIn.read(bytes);
fileIn.close();
dataOut.write(bytes, 0, fileSize);
}
connectionFromClient.close();
} catch (Exception ex) {
}
}
}