Repository navigation
Expand file tree
/
Copy pathClientHandler.java
More file actions
282 lines (244 loc) · 12.3 KB
/
Copy pathClientHandler.java
File metadata and controls
282 lines (244 loc) · 12.3 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.file.Files;
import java.util.List;
public class ClientHandler implements Runnable {
private Socket clientSocket;
private String publicPath;
private String clientIP;
public ClientHandler(Socket socket, String publicPath) {
this.clientSocket = socket;
this.publicPath = publicPath;
this.clientIP = clientSocket.getInetAddress().getHostAddress();
}
@Override
public void run() {
// in for reading request, out for writing response
try (
// InputStream instead of BufferedReader for reading raw bytes and handling both text and binary data (like images)
InputStream in = clientSocket.getInputStream(); OutputStream rawOut = clientSocket.getOutputStream(); PrintWriter out = new PrintWriter(rawOut, true)) {
// İlk satırı (Request Line) StreamUtils ile okuyoruz
String requestLine = StreamUtils.readLineFromStream(in);
if (requestLine == null || requestLine.trim().isEmpty()) {
return;
}
int contentLength = 0;
String boundary = ""; // for multipart/form-data, we need to parse the boundary to separate different parts of the form data
String headerLine;
// headers end with an empty line, so we read until we find an empty line. "in" look like this: GET /index.html HTTP/1.1 \r\n Host: localhost \r\n Content-Length: 123 \r\n \r\n (empty line indicates end of headers)
while (!(headerLine = StreamUtils.readLineFromStream(in)).isEmpty()) {
if (headerLine.startsWith("Content-Length:")) {
contentLength = Integer.parseInt(headerLine.split(":")[1].trim()); // get the content length for POST request body
}
// for file upload, we need to parse the content type to get the boundary for multipart/form-data
if (headerLine.startsWith("Content-Type:")) {
String contentType = headerLine.substring("Content-Type:".length()).trim();
if (contentType.contains("multipart/form-data")) {
String[] partsInfo = contentType.split(";");
for (String part : partsInfo) {
if (part.trim().startsWith("boundary=")) {
boundary = part.split("=")[1].trim();
}
}
}
}
}
String[] parts = requestLine.split(" ");
if (parts.length < 3) { // method, path, version (GET /index.html HTTP/1.1)
sendError(out, clientIP, "400 Bad Request");
return;
}
System.out.println("Client Request: " + requestLine + " from " + clientIP); // Debug log
// Parse the request line ( GET /index.html HTTP/1.1 )
String method = parts[0];
String requestedPath = parts[1];
switch (method) {
case "GET" ->
handleGetRequest(requestedPath, out, rawOut);
case "POST" ->
// added boundary
handlePostRequest(requestedPath, contentLength, boundary, in, out);
default ->
sendError(out, clientIP, "400 Bad Request");
}
} catch (IOException e) {
System.err.println("Connection error: " + e.getMessage());
} finally {
try {
clientSocket.close();
} catch (IOException e) {
System.err.println("Error closing client socket: " + e.getMessage());
}
}
}
private void handleGetRequest(String path, PrintWriter headerOut, OutputStream dataOut) throws IOException {
if (path.equals("/test-redirect")) {
headerOut.println("HTTP/1.1 302 Found");
headerOut.println("Location: /index.html"); // redirect to index.html
headerOut.println("Connection: close");
headerOut.println();
headerOut.flush();
System.out.println("Client Request: " + path + " - status code 302 from " + clientIP);
return;
}
// if path is root, serve index.html
if (path.equals("/")) {
path = "/index.html";
}
File file = new File(publicPath, path);
if (file.isDirectory()) {
file = new File(file, "index.html");
}
// 2. Security Check: Avoiding Path Traversal
if (!file.getCanonicalPath().startsWith(new File(publicPath).getCanonicalPath())) {
sendError(headerOut, clientIP, "403 Forbidden");
return;
}
if (file.exists() && !file.isDirectory()) {
byte[] fileBytes = Files.readAllBytes(file.toPath());
// Send the HTTP response headers
headerOut.println("HTTP/1.1 200 OK");
headerOut.println("Content-Type: " + getContentType(file.getName())); // Set the correct content type based on file extension
headerOut.println("Content-Length: " + fileBytes.length);
headerOut.println("Connection: close");
headerOut.println();
headerOut.flush();
// Send the file content as bytes, so the image can be sent correctly
dataOut.write(fileBytes);
dataOut.flush();
System.out.println("Client Request: " + path + " - status code 200 from " + clientIP);
} else {
// control if 404.html exists in public directory, if yes serve it, else send a simple 404 message
File notFoundPage = new File(publicPath, "/404.html");
if (notFoundPage.exists()) {
byte[] fileBytes = Files.readAllBytes(notFoundPage.toPath());
headerOut.println("HTTP/1.1 404 Not Found");
headerOut.println("Content-Type: text/html");
headerOut.println("Content-Length: " + fileBytes.length);
headerOut.println("Connection: close");
headerOut.println();
headerOut.flush();
dataOut.write(fileBytes);
dataOut.flush();
System.out.println("Client Request: " + path + " - status code 404 from " + clientIP);
} else {
// send a simple 404 message if 404.html is not found
sendError(headerOut, clientIP, "404 Not Found");
}
}
}
private String getContentType(String path) {
if (path.endsWith(".html") || path.endsWith(".htm")) {
return "text/html";
}
if (path.endsWith(".png")) {
return "image/png";
}
return "text/plain";
}
private void sendError(PrintWriter out, String clientIP, String status) {
System.out.println("Response: " + status + " for client " + clientIP);
out.println("HTTP/1.1 " + status);
out.println("Content-Type: text/html");
out.println();
out.println("<html><body><h1>" + status + "</h1></body></html>");
}
private void handlePostRequest(String path, int contentLength, String boundary, InputStream in, PrintWriter out) throws IOException {
// Login
if (path.equals("/login")) {
// convert the body bytes to string for parsing form data
byte[] bodyBytes = new byte[contentLength];
StreamUtils.readExactly(in, bodyBytes, contentLength);
String bodyData = new String(bodyBytes);
System.out.println("Received POST data: " + bodyData + " from " + clientIP); // Debug log
// simple parsing: "username=abc&password=123" -> username: abc, password: 123
String username = "";
String password = "";
String[] pairs = bodyData.split("&");
for (String pair : pairs) {
String[] kv = pair.split("=");
if (kv.length == 2) {
if (kv[0].equals("username")) {
username = kv[1];
}
if (kv[0].equals("password")) {
password = kv[1];
}
}
}
// authentication
boolean isAuthenticated = false;
try {
File userFile = new File("users.txt");
if (userFile.exists()) {
List<String> lines = Files.readAllLines(userFile.toPath());
for (String line : lines) {
String[] creds = line.split(":");
if (creds.length == 2) {
String storedUsername = creds[0].trim();
String storedPassword = creds[1].trim();
if (storedUsername.equals(username) && storedPassword.equals(password)) {
isAuthenticated = true;
break;
}
}
}
} else {
System.err.println("User file not found: " + userFile.getAbsolutePath());
}
} catch (IOException e) {
System.err.println("Error reading user file: " + e.getMessage());
}
if (isAuthenticated) {
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: text/html");
out.println();
out.println("<h1>Login Successful! Welcome " + username + "</h1>");
System.out.println("Client " + clientIP + " POST /login - status code 200 (Success)");
} else {
out.println("HTTP/1.1 401 Unauthorized");
out.println("Content-Type: text/html");
out.println();
out.println("<h1>401 Unauthorized - Wrong Credentials</h1>");
out.println("<p>Please try again.</p>");
out.println("<a href='/login.html'>Go back to login page</a>");
System.out.println("Client " + clientIP + " POST /login - status code 401 (Failed)");
}
// Upload iamge with multipart/form-data
} else if (path.equals("/upload")) {
// read the body bytes for multipart/form-data
byte[] bodyBytes = new byte[contentLength];
StreamUtils.readExactly(in, bodyBytes, contentLength); // read the raw bytes of the request body, which contains the multipart form data including the image file
byte[] headerEndPattern = new byte[]{'\r', '\n', '\r', '\n'}; // search for this pattern to find where the image data starts
int imageStartIndex = StreamUtils.indexOf(bodyBytes, headerEndPattern, 0);
if (imageStartIndex != -1) {
imageStartIndex += 4; // skip the headerEndPattern to get to the start of the image data
// where the image data ends? it ends at the boundary which is defined in the Content-Type header
String endBoundaryStr = "\r\n--" + boundary;
int imageEndIndex = StreamUtils.indexOf(bodyBytes, endBoundaryStr.getBytes(), imageStartIndex);
if (imageEndIndex != -1) {
int imageLength = imageEndIndex - imageStartIndex;
byte[] imageData = new byte[imageLength];
System.arraycopy(bodyBytes, imageStartIndex, imageData, 0, imageLength);
// save the image to the public directory
File savedImage = new File(publicPath, "uploaded_image.png");
Files.write(savedImage.toPath(), imageData);
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: text/html");
out.println();
out.println("<h1>Image Uploaded Successfully!</h1>");
out.println("<a href='/uploaded_image.png'>View Image</a>");
System.out.println("Client " + clientIP + " POST /upload - status code 200 (Success)");
return;
}
}
sendError(out, clientIP, "400 Bad Request");
} else {
sendError(out, clientIP, "404 Not Found");
}
}
}