-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicAuthWebServer.java
More file actions
192 lines (174 loc) · 6.62 KB
/
BasicAuthWebServer.java
File metadata and controls
192 lines (174 loc) · 6.62 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
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Base64;
public class BasicAuthWebServer {
/*run the http server on this TCP port. */
private static final int PORT = 8080;
/* The socket used to process incoming connections
from a web clients*/
private static ServerSocket dServerSocket;
public BasicAuthWebServer () throws Exception {
dServerSocket = new ServerSocket (PORT);
}
public void run() throws Exception {
while (true) {
/* Wait for a connections from a client. */
Socket s = dServerSocket.accept();
/* Then, process the client's request. */
processRequest(s);
}
}
/* Reads the HTTP request from the client and
responds with the file the user requested or an
HTTP error code */
public void processRequest(Socket s) throws Exception {
// Used to write data from the client //
BufferedReader br =
new BufferedReader (
new InputStreamReader (s.getInputStream()));
// Used to write data to the client. //
OutputStreamWriter osw =
new OutputStreamWriter (s.getOutputStream());
// Read the HTTP request from the client. //
String request = br.readLine();
String command = null;
String pathname =null;
// Parse the HTTP request.//
try{
StringTokenizer st =
new StringTokenizer (request, " ");
command = st.nextToken();
pathname = st.nextToken();
} catch (Exception e) {
osw.write ("HTTP/1.0 400 Bad Request\n\n");
osw.close();
return;
}
if (command.equals("GET")) {
// If the request is a GET, try to respond with the file the user is requesting. //
Credentials c = getAuthorization(br);
if ((c != null) && (MiniPasswordManager.checkPassword(c.getUsername(), c.getPassword()))) {
serveFile (osw,pathname);
} else {
osw.write ("HTTP/1.0 401 Unauthorized");
osw.write ("WWW-Authenticate: Basic realm=BasicAuthWebServer");
}
}
else if (command.equals("PUT")) {
storeFile(br,osw,pathname);
logEntry("logFile.txt",command);
}
else {
// if the request is NOT a GET or PUT, return an error saying this serveer does not implement the requested command//
osw.write ("HTTP/1.0 501 Not Implemented\n\n");
}
// Close the connection to the client. //
osw.close();
}
private Credentials getAuthorization (BufferedReader br) {
try {
String header = null;
while (!(header = br.readLine()).equals("")) {
if (header.startsWith("Authorization:")) {
StringTokenizer st = new StringTokenizer(header, " ");
st.nextToken(); //skip "Authorization"
st.nextToken(); // skip "Basic"
return new Credentials(st.nextToken());
}
}
} catch (Exception e) {
}
return null;
}
public void serveFile (OutputStreamWriter osw, String pathname) throws Exception {
FileReader fr = null;
int c = -1;
int sentBytes = 0;
// Remove the initial slash at the beginning of the pathname in the request //
if (pathname.charAt(0) == '/')
pathname = pathname.substring(1);
// If there was no filename specified by the client, serve the 'index.html' file. //
if (pathname.equals(""))
pathname = "C:\\Users\\Serena\\Desktop\\test\\index.html";
// Try to open file specified by pathname. //
try {
fr = new FileReader (pathname);
c = fr.read();
}
catch (Exception e) {
// if the file is not found, return the appropriate HTTP response code. //
osw.write ("HTTP/1.0 404 Not Found\n\n");
return;
}
// if the requested file can be successfully opened and read, then return an OK response code and sent the contents of the file. //
osw.write ("HTTP/1.0 200 OK\n\n");
while ( (c != -1) && (sentBytes < 1000000) ){
osw.write (c);
sentBytes++;
c = fr.read();
if (sentBytes > 1000000) {
logEntry("403Error.txt", "File Exceeds Size Limit");
throw new Exception("403 Forbidden");
}
}
}
public void storeFile(BufferedReader br,
OutputStreamWriter osw,
String pathname) throws Exception {
FileWriter fw = null;
try {
fw = new FileWriter (pathname);
String s = br.readLine();
while (s != null) {
fw.write (s);
s = br.readLine();
}
fw.close();
osw.write ("HTTP/1.0 201 Created");
}
catch (Exception e) {
osw.write ("HTTP/1.0 500 Internal Server Error");
}
}
public void logEntry(String filename,String record) {
try {
FileWriter fw = new FileWriter (filename, true);
fw.write (getTimestamp() + " " + record);
fw.close();
}
catch (Exception e) {
}
}
public String getTimestamp() {
return (new Date()).toString();
}
// This method is called when the program is run from the command line. //
public static void main (String argv[]) throws Exception {
if (argv.length == 1) {
/* Initialize MiniPasswordManager */
MiniPasswordManager.init(argv[0]);
/* Create a BasicAuthWebServer object, and run it */
BasicAuthWebServer baws = new BasicAuthWebServer();
baws.run();
} else {
System.err.println ("Usage: java BasicAuthWebServer <pwdfile>");
}
}
}
class Credentials {
private String dUsername;
private String dPassword;
public Credentials(String authString) throws Exception {
authString = new String((Base64.getDecoder().decode(authString)));
StringTokenizer st = new StringTokenizer(authString, ":");
dUsername = st.nextToken();
dPassword = st.nextToken();
}
public String getUsername() {
return dUsername;
}
public String getPassword() {
return dPassword;
}
}