Skip to content
This repository was archived by the owner on Mar 8, 2025. It is now read-only.

Commit 917fa41

Browse files
committed
Initialize web server
0 parents  commit 917fa41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+646
-0
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/description.html

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/project-template.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
717 Bytes
Binary file not shown.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.company.core.server;
2+
3+
import com.company.core.utils.Utilities;
4+
5+
import java.io.*;
6+
import java.net.Socket;
7+
import java.time.ZoneId;
8+
import java.time.ZonedDateTime;
9+
import java.time.format.DateTimeFormatter;
10+
import java.util.StringTokenizer;
11+
12+
public class ClientHandler extends Thread implements Runnable {
13+
14+
private Socket connection;
15+
16+
private BufferedReader reader;
17+
private PrintWriter writer;
18+
private BufferedOutputStream dataOut;
19+
20+
public Utilities utils = Utilities.getInstance();
21+
22+
private final File WEB_ROOT = new File("./www");
23+
private final File NOT_FOUND = new File(WEB_ROOT, "404.html");
24+
private final File NOT_IMPLEMENTED = new File(WEB_ROOT, "501.html");
25+
26+
27+
// Constructor
28+
public ClientHandler(HTTPServer server) {
29+
try {
30+
System.out.println("[CONSTRUCTOR] New client connected: " + server.getRequest());
31+
this.connection = server.getRequest();
32+
// Initialize reader and writer
33+
34+
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
35+
writer = new PrintWriter(connection.getOutputStream());
36+
dataOut = new BufferedOutputStream(connection.getOutputStream());
37+
} catch (Exception e) {
38+
System.out.println("[CONSTRUCTOR] An error has occurred during the start of client thread");
39+
}
40+
}
41+
42+
// This main function will run whenever a new thread is created
43+
@Override
44+
public void run() {
45+
46+
try {
47+
// read the first header.
48+
String header = reader.readLine();
49+
if (header != null) {
50+
// Read the headers
51+
StringTokenizer tokenizer = new StringTokenizer(header);
52+
String method = tokenizer.nextToken().toUpperCase();
53+
String resource = tokenizer.nextToken().toLowerCase();
54+
String protocol = tokenizer.nextToken();
55+
56+
String status;
57+
File file;
58+
59+
if (method.equals("GET")) {
60+
// Check if is URL path or file path
61+
// If not file path, then it's directory with an index.html
62+
if (!resource.contains(".")) {
63+
file = new File("./www" + resource, "index.html");
64+
status = " 200 OK";
65+
} else {
66+
try {
67+
file = new File("./www" + resource);
68+
status = " 200 OK";
69+
} catch (Exception error) {
70+
file = NOT_FOUND;
71+
status = " 404 Not Found";
72+
}
73+
}
74+
// if (resource.endsWith("/")) {
75+
// file = new File("./www" + resource, "index.html");
76+
// status = " 200 OK";
77+
// } else {
78+
//
79+
// }
80+
} else {
81+
file = NOT_IMPLEMENTED;
82+
status = " 501 Not Implemented";
83+
}
84+
85+
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("GMT"));
86+
String date = now.format(DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z"));
87+
88+
System.out.printf("%s %s%s %s\n", method, resource, status, date);
89+
byte[] data = utils.readFile(file);
90+
91+
// write the headers
92+
writer.println(protocol + status);
93+
writer.println("Server: HttpServer v1.0");
94+
writer.println("Date: " + date);
95+
writer.println("Content-Type: text/html; charset=utf-8");
96+
writer.println("Content-Length: " + data.length);
97+
writer.println();
98+
writer.flush();
99+
100+
// write the file contents (The website)
101+
dataOut.write(data, 0, data.length);
102+
dataOut.flush();
103+
104+
}
105+
106+
this.disconnect();
107+
108+
} catch (IOException e) {
109+
110+
this.disconnect();
111+
System.out.println("[RUN] " + e.getMessage());
112+
113+
}
114+
}
115+
116+
// This function is used to disconnect current client
117+
public void disconnect() {
118+
try {
119+
System.out.println("[DISCONNECT] Client with port " + connection.getPort() + " has disconnected");
120+
// Close connection, reader and writer to prevent resource leak
121+
this.connection.close();
122+
this.writer.close();
123+
this.reader.close();
124+
} catch(Exception e) {
125+
System.out.println("[DISCONNECT] An error has occurred during client disconnect");
126+
}
127+
}
128+
129+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.company.core.server;
2+
3+
import java.net.ServerSocket;
4+
import java.net.Socket;
5+
6+
public class HTTPServer {
7+
private static Socket request;
8+
9+
// Constructor
10+
private HTTPServer(Socket request) {
11+
this.request = request;
12+
}
13+
14+
public Socket getRequest() {
15+
return request;
16+
}
17+
18+
// Method to initialize server
19+
public static void init(int port) {
20+
try (ServerSocket serverSocket = new ServerSocket(port)) {
21+
System.out.println("[SYSTEM] Server is ready at port " + port);
22+
while(true) {
23+
// Create new accept
24+
HTTPServer server = new HTTPServer(serverSocket.accept());
25+
// Create a new thread by creating a new ClientHandler
26+
Thread thread = new ClientHandler(server);
27+
// Start thread
28+
thread.start();
29+
}
30+
} catch (Exception e) {
31+
System.out.println("[INIT] Server has closed connection");
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)