a

Dependencies:   mbed-http

Fork of http-example by sandbox

Committer:
kuwaeyoshi
Date:
Fri Aug 24 05:03:58 2018 +0000
Revision:
30:d4efad3e89f1
Parent:
29:5ad8f931e4ff
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jan Jongboom 0:85fdc69bc10c 1 #include "select-demo.h"
Jan Jongboom 0:85fdc69bc10c 2
Jan Jongboom 0:85fdc69bc10c 3 #if DEMO == DEMO_HTTPS
Jan Jongboom 0:85fdc69bc10c 4
Jan Jongboom 0:85fdc69bc10c 5 #include "mbed.h"
Jan Jongboom 0:85fdc69bc10c 6 #include "easy-connect.h"
Jan Jongboom 0:85fdc69bc10c 7 #include "https_request.h"
Jan Jongboom 0:85fdc69bc10c 8
Jan Jongboom 0:85fdc69bc10c 9 /* List of trusted root CA certificates
Jan Jongboom 10:e79a808ebbe8 10 * currently two: GlobalSign, the CA for developer.mbed.org and Let's Encrypt, the CA for httpbin.org
Jan Jongboom 0:85fdc69bc10c 11 *
Jan Jongboom 10:e79a808ebbe8 12 * To add more root certificates, just concatenate them.
Jan Jongboom 0:85fdc69bc10c 13 */
Jan Jongboom 0:85fdc69bc10c 14 const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
Jan Jongboom 0:85fdc69bc10c 15 "MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG\n"
Jan Jongboom 0:85fdc69bc10c 16 "A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv\n"
Jan Jongboom 0:85fdc69bc10c 17 "b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw\n"
Jan Jongboom 0:85fdc69bc10c 18 "MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i\n"
Jan Jongboom 0:85fdc69bc10c 19 "YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT\n"
Jan Jongboom 0:85fdc69bc10c 20 "aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ\n"
Jan Jongboom 0:85fdc69bc10c 21 "jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp\n"
Jan Jongboom 0:85fdc69bc10c 22 "xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp\n"
Jan Jongboom 0:85fdc69bc10c 23 "1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG\n"
Jan Jongboom 0:85fdc69bc10c 24 "snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ\n"
Jan Jongboom 0:85fdc69bc10c 25 "U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8\n"
Jan Jongboom 0:85fdc69bc10c 26 "9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E\n"
Jan Jongboom 0:85fdc69bc10c 27 "BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B\n"
Jan Jongboom 0:85fdc69bc10c 28 "AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz\n"
Jan Jongboom 0:85fdc69bc10c 29 "yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE\n"
Jan Jongboom 0:85fdc69bc10c 30 "38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP\n"
Jan Jongboom 0:85fdc69bc10c 31 "AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad\n"
Jan Jongboom 0:85fdc69bc10c 32 "DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME\n"
Jan Jongboom 0:85fdc69bc10c 33 "HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n"
Jan Jongboom 4:27fd8efb5bab 34 "-----END CERTIFICATE-----\n"
Jan Jongboom 4:27fd8efb5bab 35 "-----BEGIN CERTIFICATE-----\n"
Jan Jongboom 10:e79a808ebbe8 36 "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n"
Jan Jongboom 10:e79a808ebbe8 37 "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n"
Jan Jongboom 10:e79a808ebbe8 38 "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n"
Jan Jongboom 10:e79a808ebbe8 39 "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n"
Jan Jongboom 10:e79a808ebbe8 40 "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n"
Jan Jongboom 10:e79a808ebbe8 41 "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n"
Jan Jongboom 10:e79a808ebbe8 42 "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n"
Jan Jongboom 10:e79a808ebbe8 43 "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n"
Jan Jongboom 10:e79a808ebbe8 44 "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n"
Jan Jongboom 10:e79a808ebbe8 45 "a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n"
Jan Jongboom 10:e79a808ebbe8 46 "/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n"
Jan Jongboom 10:e79a808ebbe8 47 "AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n"
Jan Jongboom 10:e79a808ebbe8 48 "CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n"
Jan Jongboom 10:e79a808ebbe8 49 "bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n"
Jan Jongboom 10:e79a808ebbe8 50 "c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n"
Jan Jongboom 10:e79a808ebbe8 51 "VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n"
Jan Jongboom 10:e79a808ebbe8 52 "ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n"
Jan Jongboom 10:e79a808ebbe8 53 "MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n"
Jan Jongboom 10:e79a808ebbe8 54 "Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n"
Jan Jongboom 10:e79a808ebbe8 55 "AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n"
Jan Jongboom 10:e79a808ebbe8 56 "uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n"
Jan Jongboom 10:e79a808ebbe8 57 "wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n"
Jan Jongboom 10:e79a808ebbe8 58 "X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n"
Jan Jongboom 10:e79a808ebbe8 59 "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n"
Jan Jongboom 10:e79a808ebbe8 60 "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n"
Jan Jongboom 0:85fdc69bc10c 61 "-----END CERTIFICATE-----\n";
Jan Jongboom 0:85fdc69bc10c 62
Jan Jongboom 0:85fdc69bc10c 63 void dump_response(HttpResponse* res) {
Jan Jongboom 0:85fdc69bc10c 64 mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
Jan Jongboom 0:85fdc69bc10c 65
Jan Jongboom 0:85fdc69bc10c 66 mbedtls_printf("Headers:\n");
Jan Jongboom 0:85fdc69bc10c 67 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
Jan Jongboom 7:5d32909f77de 68 mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
Jan Jongboom 0:85fdc69bc10c 69 }
Jan Jongboom 5:42f713b19337 70 mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
Jan Jongboom 0:85fdc69bc10c 71 }
Jan Jongboom 0:85fdc69bc10c 72
Jan Jongboom 0:85fdc69bc10c 73 int main() {
Jan Jongboom 0:85fdc69bc10c 74 NetworkInterface* network = easy_connect(true);
Jan Jongboom 0:85fdc69bc10c 75 if (!network) {
Jan Jongboom 0:85fdc69bc10c 76 return 1;
Jan Jongboom 0:85fdc69bc10c 77 }
Jan Jongboom 0:85fdc69bc10c 78
Jan Jongboom 5:42f713b19337 79 // GET request to developer.mbed.org
Jan Jongboom 5:42f713b19337 80 {
Jan Jongboom 5:42f713b19337 81 printf("\n----- HTTPS GET request -----\n");
Jan Jongboom 4:27fd8efb5bab 82
Jan Jongboom 25:a8be9f3a530c 83 HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt");
Jan Jongboom 5:42f713b19337 84 get_req->set_debug(true);
Jan Jongboom 4:27fd8efb5bab 85
Jan Jongboom 5:42f713b19337 86 HttpResponse* get_res = get_req->send();
Jan Jongboom 5:42f713b19337 87 if (!get_res) {
Jan Jongboom 5:42f713b19337 88 printf("HttpRequest failed (error code %d)\n", get_req->get_error());
Jan Jongboom 5:42f713b19337 89 return 1;
Jan Jongboom 5:42f713b19337 90 }
Jan Jongboom 5:42f713b19337 91 printf("\n----- HTTPS GET response -----\n");
Jan Jongboom 5:42f713b19337 92 dump_response(get_res);
Jan Jongboom 5:42f713b19337 93
Jan Jongboom 5:42f713b19337 94 delete get_req;
Jan Jongboom 0:85fdc69bc10c 95 }
Jan Jongboom 4:27fd8efb5bab 96
Jan Jongboom 4:27fd8efb5bab 97 // POST request to httpbin.org
Jan Jongboom 5:42f713b19337 98 {
Jan Jongboom 5:42f713b19337 99 printf("\n----- HTTPS POST request -----\n");
Jan Jongboom 4:27fd8efb5bab 100
Jan Jongboom 5:42f713b19337 101 HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post");
Jan Jongboom 5:42f713b19337 102 post_req->set_debug(true);
Jan Jongboom 5:42f713b19337 103 post_req->set_header("Content-Type", "application/json");
Jan Jongboom 0:85fdc69bc10c 104
Jan Jongboom 5:42f713b19337 105 const char body[] = "{\"hello\":\"world\"}";
Jan Jongboom 4:27fd8efb5bab 106
Jan Jongboom 5:42f713b19337 107 HttpResponse* post_res = post_req->send(body, strlen(body));
Jan Jongboom 5:42f713b19337 108 if (!post_res) {
Jan Jongboom 5:42f713b19337 109 printf("HttpRequest failed (error code %d)\n", post_req->get_error());
Jan Jongboom 5:42f713b19337 110 return 1;
Jan Jongboom 5:42f713b19337 111 }
Jan Jongboom 5:42f713b19337 112
Jan Jongboom 5:42f713b19337 113 printf("\n----- HTTPS POST response -----\n");
Jan Jongboom 5:42f713b19337 114 dump_response(post_res);
Jan Jongboom 5:42f713b19337 115
Jan Jongboom 5:42f713b19337 116 delete post_req;
Jan Jongboom 4:27fd8efb5bab 117 }
Jan Jongboom 4:27fd8efb5bab 118
Jan Jongboom 28:9bccd981a393 119 wait(osWaitForever);
Jan Jongboom 0:85fdc69bc10c 120 }
Jan Jongboom 0:85fdc69bc10c 121
Jan Jongboom 0:85fdc69bc10c 122 #endif