1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
|
/** @file
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "server.h"
#include "Config.h"
#include "ContentRange.h"
#include "HttpHeader.h"
#include "response.h"
#include "transfer.h"
#include "util.h"
#include "ts/experimental.h"
#include <cinttypes>
namespace
{
ContentRange
contentRangeFrom(HttpHeader const &header)
{
ContentRange bcr;
/* Pull content length off the response header
and manipulate it into a client response header
*/
char rangestr[1024];
int rangelen = sizeof(rangestr);
// look for expected Content-Range field
bool const hasContentRange(header.valueForKey(TS_MIME_FIELD_CONTENT_RANGE, TS_MIME_LEN_CONTENT_RANGE, rangestr, &rangelen));
if (!hasContentRange) {
DEBUG_LOG("invalid response header, no Content-Range");
} else {
// ensure null termination
rangestr[rangelen] = '\0';
if (!bcr.fromStringClosed(rangestr)) {
DEBUG_LOG("invalid response header, malformed Content-Range, %s", rangestr);
}
}
return bcr;
}
int64_t
contentLengthFrom(HttpHeader const &header)
{
int64_t bytes = 0;
char constr[1024];
int conlen = sizeof(constr);
// look for expected Content-Length field
bool const hasContentLength(header.valueForKey(TS_MIME_FIELD_CONTENT_LENGTH, TS_MIME_LEN_CONTENT_LENGTH, constr, &conlen));
if (!hasContentLength) {
DEBUG_LOG("invalid response header, no Content-Length");
bytes = INT64_MAX;
} else {
// ensure null termination
constr[conlen] = '\0';
char *endptr = nullptr;
bytes = std::max(static_cast<int64_t>(0), static_cast<int64_t>(strtoll(constr, &endptr, 10)));
}
return bytes;
}
// Also reference server header
enum HeaderState {
Good,
Fail,
Passthru,
};
HeaderState
handleFirstServerHeader(Data *const data, TSCont const contp)
{
HttpHeader header(data->m_resp_hdrmgr.m_buffer, data->m_resp_hdrmgr.m_lochdr);
if (TSIsDebugTagSet(PLUGIN_NAME)) {
DEBUG_LOG("First header\n%s", header.toString().c_str());
}
data->m_dnstream.setupVioWrite(contp, INT64_MAX);
TSVIO const output_vio = data->m_dnstream.m_write.m_vio;
TSIOBuffer const output_buf = data->m_dnstream.m_write.m_iobuf;
// only process a 206, everything else gets a (possibly incomplete)
// pass through
if (TS_HTTP_STATUS_PARTIAL_CONTENT != header.status()) {
DEBUG_LOG("Initial response other than 206: %d", header.status());
// Should run TSVIONSetBytes(output_io, hlen + bodybytes);
int64_t const hlen = TSHttpHdrLengthGet(header.m_buffer, header.m_lochdr);
int64_t const clen = contentLengthFrom(header);
if (TS_HTTP_STATUS_OK == header.status() && data->onlyHeader()) {
DEBUG_LOG("HEAD/PURGE request stripped Range header: expects 200");
data->m_bytestosend = hlen;
data->m_blockexpected = 0;
TSVIONBytesSet(output_vio, hlen);
TSHttpHdrPrint(header.m_buffer, header.m_lochdr, output_buf);
data->m_bytessent = hlen;
TSVIOReenable(output_vio);
return HeaderState::Good;
}
DEBUG_LOG("Passthru bytes: header: %" PRId64 " body: %" PRId64, hlen, clen);
if (clen != INT64_MAX) {
TSVIONBytesSet(output_vio, hlen + clen);
} else {
TSVIONBytesSet(output_vio, clen);
}
TSHttpHdrPrint(header.m_buffer, header.m_lochdr, output_buf);
return HeaderState::Passthru;
}
ContentRange const blockcr = contentRangeFrom(header);
// 206 with bad content range -- should NEVER happen.
if (!blockcr.isValid()) {
std::string const msg502 = string502(header.version());
TSVIONBytesSet(output_vio, msg502.size());
TSIOBufferWrite(output_buf, msg502.data(), msg502.size());
TSVIOReenable(output_vio);
return HeaderState::Fail;
}
// set the resource content length from block response
data->m_contentlen = blockcr.m_length;
// special case last N bytes
if (data->m_req_range.isEndBytes()) {
data->m_req_range.m_end += data->m_contentlen;
data->m_req_range.m_beg += data->m_contentlen;
data->m_req_range.m_beg = std::max(static_cast<int64_t>(0), data->m_req_range.m_beg);
} else {
// fix up request range end now that we have the content length
data->m_req_range.m_end = std::min(data->m_contentlen, data->m_req_range.m_end);
}
int64_t const bodybytes = data->m_req_range.size();
// range begins past end of data but inside last block, send 416
bool const send416 = (bodybytes <= 0 || TS_HTTP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE == data->m_statustype);
if (send416) {
std::string const &bodystr = bodyString416();
form416HeaderAndBody(header, data->m_contentlen, bodystr);
int const hlen = TSHttpHdrLengthGet(header.m_buffer, header.m_lochdr);
int64_t const blen = bodystr.size();
TSVIONBytesSet(output_vio, int64_t(hlen) + blen);
TSHttpHdrPrint(header.m_buffer, header.m_lochdr, output_buf);
TSIOBufferWrite(output_buf, bodystr.data(), bodystr.size());
TSVIOReenable(output_vio);
data->m_upstream.m_read.close();
return HeaderState::Fail;
}
// save data header string
data->m_datelen = sizeof(data->m_date);
header.valueForKey(TS_MIME_FIELD_DATE, TS_MIME_LEN_DATE, data->m_date, &data->m_datelen);
// save weak cache header identifiers (rfc7232 section 2)
data->m_etaglen = sizeof(data->m_etag);
header.valueForKey(TS_MIME_FIELD_ETAG, TS_MIME_LEN_ETAG, data->m_etag, &data->m_etaglen);
data->m_lastmodifiedlen = sizeof(data->m_lastmodified);
header.valueForKey(TS_MIME_FIELD_LAST_MODIFIED, TS_MIME_LEN_LAST_MODIFIED, data->m_lastmodified, &data->m_lastmodifiedlen);
// Now we can set up the expected client response
if (TS_HTTP_STATUS_PARTIAL_CONTENT == data->m_statustype) {
ContentRange respcr;
respcr.m_beg = data->m_req_range.m_beg;
respcr.m_end = data->m_req_range.m_end;
respcr.m_length = data->m_contentlen;
char rangestr[1024];
int rangelen = sizeof(rangestr);
bool const crstat = respcr.toStringClosed(rangestr, &rangelen);
// corner case, return 500 ??
if (!crstat) {
data->m_upstream.close();
data->m_dnstream.close();
ERROR_LOG("Bad/invalid response content range");
return HeaderState::Fail;
}
header.setKeyVal(TS_MIME_FIELD_CONTENT_RANGE, TS_MIME_LEN_CONTENT_RANGE, rangestr, rangelen);
} else if (TS_HTTP_STATUS_OK == data->m_statustype) {
header.setStatus(TS_HTTP_STATUS_OK);
static char const *const reason = TSHttpHdrReasonLookup(TS_HTTP_STATUS_OK);
header.setReason(reason, strlen(reason));
header.removeKey(TS_MIME_FIELD_CONTENT_RANGE, TS_MIME_LEN_CONTENT_RANGE);
}
char bufstr[1024];
int const buflen = snprintf(bufstr, sizeof(bufstr), "%" PRId64, bodybytes);
header.setKeyVal(TS_MIME_FIELD_CONTENT_LENGTH, TS_MIME_LEN_CONTENT_LENGTH, bufstr, buflen);
// add the response header length to the total bytes to send
int const hbytes = TSHttpHdrLengthGet(header.m_buffer, header.m_lochdr);
// HEAD request only sends header
if (data->onlyHeader()) {
data->m_bytestosend = hbytes;
data->m_blockexpected = 0;
} else {
// GET request sends header + object
data->m_bytestosend = hbytes + bodybytes;
data->m_blockexpected = blockcr.rangeSize();
}
TSVIONBytesSet(output_vio, data->m_bytestosend);
TSHttpHdrPrint(header.m_buffer, header.m_lochdr, output_buf);
data->m_bytessent = hbytes;
TSVIOReenable(output_vio);
if (data->m_config->m_prefetchcount > 0 && data->m_blocknum == data->m_req_range.firstBlockFor(data->m_config->m_blockbytes) &&
header.hasKey(SLICE_CRR_HEADER.data(), SLICE_CRR_HEADER.size())) {
data->m_prefetchable = true;
}
return HeaderState::Good;
}
void
logSliceError(char const *const message, Data const *const data, HttpHeader const &header_resp)
{
Config *const conf = data->m_config;
bool const logToError = conf->canLogError();
// always write block stitch errors while in debug mode
if (!logToError && !TSIsDebugTagSet(PLUGIN_NAME)) {
return;
}
HttpHeader const header_req(data->m_req_hdrmgr.m_buffer, data->m_req_hdrmgr.m_lochdr);
TSHRTime const timenowus = TShrtime();
int64_t const msecs = timenowus / 1000000;
int64_t const secs = msecs / 1000;
int64_t const ms = msecs % 1000;
// Gather information on the request, must delete urlstr
int urllen = 0;
char *const urlstr = header_req.urlString(&urllen);
char urlpstr[16384];
size_t urlplen = sizeof(urlpstr);
TSStringPercentEncode(urlstr, urllen, urlpstr, urlplen, &urlplen, nullptr);
if (nullptr != urlstr) {
TSfree(urlstr);
}
// uas
char uasstr[8192];
int uaslen = sizeof(uasstr);
header_req.valueForKey(TS_MIME_FIELD_USER_AGENT, TS_MIME_LEN_USER_AGENT, uasstr, &uaslen);
// raw range request
char rangestr[1024];
int rangelen = sizeof(rangestr);
header_req.valueForKey(conf->m_skip_header.data(), conf->m_skip_header.size(), rangestr, &rangelen);
// Normalized range request
ContentRange const crange(data->m_req_range.m_beg, data->m_req_range.m_end, data->m_contentlen);
char normstr[1024];
int normlen = sizeof(normstr);
crange.toStringClosed(normstr, &normlen);
// block range request
int64_t const blockbeg = data->m_blocknum * conf->m_blockbytes;
int64_t const blockend = std::min(blockbeg + conf->m_blockbytes, data->m_contentlen);
// Block response data
TSHttpStatus const statusgot = header_resp.status();
// content range
char crstr[1024];
int crlen = sizeof(crstr);
header_resp.valueForKey(TS_MIME_FIELD_CONTENT_RANGE, TS_MIME_LEN_CONTENT_RANGE, crstr, &crlen);
// etag
char etagstr[1024];
int etaglen = sizeof(etagstr);
header_resp.valueForKey(TS_MIME_FIELD_ETAG, TS_MIME_LEN_ETAG, etagstr, &etaglen);
// last modified
time_t lmgot = 0;
header_resp.timeForKey(TS_MIME_FIELD_LAST_MODIFIED, TS_MIME_LEN_LAST_MODIFIED, &lmgot);
// cc
char ccstr[2048];
int cclen = sizeof(ccstr);
header_resp.valueForKey(TS_MIME_FIELD_CACHE_CONTROL, TS_MIME_LEN_CACHE_CONTROL, ccstr, &cclen);
// via tag
char viastr[8192];
int vialen = sizeof(viastr);
header_resp.valueForKey(TS_MIME_FIELD_VIA, TS_MIME_LEN_VIA, viastr, &vialen);
char etagexpstr[1024];
size_t etagexplen = sizeof(etagexpstr);
TSStringPercentEncode(data->m_etag, data->m_etaglen, etagexpstr, etagexplen, &etagexplen, nullptr);
char etaggotstr[1024];
size_t etaggotlen = sizeof(etaggotstr);
TSStringPercentEncode(etagstr, etaglen, etaggotstr, etaggotlen, &etaggotlen, nullptr);
DEBUG_LOG("Logging Block Stitch error");
ERROR_LOG("%" PRId64 ".%" PRId64 " reason=\"%s\""
" uri=\"%.*s\""
" uas=\"%.*s\""
" req_range=\"%.*s\""
" norm_range=\"%.*s\""
" etag_exp=\"%.*s\""
" lm_exp=\"%.*s\""
" blk_range=\"%" PRId64 "-%" PRId64 "\""
" status_got=\"%d\""
" cr_got=\"%.*s\""
" etag_got=\"%.*s\""
" lm_got=\"%jd\""
" cc=\"%.*s\""
" via=\"%.*s\" - attempting to recover",
secs, ms, message, (int)urlplen, urlpstr, uaslen, uasstr, rangelen, rangestr, normlen, normstr, (int)etagexplen,
etagexpstr, data->m_lastmodifiedlen, data->m_lastmodified, blockbeg, blockend - 1, statusgot, crlen, crstr,
(int)etaggotlen, etaggotstr, static_cast<intmax_t>(lmgot), cclen, ccstr, vialen, viastr);
}
bool
handleNextServerHeader(Data *const data, TSCont const contp)
{
// block response header
HttpHeader header(data->m_resp_hdrmgr.m_buffer, data->m_resp_hdrmgr.m_lochdr);
if (TSIsDebugTagSet(PLUGIN_NAME)) {
DEBUG_LOG("Next Header:\n%s", header.toString().c_str());
}
bool same = true;
switch (header.status()) {
case TS_HTTP_STATUS_NOT_FOUND:
if (data->onlyHeader()) {
return false;
}
// need to reissue reference slice
logSliceError("404 internal block response (asset gone)", data, header);
same = false;
break;
case TS_HTTP_STATUS_PARTIAL_CONTENT:
break;
default:
if (data->onlyHeader() && header.status() == TS_HTTP_STATUS_OK) {
return true;
}
DEBUG_LOG("Non 206/404 internal block response encountered");
return false;
break;
}
// can't parse the content range header, abort -- might be too strict
ContentRange blockcr;
if (same) {
blockcr = contentRangeFrom(header);
if (!blockcr.isValid() || blockcr.m_length != data->m_contentlen) {
logSliceError("Mismatch/Bad block Content-Range", data, header);
same = false;
}
}
if (same) {
// prefer the etag but use Last-Modified if we must.
char etag[8192];
int etaglen = sizeof(etag);
header.valueForKey(TS_MIME_FIELD_ETAG, TS_MIME_LEN_ETAG, etag, &etaglen);
if (0 < data->m_etaglen || 0 < etaglen) {
same = data->m_etaglen == etaglen && 0 == strncmp(etag, data->m_etag, etaglen);
if (!same) {
logSliceError("Mismatch block Etag", data, header);
}
} else {
char lastmodified[33];
int lastmodifiedlen = sizeof(lastmodified);
header.valueForKey(TS_MIME_FIELD_LAST_MODIFIED, TS_MIME_LEN_LAST_MODIFIED, lastmodified, &lastmodifiedlen);
if (0 < data->m_lastmodifiedlen || 0 < lastmodifiedlen) {
same = data->m_lastmodifiedlen == lastmodifiedlen && 0 == strncmp(lastmodified, data->m_lastmodified, lastmodifiedlen);
if (!same) {
logSliceError("Mismatch block Last-Modified", data, header);
}
}
}
}
// Header mismatch
if (same) {
// If we were in reference block refetch mode and the headers
// still match there is a problem
if (BlockState::ActiveRef == data->m_blockstate) {
ERROR_LOG("Reference block refetched, got the same block back again");
return false;
}
} else {
switch (data->m_blockstate) {
case BlockState::Active: {
data->m_upstream.abort();
// Refetch the current interior slice
data->m_blockstate = BlockState::PendingInt;
time_t date = 0;
header.timeForKey(TS_MIME_FIELD_DATE, TS_MIME_LEN_DATE, &date);
// Ask for any slice newer than the cached one
time_t const dateims = date + 1;
DEBUG_LOG("Attempting to reissue interior slice block request with IMS header time: %jd", static_cast<intmax_t>(dateims));
// add special CRR IMS header to the request
HttpHeader headerreq(data->m_req_hdrmgr.m_buffer, data->m_req_hdrmgr.m_lochdr);
Config const *const conf = data->m_config;
if (!headerreq.setKeyTime(conf->m_crr_ims_header.data(), conf->m_crr_ims_header.size(), dateims)) {
ERROR_LOG("Failed setting '%s'", conf->m_crr_ims_header.c_str());
return false;
}
} break;
case BlockState::ActiveInt: {
data->m_upstream.abort();
// New interior slice still mismatches, refetch the reference slice
data->m_blockstate = BlockState::PendingRef;
// convert reference date header to time_t
time_t const date = TSMimeParseDate(data->m_date, data->m_datelen);
// Ask for any slice newer than the cached one
time_t const dateims = date + 1;
DEBUG_LOG("Attempting to reissue reference slice block request with IMS header time: %jd", static_cast<intmax_t>(dateims));
// add special CRR IMS header to the request
HttpHeader headerreq(data->m_req_hdrmgr.m_buffer, data->m_req_hdrmgr.m_lochdr);
Config const *const conf = data->m_config;
if (!headerreq.setKeyTime(conf->m_crr_ims_header.data(), conf->m_crr_ims_header.size(), dateims)) {
ERROR_LOG("Failed setting '%s'", conf->m_crr_ims_header.c_str());
return false;
}
// Reset for first block
if (Config::RefType::First == data->m_config->m_reftype) {
data->m_blocknum = 0;
} else {
data->m_blocknum = data->m_req_range.firstBlockFor(data->m_config->m_blockbytes);
}
return true;
} break;
// Refetch the reference slice
case BlockState::ActiveRef: {
// In this state the reference changed otherwise the asset is toast
// reset the content length (if content length drove the mismatch)
data->m_contentlen = blockcr.m_length;
return true;
} break;
default:
break;
}
}
data->m_blockexpected = blockcr.rangeSize();
if (data->m_config->m_prefetchcount > 0 && data->m_blocknum == data->m_req_range.firstBlockFor(data->m_config->m_blockbytes) &&
header.hasKey(SLICE_CRR_HEADER.data(), SLICE_CRR_HEADER.size())) {
data->m_prefetchable = true;
}
return true;
}
} // namespace
// this is called every time the server has data for us
void
handle_server_resp(TSCont contp, TSEvent event, Data *const data)
{
switch (event) {
case TS_EVENT_VCONN_READ_READY: {
if (data->m_blockstate == BlockState::Passthru) {
transfer_all_bytes(data);
return;
}
// has block response header been parsed??
if (!data->m_server_block_header_parsed) {
int64_t consumed = 0;
TSIOBufferReader const reader = data->m_upstream.m_read.m_reader;
TSVIO const input_vio = data->m_upstream.m_read.m_vio;
TSParseResult const res = data->m_resp_hdrmgr.populateFrom(data->m_http_parser, reader, TSHttpHdrParseResp, &consumed);
TSVIONDoneSet(input_vio, TSVIONDoneGet(input_vio) + consumed);
// the server response header didn't fit into the input buffer.
// wait for more data from upstream
if (TS_PARSE_CONT == res) {
return;
}
bool headerStat = false;
if (TS_PARSE_DONE == res) {
if (!data->m_server_first_header_parsed) {
HeaderState const state = handleFirstServerHeader(data, contp);
data->m_server_first_header_parsed = true;
switch (state) {
case HeaderState::Fail:
data->m_blockstate = BlockState::Fail;
headerStat = false;
break;
case HeaderState::Passthru: {
data->m_blockstate = BlockState::Passthru;
transfer_all_bytes(data);
DEBUG_LOG("Going into a passthru state");
return;
} break;
case HeaderState::Good:
default:
headerStat = true;
break;
}
} else {
headerStat = handleNextServerHeader(data, contp);
}
data->m_server_block_header_parsed = true;
}
// kill the upstream and allow dnstream to clean up
if (!headerStat) {
data->m_upstream.abort();
data->m_blockstate = BlockState::Fail;
if (data->m_dnstream.m_write.isOpen()) {
TSVIOReenable(data->m_dnstream.m_write.m_vio);
} else {
shutdown(contp, data);
}
return;
}
// header may have been successfully parsed but with caveats
switch (data->m_blockstate) {
// request new version of current internal slice
case BlockState::PendingInt:
case BlockState::PendingRef: {
if (!request_block(contp, data)) {
data->m_blockstate = BlockState::Fail;
if (data->m_dnstream.m_write.isOpen()) {
TSVIOReenable(data->m_dnstream.m_write.m_vio);
} else {
shutdown(contp, data);
}
}
return;
} break;
case BlockState::ActiveRef: {
// Mark the reference block for "skip".
int64_t const blockbytes = data->m_config->m_blockbytes;
int64_t const firstblock = data->m_req_range.firstBlockFor(blockbytes);
int64_t const blockpos = firstblock * blockbytes;
int64_t const firstblockbytes = std::min(blockbytes, data->m_contentlen - blockpos);
data->m_blockskip = firstblockbytes;
// Check if we should abort the client
if (data->m_dnstream.isOpen()) {
TSVIO const output_vio = data->m_dnstream.m_write.m_vio;
int64_t const output_done = TSVIONDoneGet(output_vio);
int64_t const output_sent = data->m_bytessent;
if (output_done == output_sent) {
data->m_dnstream.abort();
}
}
} break;
default: {
// how much to normally fast forward into this data block
data->m_blockskip = data->m_req_range.skipBytesForBlock(data->m_config->m_blockbytes, data->m_blocknum);
} break;
}
}
transfer_content_bytes(data);
} break;
case TS_EVENT_VCONN_READ_COMPLETE: {
// fprintf(stderr, "%p: TS_EVENT_VCONN_READ_COMPLETE\n", data);
} break;
case TS_EVENT_VCONN_EOS: {
switch (data->m_blockstate) {
case BlockState::ActiveRef:
case BlockState::Passthru: {
transfer_all_bytes(data);
data->m_upstream.close();
TSVIO const output_vio = data->m_dnstream.m_write.m_vio;
if (nullptr != output_vio) {
TSVIOReenable(output_vio);
} else {
shutdown(contp, data);
}
return;
} break;
default:
break;
}
// corner condition, good source header + 0 length aborted content
// results in no header being read, just an EOS.
// trying to delete the upstream will crash ATS (??)
if (0 == data->m_blockexpected && !data->onlyHeader()) {
shutdown(contp, data); // this will crash if first block
return;
}
transfer_content_bytes(data);
data->m_upstream.close();
data->m_blockstate = BlockState::Pending;
// check for block truncation
if (data->m_blockconsumed < data->m_blockexpected) {
DEBUG_LOG("%p handle_server_resp truncation: %" PRId64 "\n", data, data->m_blockexpected - data->m_blockconsumed);
data->m_blockstate = BlockState::Fail;
// shutdown(contp, data);
return;
}
// prepare for the next request block
++data->m_blocknum;
// when we get a "bytes=-<end>" last N bytes request the plugin
// issues a speculative request for the first block
// in that case fast forward to the real first in range block
// Btw this isn't implemented yet, to be handled
int64_t const firstblock = data->m_req_range.firstBlockFor(data->m_config->m_blockbytes);
if (data->m_blocknum < firstblock) {
data->m_blocknum = firstblock;
}
// continue processing blocks if more requests need to be made
// HEAD requests only has one slice block
if (data->m_req_range.blockIsInside(data->m_config->m_blockbytes, data->m_blocknum) &&
data->m_method_type != TS_HTTP_METHOD_HEAD) {
// Don't immediately request the next slice if the client
// isn't keeping up
bool start_next_block = false;
if (data->m_method_type == TS_HTTP_METHOD_PURGE) {
// for PURGE requests, clients won't request more data (no body content)
start_next_block = true;
} else if (data->m_dnstream.m_write.isOpen()) {
// check throttle condition
TSVIO const output_vio = data->m_dnstream.m_write.m_vio;
int64_t const output_done = TSVIONDoneGet(output_vio);
int64_t const output_sent = data->m_bytessent;
int64_t const threshout = data->m_config->m_blockbytes;
int64_t const buffered = output_sent - output_done;
if (threshout < buffered) {
DEBUG_LOG("%p handle_server_resp: throttling %" PRId64, data, buffered);
} else {
start_next_block = true;
}
}
if (start_next_block) {
if (!request_block(contp, data)) {
data->m_blockstate = BlockState::Fail;
abort(contp, data);
return;
}
}
} else {
data->m_upstream.close();
data->m_blockstate = BlockState::Done;
if (!data->m_dnstream.m_write.isOpen()) {
shutdown(contp, data);
}
}
} break;
default: {
DEBUG_LOG("%p handle_server_resp uhandled event: %s", data, TSHttpEventNameLookup(event));
} break;
}
}
|