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
- Open
mbed_app.json
and change thenetwork-interface
option to your connectivity method (more info). - Build the project in the online compiler or using mbed CLI.
- Flash the project to your development board.
- Attach a serial monitor to your board to see the debug messages.
Tested on
- K64F with Ethernet.
- NUCLEO_F411RE with ESP8266.
- For ESP8266, you need this patch.
But every networking stack that supports the mbed OS 5 NetworkInterface API should work.
source/http_server.h@0:41f820ea137a, 2017-07-31 (annotated)
- Committer:
- Jan Jongboom
- Date:
- Mon Jul 31 15:41:22 2017 +0200
- Revision:
- 0:41f820ea137a
- Child:
- 1:fe3df398bdf5
Initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jan Jongboom |
0:41f820ea137a | 1 | /* |
Jan Jongboom |
0:41f820ea137a | 2 | * PackageLicenseDeclared: Apache-2.0 |
Jan Jongboom |
0:41f820ea137a | 3 | * Copyright (c) 2017 ARM Limited |
Jan Jongboom |
0:41f820ea137a | 4 | * |
Jan Jongboom |
0:41f820ea137a | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Jan Jongboom |
0:41f820ea137a | 6 | * you may not use this file except in compliance with the License. |
Jan Jongboom |
0:41f820ea137a | 7 | * You may obtain a copy of the License at |
Jan Jongboom |
0:41f820ea137a | 8 | * |
Jan Jongboom |
0:41f820ea137a | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jan Jongboom |
0:41f820ea137a | 10 | * |
Jan Jongboom |
0:41f820ea137a | 11 | * Unless required by applicable law or agreed to in writing, software |
Jan Jongboom |
0:41f820ea137a | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
Jan Jongboom |
0:41f820ea137a | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Jan Jongboom |
0:41f820ea137a | 14 | * See the License for the specific language governing permissions and |
Jan Jongboom |
0:41f820ea137a | 15 | * limitations under the License. |
Jan Jongboom |
0:41f820ea137a | 16 | */ |
Jan Jongboom |
0:41f820ea137a | 17 | |
Jan Jongboom |
0:41f820ea137a | 18 | #ifndef _HTTP_SERVER_ |
Jan Jongboom |
0:41f820ea137a | 19 | #define _HTTP_SERVER_ |
Jan Jongboom |
0:41f820ea137a | 20 | |
Jan Jongboom |
0:41f820ea137a | 21 | #include "mbed.h" |
Jan Jongboom |
0:41f820ea137a | 22 | #include "http_request_parser.h" |
Jan Jongboom |
0:41f820ea137a | 23 | #include "http_response.h" |
Jan Jongboom |
0:41f820ea137a | 24 | #include "http_response_builder.h" |
Jan Jongboom |
0:41f820ea137a | 25 | |
Jan Jongboom |
0:41f820ea137a | 26 | #define HTTP_SERVER_MAX_CONCURRENT 5 |
Jan Jongboom |
0:41f820ea137a | 27 | |
Jan Jongboom |
0:41f820ea137a | 28 | typedef HttpResponse ParsedHttpRequest; |
Jan Jongboom |
0:41f820ea137a | 29 | |
Jan Jongboom |
0:41f820ea137a | 30 | /** |
Jan Jongboom |
0:41f820ea137a | 31 | * \brief HttpServer implements the logic for setting up an HTTP server. |
Jan Jongboom |
0:41f820ea137a | 32 | */ |
Jan Jongboom |
0:41f820ea137a | 33 | class HttpServer { |
Jan Jongboom |
0:41f820ea137a | 34 | public: |
Jan Jongboom |
0:41f820ea137a | 35 | /** |
Jan Jongboom |
0:41f820ea137a | 36 | * HttpRequest Constructor |
Jan Jongboom |
0:41f820ea137a | 37 | * |
Jan Jongboom |
0:41f820ea137a | 38 | * @param[in] network The network interface |
Jan Jongboom |
0:41f820ea137a | 39 | */ |
Jan Jongboom |
0:41f820ea137a | 40 | HttpServer(NetworkInterface* network) : server(network) { |
Jan Jongboom |
0:41f820ea137a | 41 | |
Jan Jongboom |
0:41f820ea137a | 42 | } |
Jan Jongboom |
0:41f820ea137a | 43 | |
Jan Jongboom |
0:41f820ea137a | 44 | ~HttpServer() { |
Jan Jongboom |
0:41f820ea137a | 45 | for (size_t ix = 0; ix < HTTP_SERVER_MAX_CONCURRENT; ix++) { |
Jan Jongboom |
0:41f820ea137a | 46 | if (socket_threads[ix]) { |
Jan Jongboom |
0:41f820ea137a | 47 | delete socket_threads[ix]; |
Jan Jongboom |
0:41f820ea137a | 48 | } |
Jan Jongboom |
0:41f820ea137a | 49 | } |
Jan Jongboom |
0:41f820ea137a | 50 | } |
Jan Jongboom |
0:41f820ea137a | 51 | |
Jan Jongboom |
0:41f820ea137a | 52 | /** |
Jan Jongboom |
0:41f820ea137a | 53 | * Start listening |
Jan Jongboom |
0:41f820ea137a | 54 | * |
Jan Jongboom |
0:41f820ea137a | 55 | * @param[in] port The port to listen on |
Jan Jongboom |
0:41f820ea137a | 56 | */ |
Jan Jongboom |
0:41f820ea137a | 57 | nsapi_error_t bind(uint16_t port) { |
Jan Jongboom |
0:41f820ea137a | 58 | server.listen(HTTP_SERVER_MAX_CONCURRENT); // max. concurrent connections... |
Jan Jongboom |
0:41f820ea137a | 59 | return server.bind(port); |
Jan Jongboom |
0:41f820ea137a | 60 | } |
Jan Jongboom |
0:41f820ea137a | 61 | |
Jan Jongboom |
0:41f820ea137a | 62 | /** |
Jan Jongboom |
0:41f820ea137a | 63 | * Start running the server (it will run on it's own thread) |
Jan Jongboom |
0:41f820ea137a | 64 | */ |
Jan Jongboom |
0:41f820ea137a | 65 | nsapi_error_t start(uint16_t port, Callback<void(ParsedHttpRequest* request, TCPSocket* socket)> a_handler) { |
Jan Jongboom |
0:41f820ea137a | 66 | server.listen(HTTP_SERVER_MAX_CONCURRENT); // max. concurrent connections... |
Jan Jongboom |
0:41f820ea137a | 67 | |
Jan Jongboom |
0:41f820ea137a | 68 | nsapi_error_t ret = server.bind(port); |
Jan Jongboom |
0:41f820ea137a | 69 | if (ret != NSAPI_ERROR_OK) { |
Jan Jongboom |
0:41f820ea137a | 70 | return ret; |
Jan Jongboom |
0:41f820ea137a | 71 | } |
Jan Jongboom |
0:41f820ea137a | 72 | |
Jan Jongboom |
0:41f820ea137a | 73 | handler = a_handler; |
Jan Jongboom |
0:41f820ea137a | 74 | |
Jan Jongboom |
0:41f820ea137a | 75 | main_thread.start(callback(this, &HttpServer::main)); |
Jan Jongboom |
0:41f820ea137a | 76 | |
Jan Jongboom |
0:41f820ea137a | 77 | return NSAPI_ERROR_OK; |
Jan Jongboom |
0:41f820ea137a | 78 | } |
Jan Jongboom |
0:41f820ea137a | 79 | |
Jan Jongboom |
0:41f820ea137a | 80 | private: |
Jan Jongboom |
0:41f820ea137a | 81 | |
Jan Jongboom |
0:41f820ea137a | 82 | void receive_data() { |
Jan Jongboom |
0:41f820ea137a | 83 | // UNSAFE: should Mutex around it or something |
Jan Jongboom |
0:41f820ea137a | 84 | TCPSocket* socket = sockets.back(); |
Jan Jongboom |
0:41f820ea137a | 85 | |
Jan Jongboom |
0:41f820ea137a | 86 | // needs to keep running until the socket gets closed |
Jan Jongboom |
0:41f820ea137a | 87 | while (1) { |
Jan Jongboom |
0:41f820ea137a | 88 | |
Jan Jongboom |
0:41f820ea137a | 89 | ParsedHttpRequest* response = new ParsedHttpRequest(); |
Jan Jongboom |
0:41f820ea137a | 90 | HttpParser* parser = new HttpParser(response, HTTP_REQUEST); |
Jan Jongboom |
0:41f820ea137a | 91 | |
Jan Jongboom |
0:41f820ea137a | 92 | // Set up a receive buffer (on the heap) |
Jan Jongboom |
0:41f820ea137a | 93 | uint8_t* recv_buffer = (uint8_t*)malloc(HTTP_RECEIVE_BUFFER_SIZE); |
Jan Jongboom |
0:41f820ea137a | 94 | |
Jan Jongboom |
0:41f820ea137a | 95 | // TCPSocket::recv is called until we don't have any data anymore |
Jan Jongboom |
0:41f820ea137a | 96 | nsapi_size_or_error_t recv_ret; |
Jan Jongboom |
0:41f820ea137a | 97 | while ((recv_ret = socket->recv(recv_buffer, HTTP_RECEIVE_BUFFER_SIZE)) > 0) { |
Jan Jongboom |
0:41f820ea137a | 98 | // Pass the chunk into the http_parser |
Jan Jongboom |
0:41f820ea137a | 99 | size_t nparsed = parser->execute((const char*)recv_buffer, recv_ret); |
Jan Jongboom |
0:41f820ea137a | 100 | if (nparsed != recv_ret) { |
Jan Jongboom |
0:41f820ea137a | 101 | printf("Parsing failed... parsed %d bytes, received %d bytes\n", nparsed, recv_ret); |
Jan Jongboom |
0:41f820ea137a | 102 | recv_ret = -2101; |
Jan Jongboom |
0:41f820ea137a | 103 | break; |
Jan Jongboom |
0:41f820ea137a | 104 | } |
Jan Jongboom |
0:41f820ea137a | 105 | |
Jan Jongboom |
0:41f820ea137a | 106 | if (response->is_message_complete()) { |
Jan Jongboom |
0:41f820ea137a | 107 | break; |
Jan Jongboom |
0:41f820ea137a | 108 | } |
Jan Jongboom |
0:41f820ea137a | 109 | } |
Jan Jongboom |
0:41f820ea137a | 110 | // error? |
Jan Jongboom |
0:41f820ea137a | 111 | if (recv_ret < 0) { |
Jan Jongboom |
0:41f820ea137a | 112 | printf("Error reading from socket %d\n", recv_ret); |
Jan Jongboom |
0:41f820ea137a | 113 | |
Jan Jongboom |
0:41f820ea137a | 114 | // error = recv_ret; |
Jan Jongboom |
0:41f820ea137a | 115 | delete response; |
Jan Jongboom |
0:41f820ea137a | 116 | delete parser; |
Jan Jongboom |
0:41f820ea137a | 117 | free(recv_buffer); |
Jan Jongboom |
0:41f820ea137a | 118 | |
Jan Jongboom |
0:41f820ea137a | 119 | // q; should we always break out of the thread or only if NO_SOCKET ? |
Jan Jongboom |
0:41f820ea137a | 120 | // should we delete socket here? the socket seems already gone... |
Jan Jongboom |
0:41f820ea137a | 121 | if (recv_ret < -3000) { |
Jan Jongboom |
0:41f820ea137a | 122 | return; |
Jan Jongboom |
0:41f820ea137a | 123 | } |
Jan Jongboom |
0:41f820ea137a | 124 | else { |
Jan Jongboom |
0:41f820ea137a | 125 | continue; |
Jan Jongboom |
0:41f820ea137a | 126 | } |
Jan Jongboom |
0:41f820ea137a | 127 | } |
Jan Jongboom |
0:41f820ea137a | 128 | |
Jan Jongboom |
0:41f820ea137a | 129 | // When done, call parser.finish() |
Jan Jongboom |
0:41f820ea137a | 130 | parser->finish(); |
Jan Jongboom |
0:41f820ea137a | 131 | |
Jan Jongboom |
0:41f820ea137a | 132 | // Free the receive buffer |
Jan Jongboom |
0:41f820ea137a | 133 | free(recv_buffer); |
Jan Jongboom |
0:41f820ea137a | 134 | |
Jan Jongboom |
0:41f820ea137a | 135 | // Let user application handle the request, if user needs a handle to response they need to memcpy themselves |
Jan Jongboom |
0:41f820ea137a | 136 | handler(response, socket); |
Jan Jongboom |
0:41f820ea137a | 137 | |
Jan Jongboom |
0:41f820ea137a | 138 | // Free the response and parser |
Jan Jongboom |
0:41f820ea137a | 139 | delete response; |
Jan Jongboom |
0:41f820ea137a | 140 | delete parser; |
Jan Jongboom |
0:41f820ea137a | 141 | } |
Jan Jongboom |
0:41f820ea137a | 142 | } |
Jan Jongboom |
0:41f820ea137a | 143 | |
Jan Jongboom |
0:41f820ea137a | 144 | void main() { |
Jan Jongboom |
0:41f820ea137a | 145 | while (1) { |
Jan Jongboom |
0:41f820ea137a | 146 | TCPSocket* clt_sock = new TCPSocket(); // Q: when should these be cleared? When not connected anymore? |
Jan Jongboom |
0:41f820ea137a | 147 | SocketAddress clt_addr; |
Jan Jongboom |
0:41f820ea137a | 148 | |
Jan Jongboom |
0:41f820ea137a | 149 | nsapi_error_t accept_res = server.accept(clt_sock, &clt_addr); |
Jan Jongboom |
0:41f820ea137a | 150 | if (accept_res == NSAPI_ERROR_OK) { |
Jan Jongboom |
0:41f820ea137a | 151 | sockets.push_back(clt_sock); // so we can clear our disconnected sockets |
Jan Jongboom |
0:41f820ea137a | 152 | |
Jan Jongboom |
0:41f820ea137a | 153 | // and start listening for events there |
Jan Jongboom |
0:41f820ea137a | 154 | Thread* t = new Thread(osPriorityNormal, 2048); |
Jan Jongboom |
0:41f820ea137a | 155 | t->start(callback(this, &HttpServer::receive_data)); |
Jan Jongboom |
0:41f820ea137a | 156 | |
Jan Jongboom |
0:41f820ea137a | 157 | socket_threads.push_back(t); |
Jan Jongboom |
0:41f820ea137a | 158 | } |
Jan Jongboom |
0:41f820ea137a | 159 | |
Jan Jongboom |
0:41f820ea137a | 160 | for (size_t ix = 0; ix < socket_threads.size(); ix++) { |
Jan Jongboom |
0:41f820ea137a | 161 | if (socket_threads[ix]->get_state() == Thread::Deleted) { |
Jan Jongboom |
0:41f820ea137a | 162 | delete socket_threads[ix]; |
Jan Jongboom |
0:41f820ea137a | 163 | socket_threads.erase(socket_threads.begin() + ix); // what if there are two? |
Jan Jongboom |
0:41f820ea137a | 164 | } |
Jan Jongboom |
0:41f820ea137a | 165 | } |
Jan Jongboom |
0:41f820ea137a | 166 | |
Jan Jongboom |
0:41f820ea137a | 167 | } |
Jan Jongboom |
0:41f820ea137a | 168 | } |
Jan Jongboom |
0:41f820ea137a | 169 | |
Jan Jongboom |
0:41f820ea137a | 170 | TCPServer server; |
Jan Jongboom |
0:41f820ea137a | 171 | Thread main_thread; |
Jan Jongboom |
0:41f820ea137a | 172 | vector<TCPSocket*> sockets; |
Jan Jongboom |
0:41f820ea137a | 173 | vector<Thread*> socket_threads; |
Jan Jongboom |
0:41f820ea137a | 174 | Callback<void(ParsedHttpRequest* request, TCPSocket* socket)> handler = 0; |
Jan Jongboom |
0:41f820ea137a | 175 | }; |
Jan Jongboom |
0:41f820ea137a | 176 | |
Jan Jongboom |
0:41f820ea137a | 177 | #endif // _HTTP_SERVER |