this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sun Apr 07 10:52:37 2019 +0000
Revision:
90:ed0267eca7b5
Parent:
74:f26e846adfe9
https response format implemented and tested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ocomeni 74:f26e846adfe9 1
ocomeni 74:f26e846adfe9 2 #include "wifi_demo.h"
ocomeni 74:f26e846adfe9 3 #include "common_config.h"
ocomeni 74:f26e846adfe9 4 // Wifi-demo
ocomeni 74:f26e846adfe9 5
ocomeni 74:f26e846adfe9 6 RawSerial *device; // tx, rx
ocomeni 74:f26e846adfe9 7 extern EventQueue eventQueue(/* event count */ 20 * EVENTS_EVENT_SIZE);
ocomeni 74:f26e846adfe9 8 int chunkNum;
ocomeni 74:f26e846adfe9 9 void dump_response(HttpResponse* res) {
ocomeni 74:f26e846adfe9 10 device->printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
ocomeni 74:f26e846adfe9 11
ocomeni 74:f26e846adfe9 12 device->printf("Headers:\n");
ocomeni 74:f26e846adfe9 13 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
ocomeni 74:f26e846adfe9 14 device->printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
ocomeni 74:f26e846adfe9 15 }
ocomeni 90:ed0267eca7b5 16 char * body = (char *) res->get_body();
ocomeni 74:f26e846adfe9 17 device->printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
ocomeni 90:ed0267eca7b5 18 for (size_t ix = 0; ix < res->get_body_length(); ix++) {
ocomeni 90:ed0267eca7b5 19 device->printf("%02X: ", body[ix]);
ocomeni 90:ed0267eca7b5 20 if((ix % 32) == 0 and ix)
ocomeni 90:ed0267eca7b5 21 device->printf("\n");
ocomeni 90:ed0267eca7b5 22 }
ocomeni 90:ed0267eca7b5 23
ocomeni 74:f26e846adfe9 24 }
ocomeni 74:f26e846adfe9 25
ocomeni 74:f26e846adfe9 26 void completed(){
ocomeni 74:f26e846adfe9 27 }
ocomeni 74:f26e846adfe9 28 void dump_chunked_response(const char *at, uint32_t length) {
ocomeni 74:f26e846adfe9 29 device->printf("\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length);
ocomeni 74:f26e846adfe9 30 //device->printf("\n Try Print Header as string:\n\n ");
ocomeni 74:f26e846adfe9 31 //device->printf("recv %d [%.*s]\n", length, strstr((char *)at, "\r\n")-(char *)at, (char *)at);
ocomeni 74:f26e846adfe9 32 //if(false)
ocomeni 74:f26e846adfe9 33 if(chunkNum < 2)
ocomeni 74:f26e846adfe9 34 for(int i=0; i < length; i++){
ocomeni 74:f26e846adfe9 35
ocomeni 74:f26e846adfe9 36 while(device->writeable())
ocomeni 74:f26e846adfe9 37 {
ocomeni 74:f26e846adfe9 38 device->putc((uint8_t)at[i]);
ocomeni 74:f26e846adfe9 39 }
ocomeni 74:f26e846adfe9 40 //int resp = write( (const uint8_t *)at, (int) length, &completed, SERIAL_EVENT_TX_COMPLETE);
ocomeni 74:f26e846adfe9 41 }
ocomeni 74:f26e846adfe9 42 if(false)
ocomeni 74:f26e846adfe9 43 for (size_t ix = 0; ix < length; ix++) {
ocomeni 74:f26e846adfe9 44 device->printf("%02X: ", at[ix]);
ocomeni 74:f26e846adfe9 45 if((ix % 32) == 0 and ix)
ocomeni 74:f26e846adfe9 46 device->printf("\n");
ocomeni 74:f26e846adfe9 47 }
ocomeni 74:f26e846adfe9 48 device->printf("\n\n");
ocomeni 74:f26e846adfe9 49 chunkNum++;
ocomeni 74:f26e846adfe9 50 //device->printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
ocomeni 74:f26e846adfe9 51 }
ocomeni 74:f26e846adfe9 52
ocomeni 74:f26e846adfe9 53
ocomeni 74:f26e846adfe9 54 int wifi_demo_func(NetworkInterface* network){
ocomeni 74:f26e846adfe9 55 device = new RawSerial(USBTX, USBRX, DEFAULT_BAUD_RATE);
ocomeni 74:f26e846adfe9 56 if (!network) {
ocomeni 74:f26e846adfe9 57 device->printf("Cannot connect to the network, see serial output\n");
ocomeni 74:f26e846adfe9 58 return 1;
ocomeni 74:f26e846adfe9 59 }
ocomeni 74:f26e846adfe9 60 wait(1); // wait for 1 sec
ocomeni 74:f26e846adfe9 61
ocomeni 74:f26e846adfe9 62 mbed_trace_init();
ocomeni 90:ed0267eca7b5 63 #ifdef RUN_ALL
ocomeni 74:f26e846adfe9 64 wait(1); // wait for 1 sec
ocomeni 74:f26e846adfe9 65
ocomeni 74:f26e846adfe9 66 // GET request to os.mbed.com
ocomeni 74:f26e846adfe9 67 {
ocomeni 74:f26e846adfe9 68 chunkNum = 0;
ocomeni 74:f26e846adfe9 69 device->printf("\n----- HTTPS GET request -----\n");
ocomeni 74:f26e846adfe9 70
ocomeni 74:f26e846adfe9 71 HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt", &dump_chunked_response);
ocomeni 74:f26e846adfe9 72
ocomeni 74:f26e846adfe9 73 HttpResponse* get_res = get_req->send();
ocomeni 74:f26e846adfe9 74 if (!get_res) {
ocomeni 74:f26e846adfe9 75 device->printf("HttpRequest failed (error code %d)\n", get_req->get_error());
ocomeni 74:f26e846adfe9 76 return 1;
ocomeni 74:f26e846adfe9 77 }
ocomeni 74:f26e846adfe9 78 device->printf("\n----- HTTPS GET response -----\n");
ocomeni 74:f26e846adfe9 79 dump_response(get_res);
ocomeni 74:f26e846adfe9 80 delete get_req;
ocomeni 74:f26e846adfe9 81 }
ocomeni 74:f26e846adfe9 82
ocomeni 74:f26e846adfe9 83 wait(1); // wait for 1 sec
ocomeni 74:f26e846adfe9 84
ocomeni 74:f26e846adfe9 85 // Do a GET request to httpbin.org
ocomeni 74:f26e846adfe9 86 {
ocomeni 74:f26e846adfe9 87 chunkNum = 0;
ocomeni 74:f26e846adfe9 88 device->printf("\n----- HTTP GET request to httpbin.org -----\n");
ocomeni 74:f26e846adfe9 89
ocomeni 74:f26e846adfe9 90 // By default the body is automatically parsed and stored in a buffer, this is memory heavy.
ocomeni 74:f26e846adfe9 91 // To receive chunked response, pass in a callback as last parameter to the constructor.
ocomeni 74:f26e846adfe9 92 HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418");
ocomeni 74:f26e846adfe9 93
ocomeni 74:f26e846adfe9 94 HttpResponse* get_res = get_req->send();
ocomeni 74:f26e846adfe9 95 if (!get_res) {
ocomeni 74:f26e846adfe9 96 device->printf("HttpRequest failed (error code %d)\n", get_req->get_error());
ocomeni 74:f26e846adfe9 97 return 1;
ocomeni 74:f26e846adfe9 98 }
ocomeni 74:f26e846adfe9 99
ocomeni 74:f26e846adfe9 100 device->printf("\n----- HTTP GET response from httpbin.org -----\n");
ocomeni 74:f26e846adfe9 101 dump_response(get_res);
ocomeni 74:f26e846adfe9 102
ocomeni 74:f26e846adfe9 103 delete get_req;
ocomeni 74:f26e846adfe9 104 }
ocomeni 74:f26e846adfe9 105
ocomeni 74:f26e846adfe9 106 wait(1); // wait for 1 sec
ocomeni 74:f26e846adfe9 107
ocomeni 74:f26e846adfe9 108
ocomeni 74:f26e846adfe9 109 // Do a GET request to ovh.net
ocomeni 74:f26e846adfe9 110 if(false)
ocomeni 74:f26e846adfe9 111 {
ocomeni 74:f26e846adfe9 112 chunkNum = 0;
ocomeni 74:f26e846adfe9 113 device->printf("\n----- HTTP GET request to ovh.net (LARGE FILE) http://www.ovh.net/files/1Mio.dat -----\n");
ocomeni 74:f26e846adfe9 114 Timer t;
ocomeni 74:f26e846adfe9 115 // By default the body is automatically parsed and stored in a buffer, this is memory heavy.
ocomeni 74:f26e846adfe9 116 // To receive chunked response, pass in a callback as last parameter to the constructor.
ocomeni 74:f26e846adfe9 117 t.start();
ocomeni 74:f26e846adfe9 118 HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://www.ovh.net/files/1Mio.dat", &dump_chunked_response);
ocomeni 74:f26e846adfe9 119
ocomeni 74:f26e846adfe9 120 HttpResponse* get_res = get_req->send();
ocomeni 74:f26e846adfe9 121 if (!get_res) {
ocomeni 74:f26e846adfe9 122 device->printf("HttpRequest failed (error code %d)\n", get_req->get_error());
ocomeni 74:f26e846adfe9 123 return 1;
ocomeni 74:f26e846adfe9 124 }
ocomeni 74:f26e846adfe9 125
ocomeni 74:f26e846adfe9 126 device->printf("\n----- HTTP GET response from ovh.net -----\n");
ocomeni 74:f26e846adfe9 127 dump_response(get_res);
ocomeni 74:f26e846adfe9 128 t.stop();
ocomeni 74:f26e846adfe9 129 device->printf("The time taken was %f seconds\n", t.read());
ocomeni 74:f26e846adfe9 130
ocomeni 74:f26e846adfe9 131
ocomeni 74:f26e846adfe9 132 delete get_req;
ocomeni 74:f26e846adfe9 133 }
ocomeni 74:f26e846adfe9 134 wait(1); // wait for 1 sec
ocomeni 74:f26e846adfe9 135
ocomeni 74:f26e846adfe9 136 {
ocomeni 74:f26e846adfe9 137 device->printf("\n----- HTTPS GET request (small file!) -----\n");
ocomeni 74:f26e846adfe9 138
ocomeni 74:f26e846adfe9 139 HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt");
ocomeni 74:f26e846adfe9 140
ocomeni 74:f26e846adfe9 141 HttpResponse* get_res = get_req->send();
ocomeni 74:f26e846adfe9 142 if (!get_res) {
ocomeni 74:f26e846adfe9 143 device->printf("HttpRequest failed (error code %d)\n", get_req->get_error());
ocomeni 74:f26e846adfe9 144 return 1;
ocomeni 74:f26e846adfe9 145 }
ocomeni 74:f26e846adfe9 146 /*
ocomeni 74:f26e846adfe9 147 while (0 < (response = socket.recv(p, remaining))) {
ocomeni 74:f26e846adfe9 148 p += response;
ocomeni 74:f26e846adfe9 149 rcount += response;
ocomeni 74:f26e846adfe9 150 remaining -= response;
ocomeni 74:f26e846adfe9 151 }
ocomeni 74:f26e846adfe9 152 */
ocomeni 74:f26e846adfe9 153 device->printf("\n----- HTTPS GET response -----\n");
ocomeni 74:f26e846adfe9 154 dump_response(get_res);
ocomeni 74:f26e846adfe9 155
ocomeni 74:f26e846adfe9 156
ocomeni 74:f26e846adfe9 157
ocomeni 74:f26e846adfe9 158 delete get_req;
ocomeni 74:f26e846adfe9 159 }
ocomeni 74:f26e846adfe9 160 wait(1); // wait for 1 sec
ocomeni 74:f26e846adfe9 161
ocomeni 74:f26e846adfe9 162 // POST request to httpbin.org
ocomeni 74:f26e846adfe9 163 {
ocomeni 74:f26e846adfe9 164 device->printf("\n----- HTTPS POST request -----\n");
ocomeni 74:f26e846adfe9 165
ocomeni 74:f26e846adfe9 166 HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post");
ocomeni 74:f26e846adfe9 167 post_req->set_header("Content-Type", "application/json");
ocomeni 74:f26e846adfe9 168
ocomeni 74:f26e846adfe9 169 const char body[] = "{\"hello\":\"world\"}";
ocomeni 74:f26e846adfe9 170
ocomeni 74:f26e846adfe9 171 HttpResponse* post_res = post_req->send(body, strlen(body));
ocomeni 74:f26e846adfe9 172 if (!post_res) {
ocomeni 74:f26e846adfe9 173 device->printf("HttpRequest failed (error code %d)\n", post_req->get_error());
ocomeni 74:f26e846adfe9 174 return 1;
ocomeni 74:f26e846adfe9 175 }
ocomeni 74:f26e846adfe9 176
ocomeni 74:f26e846adfe9 177 device->printf("\n----- HTTPS POST response -----\n");
ocomeni 74:f26e846adfe9 178 dump_response(post_res);
ocomeni 74:f26e846adfe9 179
ocomeni 74:f26e846adfe9 180 delete post_req;
ocomeni 74:f26e846adfe9 181 }
ocomeni 74:f26e846adfe9 182
ocomeni 74:f26e846adfe9 183 wait(1); // wait for 1 sec
ocomeni 74:f26e846adfe9 184
ocomeni 74:f26e846adfe9 185 // POST request to ws.dnanudge.io:80
ocomeni 74:f26e846adfe9 186 {
ocomeni 74:f26e846adfe9 187 device->printf("\n----- HTTP POST request (http://ws.dnanudge.io/nudgebox/v1) -----\n");
ocomeni 74:f26e846adfe9 188
ocomeni 74:f26e846adfe9 189 HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://ws.dnanudge.io/nudgebox/v1");
ocomeni 74:f26e846adfe9 190 post_req->set_header("Host", "ws.dnanudge.io");
ocomeni 74:f26e846adfe9 191 post_req->set_header("Accept", "*/*");
ocomeni 74:f26e846adfe9 192 post_req->set_header("Content-Type", "application/octet-stream");
ocomeni 74:f26e846adfe9 193 post_req->set_header("Content-Length", "20");
ocomeni 74:f26e846adfe9 194 // 00 08 6a 48 f8 2d 8e 82 01 68
ocomeni 74:f26e846adfe9 195 const uint8_t body[] = {0x00, 0x08, 0x6a, 0x48, 0xf8, 0x2d, 0x8e, 0x82, 0x01, 0x68,
ocomeni 74:f26e846adfe9 196 // 65 6c 6c 6f 00 00 67 c3 19 f8
ocomeni 74:f26e846adfe9 197 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x67, 0xc3, 0x19, 0xf8};
ocomeni 74:f26e846adfe9 198
ocomeni 74:f26e846adfe9 199 HttpResponse* post_res = post_req->send(body, 20);
ocomeni 74:f26e846adfe9 200 if (!post_res) {
ocomeni 74:f26e846adfe9 201 device->printf("HttpRequest failed (error code %d)\n", post_req->get_error());
ocomeni 74:f26e846adfe9 202 return 1;
ocomeni 74:f26e846adfe9 203 }
ocomeni 74:f26e846adfe9 204
ocomeni 74:f26e846adfe9 205 device->printf("\n----- HTTPS POST response -----\n");
ocomeni 74:f26e846adfe9 206 dump_response(post_res);
ocomeni 74:f26e846adfe9 207
ocomeni 74:f26e846adfe9 208 delete post_req;
ocomeni 74:f26e846adfe9 209 }
ocomeni 74:f26e846adfe9 210
ocomeni 74:f26e846adfe9 211 wait(1); // wait for 1 sec
ocomeni 74:f26e846adfe9 212 // POST request to httpbin.org
ocomeni 74:f26e846adfe9 213 if(false)
ocomeni 74:f26e846adfe9 214 {
ocomeni 74:f26e846adfe9 215 device->printf("\n----- HTTPS POST request to AWS -----\n");
ocomeni 74:f26e846adfe9 216
ocomeni 74:f26e846adfe9 217 HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://q6bc0dxw7f.execute-api.eu-west-2.amazonaws.com/test/samples/v1");
ocomeni 74:f26e846adfe9 218 post_req->set_header("Content-Type", "application/json");
ocomeni 74:f26e846adfe9 219
ocomeni 74:f26e846adfe9 220 const char body[] =
ocomeni 74:f26e846adfe9 221 "{"
ocomeni 74:f26e846adfe9 222 "\"firstName\": \"Maria\", "
ocomeni 74:f26e846adfe9 223 "\"lastName\": \"Huntera\", "
ocomeni 74:f26e846adfe9 224 "\"dob\": \"1970-12-03\", "
ocomeni 74:f26e846adfe9 225 "\"mobile\": \"07841887580\", "
ocomeni 74:f26e846adfe9 226 "\"cartridgeId\": \"DN00000000RMPOE\", "
ocomeni 74:f26e846adfe9 227 "\"labSampleId\": \"DYYAK\""
ocomeni 74:f26e846adfe9 228 "}";
ocomeni 74:f26e846adfe9 229 HttpResponse* post_res = post_req->send(body, strlen(body));
ocomeni 74:f26e846adfe9 230 if (!post_res) {
ocomeni 74:f26e846adfe9 231 device->printf("HttpRequest failed (error code %d)\n", post_req->get_error());
ocomeni 74:f26e846adfe9 232 return 1;
ocomeni 74:f26e846adfe9 233 }
ocomeni 74:f26e846adfe9 234
ocomeni 74:f26e846adfe9 235 device->printf("\n----- HTTPS POST response from AWS -----\n");
ocomeni 74:f26e846adfe9 236 dump_response(post_res);
ocomeni 74:f26e846adfe9 237
ocomeni 74:f26e846adfe9 238 delete post_req;
ocomeni 74:f26e846adfe9 239 }
ocomeni 90:ed0267eca7b5 240
ocomeni 90:ed0267eca7b5 241 #endif
ocomeni 90:ed0267eca7b5 242
ocomeni 90:ed0267eca7b5 243 // POST request to dev2.dnanudge.io
ocomeni 90:ed0267eca7b5 244 if(true)
ocomeni 90:ed0267eca7b5 245 {
ocomeni 90:ed0267eca7b5 246 device->printf("\n----- HTTPS POST request to dev2.dnanudge.io -----\n");
ocomeni 90:ed0267eca7b5 247 TLSSocket* socket;
ocomeni 90:ed0267eca7b5 248 socket = new TLSSocket();
ocomeni 90:ed0267eca7b5 249 char hostName[] = "dev2.dnanudge.io";
ocomeni 90:ed0267eca7b5 250 nsapi_error_t r;
ocomeni 90:ed0267eca7b5 251 // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK)
ocomeni 90:ed0267eca7b5 252 r = socket->open(network);
ocomeni 90:ed0267eca7b5 253 if(r != NSAPI_ERROR_OK)
ocomeni 90:ed0267eca7b5 254 {
ocomeni 90:ed0267eca7b5 255 printf("TLS open failed!!\n");
ocomeni 90:ed0267eca7b5 256 return 0;
ocomeni 90:ed0267eca7b5 257 }
ocomeni 90:ed0267eca7b5 258 printf("TLS open passed!!\n");
ocomeni 90:ed0267eca7b5 259 r = socket->set_root_ca_cert(SSL_CA_PEM);
ocomeni 90:ed0267eca7b5 260 if(r != NSAPI_ERROR_OK)
ocomeni 90:ed0267eca7b5 261 {
ocomeni 90:ed0267eca7b5 262 printf("TLS set_root_ca_cert failed!!\n");
ocomeni 90:ed0267eca7b5 263 return 0;
ocomeni 90:ed0267eca7b5 264 }
ocomeni 90:ed0267eca7b5 265 printf("TLS set_root_ca_cert passed!!\n");
ocomeni 90:ed0267eca7b5 266 r = socket->connect(hostName, 443);
ocomeni 90:ed0267eca7b5 267 if(r != NSAPI_ERROR_OK)
ocomeni 90:ed0267eca7b5 268 {
ocomeni 90:ed0267eca7b5 269 printf("TLS connect failed for hostname %s!!\n", hostName);
ocomeni 90:ed0267eca7b5 270 return 0;
ocomeni 90:ed0267eca7b5 271 }
ocomeni 90:ed0267eca7b5 272 printf("TLS connection successful for https site : %s\n", hostName);
ocomeni 90:ed0267eca7b5 273 //HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://dev2.dnanudge.io/nudgebox/v1");
ocomeni 90:ed0267eca7b5 274 HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "https://dev2.dnanudge.io/nudgebox/v1");
ocomeni 90:ed0267eca7b5 275 //post_req->set_header("Content-Type", "application/json");
ocomeni 90:ed0267eca7b5 276 post_req->set_header("Accept", "*/*");
ocomeni 90:ed0267eca7b5 277 post_req->set_header("Content-Type", "application/octet-stream");
ocomeni 90:ed0267eca7b5 278 post_req->set_header("Content-Length", "20");
ocomeni 90:ed0267eca7b5 279
ocomeni 90:ed0267eca7b5 280 const char body[] = {0x00, 0x08, 0x6a, 0x48, 0xf8, 0x2d, 0x8e,
ocomeni 90:ed0267eca7b5 281 0x82, 0x01, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x67,
ocomeni 90:ed0267eca7b5 282 0xc3, 0x19, 0xf8};
ocomeni 90:ed0267eca7b5 283 HttpResponse* post_res = post_req->send(body, 20);
ocomeni 90:ed0267eca7b5 284 if (!post_res) {
ocomeni 90:ed0267eca7b5 285 device->printf("HttpRequest failed (error code %d)\n", post_req->get_error());
ocomeni 90:ed0267eca7b5 286 return 1;
ocomeni 90:ed0267eca7b5 287 }
ocomeni 90:ed0267eca7b5 288
ocomeni 90:ed0267eca7b5 289 device->printf("\n----- HTTPS POST response from dev2.dnanudge.io -----\n");
ocomeni 90:ed0267eca7b5 290 dump_response(post_res);
ocomeni 90:ed0267eca7b5 291
ocomeni 90:ed0267eca7b5 292 delete post_req;
ocomeni 90:ed0267eca7b5 293 }
ocomeni 90:ed0267eca7b5 294
ocomeni 74:f26e846adfe9 295 wait(1); // wait for 1 sec
ocomeni 74:f26e846adfe9 296 delete device;
ocomeni 74:f26e846adfe9 297 return 0;
ocomeni 74:f26e846adfe9 298 }