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 
71  using TcpClient::send;
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  // HTTP parser methods
90 
91  int onMessageBegin(http_parser* parser) override;
92  int onPath(const Url& path) override;
93  int onHeadersComplete(const HttpHeaders& headers) override;
94  int onBody(const char* at, size_t length) override;
95  int onMessageComplete(http_parser* parser) override;
96 
97  bool onProtocolUpgrade(http_parser* parser) override
98  {
99  if(upgradeCallback) {
100  return upgradeCallback();
101  }
102 
103  return true;
104  }
105 
106  bool onHttpError(HttpError error) override;
107 
108  // TCP methods
109  void onReadyToSendData(TcpConnectionEvent sourceEvent) override;
110  virtual void sendError(const String& message = nullptr, HttpStatus code = HTTP_STATUS_BAD_REQUEST);
111 
112 private:
113  void sendResponseHeaders(HttpResponse* response);
114  bool sendResponseBody(HttpResponse* response);
115 
116 public:
117  void* userData = nullptr;
118 
119 private:
120  HttpResourceTree* resourceTree = nullptr;
121  HttpResource* resource = nullptr;
122 
123  HttpRequest request;
124 
125  HttpResourceDelegate headersCompleteDelegate = nullptr;
126  HttpResourceDelegate requestCompletedDelegate = nullptr;
127  HttpServerConnectionBodyDelegate onBodyDelegate = nullptr;
128  HttpServerProtocolUpgradeCallback upgradeCallback = nullptr;
129 
130  const BodyParsers* bodyParsers = nullptr;
131  HttpBodyParserDelegate bodyParser = nullptr;
132  bool closeOnContentError = false;
133  bool hasContentError = false;
134 };
135 
~HttpServerConnection()
Definition: HttpServerConnection.h:40
Class to manage URL instance.
Definition: Url.h:66
HashMap class template.
Definition: WHashMap.h:39
int onMessageBegin(http_parser *parser) override
Called when a new incoming data is beginning to come.
HttpResponse response
Definition: HttpConnection.h:239
void close() override
Occurs on data receive.
Definition: TcpConnection.h:27
Definition: TcpConnection.h:29
int onMessageComplete(http_parser *parser) override
Called when the incoming data is complete.
Definition: HttpServerConnection.h:33
The String class.
Definition: WString.h:136
http_parser parser
Definition: HttpConnection.h:233
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:34
bool onProtocolUpgrade(http_parser *parser) override
Called when the HTTP protocol should be upgraded.
Definition: HttpServerConnection.h:97
const int PARSE_DATAEND
End of incoming data.
Definition: HttpBodyParser.h:25
Definition: Delegate.h:20
TcpConnectionEvent
Definition: TcpConnection.h:25
Definition: HttpCommon.h:85
HttpError
HTTP error codes.
Definition: HttpCommon.h:68
int onHeadersComplete(const HttpHeaders &headers) override
Called when all headers are received.
Instances of this class are registered with an HttpServer for a specific URL.
Definition: HttpResource.h:34
bool send(const char *data, uint16_t len, bool forceCloseAfterSent=false)
virtual void sendError(const String &message=nullptr, HttpStatus code=HTTP_STATUS_BAD_REQUEST)
void onReadyToSendData(TcpConnectionEvent sourceEvent) override
void * args
Definition: HttpRequest.h:289
HttpServerConnection(tcp_pcb *clientTcp)
Definition: HttpServerConnection.h:36
void setResourceTree(HttpResourceTree *resourceTree)
Definition: HttpServerConnection.h:51
void setBodyParsers(const BodyParsers *bodyParsers)
Definition: HttpServerConnection.h:56
void * userData
use to pass user data between requests
Definition: HttpServerConnection.h:117
int onBody(const char *at, size_t length) override
Called when a piece of body data is received.
Class to map URL paths to classes which handle them.
Definition: HttpResourceTree.h:26
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:36
HttpRequest * getRequest() override
Returns pointer to the current request.
Definition: HttpServerConnection.h:78
virtual void shutdown(HttpServerConnection &connection)
Takes care to cleanup the connection.
Definition: HttpResource.h:44
void setCloseOnContentError(bool close=true)
Definition: HttpServerConnection.h:83
Provides http base used for client and server connections.
Definition: HttpConnection.h:27
int onPath(const Url &path) override
Called when the URL path is known.
HttpStatus
HTTP status code.
Definition: HttpCommon.h:55
void setUpgradeCallback(HttpServerProtocolUpgradeCallback callback)
Definition: HttpServerConnection.h:73
void send()
Definition: HttpServerConnection.h:61
Definition: HttpCommon.h:86
HttpConnectionState state
Definition: HttpConnection.h:237
Represents either an incoming or outgoing response to a HTTP request.
Definition: HttpResponse.h:25
bool onHttpError(HttpError error) override
Called when there was an error.