Fork of SandBox's original mbed-http (https://os.mbed.com/teams/sandbox/code/mbed-http/) and update for MbedOS6+ Content of TESTS folder was replaced with basic examples form original SandBox's HelloWorld
TESTS/main-https.cpp@41:cc1a8c63f159, 2021-05-30 (annotated)
- Committer:
- JohnnyK
- Date:
- Sun May 30 19:45:32 2021 +0000
- Revision:
- 41:cc1a8c63f159
change test program to two basic example from SandBox's helloworld program ofc reworked.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JohnnyK | 41:cc1a8c63f159 | 1 | #if 0 /*Set it to "1" to enable content*/ |
JohnnyK | 41:cc1a8c63f159 | 2 | |
JohnnyK | 41:cc1a8c63f159 | 3 | #include "mbed.h" |
JohnnyK | 41:cc1a8c63f159 | 4 | #include "mbed_trace.h" |
JohnnyK | 41:cc1a8c63f159 | 5 | #include "https_request.h" |
JohnnyK | 41:cc1a8c63f159 | 6 | |
JohnnyK | 41:cc1a8c63f159 | 7 | /* List of trusted root CA certificates |
JohnnyK | 41:cc1a8c63f159 | 8 | * currently two: Amazon, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org |
JohnnyK | 41:cc1a8c63f159 | 9 | * |
JohnnyK | 41:cc1a8c63f159 | 10 | * To add more root certificates, just concatenate them. |
JohnnyK | 41:cc1a8c63f159 | 11 | * |
JohnnyK | 41:cc1a8c63f159 | 12 | * !!! IF CA NOT WORKING SEE THIS PAGE - https://os.mbed.com/blog/entry/Adding-TLS-Sockets-to-Mbed-OS/ |
JohnnyK | 41:cc1a8c63f159 | 13 | * Under Windows you can run OpenSSl from C:\Program Files\Git\usr\bin\openssl.exe if you have Git installed. |
JohnnyK | 41:cc1a8c63f159 | 14 | * Then just executte "s_client -connect mbed.com:443 -showcerts" and "s_client -connect httpbin.org:443 -showcerts" |
JohnnyK | 41:cc1a8c63f159 | 15 | */ |
JohnnyK | 41:cc1a8c63f159 | 16 | const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" |
JohnnyK | 41:cc1a8c63f159 | 17 | "MIIEdTCCA12gAwIBAgIJAKcOSkw0grd/MA0GCSqGSIb3DQEBCwUAMGgxCzAJBgNV\n" |
JohnnyK | 41:cc1a8c63f159 | 18 | "BAYTAlVTMSUwIwYDVQQKExxTdGFyZmllbGQgVGVjaG5vbG9naWVzLCBJbmMuMTIw\n" |
JohnnyK | 41:cc1a8c63f159 | 19 | "MAYDVQQLEylTdGFyZmllbGQgQ2xhc3MgMiBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0\n" |
JohnnyK | 41:cc1a8c63f159 | 20 | "eTAeFw0wOTA5MDIwMDAwMDBaFw0zNDA2MjgxNzM5MTZaMIGYMQswCQYDVQQGEwJV\n" |
JohnnyK | 41:cc1a8c63f159 | 21 | "UzEQMA4GA1UECBMHQXJpem9uYTETMBEGA1UEBxMKU2NvdHRzZGFsZTElMCMGA1UE\n" |
JohnnyK | 41:cc1a8c63f159 | 22 | "ChMcU3RhcmZpZWxkIFRlY2hub2xvZ2llcywgSW5jLjE7MDkGA1UEAxMyU3RhcmZp\n" |
JohnnyK | 41:cc1a8c63f159 | 23 | "ZWxkIFNlcnZpY2VzIFJvb3QgQ2VydGlmaWNhdGUgQXV0aG9yaXR5IC0gRzIwggEi\n" |
JohnnyK | 41:cc1a8c63f159 | 24 | "MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDVDDrEKvlO4vW+GZdfjohTsR8/\n" |
JohnnyK | 41:cc1a8c63f159 | 25 | "y8+fIBNtKTrID30892t2OGPZNmCom15cAICyL1l/9of5JUOG52kbUpqQ4XHj2C0N\n" |
JohnnyK | 41:cc1a8c63f159 | 26 | "Tm/2yEnZtvMaVq4rtnQU68/7JuMauh2WLmo7WJSJR1b/JaCTcFOD2oR0FMNnngRo\n" |
JohnnyK | 41:cc1a8c63f159 | 27 | "Ot+OQFodSk7PQ5E751bWAHDLUu57fa4657wx+UX2wmDPE1kCK4DMNEffud6QZW0C\n" |
JohnnyK | 41:cc1a8c63f159 | 28 | "zyyRpqbn3oUYSXxmTqM6bam17jQuug0DuDPfR+uxa40l2ZvOgdFFRjKWcIfeAg5J\n" |
JohnnyK | 41:cc1a8c63f159 | 29 | "Q4W2bHO7ZOphQazJ1FTfhy/HIrImzJ9ZVGif/L4qL8RVHHVAYBeFAlU5i38FAgMB\n" |
JohnnyK | 41:cc1a8c63f159 | 30 | "AAGjgfAwge0wDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAYYwHQYDVR0O\n" |
JohnnyK | 41:cc1a8c63f159 | 31 | "BBYEFJxfAN+qAdcwKziIorhtSpzyEZGDMB8GA1UdIwQYMBaAFL9ft9HO3R+G9FtV\n" |
JohnnyK | 41:cc1a8c63f159 | 32 | "rNzXEMIOqYjnME8GCCsGAQUFBwEBBEMwQTAcBggrBgEFBQcwAYYQaHR0cDovL28u\n" |
JohnnyK | 41:cc1a8c63f159 | 33 | "c3MyLnVzLzAhBggrBgEFBQcwAoYVaHR0cDovL3guc3MyLnVzL3guY2VyMCYGA1Ud\n" |
JohnnyK | 41:cc1a8c63f159 | 34 | "HwQfMB0wG6AZoBeGFWh0dHA6Ly9zLnNzMi51cy9yLmNybDARBgNVHSAECjAIMAYG\n" |
JohnnyK | 41:cc1a8c63f159 | 35 | "BFUdIAAwDQYJKoZIhvcNAQELBQADggEBACMd44pXyn3pF3lM8R5V/cxTbj5HD9/G\n" |
JohnnyK | 41:cc1a8c63f159 | 36 | "VfKyBDbtgB9TxF00KGu+x1X8Z+rLP3+QsjPNG1gQggL4+C/1E2DUBc7xgQjB3ad1\n" |
JohnnyK | 41:cc1a8c63f159 | 37 | "l08YuW3e95ORCLp+QCztweq7dp4zBncdDQh/U90bZKuCJ/Fp1U1ervShw3WnWEQt\n" |
JohnnyK | 41:cc1a8c63f159 | 38 | "8jxwmKy6abaVd38PMV4s/KCHOkdp8Hlf9BRUpJVeEXgSYCfOn8J3/yNTd126/+pZ\n" |
JohnnyK | 41:cc1a8c63f159 | 39 | "59vPr5KW7ySaNRB6nJHGDn2Z9j8Z3/VyVOEVqQdZe4O/Ui5GjLIAZHYcSNPYeehu\n" |
JohnnyK | 41:cc1a8c63f159 | 40 | "VsyuLAOQ1xk4meTKCRlb/weWsKh/NEnfVqn3sF/tM+2MR7cwA130A4w=\n" |
JohnnyK | 41:cc1a8c63f159 | 41 | "-----END CERTIFICATE-----\n" |
JohnnyK | 41:cc1a8c63f159 | 42 | "-----BEGIN CERTIFICATE-----\n" |
JohnnyK | 41:cc1a8c63f159 | 43 | "MIIEdTCCA12gAwIBAgIJAKcOSkw0grd/MA0GCSqGSIb3DQEBCwUAMGgxCzAJBgNV\n" |
JohnnyK | 41:cc1a8c63f159 | 44 | "BAYTAlVTMSUwIwYDVQQKExxTdGFyZmllbGQgVGVjaG5vbG9naWVzLCBJbmMuMTIw\n" |
JohnnyK | 41:cc1a8c63f159 | 45 | "MAYDVQQLEylTdGFyZmllbGQgQ2xhc3MgMiBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0\n" |
JohnnyK | 41:cc1a8c63f159 | 46 | "eTAeFw0wOTA5MDIwMDAwMDBaFw0zNDA2MjgxNzM5MTZaMIGYMQswCQYDVQQGEwJV\n" |
JohnnyK | 41:cc1a8c63f159 | 47 | "UzEQMA4GA1UECBMHQXJpem9uYTETMBEGA1UEBxMKU2NvdHRzZGFsZTElMCMGA1UE\n" |
JohnnyK | 41:cc1a8c63f159 | 48 | "ChMcU3RhcmZpZWxkIFRlY2hub2xvZ2llcywgSW5jLjE7MDkGA1UEAxMyU3RhcmZp\n" |
JohnnyK | 41:cc1a8c63f159 | 49 | "ZWxkIFNlcnZpY2VzIFJvb3QgQ2VydGlmaWNhdGUgQXV0aG9yaXR5IC0gRzIwggEi\n" |
JohnnyK | 41:cc1a8c63f159 | 50 | "MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDVDDrEKvlO4vW+GZdfjohTsR8/\n" |
JohnnyK | 41:cc1a8c63f159 | 51 | "y8+fIBNtKTrID30892t2OGPZNmCom15cAICyL1l/9of5JUOG52kbUpqQ4XHj2C0N\n" |
JohnnyK | 41:cc1a8c63f159 | 52 | "Tm/2yEnZtvMaVq4rtnQU68/7JuMauh2WLmo7WJSJR1b/JaCTcFOD2oR0FMNnngRo\n" |
JohnnyK | 41:cc1a8c63f159 | 53 | "Ot+OQFodSk7PQ5E751bWAHDLUu57fa4657wx+UX2wmDPE1kCK4DMNEffud6QZW0C\n" |
JohnnyK | 41:cc1a8c63f159 | 54 | "zyyRpqbn3oUYSXxmTqM6bam17jQuug0DuDPfR+uxa40l2ZvOgdFFRjKWcIfeAg5J\n" |
JohnnyK | 41:cc1a8c63f159 | 55 | "Q4W2bHO7ZOphQazJ1FTfhy/HIrImzJ9ZVGif/L4qL8RVHHVAYBeFAlU5i38FAgMB\n" |
JohnnyK | 41:cc1a8c63f159 | 56 | "AAGjgfAwge0wDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAYYwHQYDVR0O\n" |
JohnnyK | 41:cc1a8c63f159 | 57 | "BBYEFJxfAN+qAdcwKziIorhtSpzyEZGDMB8GA1UdIwQYMBaAFL9ft9HO3R+G9FtV\n" |
JohnnyK | 41:cc1a8c63f159 | 58 | "rNzXEMIOqYjnME8GCCsGAQUFBwEBBEMwQTAcBggrBgEFBQcwAYYQaHR0cDovL28u\n" |
JohnnyK | 41:cc1a8c63f159 | 59 | "c3MyLnVzLzAhBggrBgEFBQcwAoYVaHR0cDovL3guc3MyLnVzL3guY2VyMCYGA1Ud\n" |
JohnnyK | 41:cc1a8c63f159 | 60 | "HwQfMB0wG6AZoBeGFWh0dHA6Ly9zLnNzMi51cy9yLmNybDARBgNVHSAECjAIMAYG\n" |
JohnnyK | 41:cc1a8c63f159 | 61 | "BFUdIAAwDQYJKoZIhvcNAQELBQADggEBACMd44pXyn3pF3lM8R5V/cxTbj5HD9/G\n" |
JohnnyK | 41:cc1a8c63f159 | 62 | "VfKyBDbtgB9TxF00KGu+x1X8Z+rLP3+QsjPNG1gQggL4+C/1E2DUBc7xgQjB3ad1\n" |
JohnnyK | 41:cc1a8c63f159 | 63 | "l08YuW3e95ORCLp+QCztweq7dp4zBncdDQh/U90bZKuCJ/Fp1U1ervShw3WnWEQt\n" |
JohnnyK | 41:cc1a8c63f159 | 64 | "8jxwmKy6abaVd38PMV4s/KCHOkdp8Hlf9BRUpJVeEXgSYCfOn8J3/yNTd126/+pZ\n" |
JohnnyK | 41:cc1a8c63f159 | 65 | "59vPr5KW7ySaNRB6nJHGDn2Z9j8Z3/VyVOEVqQdZe4O/Ui5GjLIAZHYcSNPYeehu\n" |
JohnnyK | 41:cc1a8c63f159 | 66 | "VsyuLAOQ1xk4meTKCRlb/weWsKh/NEnfVqn3sF/tM+2MR7cwA130A4w=\n" |
JohnnyK | 41:cc1a8c63f159 | 67 | "-----END CERTIFICATE-----\n"; |
JohnnyK | 41:cc1a8c63f159 | 68 | |
JohnnyK | 41:cc1a8c63f159 | 69 | void dump_response(HttpResponse* res) { |
JohnnyK | 41:cc1a8c63f159 | 70 | printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
JohnnyK | 41:cc1a8c63f159 | 71 | |
JohnnyK | 41:cc1a8c63f159 | 72 | printf("Headers:\n"); |
JohnnyK | 41:cc1a8c63f159 | 73 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
JohnnyK | 41:cc1a8c63f159 | 74 | printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
JohnnyK | 41:cc1a8c63f159 | 75 | } |
JohnnyK | 41:cc1a8c63f159 | 76 | printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
JohnnyK | 41:cc1a8c63f159 | 77 | } |
JohnnyK | 41:cc1a8c63f159 | 78 | |
JohnnyK | 41:cc1a8c63f159 | 79 | int main() { |
JohnnyK | 41:cc1a8c63f159 | 80 | printf("Example of a HTTPS client\n"); |
JohnnyK | 41:cc1a8c63f159 | 81 | printf("[NWK] Connecting to network...\n"); |
JohnnyK | 41:cc1a8c63f159 | 82 | NetworkInterface* network = NetworkInterface::get_default_instance(); |
JohnnyK | 41:cc1a8c63f159 | 83 | if (!network) { |
JohnnyK | 41:cc1a8c63f159 | 84 | printf("[NWK] No network interface found, select an interface in 'network-helper.h'\n"); |
JohnnyK | 41:cc1a8c63f159 | 85 | return 1; |
JohnnyK | 41:cc1a8c63f159 | 86 | } |
JohnnyK | 41:cc1a8c63f159 | 87 | nsapi_error_t err; |
JohnnyK | 41:cc1a8c63f159 | 88 | if ((err = network->connect())!= NSAPI_ERROR_OK) { |
JohnnyK | 41:cc1a8c63f159 | 89 | printf("[NWK] Failed to connect to network (%d)\n", err); |
JohnnyK | 41:cc1a8c63f159 | 90 | return 1; |
JohnnyK | 41:cc1a8c63f159 | 91 | } |
JohnnyK | 41:cc1a8c63f159 | 92 | printf("[NWK] Connected to the network\n"); |
JohnnyK | 41:cc1a8c63f159 | 93 | SocketAddress ip; |
JohnnyK | 41:cc1a8c63f159 | 94 | network->get_ip_address(&ip); |
JohnnyK | 41:cc1a8c63f159 | 95 | printf("[NWK] IP address: %s\n", ip.get_ip_address()); |
JohnnyK | 41:cc1a8c63f159 | 96 | |
JohnnyK | 41:cc1a8c63f159 | 97 | mbed_trace_init(); |
JohnnyK | 41:cc1a8c63f159 | 98 | |
JohnnyK | 41:cc1a8c63f159 | 99 | // GET request to os.mbed.com |
JohnnyK | 41:cc1a8c63f159 | 100 | { |
JohnnyK | 41:cc1a8c63f159 | 101 | printf("\n----- HTTPS GET request -----\n"); |
JohnnyK | 41:cc1a8c63f159 | 102 | |
JohnnyK | 41:cc1a8c63f159 | 103 | HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt"); |
JohnnyK | 41:cc1a8c63f159 | 104 | |
JohnnyK | 41:cc1a8c63f159 | 105 | HttpResponse* get_res = get_req->send(); |
JohnnyK | 41:cc1a8c63f159 | 106 | if (!get_res) { |
JohnnyK | 41:cc1a8c63f159 | 107 | printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
JohnnyK | 41:cc1a8c63f159 | 108 | return 1; |
JohnnyK | 41:cc1a8c63f159 | 109 | } |
JohnnyK | 41:cc1a8c63f159 | 110 | printf("\n----- HTTPS GET response -----\n"); |
JohnnyK | 41:cc1a8c63f159 | 111 | dump_response(get_res); |
JohnnyK | 41:cc1a8c63f159 | 112 | |
JohnnyK | 41:cc1a8c63f159 | 113 | delete get_req; |
JohnnyK | 41:cc1a8c63f159 | 114 | } |
JohnnyK | 41:cc1a8c63f159 | 115 | |
JohnnyK | 41:cc1a8c63f159 | 116 | // POST request to httpbin.org |
JohnnyK | 41:cc1a8c63f159 | 117 | { |
JohnnyK | 41:cc1a8c63f159 | 118 | printf("\n----- HTTPS POST request -----\n"); |
JohnnyK | 41:cc1a8c63f159 | 119 | |
JohnnyK | 41:cc1a8c63f159 | 120 | HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post"); |
JohnnyK | 41:cc1a8c63f159 | 121 | post_req->set_header("Content-Type", "application/json"); |
JohnnyK | 41:cc1a8c63f159 | 122 | |
JohnnyK | 41:cc1a8c63f159 | 123 | const char body[] = "{\"hello\":\"world\"}"; |
JohnnyK | 41:cc1a8c63f159 | 124 | |
JohnnyK | 41:cc1a8c63f159 | 125 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
JohnnyK | 41:cc1a8c63f159 | 126 | if (!post_res) { |
JohnnyK | 41:cc1a8c63f159 | 127 | printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
JohnnyK | 41:cc1a8c63f159 | 128 | return 1; |
JohnnyK | 41:cc1a8c63f159 | 129 | } |
JohnnyK | 41:cc1a8c63f159 | 130 | |
JohnnyK | 41:cc1a8c63f159 | 131 | printf("\n----- HTTPS POST response -----\n"); |
JohnnyK | 41:cc1a8c63f159 | 132 | dump_response(post_res); |
JohnnyK | 41:cc1a8c63f159 | 133 | |
JohnnyK | 41:cc1a8c63f159 | 134 | delete post_req; |
JohnnyK | 41:cc1a8c63f159 | 135 | } |
JohnnyK | 41:cc1a8c63f159 | 136 | |
JohnnyK | 41:cc1a8c63f159 | 137 | while(osWaitForever); |
JohnnyK | 41:cc1a8c63f159 | 138 | } |
JohnnyK | 41:cc1a8c63f159 | 139 | |
JohnnyK | 41:cc1a8c63f159 | 140 | #endif |