Webserver example for mbed OS 5 - HTTP 1.1 and multi-threaded

Dependencies:   mbed-http

mbed-os-example-http-server

This application demonstrates how to run an HTTP server on an mbed OS 5 device.

Request parsing is done through nodejs/http-parser.

To build

  1. Open mbed_app.json and change the network-interface option to your connectivity method (more info).
  2. Build the project in the online compiler or using mbed CLI.
  3. Flash the project to your development board.
  4. Attach a serial monitor to your board to see the debug messages.

Tested on

  • K64F with Ethernet.
  • NUCLEO_F411RE with ESP8266.

But every networking stack that supports the mbed OS 5 NetworkInterface API should work.

Committer:
Jan Jongboom
Date:
Mon Jul 31 17:12:21 2017 +0200
Revision:
1:fe3df398bdf5
Parent:
0:41f820ea137a
Compatibility with LwIP. Fix memory leaks.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jan Jongboom 0:41f820ea137a 1 #include "mbed.h"
Jan Jongboom 0:41f820ea137a 2 #include "easy-connect.h"
Jan Jongboom 0:41f820ea137a 3 #include "http_server.h"
Jan Jongboom 0:41f820ea137a 4 #include "http_response_builder.h"
Jan Jongboom 0:41f820ea137a 5
Jan Jongboom 0:41f820ea137a 6 Serial pc(USBTX, USBRX);
Jan Jongboom 0:41f820ea137a 7 DigitalOut led(LED1);
Jan Jongboom 0:41f820ea137a 8
Jan Jongboom 0:41f820ea137a 9 // Requests come in here
Jan Jongboom 0:41f820ea137a 10 void request_handler(ParsedHttpRequest* request, TCPSocket* socket) {
Jan Jongboom 0:41f820ea137a 11
Jan Jongboom 0:41f820ea137a 12 printf("Request came in: %s %s\n", http_method_str(request->get_method()), request->get_url().c_str());
Jan Jongboom 0:41f820ea137a 13
Jan Jongboom 0:41f820ea137a 14 if (request->get_method() == HTTP_GET && request->get_url() == "/") {
Jan Jongboom 0:41f820ea137a 15 HttpResponseBuilder builder(200);
Jan Jongboom 0:41f820ea137a 16 builder.set_header("Content-Type", "text/html; charset=utf-8");
Jan Jongboom 0:41f820ea137a 17
Jan Jongboom 0:41f820ea137a 18 char response[] = "<html><head><title>Hello from mbed</title></head>"
Jan Jongboom 0:41f820ea137a 19 "<body>"
Jan Jongboom 0:41f820ea137a 20 "<h1>mbed webserver</h1>"
Jan Jongboom 0:41f820ea137a 21 "<button id=\"toggle\">Toggle LED</button>"
Jan Jongboom 0:41f820ea137a 22 "<script>document.querySelector('#toggle').onclick = function() {"
Jan Jongboom 0:41f820ea137a 23 "var x = new XMLHttpRequest(); x.open('POST', '/toggle'); x.send();"
Jan Jongboom 0:41f820ea137a 24 "}</script>"
Jan Jongboom 0:41f820ea137a 25 "</body></html>";
Jan Jongboom 0:41f820ea137a 26
Jan Jongboom 0:41f820ea137a 27 builder.send(socket, response, sizeof(response) - 1);
Jan Jongboom 0:41f820ea137a 28 }
Jan Jongboom 0:41f820ea137a 29 else if (request->get_method() == HTTP_POST && request->get_url() == "/toggle") {
Jan Jongboom 0:41f820ea137a 30 printf("toggle LED called\n");
Jan Jongboom 0:41f820ea137a 31 led = !led;
Jan Jongboom 0:41f820ea137a 32
Jan Jongboom 0:41f820ea137a 33 HttpResponseBuilder builder(200);
Jan Jongboom 0:41f820ea137a 34 builder.send(socket, NULL, 0);
Jan Jongboom 0:41f820ea137a 35 }
Jan Jongboom 0:41f820ea137a 36 else {
Jan Jongboom 0:41f820ea137a 37 HttpResponseBuilder builder(404);
Jan Jongboom 0:41f820ea137a 38 builder.send(socket, NULL, 0);
Jan Jongboom 0:41f820ea137a 39 }
Jan Jongboom 0:41f820ea137a 40 }
Jan Jongboom 0:41f820ea137a 41
Jan Jongboom 0:41f820ea137a 42 int main() {
Jan Jongboom 0:41f820ea137a 43 pc.baud(115200);
Jan Jongboom 0:41f820ea137a 44
Jan Jongboom 0:41f820ea137a 45 // Connect to the network (see mbed_app.json for the connectivity method used)
Jan Jongboom 0:41f820ea137a 46 NetworkInterface* network = easy_connect(true);
Jan Jongboom 0:41f820ea137a 47 if (!network) {
Jan Jongboom 0:41f820ea137a 48 printf("Cannot connect to the network, see serial output\n");
Jan Jongboom 0:41f820ea137a 49 return 1;
Jan Jongboom 0:41f820ea137a 50 }
Jan Jongboom 0:41f820ea137a 51
Jan Jongboom 0:41f820ea137a 52 HttpServer server(network);
Jan Jongboom 0:41f820ea137a 53 nsapi_error_t res = server.start(8080, &request_handler);
Jan Jongboom 0:41f820ea137a 54
Jan Jongboom 0:41f820ea137a 55 if (res == NSAPI_ERROR_OK) {
Jan Jongboom 0:41f820ea137a 56 printf("Server is listening at http://%s:8080\n", network->get_ip_address());
Jan Jongboom 0:41f820ea137a 57 }
Jan Jongboom 0:41f820ea137a 58 else {
Jan Jongboom 0:41f820ea137a 59 printf("Server could not be started... %d\n", res);
Jan Jongboom 0:41f820ea137a 60 }
Jan Jongboom 0:41f820ea137a 61
Jan Jongboom 0:41f820ea137a 62 wait(osWaitForever);
Jan Jongboom 0:41f820ea137a 63 }