a

Dependencies:   mbed-http

Fork of http-example by sandbox

Committer:
Jan Jongboom
Date:
Fri Feb 24 11:21:24 2017 +0100
Revision:
7:5d32909f77de
Parent:
5:42f713b19337
Child:
10:e79a808ebbe8
Add chunked encoding, make HttpResponse own response headers

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 0:85fdc69bc10c 12 * currently only GlobalSign, the CA for developer.mbed.org
Jan Jongboom 0:85fdc69bc10c 13 *
Jan Jongboom 0:85fdc69bc10c 14 * To add more than one root, 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 4:27fd8efb5bab 38 "MIIENjCCAx6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBvMQswCQYDVQQGEwJTRTEU\n"
Jan Jongboom 4:27fd8efb5bab 39 "MBIGA1UEChMLQWRkVHJ1c3QgQUIxJjAkBgNVBAsTHUFkZFRydXN0IEV4dGVybmFs\n"
Jan Jongboom 4:27fd8efb5bab 40 "IFRUUCBOZXR3b3JrMSIwIAYDVQQDExlBZGRUcnVzdCBFeHRlcm5hbCBDQSBSb290\n"
Jan Jongboom 4:27fd8efb5bab 41 "MB4XDTAwMDUzMDEwNDgzOFoXDTIwMDUzMDEwNDgzOFowbzELMAkGA1UEBhMCU0Ux\n"
Jan Jongboom 4:27fd8efb5bab 42 "FDASBgNVBAoTC0FkZFRydXN0IEFCMSYwJAYDVQQLEx1BZGRUcnVzdCBFeHRlcm5h\n"
Jan Jongboom 4:27fd8efb5bab 43 "bCBUVFAgTmV0d29yazEiMCAGA1UEAxMZQWRkVHJ1c3QgRXh0ZXJuYWwgQ0EgUm9v\n"
Jan Jongboom 4:27fd8efb5bab 44 "dDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALf3GjPm8gAELTngTlvt\n"
Jan Jongboom 4:27fd8efb5bab 45 "H7xsD821+iO2zt6bETOXpClMfZOfvUq8k+0DGuOPz+VtUFrWlymUWoCwSXrbLpX9\n"
Jan Jongboom 4:27fd8efb5bab 46 "uMq/NzgtHj6RQa1wVsfwTz/oMp50ysiQVOnGXw94nZpAPA6sYapeFI+eh6FqUNzX\n"
Jan Jongboom 4:27fd8efb5bab 47 "mk6vBbOmcZSccbNQYArHE504B4YCqOmoaSYYkKtMsE8jqzpPhNjfzp/haW+710LX\n"
Jan Jongboom 4:27fd8efb5bab 48 "a0Tkx63ubUFfclpxCDezeWWkWaCUN/cALw3CknLa0Dhy2xSoRcRdKn23tNbE7qzN\n"
Jan Jongboom 4:27fd8efb5bab 49 "E0S3ySvdQwAl+mG5aWpYIxG3pzOPVnVZ9c0p10a3CitlttNCbxWyuHv77+ldU9U0\n"
Jan Jongboom 4:27fd8efb5bab 50 "WicCAwEAAaOB3DCB2TAdBgNVHQ4EFgQUrb2YejS0Jvf6xCZU7wO94CTLVBowCwYD\n"
Jan Jongboom 4:27fd8efb5bab 51 "VR0PBAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wgZkGA1UdIwSBkTCBjoAUrb2YejS0\n"
Jan Jongboom 4:27fd8efb5bab 52 "Jvf6xCZU7wO94CTLVBqhc6RxMG8xCzAJBgNVBAYTAlNFMRQwEgYDVQQKEwtBZGRU\n"
Jan Jongboom 4:27fd8efb5bab 53 "cnVzdCBBQjEmMCQGA1UECxMdQWRkVHJ1c3QgRXh0ZXJuYWwgVFRQIE5ldHdvcmsx\n"
Jan Jongboom 4:27fd8efb5bab 54 "IjAgBgNVBAMTGUFkZFRydXN0IEV4dGVybmFsIENBIFJvb3SCAQEwDQYJKoZIhvcN\n"
Jan Jongboom 4:27fd8efb5bab 55 "AQEFBQADggEBALCb4IUlwtYj4g+WBpKdQZic2YR5gdkeWxQHIzZlj7DYd7usQWxH\n"
Jan Jongboom 4:27fd8efb5bab 56 "YINRsPkyPef89iYTx4AWpb9a/IfPeHmJIZriTAcKhjW88t5RxNKWt9x+Tu5w/Rw5\n"
Jan Jongboom 4:27fd8efb5bab 57 "6wwCURQtjr0W4MHfRnXnJK3s9EK0hZNwEGe6nQY1ShjTK3rMUUKhemPR5ruhxSvC\n"
Jan Jongboom 4:27fd8efb5bab 58 "Nr4TDea9Y355e6cJDUCrat2PisP29owaQgVR1EX1n6diIWgVIEM8med8vSTYqZEX\n"
Jan Jongboom 4:27fd8efb5bab 59 "c4g/VhsxOBi0cQ+azcgOno4uG+GMmIPLHzHxREzGBHNJdmAPx/i9F4BrLunMTA5a\n"
Jan Jongboom 4:27fd8efb5bab 60 "mnkPIAou1Z5jJh5VkpTYghdae9C8x49OhgQ=\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 pc.baud(115200);
Jan Jongboom 0:85fdc69bc10c 75
Jan Jongboom 0:85fdc69bc10c 76 NetworkInterface* network = easy_connect(true);
Jan Jongboom 0:85fdc69bc10c 77 if (!network) {
Jan Jongboom 0:85fdc69bc10c 78 return 1;
Jan Jongboom 0:85fdc69bc10c 79 }
Jan Jongboom 0:85fdc69bc10c 80
Jan Jongboom 5:42f713b19337 81 // GET request to developer.mbed.org
Jan Jongboom 5:42f713b19337 82 {
Jan Jongboom 5:42f713b19337 83 printf("\n----- HTTPS GET request -----\n");
Jan Jongboom 4:27fd8efb5bab 84
Jan Jongboom 5:42f713b19337 85 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 86 get_req->set_debug(true);
Jan Jongboom 4:27fd8efb5bab 87
Jan Jongboom 5:42f713b19337 88 HttpResponse* get_res = get_req->send();
Jan Jongboom 5:42f713b19337 89 if (!get_res) {
Jan Jongboom 5:42f713b19337 90 printf("HttpRequest failed (error code %d)\n", get_req->get_error());
Jan Jongboom 5:42f713b19337 91 return 1;
Jan Jongboom 5:42f713b19337 92 }
Jan Jongboom 5:42f713b19337 93 printf("\n----- HTTPS GET response -----\n");
Jan Jongboom 5:42f713b19337 94 dump_response(get_res);
Jan Jongboom 5:42f713b19337 95
Jan Jongboom 5:42f713b19337 96 delete get_req;
Jan Jongboom 0:85fdc69bc10c 97 }
Jan Jongboom 4:27fd8efb5bab 98
Jan Jongboom 4:27fd8efb5bab 99 // POST request to httpbin.org
Jan Jongboom 5:42f713b19337 100 {
Jan Jongboom 5:42f713b19337 101 printf("\n----- HTTPS POST request -----\n");
Jan Jongboom 4:27fd8efb5bab 102
Jan Jongboom 5:42f713b19337 103 HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post");
Jan Jongboom 5:42f713b19337 104 post_req->set_debug(true);
Jan Jongboom 5:42f713b19337 105 post_req->set_header("Content-Type", "application/json");
Jan Jongboom 0:85fdc69bc10c 106
Jan Jongboom 5:42f713b19337 107 const char body[] = "{\"hello\":\"world\"}";
Jan Jongboom 4:27fd8efb5bab 108
Jan Jongboom 5:42f713b19337 109 HttpResponse* post_res = post_req->send(body, strlen(body));
Jan Jongboom 5:42f713b19337 110 if (!post_res) {
Jan Jongboom 5:42f713b19337 111 printf("HttpRequest failed (error code %d)\n", post_req->get_error());
Jan Jongboom 5:42f713b19337 112 return 1;
Jan Jongboom 5:42f713b19337 113 }
Jan Jongboom 5:42f713b19337 114
Jan Jongboom 5:42f713b19337 115 printf("\n----- HTTPS POST response -----\n");
Jan Jongboom 5:42f713b19337 116 dump_response(post_res);
Jan Jongboom 5:42f713b19337 117
Jan Jongboom 5:42f713b19337 118 delete post_req;
Jan Jongboom 4:27fd8efb5bab 119 }
Jan Jongboom 4:27fd8efb5bab 120
Jan Jongboom 0:85fdc69bc10c 121 Thread::wait(osWaitForever);
Jan Jongboom 0:85fdc69bc10c 122 }
Jan Jongboom 0:85fdc69bc10c 123
Jan Jongboom 0:85fdc69bc10c 124 #endif