-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHTTPResponse.html
More file actions
56 lines (46 loc) · 1.54 KB
/
HTTPResponse.html
File metadata and controls
56 lines (46 loc) · 1.54 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
<!DOCTYPE html>
<html>
<head>
<title>Connection</title>
</head>
<body>
<p>
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class HTTPResponse {
HTTPRequest request;
String response = null;
String source = "D:\\Eclipse Java\\workspace\\MultiThreaded Web Server\\src";
public HTTPResponse(HTTPRequest request) {
this.request = request;
File file = new File(source + request.filename);
try {
FileInputStream file_input_stream = new FileInputStream(file);
System.out.println("\n----Start of Response Frame----");
response = "HTTP/1.1 200 \r\n";
response = response + "Server: Multi-Threaded Web Server/1.0 \r\n";
response = response + "Content-Type: text/html \r\n";
response = response + "Connection: keep-alive \r\n";
response = response + "Content-Length: " + file.length() + " \r\n";
response = response + "\r\n";
System.out.print(response);
System.out.println("----End of Response Frame----");
int stream_char;
try { //Print contents of file to console
while ((stream_char = file_input_stream.read()) != -1){
response = response + (char)stream_char;
}
System.out.print(response);
} catch (IOException e) {
System.out.println(e);
}
} catch(FileNotFoundException e) {
response = response.replace("200", "404"); //File not found
}
}
}
</p>
</body>
</html>