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 
38  {
39  headers[HTTP_HEADER_CONTENT_TYPE] = type;
40  return this;
41  }
42 
44  {
45  return setContentType(::toString(type));
46  }
47 
48  HttpResponse* setCookie(const String& name, const String& value, bool append = false);
49 
50  HttpResponse* setHeader(const String& name, const String& value)
51  {
52  headers[name] = value;
53  return this;
54  }
55 
56  HttpResponse* setCache(int maxAgeSeconds = 3600, bool isPublic = false);
57 
58  // Access-Control-Allow-Origin for AJAX from a different domain
59  HttpResponse* setAllowCrossDomainOrigin(const String& controlAllowOrigin)
60  {
61  headers[HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN] = controlAllowOrigin;
62  return this;
63  }
64 
71  bool sendFile(const String& fileName, bool allowGzipFileCheck = true);
72 
78  bool sendNamedStream(IDataSourceStream* newDataStream);
79 
85  bool sendDataStream(IDataSourceStream* newDataStream, enum MimeType type)
86  {
87  return sendDataStream(newDataStream, ::toString(type));
88  }
89 
97  bool sendDataStream(IDataSourceStream* newDataStream, const String& reqContentType = nullptr);
98 
107  {
108  String s;
109  if(stream != nullptr) {
110  stream->moveString(s);
111  }
112  return s;
113  }
114 
118  void reset();
119 
125 
129  void freeStreams();
130 
134  bool isSuccess()
135  {
136  return (code >= HTTP_STATUS_OK && code < HTTP_STATUS_BAD_REQUEST);
137  }
138 
143  String toString() const;
144 
145 private:
146  void setStream(IDataSourceStream* stream);
147 
148 public:
149  HttpStatus code = HTTP_STATUS_OK;
151  ReadWriteStream* buffer = nullptr;
153 };
154 
155 inline String toString(const HttpResponse& res)
156 {
157  return res.toString();
158 }
HttpStatus
HTTP status code.
Definition: HttpCommon.h:55
String toString(const HttpResponse &res)
Definition: HttpResponse.h:155
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:35
Represents either an incoming or outgoing response to a HTTP request.
Definition: HttpResponse.h:26
HttpResponse * setHeader(const String &name, const String &value)
Definition: HttpResponse.h:50
bool sendDataStream(IDataSourceStream *newDataStream, enum MimeType type)
Send data from the given stream object.
Definition: HttpResponse.h:85
HttpResponse * setCache(int maxAgeSeconds=3600, bool isPublic=false)
bool sendNamedStream(IDataSourceStream *newDataStream)
Parse and send stream, using the name to determine the content type.
HttpResponse * setCookie(const String &name, const String &value, bool append=false)
bool sendFile(const String &fileName, bool allowGzipFileCheck=true)
Send file by name.
void setBuffer(ReadWriteStream *buffer)
Called by connection to specify where incoming response data is written.
HttpResponse * setContentType(enum MimeType type)
Definition: HttpResponse.h:43
~HttpResponse()
Definition: HttpResponse.h:28
IDataSourceStream * stream
The body stream.
Definition: HttpResponse.h:152
bool sendString(String &&text) noexcept
String toString() const
Tries to present a readable version of the current response values.
HttpHeaders headers
Response headers.
Definition: HttpResponse.h:150
ReadWriteStream * buffer
Internal stream for storing strings and receiving responses.
Definition: HttpResponse.h:151
bool sendString(const String &text)
String getBody()
Moves content from the body stream into a String.
Definition: HttpResponse.h:106
bool sendDataStream(IDataSourceStream *newDataStream, const String &reqContentType=nullptr)
Send data from the given stream object.
bool isSuccess()
Determine if the response status indicates success.
Definition: HttpResponse.h:134
void reset()
reset response so it can be re-used
void freeStreams()
release allocated stream memory
HttpResponse * setContentType(const String &type)
Definition: HttpResponse.h:37
HttpStatus code
The HTTP status response code.
Definition: HttpResponse.h:149
HttpResponse * setAllowCrossDomainOrigin(const String &controlAllowOrigin)
Definition: HttpResponse.h:59
Base class for read-only stream.
Definition: DataSourceStream.h:46
virtual bool moveString(String &s)
Memory-based streams may be able to move content into a String.
Definition: DataSourceStream.h:190
Base class for read/write stream.
Definition: ReadWriteStream.h:20
The String class.
Definition: WString.h:137
MimeType
Definition: WebConstants.h:53