2020-08-21 12:43:46 +02:00
|
|
|
/*
|
|
|
|
Asynchronous WebServer library for Espressif MCUs
|
|
|
|
|
|
|
|
Copyright (c) 2016 Hristo Gochkov. All rights reserved.
|
|
|
|
This file is part of the esp8266 core for Arduino environment.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
#ifndef ASYNCWEBSERVERRESPONSEIMPL_H_
|
|
|
|
#define ASYNCWEBSERVERRESPONSEIMPL_H_
|
|
|
|
|
|
|
|
#ifdef Arduino_h
|
2024-07-02 20:06:33 +02:00
|
|
|
// arduino is not compatible with std::vector
|
|
|
|
#undef min
|
|
|
|
#undef max
|
2020-08-21 12:43:46 +02:00
|
|
|
#endif
|
2024-12-05 10:21:12 +01:00
|
|
|
#include "literals.h"
|
|
|
|
#include <StreamString.h>
|
2022-06-22 11:39:36 +02:00
|
|
|
#include <memory>
|
2024-07-02 20:06:33 +02:00
|
|
|
#include <vector>
|
2022-06-22 11:39:36 +02:00
|
|
|
|
2020-08-21 12:43:46 +02:00
|
|
|
// It is possible to restore these defines, but one can use _min and _max instead. Or std::min, std::max.
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
class AsyncBasicResponse : public AsyncWebServerResponse {
|
2020-08-21 12:43:46 +02:00
|
|
|
private:
|
|
|
|
String _content;
|
2024-07-02 20:06:33 +02:00
|
|
|
|
2020-08-21 12:43:46 +02:00
|
|
|
public:
|
2024-08-19 16:41:09 +02:00
|
|
|
explicit AsyncBasicResponse(int code, const char* contentType = asyncsrv::empty, const char* content = asyncsrv::empty);
|
|
|
|
AsyncBasicResponse(int code, const String& contentType, const String& content = emptyString) : AsyncBasicResponse(code, contentType.c_str(), content.c_str()) {}
|
2024-12-05 10:21:12 +01:00
|
|
|
void _respond(AsyncWebServerRequest* request) override final;
|
|
|
|
size_t _ack(AsyncWebServerRequest* request, size_t len, uint32_t time) override final;
|
|
|
|
bool _sourceValid() const override final { return true; }
|
2020-08-21 12:43:46 +02:00
|
|
|
};
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
class AsyncAbstractResponse : public AsyncWebServerResponse {
|
2020-08-21 12:43:46 +02:00
|
|
|
private:
|
|
|
|
String _head;
|
2024-07-02 20:06:33 +02:00
|
|
|
// Data is inserted into cache at begin().
|
|
|
|
// This is inefficient with vector, but if we use some other container,
|
2020-08-21 12:43:46 +02:00
|
|
|
// we won't be able to access it as contiguous array of bytes when reading from it,
|
|
|
|
// so by gaining performance in one place, we'll lose it in another.
|
|
|
|
std::vector<uint8_t> _cache;
|
|
|
|
size_t _readDataFromCacheOrContent(uint8_t* data, const size_t len);
|
|
|
|
size_t _fillBufferAndProcessTemplates(uint8_t* buf, size_t maxLen);
|
2024-07-02 20:06:33 +02:00
|
|
|
|
2020-08-21 12:43:46 +02:00
|
|
|
protected:
|
|
|
|
AwsTemplateProcessor _callback;
|
2024-07-02 20:06:33 +02:00
|
|
|
|
2020-08-21 12:43:46 +02:00
|
|
|
public:
|
2024-07-02 20:06:33 +02:00
|
|
|
AsyncAbstractResponse(AwsTemplateProcessor callback = nullptr);
|
2024-12-05 10:21:12 +01:00
|
|
|
virtual ~AsyncAbstractResponse() {}
|
|
|
|
void _respond(AsyncWebServerRequest* request) override final;
|
|
|
|
size_t _ack(AsyncWebServerRequest* request, size_t len, uint32_t time) override final;
|
|
|
|
virtual bool _sourceValid() const { return false; }
|
2024-07-02 20:06:33 +02:00
|
|
|
virtual size_t _fillBuffer(uint8_t* buf __attribute__((unused)), size_t maxLen __attribute__((unused))) { return 0; }
|
2020-08-21 12:43:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef TEMPLATE_PLACEHOLDER
|
2024-07-02 20:06:33 +02:00
|
|
|
#define TEMPLATE_PLACEHOLDER '%'
|
2020-08-21 12:43:46 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TEMPLATE_PARAM_NAME_LENGTH 32
|
2024-07-02 20:06:33 +02:00
|
|
|
class AsyncFileResponse : public AsyncAbstractResponse {
|
|
|
|
using File = fs::File;
|
|
|
|
using FS = fs::FS;
|
|
|
|
|
2020-08-21 12:43:46 +02:00
|
|
|
private:
|
|
|
|
File _content;
|
|
|
|
String _path;
|
2024-08-19 16:41:09 +02:00
|
|
|
void _setContentTypeFromPath(const String& path);
|
2024-07-02 20:06:33 +02:00
|
|
|
|
2020-08-21 12:43:46 +02:00
|
|
|
public:
|
2024-08-19 16:41:09 +02:00
|
|
|
AsyncFileResponse(FS& fs, const String& path, const char* contentType = asyncsrv::empty, bool download = false, AwsTemplateProcessor callback = nullptr);
|
|
|
|
AsyncFileResponse(FS& fs, const String& path, const String& contentType, bool download = false, AwsTemplateProcessor callback = nullptr) : AsyncFileResponse(fs, path, contentType.c_str(), download, callback) {}
|
|
|
|
AsyncFileResponse(File content, const String& path, const char* contentType = asyncsrv::empty, bool download = false, AwsTemplateProcessor callback = nullptr);
|
|
|
|
AsyncFileResponse(File content, const String& path, const String& contentType, bool download = false, AwsTemplateProcessor callack = nullptr) : AsyncFileResponse(content, path, contentType.c_str(), download, callack) {}
|
2024-12-05 10:21:12 +01:00
|
|
|
~AsyncFileResponse() { _content.close(); }
|
|
|
|
bool _sourceValid() const override final { return !!(_content); }
|
|
|
|
size_t _fillBuffer(uint8_t* buf, size_t maxLen) override final;
|
2020-08-21 12:43:46 +02:00
|
|
|
};
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
class AsyncStreamResponse : public AsyncAbstractResponse {
|
2020-08-21 12:43:46 +02:00
|
|
|
private:
|
2024-07-02 20:06:33 +02:00
|
|
|
Stream* _content;
|
|
|
|
|
2020-08-21 12:43:46 +02:00
|
|
|
public:
|
2024-08-19 16:41:09 +02:00
|
|
|
AsyncStreamResponse(Stream& stream, const char* contentType, size_t len, AwsTemplateProcessor callback = nullptr);
|
|
|
|
AsyncStreamResponse(Stream& stream, const String& contentType, size_t len, AwsTemplateProcessor callback = nullptr) : AsyncStreamResponse(stream, contentType.c_str(), len, callback) {}
|
2024-12-05 10:21:12 +01:00
|
|
|
bool _sourceValid() const override final { return !!(_content); }
|
|
|
|
size_t _fillBuffer(uint8_t* buf, size_t maxLen) override final;
|
2020-08-21 12:43:46 +02:00
|
|
|
};
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
class AsyncCallbackResponse : public AsyncAbstractResponse {
|
2020-08-21 12:43:46 +02:00
|
|
|
private:
|
|
|
|
AwsResponseFiller _content;
|
|
|
|
size_t _filledLength;
|
2024-07-02 20:06:33 +02:00
|
|
|
|
2020-08-21 12:43:46 +02:00
|
|
|
public:
|
2024-08-19 16:41:09 +02:00
|
|
|
AsyncCallbackResponse(const char* contentType, size_t len, AwsResponseFiller callback, AwsTemplateProcessor templateCallback = nullptr);
|
|
|
|
AsyncCallbackResponse(const String& contentType, size_t len, AwsResponseFiller callback, AwsTemplateProcessor templateCallback = nullptr) : AsyncCallbackResponse(contentType.c_str(), len, callback, templateCallback) {}
|
2024-12-05 10:21:12 +01:00
|
|
|
bool _sourceValid() const override final { return !!(_content); }
|
|
|
|
size_t _fillBuffer(uint8_t* buf, size_t maxLen) override final;
|
2020-08-21 12:43:46 +02:00
|
|
|
};
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
class AsyncChunkedResponse : public AsyncAbstractResponse {
|
2020-08-21 12:43:46 +02:00
|
|
|
private:
|
|
|
|
AwsResponseFiller _content;
|
|
|
|
size_t _filledLength;
|
2024-07-02 20:06:33 +02:00
|
|
|
|
2020-08-21 12:43:46 +02:00
|
|
|
public:
|
2024-08-19 16:41:09 +02:00
|
|
|
AsyncChunkedResponse(const char* contentType, AwsResponseFiller callback, AwsTemplateProcessor templateCallback = nullptr);
|
|
|
|
AsyncChunkedResponse(const String& contentType, AwsResponseFiller callback, AwsTemplateProcessor templateCallback = nullptr) : AsyncChunkedResponse(contentType.c_str(), callback, templateCallback) {}
|
2024-12-05 10:21:12 +01:00
|
|
|
bool _sourceValid() const override final { return !!(_content); }
|
|
|
|
size_t _fillBuffer(uint8_t* buf, size_t maxLen) override final;
|
2020-08-21 12:43:46 +02:00
|
|
|
};
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
class AsyncProgmemResponse : public AsyncAbstractResponse {
|
2020-08-21 12:43:46 +02:00
|
|
|
private:
|
2024-07-02 20:06:33 +02:00
|
|
|
const uint8_t* _content;
|
2020-08-21 12:43:46 +02:00
|
|
|
size_t _readLength;
|
2024-07-02 20:06:33 +02:00
|
|
|
|
2020-08-21 12:43:46 +02:00
|
|
|
public:
|
2024-08-19 16:41:09 +02:00
|
|
|
AsyncProgmemResponse(int code, const char* contentType, const uint8_t* content, size_t len, AwsTemplateProcessor callback = nullptr);
|
|
|
|
AsyncProgmemResponse(int code, const String& contentType, const uint8_t* content, size_t len, AwsTemplateProcessor callback = nullptr) : AsyncProgmemResponse(code, contentType.c_str(), content, len, callback) {}
|
2024-12-05 10:21:12 +01:00
|
|
|
bool _sourceValid() const override final { return true; }
|
|
|
|
size_t _fillBuffer(uint8_t* buf, size_t maxLen) override final;
|
2020-08-21 12:43:46 +02:00
|
|
|
};
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
class AsyncResponseStream : public AsyncAbstractResponse, public Print {
|
2020-08-21 12:43:46 +02:00
|
|
|
private:
|
2024-12-05 10:21:12 +01:00
|
|
|
StreamString _content;
|
2024-07-02 20:06:33 +02:00
|
|
|
|
2020-08-21 12:43:46 +02:00
|
|
|
public:
|
2024-08-19 16:41:09 +02:00
|
|
|
AsyncResponseStream(const char* contentType, size_t bufferSize);
|
|
|
|
AsyncResponseStream(const String& contentType, size_t bufferSize) : AsyncResponseStream(contentType.c_str(), bufferSize) {}
|
2024-12-05 10:21:12 +01:00
|
|
|
bool _sourceValid() const override final { return (_state < RESPONSE_END); }
|
|
|
|
size_t _fillBuffer(uint8_t* buf, size_t maxLen) override final;
|
2024-07-02 20:06:33 +02:00
|
|
|
size_t write(const uint8_t* data, size_t len);
|
2020-08-21 12:43:46 +02:00
|
|
|
size_t write(uint8_t data);
|
|
|
|
using Print::write;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* ASYNCWEBSERVERRESPONSEIMPL_H_ */
|