HttpResponse.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * HttpResponse.h
8  *
9  * @author: 2017 - Slavey Karadzhov <slav@attachix.com>
10  *
11  ****/
12 
13 #pragma once
14 
15 #include "HttpCommon.h"
17 #include "HttpHeaders.h"
18 #include "FileSystem.h"
19 
26 {
27 public:
29  {
30  freeStreams();
31  }
32 
33  bool sendString(const String& text);
34 
35  bool sendString(String&& text) noexcept;
36 
40  bool hasHeader(const String& name) SMING_DEPRECATED
41  {
42  return headers.contains(name);
43  }
44 
48  void redirect(const String& location) SMING_DEPRECATED
49  {
50  headers[HTTP_HEADER_LOCATION] = location;
51  }
52 
57  {
58  code = HTTP_STATUS_FORBIDDEN;
59  }
60 
65  {
66  code = HTTP_STATUS_NOT_FOUND;
67  }
68 
70  {
71  headers[HTTP_HEADER_CONTENT_TYPE] = type;
72  return this;
73  }
74 
76  {
77  return setContentType(::toString(type));
78  }
79 
80  HttpResponse* setCookie(const String& name, const String& value, bool append = false);
81 
82  HttpResponse* setHeader(const String& name, const String& value)
83  {
84  headers[name] = value;
85  return this;
86  }
87 
88  HttpResponse* setCache(int maxAgeSeconds = 3600, bool isPublic = false);
89 
90  // Access-Control-Allow-Origin for AJAX from a different domain
91  HttpResponse* setAllowCrossDomainOrigin(const String& controlAllowOrigin)
92  {
93  headers[HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN] = controlAllowOrigin;
94  return this;
95  }
96 
97  /*
98  * Send file by stat, indicates whether file is compressed
99  * A name is required in stat to get the appropriate content type
100  */
101  bool sendFile(const FileStat& stat);
102 
109  bool sendFile(const String& fileName, bool allowGzipFileCheck = true);
110 
118  {
119  return sendNamedStream(newTemplateInstance);
120  }
121 
127  bool sendNamedStream(IDataSourceStream* newDataStream);
128 
134  bool sendDataStream(IDataSourceStream* newDataStream, enum MimeType type)
135  {
136  return sendDataStream(newDataStream, ::toString(type));
137  }
138 
146  bool sendDataStream(IDataSourceStream* newDataStream, const String& reqContentType = nullptr);
147 
156  {
157  String s;
158  if(stream != nullptr) {
159  stream->moveString(s);
160  }
161  return s;
162  }
163 
167  void reset();
168 
174 
178  void freeStreams();
179 
183  bool isSuccess()
184  {
185  return (code >= HTTP_STATUS_OK && code < HTTP_STATUS_BAD_REQUEST);
186  }
187 
192  String toString() const;
193 
201  {
202  return res.toString();
203  }
204 
205 private:
206  void setStream(IDataSourceStream* stream);
207 
208 public:
209  HttpStatus code = HTTP_STATUS_OK;
211  ReadWriteStream* buffer = nullptr;
212  IDataSourceStream* stream = nullptr;
213 };
214 
215 inline String toString(const HttpResponse& res)
216 {
217  return res.toString();
218 }
bool contains(const String &name) const
Definition: HttpHeaders.h:75
static String toString(const HttpResponse &res)
Tries to present a readable version of the response.
Definition: HttpResponse.h:200
bool sendNamedStream(IDataSourceStream *newDataStream)
Parse and send stream, using the name to determine the content type.
HttpResponse * setContentType(const String &type)
Definition: HttpResponse.h:69
Base class for read-only stream.
Definition: DataSourceStream.h:40
void reset()
reset response so it can be re-used
HttpResponse * setAllowCrossDomainOrigin(const String &controlAllowOrigin)
Definition: HttpResponse.h:91
bool isSuccess()
Determine if the response status indicates success.
Definition: HttpResponse.h:183
HttpResponse * setContentType(enum MimeType type)
Definition: HttpResponse.h:75
bool sendString(const String &text)
The String class.
Definition: WString.h:136
void notFound()
Definition: HttpResponse.h:64
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:34
String toString() const
Tries to present a readable version of the current response values.
IDataSourceStream * stream
The body stream.
Definition: HttpResponse.h:212
HttpHeaders headers
Definition: HttpResponse.h:210
bool sendFile(const FileStat &stat)
#define SMING_DEPRECATED
Definition: sming_attr.h:30
bool sendTemplate(IDataSourceStream *newTemplateInstance)
Parse and send template file.
Definition: HttpResponse.h:117
HttpResponse * setHeader(const String &name, const String &value)
Definition: HttpResponse.h:82
File Status structure.
Definition: Stat.h:51
void freeStreams()
release allocated stream memory
void forbidden()
Definition: HttpResponse.h:56
HttpStatus code
The HTTP status response code.
Definition: HttpResponse.h:209
String getBody()
Moves content from the body stream into a String.
Definition: HttpResponse.h:155
bool hasHeader(const String &name)
Definition: HttpResponse.h:40
bool sendDataStream(IDataSourceStream *newDataStream, enum MimeType type)
Send data from the given stream object.
Definition: HttpResponse.h:134
HttpResponse * setCookie(const String &name, const String &value, bool append=false)
HttpStatus
HTTP status code.
Definition: HttpCommon.h:55
void redirect(const String &location)
Definition: HttpResponse.h:48
ReadWriteStream * buffer
Internal stream for storing strings and receiving responses.
Definition: HttpResponse.h:211
Base class for read/write stream.
Definition: ReadWriteStream.h:19
virtual bool moveString(String &s)
Memory-based streams may be able to move content into a String.
Definition: DataSourceStream.h:194
MimeType
Definition: WebConstants.h:53
HttpResponse * setCache(int maxAgeSeconds=3600, bool isPublic=false)
~HttpResponse()
Definition: HttpResponse.h:28
void setBuffer(ReadWriteStream *buffer)
Called by connection to specify where incoming response data is written.
Represents either an incoming or outgoing response to a HTTP request.
Definition: HttpResponse.h:25