Just testing things, based off of http-example-wnc
Dependencies: easy-connect mbed-http
Fork of http-example-wnc by
main-x.cpp
00001 00002 #include "select-demo.h" 00003 00004 /* ===================================================================== 00005 Copyright © 2016, Avnet (R) 00006 00007 Contributors: 00008 * James M Flynn, www.em.avnet.com 00009 00010 Licensed under the Apache License, Version 2.0 (the "License"); 00011 you may not use this file except in compliance with the License. 00012 You may obtain a copy of the License at 00013 00014 http://www.apache.org/licenses/LICENSE-2.0 00015 00016 Unless required by applicable law or agreed to in writing, 00017 software distributed under the License is distributed on an 00018 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 00019 either express or implied. See the License for the specific 00020 language governing permissions and limitations under the License. 00021 00022 @file main.cpp / WNC14A2AInterface_HTTP_exampel 00023 @version 1.0 00024 @date Dec 2016 00025 00026 ======================================================================== */ 00027 00028 #if DEMO == DEMO_HTTPx 00029 00030 #include "mbed.h" 00031 #include "easy-connect.h" 00032 #include "http_request.h" 00033 #include "https_request.h" 00034 00035 #define STREAM_CNT 10 //when we test streaming, this is how many times to stream the string 00036 #define STR_SIZE 150*(STREAM_CNT+1) //use a fixed size string buffer based on the streaming data count 00037 00038 #define TOSTR(x) #x 00039 #define INTSTR(x) TOSTR(x) 00040 00041 // 00042 // We do the same thing for both http and https, so create a macro to eaze the typing... 00043 // 00044 00045 00046 /* List of trusted root CA certificates 00047 * currently one: Let's Encrypt, the CA for httpbin.org 00048 * 00049 * To add more root certificates, just concatenate them. 00050 */ 00051 const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" 00052 "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" 00053 "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" 00054 "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" 00055 "SjELMAkGA1UE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3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n" 00071 "AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n" 00072 "uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n" 00073 "wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n" 00074 "X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" 00075 "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" 00076 "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" 00077 "-----END CERTIFICATE-----\n"; 00078 00079 // 00080 // This example is setup to use MBED OS (5.2). It sets up a thread to call the different tests 00081 // so the default stack size can be varied. TLS is stack intensive and needs additional stack 00082 // to work effeciently. 00083 // 00084 00085 Serial pc(USBTX, USBRX); 00086 00087 // 00088 // The two test functions do the same set of tests, the first one uses standard HTTP methods while 00089 // the second test uses HTTPS. 00090 // 00091 00092 void https_test_thread(void); //Thread that runs the two tests 00093 void test_http(NetworkInterface *net); //function makes standard HTTP calls 00094 void test_https(NetworkInterface *net); //function makes standard HTTPS calls 00095 00096 int main() { 00097 Thread http_test(osPriorityNormal, DEFAULT_STACK_SIZE*4, NULL); 00098 pc.baud(115200); 00099 00100 printf("Testing HTTP and HTTPS interface software.\n"); 00101 http_test.start(https_test_thread); 00102 while (true) { 00103 osDelay(500); 00104 } 00105 } 00106 00107 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 00108 // this is the Thread that calls each of the two tests. 00109 // 00110 void https_test_thread(void) { 00111 00112 NetworkInterface *network = easy_connect(true); 00113 00114 if (!network) { 00115 printf("Unable to connect to network!\n"); 00116 return; 00117 } 00118 else 00119 printf("My IP Address is: %s \n\n", network->get_ip_address()); 00120 00121 test_http(network); 00122 test_https(network); 00123 00124 printf(" - - - - - - - ALL DONE - - - - - - - \n"); 00125 00126 network->disconnect(); 00127 delete network; 00128 Thread::wait(osWaitForever); 00129 } 00130 00131 00132 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 00133 // Utility function to print out responses 00134 // 00135 void dump_response(HttpResponse* res) 00136 { 00137 printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); 00138 00139 printf("Headers:\n"); 00140 for (size_t ix = 0; ix < res->get_headers_length(); ix++) { 00141 printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); 00142 } 00143 printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); 00144 } 00145 00146 void stream_callback(const char *data, size_t len) 00147 { 00148 printf("Chunk Received:\n"); 00149 for( size_t x=0; x<len; x++ ) 00150 printf("%c",data[x]); 00151 printf("\n"); 00152 } 00153 00154 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 00155 // test the HTTP client class 00156 // 00157 void test_http(NetworkInterface *net) 00158 { 00159 TCPSocket *socket = new TCPSocket(); 00160 00161 printf(">>>>>>>>>>>><<<<<<<<<<<<\n"); 00162 printf(">>> TEST HTTPClient <<<\n"); 00163 printf(">>>>>>>>>>>><<<<<<<<<<<<\n\n"); 00164 00165 nsapi_error_t open_result = socket->open(net); 00166 if (open_result != 0) { 00167 printf("Opening TCPSocket failed... %d\n", open_result); 00168 return; 00169 } 00170 00171 nsapi_error_t connect_result = socket->connect("developer.mbed.org", 80); 00172 if (connect_result != 0) { 00173 printf("Connecting over TCPSocket failed... %d\n", connect_result); 00174 return; 00175 } 00176 else 00177 printf("Connected over TCP to developer.mbed.org\n\n"); 00178 00179 printf(" >>>First, lets get a page from http://developer.mbed.org\n"); 00180 { 00181 HttpRequest* get_req = new HttpRequest(socket,HTTP_GET,"http://developer.mbed.org/media/uploads/mbed_official/hello.txt"); 00182 HttpResponse* get_res = get_req->send(); 00183 if (!get_res) { 00184 printf("HttpRequest failed (error code %d)\n", get_req->get_error()); 00185 return; 00186 } 00187 00188 printf("\n----- RESPONSE: -----\n"); 00189 dump_response(get_res); 00190 delete get_req; 00191 } 00192 delete socket; 00193 00194 socket = new TCPSocket(); 00195 00196 open_result = socket->open(net); 00197 if (open_result != 0) { 00198 printf("Opening TCPSocket failed... %d\n", open_result); 00199 return; 00200 } 00201 00202 connect_result = socket->connect("httpbin.org", 80); 00203 if (connect_result != 0) { 00204 printf("Connecting over TCPSocket http://httpbin.org... %d\n", connect_result); 00205 return; 00206 } 00207 else 00208 printf("Connected over TCP to httpbin.org\n\n"); 00209 00210 printf("\n\n >>>Post data... **\n"); 00211 { 00212 HttpRequest* post_req = new HttpRequest(socket, HTTP_POST, "http://httpbin.org/post"); 00213 post_req->set_header("Content-Type", "application/json"); 00214 const char body[] = "{\"hello\":\"world\"}," 00215 "{\"test\":\"1234\"}"; 00216 00217 HttpResponse* post_res = post_req->send(body, strlen(body)); 00218 if (!post_res) { 00219 printf("HttpRequest failed (error code %d)\n", post_req->get_error()); 00220 return; 00221 } 00222 00223 printf("\n----- RESPONSE: -----\n"); 00224 dump_response(post_res); 00225 delete post_req; 00226 } 00227 00228 printf("\n\n >>>Put data... \n"); 00229 { 00230 HttpRequest* put_req = new HttpRequest(socket, HTTP_PUT, "http://httpbin.org/put"); 00231 put_req->set_header("Content-Type", "application/json"); 00232 00233 const char body[] = "This is a PUT test!"; 00234 00235 HttpResponse* put_res = put_req->send(body, strlen(body)); 00236 if (!put_res) { 00237 printf("HttpRequest failed (error code %d)\n", put_req->get_error()); 00238 return; 00239 } 00240 00241 printf("\n----- RESPONSE: -----\n"); 00242 dump_response(put_res); 00243 delete put_req; 00244 } 00245 00246 printf("\n\n >>>Delete data... \n"); 00247 { 00248 HttpRequest* del_req = new HttpRequest(socket, HTTP_DELETE, "http://httpbin.org/delete"); 00249 del_req->set_header("Content-Type", "application/json"); 00250 00251 HttpResponse* del_res = del_req->send(); 00252 if (!del_res) { 00253 printf("HttpRequest failed (error code %d)\n", del_req->get_error()); 00254 return; 00255 } 00256 00257 printf("\n----- RESPONSE: -----\n"); 00258 dump_response(del_res); 00259 delete del_req; 00260 } 00261 00262 printf("\n\n >>>HTTP:stream, send http://httpbin.org/stream/" INTSTR(STREAM_CNT) "... \n"); 00263 { 00264 HttpRequest* stream_req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/stream/" INTSTR(STREAM_CNT), 00265 stream_callback ); 00266 stream_req->send(); 00267 delete stream_req; 00268 } 00269 00270 printf("\n\n >>>HTTP:Status...\n"); 00271 { 00272 HttpRequest* get_req = new HttpRequest(socket,HTTP_GET,"http://httpbin.org/get?show_env=1"); 00273 HttpResponse* get_res = get_req->send(); 00274 if (!get_res) { 00275 printf("HttpRequest failed (error code %d)\n", get_req->get_error()); 00276 return; 00277 } 00278 00279 printf("\n----- RESPONSE: -----\n"); 00280 dump_response(get_res); 00281 delete get_req; 00282 } 00283 delete socket; 00284 } 00285 00286 00287 void dump_httpsresponse(HttpResponse* res) 00288 { 00289 mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); 00290 00291 mbedtls_printf("Headers:\n"); 00292 for (size_t ix = 0; ix < res->get_headers_length(); ix++) { 00293 mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); 00294 } 00295 mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); 00296 } 00297 00298 void stream_httpscallback(const char *data, size_t len) 00299 { 00300 printf("Chunk Received:\n"); 00301 for( size_t x=0; x<len; x++ ) 00302 printf("%c",data[x]); 00303 printf("\n"); 00304 } 00305 00306 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 00307 // test the HTTPS client class 00308 // 00309 void test_https(NetworkInterface *net) 00310 { 00311 00312 printf(">>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<\n"); 00313 printf(">>> TEST HTTPS - set up TLS connection <<<\n"); 00314 printf(">>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<\n\n"); 00315 00316 TLSSocket* socket = new TLSSocket(net, "httpbin.org", 443, SSL_CA_PEM); 00317 socket->set_debug(true); 00318 if (socket->connect() != 0) { 00319 printf("TLS Connect failed %d\n", socket->error()); 00320 return; 00321 } 00322 00323 printf("\n\n >>>Post data... **\n"); 00324 { 00325 HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "http://httpbin.org/post"); 00326 post_req->set_header("Content-Type", "application/json"); 00327 const char body[] = "{\"hello\":\"world\"}," 00328 "{\"test\":\"1234\"}"; 00329 00330 HttpResponse* post_res = post_req->send(body, strlen(body)); 00331 if (!post_res) { 00332 printf("HttpsRequest failed (error code %d)\n", post_req->get_error()); 00333 return; 00334 } 00335 00336 printf("\n----- RESPONSE: -----\n"); 00337 dump_httpsresponse(post_res); 00338 delete post_req; 00339 } 00340 00341 printf("\n\n >>>Put data... \n"); 00342 { 00343 HttpsRequest* put_req = new HttpsRequest(socket, HTTP_PUT, "http://httpbin.org/put"); 00344 put_req->set_header("Content-Type", "application/json"); 00345 00346 const char body[] = "This is a PUT test!"; 00347 00348 HttpResponse* put_res = put_req->send(body, strlen(body)); 00349 if (!put_res) { 00350 printf("HttpsRequest failed (error code %d)\n", put_req->get_error()); 00351 return; 00352 } 00353 00354 printf("\n----- RESPONSE: -----\n"); 00355 dump_httpsresponse(put_res); 00356 delete put_req; 00357 } 00358 00359 printf("\n\n >>>Delete data... \n"); 00360 { 00361 HttpsRequest* del_req = new HttpsRequest(socket, HTTP_DELETE, "http://httpbin.org/delete"); 00362 del_req->set_header("Content-Type", "application/json"); 00363 00364 HttpResponse* del_res = del_req->send(); 00365 if (!del_res) { 00366 printf("HttpsRequest failed (error code %d)\n", del_req->get_error()); 00367 return; 00368 } 00369 00370 printf("\n----- RESPONSE: -----\n"); 00371 dump_httpsresponse(del_res); 00372 delete del_req; 00373 } 00374 00375 printf("\n\n >>>HTTP:stream, send http://httpbin.org/stream/" INTSTR(STREAM_CNT)"... \n"); 00376 { 00377 HttpsRequest* stream_req = new HttpsRequest(socket, HTTP_GET, "http://httpbin.org/stream/" INTSTR(STREAM_CNT), 00378 stream_httpscallback ); 00379 stream_req->send(); 00380 delete stream_req; 00381 } 00382 00383 printf("\n\n >>>HTTP:Status...\n"); 00384 { 00385 HttpsRequest* get_req = new HttpsRequest(socket,HTTP_GET,"http://httpbin.org/get?show_env=1"); 00386 HttpResponse* get_res = get_req->send(); 00387 if (!get_res) { 00388 printf("HttpsRequest failed (error code %d)\n", get_req->get_error()); 00389 return; 00390 } 00391 00392 printf("\n----- RESPONSE: -----\n"); 00393 dump_httpsresponse(get_res); 00394 delete get_req; 00395 } 00396 delete socket; 00397 } 00398 00399 #endif // DEMO 00400
Generated on Thu Jul 14 2022 09:37:19 by 1.7.2