Revert "Fix content reader for partial input"

This reverts commit 77209caf044d58c7d87770b377027a65fe696466.
This commit is contained in:
Dan McNulty 2023-07-05 10:33:22 -05:00 committed by Ben Clayton
parent 60fee6e505
commit 8399fbae7f
3 changed files with 11 additions and 37 deletions

View File

@ -44,17 +44,13 @@ void ContentReader::close() {
}
std::string ContentReader::read() {
matched_idx = 0;
// Find Content-Length header prefix
if (!scan("Content-Length:")) {
return "";
}
// Skip whitespace and tabs
while (matchAny(" \t")) {
}
// Parse length
size_t len = 0;
while (true) {
@ -72,16 +68,10 @@ std::string ContentReader::read() {
if (!match("\r\n\r\n")) {
return "";
}
// Read message
if (!buffer(len + matched_idx)) {
if (!buffer(len)) {
return "";
}
for (size_t i = 0; i < matched_idx; i++) {
buf.pop_front();
}
std::string out;
out.reserve(len);
for (size_t i = 0; i < len; i++) {
@ -107,17 +97,18 @@ bool ContentReader::scan(const char* str) {
}
bool ContentReader::match(const uint8_t* seq, size_t len) {
if (!buffer(len + matched_idx)) {
if (!buffer(len)) {
return false;
}
auto it = matched_idx;
auto it = buf.begin();
for (size_t i = 0; i < len; i++, it++) {
if (buf[it] != seq[i]) {
if (*it != seq[i]) {
return false;
}
}
matched_idx += len;
for (size_t i = 0; i < len; i++) {
buf.pop_front();
}
return true;
}
@ -127,12 +118,12 @@ bool ContentReader::match(const char* str) {
}
char ContentReader::matchAny(const char* chars) {
if (!buffer(1 + matched_idx)) {
if (!buffer(1)) {
return false;
}
int c = buf[matched_idx];
int c = buf.front();
if (auto p = strchr(chars, c)) {
matched_idx++;
buf.pop_front();
return *p;
}
return 0;
@ -186,4 +177,4 @@ bool ContentWriter::write(const std::string& msg) const {
writer->write(msg.data(), msg.size());
}
} // namespace dap
} // namespace dap

View File

@ -47,7 +47,6 @@ class ContentReader {
std::shared_ptr<Reader> reader;
std::deque<uint8_t> buf;
uint32_t matched_idx = 0;
};
class ContentWriter {

View File

@ -81,19 +81,3 @@ TEST(ContentStreamTest, ShortRead) {
ASSERT_EQ(cs.read(), "Content payload number three");
ASSERT_EQ(cs.read(), "");
}
TEST(ContentStreamTest, PartialReadAndParse) {
auto sb = std::make_shared<dap::StringBuffer>();
dap::ContentReader cs(sb);
sb->write("Content");
ASSERT_EQ(cs.read(), "");
sb->write("-Length: ");
ASSERT_EQ(cs.read(), "");
sb->write("26");
ASSERT_EQ(cs.read(), "");
sb->write("\r\n\r\n");
ASSERT_EQ(cs.read(), "");
sb->write("Content payload number one");
ASSERT_EQ(cs.read(), "Content payload number one");
ASSERT_EQ(cs.read(), "");
}