HttpServerConnection.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  * HttpServerConnection.h
8  *
9  * Modified: 2017 - Slavey Karadzhov <slav@attachix.com>
10  *
11  ****/
12 
13 #pragma once
14 
15 #include "HttpConnection.h"
16 #include "HttpResource.h"
17 #include "HttpBodyParser.h"
18 
19 #include <functional>
20 
26 class HttpResourceTree;
28 
30 
32 
34 {
35 public:
36  HttpServerConnection(tcp_pcb* clientTcp) : HttpConnection(clientTcp, HTTP_REQUEST)
37  {
38  }
39 
41  {
42  if(this->resource != nullptr) {
43  this->resource->shutdown(*this);
44  }
45 
46  if(bodyParser && request.args != nullptr) {
47  bodyParser(request, nullptr, PARSE_DATAEND);
48  }
49  }
50 
51  void setResourceTree(HttpResourceTree* resourceTree)
52  {
53  this->resourceTree = resourceTree;
54  }
55 
56  void setBodyParsers(const BodyParsers* bodyParsers)
57  {
58  this->bodyParsers = bodyParsers;
59  }
60 
61  void send()
62  {
63  if(state == eHCS_Ready) {
66  } else {
68  }
69  }
70 
72 
74  {
75  upgradeCallback = callback;
76  }
77 
78  HttpRequest* getRequest() override
79  {
80  return &request;
81  }
82 
83  void setCloseOnContentError(bool close = true)
84  {
85  closeOnContentError = close;
86  }
87 
88 protected:
89  bool send(HttpRequest* request) override
90  {
91  return HttpConnection::send(request);
92  }
93 
94  // HTTP parser methods
95 
96  int onMessageBegin(http_parser* parser) override;
97  int onPath(const Url& path) override;
98  int onHeadersComplete(const HttpHeaders& headers) override;
99  int onBody(const char* at, size_t length) override;
100  int onMessageComplete(http_parser* parser) override;
101 
102  bool onProtocolUpgrade(http_parser* parser) override
103  {
104  if(upgradeCallback) {
105  return upgradeCallback();
106  }
107 
108  return true;
109  }
110 
111  bool onHttpError(HttpError error) override;
112 
113  // TCP methods
114  void onReadyToSendData(TcpConnectionEvent sourceEvent) override;
115  virtual void sendError(const String& message = nullptr, HttpStatus code = HTTP_STATUS_BAD_REQUEST);
116 
117 private:
118  void sendResponseHeaders(HttpResponse* response);
119  bool sendResponseBody(HttpResponse* response);
120 
121 public:
122  void* userData = nullptr;
123 
124 private:
125  HttpResourceTree* resourceTree = nullptr;
126  HttpResource* resource = nullptr;
127 
128  HttpRequest request;
129 
130  HttpResourceDelegate headersCompleteDelegate = nullptr;
131  HttpResourceDelegate requestCompletedDelegate = nullptr;
132  HttpServerConnectionBodyDelegate onBodyDelegate = nullptr;
133  HttpServerProtocolUpgradeCallback upgradeCallback = nullptr;
134 
135  const BodyParsers* bodyParsers = nullptr;
136  HttpBodyParserDelegate bodyParser = nullptr;
137  bool closeOnContentError = false;
138  bool hasContentError = false;
139 };
140 
const int PARSE_DATAEND
End of incoming data.
Definition: HttpBodyParser.h:25
@ eHCS_StartSending
Definition: HttpCommon.h:86
@ eHCS_Ready
Definition: HttpCommon.h:85
HttpStatus
HTTP status code.
Definition: HttpCommon.h:55
HttpError
HTTP error codes.
Definition: HttpCommon.h:68
Definition: Delegate.h:20
Provides http base used for client and server connections.
Definition: HttpConnection.h:28
HttpResponse response
Definition: HttpConnection.h:189
HttpConnectionState state
Definition: HttpConnection.h:187
bool send(const char *data, uint16_t len, bool forceCloseAfterSent=false)
http_parser parser
Definition: HttpConnection.h:183
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:35
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:37
void * args
Used to store data that should be valid during a single request.
Definition: HttpRequest.h:292
Class to map URL paths to classes which handle them.
Definition: HttpResourceTree.h:27
Instances of this class are registered with an HttpServer for a specific URL.
Definition: HttpResource.h:34
virtual void shutdown(HttpServerConnection &connection)
Takes care to cleanup the connection.
Definition: HttpResource.h:43
Represents either an incoming or outgoing response to a HTTP request.
Definition: HttpResponse.h:26
Definition: HttpServerConnection.h:34
~HttpServerConnection()
Definition: HttpServerConnection.h:40
void setBodyParsers(const BodyParsers *bodyParsers)
Definition: HttpServerConnection.h:56
void onReadyToSendData(TcpConnectionEvent sourceEvent) override
HttpRequest * getRequest() override
Returns pointer to the current request.
Definition: HttpServerConnection.h:78
int onMessageComplete(http_parser *parser) override
Called when the incoming data is complete.
bool send(HttpRequest *request) override
Definition: HttpServerConnection.h:89
int onPath(const Url &path) override
Called when the URL path is known.
void * userData
use to pass user data between requests
Definition: HttpServerConnection.h:122
HttpServerConnection(tcp_pcb *clientTcp)
Definition: HttpServerConnection.h:36
int onBody(const char *at, size_t length) override
Called when a piece of body data is received.
int onMessageBegin(http_parser *parser) override
Called when a new incoming data is beginning to come.
void setResourceTree(HttpResourceTree *resourceTree)
Definition: HttpServerConnection.h:51
bool onProtocolUpgrade(http_parser *parser) override
Called when the HTTP protocol should be upgraded.
Definition: HttpServerConnection.h:102
void setUpgradeCallback(HttpServerProtocolUpgradeCallback callback)
Definition: HttpServerConnection.h:73
bool onHttpError(HttpError error) override
Called when there was an error.
void send()
Definition: HttpServerConnection.h:61
virtual void sendError(const String &message=nullptr, HttpStatus code=HTTP_STATUS_BAD_REQUEST)
void setCloseOnContentError(bool close=true)
Definition: HttpServerConnection.h:83
int onHeadersComplete(const HttpHeaders &headers) override
Called when all headers are received.
The String class.
Definition: WString.h:137
void close() override
Class to manage URL instance.
Definition: Url.h:67
TcpConnectionEvent
Definition: TcpConnection.h:26
@ eTCE_Received
Occurs on data receive.
Definition: TcpConnection.h:28
@ eTCE_Poll
Definition: TcpConnection.h:30