| 37 | DbgCtl dbg_ctl{WASM_DEBUG_TAG}; |
| 38 | |
| 39 | static int |
| 40 | async_handler(TSCont cont, TSEvent event, void *edata) |
| 41 | { |
| 42 | // information for the handler |
| 43 | TSHttpTxn txn = static_cast<TSHttpTxn>(edata); |
| 44 | AsyncInfo *ai = static_cast<AsyncInfo *>(TSContDataGet(cont)); |
| 45 | uint32_t token = ai->token; |
| 46 | Context *root_context = ai->root_context; |
| 47 | Wasm *wasm = root_context->wasm(); |
| 48 | |
| 49 | // variables to be used in handler |
| 50 | TSEvent result = static_cast<TSEvent>(FETCH_EVENT_ID_BASE + 1); |
| 51 | const void *body = nullptr; |
| 52 | size_t body_size = 0; |
| 53 | TSMBuffer hdr_buf = nullptr; |
| 54 | TSMLoc hdr_loc = nullptr; |
| 55 | int header_size = 0; |
| 56 | |
| 57 | TSMutexLock(wasm->mutex()); |
| 58 | // filling in variables for a successful fetch |
| 59 | if (event == static_cast<TSEvent>(FETCH_EVENT_ID_BASE)) { |
| 60 | int data_len; |
| 61 | const char *data_start = TSFetchRespGet(txn, &data_len); |
| 62 | if (data_start && (data_len > 0)) { |
| 63 | const char *data_end = data_start + data_len; |
| 64 | TSHttpParser parser = TSHttpParserCreate(); |
| 65 | hdr_buf = TSMBufferCreate(); |
| 66 | hdr_loc = TSHttpHdrCreate(hdr_buf); |
| 67 | TSHttpHdrTypeSet(hdr_buf, hdr_loc, TS_HTTP_TYPE_RESPONSE); |
| 68 | if (TSHttpHdrParseResp(parser, hdr_buf, hdr_loc, &data_start, data_end) == TS_PARSE_DONE) { |
| 69 | TSHttpStatus status = TSHttpHdrStatusGet(hdr_buf, hdr_loc); |
| 70 | header_size = TSMimeHdrFieldsCount(hdr_buf, hdr_loc); |
| 71 | body = data_start; // data_start will now be pointing to body |
| 72 | body_size = data_end - data_start; |
| 73 | Dbg(dbg_ctl, "[%s] Fetch result had a status code of %d with a body length of %ld", __FUNCTION__, status, body_size); |
| 74 | } else { |
| 75 | TSError("[wasm][%s] Unable to parse call response", __FUNCTION__); |
| 76 | event = static_cast<TSEvent>(FETCH_EVENT_ID_BASE + 1); |
| 77 | } |
| 78 | TSHttpParserDestroy(parser); |
| 79 | } else { |
| 80 | TSError("[wasm][%s] Successful fetch did not result in any content. Assuming failure", __FUNCTION__); |
| 81 | event = static_cast<TSEvent>(FETCH_EVENT_ID_BASE + 1); |
| 82 | } |
| 83 | result = event; |
| 84 | } |
| 85 | |
| 86 | // callback function |
| 87 | Dbg(dbg_ctl, "[%s] setting root context call result", __FUNCTION__); |
| 88 | root_context->setHttpCallResult(hdr_buf, hdr_loc, body, body_size, result); |
| 89 | Dbg(dbg_ctl, "[%s] trigger root context function, token: %d", __FUNCTION__, token); |
| 90 | root_context->onHttpCallResponse(token, header_size, body_size, 0); |
| 91 | Dbg(dbg_ctl, "[%s] resetting root context call result", __FUNCTION__); |
| 92 | root_context->resetHttpCallResult(); |
| 93 | |
| 94 | // cleaning up |
| 95 | if (hdr_loc) { |
| 96 | TSMLoc null_parent_loc = nullptr; |
nothing calls this directly
no test coverage detected