@@ -154,7 +154,7 @@ void corsCallback(HTTPRequest * req, HTTPResponse * res) {
154
154
void echoCallback (HTTPRequest * req, HTTPResponse * res) {
155
155
res->setHeader (" Content-Type" ," text/plain" );
156
156
byte buffer[256 ];
157
- while (!req->requestComplete ()) {
157
+ while (!( req->requestComplete () )) {
158
158
size_t s = req->readBytes (buffer, 256 );
159
159
res->write (buffer, s);
160
160
}
@@ -203,6 +203,8 @@ void setup()
203
203
// especially important for data that should be accessed by the main thread and
204
204
// the server.
205
205
Serial.println (" Creating server task... " );
206
+ // If stack canary errors occur, try to increase the stack size (3rd parameter)
207
+ // or to put as much stuff as possible onto the heap (ResourceNodes etc)
206
208
xTaskCreatePinnedToCore (serverTask, " https443" , 4096 , NULL , 1 , NULL , ARDUINO_RUNNING_CORE);
207
209
208
210
Serial.println (" Beginning to loop()..." );
@@ -240,56 +242,56 @@ void serverTask(void *params) {
240
242
// This means, it can be accessed by opening https://myesp/favicon.ico in all
241
243
// web browsers. Most browser fetch this file in background every time a new webserver
242
244
// is used to show the icon in the tab of that website.
243
- ResourceNode faviconNode = ResourceNode (" /favicon.ico" , " GET" , &faviconCallback);
245
+ ResourceNode * faviconNode = new ResourceNode (" /favicon.ico" , " GET" , &faviconCallback);
244
246
245
247
// The awesomeCallback is very similar to the favicon.
246
- ResourceNode awesomeNode = ResourceNode (" /images/awesome.svg" , " GET" , &awesomeCallback);
248
+ ResourceNode * awesomeNode = new ResourceNode (" /images/awesome.svg" , " GET" , &awesomeCallback);
247
249
248
250
// A simple callback showing URL parameters. Every asterisk (*) is a placeholder value
249
251
// So, the following URL has two placeholders that have to be filled.
250
252
// This is especially useful for REST-APIs where you want to represent an object ID in the
251
253
// url. Placeholders are arbitrary strings, but may be converted to integers (Error handling
252
254
// is up to the callback, eg. returning 404 if there is no suitable resource for that placeholder
253
255
// value)
254
- ResourceNode urlParamNode = ResourceNode (" /param/*/*" , " GET" , &urlParamCallback);
256
+ ResourceNode * urlParamNode = new ResourceNode (" /param/*/*" , " GET" , &urlParamCallback);
255
257
256
258
// The echoCallback is configured on the path /echo for POST and PUT requests. It just copies request
257
259
// body to response body. To enable it for both methods, two nodes have to be created:
258
- ResourceNode echoNodePost = ResourceNode (" /echo" , " POST" , &echoCallback);
259
- ResourceNode echoNodePut = ResourceNode (" /echo" , " PUT" , &echoCallback);
260
+ ResourceNode * echoNodePost = new ResourceNode (" /echo" , " POST" , &echoCallback);
261
+ ResourceNode * echoNodePut = new ResourceNode (" /echo" , " PUT" , &echoCallback);
260
262
261
263
// The root node (on GET /) will be called when no directory on the server is specified in
262
264
// the request, so this node can be accessed through https://myesp/
263
- ResourceNode rootNode = ResourceNode (" /" , " GET" , &testCallback);
265
+ ResourceNode * rootNode = new ResourceNode (" /" , " GET" , &testCallback);
264
266
265
267
// As mentioned above, we want to answer all OPTIONS requests with a response that allows
266
268
// cross-domain XHR. To do so, we bind the corsCallback to match all options request
267
269
// (we can exploit the asterisk functionality for this. The callback is not required to
268
270
// process the parameters in any way.)
269
271
// Note the difference to the "/" in the rootNode above - "/" matches ONLY that specific
270
272
// resource, while slash and asterisk is more or less provides a catch all behavior
271
- ResourceNode corsNode = ResourceNode (" /*" , " OPTIONS" , &corsCallback);
273
+ ResourceNode * corsNode = new ResourceNode (" /*" , " OPTIONS" , &corsCallback);
272
274
273
275
// The not found node will be used when no other node matches, and it's configured as
274
276
// defaultNode in the server.
275
277
// Note: Despite resource and method string have to be specified when a node is created,
276
278
// they are ignored for the default node. However, this makes it possible to register another
277
279
// node as default node as well.
278
- ResourceNode notFoundNode = ResourceNode (" /" , " GET" , ¬foundCallback);
280
+ ResourceNode * notFoundNode = new ResourceNode (" /" , " GET" , ¬foundCallback);
279
281
280
282
// Create the server. The constructor takes some optional parameters, eg. to specify the TCP
281
283
// port that should be used. However, defining a certificate is mandatory.
282
284
HTTPSServer server = HTTPSServer (&cert);
283
285
284
286
// Register the nodes that have been configured on the web server.
285
- server.setDefaultNode (& notFoundNode);
286
- server.registerNode (& rootNode);
287
- server.registerNode (& faviconNode);
288
- server.registerNode (& awesomeNode);
289
- server.registerNode (& urlParamNode);
290
- server.registerNode (& echoNodePost);
291
- server.registerNode (& echoNodePut);
292
- server.registerNode (& corsNode);
287
+ server.setDefaultNode (notFoundNode);
288
+ server.registerNode (rootNode);
289
+ server.registerNode (faviconNode);
290
+ server.registerNode (awesomeNode);
291
+ server.registerNode (urlParamNode);
292
+ server.registerNode (echoNodePost);
293
+ server.registerNode (echoNodePut);
294
+ server.registerNode (corsNode);
293
295
294
296
// The web server can be start()ed and stop()ed. When it's stopped, it will close its server port and
295
297
// all open connections and free the resources. Theoretically, it should be possible to run multiple
0 commit comments