Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vpcola 0:f1d3878b8dd9 1 #include "DownloadFile.h"
vpcola 0:f1d3878b8dd9 2 #include "https_request.h"
vpcola 0:f1d3878b8dd9 3
vpcola 0:f1d3878b8dd9 4 void dump_response(HttpResponse* res)
vpcola 0:f1d3878b8dd9 5 {
vpcola 0:f1d3878b8dd9 6 mbedtls_printf("\r\nStatus: %d - %s\r\n", res->get_status_code(), res->get_status_message().c_str());
vpcola 0:f1d3878b8dd9 7
vpcola 0:f1d3878b8dd9 8 mbedtls_printf("Headers:\r\n");
vpcola 0:f1d3878b8dd9 9 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
vpcola 0:f1d3878b8dd9 10 mbedtls_printf("\t%s: %s\r\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
vpcola 0:f1d3878b8dd9 11 }
vpcola 0:f1d3878b8dd9 12 mbedtls_printf("\nBody (%d bytes):\r\n\r\n%s\r\n", res->get_body_length(), res->get_body_as_string().c_str());
vpcola 0:f1d3878b8dd9 13 }
vpcola 0:f1d3878b8dd9 14
vpcola 0:f1d3878b8dd9 15 static unsigned int base64enc_len(const char *str)
vpcola 0:f1d3878b8dd9 16 {
vpcola 0:f1d3878b8dd9 17 return (((strlen(str)-1)/3)+1)<<2;
vpcola 0:f1d3878b8dd9 18 }
vpcola 0:f1d3878b8dd9 19
vpcola 0:f1d3878b8dd9 20 static void base64enc(const char *input, unsigned int length, char *output)
vpcola 0:f1d3878b8dd9 21 {
vpcola 0:f1d3878b8dd9 22 static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
vpcola 0:f1d3878b8dd9 23 unsigned int c, c1, c2, c3;
vpcola 0:f1d3878b8dd9 24 for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) {
vpcola 0:f1d3878b8dd9 25 c1 = ((((unsigned char)*((unsigned char *)&input[i]))));
vpcola 0:f1d3878b8dd9 26 c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0;
vpcola 0:f1d3878b8dd9 27 c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0;
vpcola 0:f1d3878b8dd9 28
vpcola 0:f1d3878b8dd9 29 c = ((c1 & 0xFC) >> 2);
vpcola 0:f1d3878b8dd9 30 output[j+0] = base64[c];
vpcola 0:f1d3878b8dd9 31 c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
vpcola 0:f1d3878b8dd9 32 output[j+1] = base64[c];
vpcola 0:f1d3878b8dd9 33 c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
vpcola 0:f1d3878b8dd9 34 output[j+2] = (length>i+1)?base64[c]:'=';
vpcola 0:f1d3878b8dd9 35 c = (c3 & 0x3F);
vpcola 0:f1d3878b8dd9 36 output[j+3] = (length>i+2)?base64[c]:'=';
vpcola 0:f1d3878b8dd9 37 }
vpcola 0:f1d3878b8dd9 38 output[(((length-1)/3)+1)<<2] = '\0';
vpcola 0:f1d3878b8dd9 39 }
vpcola 0:f1d3878b8dd9 40
vpcola 0:f1d3878b8dd9 41 static string encode(const string& str)
vpcola 0:f1d3878b8dd9 42 {
vpcola 0:f1d3878b8dd9 43 char* out = new char[ base64enc_len(str.c_str()) ];
vpcola 0:f1d3878b8dd9 44 base64enc(str.c_str(), str.length(), out);
vpcola 0:f1d3878b8dd9 45 string res(out);
vpcola 0:f1d3878b8dd9 46 delete[] out;
vpcola 0:f1d3878b8dd9 47 return res;
vpcola 0:f1d3878b8dd9 48 }
vpcola 0:f1d3878b8dd9 49
vpcola 0:f1d3878b8dd9 50 void DownloadFile::basic_auth(const char * user, const char * password)
vpcola 0:f1d3878b8dd9 51 {
vpcola 0:f1d3878b8dd9 52 authstr = user;
vpcola 0:f1d3878b8dd9 53 authstr += ":";
vpcola 0:f1d3878b8dd9 54 authstr += password;
vpcola 0:f1d3878b8dd9 55 printf("Auth Str : %s\r\n", authstr.c_str());
vpcola 0:f1d3878b8dd9 56
vpcola 0:f1d3878b8dd9 57 std::string base64str = encode(authstr);
vpcola 0:f1d3878b8dd9 58 printf("Base64 conversion : %s\r\n", base64str.c_str());
vpcola 0:f1d3878b8dd9 59
vpcola 0:f1d3878b8dd9 60 authstr = "Basic " + base64str;
vpcola 0:f1d3878b8dd9 61 printf("Authorization: %s\r\n", authstr.c_str());
vpcola 0:f1d3878b8dd9 62 }
vpcola 0:f1d3878b8dd9 63
vpcola 0:f1d3878b8dd9 64 HttpResponse* DownloadFile::get_file(const char * url)
vpcola 0:f1d3878b8dd9 65 {
vpcola 0:f1d3878b8dd9 66 if (url == NULL)
vpcola 0:f1d3878b8dd9 67 return NULL;
vpcola 0:f1d3878b8dd9 68
vpcola 0:f1d3878b8dd9 69 if(get_req != NULL)
vpcola 0:f1d3878b8dd9 70 delete get_req;
vpcola 0:f1d3878b8dd9 71
vpcola 0:f1d3878b8dd9 72 HttpResponse* get_res;
vpcola 0:f1d3878b8dd9 73 Callback<void(const char *at, size_t length)> aBodyCallback = NULL;
vpcola 0:f1d3878b8dd9 74
vpcola 0:f1d3878b8dd9 75 if (fp != NULL)
vpcola 0:f1d3878b8dd9 76 aBodyCallback = mbed::callback(this, &DownloadFile::body_callback);
vpcola 0:f1d3878b8dd9 77
vpcola 0:f1d3878b8dd9 78 if (useSSL)
vpcola 0:f1d3878b8dd9 79 {
vpcola 0:f1d3878b8dd9 80 get_req_ssl = new HttpsRequest(network,
vpcola 0:f1d3878b8dd9 81 pem, HTTP_GET,
vpcola 0:f1d3878b8dd9 82 url,
vpcola 0:f1d3878b8dd9 83 aBodyCallback);
vpcola 0:f1d3878b8dd9 84
vpcola 0:f1d3878b8dd9 85 if (!authstr.empty())
vpcola 0:f1d3878b8dd9 86 get_req_ssl->set_header("Authorization", authstr.c_str());
vpcola 0:f1d3878b8dd9 87
vpcola 0:f1d3878b8dd9 88 get_req_ssl->set_debug(true);
vpcola 0:f1d3878b8dd9 89
vpcola 0:f1d3878b8dd9 90 get_res = get_req_ssl->send();
vpcola 0:f1d3878b8dd9 91
vpcola 0:f1d3878b8dd9 92 }
vpcola 0:f1d3878b8dd9 93 else
vpcola 0:f1d3878b8dd9 94 {
vpcola 0:f1d3878b8dd9 95 get_req = new HttpRequest(network,
vpcola 0:f1d3878b8dd9 96 HTTP_GET,
vpcola 0:f1d3878b8dd9 97 url,
vpcola 0:f1d3878b8dd9 98 aBodyCallback);
vpcola 0:f1d3878b8dd9 99
vpcola 0:f1d3878b8dd9 100 if (!authstr.empty())
vpcola 0:f1d3878b8dd9 101 get_req->set_header("Authorization", authstr.c_str());
vpcola 0:f1d3878b8dd9 102
vpcola 0:f1d3878b8dd9 103 get_res = get_req->send();
vpcola 0:f1d3878b8dd9 104 }
vpcola 0:f1d3878b8dd9 105
vpcola 0:f1d3878b8dd9 106 if (!get_res) {
vpcola 0:f1d3878b8dd9 107 printf("HttpRequest failed (error code %d)\r\n", get_req->get_error());
vpcola 0:f1d3878b8dd9 108 return NULL;
vpcola 0:f1d3878b8dd9 109 }
vpcola 0:f1d3878b8dd9 110
vpcola 0:f1d3878b8dd9 111 //dump_response(get_res);
vpcola 0:f1d3878b8dd9 112
vpcola 0:f1d3878b8dd9 113 return get_res;
vpcola 0:f1d3878b8dd9 114 }
vpcola 0:f1d3878b8dd9 115
vpcola 0:f1d3878b8dd9 116 std::string DownloadFile::get_file_content()
vpcola 0:f1d3878b8dd9 117 {
vpcola 0:f1d3878b8dd9 118 size_t numread;
vpcola 0:f1d3878b8dd9 119
vpcola 0:f1d3878b8dd9 120 if (fp == NULL)
vpcola 0:f1d3878b8dd9 121 return "";
vpcola 0:f1d3878b8dd9 122
vpcola 0:f1d3878b8dd9 123 // plain old c ..
vpcola 0:f1d3878b8dd9 124 // Determine file size
vpcola 0:f1d3878b8dd9 125 fseek(fp, 0, SEEK_END);
vpcola 0:f1d3878b8dd9 126 size_t size = ftell(fp);
vpcola 0:f1d3878b8dd9 127
vpcola 0:f1d3878b8dd9 128 char* dummy = new char[(sizeof(char) * size) + 1];
vpcola 0:f1d3878b8dd9 129
vpcola 0:f1d3878b8dd9 130 rewind(fp);
vpcola 0:f1d3878b8dd9 131 numread = fread(dummy, sizeof(char), size, fp);
vpcola 0:f1d3878b8dd9 132 // Make sure its NULL terminanted
vpcola 0:f1d3878b8dd9 133 dummy[numread] = 0;
vpcola 0:f1d3878b8dd9 134
vpcola 0:f1d3878b8dd9 135 // create a return string
vpcola 0:f1d3878b8dd9 136 std::string retstr = std::string((const char *) dummy);
vpcola 0:f1d3878b8dd9 137
vpcola 0:f1d3878b8dd9 138 delete[] dummy;
vpcola 0:f1d3878b8dd9 139
vpcola 0:f1d3878b8dd9 140 return retstr;
vpcola 0:f1d3878b8dd9 141 }
vpcola 0:f1d3878b8dd9 142
vpcola 0:f1d3878b8dd9 143 void DownloadFile::body_callback(const char* data, size_t data_len)
vpcola 0:f1d3878b8dd9 144 {
vpcola 0:f1d3878b8dd9 145 // do something with the data
vpcola 0:f1d3878b8dd9 146 if (fp != NULL) {
vpcola 0:f1d3878b8dd9 147 size_written += fwrite((const void *) data, sizeof(char), data_len, fp);
vpcola 0:f1d3878b8dd9 148 }
vpcola 0:f1d3878b8dd9 149 }