wifi test
Dependencies: X_NUCLEO_IKS01A2 mbed-http
source/main-x.cpp@0:24d3eb812fd4, 2018-09-05 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 05 14:28:24 2018 +0000
- Revision:
- 0:24d3eb812fd4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:24d3eb812fd4 | 1 | |
JMF | 0:24d3eb812fd4 | 2 | |
JMF | 0:24d3eb812fd4 | 3 | #include "select-demo.h" |
JMF | 0:24d3eb812fd4 | 4 | |
JMF | 0:24d3eb812fd4 | 5 | /* ===================================================================== |
JMF | 0:24d3eb812fd4 | 6 | Copyright © 2016, Avnet (R) |
JMF | 0:24d3eb812fd4 | 7 | Contributors: |
JMF | 0:24d3eb812fd4 | 8 | * James M Flynn, www.em.avnet.com |
JMF | 0:24d3eb812fd4 | 9 | |
JMF | 0:24d3eb812fd4 | 10 | Licensed under the Apache License, Version 2.0 (the "License"); |
JMF | 0:24d3eb812fd4 | 11 | you may not use this file except in compliance with the License. |
JMF | 0:24d3eb812fd4 | 12 | You may obtain a copy of the License at |
JMF | 0:24d3eb812fd4 | 13 | http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 0:24d3eb812fd4 | 14 | Unless required by applicable law or agreed to in writing, |
JMF | 0:24d3eb812fd4 | 15 | software distributed under the License is distributed on an |
JMF | 0:24d3eb812fd4 | 16 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
JMF | 0:24d3eb812fd4 | 17 | either express or implied. See the License for the specific |
JMF | 0:24d3eb812fd4 | 18 | language governing permissions and limitations under the License. |
JMF | 0:24d3eb812fd4 | 19 | @file main.cpp / WNC14A2AInterface_HTTP_exampel |
JMF | 0:24d3eb812fd4 | 20 | @version 1.0 |
JMF | 0:24d3eb812fd4 | 21 | @date Dec 2016 |
JMF | 0:24d3eb812fd4 | 22 | ======================================================================== */ |
JMF | 0:24d3eb812fd4 | 23 | |
JMF | 0:24d3eb812fd4 | 24 | #if DEMO == DEMO_HTTPx |
JMF | 0:24d3eb812fd4 | 25 | |
JMF | 0:24d3eb812fd4 | 26 | #include "easy-connect.h" |
JMF | 0:24d3eb812fd4 | 27 | #include "http_request.h" |
JMF | 0:24d3eb812fd4 | 28 | #include "https_request.h" |
JMF | 0:24d3eb812fd4 | 29 | |
JMF | 0:24d3eb812fd4 | 30 | #define STREAM_CNT 10 //when we test streaming, this is how many times to stream the string |
JMF | 0:24d3eb812fd4 | 31 | #define STR_SIZE 150*(STREAM_CNT+1) //use a fixed size string buffer based on the streaming data count |
JMF | 0:24d3eb812fd4 | 32 | |
JMF | 0:24d3eb812fd4 | 33 | #define TOSTR(x) #x |
JMF | 0:24d3eb812fd4 | 34 | #define INTSTR(x) TOSTR(x) |
JMF | 0:24d3eb812fd4 | 35 | |
JMF | 0:24d3eb812fd4 | 36 | // |
JMF | 0:24d3eb812fd4 | 37 | // We do the same thing for both http and https, so create a macro to eaze the typing... |
JMF | 0:24d3eb812fd4 | 38 | // |
JMF | 0:24d3eb812fd4 | 39 | |
JMF | 0:24d3eb812fd4 | 40 | |
JMF | 0:24d3eb812fd4 | 41 | /* List of trusted root CA certificates |
JMF | 0:24d3eb812fd4 | 42 | * currently one: Let's Encrypt, the CA for httpbin.org |
JMF | 0:24d3eb812fd4 | 43 | * |
JMF | 0:24d3eb812fd4 | 44 | * To add more root certificates, just concatenate them. |
JMF | 0:24d3eb812fd4 | 45 | */ |
JMF | 0:24d3eb812fd4 | 46 | const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" |
JMF | 0:24d3eb812fd4 | 47 | "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" |
JMF | 0:24d3eb812fd4 | 48 | "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" |
JMF | 0:24d3eb812fd4 | 49 | "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" |
JMF | 0:24d3eb812fd4 | 50 | "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" |
JMF | 0:24d3eb812fd4 | 51 | "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" |
JMF | 0:24d3eb812fd4 | 52 | "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" |
JMF | 0:24d3eb812fd4 | 53 | "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" |
JMF | 0:24d3eb812fd4 | 54 | "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" |
JMF | 0:24d3eb812fd4 | 55 | "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n" |
JMF | 0:24d3eb812fd4 | 56 | "a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n" |
JMF | 0:24d3eb812fd4 | 57 | "/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n" |
JMF | 0:24d3eb812fd4 | 58 | "AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n" |
JMF | 0:24d3eb812fd4 | 59 | "CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n" |
JMF | 0:24d3eb812fd4 | 60 | "bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n" |
JMF | 0:24d3eb812fd4 | 61 | "c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n" |
JMF | 0:24d3eb812fd4 | 62 | "VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n" |
JMF | 0:24d3eb812fd4 | 63 | "ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n" |
JMF | 0:24d3eb812fd4 | 64 | "MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n" |
JMF | 0:24d3eb812fd4 | 65 | "Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n" |
JMF | 0:24d3eb812fd4 | 66 | "AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n" |
JMF | 0:24d3eb812fd4 | 67 | "uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n" |
JMF | 0:24d3eb812fd4 | 68 | "wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n" |
JMF | 0:24d3eb812fd4 | 69 | "X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" |
JMF | 0:24d3eb812fd4 | 70 | "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" |
JMF | 0:24d3eb812fd4 | 71 | "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" |
JMF | 0:24d3eb812fd4 | 72 | "-----END CERTIFICATE-----\n"; |
JMF | 0:24d3eb812fd4 | 73 | |
JMF | 0:24d3eb812fd4 | 74 | // |
JMF | 0:24d3eb812fd4 | 75 | // This example is setup to use MBED OS (5.2). It sets up a thread to call the different tests |
JMF | 0:24d3eb812fd4 | 76 | // so the default stack size can be varied. TLS is stack intensive and needs additional stack |
JMF | 0:24d3eb812fd4 | 77 | // to work effeciently. |
JMF | 0:24d3eb812fd4 | 78 | // |
JMF | 0:24d3eb812fd4 | 79 | |
JMF | 0:24d3eb812fd4 | 80 | // |
JMF | 0:24d3eb812fd4 | 81 | // The two test functions do the same set of tests, the first one uses standard HTTP methods while |
JMF | 0:24d3eb812fd4 | 82 | // the second test uses HTTPS. |
JMF | 0:24d3eb812fd4 | 83 | // |
JMF | 0:24d3eb812fd4 | 84 | |
JMF | 0:24d3eb812fd4 | 85 | void https_test_thread(void); //Thread that runs the two tests |
JMF | 0:24d3eb812fd4 | 86 | void test_http(NetworkInterface *net); //function makes standard HTTP calls |
JMF | 0:24d3eb812fd4 | 87 | void test_https(NetworkInterface *net); //function makes standard HTTPS calls |
JMF | 0:24d3eb812fd4 | 88 | |
JMF | 0:24d3eb812fd4 | 89 | Thread http_test(osPriorityNormal, 4*1024, NULL); |
JMF | 0:24d3eb812fd4 | 90 | |
JMF | 0:24d3eb812fd4 | 91 | int main() { |
JMF | 0:24d3eb812fd4 | 92 | |
JMF | 0:24d3eb812fd4 | 93 | printf("Test HTTP and HTTPS interface\n"); |
JMF | 0:24d3eb812fd4 | 94 | http_test.start(https_test_thread); |
JMF | 0:24d3eb812fd4 | 95 | wait(5); |
JMF | 0:24d3eb812fd4 | 96 | http_test.join(); |
JMF | 0:24d3eb812fd4 | 97 | printf(" - - - - - - - ALL DONE - - - - - - - \n"); |
JMF | 0:24d3eb812fd4 | 98 | } |
JMF | 0:24d3eb812fd4 | 99 | |
JMF | 0:24d3eb812fd4 | 100 | //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
JMF | 0:24d3eb812fd4 | 101 | // this is the Thread that calls each of the two tests. |
JMF | 0:24d3eb812fd4 | 102 | // |
JMF | 0:24d3eb812fd4 | 103 | void https_test_thread(void) { |
JMF | 0:24d3eb812fd4 | 104 | |
JMF | 0:24d3eb812fd4 | 105 | NetworkInterface *network = easy_connect(true); |
JMF | 0:24d3eb812fd4 | 106 | |
JMF | 0:24d3eb812fd4 | 107 | printf(" software.\n"); |
JMF | 0:24d3eb812fd4 | 108 | if (!network) { |
JMF | 0:24d3eb812fd4 | 109 | printf("Unable to connect to network!\n"); |
JMF | 0:24d3eb812fd4 | 110 | return; |
JMF | 0:24d3eb812fd4 | 111 | } |
JMF | 0:24d3eb812fd4 | 112 | printf("My IP Address is: %s \n\n", network->get_ip_address()); |
JMF | 0:24d3eb812fd4 | 113 | |
JMF | 0:24d3eb812fd4 | 114 | test_http(network); |
JMF | 0:24d3eb812fd4 | 115 | test_https(network); |
JMF | 0:24d3eb812fd4 | 116 | |
JMF | 0:24d3eb812fd4 | 117 | network->disconnect(); |
JMF | 0:24d3eb812fd4 | 118 | } |
JMF | 0:24d3eb812fd4 | 119 | |
JMF | 0:24d3eb812fd4 | 120 | |
JMF | 0:24d3eb812fd4 | 121 | //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
JMF | 0:24d3eb812fd4 | 122 | // Utility function to print out responses |
JMF | 0:24d3eb812fd4 | 123 | // |
JMF | 0:24d3eb812fd4 | 124 | void dump_response(HttpResponse* res) |
JMF | 0:24d3eb812fd4 | 125 | { |
JMF | 0:24d3eb812fd4 | 126 | printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
JMF | 0:24d3eb812fd4 | 127 | |
JMF | 0:24d3eb812fd4 | 128 | printf("Headers:\n"); |
JMF | 0:24d3eb812fd4 | 129 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
JMF | 0:24d3eb812fd4 | 130 | printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
JMF | 0:24d3eb812fd4 | 131 | } |
JMF | 0:24d3eb812fd4 | 132 | printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
JMF | 0:24d3eb812fd4 | 133 | } |
JMF | 0:24d3eb812fd4 | 134 | |
JMF | 0:24d3eb812fd4 | 135 | void stream_callback(const char *data, size_t len) |
JMF | 0:24d3eb812fd4 | 136 | { |
JMF | 0:24d3eb812fd4 | 137 | printf("Chunk Received:\n"); |
JMF | 0:24d3eb812fd4 | 138 | for( size_t x=0; x<len; x++ ) |
JMF | 0:24d3eb812fd4 | 139 | printf("%c",data[x]); |
JMF | 0:24d3eb812fd4 | 140 | printf("\n"); |
JMF | 0:24d3eb812fd4 | 141 | } |
JMF | 0:24d3eb812fd4 | 142 | |
JMF | 0:24d3eb812fd4 | 143 | //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
JMF | 0:24d3eb812fd4 | 144 | // test the HTTP client class |
JMF | 0:24d3eb812fd4 | 145 | // |
JMF | 0:24d3eb812fd4 | 146 | void test_http(NetworkInterface *net) |
JMF | 0:24d3eb812fd4 | 147 | { |
JMF | 0:24d3eb812fd4 | 148 | TCPSocket *socket = new TCPSocket(); |
JMF | 0:24d3eb812fd4 | 149 | |
JMF | 0:24d3eb812fd4 | 150 | printf(">>>>>>>>>>>><<<<<<<<<<<<\n"); |
JMF | 0:24d3eb812fd4 | 151 | printf(">>> TEST HTTPClient <<<\n"); |
JMF | 0:24d3eb812fd4 | 152 | printf(">>>>>>>>>>>><<<<<<<<<<<<\n\n"); |
JMF | 0:24d3eb812fd4 | 153 | |
JMF | 0:24d3eb812fd4 | 154 | nsapi_error_t open_result = socket->open(net); |
JMF | 0:24d3eb812fd4 | 155 | if (open_result != 0) { |
JMF | 0:24d3eb812fd4 | 156 | printf("Opening TCPSocket failed... %d\n", open_result); |
JMF | 0:24d3eb812fd4 | 157 | return; |
JMF | 0:24d3eb812fd4 | 158 | } |
JMF | 0:24d3eb812fd4 | 159 | |
JMF | 0:24d3eb812fd4 | 160 | nsapi_error_t connect_result = socket->connect("developer.mbed.org", 80); |
JMF | 0:24d3eb812fd4 | 161 | if (connect_result != 0) { |
JMF | 0:24d3eb812fd4 | 162 | printf("Connecting over TCPSocket failed... %d\n", connect_result); |
JMF | 0:24d3eb812fd4 | 163 | return; |
JMF | 0:24d3eb812fd4 | 164 | } |
JMF | 0:24d3eb812fd4 | 165 | else |
JMF | 0:24d3eb812fd4 | 166 | printf("Connected over TCP to developer.mbed.org\n\n"); |
JMF | 0:24d3eb812fd4 | 167 | |
JMF | 0:24d3eb812fd4 | 168 | printf(" >>>First, lets get a page from http://developer.mbed.org\n"); |
JMF | 0:24d3eb812fd4 | 169 | { |
JMF | 0:24d3eb812fd4 | 170 | HttpRequest* get_req = new HttpRequest(socket,HTTP_GET,"https://os.mbed.com/media/uploads/mbed_official/hello.txt"); |
JMF | 0:24d3eb812fd4 | 171 | HttpResponse* get_res = get_req->send(); |
JMF | 0:24d3eb812fd4 | 172 | if (!get_res) { |
JMF | 0:24d3eb812fd4 | 173 | printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
JMF | 0:24d3eb812fd4 | 174 | return; |
JMF | 0:24d3eb812fd4 | 175 | } |
JMF | 0:24d3eb812fd4 | 176 | |
JMF | 0:24d3eb812fd4 | 177 | printf("\n----- RESPONSE: -----\n"); |
JMF | 0:24d3eb812fd4 | 178 | dump_response(get_res); |
JMF | 0:24d3eb812fd4 | 179 | delete get_req; |
JMF | 0:24d3eb812fd4 | 180 | } |
JMF | 0:24d3eb812fd4 | 181 | delete socket; |
JMF | 0:24d3eb812fd4 | 182 | |
JMF | 0:24d3eb812fd4 | 183 | socket = new TCPSocket(); |
JMF | 0:24d3eb812fd4 | 184 | |
JMF | 0:24d3eb812fd4 | 185 | open_result = socket->open(net); |
JMF | 0:24d3eb812fd4 | 186 | if (open_result != 0) { |
JMF | 0:24d3eb812fd4 | 187 | printf("Opening TCPSocket failed... %d\n", open_result); |
JMF | 0:24d3eb812fd4 | 188 | return; |
JMF | 0:24d3eb812fd4 | 189 | } |
JMF | 0:24d3eb812fd4 | 190 | |
JMF | 0:24d3eb812fd4 | 191 | connect_result = socket->connect("httpbin.org", 80); |
JMF | 0:24d3eb812fd4 | 192 | if (connect_result != 0) { |
JMF | 0:24d3eb812fd4 | 193 | printf("Connecting over TCPSocket http://httpbin.org... %d\n", connect_result); |
JMF | 0:24d3eb812fd4 | 194 | return; |
JMF | 0:24d3eb812fd4 | 195 | } |
JMF | 0:24d3eb812fd4 | 196 | else |
JMF | 0:24d3eb812fd4 | 197 | printf("Connected over TCP to httpbin.org\n\n"); |
JMF | 0:24d3eb812fd4 | 198 | |
JMF | 0:24d3eb812fd4 | 199 | printf("\n\n >>>Post data... **\n"); |
JMF | 0:24d3eb812fd4 | 200 | { |
JMF | 0:24d3eb812fd4 | 201 | HttpRequest* post_req = new HttpRequest(socket, HTTP_POST, "http://httpbin.org/post"); |
JMF | 0:24d3eb812fd4 | 202 | post_req->set_header("Content-Type", "application/json"); |
JMF | 0:24d3eb812fd4 | 203 | const char body[] = "{\"hello\":\"world\"}," |
JMF | 0:24d3eb812fd4 | 204 | "{\"test\":\"1234\"}"; |
JMF | 0:24d3eb812fd4 | 205 | |
JMF | 0:24d3eb812fd4 | 206 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
JMF | 0:24d3eb812fd4 | 207 | if (!post_res) { |
JMF | 0:24d3eb812fd4 | 208 | printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
JMF | 0:24d3eb812fd4 | 209 | return; |
JMF | 0:24d3eb812fd4 | 210 | } |
JMF | 0:24d3eb812fd4 | 211 | |
JMF | 0:24d3eb812fd4 | 212 | printf("\n----- RESPONSE: -----\n"); |
JMF | 0:24d3eb812fd4 | 213 | dump_response(post_res); |
JMF | 0:24d3eb812fd4 | 214 | delete post_req; |
JMF | 0:24d3eb812fd4 | 215 | } |
JMF | 0:24d3eb812fd4 | 216 | |
JMF | 0:24d3eb812fd4 | 217 | printf("\n\n >>>Put data... \n"); |
JMF | 0:24d3eb812fd4 | 218 | { |
JMF | 0:24d3eb812fd4 | 219 | HttpRequest* put_req = new HttpRequest(socket, HTTP_PUT, "http://httpbin.org/put"); |
JMF | 0:24d3eb812fd4 | 220 | put_req->set_header("Content-Type", "application/json"); |
JMF | 0:24d3eb812fd4 | 221 | |
JMF | 0:24d3eb812fd4 | 222 | const char body[] = "This is a PUT test!"; |
JMF | 0:24d3eb812fd4 | 223 | |
JMF | 0:24d3eb812fd4 | 224 | HttpResponse* put_res = put_req->send(body, strlen(body)); |
JMF | 0:24d3eb812fd4 | 225 | if (!put_res) { |
JMF | 0:24d3eb812fd4 | 226 | printf("HttpRequest failed (error code %d)\n", put_req->get_error()); |
JMF | 0:24d3eb812fd4 | 227 | return; |
JMF | 0:24d3eb812fd4 | 228 | } |
JMF | 0:24d3eb812fd4 | 229 | |
JMF | 0:24d3eb812fd4 | 230 | printf("\n----- RESPONSE: -----\n"); |
JMF | 0:24d3eb812fd4 | 231 | dump_response(put_res); |
JMF | 0:24d3eb812fd4 | 232 | delete put_req; |
JMF | 0:24d3eb812fd4 | 233 | } |
JMF | 0:24d3eb812fd4 | 234 | |
JMF | 0:24d3eb812fd4 | 235 | printf("\n\n >>>Delete data... \n"); |
JMF | 0:24d3eb812fd4 | 236 | { |
JMF | 0:24d3eb812fd4 | 237 | HttpRequest* del_req = new HttpRequest(socket, HTTP_DELETE, "http://httpbin.org/delete"); |
JMF | 0:24d3eb812fd4 | 238 | del_req->set_header("Content-Type", "application/json"); |
JMF | 0:24d3eb812fd4 | 239 | |
JMF | 0:24d3eb812fd4 | 240 | HttpResponse* del_res = del_req->send(); |
JMF | 0:24d3eb812fd4 | 241 | if (!del_res) { |
JMF | 0:24d3eb812fd4 | 242 | printf("HttpRequest failed (error code %d)\n", del_req->get_error()); |
JMF | 0:24d3eb812fd4 | 243 | return; |
JMF | 0:24d3eb812fd4 | 244 | } |
JMF | 0:24d3eb812fd4 | 245 | |
JMF | 0:24d3eb812fd4 | 246 | printf("\n----- RESPONSE: -----\n"); |
JMF | 0:24d3eb812fd4 | 247 | dump_response(del_res); |
JMF | 0:24d3eb812fd4 | 248 | delete del_req; |
JMF | 0:24d3eb812fd4 | 249 | } |
JMF | 0:24d3eb812fd4 | 250 | |
JMF | 0:24d3eb812fd4 | 251 | printf("\n\n >>>HTTP:stream, send http://httpbin.org/stream/" INTSTR(STREAM_CNT) "... \n"); |
JMF | 0:24d3eb812fd4 | 252 | { |
JMF | 0:24d3eb812fd4 | 253 | HttpRequest* stream_req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/stream/" INTSTR(STREAM_CNT), |
JMF | 0:24d3eb812fd4 | 254 | stream_callback ); |
JMF | 0:24d3eb812fd4 | 255 | stream_req->send(); |
JMF | 0:24d3eb812fd4 | 256 | delete stream_req; |
JMF | 0:24d3eb812fd4 | 257 | } |
JMF | 0:24d3eb812fd4 | 258 | |
JMF | 0:24d3eb812fd4 | 259 | printf("\n\n >>>HTTP:Status...\n"); |
JMF | 0:24d3eb812fd4 | 260 | { |
JMF | 0:24d3eb812fd4 | 261 | HttpRequest* get_req = new HttpRequest(socket,HTTP_GET,"http://httpbin.org/get?show_env=1"); |
JMF | 0:24d3eb812fd4 | 262 | HttpResponse* get_res = get_req->send(); |
JMF | 0:24d3eb812fd4 | 263 | if (!get_res) { |
JMF | 0:24d3eb812fd4 | 264 | printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
JMF | 0:24d3eb812fd4 | 265 | return; |
JMF | 0:24d3eb812fd4 | 266 | } |
JMF | 0:24d3eb812fd4 | 267 | |
JMF | 0:24d3eb812fd4 | 268 | printf("\n----- RESPONSE: -----\n"); |
JMF | 0:24d3eb812fd4 | 269 | dump_response(get_res); |
JMF | 0:24d3eb812fd4 | 270 | delete get_req; |
JMF | 0:24d3eb812fd4 | 271 | } |
JMF | 0:24d3eb812fd4 | 272 | delete socket; |
JMF | 0:24d3eb812fd4 | 273 | } |
JMF | 0:24d3eb812fd4 | 274 | |
JMF | 0:24d3eb812fd4 | 275 | |
JMF | 0:24d3eb812fd4 | 276 | void dump_httpsresponse(HttpResponse* res) |
JMF | 0:24d3eb812fd4 | 277 | { |
JMF | 0:24d3eb812fd4 | 278 | mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
JMF | 0:24d3eb812fd4 | 279 | |
JMF | 0:24d3eb812fd4 | 280 | mbedtls_printf("Headers:\n"); |
JMF | 0:24d3eb812fd4 | 281 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
JMF | 0:24d3eb812fd4 | 282 | mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
JMF | 0:24d3eb812fd4 | 283 | } |
JMF | 0:24d3eb812fd4 | 284 | mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
JMF | 0:24d3eb812fd4 | 285 | } |
JMF | 0:24d3eb812fd4 | 286 | |
JMF | 0:24d3eb812fd4 | 287 | void stream_httpscallback(const char *data, size_t len) |
JMF | 0:24d3eb812fd4 | 288 | { |
JMF | 0:24d3eb812fd4 | 289 | printf("Chunk Received:\n"); |
JMF | 0:24d3eb812fd4 | 290 | for( size_t x=0; x<len; x++ ) |
JMF | 0:24d3eb812fd4 | 291 | printf("%c",data[x]); |
JMF | 0:24d3eb812fd4 | 292 | printf("\n"); |
JMF | 0:24d3eb812fd4 | 293 | } |
JMF | 0:24d3eb812fd4 | 294 | |
JMF | 0:24d3eb812fd4 | 295 | //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
JMF | 0:24d3eb812fd4 | 296 | // test the HTTPS client class |
JMF | 0:24d3eb812fd4 | 297 | // |
JMF | 0:24d3eb812fd4 | 298 | void test_https(NetworkInterface *net) |
JMF | 0:24d3eb812fd4 | 299 | { |
JMF | 0:24d3eb812fd4 | 300 | |
JMF | 0:24d3eb812fd4 | 301 | printf(">>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<\n"); |
JMF | 0:24d3eb812fd4 | 302 | printf(">>> TEST HTTPS - set up TLS connection <<<\n"); |
JMF | 0:24d3eb812fd4 | 303 | printf(">>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<\n\n"); |
JMF | 0:24d3eb812fd4 | 304 | |
JMF | 0:24d3eb812fd4 | 305 | TLSSocket* socket = new TLSSocket(net, "httpbin.org", 443, SSL_CA_PEM); |
JMF | 0:24d3eb812fd4 | 306 | socket->set_debug(true); |
JMF | 0:24d3eb812fd4 | 307 | if (socket->connect() != 0) { |
JMF | 0:24d3eb812fd4 | 308 | printf("TLS Connect failed %d\n", socket->error()); |
JMF | 0:24d3eb812fd4 | 309 | return; |
JMF | 0:24d3eb812fd4 | 310 | } |
JMF | 0:24d3eb812fd4 | 311 | |
JMF | 0:24d3eb812fd4 | 312 | printf("\n\n >>>Post data... **\n"); |
JMF | 0:24d3eb812fd4 | 313 | { |
JMF | 0:24d3eb812fd4 | 314 | HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "http://httpbin.org/post"); |
JMF | 0:24d3eb812fd4 | 315 | post_req->set_header("Content-Type", "application/json"); |
JMF | 0:24d3eb812fd4 | 316 | const char body[] = "{\"hello\":\"world\"}," |
JMF | 0:24d3eb812fd4 | 317 | "{\"test\":\"1234\"}"; |
JMF | 0:24d3eb812fd4 | 318 | |
JMF | 0:24d3eb812fd4 | 319 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
JMF | 0:24d3eb812fd4 | 320 | if (!post_res) { |
JMF | 0:24d3eb812fd4 | 321 | printf("HttpsRequest failed (error code %d)\n", post_req->get_error()); |
JMF | 0:24d3eb812fd4 | 322 | return; |
JMF | 0:24d3eb812fd4 | 323 | } |
JMF | 0:24d3eb812fd4 | 324 | |
JMF | 0:24d3eb812fd4 | 325 | printf("\n----- RESPONSE: -----\n"); |
JMF | 0:24d3eb812fd4 | 326 | dump_httpsresponse(post_res); |
JMF | 0:24d3eb812fd4 | 327 | delete post_req; |
JMF | 0:24d3eb812fd4 | 328 | } |
JMF | 0:24d3eb812fd4 | 329 | |
JMF | 0:24d3eb812fd4 | 330 | printf("\n\n >>>Put data... \n"); |
JMF | 0:24d3eb812fd4 | 331 | { |
JMF | 0:24d3eb812fd4 | 332 | HttpsRequest* put_req = new HttpsRequest(socket, HTTP_PUT, "http://httpbin.org/put"); |
JMF | 0:24d3eb812fd4 | 333 | put_req->set_header("Content-Type", "application/json"); |
JMF | 0:24d3eb812fd4 | 334 | |
JMF | 0:24d3eb812fd4 | 335 | const char body[] = "This is a PUT test!"; |
JMF | 0:24d3eb812fd4 | 336 | |
JMF | 0:24d3eb812fd4 | 337 | HttpResponse* put_res = put_req->send(body, strlen(body)); |
JMF | 0:24d3eb812fd4 | 338 | if (!put_res) { |
JMF | 0:24d3eb812fd4 | 339 | printf("HttpsRequest failed (error code %d)\n", put_req->get_error()); |
JMF | 0:24d3eb812fd4 | 340 | return; |
JMF | 0:24d3eb812fd4 | 341 | } |
JMF | 0:24d3eb812fd4 | 342 | |
JMF | 0:24d3eb812fd4 | 343 | printf("\n----- RESPONSE: -----\n"); |
JMF | 0:24d3eb812fd4 | 344 | dump_httpsresponse(put_res); |
JMF | 0:24d3eb812fd4 | 345 | delete put_req; |
JMF | 0:24d3eb812fd4 | 346 | } |
JMF | 0:24d3eb812fd4 | 347 | |
JMF | 0:24d3eb812fd4 | 348 | printf("\n\n >>>Delete data... \n"); |
JMF | 0:24d3eb812fd4 | 349 | { |
JMF | 0:24d3eb812fd4 | 350 | HttpsRequest* del_req = new HttpsRequest(socket, HTTP_DELETE, "http://httpbin.org/delete"); |
JMF | 0:24d3eb812fd4 | 351 | del_req->set_header("Content-Type", "application/json"); |
JMF | 0:24d3eb812fd4 | 352 | |
JMF | 0:24d3eb812fd4 | 353 | HttpResponse* del_res = del_req->send(); |
JMF | 0:24d3eb812fd4 | 354 | if (!del_res) { |
JMF | 0:24d3eb812fd4 | 355 | printf("HttpsRequest failed (error code %d)\n", del_req->get_error()); |
JMF | 0:24d3eb812fd4 | 356 | return; |
JMF | 0:24d3eb812fd4 | 357 | } |
JMF | 0:24d3eb812fd4 | 358 | |
JMF | 0:24d3eb812fd4 | 359 | printf("\n----- RESPONSE: -----\n"); |
JMF | 0:24d3eb812fd4 | 360 | dump_httpsresponse(del_res); |
JMF | 0:24d3eb812fd4 | 361 | delete del_req; |
JMF | 0:24d3eb812fd4 | 362 | } |
JMF | 0:24d3eb812fd4 | 363 | |
JMF | 0:24d3eb812fd4 | 364 | printf("\n\n >>>HTTPS:stream, send http://httpbin.org/stream/" INTSTR(STREAM_CNT)"... \n"); |
JMF | 0:24d3eb812fd4 | 365 | { |
JMF | 0:24d3eb812fd4 | 366 | HttpsRequest* stream_req = new HttpsRequest(socket, HTTP_GET, "http://httpbin.org/stream/" INTSTR(STREAM_CNT), |
JMF | 0:24d3eb812fd4 | 367 | stream_httpscallback ); |
JMF | 0:24d3eb812fd4 | 368 | stream_req->send(); |
JMF | 0:24d3eb812fd4 | 369 | delete stream_req; |
JMF | 0:24d3eb812fd4 | 370 | } |
JMF | 0:24d3eb812fd4 | 371 | |
JMF | 0:24d3eb812fd4 | 372 | printf("\n\n >>>HTTPS:Status...\n"); |
JMF | 0:24d3eb812fd4 | 373 | { |
JMF | 0:24d3eb812fd4 | 374 | HttpsRequest* get_req = new HttpsRequest(socket,HTTP_GET,"http://httpbin.org/get?show_env=1"); |
JMF | 0:24d3eb812fd4 | 375 | HttpResponse* get_res = get_req->send(); |
JMF | 0:24d3eb812fd4 | 376 | if (!get_res) { |
JMF | 0:24d3eb812fd4 | 377 | printf("HttpsRequest failed (error code %d)\n", get_req->get_error()); |
JMF | 0:24d3eb812fd4 | 378 | return; |
JMF | 0:24d3eb812fd4 | 379 | } |
JMF | 0:24d3eb812fd4 | 380 | |
JMF | 0:24d3eb812fd4 | 381 | printf("\n----- RESPONSE: -----\n"); |
JMF | 0:24d3eb812fd4 | 382 | dump_httpsresponse(get_res); |
JMF | 0:24d3eb812fd4 | 383 | delete get_req; |
JMF | 0:24d3eb812fd4 | 384 | } |
JMF | 0:24d3eb812fd4 | 385 | delete socket; |
JMF | 0:24d3eb812fd4 | 386 | } |
JMF | 0:24d3eb812fd4 | 387 | |
JMF | 0:24d3eb812fd4 | 388 | #endif // DEMO |