-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpRequest.java
85 lines (68 loc) · 2.84 KB
/
HttpRequest.java
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
/**
* This class, HttpRequest is responsible for forming the request.
*/
class HttpRequest {
String filename = "";
String mimeType = "";
Boolean validRequest = false;
Boolean redirect = false;
Boolean getRequest = false;
String reDirection = "";
String gotoURL = "";
String invalidNum = "403 Forbidden";
HttpRequest(String request) throws Exception {
String lines[] = request.split("\n");
String header[] = lines[0].split(" ");
String httpMethod = header[0];
if (header.length > 1) {
String protocol = "";
try {
filename = header[1];
protocol = header[2];
} catch (ArrayIndexOutOfBoundsException excpt) {
excpt.printStackTrace();
filename = "";
protocol = "";
}
}
//This server only handles GET and HEAD methods. So, we flip the validRequest to True if either of these methods is found
if (httpMethod.equals("GET")) {validRequest = true; getRequest = true;}
else if (httpMethod.equals("HEAD")) {
validRequest = true;
}
String currentDirectory = System.getProperty("user.dir"); //get the path of the current directory
String redirectFile = currentDirectory + File.separator + "www" + File.separator + "redirect.defs";
BufferedReader buff = new BufferedReader(new FileReader(redirectFile));
String sCurrentLine;
while ((sCurrentLine = buff.readLine()) != null) {
String[] redirected = sCurrentLine.split(" ");
String searchFor = redirected[0];
String redirectUrl = redirected[1];
if (searchFor.equals(filename)) {
redirect = true;
gotoURL = redirectUrl;
}
}
//set the MIME type based on the extension of the content being requested
if (!(redirect)) {
if (filename.toLowerCase().contains(".html") || filename.toLowerCase().contains(".htm")) {
mimeType = "text/html";
} else if (filename.toLowerCase().contains(".pdf")) {
mimeType = "application/pdf";
} else if (filename.toLowerCase().contains(".png")) {
mimeType = "image/png";
} else if (filename.toLowerCase().contains(".jpeg") || filename.toLowerCase().contains(".jpg")) {
mimeType = "image/jpeg";
} else if (filename.toLowerCase().contains(".txt") || filename.toLowerCase().contains(".java")) {
mimeType = "text/plain";
}
if (filename.trim().equals("/redirect.defs")) {
validRequest = false;
invalidNum = "404 Not Found";
}
}
}
}