-
Notifications
You must be signed in to change notification settings - Fork 1
/
HTTPCalcService.java
259 lines (230 loc) · 8.53 KB
/
HTTPCalcService.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
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.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* HTTP Service example.
*
* Implements a simple calculator over HTTP.
* Supports the following request:
*
* GET /calc?op=<op>&a=<number>&b=<number>
*
* where <op> is one of:
* - add
* - subtract
* - multiply
* - divide
* - exponent
*
* and `a` and `b` are real numbers for the service to calculate:
* - a + b
* - a - b
* - a * b
* - a / b
* - a ^ b
*
* You can also request the following:
*
* GET /add?a=<number>&b=<number>
* GET /subtract?a=<number>&b=<number>
* GET /multiply?a=<number>&b=<number>
* GET /divide?a=<number>&b=<number>
* GET /exponent?a=<number>&b=<number>
*
* The server redirects the web browser to:
*
* GET /calc?op=add&a=<number>&b=<number>
* GET /calc?op=subtract&a=<number>&b=<number>
* GET /calc?op=multiply&a=<number>&b=<number>
* GET /calc?op=divide&a=<number>&b=<number>
* GET /calc?op=exponent&a=<number>&b=<number>
*
* via a HTTP 301 response code.
*
*/
public class HTTPCalcService extends Thread {
private static final PrintStream log = System.out;
private static final Map<Integer, String> httpResponseCodes = new HashMap<>();
private static final Pattern isDouble = Pattern.compile("^[+-]?([0-9]+)([.][0-9]+)?(E[+-]?[0-9]+)?$");
private static final String[] redirectedEndpoints = {
"/add",
"/subtract",
"/multiply",
"/divide",
"/exponent"
};
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 HTTPCalcService(Socket client) {
this.client = client;
}
private void sendHeaders(PrintStream res, int code, String contentType, String response) {
sendHeaders(res, code, contentType, response, new String[]{});
}
private void sendHeaders(PrintStream res, int code, String contentType, String response, String[] headers) {
// 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);
Arrays.stream(headers).forEach(h -> res.println(h));
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;
}
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();
String method, resource, version;
String response = "";
List<String> headers = new ArrayList<>();
try (Scanner parse = new Scanner(request)) {
method = parse.next();
resource = parse.next();
version = parse.next();
}
int status = 200;
try {
if (!method.equals("GET")) {
status = 501;
} else if (!version.equals("HTTP/1.1")) {
status = 505;
} else if (Arrays.stream(redirectedEndpoints).anyMatch((s) -> resource.startsWith(s + "?"))) {
String[] components = getComponents(resource);
String location = String.format("/calc?op=%s&%s", components[0].substring(1), components[1]);
status = 301;
headers.add("Location: " + location); // redirection
} else if (resource.startsWith("/calc?")) {
Map<String, String> qs = getQueryStrings(resource.substring(resource.indexOf('?') + 1));
if (qs.containsKey("op") && qs.containsKey("a") && qs.containsKey("b")) {
String op = qs.get("op");
Matcher matcherA = isDouble.matcher(qs.get("a"));
Matcher matcherB = isDouble.matcher(qs.get("b"));
if (matcherA.find() && matcherB.find()) {
double a = Double.parseDouble(qs.get("a"));
double b = Double.parseDouble(qs.get("b"));
if (op.equals("add")) {
response = "" + (a + b);
} else if (op.equals("subtract")) {
response = "" + (a - b);
} else if (op.equals("multiply")) {
response = "" + (a * b);
} else if (op.equals("divide")) {
response = "" + (a / b);
} else if (op.equals("exponent")) {
response = "" + Math.pow(a, b);
}
}
}
if (status == 200 && response.isEmpty()) {
status = 400;
}
} else {
status = 404;
}
} catch (Exception e) {
log.println(e);
e.printStackTrace(log);
status = 500;
}
if (status != 200 && response.isEmpty()) {
response = httpResponseCodes.get(status);
}
if (headers.size() > 0) {
sendHeaders(res, status, "text/plain", response, headers.toArray(new String[]{}));
} else {
sendHeaders(res, status, "text/plain", response);
}
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 HTTPCalcService(client)).start();
}
}
}
}