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 
21 {
22 public:
24  {
25  freeStreams();
26  }
27 
28  bool sendString(const String& text);
29 
33  bool hasHeader(const String& name) SMING_DEPRECATED
34  {
35  return headers.contains(name);
36  }
37 
41  void redirect(const String& location) SMING_DEPRECATED
42  {
43  headers[HTTP_HEADER_LOCATION] = location;
44  }
45 
50  {
51  code = HTTP_STATUS_FORBIDDEN;
52  }
53 
58  {
59  code = HTTP_STATUS_NOT_FOUND;
60  }
61 
62  HttpResponse* setContentType(const String& type);
64  HttpResponse* setCookie(const String& name, const String& value);
65  HttpResponse* setHeader(const String& name, const String& value);
66  HttpResponse* setCache(int maxAgeSeconds = 3600, bool isPublic = false);
67  // Access-Control-Allow-Origin for AJAX from a different domain
68  HttpResponse* setAllowCrossDomainOrigin(const String& controlAllowOrigin);
69 
76  bool sendFile(const String& fileName, bool allowGzipFileCheck = true);
77 
84  bool sendTemplate(IDataSourceStream* newTemplateInstance) SMING_DEPRECATED
85  {
86  return sendNamedStream(newTemplateInstance);
87  }
88 
94  bool sendNamedStream(IDataSourceStream* newDataStream);
95 
101  bool sendDataStream(IDataSourceStream* newDataStream, enum MimeType type)
102  {
103  return sendDataStream(newDataStream, ContentType::toString(type));
104  }
105 
113  bool sendDataStream(IDataSourceStream* newDataStream, const String& reqContentType = nullptr);
114 
120  String getBody();
121 
125  void reset();
126 
132 
136  void freeStreams();
137 
138  bool isSuccess()
139  {
140  return (code >= HTTP_STATUS_OK && code <= 399);
141  }
142 
143 private:
144  void setStream(IDataSourceStream* stream);
145 
146 public:
147  unsigned code = HTTP_STATUS_OK;
149  ReadWriteStream* buffer = nullptr;
150  IDataSourceStream* stream = nullptr;
151 };
bool contains(const String &name) const
Definition: HttpHeaders.h:170
bool sendNamedStream(IDataSourceStream *newDataStream)
Parse and send stream, using the name to determine the content type.
HttpResponse * setContentType(const String &type)
Base class for data source stream.
Definition: DataSourceStream.h:39
void reset()
reset response so it can be re-used
HttpResponse * setAllowCrossDomainOrigin(const String &controlAllowOrigin)
bool isSuccess()
Definition: HttpResponse.h:138
bool sendString(const String &text)
The String class.
Definition: WString.h:136
void notFound()
Definition: HttpResponse.h:57
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:96
IDataSourceStream * stream
The body stream.
Definition: HttpResponse.h:150
HttpHeaders headers
Definition: HttpResponse.h:148
#define SMING_DEPRECATED
Definition: sming_attr.h:30
String toString(enum MimeType m)
Get textual representation for a MIME type.
HttpResponse * setCookie(const String &name, const String &value)
bool sendTemplate(IDataSourceStream *newTemplateInstance)
Parse and send template file.
Definition: HttpResponse.h:84
HttpResponse * setHeader(const String &name, const String &value)
unsigned code
The HTTP status response code.
Definition: HttpResponse.h:147
void freeStreams()
release allocated stream memory
void forbidden()
Definition: HttpResponse.h:49
bool sendFile(const String &fileName, bool allowGzipFileCheck=true)
Send file by name.
String getBody()
Get response body as a string.
bool hasHeader(const String &name)
Definition: HttpResponse.h:33
bool sendDataStream(IDataSourceStream *newDataStream, enum MimeType type)
Send data from the given stream object.
Definition: HttpResponse.h:101
void redirect(const String &location)
Definition: HttpResponse.h:41
ReadWriteStream * buffer
Internal stream for storing strings and receiving responses.
Definition: HttpResponse.h:149
Base class for read/write stream.
Definition: ReadWriteStream.h:22
MimeType
Definition: WebConstants.h:54
HttpResponse * setCache(int maxAgeSeconds=3600, bool isPublic=false)
~HttpResponse()
Definition: HttpResponse.h:23
void setBuffer(ReadWriteStream *buffer)
Called by connection to specify where incoming response data is written.
Definition: HttpResponse.h:20