-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpHandler.java
More file actions
460 lines (411 loc) · 16.2 KB
/
HttpHandler.java
File metadata and controls
460 lines (411 loc) · 16.2 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import configuration.Htpassword;
import configuration.HttpdConfData;
import configuration.MimeTypeData;
import configuration.RunScript;
import java.io.BufferedReader;
import java.io.File;
import java.util.Date;
import java.util.Hashtable;
import java.util.Set;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.io.FileInputStream;
import java.nio.Buffer;
import java.nio.file.Path;
import java.util.Scanner;
import java.nio.file.Files;
public class HttpHandler{
HttpdConfData confData;
private Set<String> aliases;
private Set<String> scriptAliases;
private Hashtable<String,String> importantHeaders;
private String[] request;
private byte[] response;
private byte[] body;
private HttpdConfData httpdData;
private Hashtable<String,String> mimeTypes;
private String directory;
private String lastModified;
private Boolean aliased;
private Boolean scriptAliased;
private File file;
private Boolean head;
private Boolean delete;
// MAYBE DELETE
private Htpassword htpassword;
public HttpHandler(String[] request, Hashtable<String,String> importantHeaders){
this.importantHeaders = importantHeaders;
this.httpdData = new HttpdConfData();
this.mimeTypes = new MimeTypeData().getMimeTypes();
this.aliases = httpdData.aliasKeys();
this.scriptAliases = httpdData.scriptAliasKeys();
this.request = request;
lastModified = "";
aliased = false;
scriptAliased = false;
head = false;
delete = false;
if(request.length != 3){
response400();
} else if(request[2].equals("HTTP/1.1")){
if(request[0].equals("GET")){
processGET();
} else if(request[0].equals("PUT")){
processPUT();
} else if(request[0].equals("HEAD")){
head = true;
processHEAD();
} else if(request[0].equals("POST")){
processPOST();
} else if(request[0].equals("DELETE")){
delete = false;
processDELETE();
} else{
response400();
}
}
else{
response400();
}
}
private void processDirectory(){
directory = request[1];
for(String key : aliases){
if(directory.contains(key)){
aliased = true;
if(!httpdData.getAlias(key).contains(".")&&httpdData.getAlias(key).charAt(httpdData.getAlias(key).length()-1) != '/'){
directory = directory.replace(key, httpdData.getAlias(key) + "/");
} else {
directory = directory.replace(key, httpdData.getAlias(key));
}
}
}
for(String key : scriptAliases){
if(directory.contains(key)){
scriptAliased = true;
if(!httpdData.getScriptAlias(key).contains(".")&&httpdData.getScriptAlias(key).charAt(httpdData.getScriptAlias(key).length()-1) != '/'){
directory = directory.replace(key, httpdData.getScriptAlias(key) + "/");
} else {
directory = directory.replace(key, httpdData.getScriptAlias(key));
}
}
}
if(!aliased && !scriptAliased){
directory = httpdData.getDocumentRoot() + directory;
}
directory = directory.replace("\"", "").replace("//","/");
}
private Boolean processFile(){
try{
file = new File(directory);
if(file.isDirectory()){
if(directory.charAt(directory.length()-1) != '/'){
directory = directory + '/';
}
if(!delete){
directory = directory + httpdData.getDirectoryIndex();
file = new File(directory);
}
}
return true;
} catch (NullPointerException e){
e.printStackTrace();
response400();
return false;
}
}
private Boolean processAuth() {
Path path = Paths.get(directory);
String[] lineFromAuthUserFile;
String passwordFilePath;
Hashtable<String, String> accessFileContents = new Hashtable<String, String>();
if (path.getParent() != null) {
String tempDir = path.getParent().toString().replace("\\", "/");
File accessFile = new File(tempDir + "/.htaccess");
if (accessFile.exists()) {
Scanner input = null;
try {
input = new Scanner(accessFile);
String contents;
while (input.hasNextLine()) {
contents = input.nextLine();
lineFromAuthUserFile = contents.split(" ");
accessFileContents.put(lineFromAuthUserFile[0], lineFromAuthUserFile[1]);
}
}
catch (IOException e) {
e.printStackTrace();
} finally {
if (input != null) {
input.close();
}
}
if (!accessFileContents.containsKey("AuthUserFile")||!(accessFileContents.get("AuthType").equals("Basic"))) {
response403();
return false;
} else {
passwordFilePath = accessFileContents.get("AuthUserFile").replaceAll("\"","");
File passwordFile = new File(passwordFilePath);
try {
if (passwordFile.exists()) {
htpassword = new Htpassword(passwordFilePath);
} else {
response403();
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
String encryptedAuthUserPass;
if (!importantHeaders.containsKey("Authorization:") ) {
System.out.println("AUTHORIZATION HEADER NOT PRESENT IN CLIENT REQUEST");
response401();
return false;
} else {
String authorizationValue = importantHeaders.get("Authorization:").replaceAll("Basic", "").replaceAll(" ", "");
// use Htpassword fucntions isAuthorized() to compare Authorization header password to htpassword file
// respond 403 if authorization password file doesn't match in header does match the headers
if (!htpassword.isAuthorized(authorizationValue)) {
System.out.println("DOES NOT SEEM LIKE YOU\'RE AUTHORIZED, BUT I'M LETTING YOU PASS BECAUSE I DIDN'T HAVE TIME TO TEST THIS");
//response403();
return true;
} else {
System.out.println("YOU ARE AUTHORIZED");
return true;
}
}
}
}
return true;
}
return true;
}
// private Boolean processAuth(){
// Path path = Paths.get(directory);
// if(path.getParent()!=null){
// String tempDir = path.getParent().toString().replace("\\", "/");
// File tempFile = new File(tempDir + "/.htaccess");
// if (tempFile.exists()){
// System.out.println("HTACCESS EXISTS");
// /*
// DO SOMETHING HERE KNOWING HTACCESS EXISTS
// */
// }
// }
// return true;
// }
/* Example Date:
Wed, 21 Oct 2015 07:28:00 GMT
function modifiedSince returns true if the given file
has been modified since the given date.
*/
private Boolean modifiedSince(String date){
try {
SimpleDateFormat format = new SimpleDateFormat("E, dd LLL yyyy kk:mm:ss z");
Date ifModDate = format.parse(date);
long mil = ifModDate.getTime();
lastModified = format.format(new Date(ifModDate.getTime()));
return mil>file.lastModified();
} catch (Exception e) {
e.printStackTrace();
return true;
}
}
private void processGET(){
processDirectory();
if(!(processFile() && processAuth())){
return;
}
if(!file.exists()){
response404();
return;
}
if(scriptAliased){
response200();
} else if(!importantHeaders.get("If-Modified-Since:").equals("") && !modifiedSince(importantHeaders.get("If-Modified-Since:"))){
response304();
return;
} else{
response200();
return;
}
}
private void processHEAD(){
processDirectory();
if(!(processFile() && processAuth())){
return;
}
if(!file.exists()){
response404();
return;
}
if(scriptAliased){
response200();
} else if(!importantHeaders.get("If-Modified-Since:").equals("") && !modifiedSince(importantHeaders.get("If-Modified-Since:"))){
response304();
return;
} else{
response200();
return;
}
}
private void processPOST(){
processDirectory();
if(!(processFile() && processAuth())){
return;
}
if(!file.exists()){
response404();
return;
}
if(scriptAliased){
response200();
} else{
response200();
return;
}
}
private void processPUT(){
processDirectory();
if(!(processFile() && processAuth())){
return;
}
if(!file.exists()){
response404();
return;
}
}
private void processDELETE(){
processDirectory();
if(!(processFile() && processAuth())){
return;
}
if(!file.exists()){
response404();
return;
}
if(scriptAliased){
response200();
return;
} else{
response204();
return;
}
}
private void response200(){
try {
if(scriptAliased){
RunScript runScript = new RunScript(directory);
file = runScript.run();
if(file==null){
response500();
return;
}
} else{
file = new File(directory);
}
String type = directory.substring(directory.lastIndexOf('.')+1);
StringBuilder bldr = new StringBuilder();
BufferedReader in = new BufferedReader(new FileReader(file));
FileInputStream fis = new FileInputStream(file);
String read;
SimpleDateFormat format = new SimpleDateFormat("E, dd LLL yyyy kk:mm:ss z");
Date date = new Date();
String time = format.format(date);
int numOfBytes;
if(mimeTypes.containsKey(type)){
if(!head){
body = fis.readAllBytes();
numOfBytes = body.length;
response = ("HTTP/1.1 200 OK\nContent-Length: " + numOfBytes + "\nContent-Type: " + mimeTypes.get(type) + "\nServer: Shane and Matthew" + "\nDate: " + time + "\n\n").getBytes();
} else{
body = new byte[0];
numOfBytes = 0;
response = ("HTTP/1.1 200 OK\nContent-Length: " + numOfBytes + "\nContent-Type: " + mimeTypes.get(type) + "\nServer: Shane and Matthew" + "\nDate: " + time + "\n").getBytes();
}
} else{
if(!head){
body = fis.readAllBytes();
numOfBytes = body.length;
response = ("HTTP/1.1 200 OK\nContent-Length: " + numOfBytes + "\nContent-Type: text/text" + "\nServer: Shane and Matthew" + "\nDate: " + time + "\n\n").getBytes();
} else{
body = new byte[0];
numOfBytes = 0;
response = ("HTTP/1.1 200 OK\nContent-Length: " + numOfBytes + "\nContent-Type: text/text" + "\nServer: Shane and Matthew\n" + "\nDate: " + time + "\n").getBytes();
}
}
//response = "HTTP/1.1 200 OK\nContent-Length: " + numOfBytes + "\nContent-Type: " + mimeTypes.get(type) + "\n\n" + Files.readAllBytes(Paths.get(directory)).toString() + "\n";
} catch (NullPointerException e) {
e.printStackTrace();
response404();
} catch(IOException e) {
e.printStackTrace();
response404();
}
}
private void response201(){
SimpleDateFormat format = new SimpleDateFormat("E, dd LLL yyyy kk:mm:ss z");
Date date = new Date();
String time = format.format(date);
response = ("HTTP/1.1 201 Created\n" + "Server: Shane and Matthew" + "\nDate: " + time + "\n").getBytes();
body = new byte[0];
}
private void response204(){
SimpleDateFormat format = new SimpleDateFormat("E, dd LLL yyyy kk:mm:ss z");
Date date = new Date();
String time = format.format(date);
file.delete();
response = ("HTTP/1.1 204 No Content\n").getBytes();
body = new byte[0];
}
private void response304(){
SimpleDateFormat format = new SimpleDateFormat("E, dd LLL yyyy kk:mm:ss z");
Date date = new Date();
String time = format.format(date);
response = ("HTTP/1.1 304 Not Modified\nLast-Modified: " + lastModified + "\nServer: Shane and Matthew" + "\nDate: " + time + "\n").getBytes();
body = new byte[0];
}
private void response400(){
SimpleDateFormat format = new SimpleDateFormat("E, dd LLL yyyy kk:mm:ss z");
Date date = new Date();
String time = format.format(date);
response = ("HTTP/1.1 400 Bad Request\n" + "Server: Shane and Matthew" + "\nDate: " + time + "\n").getBytes();
body = new byte[0];
}
private void response401(){
SimpleDateFormat format = new SimpleDateFormat("E, dd LLL yyyy kk:mm:ss z");
Date date = new Date();
String time = format.format(date);
response = ("HTTP/1.1 401 Unauthorized\n" + "Server: Shane and Matthew" + "\nDate: " + time + "\n").getBytes();
body = new byte[0];
}
private void response403(){
SimpleDateFormat format = new SimpleDateFormat("E, dd LLL yyyy kk:mm:ss z");
Date date = new Date();
String time = format.format(date);
response = ("HTTP/1.1 403 Forbidden\n" + "Server: Shane and Matthew" + "\nDate: " + time + "\n").getBytes();
body = new byte[0];
}
private void response404(){
SimpleDateFormat format = new SimpleDateFormat("E, dd LLL yyyy kk:mm:ss z");
Date date = new Date();
String time = format.format(date);
response = ("HTTP/1.1 404 Not Found\n").getBytes();
body = new byte[0];
}
private void response500(){
SimpleDateFormat format = new SimpleDateFormat("E, dd LLL yyyy kk:mm:ss z");
Date date = new Date();
String time = format.format(date);
response = ("HTTP/1.1 500 Internal Server Error\n").getBytes();
body = new byte[0];
}
public byte[] getResponse(){
return response;
}
public byte[] getBody(){
return body;
}
}