|
| 1 | +package io.roastedroot.proxywasm.plugin; |
| 2 | + |
| 3 | +import io.roastedroot.proxywasm.Action; |
| 4 | +import io.roastedroot.proxywasm.ChainedHandler; |
| 5 | +import io.roastedroot.proxywasm.Handler; |
| 6 | +import io.roastedroot.proxywasm.Helpers; |
| 7 | +import io.roastedroot.proxywasm.ProxyMap; |
| 8 | +import io.roastedroot.proxywasm.StreamType; |
| 9 | +import io.roastedroot.proxywasm.WasmException; |
| 10 | +import io.roastedroot.proxywasm.WasmResult; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.concurrent.CountDownLatch; |
| 14 | + |
| 15 | +public class HttpContext { |
| 16 | + |
| 17 | + private final Plugin plugin; |
| 18 | + private final io.roastedroot.proxywasm.HttpContext context; |
| 19 | + private final HttpRequestAdaptor requestAdaptor; |
| 20 | + private final long startedAt = System.currentTimeMillis(); |
| 21 | + |
| 22 | + final HashMap<List<String>, byte[]> properties = new HashMap<>(); |
| 23 | + private byte[] httpRequestBody = new byte[0]; |
| 24 | + private byte[] grpcReceiveBuffer = new byte[0]; |
| 25 | + private byte[] upstreamData = new byte[0]; |
| 26 | + private byte[] downStreamData = new byte[0]; |
| 27 | + private byte[] httpResponseBody = new byte[0]; |
| 28 | + private SendResponse sendResponse; |
| 29 | + private Action action; |
| 30 | + private CountDownLatch resumeLatch; |
| 31 | + |
| 32 | + HttpContext(Plugin plugin, HttpRequestAdaptor requestAdaptor) { |
| 33 | + this.plugin = plugin; |
| 34 | + this.requestAdaptor = requestAdaptor; |
| 35 | + this.context = plugin.wasm.createHttpContext(new HandlerImpl()); |
| 36 | + } |
| 37 | + |
| 38 | + public Plugin plugin() { |
| 39 | + return plugin; |
| 40 | + } |
| 41 | + |
| 42 | + public io.roastedroot.proxywasm.HttpContext context() { |
| 43 | + return context; |
| 44 | + } |
| 45 | + |
| 46 | + public HttpRequestAdaptor requestAdaptor() { |
| 47 | + return requestAdaptor; |
| 48 | + } |
| 49 | + |
| 50 | + public Action getAction() { |
| 51 | + return action; |
| 52 | + } |
| 53 | + |
| 54 | + public void maybePause() { |
| 55 | + // don't pause if plugin wants us to continue |
| 56 | + if (action == Action.CONTINUE) { |
| 57 | + return; |
| 58 | + } |
| 59 | + // don't pause if the plugin wants to respond to the request |
| 60 | + if (sendResponse != null) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // ok, lets pause the request processing. A tick or http call response event will |
| 65 | + // need to resume the processing |
| 66 | + resumeLatch = new CountDownLatch(1); |
| 67 | + plugin.unlock(); |
| 68 | + try { |
| 69 | + resumeLatch.await(); |
| 70 | + } catch (InterruptedException ignore) { |
| 71 | + return; |
| 72 | + } finally { |
| 73 | + plugin.lock(); |
| 74 | + resumeLatch = null; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public byte[] getHttpRequestBody() { |
| 79 | + return httpRequestBody; |
| 80 | + } |
| 81 | + |
| 82 | + public byte[] getHttpResponseBody() { |
| 83 | + return httpResponseBody; |
| 84 | + } |
| 85 | + |
| 86 | + public void setHttpRequestBody(byte[] httpRequestBody) { |
| 87 | + this.httpRequestBody = httpRequestBody; |
| 88 | + } |
| 89 | + |
| 90 | + public byte[] getGrpcReceiveBuffer() { |
| 91 | + return grpcReceiveBuffer; |
| 92 | + } |
| 93 | + |
| 94 | + public void setGrpcReceiveBuffer(byte[] grpcReceiveBuffer) { |
| 95 | + this.grpcReceiveBuffer = grpcReceiveBuffer; |
| 96 | + } |
| 97 | + |
| 98 | + public byte[] getUpstreamData() { |
| 99 | + return upstreamData; |
| 100 | + } |
| 101 | + |
| 102 | + public void setUpstreamData(byte[] upstreamData) { |
| 103 | + this.upstreamData = upstreamData; |
| 104 | + } |
| 105 | + |
| 106 | + public byte[] getDownStreamData() { |
| 107 | + return downStreamData; |
| 108 | + } |
| 109 | + |
| 110 | + public void setDownStreamData(byte[] downStreamData) { |
| 111 | + this.downStreamData = downStreamData; |
| 112 | + } |
| 113 | + |
| 114 | + public void setHttpResponseBody(byte[] httpResponseBody) { |
| 115 | + this.httpResponseBody = httpResponseBody; |
| 116 | + } |
| 117 | + |
| 118 | + public SendResponse getSendResponse() { |
| 119 | + return sendResponse; |
| 120 | + } |
| 121 | + |
| 122 | + public SendResponse consumeSentHttpResponse() { |
| 123 | + var result = sendResponse; |
| 124 | + sendResponse = null; |
| 125 | + return result; |
| 126 | + } |
| 127 | + |
| 128 | + class HandlerImpl extends ChainedHandler { |
| 129 | + |
| 130 | + @Override |
| 131 | + protected Handler next() { |
| 132 | + return plugin.handler; |
| 133 | + } |
| 134 | + |
| 135 | + public ProxyMap getHttpRequestHeaders() { |
| 136 | + return requestAdaptor.getHttpRequestHeaders(); |
| 137 | + } |
| 138 | + |
| 139 | + public ProxyMap getHttpRequestTrailers() { |
| 140 | + return requestAdaptor.getHttpRequestTrailers(); |
| 141 | + } |
| 142 | + |
| 143 | + public ProxyMap getHttpResponseHeaders() { |
| 144 | + return requestAdaptor.getHttpResponseHeaders(); |
| 145 | + } |
| 146 | + |
| 147 | + public ProxyMap getHttpResponseTrailers() { |
| 148 | + return requestAdaptor.getHttpResponseTrailers(); |
| 149 | + } |
| 150 | + |
| 151 | + public ProxyMap getGrpcReceiveInitialMetaData() { |
| 152 | + return requestAdaptor.getGrpcReceiveInitialMetaData(); |
| 153 | + } |
| 154 | + |
| 155 | + public ProxyMap getGrpcReceiveTrailerMetaData() { |
| 156 | + return requestAdaptor.getGrpcReceiveTrailerMetaData(); |
| 157 | + } |
| 158 | + |
| 159 | + // ////////////////////////////////////////////////////////////////////// |
| 160 | + // Buffers |
| 161 | + // ////////////////////////////////////////////////////////////////////// |
| 162 | + |
| 163 | + @Override |
| 164 | + public byte[] getHttpRequestBody() { |
| 165 | + return httpRequestBody; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public WasmResult setHttpRequestBody(byte[] body) { |
| 170 | + httpRequestBody = body; |
| 171 | + return WasmResult.OK; |
| 172 | + } |
| 173 | + |
| 174 | + public void appendHttpRequestBody(byte[] body) { |
| 175 | + httpRequestBody = Helpers.append(httpRequestBody, body); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public byte[] getGrpcReceiveBuffer() { |
| 180 | + return grpcReceiveBuffer; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public WasmResult setGrpcReceiveBuffer(byte[] buffer) { |
| 185 | + grpcReceiveBuffer = buffer; |
| 186 | + return WasmResult.OK; |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public byte[] getUpstreamData() { |
| 191 | + return upstreamData; |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public WasmResult setUpstreamData(byte[] data) { |
| 196 | + upstreamData = data; |
| 197 | + return WasmResult.OK; |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public byte[] getDownStreamData() { |
| 202 | + return downStreamData; |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public WasmResult setDownStreamData(byte[] data) { |
| 207 | + downStreamData = data; |
| 208 | + return WasmResult.OK; |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public byte[] getHttpResponseBody() { |
| 213 | + return httpResponseBody; |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public WasmResult setHttpResponseBody(byte[] body) { |
| 218 | + httpResponseBody = body; |
| 219 | + return WasmResult.OK; |
| 220 | + } |
| 221 | + |
| 222 | + public void appendHttpResponseBody(byte[] body) { |
| 223 | + httpResponseBody = Helpers.append(httpResponseBody, body); |
| 224 | + } |
| 225 | + |
| 226 | + // ////////////////////////////////////////////////////////////////////// |
| 227 | + // HTTP streams |
| 228 | + // ////////////////////////////////////////////////////////////////////// |
| 229 | + |
| 230 | + @Override |
| 231 | + public WasmResult sendHttpResponse( |
| 232 | + int responseCode, |
| 233 | + byte[] responseCodeDetails, |
| 234 | + byte[] responseBody, |
| 235 | + ProxyMap additionalHeaders, |
| 236 | + int grpcStatus) { |
| 237 | + sendResponse = |
| 238 | + new SendResponse( |
| 239 | + responseCode, |
| 240 | + responseCodeDetails, |
| 241 | + responseBody, |
| 242 | + additionalHeaders, |
| 243 | + grpcStatus); |
| 244 | + |
| 245 | + if (resumeLatch != null) { |
| 246 | + resumeLatch.countDown(); |
| 247 | + } |
| 248 | + return WasmResult.OK; |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public WasmResult setAction(StreamType streamType, Action actionValue) { |
| 253 | + action = actionValue; |
| 254 | + if (action == Action.CONTINUE && resumeLatch != null) { |
| 255 | + resumeLatch.countDown(); |
| 256 | + } |
| 257 | + return WasmResult.OK; |
| 258 | + } |
| 259 | + |
| 260 | + // ////////////////////////////////////////////////////////////////////// |
| 261 | + // Properties |
| 262 | + // ////////////////////////////////////////////////////////////////////// |
| 263 | + @Override |
| 264 | + public byte[] getProperty(List<String> path) throws WasmException { |
| 265 | + byte[] result = requestAdaptor.getProperty(HttpContext.this, path); |
| 266 | + if (result == null) { |
| 267 | + result = properties.get(path); |
| 268 | + } |
| 269 | + return result; |
| 270 | + } |
| 271 | + |
| 272 | + @Override |
| 273 | + public WasmResult setProperty(List<String> path, byte[] value) { |
| 274 | + var result = requestAdaptor.setProperty(HttpContext.this, path, value); |
| 275 | + if (result == WasmResult.NOT_FOUND) { |
| 276 | + properties.put(path, value); |
| 277 | + } |
| 278 | + return WasmResult.OK; |
| 279 | + } |
| 280 | + } |
| 281 | +} |
0 commit comments