Vergil Cola
/
MQTTGateway2
Fork of my original MQTTGateway
DownloadFile.h@0:a1734fe1ec4b, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:43:14 2017 +0000
- Revision:
- 0:a1734fe1ec4b
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:a1734fe1ec4b | 1 | #ifndef _DOWNLOAD_FILE_H_ |
vpcola | 0:a1734fe1ec4b | 2 | #define _DOWNLOAD_FILE_H_ |
vpcola | 0:a1734fe1ec4b | 3 | |
vpcola | 0:a1734fe1ec4b | 4 | #include <stdio.h> |
vpcola | 0:a1734fe1ec4b | 5 | #include <string> |
vpcola | 0:a1734fe1ec4b | 6 | #include "mbed.h" |
vpcola | 0:a1734fe1ec4b | 7 | #include "rtos.h" |
vpcola | 0:a1734fe1ec4b | 8 | #include "NetworkInterface.h" |
vpcola | 0:a1734fe1ec4b | 9 | #include "https_request.h" |
vpcola | 0:a1734fe1ec4b | 10 | #include "http_request.h" |
vpcola | 0:a1734fe1ec4b | 11 | |
vpcola | 0:a1734fe1ec4b | 12 | class DownloadFile |
vpcola | 0:a1734fe1ec4b | 13 | { |
vpcola | 0:a1734fe1ec4b | 14 | public: |
vpcola | 0:a1734fe1ec4b | 15 | DownloadFile(NetworkInterface* nw, const char * file = NULL, const char * capem = NULL) |
vpcola | 0:a1734fe1ec4b | 16 | :network(nw), |
vpcola | 0:a1734fe1ec4b | 17 | filename(file), |
vpcola | 0:a1734fe1ec4b | 18 | pem(capem), |
vpcola | 0:a1734fe1ec4b | 19 | useSSL(capem != NULL), |
vpcola | 0:a1734fe1ec4b | 20 | fp(NULL), |
vpcola | 0:a1734fe1ec4b | 21 | size_written(0), |
vpcola | 0:a1734fe1ec4b | 22 | get_req_ssl(NULL), |
vpcola | 0:a1734fe1ec4b | 23 | get_req(NULL) |
vpcola | 0:a1734fe1ec4b | 24 | { |
vpcola | 0:a1734fe1ec4b | 25 | if (filename) |
vpcola | 0:a1734fe1ec4b | 26 | { |
vpcola | 0:a1734fe1ec4b | 27 | fp = fopen(file, "w+"); |
vpcola | 0:a1734fe1ec4b | 28 | if (fp != NULL) |
vpcola | 0:a1734fe1ec4b | 29 | printf("File open successfull!\r\n"); |
vpcola | 0:a1734fe1ec4b | 30 | } |
vpcola | 0:a1734fe1ec4b | 31 | } |
vpcola | 0:a1734fe1ec4b | 32 | |
vpcola | 0:a1734fe1ec4b | 33 | virtual ~DownloadFile() |
vpcola | 0:a1734fe1ec4b | 34 | { |
vpcola | 0:a1734fe1ec4b | 35 | if (fp != NULL) |
vpcola | 0:a1734fe1ec4b | 36 | fclose(fp); |
vpcola | 0:a1734fe1ec4b | 37 | |
vpcola | 0:a1734fe1ec4b | 38 | // HttpsRequest destructor also free's up |
vpcola | 0:a1734fe1ec4b | 39 | // the HttpsResult ... so it must be consumed |
vpcola | 0:a1734fe1ec4b | 40 | // before this class goes out of scope |
vpcola | 0:a1734fe1ec4b | 41 | if(get_req) |
vpcola | 0:a1734fe1ec4b | 42 | delete get_req; |
vpcola | 0:a1734fe1ec4b | 43 | if(get_req_ssl) |
vpcola | 0:a1734fe1ec4b | 44 | delete get_req_ssl; |
vpcola | 0:a1734fe1ec4b | 45 | } |
vpcola | 0:a1734fe1ec4b | 46 | |
vpcola | 0:a1734fe1ec4b | 47 | HttpResponse* get_file(const char * url); |
vpcola | 0:a1734fe1ec4b | 48 | |
vpcola | 0:a1734fe1ec4b | 49 | std::string get_file_content(); |
vpcola | 0:a1734fe1ec4b | 50 | |
vpcola | 0:a1734fe1ec4b | 51 | const char * get_filename() |
vpcola | 0:a1734fe1ec4b | 52 | { |
vpcola | 0:a1734fe1ec4b | 53 | return filename; |
vpcola | 0:a1734fe1ec4b | 54 | } |
vpcola | 0:a1734fe1ec4b | 55 | |
vpcola | 0:a1734fe1ec4b | 56 | size_t get_written_size() { |
vpcola | 0:a1734fe1ec4b | 57 | return size_written; |
vpcola | 0:a1734fe1ec4b | 58 | } |
vpcola | 0:a1734fe1ec4b | 59 | |
vpcola | 0:a1734fe1ec4b | 60 | void basic_auth(const char * user, const char * password); |
vpcola | 0:a1734fe1ec4b | 61 | |
vpcola | 0:a1734fe1ec4b | 62 | |
vpcola | 0:a1734fe1ec4b | 63 | protected: |
vpcola | 0:a1734fe1ec4b | 64 | void body_callback(const char* data, size_t data_len); |
vpcola | 0:a1734fe1ec4b | 65 | |
vpcola | 0:a1734fe1ec4b | 66 | private: |
vpcola | 0:a1734fe1ec4b | 67 | NetworkInterface* network; |
vpcola | 0:a1734fe1ec4b | 68 | const char * filename; |
vpcola | 0:a1734fe1ec4b | 69 | const char * pem; |
vpcola | 0:a1734fe1ec4b | 70 | bool useSSL; |
vpcola | 0:a1734fe1ec4b | 71 | FILE * fp; |
vpcola | 0:a1734fe1ec4b | 72 | size_t size_written; |
vpcola | 0:a1734fe1ec4b | 73 | std::string authstr; |
vpcola | 0:a1734fe1ec4b | 74 | |
vpcola | 0:a1734fe1ec4b | 75 | HttpsRequest* get_req_ssl; |
vpcola | 0:a1734fe1ec4b | 76 | HttpRequest* get_req; |
vpcola | 0:a1734fe1ec4b | 77 | }; |
vpcola | 0:a1734fe1ec4b | 78 | |
vpcola | 0:a1734fe1ec4b | 79 | #endif |
vpcola | 0:a1734fe1ec4b | 80 |