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 
103  bool sendFile(const String& fileName, bool allowGzipFileCheck = true);
104 
112  {
113  return sendNamedStream(newTemplateInstance);
114  }
115 
121  bool sendNamedStream(IDataSourceStream* newDataStream);
122 
128  bool sendDataStream(IDataSourceStream* newDataStream, enum MimeType type)
129  {
130  return sendDataStream(newDataStream, ::toString(type));
131  }
132 
140  bool sendDataStream(IDataSourceStream* newDataStream, const String& reqContentType = nullptr);
141 
150  {
151  String s;
152  if(stream != nullptr) {
153  stream->moveString(s);
154  }
155  return s;
156  }
157 
161  void reset();
162 
168 
172  void freeStreams();
173 
177  bool isSuccess()
178  {
179  return (code >= HTTP_STATUS_OK && code < HTTP_STATUS_BAD_REQUEST);
180  }
181 
186  String toString() const;
187 
195  {
196  return res.toString();
197  }
198 
199 private:
200  void setStream(IDataSourceStream* stream);
201 
202 public:
203  HttpStatus code = HTTP_STATUS_OK;
205  ReadWriteStream* buffer = nullptr;
206  IDataSourceStream* stream = nullptr;
207 };
208 
209 inline String toString(const HttpResponse& res)
210 {
211  return res.toString();
212 }
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:194
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:177
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:206
HttpHeaders headers
Definition: HttpResponse.h:204
#define SMING_DEPRECATED
Definition: sming_attr.h:30
bool sendTemplate(IDataSourceStream *newTemplateInstance)
Parse and send template file.
Definition: HttpResponse.h:111
HttpResponse * setHeader(const String &name, const String &value)
Definition: HttpResponse.h:82
void freeStreams()
release allocated stream memory
void forbidden()
Definition: HttpResponse.h:56
HttpStatus code
The HTTP status response code.
Definition: HttpResponse.h:203
bool sendFile(const String &fileName, bool allowGzipFileCheck=true)
Send file by name.
String getBody()
Moves content from the body stream into a String.
Definition: HttpResponse.h:149
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:128
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:205
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