1
1
package com .ot .webserver .maven_ot_webserver ;
2
2
3
+ import java .io .BufferedReader ;
4
+ import java .io .ByteArrayInputStream ;
5
+ import java .io .ByteArrayOutputStream ;
3
6
import java .io .IOException ;
7
+ import java .io .InputStreamReader ;
8
+ import java .io .ObjectInputStream ;
9
+ import java .io .ObjectOutputStream ;
10
+ import java .io .Serializable ;
4
11
import java .net .ServerSocket ;
5
12
import java .net .Socket ;
6
13
import java .util .concurrent .ExecutorService ;
12
19
import com .ot .webserver .maven_ot_webserver .request .Handler ;
13
20
import com .ot .webserver .maven_ot_webserver .request .Listener ;
14
21
15
- public class App {
22
+ public class App implements Serializable {
16
23
17
24
private static final Logger logger = LogManager .getLogger (App .class );
25
+ private static boolean [] authResult ;
18
26
19
27
public static void main (String [] args ) {
20
28
@@ -46,22 +54,23 @@ public void run() {
46
54
ServerSocket serverSocket = new ServerSocket (Config .PORT );
47
55
while (true ) {
48
56
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
+ }
65
74
Listener .getInstance ().addRequestToQueue (socket );
66
75
requestHandler .execute (new Handler (Listener .getInstance ().handleRequest ()));
67
76
}
@@ -72,6 +81,31 @@ public void run() {
72
81
73
82
}
74
83
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
+
75
109
public static void setPort (String [] args ) {
76
110
if (args .length == 1 && Integer .parseInt (args [0 ]) >= Config .PORT_MIN && Integer .parseInt (args [0 ]) <= Config .PORT_MAX ) {
77
111
Config .PORT = Integer .parseInt (args [0 ]);
0 commit comments