11
11
#include < JsonSerializer.h>
12
12
#include < JsonDeserializer.h>
13
13
14
- #define MAX_CONTENT_LENGTH 1024
15
14
#define HTTP_ENDPOINT_ORIGIN_ID " http"
16
15
17
16
template <class T >
@@ -22,8 +21,9 @@ class HttpGetEndpoint {
22
21
AsyncWebServer* server,
23
22
const String& servicePath,
24
23
SecurityManager* securityManager,
25
- AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN) :
26
- _jsonSerializer (jsonSerializer), _statefulService(statefulService) {
24
+ AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
25
+ size_t bufferSize = DEFAULT_BUFFER_SIZE) :
26
+ _jsonSerializer (jsonSerializer), _statefulService(statefulService), _bufferSize(bufferSize) {
27
27
server->on (servicePath.c_str (),
28
28
HTTP_GET,
29
29
securityManager->wrapRequest (std::bind (&HttpGetEndpoint::fetchSettings, this , std::placeholders::_1),
@@ -33,17 +33,19 @@ class HttpGetEndpoint {
33
33
HttpGetEndpoint (JsonSerializer<T> jsonSerializer,
34
34
StatefulService<T>* statefulService,
35
35
AsyncWebServer* server,
36
- const String& servicePath) :
37
- _jsonSerializer (jsonSerializer), _statefulService(statefulService) {
36
+ const String& servicePath,
37
+ size_t bufferSize = DEFAULT_BUFFER_SIZE) :
38
+ _jsonSerializer (jsonSerializer), _statefulService(statefulService), _bufferSize(bufferSize) {
38
39
server->on (servicePath.c_str (), HTTP_GET, std::bind (&HttpGetEndpoint::fetchSettings, this , std::placeholders::_1));
39
40
}
40
41
41
42
protected:
42
43
JsonSerializer<T> _jsonSerializer;
43
44
StatefulService<T>* _statefulService;
45
+ size_t _bufferSize;
44
46
45
47
void fetchSettings (AsyncWebServerRequest* request) {
46
- AsyncJsonResponse* response = new AsyncJsonResponse (false , MAX_CONTENT_LENGTH );
48
+ AsyncJsonResponse* response = new AsyncJsonResponse (false , _bufferSize );
47
49
JsonObject jsonObject = response->getRoot ().to <JsonObject>();
48
50
_statefulService->read (jsonObject, _jsonSerializer);
49
51
@@ -61,32 +63,36 @@ class HttpPostEndpoint {
61
63
AsyncWebServer* server,
62
64
const String& servicePath,
63
65
SecurityManager* securityManager,
64
- AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN) :
66
+ AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
67
+ size_t bufferSize = DEFAULT_BUFFER_SIZE) :
65
68
_jsonSerializer (jsonSerializer),
66
69
_jsonDeserializer (jsonDeserializer),
67
70
_statefulService (statefulService),
68
71
_updateHandler (
69
72
servicePath,
70
73
securityManager->wrapCallback (
71
74
std::bind (&HttpPostEndpoint::updateSettings, this , std::placeholders::_1, std::placeholders::_2),
72
- authenticationPredicate)) {
75
+ authenticationPredicate),
76
+ bufferSize),
77
+ _bufferSize(bufferSize) {
73
78
_updateHandler.setMethod (HTTP_POST);
74
- _updateHandler.setMaxContentLength (MAX_CONTENT_LENGTH);
75
79
server->addHandler (&_updateHandler);
76
80
}
77
81
78
82
HttpPostEndpoint (JsonSerializer<T> jsonSerializer,
79
83
JsonDeserializer<T> jsonDeserializer,
80
84
StatefulService<T>* statefulService,
81
85
AsyncWebServer* server,
82
- const String& servicePath) :
86
+ const String& servicePath,
87
+ size_t bufferSize = DEFAULT_BUFFER_SIZE) :
83
88
_jsonSerializer(jsonSerializer),
84
89
_jsonDeserializer(jsonDeserializer),
85
90
_statefulService(statefulService),
86
91
_updateHandler(servicePath,
87
- std::bind (&HttpPostEndpoint::updateSettings, this , std::placeholders::_1, std::placeholders::_2)) {
92
+ std::bind (&HttpPostEndpoint::updateSettings, this , std::placeholders::_1, std::placeholders::_2),
93
+ bufferSize),
94
+ _bufferSize(bufferSize) {
88
95
_updateHandler.setMethod (HTTP_POST);
89
- _updateHandler.setMaxContentLength (MAX_CONTENT_LENGTH);
90
96
server->addHandler (&_updateHandler);
91
97
}
92
98
@@ -95,19 +101,11 @@ class HttpPostEndpoint {
95
101
JsonDeserializer<T> _jsonDeserializer;
96
102
StatefulService<T>* _statefulService;
97
103
AsyncCallbackJsonWebHandler _updateHandler;
98
-
99
- void fetchSettings (AsyncWebServerRequest* request) {
100
- AsyncJsonResponse* response = new AsyncJsonResponse (false , MAX_CONTENT_LENGTH);
101
- JsonObject jsonObject = response->getRoot ().to <JsonObject>();
102
- _statefulService->read (jsonObject, _jsonSerializer);
103
-
104
- response->setLength ();
105
- request->send (response);
106
- }
104
+ size_t _bufferSize;
107
105
108
106
void updateSettings (AsyncWebServerRequest* request, JsonVariant& json) {
109
107
if (json.is <JsonObject>()) {
110
- AsyncJsonResponse* response = new AsyncJsonResponse (false , MAX_CONTENT_LENGTH );
108
+ AsyncJsonResponse* response = new AsyncJsonResponse (false , _bufferSize );
111
109
112
110
// use callback to update the settings once the response is complete
113
111
request->onDisconnect ([this ]() { _statefulService->callUpdateHandlers (HTTP_ENDPOINT_ORIGIN_ID); });
@@ -138,29 +136,33 @@ class HttpEndpoint : public HttpGetEndpoint<T>, public HttpPostEndpoint<T> {
138
136
AsyncWebServer* server,
139
137
const String& servicePath,
140
138
SecurityManager* securityManager,
141
- AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN) :
139
+ AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
140
+ size_t bufferSize = DEFAULT_BUFFER_SIZE) :
142
141
HttpGetEndpoint<T>(jsonSerializer,
143
142
statefulService,
144
143
server,
145
144
servicePath,
146
145
securityManager,
147
- authenticationPredicate),
146
+ authenticationPredicate,
147
+ bufferSize),
148
148
HttpPostEndpoint<T>(jsonSerializer,
149
149
jsonDeserializer,
150
150
statefulService,
151
151
server,
152
152
servicePath,
153
153
securityManager,
154
- authenticationPredicate) {
154
+ authenticationPredicate,
155
+ bufferSize) {
155
156
}
156
157
157
158
HttpEndpoint (JsonSerializer<T> jsonSerializer,
158
159
JsonDeserializer<T> jsonDeserializer,
159
160
StatefulService<T>* statefulService,
160
161
AsyncWebServer* server,
161
- const String& servicePath) :
162
- HttpGetEndpoint<T>(jsonSerializer, statefulService, server, servicePath),
163
- HttpPostEndpoint<T>(jsonSerializer, jsonDeserializer, statefulService, server, servicePath) {
162
+ const String& servicePath,
163
+ size_t bufferSize = DEFAULT_BUFFER_SIZE) :
164
+ HttpGetEndpoint<T>(jsonSerializer, statefulService, server, servicePath, bufferSize),
165
+ HttpPostEndpoint<T>(jsonSerializer, jsonDeserializer, statefulService, server, servicePath, bufferSize) {
164
166
}
165
167
};
166
168
0 commit comments