this is using the mbed os version 5-13-1
source/main-https-chunked-request.cpp@73:6f5021cbe752, 2019-02-28 (annotated)
- Committer:
- ocomeni
- Date:
- Thu Feb 28 18:13:48 2019 +0000
- Revision:
- 73:6f5021cbe752
commit current application code
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ocomeni | 73:6f5021cbe752 | 1 | /** |
| ocomeni | 73:6f5021cbe752 | 2 | * This is an example of doing chunked requests, where you do not need to load the full request body |
| ocomeni | 73:6f5021cbe752 | 3 | * into memory. You do this by adding a callback to the `send` function of the HTTP/HTTPS request. |
| ocomeni | 73:6f5021cbe752 | 4 | */ |
| ocomeni | 73:6f5021cbe752 | 5 | |
| ocomeni | 73:6f5021cbe752 | 6 | #include "select-demo.h" |
| ocomeni | 73:6f5021cbe752 | 7 | |
| ocomeni | 73:6f5021cbe752 | 8 | #if DEMO == DEMO_HTTPS_CHUNKED_REQUEST |
| ocomeni | 73:6f5021cbe752 | 9 | |
| ocomeni | 73:6f5021cbe752 | 10 | #include "mbed.h" |
| ocomeni | 73:6f5021cbe752 | 11 | #include "mbed_trace.h" |
| ocomeni | 73:6f5021cbe752 | 12 | #include "https_request.h" |
| ocomeni | 73:6f5021cbe752 | 13 | #include "network-helper.h" |
| ocomeni | 73:6f5021cbe752 | 14 | |
| ocomeni | 73:6f5021cbe752 | 15 | /* List of trusted root CA certificates |
| ocomeni | 73:6f5021cbe752 | 16 | * currently one: Comodo, the CA for reqres.in |
| ocomeni | 73:6f5021cbe752 | 17 | * |
| ocomeni | 73:6f5021cbe752 | 18 | * To add more root certificates, just concatenate them. |
| ocomeni | 73:6f5021cbe752 | 19 | */ |
| ocomeni | 73:6f5021cbe752 | 20 | const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" |
| ocomeni | 73:6f5021cbe752 | 21 | "MIICiTCCAg+gAwIBAgIQH0evqmIAcFBUTAGem2OZKjAKBggqhkjOPQQDAzCBhTEL\n" |
| ocomeni | 73:6f5021cbe752 | 22 | "MAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UE\n" |
| ocomeni | 73:6f5021cbe752 | 23 | "BxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxKzApBgNVBAMT\n" |
| ocomeni | 73:6f5021cbe752 | 24 | "IkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDgwMzA2MDAw\n" |
| ocomeni | 73:6f5021cbe752 | 25 | "MDAwWhcNMzgwMTE4MjM1OTU5WjCBhTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdy\n" |
| ocomeni | 73:6f5021cbe752 | 26 | "ZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09N\n" |
| ocomeni | 73:6f5021cbe752 | 27 | "T0RPIENBIExpbWl0ZWQxKzApBgNVBAMTIkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlv\n" |
| ocomeni | 73:6f5021cbe752 | 28 | "biBBdXRob3JpdHkwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAQDR3svdcmCFYX7deSR\n" |
| ocomeni | 73:6f5021cbe752 | 29 | "FtSrYpn1PlILBs5BAH+X4QokPB0BBO490o0JlwzgdeT6+3eKKvUDYEs2ixYjFq0J\n" |
| ocomeni | 73:6f5021cbe752 | 30 | "cfRK9ChQtP6IHG4/bC8vCVlbpVsLM5niwz2J+Wos77LTBumjQjBAMB0GA1UdDgQW\n" |
| ocomeni | 73:6f5021cbe752 | 31 | "BBR1cacZSBm8nZ3qQUfflMRId5nTeTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/\n" |
| ocomeni | 73:6f5021cbe752 | 32 | "BAUwAwEB/zAKBggqhkjOPQQDAwNoADBlAjEA7wNbeqy3eApyt4jf/7VGFAkK+qDm\n" |
| ocomeni | 73:6f5021cbe752 | 33 | "fQjGGoe9GKhzvSbKYAydzpmfz1wPMOG+FDHqAjAU9JM8SaczepBGR7NjfRObTrdv\n" |
| ocomeni | 73:6f5021cbe752 | 34 | "GDeAU/7dIOA1mjbRxwG55tzd8/8dLDoWV9mSOdY=\n" |
| ocomeni | 73:6f5021cbe752 | 35 | "-----END CERTIFICATE-----\n"; |
| ocomeni | 73:6f5021cbe752 | 36 | |
| ocomeni | 73:6f5021cbe752 | 37 | void dump_response(HttpResponse* res) { |
| ocomeni | 73:6f5021cbe752 | 38 | printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
| ocomeni | 73:6f5021cbe752 | 39 | |
| ocomeni | 73:6f5021cbe752 | 40 | printf("Headers:\n"); |
| ocomeni | 73:6f5021cbe752 | 41 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
| ocomeni | 73:6f5021cbe752 | 42 | printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
| ocomeni | 73:6f5021cbe752 | 43 | } |
| ocomeni | 73:6f5021cbe752 | 44 | printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
| ocomeni | 73:6f5021cbe752 | 45 | } |
| ocomeni | 73:6f5021cbe752 | 46 | |
| ocomeni | 73:6f5021cbe752 | 47 | // Spread the message out over 3 different chunks |
| ocomeni | 73:6f5021cbe752 | 48 | const char * chunks[] = { |
| ocomeni | 73:6f5021cbe752 | 49 | "{\"message\":", |
| ocomeni | 73:6f5021cbe752 | 50 | "\"this is an example", |
| ocomeni | 73:6f5021cbe752 | 51 | " of chunked encoding\"}" |
| ocomeni | 73:6f5021cbe752 | 52 | }; |
| ocomeni | 73:6f5021cbe752 | 53 | |
| ocomeni | 73:6f5021cbe752 | 54 | int chunk_ix = 0; |
| ocomeni | 73:6f5021cbe752 | 55 | |
| ocomeni | 73:6f5021cbe752 | 56 | // Callback function, grab the next chunk and return it |
| ocomeni | 73:6f5021cbe752 | 57 | const void * get_chunk(uint32_t* out_size) { |
| ocomeni | 73:6f5021cbe752 | 58 | // If you don't have any data left, set out_size to 0 and return a null pointer |
| ocomeni | 73:6f5021cbe752 | 59 | if (chunk_ix == (sizeof(chunks) / sizeof(chunks[0]))) { |
| ocomeni | 73:6f5021cbe752 | 60 | *out_size = 0; |
| ocomeni | 73:6f5021cbe752 | 61 | return NULL; |
| ocomeni | 73:6f5021cbe752 | 62 | } |
| ocomeni | 73:6f5021cbe752 | 63 | const char *chunk = chunks[chunk_ix]; |
| ocomeni | 73:6f5021cbe752 | 64 | *out_size = strlen(chunk); |
| ocomeni | 73:6f5021cbe752 | 65 | chunk_ix++; |
| ocomeni | 73:6f5021cbe752 | 66 | |
| ocomeni | 73:6f5021cbe752 | 67 | return chunk; |
| ocomeni | 73:6f5021cbe752 | 68 | } |
| ocomeni | 73:6f5021cbe752 | 69 | |
| ocomeni | 73:6f5021cbe752 | 70 | int main() { |
| ocomeni | 73:6f5021cbe752 | 71 | NetworkInterface* network = connect_to_default_network_interface(); |
| ocomeni | 73:6f5021cbe752 | 72 | if (!network) { |
| ocomeni | 73:6f5021cbe752 | 73 | printf("Cannot connect to the network, see serial output\n"); |
| ocomeni | 73:6f5021cbe752 | 74 | return 1; |
| ocomeni | 73:6f5021cbe752 | 75 | } |
| ocomeni | 73:6f5021cbe752 | 76 | |
| ocomeni | 73:6f5021cbe752 | 77 | mbed_trace_init(); |
| ocomeni | 73:6f5021cbe752 | 78 | |
| ocomeni | 73:6f5021cbe752 | 79 | // This example also logs the raw request, you can do this by calling 'set_request_log_buffer' on the request |
| ocomeni | 73:6f5021cbe752 | 80 | uint8_t *request_buffer = (uint8_t*)calloc(2048, 1); |
| ocomeni | 73:6f5021cbe752 | 81 | |
| ocomeni | 73:6f5021cbe752 | 82 | // POST request to reqres.in |
| ocomeni | 73:6f5021cbe752 | 83 | { |
| ocomeni | 73:6f5021cbe752 | 84 | HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://reqres.in/api/users"); |
| ocomeni | 73:6f5021cbe752 | 85 | post_req->set_header("Content-Type", "application/json"); |
| ocomeni | 73:6f5021cbe752 | 86 | post_req->set_request_log_buffer(request_buffer, 2048); |
| ocomeni | 73:6f5021cbe752 | 87 | |
| ocomeni | 73:6f5021cbe752 | 88 | // If you pass a callback here, the Transfer-Encoding header is automatically set to chunked |
| ocomeni | 73:6f5021cbe752 | 89 | HttpResponse* post_res = post_req->send(&get_chunk); |
| ocomeni | 73:6f5021cbe752 | 90 | if (!post_res) { |
| ocomeni | 73:6f5021cbe752 | 91 | printf("HttpsRequest failed (error code %d)\n", post_req->get_error()); |
| ocomeni | 73:6f5021cbe752 | 92 | return 1; |
| ocomeni | 73:6f5021cbe752 | 93 | } |
| ocomeni | 73:6f5021cbe752 | 94 | |
| ocomeni | 73:6f5021cbe752 | 95 | // Log the raw request that went over the line (if you decode the hex you can see the chunked parts) |
| ocomeni | 73:6f5021cbe752 | 96 | // e.g. in Node.js (take the output from below): |
| ocomeni | 73:6f5021cbe752 | 97 | // '50 4f 53 54 20'.split(' ').map(c=>parseInt(c,16)).map(c=>String.fromCharCode(c)).join('') |
| ocomeni | 73:6f5021cbe752 | 98 | printf("\n----- Request buffer -----\n"); |
| ocomeni | 73:6f5021cbe752 | 99 | for (size_t ix = 0; ix < post_req->get_request_log_buffer_length(); ix++) { |
| ocomeni | 73:6f5021cbe752 | 100 | printf("%02x ", request_buffer[ix]); |
| ocomeni | 73:6f5021cbe752 | 101 | } |
| ocomeni | 73:6f5021cbe752 | 102 | printf("\n"); |
| ocomeni | 73:6f5021cbe752 | 103 | |
| ocomeni | 73:6f5021cbe752 | 104 | printf("\n----- HTTPS POST response -----\n"); |
| ocomeni | 73:6f5021cbe752 | 105 | dump_response(post_res); |
| ocomeni | 73:6f5021cbe752 | 106 | |
| ocomeni | 73:6f5021cbe752 | 107 | delete post_req; |
| ocomeni | 73:6f5021cbe752 | 108 | } |
| ocomeni | 73:6f5021cbe752 | 109 | |
| ocomeni | 73:6f5021cbe752 | 110 | wait(osWaitForever); |
| ocomeni | 73:6f5021cbe752 | 111 | } |
| ocomeni | 73:6f5021cbe752 | 112 | |
| ocomeni | 73:6f5021cbe752 | 113 | #endif |