forked from roastedroot/proxy-wasm-java-host
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpHandler.java
More file actions
383 lines (325 loc) · 13.2 KB
/
HttpHandler.java
File metadata and controls
383 lines (325 loc) · 13.2 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package io.roastedroot.proxywasm.jaxrs;
import static io.roastedroot.proxywasm.Helpers.bytes;
import static io.roastedroot.proxywasm.Helpers.string;
import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_DNS_SAN_LOCAL_CERTIFICATE;
import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_DNS_SAN_PEER_CERTIFICATE;
import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_ID;
import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_MTLS;
import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_REQUESTED_SERVER_NAME;
import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_SHA256_PEER_CERTIFICATE_DIGEST;
import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_SUBJECT_LOCAL_CERTIFICATE;
import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_SUBJECT_PEER_CERTIFICATE;
import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_TLS_VERSION;
import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_URI_SAN_LOCAL_CERTIFICATE;
import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_URI_SAN_PEER_CERTIFICATE;
import static io.roastedroot.proxywasm.WellKnownProperties.DESTINATION_ADDRESS;
import static io.roastedroot.proxywasm.WellKnownProperties.DESTINATION_PORT;
import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_DURATION;
import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_PROTOCOL;
import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_SIZE;
import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_TIME;
import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_TOTAL_SIZE;
import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_SIZE;
import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_TOTAL_SIZE;
import static io.roastedroot.proxywasm.WellKnownProperties.SOURCE_ADDRESS;
import static io.roastedroot.proxywasm.WellKnownProperties.SOURCE_PORT;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.ChainedHandler;
import io.roastedroot.proxywasm.Handler;
import io.roastedroot.proxywasm.Helpers;
import io.roastedroot.proxywasm.ProxyMap;
import io.roastedroot.proxywasm.StreamType;
import io.roastedroot.proxywasm.WasmException;
import io.roastedroot.proxywasm.WasmResult;
import io.roastedroot.proxywasm.jaxrs.spi.HttpServer;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerResponseContext;
import jakarta.ws.rs.core.Response;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
class HttpHandler extends ChainedHandler {
private final PluginHandler next;
private final HttpServer httpServer;
private final long startedAt;
HttpHandler(PluginHandler pluginHandler, HttpServer httpServer) {
this.next = pluginHandler;
this.httpServer = httpServer;
this.startedAt = System.currentTimeMillis();
}
@Override
protected Handler next() {
return next;
}
// //////////////////////////////////////////////////////////////////////
// HTTP fields
// //////////////////////////////////////////////////////////////////////
private ContainerRequestContext requestContext;
public ContainerRequestContext getRequestContext() {
return requestContext;
}
public void setRequestContext(ContainerRequestContext requestContext) {
this.requestContext = requestContext;
}
@Override
public ProxyMap getHttpRequestHeaders() {
return new JaxrsProxyMap(requestContext.getHeaders());
}
@Override
public ProxyMap getHttpRequestTrailers() {
return null;
}
private ContainerResponseContext responseContext;
public void setResponseContext(ContainerResponseContext responseContext) {
this.responseContext = responseContext;
}
@Override
public ProxyMap getHttpResponseHeaders() {
return new JaxrsProxyMap(responseContext.getHeaders());
}
@Override
public ProxyMap getHttpResponseTrailers() {
return null;
}
@Override
public ProxyMap getGrpcReceiveInitialMetaData() {
return null;
}
@Override
public ProxyMap getGrpcReceiveTrailerMetaData() {
return null;
}
// //////////////////////////////////////////////////////////////////////
// Buffers
// //////////////////////////////////////////////////////////////////////
private byte[] httpRequestBody = new byte[0];
@Override
public byte[] getHttpRequestBody() {
return this.httpRequestBody;
}
@Override
public WasmResult setHttpRequestBody(byte[] body) {
this.httpRequestBody = body;
return WasmResult.OK;
}
public void appendHttpRequestBody(byte[] body) {
this.httpRequestBody = Helpers.append(this.httpRequestBody, body);
}
private byte[] grpcReceiveBuffer = new byte[0];
@Override
public byte[] getGrpcReceiveBuffer() {
return this.grpcReceiveBuffer;
}
@Override
public WasmResult setGrpcReceiveBuffer(byte[] buffer) {
this.grpcReceiveBuffer = buffer;
return WasmResult.OK;
}
private byte[] upstreamData = new byte[0];
@Override
public byte[] getUpstreamData() {
return this.upstreamData;
}
@Override
public WasmResult setUpstreamData(byte[] data) {
this.upstreamData = data;
return WasmResult.OK;
}
private byte[] downStreamData = new byte[0];
@Override
public byte[] getDownStreamData() {
return this.downStreamData;
}
@Override
public WasmResult setDownStreamData(byte[] data) {
this.downStreamData = data;
return WasmResult.OK;
}
private byte[] httpResponseBody = new byte[0];
@Override
public byte[] getHttpResponseBody() {
return this.httpResponseBody;
}
@Override
public WasmResult setHttpResponseBody(byte[] body) {
this.httpResponseBody = body;
return WasmResult.OK;
}
public void appendHttpResponseBody(byte[] body) {
this.httpResponseBody = Helpers.append(this.httpResponseBody, body);
}
// //////////////////////////////////////////////////////////////////////
// HTTP streams
// //////////////////////////////////////////////////////////////////////
public static class HttpResponse {
public final int statusCode;
public final byte[] statusCodeDetails;
public final byte[] body;
public final ProxyMap headers;
public final int grpcStatus;
public HttpResponse(
int responseCode,
byte[] responseCodeDetails,
byte[] responseBody,
ProxyMap additionalHeaders,
int grpcStatus) {
this.statusCode = responseCode;
this.statusCodeDetails = responseCodeDetails;
this.body = responseBody;
this.headers = additionalHeaders;
this.grpcStatus = grpcStatus;
}
public Response toResponse() {
Response.ResponseBuilder builder =
Response.status(statusCode, string(statusCodeDetails));
if (headers != null) {
for (var entry : headers.entries()) {
builder = builder.header(entry.getKey(), entry.getValue());
}
}
builder.entity(body);
return builder.build();
}
}
private HttpResponse senthttpResponse;
@Override
public WasmResult sendHttpResponse(
int responseCode,
byte[] responseCodeDetails,
byte[] responseBody,
ProxyMap additionalHeaders,
int grpcStatus) {
this.senthttpResponse =
new HttpResponse(
responseCode,
responseCodeDetails,
responseBody,
additionalHeaders,
grpcStatus);
return WasmResult.OK;
}
public HttpResponse getSentHttpResponse() {
return senthttpResponse;
}
private Action action;
@Override
public WasmResult setAction(StreamType streamType, Action action) {
this.action = action;
return WasmResult.OK;
}
public Action getAction() {
return action;
}
// //////////////////////////////////////////////////////////////////////
// Properties
// //////////////////////////////////////////////////////////////////////
final HashMap<List<String>, byte[]> properties = new HashMap<>();
@Override
public byte[] getProperty(List<String> path) throws WasmException {
// Check to see if it's a well known property
// Downstream connection properties
if (CONNECTION_ID.equals(path)) {
// Do we need to generate one?
return null;
} else if (SOURCE_ADDRESS.equals(path)) {
return bytes(httpServer.remoteAddress());
} else if (SOURCE_PORT.equals(path)) {
return bytes(httpServer.remotePort());
} else if (DESTINATION_ADDRESS.equals(path)) {
return bytes(httpServer.localAddress());
} else if (DESTINATION_PORT.equals(path)) {
return bytes(httpServer.localPort());
}
// TLS connection properties
else if (CONNECTION_TLS_VERSION.equals(path)) {
return null;
} else if (CONNECTION_REQUESTED_SERVER_NAME.equals(path)) {
return null;
} else if (CONNECTION_MTLS.equals(path)) {
return null;
} else if (CONNECTION_SUBJECT_LOCAL_CERTIFICATE.equals(path)) {
return null;
} else if (CONNECTION_SUBJECT_PEER_CERTIFICATE.equals(path)) {
return null;
} else if (CONNECTION_DNS_SAN_LOCAL_CERTIFICATE.equals(path)) {
return null;
} else if (CONNECTION_DNS_SAN_PEER_CERTIFICATE.equals(path)) {
return null;
} else if (CONNECTION_URI_SAN_LOCAL_CERTIFICATE.equals(path)) {
return null;
} else if (CONNECTION_URI_SAN_PEER_CERTIFICATE.equals(path)) {
return null;
} else if (CONNECTION_SHA256_PEER_CERTIFICATE_DIGEST.equals(path)) {
return null;
}
// Upstream connection properties: we are not directly connecting to an upstream server, so
// these are not implemented.
// else if (UPSTREAM_ADDRESS.equals(path)) {
// return null;
// } else if (UPSTREAM_PORT.equals(path)) {
// return null;
// } else if (UPSTREAM_LOCAL_ADDRESS.equals(path)) {
// return null;
// } else if (UPSTREAM_LOCAL_PORT.equals(path)) {
// return null;
// } else if (UPSTREAM_TLS_VERSION.equals(path)) {
// return null;
// } else if (UPSTREAM_SUBJECT_LOCAL_CERTIFICATE.equals(path)) {
// return null;
// } else if (UPSTREAM_SUBJECT_PEER_CERTIFICATE.equals(path)) {
// return null;
// } else if (UPSTREAM_DNS_SAN_LOCAL_CERTIFICATE.equals(path)) {
// return null;
// } else if (UPSTREAM_DNS_SAN_PEER_CERTIFICATE.equals(path)) {
// return null;
// } else if (UPSTREAM_URI_SAN_LOCAL_CERTIFICATE.equals(path)) {
// return null;
// } else if (UPSTREAM_URI_SAN_PEER_CERTIFICATE.equals(path)) {
// return null;
// } else if (UPSTREAM_SHA256_PEER_CERTIFICATE_DIGEST.equals(path)) {
// return null;
// }
// HTTP request properties
else if (REQUEST_PROTOCOL.equals(path)) {
if (requestContext == null) {
return null;
}
return bytes(requestContext.getUriInfo().getRequestUri().getScheme());
} else if (REQUEST_TIME.equals(path)) {
// TODO: check encoding /w other impls
return bytes(new Date(startedAt).toString());
} else if (REQUEST_DURATION.equals(path)) {
// TODO: check encoding /w other impls
return bytes("" + (System.currentTimeMillis() - startedAt));
} else if (REQUEST_SIZE.equals(path)) {
if (httpRequestBody == null) {
return null;
}
// TODO: check encoding /w other impls
return bytes("" + httpRequestBody.length);
} else if (REQUEST_TOTAL_SIZE.equals(path)) {
return null;
}
// HTTP response properties
else if (RESPONSE_SIZE.equals(path)) {
if (httpResponseBody == null) {
return null;
}
// TODO: check encoding /w other impls
return bytes("" + httpResponseBody.length);
} else if (RESPONSE_TOTAL_SIZE.equals(path)) {
// TODO: how can we do this?
return null;
}
byte[] result = properties.get(path);
if (result != null) {
return result;
}
return next().getProperty(path);
}
@Override
public WasmResult setProperty(List<String> path, byte[] value) {
properties.put(path, value);
return WasmResult.OK;
}
}