Files
ESPAsyncWebServer/src/WebResponseImpl.h

111 lines
4.1 KiB
C
Raw Normal View History

2015-12-19 18:53:33 +02:00
/*
Asynchronous WebServer library for Espressif MCUs
2015-12-19 18:53:33 +02:00
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
*/
2015-12-19 18:53:33 +02:00
#ifndef ASYNCWEBSERVERRESPONSEIMPL_H_
#define ASYNCWEBSERVERRESPONSEIMPL_H_
class AsyncBasicResponse: public AsyncWebServerResponse {
private:
String _content;
public:
AsyncBasicResponse(int code, const String& contentType=String(), const String& content=String());
2015-12-19 18:53:33 +02:00
void _respond(AsyncWebServerRequest *request);
size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time);
bool _sourceValid() const { return true; }
2015-12-19 18:53:33 +02:00
};
2015-12-19 20:48:53 +02:00
class AsyncAbstractResponse: public AsyncWebServerResponse {
private:
String _head;
public:
void _respond(AsyncWebServerRequest *request);
size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time);
bool _sourceValid() const { return false; }
virtual size_t _fillBuffer(uint8_t *buf __attribute__((unused)), size_t maxLen __attribute__((unused))) { return 0; }
2015-12-19 20:48:53 +02:00
};
class AsyncFileResponse: public AsyncAbstractResponse {
using File = fs::File;
using FS = fs::FS;
2015-12-19 18:53:33 +02:00
private:
File _content;
String _path;
void _setContentType(const String& path);
2015-12-19 18:53:33 +02:00
public:
AsyncFileResponse(FS &fs, const String& path, const String& contentType=String(), bool download=false);
AsyncFileResponse(File content, const String& path, const String& contentType=String(), bool download=false);
2015-12-19 18:53:33 +02:00
~AsyncFileResponse();
bool _sourceValid() const { return !!(_content); }
virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override;
2015-12-19 18:53:33 +02:00
};
2015-12-19 20:48:53 +02:00
class AsyncStreamResponse: public AsyncAbstractResponse {
2015-12-19 18:53:33 +02:00
private:
Stream *_content;
public:
AsyncStreamResponse(Stream &stream, const String& contentType, size_t len);
bool _sourceValid() const { return !!(_content); }
virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override;
2015-12-19 18:53:33 +02:00
};
2015-12-19 20:48:53 +02:00
class AsyncCallbackResponse: public AsyncAbstractResponse {
2015-12-19 18:53:33 +02:00
private:
AwsResponseFiller _content;
public:
AsyncCallbackResponse(const String& contentType, size_t len, AwsResponseFiller callback);
bool _sourceValid() const { return !!(_content); }
virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override;
2015-12-19 18:53:33 +02:00
};
class AsyncChunkedResponse: public AsyncAbstractResponse {
private:
AwsResponseFiller _content;
public:
AsyncChunkedResponse(const String& contentType, AwsResponseFiller callback);
bool _sourceValid() const { return !!(_content); }
virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override;
};
class AsyncProgmemResponse: public AsyncAbstractResponse {
private:
const uint8_t * _content;
public:
AsyncProgmemResponse(int code, const String& contentType, const uint8_t * content, size_t len);
bool _sourceValid() const { return true; }
virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override;
};
2016-01-22 18:06:23 +02:00
class cbuf;
2016-01-19 21:47:26 +02:00
class AsyncResponseStream: public AsyncAbstractResponse, public Print {
private:
cbuf *_content;
public:
AsyncResponseStream(const String& contentType, size_t bufferSize);
2016-01-19 21:47:26 +02:00
~AsyncResponseStream();
bool _sourceValid() const { return (_state < RESPONSE_END); }
virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override;
2016-01-19 21:47:26 +02:00
size_t write(const uint8_t *data, size_t len);
size_t write(uint8_t data);
using Print::write;
2016-01-19 21:47:26 +02:00
};
2015-12-19 18:53:33 +02:00
#endif /* ASYNCWEBSERVERRESPONSEIMPL_H_ */