HttpConnection.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  * HttpConnection.h
8  *
9  * @author: 2018 - Slavey Karadzhov <slav@attachix.com>
10  *
11  ****/
12 
13 #pragma once
14 
15 #include "../TcpClient.h"
16 #include "WString.h"
17 #include "HttpCommon.h"
18 #include "HttpResponse.h"
19 #include "HttpRequest.h"
20 #include "HttpHeaderBuilder.h"
21 
27 class HttpConnection : public TcpClient
28 {
29 public:
30  HttpConnection(http_parser_type type, bool autoDestruct = false) : TcpClient(autoDestruct)
31  {
32  init(type);
33  }
34 
35  HttpConnection(tcp_pcb* connection, http_parser_type type) : TcpClient(connection, nullptr, nullptr)
36  {
37  init(type);
38  }
39 
40  virtual void reset()
41  {
42  resetHeaders();
43  }
44 
45  virtual void cleanup()
46  {
47  reset();
48  }
49 
50  virtual void setDefaultParser();
51 
54 
55  using TcpClient::send;
56 
57  /* Overridden by HttpClientConnection */
58  virtual bool send(HttpRequest* request)
59  {
60  delete request;
61  return false;
62  }
63 
64  bool isActive();
65 
70  virtual HttpRequest* getRequest() = 0;
71 
77  {
78  return &response;
79  }
80 
81  // Backported for compatibility reasons
82 
87  {
88  return int(response.code);
89  }
90 
94  String getResponseHeader(const String& headerName, const String& defaultValue = nullptr) const SMING_DEPRECATED
95  {
96  return response.headers[headerName] ?: defaultValue;
97  }
98 
103  {
104  return response.headers;
105  }
106 
111  {
113  }
114 
119  {
120  return response.headers.getServerDate();
121  }
122 
127  {
128  return response.getBody();
129  }
130 
131 protected:
133  void resetHeaders();
134 
138  virtual void init(http_parser_type type);
139 
140  // HTTP parser methods
141 
146  virtual int onMessageBegin(http_parser* parser) = 0;
147 
152  virtual int onPath(const Url& uri)
153  {
154  return 0;
155  }
156 
161  virtual int onHeadersComplete(const HttpHeaders& headers) = 0;
162 
163 #ifndef COMPACT_MODE
164  virtual int onStatus(http_parser* parser)
165  {
166  return 0;
167  }
168 
169  virtual int onChunkHeader(http_parser* parser)
170  {
171  return 0;
172  }
173 
174  virtual int onChunkComplete(http_parser* parser)
175  {
176  return 0;
177  }
178 
179 #endif /* COMPACT MODE */
180 
186  virtual int onBody(const char* at, size_t length) = 0;
187 
192  virtual int onMessageComplete(http_parser* parser) = 0;
193 
198  virtual bool onProtocolUpgrade(http_parser* parser)
199  {
200  return true;
201  }
202 
208  virtual bool onHttpError(HttpError error);
209 
210  // TCP methods
211  virtual bool onTcpReceive(TcpClient& client, char* data, int size);
212 
213  void onError(err_t err) override;
214 
215 private:
216  // http_parser callback functions
217  static int staticOnMessageBegin(http_parser* parser);
218  static int staticOnPath(http_parser* parser, const char* at, size_t length);
219 #ifndef COMPACT_MODE
220  static int staticOnStatus(http_parser* parser, const char* at, size_t length);
221 #endif
222  static int staticOnHeadersComplete(http_parser* parser);
223  static int staticOnHeaderField(http_parser* parser, const char* at, size_t length);
224  static int staticOnHeaderValue(http_parser* parser, const char* at, size_t length);
225  static int staticOnBody(http_parser* parser, const char* at, size_t length);
226 #ifndef COMPACT_MODE
227  static int staticOnChunkHeader(http_parser* parser);
228  static int staticOnChunkComplete(http_parser* parser);
229 #endif
230  static int staticOnMessageComplete(http_parser* parser);
231 
232 protected:
233  http_parser parser;
234  static const http_parser_settings parserSettings;
238 
240 };
241 
virtual void reset()
Definition: HttpConnection.h:40
Date and time class.
Definition: DateTime.h:77
Class to manage URL instance.
Definition: Url.h:66
virtual int onBody(const char *at, size_t length)=0
Called when a piece of body data is received.
virtual void cleanup()
Definition: HttpConnection.h:45
IpAddress getRemoteIp() const
Definition: TcpConnection.h:102
HttpResponse response
Definition: HttpConnection.h:239
uint16_t getRemotePort() const
Definition: TcpConnection.h:107
virtual int onHeadersComplete(const HttpHeaders &headers)=0
Called when all headers are received.
virtual int onChunkHeader(http_parser *parser)
Definition: HttpConnection.h:169
virtual void init(http_parser_type type)
Initializes the http parser for a specific type of HTTP message.
virtual int onMessageBegin(http_parser *parser)=0
Called when a new incoming data is beginning to come.
virtual HttpRequest * getRequest()=0
Returns pointer to the current request.
DateTime getLastModifiedDate() const
Definition: HttpConnection.h:110
The String class.
Definition: WString.h:136
http_parser parser
Definition: HttpConnection.h:233
static const http_parser_settings parserSettings
Callback table for parser.
Definition: HttpConnection.h:234
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:34
virtual int onMessageComplete(http_parser *parser)=0
Called when the incoming data is complete.
virtual void setDefaultParser()
Definition: TcpClient.h:46
Definition: HttpCommon.h:85
HttpError
HTTP error codes.
Definition: HttpCommon.h:68
Re-assembles headers from fragments via onHeaderField / onHeaderValue callbacks.
Definition: HttpHeaderBuilder.h:19
HttpHeaders headers
Definition: HttpResponse.h:204
String getResponseHeader(const String &headerName, const String &defaultValue=nullptr) const
Definition: HttpConnection.h:94
#define SMING_DEPRECATED
Definition: sming_attr.h:30
virtual bool onTcpReceive(TcpClient &client, char *data, int size)
HttpHeaders & getResponseHeaders()
Definition: HttpConnection.h:102
bool send(const char *data, uint16_t len, bool forceCloseAfterSent=false)
virtual bool send(HttpRequest *request)
Definition: HttpConnection.h:58
virtual bool onHttpError(HttpError error)
Called when there was an error.
virtual bool onProtocolUpgrade(http_parser *parser)
Called when the HTTP protocol should be upgraded.
Definition: HttpConnection.h:198
virtual int onChunkComplete(http_parser *parser)
Definition: HttpConnection.h:174
int getResponseCode() const
Definition: HttpConnection.h:86
HttpConnection(tcp_pcb *connection, http_parser_type type)
Definition: HttpConnection.h:35
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:36
void resetHeaders()
Called after all headers have been received and processed.
HttpHeaderBuilder header
Header construction.
Definition: HttpConnection.h:235
HttpConnection(http_parser_type type, bool autoDestruct=false)
Definition: HttpConnection.h:30
Provides http base used for client and server connections.
Definition: HttpConnection.h:27
String getResponseString()
Definition: HttpConnection.h:126
HttpStatus code
The HTTP status response code.
Definition: HttpResponse.h:203
String getBody()
Moves content from the body stream into a String.
Definition: HttpResponse.h:149
DateTime getServerDate() const
Definition: HttpHeaders.h:119
virtual int onPath(const Url &uri)
Called when the URL path is known.
Definition: HttpConnection.h:152
DateTime getLastModifiedDate() const
Definition: HttpHeaders.h:112
void onError(err_t err) override
HttpResponse * getResponse()
Returns pointer to the current response.
Definition: HttpConnection.h:76
virtual int onStatus(http_parser *parser)
Definition: HttpConnection.h:164
HttpConnectionState
Identifies current state for an HTTP connection.
Definition: HttpCommon.h:84
DateTime getServerDate() const
Definition: HttpConnection.h:118
HttpConnectionState state
Definition: HttpConnection.h:237
Represents either an incoming or outgoing response to a HTTP request.
Definition: HttpResponse.h:25
HttpHeaders incomingHeaders
Full set of incoming headers.
Definition: HttpConnection.h:236