sandbox / Mbed OS http-example

Dependencies:   mbed-http

Revision:
32:5fa61ebc2689
Parent:
31:66704f6f17c5
--- a/source/main-https-socket-reuse.cpp	Tue Mar 27 11:57:33 2018 +0200
+++ b/source/main-https-socket-reuse.cpp	Tue Oct 30 11:07:46 2018 +0800
@@ -7,8 +7,9 @@
 #if DEMO == DEMO_HTTPS_SOCKET_REUSE
 
 #include "mbed.h"
-#include "easy-connect.h"
+#include "mbed_trace.h"
 #include "https_request.h"
+#include "network-helper.h"
 
 /* List of trusted root CA certificates
  * currently one: Let's Encrypt, the CA for httpbin.org
@@ -44,35 +45,46 @@
     "-----END CERTIFICATE-----\n";
 
 void dump_response(HttpResponse* res) {
-    mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+    printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
 
-    mbedtls_printf("Headers:\n");
+    printf("Headers:\n");
     for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
-        mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
+        printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
     }
-    mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+    printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
 }
 
 int main() {
-    NetworkInterface* network = easy_connect(true);
+    NetworkInterface* network = connect_to_default_network_interface();
     if (!network) {
+        printf("Cannot connect to the network, see serial output\n");
         return 1;
     }
 
+    mbed_trace_init();
+
     // Create a TLS socket (which holds a TCPSocket)
     printf("\n----- Setting up TLS connection -----\n");
 
-    TLSSocket* socket = new TLSSocket(network, "httpbin.org", 443, SSL_CA_PEM);
-    socket->set_debug(true);
-    if (socket->connect() != 0) {
-        printf("TLS Connect failed %d\n", socket->error());
+    nsapi_error_t r;
+
+    TLSSocket* socket = new TLSSocket();
+    if ((r = socket->open(network)) != NSAPI_ERROR_OK) {
+        printf("TLS socket open failed (%d)\n", r);
+        return 1;
+    }
+    if ((r = socket->set_root_ca_cert(SSL_CA_PEM)) != NSAPI_ERROR_OK) {
+        printf("TLS socket set_root_ca_cert failed (%d)\n", r);
+        return 1;
+    }
+    if ((r = socket->connect("httpbin.org", 443)) != NSAPI_ERROR_OK) {
+        printf("TLS socket connect failed (%d)\n", r);
         return 1;
     }
 
     // GET request to httpbin.org
     {
         HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418");
-        get_req->set_debug(true);
 
         HttpResponse* get_res = get_req->send();
         if (!get_res) {
@@ -88,7 +100,6 @@
     // POST request to httpbin.org
     {
         HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "https://httpbin.org/post");
-        post_req->set_debug(true);
         post_req->set_header("Content-Type", "application/json");
 
         const char body[] = "{\"hello\":\"world\"}";
@@ -105,6 +116,7 @@
         delete post_req;
     }
 
+    socket->close();
     delete socket;
 
     wait(osWaitForever);