-
Notifications
You must be signed in to change notification settings - Fork 1
/
HTTPServer.java
300 lines (274 loc) · 10.1 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
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
package services;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
import com.google.gson.Gson;
/**
* Example HTTP Server.
*
* Supports GET and HEAD requests.
* Takes a GET and returns a HTTP response to the following endpoints:
*
* GET / (root) Response with the message: "Hello! Welcome to this Server.".
* GET /gettime Response with the current date and time on the server.
* GET /headers Response with the request headers as a JSON object.
* GET /qs Response with the request query-string as a JSON object.
*
* If the request is a HEAD request, returns the same response without the content.
* If the request is not a GET or HEAD request, returns 501 NOT IMPLEMENTED response.
* If the endpoint does not match one of the above, returns 404 NOT FOUND.
*
* Examples:
*
* $ telnet 130.63.96.85 36430
* > GET / HTTP/1.1
* >
* HTTP/1.1 200 OK
* Server: Java HTTP Server : 1.0
* Date: Thu Sep 23 15:12:43 EDT 2021
* Content-type: text/plain
* Content-length: 30
*
* Hello! Welcome to this Server.
*
* $ telnet 130.63.96.85 36430
* > GET /gettime HTTP/1.1
* >
* HTTP/1.1 200 OK
* Server: Java HTTP Server : 1.0
* Date: Thu Sep 23 15:40:29 EDT 2021
* Content-type: text/plain
* Content-length: 28
*
* Thu Sep 23 15:40:29 EDT 2021
*
* $ telnet 130.63.96.85 36430
* > GET /qs?key1=value1&key2=value%20two&key3=value%5Cthree HTTP/1.1
* HTTP/1.1 200 OK
* Server: Java HTTP Server : 1.0
* Date: Thu Sep 23 15:29:54 EDT 2021
* Content-type: application/json
* Content-length: 58
*
* {"key1":"value1","key2":"value two","key3":"value\\three"}
*
* $ telnet 130.63.96.85 36430
* > GET /headers HTTP/1.1
* > Host: 130.63.96.85:42507
* > Connection: keep-alive
* > User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0
* > Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*\/*;q=0.8
* > Accept-Encoding: gzip, deflate
* > Accept-Language: en-US,en;q=0.5
* > Upgrade-Insecure-Requests: 1
* > DNT 1
* >
* HTTP/1.1 200 OK
* Server: Java HTTP Server : 1.0
* Date: Thu Sep 23 15:36:47 EDT 2021
* Content-type: application/json
* Content-length: 351
*
* {"Accept":"text/html,application/xhtml+xml,application/xml;q\u003d0.9,image/webp,*\/*;
* q\u003d0.8","Upgrade-Insecure-Requests":"1","Connection":"keep-alive",
* "User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0",
* "Host":"130.63.96.85:42507","Accept-Encoding":"gzip, deflate",
* "Accept-Language":"en-US,en;q\u003d0.5","DNT":"1"}
*
* $ telnet 130.63.96.85 36430
* > GET /doesnotexist HTTP/1.1
* HTTP/1.1 404 NOT FOUND
* Server: Java HTTP Server : 1.0
* Date: Thu Sep 23 17:01:59 EDT 2021
* Content-type: text/plain
* Content-length: 9
*
* NOT FOUND
*
* $ telnet 130.63.96.85 36430
* > POST / HTTP/1.1
* POST / HTTP/1.1
* HTTP/1.1 501 NOT IMPLEMENTED
* Server: Java HTTP Server : 1.0
* Date: Thu Sep 23 17:02:52 EDT 2021
* Content-type: text/plain
* Content-length: 15
*
* $ telnet 130.63.96.85 36430
* > GET / HTTP/2.0
* HTTP/1.1 505 HTTP VERSION NOT SUPPORTED
* Server: Java HTTP Server : 1.0
* Date: Thu Sep 23 17:04:07 EDT 2021
* Content-type: text/plain
* Content-length: 26
*
* HTTP VERSION NOT SUPPORTED
*
*/
public class HTTPServer extends Thread {
private static PrintStream log = System.out;
private static final Map<Integer, String> httpResponseCodes = new HashMap<>();
static {
httpResponseCodes.put(100, "HTTP CONTINUE");
httpResponseCodes.put(101, "SWITCHING PROTOCOLS");
httpResponseCodes.put(200, "OK");
httpResponseCodes.put(201, "CREATED");
httpResponseCodes.put(202, "ACCEPTED");
httpResponseCodes.put(203, "NON AUTHORITATIVE INFORMATION");
httpResponseCodes.put(204, "NO CONTENT");
httpResponseCodes.put(205, "RESET CONTENT");
httpResponseCodes.put(206, "PARTIAL CONTENT");
httpResponseCodes.put(300, "MULTIPLE CHOICES");
httpResponseCodes.put(301, "MOVED PERMANENTLY");
httpResponseCodes.put(302, "MOVED TEMPORARILY");
httpResponseCodes.put(303, "SEE OTHER");
httpResponseCodes.put(304, "NOT MODIFIED");
httpResponseCodes.put(305, "USE PROXY");
httpResponseCodes.put(400, "BAD REQUEST");
httpResponseCodes.put(401, "UNAUTHORIZED");
httpResponseCodes.put(402, "PAYMENT REQUIRED");
httpResponseCodes.put(403, "FORBIDDEN");
httpResponseCodes.put(404, "NOT FOUND");
httpResponseCodes.put(405, "METHOD NOT ALLOWED");
httpResponseCodes.put(406, "NOT ACCEPTABLE");
httpResponseCodes.put(407, "PROXY AUTHENTICATION REQUIRED");
httpResponseCodes.put(408, "REQUEST TIME OUT");
httpResponseCodes.put(409, "CONFLICT");
httpResponseCodes.put(410, "GONE");
httpResponseCodes.put(411, "LENGTH REQUIRED");
httpResponseCodes.put(412, "PRECONDITION FAILED");
httpResponseCodes.put(413, "REQUEST ENTITY TOO LARGE");
httpResponseCodes.put(414, "REQUEST URI TOO LARGE");
httpResponseCodes.put(415, "UNSUPPORTED MEDIA TYPE");
httpResponseCodes.put(500, "INTERNAL SERVER ERROR");
httpResponseCodes.put(501, "NOT IMPLEMENTED");
httpResponseCodes.put(502, "BAD GATEWAY");
httpResponseCodes.put(503, "SERVICE UNAVAILABLE");
httpResponseCodes.put(504, "GATEWAY TIME OUT");
httpResponseCodes.put(505, "HTTP VERSION NOT SUPPORTED");
}
private Socket client;
private HTTPServer(Socket client) {
this.client = client;
}
private void sendHeaders(PrintStream res, int code, String contentType, String response) {
// send HTTP Headers
res.printf("HTTP/1.1 %d %s\n", code, httpResponseCodes.get(code));
res.println("Server: Java HTTP Server : 1.0");
res.println("Date: " + new Date());
res.println("Content-type: " + contentType);
res.println("Content-length: " + response.getBytes().length);
res.println(); // blank line between headers and content, very important !
}
private String[] getComponents(String resourcePath) {
if (!resourcePath.contains("?")) {
return new String[]{ resourcePath, "" };
} else {
return resourcePath.split("\\?", 2);
}
}
private Map<String, String> getQueryStrings(String qs) throws Exception {
Map<String, String> queries = new HashMap<>();
String[] fields = qs.split("&");
for (String field : fields) {
String[] pairs = field.split("=", 2);
if (pairs.length == 2) {
queries.put(pairs[0], URLDecoder.decode(pairs[1], "UTF-8"));
}
}
return queries;
}
private Map<String, String> getHeaders(List<String> headerLines) {
String[] keyvalue;
Map<String, String> headers = new HashMap<String, String>();
for (String header : headerLines) {
keyvalue = header.split(":", 2);
headers.put(keyvalue[0], keyvalue[1].trim());
}
return headers;
}
public void run() {
final String clientAddress = String.format("%s:%d", client.getInetAddress(), client.getPort());
log.printf("Connected to %s\n", clientAddress);
try (
Socket client = this.client; // Makes sure that client is closed at end of try-statement.
Scanner req = new Scanner(client.getInputStream());
PrintStream res = new PrintStream(client.getOutputStream(), true);
) {
String request = req.nextLine();
StringTokenizer parse = new StringTokenizer(request);
String method = parse.nextToken().toUpperCase(); // The HTTP method requested
String endpoint = parse.nextToken().toLowerCase(); // The endpoint / URL
String version = parse.nextToken().toUpperCase(); // The HTTP version
int status;
String response = "";
String contentType = "text/plain";
try {
if (!method.equals("GET") && !method.equals("HEAD")) { // only support GET + HEAD methods
status = 501;
} else if (!version.equals("HTTP/1.1")) { // only support HTTP version 1.1
status = 505;
} else {
status = 200;
if (endpoint.equals("/")) {
response = "Hello! Welcome to this Server.";
} else if (endpoint.equals("/gettime")) {
response = (new Date()).toString();
} else if (endpoint.startsWith("/qs?")) {
String[] components = getComponents(endpoint);
contentType = "application/json";
response = (new Gson()).toJson(getQueryStrings(components[1]));
} else if (endpoint.equals("/headers")) {
// Read the request headers
String buff;
List<String> headers = new ArrayList<>();
while (req.hasNextLine()) {
buff = req.nextLine();
if (buff.isEmpty()) break;
headers.add(buff);
}
contentType = "application/json";
response = (new Gson()).toJson(getHeaders(headers));
} else {
status = 404;
}
}
} catch (Exception e) {
status = 500;
}
if (status != 200) {
response = httpResponseCodes.get(status);
}
log.printf("%s: %d - %s\n", clientAddress, status, request);
sendHeaders(res, status, contentType, response);
if (method.equals("GET")) {
res.println(response);
}
res.flush(); // flush character output stream buffer
} catch (Exception e) {
log.println(e);
} finally {
log.printf("Disconnected from %s\n", clientAddress);
}
}
public static void main(String[] args) throws Exception {
int port = 0;
InetAddress host = InetAddress.getLocalHost(); // .getLoopbackAddress();
try (ServerSocket server = new ServerSocket(port, 0, host)) {
log.printf("Server listening on %s:%d\n", server.getInetAddress(), server.getLocalPort());
while (true) {
Socket client = server.accept();
(new HTTPServer(client)).start();
}
}
}
}