* Initialize and schedule the background fetch */
| 41 | * Initialize and schedule the background fetch |
| 42 | */ |
| 43 | bool |
| 44 | BgBlockFetch::fetch(Data *const data) |
| 45 | { |
| 46 | if (m_stream.m_read.isOpen()) { |
| 47 | // should never happen since the connection was just initialized |
| 48 | ERROR_LOG("Background block request already in flight!"); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | int64_t const blockbeg = (data->m_config->m_blockbytes * m_blocknum); |
| 53 | Range blockbe(blockbeg, blockbeg + data->m_config->m_blockbytes); |
| 54 | |
| 55 | char rangestr[1024]; |
| 56 | int rangelen = sizeof(rangestr); |
| 57 | bool const rpstat = blockbe.toStringClosed(rangestr, &rangelen); |
| 58 | TSAssert(rpstat); |
| 59 | |
| 60 | DEBUG_LOG("Request background block: %s", rangestr); |
| 61 | |
| 62 | // reuse the incoming client header, just change the range |
| 63 | HttpHeader header(data->m_req_hdrmgr.m_buffer, data->m_req_hdrmgr.m_lochdr); |
| 64 | |
| 65 | // add/set sub range key and add slicer tag |
| 66 | bool const rangestat = header.setKeyVal(TS_MIME_FIELD_RANGE, TS_MIME_LEN_RANGE, rangestr, rangelen); |
| 67 | |
| 68 | if (!rangestat) { |
| 69 | ERROR_LOG("Error trying to set range request header %s", rangestr); |
| 70 | return false; |
| 71 | } |
| 72 | TSAssert(nullptr == m_cont); |
| 73 | |
| 74 | // Setup the continuation |
| 75 | m_cont = TSContCreate(handler, TSMutexCreate()); |
| 76 | TSContDataSet(m_cont, static_cast<void *>(this)); |
| 77 | |
| 78 | // create virtual connection back into ATS |
| 79 | TSHttpConnectOptions options = TSHttpConnectOptionsGet(TS_CONNECT_PLUGIN); |
| 80 | options.addr = reinterpret_cast<sockaddr *>(&data->m_client_ip); |
| 81 | options.tag = PLUGIN_NAME; |
| 82 | options.id = 0; |
| 83 | options.buffer_index = data->m_buffer_index; |
| 84 | options.buffer_water_mark = data->m_buffer_water_mark; |
| 85 | |
| 86 | TSVConn const upvc = TSHttpConnectPlugin(&options); |
| 87 | |
| 88 | int const hlen = TSHttpHdrLengthGet(header.m_buffer, header.m_lochdr); |
| 89 | |
| 90 | // set up connection with the HttpConnect server |
| 91 | m_stream.setupConnection(upvc); |
| 92 | m_stream.setupVioWrite(m_cont, hlen); |
| 93 | TSHttpHdrPrint(header.m_buffer, header.m_lochdr, m_stream.m_write.m_iobuf); |
| 94 | |
| 95 | if (dbg_ctl.on()) { |
| 96 | std::string const headerstr(header.toString()); |
| 97 | DEBUG_LOG("Headers\n%s", headerstr.c_str()); |
| 98 | } |
| 99 | // ensure blocks are pulled through to cache |
| 100 | m_stream.setupVioRead(m_cont, INT64_MAX); |
no test coverage detected