-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpServer.java
157 lines (139 loc) · 5.69 KB
/
HttpServer.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
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
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileReader;
import java.io.File;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;
public class HttpServer {
private static class Processor implements Runnable {
private Socket socket;
private OutputStream output;
private BufferedReader reader;
private String root;
private Processor(Socket socket, String root) throws IOException {
this.root = root;
this.socket = socket;
this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
private byte[] byteStream(String fileRequest) {
File file = new File(root + fileRequest);
byte[] byteStream = new byte[(int)file.length()]; //???
FileInputStream fileInputStream = null;
String error405 = "/405.html";
String error404 = "/404.html";
try {
fileInputStream = new FileInputStream(file);
fileInputStream.read(byteStream);
if ( error405.equals(fileRequest) ) {
System.out.println("405 /");
} else if ( error404.equals(fileRequest) ) {
System.out.println("404 /");
} else {
System.out.println("200 " + fileRequest);
}
} catch ( FileNotFoundException ex ) {
if ( fileRequest.equals(error404) ) {
System.out.println("File 404 not found.");
} else {
byteStream = byteStream(error404);
}
} catch ( IOException ex ) {
System.err.println("Error Reading the file.");
} finally {
try {
if ( fileInputStream != null ) {
fileInputStream.close();
}
} catch ( IOException ex ) {}
}
return byteStream;
}
private String getResponse(String mapValue, int length) {
StringBuffer response = new StringBuffer();
if ( mapValue == null ) {
mapValue = "application/octet-stream";
}
response.append("HTTP/1.1 200 OK\r\n");
response.append("Connection: close\r\n");
response.append("Content-Length: " + length + "\r\n");
response.append("Content-Type: " + mapValue + "\r\n");
response.append("Date: " + new Date() + "\r\n\r\n");
return response.toString();
}
private void writeResponse(String file) throws IOException {
OutputStream output = socket.getOutputStream();
byte[] text = byteStream(file);
Map<String, String> mimeMap = parseFile("mime.types");
String key = file.split("\\.")[1];
String mapValue = mimeMap.get(key);
String response = getResponse(mapValue, text.length);
byte[] result = new byte[text.length + response.getBytes().length];
System.arraycopy(response.getBytes(), 0, result, 0, response.getBytes().length);
System.arraycopy(text, 0, result, response.getBytes().length, text.length);
output.write(result);
output.flush();
}
public static Map<String, String> parseFile(String fileName) {
Map<String, String> map = new HashMap<String, String>();
BufferedReader br = null;
String line;
try {
br = new BufferedReader(new FileReader(fileName));
while ( (line = br.readLine()) != null ) {
String[] parts = line.split("\\s", 2);
int last = parts.length - 1;
if ( parts[0].equals("")) {}
else {
parts[last] = parts[last].trim();
map.put(parts[0], parts[last]);
}
}
} catch ( FileNotFoundException ex ) {
ex.printStackTrace();
} catch ( IOException ex ) {
ex.printStackTrace();
}
return map;
}
public void run() {
try {
String line = reader.readLine();
String[] configArray = line.split(" ");
if ( !configArray[0].equals("GET") ) {
writeResponse("/405.html");
} else if ( configArray[1].equals("/") ) {
writeResponse("/index.html");
} else {
writeResponse(configArray[1]);
}
} catch ( IOException ex ) {
} finally {
try {
socket.close();
} catch ( IOException ex ) {
}
}
}
}
public static void main(String[] args) throws IOException {
Map<String, String> httpMap = Processor.parseFile("http.conf");
int port = Integer.parseInt(httpMap.get("port"));
String address = httpMap.get("address");
String root = httpMap.get("root_dir");
int backlog = 42;
ServerSocket server = new ServerSocket(port, backlog, InetAddress.getByName(address));
while (true) {
Socket socket = server.accept();
new Thread(new Processor(socket, root)).start();
}
}
}