Skip to content

Commit 53e899a

Browse files
committed
implemented socket cloning
1 parent df3e798 commit 53e899a

File tree

1 file changed

+51
-17
lines changed
  • webserver/src/main/java/com/ot/webserver/maven_ot_webserver

1 file changed

+51
-17
lines changed

webserver/src/main/java/com/ot/webserver/maven_ot_webserver/App.java

+51-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package com.ot.webserver.maven_ot_webserver;
22

3+
import java.io.BufferedReader;
4+
import java.io.ByteArrayInputStream;
5+
import java.io.ByteArrayOutputStream;
36
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.io.ObjectInputStream;
9+
import java.io.ObjectOutputStream;
10+
import java.io.Serializable;
411
import java.net.ServerSocket;
512
import java.net.Socket;
613
import java.util.concurrent.ExecutorService;
@@ -12,9 +19,10 @@
1219
import com.ot.webserver.maven_ot_webserver.request.Handler;
1320
import com.ot.webserver.maven_ot_webserver.request.Listener;
1421

15-
public class App {
22+
public class App implements Serializable {
1623

1724
private static final Logger logger = LogManager.getLogger(App.class);
25+
private static boolean[] authResult;
1826

1927
public static void main(String[] args) {
2028

@@ -46,22 +54,23 @@ public void run() {
4654
ServerSocket serverSocket = new ServerSocket(Config.PORT);
4755
while(true) {
4856
Socket socket = serverSocket.accept();
49-
50-
// TODO Implement Basic Authentication Check
51-
// BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
52-
// String line = in.readLine();
53-
// while(line != null) {
54-
// if(line.contains("Authorization: Basic" )) {
55-
// Authenticator.authenticate(socket, line.split("\\s+")[2]);
56-
// break;
57-
// }
58-
// line = in.readLine();
59-
// }
60-
// if(Config.username != null && Config.password != null) {
61-
// Authenticator.responseView(socket);
62-
// continue;
63-
// }
64-
57+
if(Config.username != null && Config.password != null) {
58+
try {
59+
authResult = authStatus(cloneSocket(socket));
60+
if(!authResult[0] && !authResult[1]) {
61+
Authenticator.responseView(socket);
62+
continue;
63+
} else if (authResult[0] && !authResult[1]) {
64+
Authenticator.failedAuthenticationView(socket);
65+
continue;
66+
}
67+
68+
} catch (ClassNotFoundException e) {
69+
// TODO Auto-generated catch block
70+
e.printStackTrace();
71+
continue;
72+
}
73+
}
6574
Listener.getInstance().addRequestToQueue(socket);
6675
requestHandler.execute(new Handler(Listener.getInstance().handleRequest()));
6776
}
@@ -72,6 +81,31 @@ public void run() {
7281

7382
}
7483

84+
public static boolean[] authStatus(Socket socket) throws IOException {
85+
boolean[] auth = new boolean[2];
86+
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
87+
String line = in.readLine();
88+
while(line != null) {
89+
if(line.contains("Authorization: Basic" )) {
90+
auth[0] = true; auth[1] = Authenticator.authenticate(socket, line.split("\\s+")[2]);
91+
return auth;
92+
}
93+
line = in.readLine();
94+
}
95+
auth[0] = false; auth[1] = false;
96+
return auth;
97+
}
98+
99+
public static Socket cloneSocket(Socket socket) throws IOException, ClassNotFoundException {
100+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
101+
ObjectOutputStream oos = new ObjectOutputStream(baos);
102+
oos.writeObject(socket);
103+
oos.flush();
104+
byte[] bytes = baos.toByteArray();
105+
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
106+
return (Socket) new ObjectInputStream(bais).readObject();
107+
}
108+
75109
public static void setPort(String[] args) {
76110
if(args.length == 1 && Integer.parseInt(args[0]) >= Config.PORT_MIN && Integer.parseInt(args[0]) <= Config.PORT_MAX) {
77111
Config.PORT = Integer.parseInt(args[0]);

0 commit comments

Comments
 (0)