CO528 - Assessment 3 - Guard Room Project

Dependencies:   C12832 MMA7660 mbed-http

Fork of http-example by sandbox

Committer:
Jan Jongboom
Date:
Thu Mar 30 15:15:47 2017 +0200
Revision:
14:3c173847e681
Parent:
10:e79a808ebbe8
Bump mbed-http, treat response without Content-Length but with a body as chunked

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