/
Download.cpp
151 lines (138 loc) · 3.76 KB
/
Download.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdlib.h>
#include "Download.h"
#include "DownloadManager.h"
#include "http.h"
Download::Download(std::string uri_, bool keepAlive):
http(NULL),
keepAlive(keepAlive),
downloadData(NULL),
downloadSize(0),
downloadStatus(0),
downloadFinished(false),
downloadCanceled(false),
downloadStarted(false),
postData(""),
postDataBoundary(""),
userID(""),
userSession("")
{
uri = std::string(uri_);
DownloadManager::Ref().AddDownload(this);
}
// called by download thread itself if download was canceled
Download::~Download()
{
if (http && (keepAlive || downloadCanceled))
http_async_req_close(http);
if (downloadData)
free(downloadData);
}
// add post data to a request
void Download::AddPostData(std::map<std::string, std::string> data)
{
postDataBoundary = FindBoundary(data, "");
postData = GetMultipartMessage(data, postDataBoundary);
}
void Download::AddPostData(std::pair<std::string, std::string> data)
{
std::map<std::string, std::string> postData;
postData.insert(data);
AddPostData(postData);
}
// add userID and sessionID headers to the download. Must be done after download starts for some reason
void Download::AuthHeaders(std::string ID, std::string session)
{
if (ID != "0")
userID = ID;
userSession = session;
}
// start the download thread
void Download::Start()
{
if (CheckStarted() || CheckDone())
return;
http = http_async_req_start(http, uri.c_str(), postData.c_str(), postData.length(), keepAlive ? 1 : 0);
// add the necessary headers
if (userID.length() || userSession.length())
http_auth_headers(http, userID.c_str(), NULL, userSession.c_str());
if (postDataBoundary.length())
http_add_multipart_header(http, postDataBoundary);
DownloadManager::Ref().Lock();
downloadStarted = true;
DownloadManager::Ref().Unlock();
}
// for persistent connections (keepAlive = true), reuse the open connection to make another request
bool Download::Reuse(std::string newuri)
{
if (!keepAlive || !CheckDone() || CheckCanceled())
{
return false;
}
uri = std::string(newuri);
DownloadManager::Ref().Lock();
downloadFinished = false;
DownloadManager::Ref().Unlock();
Start();
DownloadManager::Ref().EnsureRunning();
return true;
}
// finish the download (if called before the download is done, this will block)
char* Download::Finish(int *length, int *status)
{
if (CheckCanceled())
return NULL; // shouldn't happen but just in case
while (!CheckDone()); // block
DownloadManager::Ref().Lock();
downloadStarted = false;
if (length)
*length = downloadSize;
if (status)
*status = downloadStatus;
char *ret = downloadData;
downloadData = NULL;
if (!keepAlive)
downloadCanceled = true;
DownloadManager::Ref().Unlock();
return ret;
}
// returns the download size and progress (if the download has the correct length headers)
void Download::CheckProgress(int *total, int *done)
{
DownloadManager::Ref().Lock();
if (!downloadFinished && http)
http_async_get_length(http, total, done);
else
*total = *done = 0;
DownloadManager::Ref().Unlock();
}
// returns true if the download has finished
bool Download::CheckDone()
{
DownloadManager::Ref().Lock();
bool ret = downloadFinished;
DownloadManager::Ref().Unlock();
return ret;
}
// returns true if the download was canceled
bool Download::CheckCanceled()
{
DownloadManager::Ref().Lock();
bool ret = downloadCanceled;
DownloadManager::Ref().Unlock();
return ret;
}
// returns true if the download is running
bool Download::CheckStarted()
{
DownloadManager::Ref().Lock();
bool ret = downloadStarted;
DownloadManager::Ref().Unlock();
return ret;
}
// cancels the download, the download thread will delete the Download* when it finishes (do not use Download in any way after canceling)
void Download::Cancel()
{
DownloadManager::Ref().Lock();
downloadCanceled = true;
DownloadManager::Ref().Unlock();
}