Webserver example for Nuvoton NuMaker boards and mbed OS 5.15 - HTTP 1.1 and multi-threaded.
For Mbed OS 6, please use the NuMaker-simple-httpd example.
This application demonstrates how to run an HTTP server on an mbed OS 5 device. It is derived from http-webserver-example Request parsing is done through nodejs/http-parser.
Fixed for Mbed OS 5.15 or later
Tested on
NuMaker IoT-M487 with Ethernet NuMaker PFM-M487 with Ethernet
Diff: source/http_server.h
- Revision:
- 2:ff1a293c4df3
- Parent:
- 1:fe3df398bdf5
--- a/source/http_server.h Mon Jul 31 17:12:21 2017 +0200 +++ b/source/http_server.h Mon Jun 29 08:17:16 2020 +0000 @@ -55,7 +55,7 @@ * Start running the server (it will run on it's own thread) */ nsapi_error_t start(uint16_t port, Callback<void(ParsedHttpRequest* request, TCPSocket* socket)> a_handler) { - server = new TCPServer(); + server = new TCPSocket(); nsapi_error_t ret; @@ -148,10 +148,12 @@ void main() { while (1) { - TCPSocket* clt_sock = new TCPSocket(); // Q: when should these be cleared? When not connected anymore? - SocketAddress clt_addr; - - nsapi_error_t accept_res = server->accept(clt_sock, &clt_addr); + TCPSocket* clt_sock; // = new TCPSocket(); // Q: when should these be cleared? When not connected anymore? + //SocketAddress clt_addr; + nsapi_error_t accept_res; + + //nsapi_error_t accept_res = server->accept(clt_sock, &clt_addr); + clt_sock = server->accept(&accept_res); if (accept_res == NSAPI_ERROR_OK) { sockets.push_back(clt_sock); // so we can clear our disconnected sockets @@ -165,14 +167,15 @@ socket_threads.push_back(m); } else { - delete clt_sock; + //delete clt_sock; } for (size_t ix = 0; ix < socket_threads.size(); ix++) { if (socket_threads[ix]->thread->get_state() == Thread::Deleted) { socket_threads[ix]->thread->terminate(); delete socket_threads[ix]->thread; - delete socket_threads[ix]->socket; // does this work on esp8266? + //delete socket_threads[ix]->socket; // does this work on esp8266? + socket_threads[ix]->socket->close(); socket_threads.erase(socket_threads.begin() + ix); // what if there are two? } } @@ -185,7 +188,7 @@ Thread* thread; } socket_thread_metadata_t; - TCPServer* server; + TCPSocket* server; NetworkInterface* _network; Thread main_thread; vector<TCPSocket*> sockets;