10000 Added Post/Put example · gmos/esp32_https_server@d67d506 · GitHub
[go: up one dir, main page]

Skip to content

Commit d67d506

Browse files
committed
Added Post/Put example
1 parent 6030d03 commit d67d506

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

https_server.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void corsCallback(HTTPRequest * req, HTTPResponse * res) {
154154
void echoCallback(HTTPRequest * req, HTTPResponse * res) {
155155
res->setHeader("Content-Type","text/plain");
156156
byte buffer[256];
157-
while(!req->requestComplete()) {
157+
while(!(req->requestComplete())) {
158158
size_t s = req->readBytes(buffer, 256);
159159
res->write(buffer, s);
160160
}
@@ -203,6 +203,8 @@ void setup()
203203
// especially important for data that should be accessed by the main thread and
204204
// the server.
205205
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)
206208
xTaskCreatePinnedToCore(serverTask, "https443", 4096, NULL, 1, NULL, ARDUINO_RUNNING_CORE);
207209

208210
Serial.println("Beginning to loop()...");
@@ -240,56 +242,56 @@ void serverTask(void *params) {
240242
// This means, it can be accessed by opening https://myesp/favicon.ico in all
241243
// web browsers. Most browser fetch this file in background every time a new webserver
242244
// 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);
244246

245247
// 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);
247249

248250
// A simple callback showing URL parameters. Every asterisk (*) is a placeholder value
249251
// So, the following URL has two placeholders that have to be filled.
250252
// This is especially useful for REST-APIs where you want to represent an object ID in the
251253
// url. Placeholders are arbitrary strings, but may be converted to integers (Error handling
252254
// is up to the callback, eg. returning 404 if there is no suitable resource for that placeholder
253255
// value)
254-
ResourceNode urlParamNode = ResourceNode("/param/*/*", "GET", &urlParamCallback);
256+
ResourceNode * urlParamNode = new ResourceNode("/param/*/*", "GET", &urlParamCallback);
255257

256258
// The echoCallback is configured on the path /echo for POST and PUT requests. It just copies request
257259
// 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);
260262

261263
// The root node (on GET /) will be called when no directory on the server is specified in
262264
// the request, so this node can be accessed through https://myesp/
263-
ResourceNode rootNode = ResourceNode("/", "GET", &testCallback);
265+
ResourceNode * rootNode = new ResourceNode("/", "GET", &testCallback);
264266

265267
// As mentioned above, we want to answer all OPTIONS requests with a response that allows
266268
// cross-domain XHR. To do so, we bind the corsCallback to match all options request
267269
// (we can exploit the asterisk functionality for this. The callback is not required to
268270
// process the parameters in any way.)
269271
// Note the difference to the "/" in the rootNode above - "/" matches ONLY that specific
270272
// 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);
272274

273275
// The not found node will be used when no other node matches, and it's configured as
274276
// defaultNode in the server.
275277
// Note: Despite resource and method string have to be specified when a node is created,
276278
// they are ignored for the default node. However, this makes it possible to register another
277279
// node as default node as well.
278-
ResourceNode notFoundNode = ResourceNode("/", "GET", &notfoundCallback);
280+
ResourceNode * notFoundNode = new ResourceNode("/", "GET", &notfoundCallback);
279281

280282
// Create the server. The constructor takes some optional parameters, eg. to specify the TCP
281283
// port that should be used. However, defining a certificate is mandatory.
282284
HTTPSServer server = HTTPSServer(&cert);
283285

284286
// 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);
293295

294296
// The web server can be start()ed and stop()ed. When it's stopped, it will close its server port and
295297
// all open connections and free the resources. Theoretically, it should be possible to run multiple

0 commit comments

Comments
 (0)
0