Path: blob/master/Utilities/cmcppdap/src/content_stream.cpp
3153 views
// Copyright 2019 Google LLC1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// https://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#include "content_stream.h"1516#include "dap/io.h"1718#include <string.h> // strlen19#include <algorithm> // std::min2021namespace dap {2223////////////////////////////////////////////////////////////////////////////////24// ContentReader25////////////////////////////////////////////////////////////////////////////////26ContentReader::ContentReader(27const std::shared_ptr<Reader>& reader,28OnInvalidData on_invalid_data /* = OnInvalidData::kIgnore */)29: reader(reader), on_invalid_data(on_invalid_data) {}3031ContentReader& ContentReader::operator=(ContentReader&& rhs) noexcept {32buf = std::move(rhs.buf);33reader = std::move(rhs.reader);34on_invalid_data = std::move(rhs.on_invalid_data);35return *this;36}3738bool ContentReader::isOpen() {39return reader ? reader->isOpen() : false;40}4142void ContentReader::close() {43if (reader) {44reader->close();45}46}4748std::string ContentReader::read() {49// Find Content-Length header prefix50if (on_invalid_data == kClose) {51if (!match("Content-Length:")) {52return badHeader();53}54} else {55if (!scan("Content-Length:")) {56return "";57}58}59// Skip whitespace and tabs60while (matchAny(" \t")) {61}62// Parse length63size_t len = 0;64while (true) {65auto c = matchAny("0123456789");66if (c == 0) {67break;68}69len *= 10;70len += size_t(c) - size_t('0');71}72if (len == 0) {73return "";74}7576// Expect \r\n\r\n77if (!match("\r\n\r\n")) {78return badHeader();79}8081// Read message82if (!buffer(len)) {83return "";84}85std::string out;86out.reserve(len);87for (size_t i = 0; i < len; i++) {88out.push_back(static_cast<char>(buf.front()));89buf.pop_front();90}91return out;92}9394bool ContentReader::scan(const uint8_t* seq, size_t len) {95while (buffer(len)) {96if (match(seq, len)) {97return true;98}99buf.pop_front();100}101return false;102}103104bool ContentReader::scan(const char* str) {105auto len = strlen(str);106return scan(reinterpret_cast<const uint8_t*>(str), len);107}108109bool ContentReader::match(const uint8_t* seq, size_t len) {110if (!buffer(len)) {111return false;112}113auto it = buf.begin();114for (size_t i = 0; i < len; i++, it++) {115if (*it != seq[i]) {116return false;117}118}119for (size_t i = 0; i < len; i++) {120buf.pop_front();121}122return true;123}124125bool ContentReader::match(const char* str) {126auto len = strlen(str);127return match(reinterpret_cast<const uint8_t*>(str), len);128}129130char ContentReader::matchAny(const char* chars) {131if (!buffer(1)) {132return false;133}134int c = buf.front();135if (auto p = strchr(chars, c)) {136buf.pop_front();137return *p;138}139return 0;140}141142bool ContentReader::buffer(size_t bytes) {143if (bytes < buf.size()) {144return true;145}146bytes -= buf.size();147while (bytes > 0) {148uint8_t chunk[256];149auto numWant = std::min(sizeof(chunk), bytes);150auto numGot = reader->read(chunk, numWant);151if (numGot == 0) {152return false;153}154for (size_t i = 0; i < numGot; i++) {155buf.push_back(chunk[i]);156}157bytes -= numGot;158}159return true;160}161162std::string ContentReader::badHeader() {163if (on_invalid_data == kClose) {164close();165}166return "";167}168169////////////////////////////////////////////////////////////////////////////////170// ContentWriter171////////////////////////////////////////////////////////////////////////////////172ContentWriter::ContentWriter(const std::shared_ptr<Writer>& rhs)173: writer(rhs) {}174175ContentWriter& ContentWriter::operator=(ContentWriter&& rhs) noexcept {176writer = std::move(rhs.writer);177return *this;178}179180bool ContentWriter::isOpen() {181return writer ? writer->isOpen() : false;182}183184void ContentWriter::close() {185if (writer) {186writer->close();187}188}189190bool ContentWriter::write(const std::string& msg) const {191auto header =192std::string("Content-Length: ") + std::to_string(msg.size()) + "\r\n\r\n";193return writer->write(header.data(), header.size()) &&194writer->write(msg.data(), msg.size());195}196197} // namespace dap198199200