|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include "proxygen/lib/http/coro/HTTPBodyEventQueue.h" |
| 10 | + |
| 11 | +#include <folly/Conv.h> |
| 12 | + |
| 13 | +using folly::coro::co_error; |
| 14 | +using folly::coro::co_nothrow; |
| 15 | +using folly::coro::co_safe_point; |
| 16 | + |
| 17 | +namespace proxygen::coro { |
| 18 | + |
| 19 | +folly::coro::Task<HTTPHeaderEvent> HTTPBodyEventQueue::readHeaderEvent() { |
| 20 | + auto res = co_await co_nothrow(source_.readHeaderEvent()); |
| 21 | + const auto& ct = co_await folly::coro::co_current_cancellation_token; |
| 22 | + if (ct.isCancellationRequested()) { |
| 23 | + co_yield co_error(HTTPError{HTTPErrorCode::CORO_CANCELLED}); |
| 24 | + } |
| 25 | + |
| 26 | + if (res.headers->isResponse() && res.headers->getStatusCode() == 304) { |
| 27 | + skipContentLengthValidation(); |
| 28 | + } else if (shouldValidateContentLength_ && res.isFinal()) { |
| 29 | + setExpectedEgressContentLength( |
| 30 | + res.headers->getHeaders().getSingleOrEmpty(HTTP_HEADER_CONTENT_LENGTH), |
| 31 | + res.eom); |
| 32 | + } |
| 33 | + |
| 34 | + co_return res; |
| 35 | +} |
| 36 | + |
| 37 | +folly::coro::Task<HTTPBodyEventQueue::ReadBodyResult> |
| 38 | +HTTPBodyEventQueue::readBodyEvent(uint32_t max) { |
| 39 | + XCHECK(source_); |
| 40 | + XLOG(DBG4) << "Waiting for buffer space"; |
| 41 | + auto bufferSpace = availableBuffer(); |
| 42 | + if (!bufferSpace) { |
| 43 | + bufferSpace = co_await co_nothrow(waitAvailableBuffer()); |
| 44 | + } |
| 45 | + XLOG(DBG4) << "Got buffer space len=" << bufferSpace |
| 46 | + << " waiting for body event"; |
| 47 | + max = std::min(max, uint32_t(bufferSpace)); |
| 48 | + auto bodyEvent = co_await co_nothrow(source_.readBodyEvent(max)); |
| 49 | + const auto& ct = co_await folly::coro::co_current_cancellation_token; |
| 50 | + if (ct.isCancellationRequested()) { |
| 51 | + co_yield co_error(HTTPError{HTTPErrorCode::CORO_CANCELLED}); |
| 52 | + } |
| 53 | + |
| 54 | + if (bodyEvent.eventType == HTTPBodyEvent::SUSPEND) { |
| 55 | + co_return ReadBodyResult({.resume = std::move(bodyEvent.event.resume)}); |
| 56 | + } |
| 57 | + if (bodyEvent.eventType == HTTPBodyEvent::BODY && |
| 58 | + !bodyEvent.event.body.empty()) { |
| 59 | + callback_.onEgressBytesBuffered( |
| 60 | + static_cast<int64_t>(bodyEvent.event.body.chainLength())); |
| 61 | + } |
| 62 | + co_return ReadBodyResult({ |
| 63 | + .resume = folly::none, |
| 64 | + .eom = processBodyEvent(std::move(bodyEvent)), |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +void HTTPBodyEventQueue::setExpectedEgressContentLength( |
| 69 | + const std::string& contentLen, bool eom) { |
| 70 | + if (contentLen.empty()) { |
| 71 | + shouldValidateContentLength_ = false; |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + auto convResult = folly::tryTo<uint64_t>(contentLen); |
| 76 | + if (convResult.hasError()) { |
| 77 | + XLOG(ERR) |
| 78 | + << "Invalid content-length: " << contentLen << ", ex=" |
| 79 | + << folly::makeConversionError(convResult.error(), contentLen).what(); |
| 80 | + shouldValidateContentLength_ = false; |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + expectedContentLength_ = convResult.value(); |
| 85 | + validateContentLength(eom); |
| 86 | +} |
| 87 | + |
| 88 | +void HTTPBodyEventQueue::clear(const HTTPError& err) { |
| 89 | + size_t bytesBuffered{0}; |
| 90 | + for (auto& ev : bodyQueue_) { |
| 91 | + for (auto& reg : ev.byteEventRegistrations) { |
| 92 | + reg.cancel(err, folly::none); |
| 93 | + } |
| 94 | + if (ev.eventType == HTTPBodyEvent::EventType::BODY) { |
| 95 | + bytesBuffered += ev.event.body.chainLength(); |
| 96 | + } |
| 97 | + } |
| 98 | + bodyQueue_.clear(); |
| 99 | + callback_.onEgressBytesBuffered(-static_cast<int64_t>(bytesBuffered)); |
| 100 | +} |
| 101 | + |
| 102 | +HTTPBodyEvent HTTPBodyEventQueue::dequeueBodyEvent(uint32_t max) { |
| 103 | + XCHECK(!bodyQueue_.empty()); |
| 104 | + auto bodyEvent = std::move(bodyQueue_.front()); |
| 105 | + bodyQueue_.pop_front(); |
| 106 | + if (bodyEvent.eventType == HTTPBodyEvent::BODY) { |
| 107 | + auto length = bodyEvent.event.body.chainLength(); |
| 108 | + auto toReturn = std::min(max, uint32_t(length)); |
| 109 | + XCHECK_GE(bufferedBodyBytes_, toReturn); |
| 110 | + bool wasOverLimit = bufferedBodyBytes_ >= limit_; |
| 111 | + bufferedBodyBytes_ -= toReturn; |
| 112 | + if (wasOverLimit && bufferedBodyBytes_ < limit_) { |
| 113 | + event_.signal(); |
| 114 | + } |
| 115 | + if (toReturn < length) { |
| 116 | + // only return part of it, split the buffer and re-push it to the front |
| 117 | + auto resultBuf = bodyEvent.event.body.splitAtMost(toReturn); |
| 118 | + bodyQueue_.emplace_front(std::move(bodyEvent)); |
| 119 | + return HTTPBodyEvent(std::move(resultBuf), false); |
| 120 | + } |
| 121 | + } |
| 122 | + return bodyEvent; |
| 123 | +} |
| 124 | + |
| 125 | +folly::coro::Task<size_t> HTTPBodyEventQueue::waitAvailableBuffer() { |
| 126 | + while (bufferedBodyBytes_ >= limit_) { |
| 127 | + event_.reset(); |
| 128 | + auto status = co_await event_.wait(); |
| 129 | + if (status == TimedBaton::Status::timedout) { |
| 130 | + co_yield folly::coro::co_error(HTTPError( |
| 131 | + HTTPErrorCode::READ_TIMEOUT, "timed out waiting for buffer space")); |
| 132 | + } else if (status == TimedBaton::Status::cancelled) { |
| 133 | + co_yield folly::coro::co_error(HTTPError( |
| 134 | + HTTPErrorCode::CORO_CANCELLED, "cancelled waiting for buffer space")); |
| 135 | + } |
| 136 | + } |
| 137 | + co_return limit_ - bufferedBodyBytes_; |
| 138 | +} |
| 139 | + |
| 140 | +bool HTTPBodyEventQueue::processBodyEvent(HTTPBodyEvent&& bodyEvent) { |
| 141 | + XLOG(DBG4) << "Queuing body event"; |
| 142 | + for (auto& reg : bodyEvent.byteEventRegistrations) { |
| 143 | + if (!reg.streamID) { |
| 144 | + reg.streamID = id_; |
| 145 | + } |
| 146 | + } |
| 147 | + bool eom = bodyEvent.eom; |
| 148 | + size_t addedBodyLength = 0; |
| 149 | + if (bodyEvent.eventType == HTTPBodyEvent::BODY) { |
| 150 | + addedBodyLength = bodyEvent.event.body.chainLength(); |
| 151 | + bufferedBodyBytes_ += addedBodyLength; |
| 152 | + observedBodyLength_ += addedBodyLength; |
| 153 | + if (!bodyQueue_.empty() && |
| 154 | + bodyQueue_.back().eventType == HTTPBodyEvent::BODY && |
| 155 | + bodyQueue_.back().byteEventRegistrations.empty()) { |
| 156 | + // Coalesce this bodyEvent into the last one in queue |
| 157 | + auto& back = bodyQueue_.back(); |
| 158 | + back.event.body.append(bodyEvent.event.body.move()); |
| 159 | + back.eom = eom; |
| 160 | + back.byteEventRegistrations = std::move(bodyEvent.byteEventRegistrations); |
| 161 | + } else { |
| 162 | + bodyQueue_.emplace_back(std::move(bodyEvent)); |
| 163 | + } |
| 164 | + } else { |
| 165 | + bodyQueue_.emplace_back(std::move(bodyEvent)); |
| 166 | + } |
| 167 | + validateContentLength(eom); |
| 168 | + return eom; |
| 169 | +} |
| 170 | + |
| 171 | +} // namespace proxygen::coro |
0 commit comments