///////////////////////////////////////////////////////////////////////// Main "plugin", which is a global READ_RESPONSE_HDR hook. Before initiating a background fetch, this checks: 1. Check if a background fetch is allowed for this request and 2. Is the response from origin a 206 (Partial)? We defer the check on cacheability to the SEND_RESPONSE_HDR hook, since there could be other plugins that
| 512 | // there could be other plugins that modifies the response after us. |
| 513 | // |
| 514 | static int |
| 515 | cont_handle_response(TSCont contp, TSEvent event, void *edata) |
| 516 | { |
| 517 | // ToDo: If we want to support per-remap configurations, we have to pass along the data here |
| 518 | TSHttpTxn txnp = static_cast<TSHttpTxn>(edata); |
| 519 | BgFetchConfig *config = static_cast<BgFetchConfig *>(TSContDataGet(contp)); |
| 520 | |
| 521 | if (nullptr == config) { |
| 522 | // something seriously wrong.. |
| 523 | TSError("[%s] Can't get configurations", PLUGIN_NAME); |
| 524 | } else { |
| 525 | switch (event) { |
| 526 | case TS_EVENT_HTTP_READ_RESPONSE_HDR: |
| 527 | if (config->bgFetchAllowed(txnp)) { |
| 528 | TSMBuffer response; |
| 529 | TSMLoc resp_hdr; |
| 530 | |
| 531 | if (TS_SUCCESS == TSHttpTxnServerRespGet(txnp, &response, &resp_hdr)) { |
| 532 | // ToDo: Check the MIME type first, to see if it's a type we care about. |
| 533 | // ToDo: Such MIME types should probably be per remap rule. |
| 534 | |
| 535 | // Only deal with 206 and possibly 304 responses from Origin |
| 536 | TSHttpStatus status = TSHttpHdrStatusGet(response, resp_hdr); |
| 537 | Dbg(Bg_dbg_ctl, "Testing: response status code: %d?", status); |
| 538 | if (TS_HTTP_STATUS_PARTIAL_CONTENT == status || (config->allow304() && TS_HTTP_STATUS_NOT_MODIFIED == status)) { |
| 539 | // Everything looks good so far, add a TXN hook for SEND_RESPONSE_HDR |
| 540 | TSCont localcontp = TSContCreate(cont_check_cacheable, nullptr); |
| 541 | |
| 542 | TSHttpTxnHookAdd(txnp, TS_HTTP_SEND_RESPONSE_HDR_HOOK, localcontp); |
| 543 | } |
| 544 | // Release the response MLoc |
| 545 | TSHandleMLocRelease(response, TS_NULL_MLOC, resp_hdr); |
| 546 | } |
| 547 | } |
| 548 | break; |
| 549 | default: |
| 550 | TSError("[%s] Unknown event for this plugin", PLUGIN_NAME); |
| 551 | Dbg(Bg_dbg_ctl, "unknown event for this plugin"); |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | // Reenable and continue with the state machine. |
| 557 | TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | /////////////////////////////////////////////////////////////////////////// |
| 562 | // Setup global hooks |
nothing calls this directly
no test coverage detected