a
Fork of http-example by
Revision 25:a8be9f3a530c, committed 2018-01-11
- Comitter:
- Jan Jongboom
- Date:
- Thu Jan 11 14:39:11 2018 +0100
- Parent:
- 24:9faf23bbfb7f
- Child:
- 26:22f87edb433c
- Commit message:
- Update mbed-http, add chunked requests
Changed in this revision
--- a/README.md Wed Jan 03 11:26:52 2018 +0000
+++ b/README.md Thu Jan 11 14:39:11 2018 +0100
@@ -12,6 +12,8 @@
* Does a POST request to https://httpbin.org/post.
* HTTP demo with socket re-use.
* HTTPS demo with socket re-use.
+* HTTP demo over IPv6.
+* HTTPS demo with chunked requests.
Response parsing is done through [nodejs/http-parser](https://github.com/nodejs/http-parser).
@@ -43,3 +45,4 @@
* K64F with Ethernet.
* NUCLEO_F411RE with ESP8266.
* ODIN-W2 with WiFi.
+* K64F with Atmel 6LoWPAN shield.
--- a/mbed-http.lib Wed Jan 03 11:26:52 2018 +0000 +++ b/mbed-http.lib Thu Jan 11 14:39:11 2018 +0100 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/sandbox/code/mbed-http/#71fc1b1894f8 +https://developer.mbed.org/teams/sandbox/code/mbed-http/#47d5c90c9ceb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/source/main-https-chunked-request.cpp Thu Jan 11 14:39:11 2018 +0100
@@ -0,0 +1,101 @@
+/**
+ * This is an example of doing chunked requests, where you do not need to load the full request body
+ * into memory. You do this by adding a callback to the `send` function of the HTTP/HTTPS request.
+ */
+
+#include "select-demo.h"
+
+#if DEMO == DEMO_HTTPS_CHUNKED_REQUEST
+
+#include "mbed.h"
+#include "https_request.h"
+#include "easy-connect.h"
+
+Serial pc(USBTX, USBRX);
+
+/* List of trusted root CA certificates
+ * currently one: Comodo, the CA for reqres.in
+ *
+ * To add more root certificates, just concatenate them.
+ */
+const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
+ "MIICiTCCAg+gAwIBAgIQH0evqmIAcFBUTAGem2OZKjAKBggqhkjOPQQDAzCBhTEL\n"
+ "MAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UE\n"
+ "BxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxKzApBgNVBAMT\n"
+ "IkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDgwMzA2MDAw\n"
+ "MDAwWhcNMzgwMTE4MjM1OTU5WjCBhTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdy\n"
+ "ZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09N\n"
+ "T0RPIENBIExpbWl0ZWQxKzApBgNVBAMTIkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlv\n"
+ "biBBdXRob3JpdHkwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAQDR3svdcmCFYX7deSR\n"
+ "FtSrYpn1PlILBs5BAH+X4QokPB0BBO490o0JlwzgdeT6+3eKKvUDYEs2ixYjFq0J\n"
+ "cfRK9ChQtP6IHG4/bC8vCVlbpVsLM5niwz2J+Wos77LTBumjQjBAMB0GA1UdDgQW\n"
+ "BBR1cacZSBm8nZ3qQUfflMRId5nTeTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/\n"
+ "BAUwAwEB/zAKBggqhkjOPQQDAwNoADBlAjEA7wNbeqy3eApyt4jf/7VGFAkK+qDm\n"
+ "fQjGGoe9GKhzvSbKYAydzpmfz1wPMOG+FDHqAjAU9JM8SaczepBGR7NjfRObTrdv\n"
+ "GDeAU/7dIOA1mjbRxwG55tzd8/8dLDoWV9mSOdY=\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());
+}
+
+// Spread the message out over 3 different chunks
+const char * chunks[] = {
+ "{\"message\":",
+ "\"this is an example",
+ " of chunked encoding\"}"
+};
+
+int chunk_ix = 0;
+
+// Callback function, grab the next chunk and return it
+const void * get_chunk(size_t* out_size) {
+ // If you don't have any data left, set out_size to 0 and return a null pointer
+ if (chunk_ix == (sizeof(chunks) / sizeof(chunks[0]))) {
+ *out_size = 0;
+ return NULL;
+ }
+ const char *chunk = chunks[chunk_ix];
+ *out_size = strlen(chunk);
+ chunk_ix++;
+
+ return chunk;
+}
+
+int main() {
+ pc.baud(115200);
+
+ NetworkInterface* network = easy_connect(true);
+ if (!network) {
+ return 1;
+ }
+
+ // POST request to httpbin.org
+ {
+ HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://reqres.in/api/users");
+ post_req->set_debug(true);
+ post_req->set_header("Content-Type", "application/json");
+
+ // If you pass a callback here, the Transfer-Encoding header is automatically set to chunked
+ HttpResponse* post_res = post_req->send(&get_chunk);
+ if (!post_res) {
+ printf("HttpsRequest failed (error code %d)\n", post_req->get_error());
+ return 1;
+ }
+
+ printf("\n----- HTTPS POST response -----\n");
+ dump_response(post_res);
+
+ delete post_req;
+ }
+
+ wait(osWaitForever);
+}
+
+#endif
--- a/source/main-https-socket-reuse.cpp Wed Jan 03 11:26:52 2018 +0000
+++ b/source/main-https-socket-reuse.cpp Thu Jan 11 14:39:11 2018 +0100
@@ -55,10 +55,12 @@
mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
}
-void run() {
+int main() {
+ pc.baud(115200);
+
NetworkInterface* network = easy_connect(true);
if (!network) {
- return;
+ return 1;
}
// Create a TLS socket (which holds a TCPSocket)
@@ -68,7 +70,7 @@
socket->set_debug(true);
if (socket->connect() != 0) {
printf("TLS Connect failed %d\n", socket->error());
- return;
+ return 1;
}
// GET request to httpbin.org
@@ -79,7 +81,7 @@
HttpResponse* get_res = get_req->send();
if (!get_res) {
printf("HttpRequest failed (error code %d)\n", get_req->get_error());
- return;
+ return 1;
}
printf("\n----- HTTPS GET response -----\n");
dump_response(get_res);
@@ -98,7 +100,7 @@
HttpResponse* post_res = post_req->send(body, strlen(body));
if (!post_res) {
printf("HttpRequest failed (error code %d)\n", post_req->get_error());
- return;
+ return 1;
}
printf("\n----- HTTPS POST response -----\n");
@@ -112,13 +114,4 @@
Thread::wait(osWaitForever);
}
-int main() {
- pc.baud(115200);
-
- Thread t(osPriorityNormal, 8 * 1024);
- t.start(&run);
-
- Thread::wait(osWaitForever);
-}
-
#endif
--- a/source/main-https.cpp Wed Jan 03 11:26:52 2018 +0000
+++ b/source/main-https.cpp Thu Jan 11 14:39:11 2018 +0100
@@ -84,7 +84,7 @@
{
printf("\n----- HTTPS GET request -----\n");
- HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://developer.mbed.org/media/uploads/mbed_official/hello.txt");
+ HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt");
get_req->set_debug(true);
HttpResponse* get_res = get_req->send();
--- a/source/select-demo.h Wed Jan 03 11:26:52 2018 +0000 +++ b/source/select-demo.h Thu Jan 11 14:39:11 2018 +0100 @@ -6,7 +6,8 @@ #define DEMO_HTTP_IPV6 3 #define DEMO_HTTPS 4 #define DEMO_HTTPS_SOCKET_REUSE 5 +#define DEMO_HTTPS_CHUNKED_REQUEST 6 -#define DEMO DEMO_HTTP +#define DEMO DEMO_HTTPS_CHUNKED_REQUEST #endif // _SELECT_METHOD_H_
