This sets up the data and continuation properly, this is done outside of the CTor, since this can actually fail. If we fail, the data is useless, and should be delete'd. This needs the txnp temporarily, so it can copy the pristine request URL. The txnp is not used once initialize() returns. Upon successful completion, the struct should be ready to start a background fetch.
| 226 | // Upon successful completion, the struct should be ready to start a |
| 227 | // background fetch. |
| 228 | bool |
| 229 | BgFetchData::initialize(TSMBuffer request, TSMLoc req_hdr, TSHttpTxn txnp) |
| 230 | { |
| 231 | struct sockaddr const *ip = TSHttpTxnClientAddrGet(txnp); |
| 232 | bool ret = false; |
| 233 | |
| 234 | TSAssert(TS_NULL_MLOC == hdr_loc); |
| 235 | TSAssert(TS_NULL_MLOC == url_loc); |
| 236 | |
| 237 | if (ip) { |
| 238 | if (ip->sa_family == AF_INET) { |
| 239 | memcpy(&client_ip, ip, sizeof(sockaddr_in)); |
| 240 | } else if (ip->sa_family == AF_INET6) { |
| 241 | memcpy(&client_ip, ip, sizeof(sockaddr_in6)); |
| 242 | } else if (ip->sa_family == AF_UNIX) { |
| 243 | memcpy(&client_ip, ip, sizeof(sockaddr_un)); |
| 244 | } else { |
| 245 | TSError("[%s] Unknown address family %d", PLUGIN_NAME, ip->sa_family); |
| 246 | return false; |
| 247 | } |
| 248 | } else { |
| 249 | TSError("[%s] Failed to get client host info", PLUGIN_NAME); |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | hdr_loc = TSHttpHdrCreate(mbuf); |
| 254 | if (TS_SUCCESS == TSHttpHdrCopy(mbuf, hdr_loc, request, req_hdr)) { |
| 255 | TSMLoc p_url; |
| 256 | |
| 257 | // Now copy the pristine request URL into our MBuf |
| 258 | if (TS_SUCCESS == TSHttpTxnPristineUrlGet(txnp, &request, &p_url)) { |
| 259 | if (TS_SUCCESS == TSUrlClone(mbuf, request, p_url, &url_loc)) { |
| 260 | TSMLoc c_url = TS_NULL_MLOC; |
| 261 | int len; |
| 262 | char *url = nullptr; |
| 263 | |
| 264 | // Get the cache key URL (for now), since this has better lookup behavior when using |
| 265 | // e.g. the cachekey plugin. |
| 266 | if (TS_SUCCESS == TSUrlCreate(request, &c_url)) { |
| 267 | if (TS_SUCCESS == TSHttpTxnCacheLookupUrlGet(txnp, request, c_url)) { |
| 268 | url = TSUrlStringGet(request, c_url, &len); |
| 269 | TSHandleMLocRelease(request, TS_NULL_MLOC, c_url); |
| 270 | Dbg(Bg_dbg_ctl, "Cache URL is %.*s", len, url); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (url) { |
| 275 | _url.assign(url, len); // Save away the cache URL for later use when acquiring lock |
| 276 | TSfree(static_cast<void *>(url)); |
| 277 | |
| 278 | if (TS_SUCCESS == TSHttpHdrUrlSet(mbuf, hdr_loc, url_loc)) { |
| 279 | // Make sure we have the correct Host: header for this request. |
| 280 | const char *hostp = TSUrlHostGet(mbuf, url_loc, &len); |
| 281 | |
| 282 | if (set_header(mbuf, hdr_loc, TS_MIME_FIELD_HOST, TS_MIME_LEN_HOST, hostp, len)) { |
| 283 | Dbg(Bg_dbg_ctl, "Set header Host: %.*s", len, hostp); |
| 284 | } |
| 285 |
no test coverage detected