Skip to content

Commit

Permalink
Fix bug in StreamTransformer - ending fragment not getting sent (#2317)
Browse files Browse the repository at this point in the history
* Fix bug in StreamTransformer - ending fragment not getting sent

* Add test to verify fix

Before:

```
> ChunkedStream / StreamTransformer
758 OK: output.moveString(s)
OUTPUT: 65 0d 0a 53 6f 6d 65 20 74 65 73 74 20 64 61 74  e..Some test dat
        61 0d 0a                                         a..
792 FAIL: FS_OUTPUT == s
FAIL in `virtual void StreamTest::execute()`
```

After:

```
>> ChunkedStream / StreamTransformer
741 OK: output.moveString(s)
OUTPUT: 65 0d 0a 53 6f 6d 65 20 74 65 73 74 20 64 61 74  e..Some test dat
        61 0d 0a 30 0d 0a 0d 0a                          a..0....
760 OK: FS_OUTPUT == s
```
  • Loading branch information
mikee47 authored Apr 26, 2021
1 parent 40e22c3 commit 639c06b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sming/Core/Data/Buffer/CircularBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ size_t CircularBuffer::room() const

size_t CircularBuffer::write(uint8_t charToWrite)
{
if(!room()) {
if(room() == 0) {
return 0;
}

Expand Down
6 changes: 4 additions & 2 deletions Sming/Core/Data/StreamTransformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void StreamTransformer::fillTempStream(char* buffer, size_t bufSize)
while((room = tempStream->room()) >= maxChunkSize) {
auto chunkSize = sourceStream->readMemoryBlock(buffer, maxChunkSize);
if(chunkSize == 0) {
return;
break;
}

saveState();
Expand All @@ -55,7 +55,9 @@ void StreamTransformer::fillTempStream(char* buffer, size_t bufSize)

if(sourceStream->isFinished()) {
auto outLength = transform(nullptr, 0, result, resultSize);
tempStream->write(result, outLength);
auto written = tempStream->write(result, outLength);
(void)written;
assert(written == outLength);
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/HostTests/modules/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <FlashString/TemplateStream.hpp>
#include <Data/Stream/MemoryDataStream.h>
#include <Data/Stream/Base64OutputStream.h>
#include <Data/Stream/ChunkedStream.h>
#include <Network/WebHelpers/base64.h>

DEFINE_FSTR_LOCAL(template1, "Stream containing {var1}, {var2} and {var3}. {} {{}} {{12345")
Expand Down Expand Up @@ -86,6 +87,19 @@ class StreamTest : public TestGroup
REQUIRE(Resource::image_png == s);
}

TEST_CASE("ChunkedStream / StreamTransformer")
{
DEFINE_FSTR_LOCAL(FS_INPUT, "Some test data");
DEFINE_FSTR_LOCAL(FS_OUTPUT, "e\r\nSome test data\r\n0\r\n\r\n");
ChunkedStream chunked(new FlashMemoryStream(FS_INPUT));
MemoryDataStream output;
output.copyFrom(&chunked);
String s;
REQUIRE(output.moveString(s));
m_printHex("OUTPUT", s.c_str(), s.length());
REQUIRE(FS_OUTPUT == s);
}

TEST_CASE("MultipartStream / MultiStream")
{
unsigned itemIndex{0};
Expand Down

0 comments on commit 639c06b

Please sign in to comment.