|
1 | 1 | package io.roastedroot.proxywasm.jaxrs; |
2 | 2 |
|
| 3 | +import static io.roastedroot.proxywasm.Helpers.bytes; |
3 | 4 | import static io.roastedroot.proxywasm.Helpers.string; |
| 5 | +import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_DNS_SAN_LOCAL_CERTIFICATE; |
| 6 | +import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_DNS_SAN_PEER_CERTIFICATE; |
| 7 | +import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_ID; |
| 8 | +import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_MTLS; |
| 9 | +import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_REQUESTED_SERVER_NAME; |
| 10 | +import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_SHA256_PEER_CERTIFICATE_DIGEST; |
| 11 | +import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_SUBJECT_LOCAL_CERTIFICATE; |
| 12 | +import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_SUBJECT_PEER_CERTIFICATE; |
| 13 | +import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_TLS_VERSION; |
| 14 | +import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_URI_SAN_LOCAL_CERTIFICATE; |
| 15 | +import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_URI_SAN_PEER_CERTIFICATE; |
| 16 | +import static io.roastedroot.proxywasm.WellKnownProperties.DESTINATION_ADDRESS; |
| 17 | +import static io.roastedroot.proxywasm.WellKnownProperties.DESTINATION_PORT; |
| 18 | +import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_DURATION; |
| 19 | +import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_PROTOCOL; |
| 20 | +import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_SIZE; |
| 21 | +import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_TIME; |
| 22 | +import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_TOTAL_SIZE; |
| 23 | +import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_SIZE; |
| 24 | +import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_TOTAL_SIZE; |
| 25 | +import static io.roastedroot.proxywasm.WellKnownProperties.SOURCE_ADDRESS; |
| 26 | +import static io.roastedroot.proxywasm.WellKnownProperties.SOURCE_PORT; |
4 | 27 |
|
5 | 28 | import io.roastedroot.proxywasm.Action; |
6 | 29 | import io.roastedroot.proxywasm.ChainedHandler; |
|
10 | 33 | import io.roastedroot.proxywasm.StreamType; |
11 | 34 | import io.roastedroot.proxywasm.WasmException; |
12 | 35 | import io.roastedroot.proxywasm.WasmResult; |
| 36 | +import io.roastedroot.proxywasm.jaxrs.spi.HttpServer; |
13 | 37 | import jakarta.ws.rs.container.ContainerRequestContext; |
14 | 38 | import jakarta.ws.rs.container.ContainerResponseContext; |
15 | 39 | import jakarta.ws.rs.core.Response; |
| 40 | +import java.util.Date; |
16 | 41 | import java.util.HashMap; |
17 | 42 | import java.util.List; |
18 | 43 |
|
19 | 44 | class HttpHandler extends ChainedHandler { |
20 | 45 |
|
21 | 46 | private final PluginHandler next; |
| 47 | + private final HttpServer httpServer; |
| 48 | + private final long startedAt; |
22 | 49 |
|
23 | | - HttpHandler(PluginHandler pluginHandler) { |
| 50 | + HttpHandler(PluginHandler pluginHandler, HttpServer httpServer) { |
24 | 51 | this.next = pluginHandler; |
| 52 | + this.httpServer = httpServer; |
| 53 | + this.startedAt = System.currentTimeMillis(); |
25 | 54 | } |
26 | 55 |
|
27 | 56 | @Override |
@@ -237,11 +266,113 @@ public Action getAction() { |
237 | 266 |
|
238 | 267 | @Override |
239 | 268 | public byte[] getProperty(List<String> path) throws WasmException { |
| 269 | + |
| 270 | + // Check to see if it's a well known property |
| 271 | + |
| 272 | + // Downstream connection properties |
| 273 | + if (CONNECTION_ID.equals(path)) { |
| 274 | + // Do we need to generate one? |
| 275 | + return null; |
| 276 | + } else if (SOURCE_ADDRESS.equals(path)) { |
| 277 | + return bytes(httpServer.remoteAddress()); |
| 278 | + } else if (SOURCE_PORT.equals(path)) { |
| 279 | + return bytes(httpServer.remotePort()); |
| 280 | + } else if (DESTINATION_ADDRESS.equals(path)) { |
| 281 | + return bytes(httpServer.localAddress()); |
| 282 | + } else if (DESTINATION_PORT.equals(path)) { |
| 283 | + return bytes(httpServer.localPort()); |
| 284 | + } |
| 285 | + |
| 286 | + // TLS connection properties |
| 287 | + else if (CONNECTION_TLS_VERSION.equals(path)) { |
| 288 | + return null; |
| 289 | + } else if (CONNECTION_REQUESTED_SERVER_NAME.equals(path)) { |
| 290 | + return null; |
| 291 | + } else if (CONNECTION_MTLS.equals(path)) { |
| 292 | + return null; |
| 293 | + } else if (CONNECTION_SUBJECT_LOCAL_CERTIFICATE.equals(path)) { |
| 294 | + return null; |
| 295 | + } else if (CONNECTION_SUBJECT_PEER_CERTIFICATE.equals(path)) { |
| 296 | + return null; |
| 297 | + } else if (CONNECTION_DNS_SAN_LOCAL_CERTIFICATE.equals(path)) { |
| 298 | + return null; |
| 299 | + } else if (CONNECTION_DNS_SAN_PEER_CERTIFICATE.equals(path)) { |
| 300 | + return null; |
| 301 | + } else if (CONNECTION_URI_SAN_LOCAL_CERTIFICATE.equals(path)) { |
| 302 | + return null; |
| 303 | + } else if (CONNECTION_URI_SAN_PEER_CERTIFICATE.equals(path)) { |
| 304 | + return null; |
| 305 | + } else if (CONNECTION_SHA256_PEER_CERTIFICATE_DIGEST.equals(path)) { |
| 306 | + return null; |
| 307 | + } |
| 308 | + |
| 309 | + // Upstream connection properties: we are not directly connecting to an upstream server, so |
| 310 | + // these are not implemented. |
| 311 | + // else if (UPSTREAM_ADDRESS.equals(path)) { |
| 312 | + // return null; |
| 313 | + // } else if (UPSTREAM_PORT.equals(path)) { |
| 314 | + // return null; |
| 315 | + // } else if (UPSTREAM_LOCAL_ADDRESS.equals(path)) { |
| 316 | + // return null; |
| 317 | + // } else if (UPSTREAM_LOCAL_PORT.equals(path)) { |
| 318 | + // return null; |
| 319 | + // } else if (UPSTREAM_TLS_VERSION.equals(path)) { |
| 320 | + // return null; |
| 321 | + // } else if (UPSTREAM_SUBJECT_LOCAL_CERTIFICATE.equals(path)) { |
| 322 | + // return null; |
| 323 | + // } else if (UPSTREAM_SUBJECT_PEER_CERTIFICATE.equals(path)) { |
| 324 | + // return null; |
| 325 | + // } else if (UPSTREAM_DNS_SAN_LOCAL_CERTIFICATE.equals(path)) { |
| 326 | + // return null; |
| 327 | + // } else if (UPSTREAM_DNS_SAN_PEER_CERTIFICATE.equals(path)) { |
| 328 | + // return null; |
| 329 | + // } else if (UPSTREAM_URI_SAN_LOCAL_CERTIFICATE.equals(path)) { |
| 330 | + // return null; |
| 331 | + // } else if (UPSTREAM_URI_SAN_PEER_CERTIFICATE.equals(path)) { |
| 332 | + // return null; |
| 333 | + // } else if (UPSTREAM_SHA256_PEER_CERTIFICATE_DIGEST.equals(path)) { |
| 334 | + // return null; |
| 335 | + // } |
| 336 | + |
| 337 | + // HTTP request properties |
| 338 | + else if (REQUEST_PROTOCOL.equals(path)) { |
| 339 | + if (requestContext == null) { |
| 340 | + return null; |
| 341 | + } |
| 342 | + return bytes(requestContext.getUriInfo().getRequestUri().getScheme()); |
| 343 | + } else if (REQUEST_TIME.equals(path)) { |
| 344 | + // TODO: check encoding /w other impls |
| 345 | + return bytes(new Date(startedAt).toString()); |
| 346 | + } else if (REQUEST_DURATION.equals(path)) { |
| 347 | + // TODO: check encoding /w other impls |
| 348 | + return bytes("" + (System.currentTimeMillis() - startedAt)); |
| 349 | + } else if (REQUEST_SIZE.equals(path)) { |
| 350 | + if (httpRequestBody == null) { |
| 351 | + return null; |
| 352 | + } |
| 353 | + // TODO: check encoding /w other impls |
| 354 | + return bytes("" + httpRequestBody.length); |
| 355 | + } else if (REQUEST_TOTAL_SIZE.equals(path)) { |
| 356 | + return null; |
| 357 | + } |
| 358 | + |
| 359 | + // HTTP response properties |
| 360 | + else if (RESPONSE_SIZE.equals(path)) { |
| 361 | + if (httpResponseBody == null) { |
| 362 | + return null; |
| 363 | + } |
| 364 | + // TODO: check encoding /w other impls |
| 365 | + return bytes("" + httpResponseBody.length); |
| 366 | + } else if (RESPONSE_TOTAL_SIZE.equals(path)) { |
| 367 | + // TODO: how can we do this? |
| 368 | + return null; |
| 369 | + } |
| 370 | + |
240 | 371 | byte[] result = properties.get(path); |
241 | | - if (result == null) { |
242 | | - return next().getProperty(path); |
| 372 | + if (result != null) { |
| 373 | + return result; |
243 | 374 | } |
244 | | - return result; |
| 375 | + return next().getProperty(path); |
245 | 376 | } |
246 | 377 |
|
247 | 378 | @Override |
|
0 commit comments