17
17
#include " curl/easy.h"
18
18
#include " curl/types.h"
19
19
20
- void progress_callback ( void * percent, double TotalToDL, double CurrentDL, double TotalToUL, double CurrentUL );
21
- size_t write_data ( void *ptr, size_t size, size_t nmemb, void *stream );
20
+ void progress_callback ( void * percent, double TotalToDL, double CurrentDL, double TotalToUL, double CurrentUL ); // percentage progress callback
21
+ size_t write_data ( void *ptr, size_t size, size_t nmemb, void *stream ); // writes HTTP response to a string
22
22
23
23
bool Curl::DownloadFile ( std::string fileURL, std::string filePath )
24
24
{
25
- CURL *curl;
26
- CURLcode res;
27
- curl = curl_easy_init ();
28
- if ( curl )
25
+ CURL *curl; // initialize curl
26
+ CURLcode res; // variable to store curl result
27
+ curl = curl_easy_init (); // initialize curl_easy
28
+ if ( curl ) // if curl was initialized
29
29
{
30
- FILE *fp;
31
- fopen_s ( &fp, filePath.c_str (), " wb" );
32
- curl_easy_setopt ( curl, CURLOPT_URL, fileURL.c_str () );
33
- curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, fwrite );
34
- curl_easy_setopt ( curl, CURLOPT_WRITEDATA, fp );
35
- curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, FALSE );
36
- curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, progress_callback );
37
- res = curl_easy_perform ( curl );
38
- curl_easy_cleanup ( curl );
39
- fclose ( fp );
30
+ FILE *fp; // file pointer
31
+ fopen_s ( &fp, filePath.c_str (), " wb" ); // open the temporary file download location
32
+ curl_easy_setopt ( curl, CURLOPT_URL, fileURL.c_str () ); // set the URL
33
+ curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, fwrite ); // set the write function
34
+ curl_easy_setopt ( curl, CURLOPT_WRITEDATA, fp ); // set the file path
35
+ curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, FALSE ); // we want progress
36
+ curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, progress_callback ); // set the progress callback function
37
+ res = curl_easy_perform ( curl ); // perform; store result into res
38
+ curl_easy_cleanup ( curl ); // clean up
39
+ fclose ( fp ); // close the file
40
40
}
41
- if ( !res )
41
+ if ( !res ) // if we were successful
42
42
return true ;
43
- else
43
+ else // failure
44
44
return false ;
45
45
}
46
46
47
47
std::string Curl::CreatePasteBin ( std::string filePath, std::string pasteName )
48
48
{
49
- CURL *curl;
50
- CURLcode res;
51
- curl = curl_easy_init ();
49
+ CURL *curl; // initialize curl
50
+ CURLcode res; // variable to store curl result
51
+ curl = curl_easy_init (); // initialize curl_easy
52
52
53
- std::string logText;
54
- std::string post;
55
- std::stringstream ss;
53
+ std::string logText; // stores entire log file
54
+ std::string post; // stores POST string
55
+ std::stringstream ss; // create a stringstream
56
56
std::ifstream file;
57
57
58
58
// read entire MTADiag log into string
@@ -75,7 +75,7 @@ std::string Curl::CreatePasteBin ( std::string filePath, std::string pasteName )
75
75
<< curl_easy_escape ( curl, pasteName.c_str (), pasteName.length () ) // urlencode MTADiag log filename
76
76
<< " &"
77
77
<< " api_paste_expire_date=1M&" // paste will expire in one month
78
- << " api_dev_key=supply_your_own &" // Pastebin API dev key
78
+ << " api_dev_key=384625ea6f332a4eaf07ed02f00d2e2b &" // Pastebin API dev key
79
79
<< " api_paste_code="
80
80
<< curl_easy_escape ( curl, logText.c_str (), logText.length () ); // urlencode log file contents
81
81
post = ss.str ();
@@ -84,21 +84,22 @@ std::string Curl::CreatePasteBin ( std::string filePath, std::string pasteName )
84
84
ss.str (" " );
85
85
ss.clear ();
86
86
87
- if ( curl )
87
+ if ( curl ) // if curl was initialized
88
88
{
89
- curl_easy_setopt ( curl, CURLOPT_URL, " http://pastebin.com/api/api_post.php" );
90
- curl_easy_setopt ( curl, CURLOPT_POSTFIELDS, post.c_str () );
91
- curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, FALSE );
92
- curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, progress_callback );
93
- curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, write_data );
94
- res = curl_easy_perform ( curl );
95
- curl_easy_cleanup ( curl );
89
+ curl_easy_setopt ( curl, CURLOPT_URL, " http://pastebin.com/api/api_post.php" ); // set the URL
90
+ curl_easy_setopt ( curl, CURLOPT_POSTFIELDS, post.c_str () ); // set our log file as the POST field
91
+ curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, FALSE ); // we want progress
92
+ curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, progress_callback ); // set the progress callback function
93
+ curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, write_data ); // set the write function
94
+ res = curl_easy_perform ( curl ); // perform; store result into res
95
+ curl_easy_cleanup ( curl ); // clean up
96
96
}
97
- if ( !res )
97
+ if ( !res ) // if we were successful
98
98
{
99
+
99
100
return response;
100
101
}
101
- else
102
+ else // failure
102
103
{
103
104
return " Failed to upload to Pastebin." ;
104
105
}
0 commit comments