this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Thu Feb 28 18:13:48 2019 +0000
Revision:
73:6f5021cbe752
commit current application code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ocomeni 73:6f5021cbe752 1 #include "select-demo.h"
ocomeni 73:6f5021cbe752 2
ocomeni 73:6f5021cbe752 3 /**
ocomeni 73:6f5021cbe752 4 * This demo shows how to re-use sockets, so the TLS handshake only has to happen once
ocomeni 73:6f5021cbe752 5 */
ocomeni 73:6f5021cbe752 6
ocomeni 73:6f5021cbe752 7 #if DEMO == DEMO_HTTPS_SOCKET_REUSE
ocomeni 73:6f5021cbe752 8
ocomeni 73:6f5021cbe752 9 #include "mbed.h"
ocomeni 73:6f5021cbe752 10 #include "mbed_trace.h"
ocomeni 73:6f5021cbe752 11 #include "https_request.h"
ocomeni 73:6f5021cbe752 12 #include "network-helper.h"
ocomeni 73:6f5021cbe752 13
ocomeni 73:6f5021cbe752 14 /* List of trusted root CA certificates
ocomeni 73:6f5021cbe752 15 * currently one: Let's Encrypt, the CA for httpbin.org
ocomeni 73:6f5021cbe752 16 *
ocomeni 73:6f5021cbe752 17 * To add more root certificates, just concatenate them.
ocomeni 73:6f5021cbe752 18 */
ocomeni 73:6f5021cbe752 19 const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
ocomeni 73:6f5021cbe752 20 "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n"
ocomeni 73:6f5021cbe752 21 "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n"
ocomeni 73:6f5021cbe752 22 "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n"
ocomeni 73:6f5021cbe752 23 "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n"
ocomeni 73:6f5021cbe752 24 "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n"
ocomeni 73:6f5021cbe752 25 "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n"
ocomeni 73:6f5021cbe752 26 "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n"
ocomeni 73:6f5021cbe752 27 "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n"
ocomeni 73:6f5021cbe752 28 "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n"
ocomeni 73:6f5021cbe752 29 "a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n"
ocomeni 73:6f5021cbe752 30 "/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n"
ocomeni 73:6f5021cbe752 31 "AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n"
ocomeni 73:6f5021cbe752 32 "CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n"
ocomeni 73:6f5021cbe752 33 "bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n"
ocomeni 73:6f5021cbe752 34 "c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n"
ocomeni 73:6f5021cbe752 35 "VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n"
ocomeni 73:6f5021cbe752 36 "ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n"
ocomeni 73:6f5021cbe752 37 "MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n"
ocomeni 73:6f5021cbe752 38 "Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n"
ocomeni 73:6f5021cbe752 39 "AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n"
ocomeni 73:6f5021cbe752 40 "uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n"
ocomeni 73:6f5021cbe752 41 "wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n"
ocomeni 73:6f5021cbe752 42 "X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n"
ocomeni 73:6f5021cbe752 43 "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n"
ocomeni 73:6f5021cbe752 44 "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n"
ocomeni 73:6f5021cbe752 45 "-----END CERTIFICATE-----\n";
ocomeni 73:6f5021cbe752 46
ocomeni 73:6f5021cbe752 47 void dump_response(HttpResponse* res) {
ocomeni 73:6f5021cbe752 48 printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
ocomeni 73:6f5021cbe752 49
ocomeni 73:6f5021cbe752 50 printf("Headers:\n");
ocomeni 73:6f5021cbe752 51 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
ocomeni 73:6f5021cbe752 52 printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
ocomeni 73:6f5021cbe752 53 }
ocomeni 73:6f5021cbe752 54 printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
ocomeni 73:6f5021cbe752 55 }
ocomeni 73:6f5021cbe752 56
ocomeni 73:6f5021cbe752 57 int main() {
ocomeni 73:6f5021cbe752 58 NetworkInterface* network = connect_to_default_network_interface();
ocomeni 73:6f5021cbe752 59 if (!network) {
ocomeni 73:6f5021cbe752 60 printf("Cannot connect to the network, see serial output\n");
ocomeni 73:6f5021cbe752 61 return 1;
ocomeni 73:6f5021cbe752 62 }
ocomeni 73:6f5021cbe752 63
ocomeni 73:6f5021cbe752 64 mbed_trace_init();
ocomeni 73:6f5021cbe752 65
ocomeni 73:6f5021cbe752 66 // Create a TLS socket (which holds a TCPSocket)
ocomeni 73:6f5021cbe752 67 printf("\n----- Setting up TLS connection -----\n");
ocomeni 73:6f5021cbe752 68
ocomeni 73:6f5021cbe752 69 nsapi_error_t r;
ocomeni 73:6f5021cbe752 70
ocomeni 73:6f5021cbe752 71 TLSSocket* socket = new TLSSocket();
ocomeni 73:6f5021cbe752 72 if ((r = socket->open(network)) != NSAPI_ERROR_OK) {
ocomeni 73:6f5021cbe752 73 printf("TLS socket open failed (%d)\n", r);
ocomeni 73:6f5021cbe752 74 return 1;
ocomeni 73:6f5021cbe752 75 }
ocomeni 73:6f5021cbe752 76 if ((r = socket->set_root_ca_cert(SSL_CA_PEM)) != NSAPI_ERROR_OK) {
ocomeni 73:6f5021cbe752 77 printf("TLS socket set_root_ca_cert failed (%d)\n", r);
ocomeni 73:6f5021cbe752 78 return 1;
ocomeni 73:6f5021cbe752 79 }
ocomeni 73:6f5021cbe752 80 if ((r = socket->connect("httpbin.org", 443)) != NSAPI_ERROR_OK) {
ocomeni 73:6f5021cbe752 81 printf("TLS socket connect failed (%d)\n", r);
ocomeni 73:6f5021cbe752 82 return 1;
ocomeni 73:6f5021cbe752 83 }
ocomeni 73:6f5021cbe752 84
ocomeni 73:6f5021cbe752 85 // GET request to httpbin.org
ocomeni 73:6f5021cbe752 86 {
ocomeni 73:6f5021cbe752 87 HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418");
ocomeni 73:6f5021cbe752 88
ocomeni 73:6f5021cbe752 89 HttpResponse* get_res = get_req->send();
ocomeni 73:6f5021cbe752 90 if (!get_res) {
ocomeni 73:6f5021cbe752 91 printf("HttpRequest failed (error code %d)\n", get_req->get_error());
ocomeni 73:6f5021cbe752 92 return 1;
ocomeni 73:6f5021cbe752 93 }
ocomeni 73:6f5021cbe752 94 printf("\n----- HTTPS GET response -----\n");
ocomeni 73:6f5021cbe752 95 dump_response(get_res);
ocomeni 73:6f5021cbe752 96
ocomeni 73:6f5021cbe752 97 delete get_req;
ocomeni 73:6f5021cbe752 98 }
ocomeni 73:6f5021cbe752 99
ocomeni 73:6f5021cbe752 100 // POST request to httpbin.org
ocomeni 73:6f5021cbe752 101 {
ocomeni 73:6f5021cbe752 102 HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "https://httpbin.org/post");
ocomeni 73:6f5021cbe752 103 post_req->set_header("Content-Type", "application/json");
ocomeni 73:6f5021cbe752 104
ocomeni 73:6f5021cbe752 105 const char body[] = "{\"hello\":\"world\"}";
ocomeni 73:6f5021cbe752 106
ocomeni 73:6f5021cbe752 107 HttpResponse* post_res = post_req->send(body, strlen(body));
ocomeni 73:6f5021cbe752 108 if (!post_res) {
ocomeni 73:6f5021cbe752 109 printf("HttpRequest failed (error code %d)\n", post_req->get_error());
ocomeni 73:6f5021cbe752 110 return 1;
ocomeni 73:6f5021cbe752 111 }
ocomeni 73:6f5021cbe752 112
ocomeni 73:6f5021cbe752 113 printf("\n----- HTTPS POST response -----\n");
ocomeni 73:6f5021cbe752 114 dump_response(post_res);
ocomeni 73:6f5021cbe752 115
ocomeni 73:6f5021cbe752 116 delete post_req;
ocomeni 73:6f5021cbe752 117 }
ocomeni 73:6f5021cbe752 118
ocomeni 73:6f5021cbe752 119 socket->close();
ocomeni 73:6f5021cbe752 120 delete socket;
ocomeni 73:6f5021cbe752 121
ocomeni 73:6f5021cbe752 122 wait(osWaitForever);
ocomeni 73:6f5021cbe752 123 }
ocomeni 73:6f5021cbe752 124
ocomeni 73:6f5021cbe752 125 #endif