From d8247896ae90d4e98e23019fe8ea705cfc48fb0a Mon Sep 17 00:00:00 2001 From: mikee47 Date: Thu, 18 Oct 2018 21:40:14 +0100 Subject: [PATCH 1/2] Rename TemplateFileStream files to TemplateStream --- .../Data/Stream/{TemplateFileStream.cpp => TemplateStream.cpp} | 0 .../Data/Stream/{TemplateFileStream.h => TemplateStream.h} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename Sming/SmingCore/Data/Stream/{TemplateFileStream.cpp => TemplateStream.cpp} (100%) rename Sming/SmingCore/Data/Stream/{TemplateFileStream.h => TemplateStream.h} (100%) diff --git a/Sming/SmingCore/Data/Stream/TemplateFileStream.cpp b/Sming/SmingCore/Data/Stream/TemplateStream.cpp similarity index 100% rename from Sming/SmingCore/Data/Stream/TemplateFileStream.cpp rename to Sming/SmingCore/Data/Stream/TemplateStream.cpp diff --git a/Sming/SmingCore/Data/Stream/TemplateFileStream.h b/Sming/SmingCore/Data/Stream/TemplateStream.h similarity index 100% rename from Sming/SmingCore/Data/Stream/TemplateFileStream.h rename to Sming/SmingCore/Data/Stream/TemplateStream.h From ebc8e7672686b2bdbc51a344731081e8901296ae Mon Sep 17 00:00:00 2001 From: mikee47 Date: Mon, 22 Oct 2018 22:59:12 +0100 Subject: [PATCH 2/2] Refactor TemplateFileStream class using TemplateStream DataSourceStream * [BC] Amend StreamType enumeration eSST_TemplateFile -> eSST_Template * [BC] Make getStreamType() method const * [BC] make id() method const * Add virtual getName() method to provide generic way of getting filename from TemplateStream wrapper * Add virtual isValid() method TemplateFileStream * Refactor as TemplateStream class to allow any other stream type to provide source template data * Replace int with unsigned or size_t as appropriate * Minor code improvements HttpRequest * setFile() method can use any ReadWriteStream-derived class, doesn't have to be FileStream. --- Sming/Libraries/ArduCAM/ArduCAMStream.h | 2 +- Sming/SmingCore/Data/CircularBuffer.h | 4 +- Sming/SmingCore/Data/MailMessage.cpp | 2 +- .../SmingCore/Data/Stream/DataSourceStream.h | 39 +++-- .../Data/Stream/EndlessMemoryStream.h | 2 +- Sming/SmingCore/Data/Stream/FileStream.cpp | 12 +- Sming/SmingCore/Data/Stream/FileStream.h | 17 ++- .../SmingCore/Data/Stream/JsonObjectStream.h | 2 +- .../Data/Stream/LimitedMemoryStream.cpp | 2 +- .../Data/Stream/LimitedMemoryStream.h | 2 +- .../SmingCore/Data/Stream/MemoryDataStream.h | 2 +- Sming/SmingCore/Data/Stream/MultipartStream.h | 2 +- .../Data/Stream/TemplateFileStream.h | 34 +++++ .../SmingCore/Data/Stream/TemplateStream.cpp | 138 +++++++++--------- Sming/SmingCore/Data/Stream/TemplateStream.h | 71 ++++++--- .../Data/Stream/UrlencodedOutputStream.h | 2 +- Sming/SmingCore/Data/StreamTransformer.h | 2 +- .../SmingCore/Network/Http/HttpConnection.cpp | 6 +- Sming/SmingCore/Network/Http/HttpRequest.h | 6 +- Sming/SmingCore/Network/Http/HttpResponse.cpp | 7 +- Sming/SmingCore/Network/Http/HttpResponse.h | 6 +- Sming/SmingCore/Network/HttpClient.cpp | 2 +- Sming/SmingCore/Network/rBootHttpUpdate.h | 2 +- 23 files changed, 231 insertions(+), 133 deletions(-) create mode 100644 Sming/SmingCore/Data/Stream/TemplateFileStream.h diff --git a/Sming/Libraries/ArduCAM/ArduCAMStream.h b/Sming/Libraries/ArduCAM/ArduCAMStream.h index 89c74cbc2c..8a7ceee1d1 100644 --- a/Sming/Libraries/ArduCAM/ArduCAMStream.h +++ b/Sming/Libraries/ArduCAM/ArduCAMStream.h @@ -18,7 +18,7 @@ class ArduCAMStream: public ReadWriteStream { ArduCAMStream(ArduCAM *cam); virtual ~ArduCAMStream(); - virtual StreamType getStreamType() { return eSST_User; } + virtual StreamType getStreamType() const { return eSST_User; } virtual uint16_t readMemoryBlock(char* data, int bufSize); virtual bool seek(int len); diff --git a/Sming/SmingCore/Data/CircularBuffer.h b/Sming/SmingCore/Data/CircularBuffer.h index 332ade33ed..c473a87566 100644 --- a/Sming/SmingCore/Data/CircularBuffer.h +++ b/Sming/SmingCore/Data/CircularBuffer.h @@ -40,7 +40,7 @@ class CircularBuffer : public ReadWriteStream * @retval StreamType The stream type. * @todo Return value of IDataSourceStream:getStreamType base class function should be of type StreamType, e.g. eSST_User */ - virtual StreamType getStreamType() + virtual StreamType getStreamType() const { return StreamType::eSST_Memory; } @@ -77,7 +77,7 @@ class CircularBuffer : public ReadWriteStream * @brief Returns unique id of the resource. * @retval String the unique id of the stream. */ - virtual String id() + virtual String id() const { return String(reinterpret_cast(&buffer), HEX); } diff --git a/Sming/SmingCore/Data/MailMessage.cpp b/Sming/SmingCore/Data/MailMessage.cpp index 8f6d7fa668..8b85515931 100644 --- a/Sming/SmingCore/Data/MailMessage.cpp +++ b/Sming/SmingCore/Data/MailMessage.cpp @@ -65,7 +65,7 @@ MailMessage& MailMessage::addAttachment(FileStream* stream) return *this; } - String filename = stream->fileName(); + String filename = stream->getName(); String mime = ContentType::fromFullFileName(filename); return addAttachment(stream, mime, filename); diff --git a/Sming/SmingCore/Data/Stream/DataSourceStream.h b/Sming/SmingCore/Data/Stream/DataSourceStream.h index b5ebe89b53..ff0ca1194b 100644 --- a/Sming/SmingCore/Data/Stream/DataSourceStream.h +++ b/Sming/SmingCore/Data/Stream/DataSourceStream.h @@ -17,12 +17,13 @@ * @{ */ enum StreamType { - eSST_Memory, ///< Memory data stream - eSST_File, ///< File data stream - eSST_TemplateFile, ///< Template file data stream - eSST_JsonObject, ///< JSON object data stream - eSST_User, ///< User defined data stream - eSST_Unknown ///< Unknown data stream type + eSST_Invalid, ///< Stream content not valid + eSST_Memory, ///< Memory data stream + eSST_File, ///< File data stream + eSST_Template, ///< Template data stream + eSST_JsonObject, ///< JSON object data stream + eSST_User, ///< User defined data stream + eSST_Unknown ///< Unknown data stream type }; /** @} */ @@ -43,7 +44,17 @@ class IDataSourceStream : public Stream * @retval StreamType The stream type. * @todo Return value of IDataSourceStream:getStreamType base class function should be of type StreamType, e.g. eSST_User */ - virtual StreamType getStreamType() = 0; + virtual StreamType getStreamType() const = 0; + + /** @brief Determine if the stream object contains valid data + * @retval bool true if valid, false if invalid + * @note Where inherited classes are initialised by constructor this method indicates + * whether that was successful or not (e.g. FileStream) + */ + virtual bool isValid() const + { + return getStreamType() != eSST_Invalid; + } /** @brief Read a block of memory * @param data Pointer to the data to be read @@ -61,7 +72,7 @@ class IDataSourceStream : public Stream /** * @brief Read a character without advancing the stream pointer - * @retval The character that was read or -1 if none is available + * @retval int The character that was read or -1 if none is available */ virtual int peek(); @@ -116,7 +127,17 @@ class IDataSourceStream : public Stream * @brief Returns unique id of the resource. * @retval String the unique id of the stream. */ - virtual String id() + virtual String id() const + { + return nullptr; + } + + /** + * @brief Returns name of the resource. + * @retval String + * @note Commonly used to obtain name of file + */ + virtual String getName() const { return nullptr; } diff --git a/Sming/SmingCore/Data/Stream/EndlessMemoryStream.h b/Sming/SmingCore/Data/Stream/EndlessMemoryStream.h index eaf7166d3f..e21f04bb02 100644 --- a/Sming/SmingCore/Data/Stream/EndlessMemoryStream.h +++ b/Sming/SmingCore/Data/Stream/EndlessMemoryStream.h @@ -30,7 +30,7 @@ class EndlessMemoryStream : public ReadWriteStream delete stream; } - virtual StreamType getStreamType() + virtual StreamType getStreamType() const { return eSST_Memory; } diff --git a/Sming/SmingCore/Data/Stream/FileStream.cpp b/Sming/SmingCore/Data/Stream/FileStream.cpp index 4f7d90c7cb..55aa2fdf6f 100644 --- a/Sming/SmingCore/Data/Stream/FileStream.cpp +++ b/Sming/SmingCore/Data/Stream/FileStream.cpp @@ -91,19 +91,21 @@ bool FileStream::isFinished() return fileIsEOF(handle); } -String FileStream::fileName() +String FileStream::fileName() const { spiffs_stat stat; - fileStats(handle, &stat); - return String((char*)stat.name); + if(fileStats(handle, &stat) < 0) + return nullptr; + else + return String((char*)stat.name); } -bool FileStream::fileExist() +bool FileStream::fileExist() const { return size != -1; } -String FileStream::id() +String FileStream::id() const { spiffs_stat stat; fileStats(handle, &stat); diff --git a/Sming/SmingCore/Data/Stream/FileStream.h b/Sming/SmingCore/Data/Stream/FileStream.h index abaa8c9dfb..febbe575c5 100644 --- a/Sming/SmingCore/Data/Stream/FileStream.h +++ b/Sming/SmingCore/Data/Stream/FileStream.h @@ -30,7 +30,7 @@ class FileStream : public ReadWriteStream virtual bool attach(const String& fileName, FileOpenFlags openFlags); //Use base class documentation - virtual StreamType getStreamType() + virtual StreamType getStreamType() const { return eSST_File; } @@ -47,8 +47,17 @@ class FileStream : public ReadWriteStream //Use base class documentation virtual bool isFinished(); - String fileName(); ///< Filename of file stream is attached to - bool fileExist(); ///< True if file exists + String fileName() const; ///< Filename of file stream is attached to + bool fileExist() const; ///< True if file exists + virtual String getName() const + { + return fileName(); + } + + virtual bool isValid() const + { + return fileExist(); + } /** @brief Get the offset of cursor from beginning of data * @retval int Cursor offset @@ -67,7 +76,7 @@ class FileStream : public ReadWriteStream return size; } - virtual String id(); + virtual String id() const; private: file_t handle; diff --git a/Sming/SmingCore/Data/Stream/JsonObjectStream.h b/Sming/SmingCore/Data/Stream/JsonObjectStream.h index 84b6d97d45..bec4dde01d 100644 --- a/Sming/SmingCore/Data/Stream/JsonObjectStream.h +++ b/Sming/SmingCore/Data/Stream/JsonObjectStream.h @@ -31,7 +31,7 @@ class JsonObjectStream : public MemoryDataStream } //Use base class documentation - virtual StreamType getStreamType() + virtual StreamType getStreamType() const { return eSST_JsonObject; } diff --git a/Sming/SmingCore/Data/Stream/LimitedMemoryStream.cpp b/Sming/SmingCore/Data/Stream/LimitedMemoryStream.cpp index fa6324c1db..9c33ce5540 100644 --- a/Sming/SmingCore/Data/Stream/LimitedMemoryStream.cpp +++ b/Sming/SmingCore/Data/Stream/LimitedMemoryStream.cpp @@ -18,7 +18,7 @@ LimitedMemoryStream::~LimitedMemoryStream() delete[] buffer; } -StreamType LimitedMemoryStream::getStreamType() +StreamType LimitedMemoryStream::getStreamType() const { return eSST_Memory; } diff --git a/Sming/SmingCore/Data/Stream/LimitedMemoryStream.h b/Sming/SmingCore/Data/Stream/LimitedMemoryStream.h index 89fa0350b2..e3c8718898 100644 --- a/Sming/SmingCore/Data/Stream/LimitedMemoryStream.h +++ b/Sming/SmingCore/Data/Stream/LimitedMemoryStream.h @@ -25,7 +25,7 @@ class LimitedMemoryStream : public ReadWriteStream virtual ~LimitedMemoryStream(); //Use base class documentation - virtual StreamType getStreamType(); + virtual StreamType getStreamType() const; /** * @brief Return the total length of the stream diff --git a/Sming/SmingCore/Data/Stream/MemoryDataStream.h b/Sming/SmingCore/Data/Stream/MemoryDataStream.h index 0f648c853c..8932e662c4 100644 --- a/Sming/SmingCore/Data/Stream/MemoryDataStream.h +++ b/Sming/SmingCore/Data/Stream/MemoryDataStream.h @@ -38,7 +38,7 @@ class MemoryDataStream : public ReadWriteStream } //Use base class documentation - virtual StreamType getStreamType() + virtual StreamType getStreamType() const { return eSST_Memory; } diff --git a/Sming/SmingCore/Data/Stream/MultipartStream.h b/Sming/SmingCore/Data/Stream/MultipartStream.h index 792d8cb697..8ec71ea148 100644 --- a/Sming/SmingCore/Data/Stream/MultipartStream.h +++ b/Sming/SmingCore/Data/Stream/MultipartStream.h @@ -36,7 +36,7 @@ class MultipartStream : public ReadWriteStream virtual ~MultipartStream(); //Use base class documentation - virtual StreamType getStreamType() + virtual StreamType getStreamType() const { // TODO: fix this... return stream->getStreamType(); diff --git a/Sming/SmingCore/Data/Stream/TemplateFileStream.h b/Sming/SmingCore/Data/Stream/TemplateFileStream.h new file mode 100644 index 0000000000..5ed63e2c87 --- /dev/null +++ b/Sming/SmingCore/Data/Stream/TemplateFileStream.h @@ -0,0 +1,34 @@ +/**** + * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. + * Created 2015 by Skurydin Alexey + * http://github.com/anakod/Sming + * All files of the Sming Core are provided under the LGPL v3 license. + ****/ + +#ifndef _SMING_CORE_DATA_TEMPLATE_FILE_STREAM_H_ +#define _SMING_CORE_DATA_TEMPLATE_FILE_STREAM_H_ + +#include "FileStream.h" +#include "TemplateStream.h" + +/** + * @brief Template File stream class + * @ingroup stream data + * + * @{ + */ + +class TemplateFileStream : public TemplateStream +{ +public: + /** @brief Create a template file stream + * @param templateFileName Template filename + */ + TemplateFileStream(const String& fileName) : TemplateStream(new FileStream(fileName)) + { + } +}; + +/** @} */ + +#endif /* _SMING_CORE_DATA_TEMPLATE_FILE_STREAM_H_ */ diff --git a/Sming/SmingCore/Data/Stream/TemplateStream.cpp b/Sming/SmingCore/Data/Stream/TemplateStream.cpp index 8949c3a094..06076b0eef 100644 --- a/Sming/SmingCore/Data/Stream/TemplateStream.cpp +++ b/Sming/SmingCore/Data/Stream/TemplateStream.cpp @@ -5,60 +5,57 @@ * All files of the Sming Core are provided under the LGPL v3 license. ****/ -#include "TemplateFileStream.h" +#include "TemplateStream.h" -TemplateFileStream::TemplateFileStream(const String& templateFileName) : FileStream(templateFileName) +uint16_t TemplateStream::readMemoryBlock(char* data, int bufSize) { - state = eTES_Wait; -} - -TemplateFileStream::~TemplateFileStream() -{ -} + debug_d("TemplateStream::read(%d), state = %d", bufSize, state); -uint16_t TemplateFileStream::readMemoryBlock(char* data, int bufSize) -{ - debug_d("READ Template (%d)", state); - int available; + if(!data || bufSize <= 0) + return 0; if(state == eTES_StartVar) { - if(templateData.contains(varName)) { - // Return variable value - debug_d("StartVar %s", varName.c_str()); - available = templateData[varName].length(); - memcpy(data, (char*)templateData[varName].c_str(), available); - seek(skipBlockSize); - varDataPos = 0; - state = eTES_SendingVar; - return available; - } else { - debug_d("var %s not found", varName.c_str()); + int i = templateData.indexOf(varName); + debug_d("StartVar '%s' %sfound", varName.c_str(), i < 0 ? "NOT " : ""); + if(i < 0) { state = eTES_Wait; - int len = FileStream::readMemoryBlock(data, bufSize); - return std::min(len, skipBlockSize); + return stream->readMemoryBlock(data, std::min(size_t(bufSize), skipBlockSize)); } - } else if(state == eTES_SendingVar) { - String* val = &templateData[varName]; - if(varDataPos < val->length()) { + + // Return variable value + const String& value = templateData.valueAt(i); + if(bufSize < value.length()) { + debug_e("TemplateStream, buffer too small"); + return 0; + } + memcpy(data, value.c_str(), value.length()); + stream->seek(skipBlockSize); + varDataPos = 0; + state = eTES_SendingVar; + return value.length(); + } + + if(state == eTES_SendingVar) { + const String& val = templateData[varName]; + if(varDataPos < val.length()) { debug_d("continue TRANSFER variable value (not completed)"); - available = val->length() - varDataPos; - memcpy(data, ((char*)val->c_str()) + varDataPos, available); + size_t available = val.length() - varDataPos; + memcpy(data, val.c_str() + varDataPos, available); return available; } else { - debug_d("continue to plaint text"); + debug_d("continue to plain text"); state = eTES_Wait; } } - int len = FileStream::readMemoryBlock(data, bufSize); - char* tpl = data; - if(tpl && len > 0) { - char* end = tpl + len; - char* cur = (char*)memchr(tpl, '{', len); - char* lastFound = cur; - while(cur != NULL) { + unsigned datalen = stream->readMemoryBlock(data, bufSize); + if(datalen != 0) { + auto end = data + datalen; + auto cur = (const char*)memchr(data, '{', datalen); + auto lastFound = cur; + while(cur != nullptr) { lastFound = cur; - char* p = cur + 1; + const char* p = cur + 1; for(; p < end; p++) { if(isspace(*p)) break; // Not a var name @@ -68,40 +65,51 @@ uint16_t TemplateFileStream::readMemoryBlock(char* data, int bufSize) break; // New start.. if(*p == '}') { - int block = p - cur + 1; - char varname[TEMPLATE_MAX_VAR_NAME_LEN + 1] = {0}; - memcpy(varname, cur + 1, p - cur - 1); // name without { and } - varName = varname; - state = eTES_Found; - varWaitSize = cur - tpl; - debug_d("found var: %s, at %d (%d) - %d, send size %d", varName.c_str(), varWaitSize + 1, - varWaitSize + getPos(), p - tpl, varWaitSize); - skipBlockSize = block; - if(varWaitSize == 0) - state = eTES_StartVar; - return varWaitSize; // return only plain text from template without our variable + varName.setLength(p - cur - 1); + memcpy(varName.begin(), cur + 1, varName.length()); // name without { and } + skipBlockSize = p - cur + 1; + varWaitSize = cur - data; + state = varWaitSize ? eTES_Found : eTES_StartVar; + debug_d("found var '%s' at %u - %u, send size %u", varName.c_str(), varWaitSize + 1, p - data, + varWaitSize); + + // return only plain text from template without our variable + return varWaitSize; } } - cur = (char*)memchr(p, '{', len - (p - tpl)); // continue searching.. + + // continue searching... + cur = (const char*)memchr(p, '{', datalen - (p - data)); } - if(lastFound != NULL && (lastFound - tpl) > (len - TEMPLATE_MAX_VAR_NAME_LEN)) { - debug_d("trim end to %d from %d", lastFound - tpl, len); - len = lastFound - tpl; // It can be a incomplete variable name. Don't split it! + + if(lastFound != nullptr) { + unsigned newlen = lastFound - data; + if(newlen + TEMPLATE_MAX_VAR_NAME_LEN > datalen) { + debug_d("trim end to %u from %u", newlen, datalen); + // It can be a incomplete variable name - don't split it + datalen = newlen; + } } } - debug_d("plain template text pos: %d, len: %d", getPos(), len); - return len; + debug_d("plain template text pos: %d, len: %d", -1, datalen); + return datalen; } -bool TemplateFileStream::seek(int len) +bool TemplateStream::seek(int len) { + debug_d("TemplateStream::seek(%d), state = %d", len, state); + + // Forward-only seeks if(len < 0) return false; - //debug_d("SEEK: %d, (%d)", len, state); if(state == eTES_Found) { //debug_d("SEEK before Var: %d, (%d)", len, varWaitSize); + if(varWaitSize < (unsigned)len) { + debug_e("len > varWaitSize"); + return false; + } varWaitSize -= len; if(varWaitSize == 0) state = eTES_StartVar; @@ -110,15 +118,5 @@ bool TemplateFileStream::seek(int len) return false; // not the end } - return FileStream::seek(len); -} - -void TemplateFileStream::setVar(String name, String value) -{ - templateData[name] = value; -} - -void TemplateFileStream::setVars(const TemplateVariables& vars) -{ - templateData.setMultiple(vars); + return stream->seek(len); } diff --git a/Sming/SmingCore/Data/Stream/TemplateStream.h b/Sming/SmingCore/Data/Stream/TemplateStream.h index d8d3dea903..9c4443593f 100644 --- a/Sming/SmingCore/Data/Stream/TemplateStream.h +++ b/Sming/SmingCore/Data/Stream/TemplateStream.h @@ -5,10 +5,10 @@ * All files of the Sming Core are provided under the LGPL v3 license. ****/ -#ifndef _SMING_CORE_DATA_TEMPLATE_FILE_STREAM_H_ -#define _SMING_CORE_DATA_TEMPLATE_FILE_STREAM_H_ +#ifndef _SMING_CORE_DATA_TEMPLATE_STREAM_H_ +#define _SMING_CORE_DATA_TEMPLATE_STREAM_H_ -#include "FileStream.h" +#include "ReadWriteStream.h" #include "WHashMap.h" #include "WString.h" @@ -37,20 +37,27 @@ enum TemplateExpandState { * @{ */ -/// Template file stream class -class TemplateFileStream : public FileStream +class TemplateStream : public ReadWriteStream { public: - /** @brief Create a template file stream - * @param templateFileName Template filename + /** @brief Create a template stream + * @param stream source of template data */ - TemplateFileStream(const String& templateFileName); - virtual ~TemplateFileStream(); + TemplateStream(IDataSourceStream* stream) : stream(stream) + { + // Pre-allocate string to maximum length + varName.reserve(TEMPLATE_MAX_VAR_NAME_LEN); + } + + virtual ~TemplateStream() + { + delete stream; + } //Use base class documentation - virtual StreamType getStreamType() + virtual StreamType getStreamType() const { - return eSST_TemplateFile; + return stream ? eSST_Template : eSST_Invalid; } //Use base class documentation @@ -59,17 +66,28 @@ class TemplateFileStream : public FileStream //Use base class documentation virtual bool seek(int len); + virtual bool isFinished() + { + return stream ? stream->isFinished() : true; + } + /** @brief Set value of a variable in the template file * @param name Name of variable * @param value Value to assign to the variable * @note Sets and existing variable or adds a new variable if variable does not already exist */ - void setVar(String name, String value); + void setVar(const String& name, const String& value) + { + templateData[name] = value; + } /** @brief Set multiple variables in the template file * @param vars Template Variables */ - void setVars(const TemplateVariables& vars); + void setVars(const TemplateVariables& vars) + { + templateData.setMultiple(vars); + } /** @brief Get the template variables * @retval TemplateVariables Reference to the template variables @@ -79,23 +97,36 @@ class TemplateFileStream : public FileStream return templateData; } + virtual String getName() const + { + return stream ? stream->getName() : nullptr; + } + /** * @brief Return the total length of the stream * @retval int -1 is returned when the size cannot be determined + * + * We cannot reliably determine available size so use default method which returns -1. + * + * int available() */ - int available() + + /* @deprecated to be removed once class is migrated to IDataSourceStream base */ + virtual size_t write(const uint8_t* buffer, size_t size) { - return -1; + return 0; } private: + IDataSourceStream* stream = nullptr; TemplateVariables templateData; - TemplateExpandState state; + TemplateExpandState state = eTES_Wait; String varName; - int skipBlockSize = 0; - int varDataPos = 0; - int varWaitSize = 0; + size_t skipBlockSize = 0; + size_t varDataPos = 0; + size_t varWaitSize = 0; }; /** @} */ -#endif /* _SMING_CORE_DATA_TEMPLATE_FILE_STREAM_H_ */ + +#endif /* _SMING_CORE_DATA_TEMPLATESTREAM_H_ */ diff --git a/Sming/SmingCore/Data/Stream/UrlencodedOutputStream.h b/Sming/SmingCore/Data/Stream/UrlencodedOutputStream.h index 8699d9576b..5bc9240a7e 100644 --- a/Sming/SmingCore/Data/Stream/UrlencodedOutputStream.h +++ b/Sming/SmingCore/Data/Stream/UrlencodedOutputStream.h @@ -35,7 +35,7 @@ class UrlencodedOutputStream : public ReadWriteStream } //Use base class documentation - virtual StreamType getStreamType() + virtual StreamType getStreamType() const { return stream.getStreamType(); } diff --git a/Sming/SmingCore/Data/StreamTransformer.h b/Sming/SmingCore/Data/StreamTransformer.h index a70fc4cad0..1c00656c65 100644 --- a/Sming/SmingCore/Data/StreamTransformer.h +++ b/Sming/SmingCore/Data/StreamTransformer.h @@ -44,7 +44,7 @@ class StreamTransformer : public ReadWriteStream virtual ~StreamTransformer(); //Use base class documentation - virtual StreamType getStreamType() + virtual StreamType getStreamType() const { return sourceStream->getStreamType(); } diff --git a/Sming/SmingCore/Network/Http/HttpConnection.cpp b/Sming/SmingCore/Network/Http/HttpConnection.cpp index 2933ef5a97..cde3f61a3d 100644 --- a/Sming/SmingCore/Network/Http/HttpConnection.cpp +++ b/Sming/SmingCore/Network/Http/HttpConnection.cpp @@ -186,13 +186,13 @@ HttpPartResult HttpConnection::multipartProducer() if(outgoingRequest->files.count()) { String name = outgoingRequest->files.keyAt(0); - FileStream* file = outgoingRequest->files[name]; + auto file = outgoingRequest->files[name]; result.stream = file; HttpHeaders* headers = new HttpHeaders(); (*headers)[HTTP_HEADER_CONTENT_DISPOSITION] = - F("form-data; name=\"") + name + F("\"; filename=\"") + file->fileName() + '"'; - (*headers)[HTTP_HEADER_CONTENT_TYPE] = ContentType::fromFullFileName(file->fileName()); + F("form-data; name=\"") + name + F("\"; filename=\"") + file->getName() + '"'; + (*headers)[HTTP_HEADER_CONTENT_TYPE] = ContentType::fromFullFileName(file->getName()); result.headers = headers; outgoingRequest->files.remove(name); diff --git a/Sming/SmingCore/Network/Http/HttpRequest.h b/Sming/SmingCore/Network/Http/HttpRequest.h index 73e39f8be6..0cece426a5 100644 --- a/Sming/SmingCore/Network/Http/HttpRequest.h +++ b/Sming/SmingCore/Network/Http/HttpRequest.h @@ -106,11 +106,11 @@ class HttpRequest /** * @brief Sets a file to be sent * @param const String& formElementName the name of the element in the form - * @param FileStream* stream - pointer to the file stream + * @param ReadWriteStream* stream - pointer to the stream (doesn't have to be a FileStream) * * @return HttpRequest* */ - HttpRequest* setFile(const String& formElementName, FileStream* stream) + HttpRequest* setFile(const String& formElementName, ReadWriteStream* stream) { if(stream) { files[formElementName] = stream; @@ -290,7 +290,7 @@ class HttpRequest #endif private: - HashMap files; + HashMap files; HttpParams* queryParams = nullptr; // << deprecated }; diff --git a/Sming/SmingCore/Network/Http/HttpResponse.cpp b/Sming/SmingCore/Network/Http/HttpResponse.cpp index da18faf252..4064e66f1d 100644 --- a/Sming/SmingCore/Network/Http/HttpResponse.cpp +++ b/Sming/SmingCore/Network/Http/HttpResponse.cpp @@ -14,6 +14,7 @@ #include "../WebConstants.h" #include "Data/Stream/MemoryDataStream.h" #include "Data/Stream/JsonObjectStream.h" +#include "Data/Stream/FileStream.h" HttpResponse::~HttpResponse() { @@ -116,7 +117,7 @@ bool HttpResponse::sendFile(String fileName, bool allowGzipFileCheck /* = true*/ return true; } -bool HttpResponse::sendTemplate(TemplateFileStream* newTemplateInstance) +bool HttpResponse::sendTemplate(TemplateStream* newTemplateInstance) { if(stream != nullptr) { SYSTEM_ERROR("Stream already created"); @@ -125,7 +126,7 @@ bool HttpResponse::sendTemplate(TemplateFileStream* newTemplateInstance) } stream = newTemplateInstance; - if(!newTemplateInstance->fileExist()) { + if(!newTemplateInstance->isValid()) { code = HTTP_STATUS_NOT_FOUND; delete stream; stream = nullptr; @@ -133,7 +134,7 @@ bool HttpResponse::sendTemplate(TemplateFileStream* newTemplateInstance) } if(!headers.contains(HTTP_HEADER_CONTENT_TYPE)) { - String mime = ContentType::fromFullFileName(newTemplateInstance->fileName()); + String mime = ContentType::fromFullFileName(newTemplateInstance->getName()); if(mime) setContentType(mime); } diff --git a/Sming/SmingCore/Network/Http/HttpResponse.h b/Sming/SmingCore/Network/Http/HttpResponse.h index 0f87e14bc6..99045f4b86 100644 --- a/Sming/SmingCore/Network/Http/HttpResponse.h +++ b/Sming/SmingCore/Network/Http/HttpResponse.h @@ -14,8 +14,10 @@ #define _SMING_CORE_HTTP_RESPONSE_H_ #include "HttpCommon.h" -#include "Data/Stream/TemplateFileStream.h" +#include "Data/Stream/TemplateStream.h" +#include "Data/Stream/ReadWriteStream.h" #include "Network/Http/HttpHeaders.h" +#include "FileSystem.h" class JsonObjectStream; // << TODO: deprecated and should be removed in the next version @@ -66,7 +68,7 @@ class HttpResponse // @deprecated // Parse and send template file - bool sendTemplate(TemplateFileStream* newTemplateInstance); + bool sendTemplate(TemplateStream* newTemplateInstance); /** * @brief Build and send JSON string diff --git a/Sming/SmingCore/Network/HttpClient.cpp b/Sming/SmingCore/Network/HttpClient.cpp index 0b8d2e2889..01d77010a2 100644 --- a/Sming/SmingCore/Network/HttpClient.cpp +++ b/Sming/SmingCore/Network/HttpClient.cpp @@ -11,7 +11,7 @@ ****/ #include "HttpClient.h" -#include "../Data/Stream/DataSourceStream.h" +#include "Data/Stream/FileStream.h" /* Low Level Methods */ bool HttpClient::send(HttpRequest* request) diff --git a/Sming/SmingCore/Network/rBootHttpUpdate.h b/Sming/SmingCore/Network/rBootHttpUpdate.h index d84fb4dd5d..1e1948cd82 100644 --- a/Sming/SmingCore/Network/rBootHttpUpdate.h +++ b/Sming/SmingCore/Network/rBootHttpUpdate.h @@ -38,7 +38,7 @@ class rBootItemOutputStream : public ReadWriteStream return write(&charToWrite, 1); } - virtual StreamType getStreamType() + virtual StreamType getStreamType() const { return eSST_File; }