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 protected:
83  void resetHeaders();
84 
88  virtual void init(http_parser_type type);
89 
90  // HTTP parser methods
91 
96  virtual int onMessageBegin(http_parser* parser) = 0;
97 
102  virtual int onPath(const Url& uri)
103  {
104  return 0;
105  }
106 
111  virtual int onHeadersComplete(const HttpHeaders& headers) = 0;
112 
113 #ifndef COMPACT_MODE
114  virtual int onStatus(http_parser* parser)
115  {
116  return 0;
117  }
118 
119  virtual int onChunkHeader(http_parser* parser)
120  {
121  return 0;
122  }
123 
124  virtual int onChunkComplete(http_parser* parser)
125  {
126  return 0;
127  }
128 
129 #endif /* COMPACT MODE */
130 
136  virtual int onBody(const char* at, size_t length) = 0;
137 
142  virtual int onMessageComplete(http_parser* parser) = 0;
143 
148  virtual bool onProtocolUpgrade(http_parser* parser)
149  {
150  return true;
151  }
152 
158  virtual bool onHttpError(HttpError error);
159 
160  // TCP methods
161  virtual bool onTcpReceive(TcpClient& client, char* data, int size);
162 
163  void onError(err_t err) override;
164 
165 private:
166  // http_parser callback functions
167  static int staticOnMessageBegin(http_parser* parser);
168  static int staticOnPath(http_parser* parser, const char* at, size_t length);
169 #ifndef COMPACT_MODE
170  static int staticOnStatus(http_parser* parser, const char* at, size_t length);
171 #endif
172  static int staticOnHeadersComplete(http_parser* parser);
173  static int staticOnHeaderField(http_parser* parser, const char* at, size_t length);
174  static int staticOnHeaderValue(http_parser* parser, const char* at, size_t length);
175  static int staticOnBody(http_parser* parser, const char* at, size_t length);
176 #ifndef COMPACT_MODE
177  static int staticOnChunkHeader(http_parser* parser);
178  static int staticOnChunkComplete(http_parser* parser);
179 #endif
180  static int staticOnMessageComplete(http_parser* parser);
181 
182 protected:
183  http_parser parser;
184  static const http_parser_settings parserSettings;
188 
190 };
191 
HttpConnectionState
Identifies current state for an HTTP connection.
Definition: HttpCommon.h:84
@ eHCS_Ready
Definition: HttpCommon.h:85
HttpError
HTTP error codes.
Definition: HttpCommon.h:68
Provides http base used for client and server connections.
Definition: HttpConnection.h:28
void resetHeaders()
Called after all headers have been received and processed.
HttpResponse response
Definition: HttpConnection.h:189
HttpConnectionState state
Definition: HttpConnection.h:187
virtual int onChunkHeader(http_parser *parser)
Definition: HttpConnection.h:119
virtual int onStatus(http_parser *parser)
Definition: HttpConnection.h:114
virtual int onHeadersComplete(const HttpHeaders &headers)=0
Called when all headers are received.
virtual void reset()
Definition: HttpConnection.h:40
static const http_parser_settings parserSettings
Callback table for parser.
Definition: HttpConnection.h:184
virtual void cleanup()
Definition: HttpConnection.h:45
virtual bool send(HttpRequest *request)
Definition: HttpConnection.h:58
virtual void setDefaultParser()
HttpConnection(tcp_pcb *connection, http_parser_type type)
Definition: HttpConnection.h:35
virtual bool onHttpError(HttpError error)
Called when there was an error.
virtual int onPath(const Url &uri)
Called when the URL path is known.
Definition: HttpConnection.h:102
virtual int onMessageBegin(http_parser *parser)=0
Called when a new incoming data is beginning to come.
virtual bool onTcpReceive(TcpClient &client, char *data, int size)
HttpHeaderBuilder header
Header construction.
Definition: HttpConnection.h:185
http_parser parser
Definition: HttpConnection.h:183
virtual int onChunkComplete(http_parser *parser)
Definition: HttpConnection.h:124
virtual int onBody(const char *at, size_t length)=0
Called when a piece of body data is received.
void onError(err_t err) override
virtual HttpRequest * getRequest()=0
Returns pointer to the current request.
virtual void init(http_parser_type type)
Initializes the http parser for a specific type of HTTP message.
HttpConnection(http_parser_type type, bool autoDestruct=false)
Definition: HttpConnection.h:30
HttpHeaders incomingHeaders
Full set of incoming headers.
Definition: HttpConnection.h:186
HttpResponse * getResponse()
Returns pointer to the current response.
Definition: HttpConnection.h:76
virtual int onMessageComplete(http_parser *parser)=0
Called when the incoming data is complete.
virtual bool onProtocolUpgrade(http_parser *parser)
Called when the HTTP protocol should be upgraded.
Definition: HttpConnection.h:148
Re-assembles headers from fragments via onHeaderField / onHeaderValue callbacks.
Definition: HttpHeaderBuilder.h:20
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:35
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:37
Represents either an incoming or outgoing response to a HTTP request.
Definition: HttpResponse.h:26
Definition: TcpClient.h:46
bool send(const char *data, uint16_t len, bool forceCloseAfterSent=false)
uint16_t getRemotePort() const
Definition: TcpConnection.h:111
IpAddress getRemoteIp() const
Definition: TcpConnection.h:106
Class to manage URL instance.
Definition: Url.h:67