Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
DownloadFile.cpp
00001 #include "DownloadFile.h" 00002 #include "https_request.h" 00003 00004 void dump_response(HttpResponse* res) 00005 { 00006 mbedtls_printf("\r\nStatus: %d - %s\r\n", res->get_status_code(), res->get_status_message().c_str()); 00007 00008 mbedtls_printf("Headers:\r\n"); 00009 for (size_t ix = 0; ix < res->get_headers_length(); ix++) { 00010 mbedtls_printf("\t%s: %s\r\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); 00011 } 00012 mbedtls_printf("\nBody (%d bytes):\r\n\r\n%s\r\n", res->get_body_length(), res->get_body_as_string().c_str()); 00013 } 00014 00015 static unsigned int base64enc_len(const char *str) 00016 { 00017 return (((strlen(str)-1)/3)+1)<<2; 00018 } 00019 00020 static void base64enc(const char *input, unsigned int length, char *output) 00021 { 00022 static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 00023 unsigned int c, c1, c2, c3; 00024 for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { 00025 c1 = ((((unsigned char)*((unsigned char *)&input[i])))); 00026 c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; 00027 c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; 00028 00029 c = ((c1 & 0xFC) >> 2); 00030 output[j+0] = base64[c]; 00031 c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); 00032 output[j+1] = base64[c]; 00033 c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); 00034 output[j+2] = (length>i+1)?base64[c]:'='; 00035 c = (c3 & 0x3F); 00036 output[j+3] = (length>i+2)?base64[c]:'='; 00037 } 00038 output[(((length-1)/3)+1)<<2] = '\0'; 00039 } 00040 00041 static string encode(const string& str) 00042 { 00043 char* out = new char[ base64enc_len(str.c_str()) ]; 00044 base64enc(str.c_str(), str.length(), out); 00045 string res(out); 00046 delete[] out; 00047 return res; 00048 } 00049 00050 void DownloadFile::basic_auth(const char * user, const char * password) 00051 { 00052 authstr = user; 00053 authstr += ":"; 00054 authstr += password; 00055 printf("Auth Str : %s\r\n", authstr.c_str()); 00056 00057 std::string base64str = encode(authstr); 00058 printf("Base64 conversion : %s\r\n", base64str.c_str()); 00059 00060 authstr = "Basic " + base64str; 00061 printf("Authorization: %s\r\n", authstr.c_str()); 00062 } 00063 00064 HttpResponse* DownloadFile::get_file(const char * url) 00065 { 00066 if (url == NULL) 00067 return NULL; 00068 00069 if(get_req != NULL) 00070 delete get_req; 00071 00072 HttpResponse* get_res; 00073 Callback<void(const char *at, size_t length)> aBodyCallback = NULL; 00074 00075 if (fp != NULL) 00076 aBodyCallback = mbed::callback(this, &DownloadFile::body_callback); 00077 00078 if (useSSL) 00079 { 00080 get_req_ssl = new HttpsRequest(network, 00081 pem, HTTP_GET, 00082 url, 00083 aBodyCallback); 00084 00085 if (!authstr.empty()) 00086 get_req_ssl->set_header("Authorization", authstr.c_str()); 00087 00088 get_req_ssl->set_debug(true); 00089 00090 get_res = get_req_ssl->send(); 00091 00092 } 00093 else 00094 { 00095 get_req = new HttpRequest(network, 00096 HTTP_GET, 00097 url, 00098 aBodyCallback); 00099 00100 if (!authstr.empty()) 00101 get_req->set_header("Authorization", authstr.c_str()); 00102 00103 get_res = get_req->send(); 00104 } 00105 00106 if (!get_res) { 00107 printf("HttpRequest failed (error code %d)\r\n", get_req->get_error()); 00108 return NULL; 00109 } 00110 00111 //dump_response(get_res); 00112 00113 return get_res; 00114 } 00115 00116 std::string DownloadFile::get_file_content() 00117 { 00118 size_t numread; 00119 00120 if (fp == NULL) 00121 return ""; 00122 00123 // plain old c .. 00124 // Determine file size 00125 fseek(fp, 0, SEEK_END); 00126 size_t size = ftell(fp); 00127 00128 char* dummy = new char[(sizeof(char) * size) + 1]; 00129 00130 rewind(fp); 00131 numread = fread(dummy, sizeof(char), size, fp); 00132 // Make sure its NULL terminanted 00133 dummy[numread] = 0; 00134 00135 // create a return string 00136 std::string retstr = std::string((const char *) dummy); 00137 00138 delete[] dummy; 00139 00140 return retstr; 00141 } 00142 00143 void DownloadFile::body_callback(const char* data, size_t data_len) 00144 { 00145 // do something with the data 00146 if (fp != NULL) { 00147 size_written += fwrite((const void *) data, sizeof(char), data_len, fp); 00148 } 00149 }
Generated on Tue Jul 12 2022 18:06:45 by
1.7.2