HTTP and HTTPS example application for Mbed OS 5
Dependencies: mbed-http
This application demonstrates how to make HTTP and HTTPS requests and parse the response from Mbed OS 5.
It consists of six example applications, which you can select in source/select-demo.h:
- HTTP:
- Does a GET request to http:httpbin.org/status/418.
- Does a POST request to http:httpbin.org/post.
- HTTPS:
- Does a GET request to https:os.mbed.com/media/uploads/mbed_official/hello.txt.
- Does a POST request to https:httpbin.org/post.
- HTTP with socket re-use.
- HTTPS with socket re-use.
- HTTP over IPv6.
- HTTPS with chunked requests.
Response parsing is done through nodejs/http-parser.
Note: HTTPS requests do not work on targets with less than 128K of RAM due to the size of the TLS handshake. For more background see mbed-http.
To build
- If you're using WiFi, specify the credentials in
mbed_app.json. - 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.
Defining the network interface
This application uses the on-board network interface for your board. If you use an external network interface (f.e. a WiFi module) you need to add the driver to this project. Then, open network-helper.h and specify which network driver to use.
More information is in the Mbed OS documentation under IP Networking.
Entropy (or lack thereof)
On all platforms that do not have the TRNG feature, the application is compiled without TLS entropy sources. This means that your code is inherently unsafe and should not be deployed to any production systems. To enable entropy, remove the MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY macros from mbed_app.json.
Flash size
Default flash size for HTTPS is very large, as the application is loading the default Mbed TLS configuration. To use a more optimized version, you can disable unused cypher suites and other Mbed TLS features with a custom configuration file. Create a new configuration file, then add in mbed_app.json:
"MBEDTLS_CONFIG_FILE=\"mbedtls_config.h\""
to the macros array.
Running tests
You can run the integration tests from this project via Mbed CLI.
- In
select-demo.hset theDEMOmacro toDEMO_TESTS. - Set your WiFi credentials in
mbed_app.json. - Then run the tests via:
$ mbed test -v -n mbed-http-tests-tests-*
Tested on
- K64F with Ethernet.
- NUCLEO_F411RE with ESP8266 (not working on Mbed OS 5.12+)
- ODIN-W2 with WiFi.
- K64F with Atmel 6LoWPAN shield.
- DISCO-L475VG-IOT01A with WiFi (requires the wifi-ism43362 driver).
source/mbed-http/http_response_parser.h@1:3bff14db67c7, 2017-02-16 (annotated)
- Committer:
- Jan Jongboom
- Date:
- Thu Feb 16 11:13:40 2017 +0100
- Revision:
- 1:3bff14db67c7
Add mbed-http
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Jan Jongboom |
1:3bff14db67c7 | 1 | #ifndef _HTTP_RESPONSE_PARSER_H_ |
| Jan Jongboom |
1:3bff14db67c7 | 2 | #define _HTTP_RESPONSE_PARSER_H_ |
| Jan Jongboom |
1:3bff14db67c7 | 3 | |
| Jan Jongboom |
1:3bff14db67c7 | 4 | #include "http_parser.h" |
| Jan Jongboom |
1:3bff14db67c7 | 5 | #include "http_response.h" |
| Jan Jongboom |
1:3bff14db67c7 | 6 | |
| Jan Jongboom |
1:3bff14db67c7 | 7 | class HttpResponseParser { |
| Jan Jongboom |
1:3bff14db67c7 | 8 | public: |
| Jan Jongboom |
1:3bff14db67c7 | 9 | HttpResponseParser(HttpResponse* a_response, Callback<void(const char *at, size_t length)> a_body_callback = 0) |
| Jan Jongboom |
1:3bff14db67c7 | 10 | : response(a_response), body_callback(a_body_callback) |
| Jan Jongboom |
1:3bff14db67c7 | 11 | { |
| Jan Jongboom |
1:3bff14db67c7 | 12 | settings = new http_parser_settings(); |
| Jan Jongboom |
1:3bff14db67c7 | 13 | |
| Jan Jongboom |
1:3bff14db67c7 | 14 | settings->on_message_begin = &HttpResponseParser::on_message_begin_callback; |
| Jan Jongboom |
1:3bff14db67c7 | 15 | settings->on_url = &HttpResponseParser::on_url_callback; |
| Jan Jongboom |
1:3bff14db67c7 | 16 | settings->on_status = &HttpResponseParser::on_status_callback; |
| Jan Jongboom |
1:3bff14db67c7 | 17 | settings->on_header_field = &HttpResponseParser::on_header_field_callback; |
| Jan Jongboom |
1:3bff14db67c7 | 18 | settings->on_header_value = &HttpResponseParser::on_header_value_callback; |
| Jan Jongboom |
1:3bff14db67c7 | 19 | settings->on_headers_complete = &HttpResponseParser::on_headers_complete_callback; |
| Jan Jongboom |
1:3bff14db67c7 | 20 | settings->on_chunk_header = &HttpResponseParser::on_chunk_header_callback; |
| Jan Jongboom |
1:3bff14db67c7 | 21 | settings->on_chunk_complete = &HttpResponseParser::on_chunk_complete_callback; |
| Jan Jongboom |
1:3bff14db67c7 | 22 | settings->on_body = &HttpResponseParser::on_body_callback; |
| Jan Jongboom |
1:3bff14db67c7 | 23 | settings->on_message_complete = &HttpResponseParser::on_message_complete_callback; |
| Jan Jongboom |
1:3bff14db67c7 | 24 | |
| Jan Jongboom |
1:3bff14db67c7 | 25 | // Construct the http_parser object |
| Jan Jongboom |
1:3bff14db67c7 | 26 | parser = new http_parser(); |
| Jan Jongboom |
1:3bff14db67c7 | 27 | http_parser_init(parser, HTTP_RESPONSE); |
| Jan Jongboom |
1:3bff14db67c7 | 28 | parser->data = (void*)this; |
| Jan Jongboom |
1:3bff14db67c7 | 29 | } |
| Jan Jongboom |
1:3bff14db67c7 | 30 | |
| Jan Jongboom |
1:3bff14db67c7 | 31 | ~HttpResponseParser() { |
| Jan Jongboom |
1:3bff14db67c7 | 32 | if (parser) { |
| Jan Jongboom |
1:3bff14db67c7 | 33 | delete parser; |
| Jan Jongboom |
1:3bff14db67c7 | 34 | } |
| Jan Jongboom |
1:3bff14db67c7 | 35 | if (settings) { |
| Jan Jongboom |
1:3bff14db67c7 | 36 | delete settings; |
| Jan Jongboom |
1:3bff14db67c7 | 37 | } |
| Jan Jongboom |
1:3bff14db67c7 | 38 | } |
| Jan Jongboom |
1:3bff14db67c7 | 39 | |
| Jan Jongboom |
1:3bff14db67c7 | 40 | size_t execute(const char* buffer, size_t buffer_size) { |
| Jan Jongboom |
1:3bff14db67c7 | 41 | return http_parser_execute(parser, settings, buffer, buffer_size); |
| Jan Jongboom |
1:3bff14db67c7 | 42 | } |
| Jan Jongboom |
1:3bff14db67c7 | 43 | |
| Jan Jongboom |
1:3bff14db67c7 | 44 | void finish() { |
| Jan Jongboom |
1:3bff14db67c7 | 45 | http_parser_execute(parser, settings, NULL, 0); |
| Jan Jongboom |
1:3bff14db67c7 | 46 | } |
| Jan Jongboom |
1:3bff14db67c7 | 47 | |
| Jan Jongboom |
1:3bff14db67c7 | 48 | private: |
| Jan Jongboom |
1:3bff14db67c7 | 49 | // Member functions |
| Jan Jongboom |
1:3bff14db67c7 | 50 | int on_message_begin(http_parser* parser) { |
| Jan Jongboom |
1:3bff14db67c7 | 51 | return 0; |
| Jan Jongboom |
1:3bff14db67c7 | 52 | } |
| Jan Jongboom |
1:3bff14db67c7 | 53 | |
| Jan Jongboom |
1:3bff14db67c7 | 54 | int on_url(http_parser* parser, const char *at, size_t length) { |
| Jan Jongboom |
1:3bff14db67c7 | 55 | return 0; |
| Jan Jongboom |
1:3bff14db67c7 | 56 | } |
| Jan Jongboom |
1:3bff14db67c7 | 57 | |
| Jan Jongboom |
1:3bff14db67c7 | 58 | int on_status(http_parser* parser, const char *at, size_t length) { |
| Jan Jongboom |
1:3bff14db67c7 | 59 | string s(at, length); |
| Jan Jongboom |
1:3bff14db67c7 | 60 | response->set_status(parser->status_code, s); |
| Jan Jongboom |
1:3bff14db67c7 | 61 | return 0; |
| Jan Jongboom |
1:3bff14db67c7 | 62 | } |
| Jan Jongboom |
1:3bff14db67c7 | 63 | |
| Jan Jongboom |
1:3bff14db67c7 | 64 | int on_header_field(http_parser* parser, const char *at, size_t length) { |
| Jan Jongboom |
1:3bff14db67c7 | 65 | string s(at, length); |
| Jan Jongboom |
1:3bff14db67c7 | 66 | response->set_header_field(s); |
| Jan Jongboom |
1:3bff14db67c7 | 67 | return 0; |
| Jan Jongboom |
1:3bff14db67c7 | 68 | } |
| Jan Jongboom |
1:3bff14db67c7 | 69 | |
| Jan Jongboom |
1:3bff14db67c7 | 70 | int on_header_value(http_parser* parser, const char *at, size_t length) { |
| Jan Jongboom |
1:3bff14db67c7 | 71 | string s(at, length); |
| Jan Jongboom |
1:3bff14db67c7 | 72 | response->set_header_value(s); |
| Jan Jongboom |
1:3bff14db67c7 | 73 | return 0; |
| Jan Jongboom |
1:3bff14db67c7 | 74 | } |
| Jan Jongboom |
1:3bff14db67c7 | 75 | |
| Jan Jongboom |
1:3bff14db67c7 | 76 | static int on_headers_complete(http_parser* parser) { |
| Jan Jongboom |
1:3bff14db67c7 | 77 | return 0; |
| Jan Jongboom |
1:3bff14db67c7 | 78 | } |
| Jan Jongboom |
1:3bff14db67c7 | 79 | |
| Jan Jongboom |
1:3bff14db67c7 | 80 | int on_body(http_parser* parser, const char *at, size_t length) { |
| Jan Jongboom |
1:3bff14db67c7 | 81 | if (body_callback) { |
| Jan Jongboom |
1:3bff14db67c7 | 82 | body_callback(at, length); |
| Jan Jongboom |
1:3bff14db67c7 | 83 | return 0; |
| Jan Jongboom |
1:3bff14db67c7 | 84 | } |
| Jan Jongboom |
1:3bff14db67c7 | 85 | |
| Jan Jongboom |
1:3bff14db67c7 | 86 | string s(at, length); |
| Jan Jongboom |
1:3bff14db67c7 | 87 | response->set_body(s); |
| Jan Jongboom |
1:3bff14db67c7 | 88 | return 0; |
| Jan Jongboom |
1:3bff14db67c7 | 89 | } |
| Jan Jongboom |
1:3bff14db67c7 | 90 | |
| Jan Jongboom |
1:3bff14db67c7 | 91 | int on_message_complete(http_parser* parser) { |
| Jan Jongboom |
1:3bff14db67c7 | 92 | return 0; |
| Jan Jongboom |
1:3bff14db67c7 | 93 | } |
| Jan Jongboom |
1:3bff14db67c7 | 94 | |
| Jan Jongboom |
1:3bff14db67c7 | 95 | int on_chunk_header(http_parser* parser) { |
| Jan Jongboom |
1:3bff14db67c7 | 96 | // ?? Don't know when this is used |
| Jan Jongboom |
1:3bff14db67c7 | 97 | return 0; |
| Jan Jongboom |
1:3bff14db67c7 | 98 | } |
| Jan Jongboom |
1:3bff14db67c7 | 99 | |
| Jan Jongboom |
1:3bff14db67c7 | 100 | int on_chunk_complete(http_parser* parser) { |
| Jan Jongboom |
1:3bff14db67c7 | 101 | // ?? Don't know when this is used |
| Jan Jongboom |
1:3bff14db67c7 | 102 | return 0; |
| Jan Jongboom |
1:3bff14db67c7 | 103 | } |
| Jan Jongboom |
1:3bff14db67c7 | 104 | |
| Jan Jongboom |
1:3bff14db67c7 | 105 | // Static http_parser callback functions |
| Jan Jongboom |
1:3bff14db67c7 | 106 | static int on_message_begin_callback(http_parser* parser) { |
| Jan Jongboom |
1:3bff14db67c7 | 107 | return ((HttpResponseParser*)parser->data)->on_message_begin(parser); |
| Jan Jongboom |
1:3bff14db67c7 | 108 | } |
| Jan Jongboom |
1:3bff14db67c7 | 109 | |
| Jan Jongboom |
1:3bff14db67c7 | 110 | static int on_url_callback(http_parser* parser, const char *at, size_t length) { |
| Jan Jongboom |
1:3bff14db67c7 | 111 | return ((HttpResponseParser*)parser->data)->on_url(parser, at, length); |
| Jan Jongboom |
1:3bff14db67c7 | 112 | } |
| Jan Jongboom |
1:3bff14db67c7 | 113 | |
| Jan Jongboom |
1:3bff14db67c7 | 114 | static int on_status_callback(http_parser* parser, const char *at, size_t length) { |
| Jan Jongboom |
1:3bff14db67c7 | 115 | return ((HttpResponseParser*)parser->data)->on_status(parser, at, length); |
| Jan Jongboom |
1:3bff14db67c7 | 116 | } |
| Jan Jongboom |
1:3bff14db67c7 | 117 | |
| Jan Jongboom |
1:3bff14db67c7 | 118 | static int on_header_field_callback(http_parser* parser, const char *at, size_t length) { |
| Jan Jongboom |
1:3bff14db67c7 | 119 | return ((HttpResponseParser*)parser->data)->on_header_field(parser, at, length); |
| Jan Jongboom |
1:3bff14db67c7 | 120 | } |
| Jan Jongboom |
1:3bff14db67c7 | 121 | |
| Jan Jongboom |
1:3bff14db67c7 | 122 | static int on_header_value_callback(http_parser* parser, const char *at, size_t length) { |
| Jan Jongboom |
1:3bff14db67c7 | 123 | return ((HttpResponseParser*)parser->data)->on_header_value(parser, at, length); |
| Jan Jongboom |
1:3bff14db67c7 | 124 | } |
| Jan Jongboom |
1:3bff14db67c7 | 125 | |
| Jan Jongboom |
1:3bff14db67c7 | 126 | static int on_headers_complete_callback(http_parser* parser) { |
| Jan Jongboom |
1:3bff14db67c7 | 127 | return ((HttpResponseParser*)parser->data)->on_headers_complete(parser); |
| Jan Jongboom |
1:3bff14db67c7 | 128 | } |
| Jan Jongboom |
1:3bff14db67c7 | 129 | |
| Jan Jongboom |
1:3bff14db67c7 | 130 | static int on_body_callback(http_parser* parser, const char *at, size_t length) { |
| Jan Jongboom |
1:3bff14db67c7 | 131 | return ((HttpResponseParser*)parser->data)->on_body(parser, at, length); |
| Jan Jongboom |
1:3bff14db67c7 | 132 | } |
| Jan Jongboom |
1:3bff14db67c7 | 133 | |
| Jan Jongboom |
1:3bff14db67c7 | 134 | static int on_message_complete_callback(http_parser* parser) { |
| Jan Jongboom |
1:3bff14db67c7 | 135 | return ((HttpResponseParser*)parser->data)->on_message_complete(parser); |
| Jan Jongboom |
1:3bff14db67c7 | 136 | } |
| Jan Jongboom |
1:3bff14db67c7 | 137 | |
| Jan Jongboom |
1:3bff14db67c7 | 138 | static int on_chunk_header_callback(http_parser* parser) { |
| Jan Jongboom |
1:3bff14db67c7 | 139 | return ((HttpResponseParser*)parser->data)->on_chunk_header(parser); |
| Jan Jongboom |
1:3bff14db67c7 | 140 | } |
| Jan Jongboom |
1:3bff14db67c7 | 141 | |
| Jan Jongboom |
1:3bff14db67c7 | 142 | static int on_chunk_complete_callback(http_parser* parser) { |
| Jan Jongboom |
1:3bff14db67c7 | 143 | return ((HttpResponseParser*)parser->data)->on_chunk_complete_callback(parser); |
| Jan Jongboom |
1:3bff14db67c7 | 144 | } |
| Jan Jongboom |
1:3bff14db67c7 | 145 | |
| Jan Jongboom |
1:3bff14db67c7 | 146 | HttpResponse* response; |
| Jan Jongboom |
1:3bff14db67c7 | 147 | Callback<void(const char *at, size_t length)> body_callback; |
| Jan Jongboom |
1:3bff14db67c7 | 148 | http_parser* parser; |
| Jan Jongboom |
1:3bff14db67c7 | 149 | http_parser_settings* settings; |
| Jan Jongboom |
1:3bff14db67c7 | 150 | }; |
| Jan Jongboom |
1:3bff14db67c7 | 151 | |
| Jan Jongboom |
1:3bff14db67c7 | 152 | #endif // _HTTP_RESPONSE_PARSER_H_ |