modified to get more signal info

Dependencies:   easy-connect mbed-http

Fork of http-example-wnc-modified by Tyler Davis

Files at this revision

API Documentation at this revision

Comitter:
Jan Jongboom
Date:
Tue Mar 28 14:53:07 2017 +0200
Parent:
11:72c089200302
Child:
13:5a210d227769
Commit message:
Add socket reuse demo's

Changed in this revision

source/main-http-socket-reuse.cpp Show annotated file Show diff for this revision Revisions of this file
source/main-https-socket-reuse.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/main-http-socket-reuse.cpp	Tue Mar 28 14:53:07 2017 +0200
@@ -0,0 +1,90 @@
+#include "select-demo.h"
+
+#if DEMO == DEMO_HTTP_SOCKET_REUSE
+
+#include "mbed.h"
+#include "easy-connect.h"
+#include "http_request.h"
+
+Serial pc(USBTX, USBRX);
+
+void dump_response(HttpResponse* res) {
+    printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+
+    printf("Headers:\n");
+    for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
+        printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
+    }
+    printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+}
+
+int main() {
+    pc.baud(115200);
+    // Connect to the network (see mbed_app.json for the connectivity method used)
+    NetworkInterface *network = easy_connect(true);
+    if (!network) {
+        printf("Cannot connect to the network, see serial output");
+        return 1;
+    }
+
+    // Create a TCP socket
+    printf("\n----- Setting up TCP connection -----\n");
+
+    TCPSocket* socket = new TCPSocket();
+    nsapi_error_t open_result = socket->open(network);
+    if (open_result != 0) {
+        printf("Opening TCPSocket failed... %d\n", open_result);
+        return 1;
+    }
+
+    nsapi_error_t connect_result = socket->connect("httpbin.org", 80);
+    if (connect_result != 0) {
+        printf("Connecting over TCPSocket failed... %d\n", connect_result);
+        return 1;
+    }
+
+    printf("Connected over TCP to httpbin.org:80\n");
+
+    // Do a GET request to httpbin.org
+    {
+        HttpRequest* get_req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/status/418");
+
+        // By default the body is automatically parsed and stored in a string, this is memory heavy.
+        // To receive chunked response, pass in a callback as third parameter to 'send'.
+        HttpResponse* get_res = get_req->send();
+        if (!get_res) {
+            printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+            return 1;
+        }
+
+        printf("\n----- HTTP GET response -----\n");
+        dump_response(get_res);
+
+        delete get_req;
+    }
+
+    // POST request to httpbin.org
+    {
+        HttpRequest* post_req = new HttpRequest(socket, HTTP_POST, "http://httpbin.org/post");
+        post_req->set_header("Content-Type", "application/json");
+
+        const char body[] = "{\"hello\":\"world\"}";
+
+        HttpResponse* post_res = post_req->send(body, strlen(body));
+        if (!post_res) {
+            printf("HttpRequest failed (error code %d)\n", post_req->get_error());
+            return 1;
+        }
+
+        printf("\n----- HTTP POST response -----\n");
+        dump_response(post_res);
+
+        delete post_req;
+    }
+
+    delete socket;
+
+    Thread::wait(osWaitForever);
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/main-https-socket-reuse.cpp	Tue Mar 28 14:53:07 2017 +0200
@@ -0,0 +1,117 @@
+#include "select-demo.h"
+
+/**
+ * This demo shows how to re-use sockets, so the TLS handshake only has to happen once
+ */
+
+#if DEMO == DEMO_HTTPS_SOCKET_REUSE
+
+#include "mbed.h"
+#include "easy-connect.h"
+#include "https_request.h"
+
+Serial pc(USBTX, USBRX);
+
+/* List of trusted root CA certificates
+ * currently one: Let's Encrypt, the CA for httpbin.org
+ *
+ * To add more root certificates, just concatenate them.
+ */
+const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
+    "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n"
+    "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n"
+    "DkRTVCBSb290IENBIFgzMB4XDTE2M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VzdC5jb20vRFNUUk9PVENBWDNDUkwu\n"
+    "Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n"
+    "AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n"
+    "uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n"
+    "wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n"
+    "X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n"
+    "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n"
+    "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n"
+    "-----END CERTIFICATE-----\n";
+
+void dump_response(HttpResponse* res) {
+    mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+
+    mbedtls_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());
+    }
+    mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+}
+
+int main() {
+    pc.baud(115200);
+
+    NetworkInterface* network = easy_connect(true);
+    if (!network) {
+        return 1;
+    }
+
+    // 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());
+        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) {
+            printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+            return 1;
+        }
+        printf("\n----- HTTPS GET response -----\n");
+        dump_response(get_res);
+
+        delete get_req;
+    }
+
+    // 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\"}";
+
+        HttpResponse* post_res = post_req->send(body, strlen(body));
+        if (!post_res) {
+            printf("HttpRequest failed (error code %d)\n", post_req->get_error());
+            return 1;
+        }
+
+        printf("\n----- HTTPS POST response -----\n");
+        dump_response(post_res);
+
+        delete post_req;
+    }
+
+    delete socket;
+
+    Thread::wait(osWaitForever);
+}
+
+#endif