-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHttpHandler.java
119 lines (107 loc) · 3.18 KB
/
HttpHandler.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
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
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
/**
* @author ambarmodi
*
*/
public class HttpHandler implements Runnable {
private Socket socket;
private String res;
public HttpHandler(Socket socket) {
this.res = null;
this.socket = socket;
}
public void run() {
try {
handleRequest();
} catch (Exception e) {
System.err.println("Error Occured: " + e.getMessage());
try {
socket.close();
System.exit(0);
} catch (IOException e1) {
System.err.println("Error Closing socket Connection.");
System.exit(0);
}
System.err.println("Server is Terminating!");
}
}
/**
* @throws Exception
*/
private void handleRequest() throws Exception {
InputStream input;
OutputStream output;
//
File parent = new File(System.getProperty("user.dir"));
File root = new File(parent.getParent() + File.separator +"www");
if (root.isDirectory()) {
input = socket.getInputStream();
output = socket.getOutputStream();
serverRequest(input, output, root.toString());
output.close();
input.close();
} else {
throw new Exception("www directory not present!");
}
socket.close();
}
/**
* @param input
* @param output
* @param root
* @throws Exception
*/
private void serverRequest(InputStream input, OutputStream output, String root) throws Exception {
String line;
BufferedReader bf = new BufferedReader(new InputStreamReader(input));
while ((line = bf.readLine()) != null) {
if (line.length() <= 0) {
break;
}
if (line.startsWith("GET")) {
String filename= line.split(" ")[1].substring(1);
File resource = new File(root + File.separator + filename);
if (resource.isFile()) {
res = filename;
populateResponse(resource, output);
Server.printResult(res, socket.getPort(), socket.getRemoteSocketAddress().toString());
} else {
String Content_NOT_FOUND = "<html><head></head><body><h1>File Not Found</h1></body></html>";
String REQ_NOT_FOUND = "HTTP/1.1 404 Not Found\n\n";
String header = REQ_NOT_FOUND+ Content_NOT_FOUND;
output.write(header.getBytes());
}
break;
}
}
}
/**
* @param resource
* @param output
* @throws IOException
*/
private void populateResponse(File resource, OutputStream output) throws IOException {
SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
String REQ_FOUND = "HTTP/1.0 200 OK\n";
String SERVER = "Server: HTTP server/0.1\n";
String DATE = "Date: " + format.format(new java.util.Date()) + "\n";
String CONTENT_TYPE = "Content-type: " + URLConnection.guessContentTypeFromName(resource.getName());
String LENGTH = "Content-Length: " + (resource.length()) + "\n\n";
String header = REQ_FOUND + SERVER + DATE + CONTENT_TYPE + LENGTH;
output.write(header.getBytes());
Files.copy(Paths.get(resource.toString()), output);
output.flush();
}
}