| 1 |
/* |
| 2 |
* originally taken from pvr.zattoo |
| 3 |
*/ |
| 4 |
#include "Curl.h" |
| 5 |
|
| 6 |
#include "Utils.h" |
| 7 |
#include "kodi/tools/StringUtils.h" |
| 8 |
|
| 9 |
Curl::Curl() = default; |
| 10 |
|
| 11 |
Curl::~Curl() = default; |
| 12 |
|
| 13 |
std::string Curl::GetCookie(const std::string& name) |
| 14 |
{ |
| 15 |
for (const auto& cookie : m_cookies) |
| 16 |
{ |
| 17 |
if (cookie.name == name) |
| 18 |
return cookie.value; |
| 19 |
} |
| 20 |
return ""; |
| 21 |
} |
| 22 |
|
| 23 |
void Curl::SetCookie(const std::string& host, const std::string& name, const std::string& value) |
| 24 |
{ |
| 25 |
for (auto& cookie : m_cookies) |
| 26 |
{ |
| 27 |
if (cookie.host == host && cookie.name == name) |
| 28 |
{ |
| 29 |
cookie.value = value; |
| 30 |
return; |
| 31 |
} |
| 32 |
} |
| 33 |
Cookie cookie; |
| 34 |
cookie.host = host; |
| 35 |
cookie.name = name; |
| 36 |
cookie.value = value; |
| 37 |
m_cookies.emplace_back(cookie); |
| 38 |
} |
| 39 |
|
| 40 |
void Curl::AddHeader(const std::string& name, const std::string& value) |
| 41 |
{ |
| 42 |
m_headers[name] = value; |
| 43 |
} |
| 44 |
|
| 45 |
void Curl::AddOption(const std::string& name, const std::string& value) |
| 46 |
{ |
| 47 |
m_options[name] = value; |
| 48 |
} |
| 49 |
|
| 50 |
void Curl::ResetHeaders() |
| 51 |
{ |
| 52 |
m_headers.clear(); |
| 53 |
} |
| 54 |
|
| 55 |
std::string Curl::Delete(const std::string& url, const std::string& postData, int& statusCode) |
| 56 |
{ |
| 57 |
return Request("DELETE", url, postData, statusCode); |
| 58 |
} |
| 59 |
|
| 60 |
std::string Curl::Get(const std::string& url, int& statusCode) |
| 61 |
{ |
| 62 |
return Request("GET", url, "", statusCode); |
| 63 |
} |
| 64 |
|
| 65 |
std::string Curl::Post(const std::string& url, const std::string& postData, int& statusCode) |
| 66 |
{ |
| 67 |
return Request("POST", url, postData, statusCode); |
| 68 |
} |
| 69 |
|
| 70 |
std::string Curl::Put(const std::string& url, const std::string& postData, int& statusCode) |
| 71 |
{ |
| 72 |
return Request("PUT", url, postData, statusCode); |
| 73 |
} |
| 74 |
|
| 75 |
void Curl::ParseCookies(kodi::vfs::CFile* file, const std::string& host) |
| 76 |
{ |
| 77 |
const std::vector<std::string> cookies = |
| 78 |
file->GetPropertyValues(ADDON_FILE_PROPERTY_RESPONSE_HEADER, "set-cookie"); |
| 79 |
for (auto cookie : cookies) |
| 80 |
{ |
| 81 |
const std::string::size_type paramPos = cookie.find(';'); |
| 82 |
if (paramPos != std::string::npos) |
| 83 |
cookie.resize(paramPos); |
| 84 |
const std::vector<std::string> parts = kodi::tools::StringUtils::Split(cookie, "=", 2); |
| 85 |
if (parts.size() != 2) |
| 86 |
continue; |
| 87 |
SetCookie(host, parts[0], parts[1]); |
| 88 |
kodi::Log(ADDON_LOG_DEBUG, "Got cookie: %s.", parts[0].c_str()); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
std::string Curl::ParseHostname(const std::string& url) |
| 93 |
{ |
| 94 |
const size_t pos = url.find_first_of(":"); |
| 95 |
if (pos == std::string::npos) |
| 96 |
return ""; |
| 97 |
|
| 98 |
std::string host = url.substr(pos + 3); |
| 99 |
|
| 100 |
const size_t pos_end = host.find_first_of("://"); |
| 101 |
if (pos_end == std::string::npos) |
| 102 |
return host; |
| 103 |
|
| 104 |
host = host.substr(0, pos_end); |
| 105 |
return host; |
| 106 |
} |
| 107 |
|
| 108 |
kodi::vfs::CFile* Curl::PrepareRequest(const std::string& action, |
| 109 |
const std::string& url, |
| 110 |
const std::string& postData) |
| 111 |
{ |
| 112 |
kodi::vfs::CFile* file = new kodi::vfs::CFile; |
| 113 |
if (!file->CURLCreate(url)) |
| 114 |
{ |
| 115 |
delete file; |
| 116 |
return nullptr; |
| 117 |
} |
| 118 |
file->CURLAddOption(ADDON_CURL_OPTION_PROTOCOL, "redirect-limit", "0"); |
| 119 |
file->CURLAddOption(ADDON_CURL_OPTION_PROTOCOL, "customrequest", action); |
| 120 |
|
| 121 |
file->CURLAddOption(ADDON_CURL_OPTION_HEADER, "acceptencoding", "gzip"); |
| 122 |
|
| 123 |
if (!postData.empty()) |
| 124 |
{ |
| 125 |
const std::string base64 = Base64Encode(postData, false); |
| 126 |
file->CURLAddOption(ADDON_CURL_OPTION_PROTOCOL, "postdata", base64); |
| 127 |
} |
| 128 |
|
| 129 |
for (auto const& entry : m_headers) |
| 130 |
{ |
| 131 |
file->CURLAddOption(ADDON_CURL_OPTION_HEADER, entry.first, entry.second); |
| 132 |
} |
| 133 |
|
| 134 |
for (auto const& entry : m_options) |
| 135 |
{ |
| 136 |
file->CURLAddOption(ADDON_CURL_OPTION_PROTOCOL, entry.first, entry.second); |
| 137 |
} |
| 138 |
|
| 139 |
const std::string host = ParseHostname(url); |
| 140 |
kodi::Log(ADDON_LOG_DEBUG, "Add cookies for host: %s.", host.c_str()); |
| 141 |
std::string cookie_s; |
| 142 |
for (auto& cookie : m_cookies) |
| 143 |
{ |
| 144 |
if (cookie.host != host) |
| 145 |
continue; |
| 146 |
cookie_s = cookie_s + cookie.name + "=" + cookie.value + "; "; |
| 147 |
} |
| 148 |
if (cookie_s.size() > 0) |
| 149 |
file->CURLAddOption(ADDON_CURL_OPTION_PROTOCOL, "cookie", cookie_s); |
| 150 |
|
| 151 |
// we have to set "failonerror" to get error results |
| 152 |
file->CURLAddOption(ADDON_CURL_OPTION_HEADER, "failonerror", "false"); |
| 153 |
return file; |
| 154 |
} |
| 155 |
|
| 156 |
std::string Curl::Request(const std::string& action, |
| 157 |
const std::string& url, |
| 158 |
const std::string& postData, |
| 159 |
int& statusCode) |
| 160 |
{ |
| 161 |
int remaining_redirects = m_redirectLimit; |
| 162 |
m_location = url; |
| 163 |
bool redirect; |
| 164 |
kodi::vfs::CFile* file = PrepareRequest(action, url, postData); |
| 165 |
|
| 166 |
do |
| 167 |
{ |
| 168 |
redirect = false; |
| 169 |
if (file == nullptr) |
| 170 |
{ |
| 171 |
statusCode = -1; |
| 172 |
return ""; |
| 173 |
} |
| 174 |
|
| 175 |
if (!file->CURLOpen(ADDON_READ_NO_CACHE)) |
| 176 |
{ |
| 177 |
statusCode = -1; |
| 178 |
return ""; |
| 179 |
} |
| 180 |
|
| 181 |
statusCode = 200; |
| 182 |
|
| 183 |
// get the real statusCode |
| 184 |
const std::string tmpRespLine = |
| 185 |
file->GetPropertyValue(ADDON_FILE_PROPERTY_RESPONSE_PROTOCOL, ""); |
| 186 |
const std::vector<std::string> resp_protocol_parts = |
| 187 |
kodi::tools::StringUtils::Split(tmpRespLine, " ", 3); |
| 188 |
|
| 189 |
if (resp_protocol_parts.size() >= 2) |
| 190 |
{ |
| 191 |
statusCode = Utils::StringToInt(resp_protocol_parts[1], -1); |
| 192 |
kodi::Log(ADDON_LOG_DEBUG, "HTTP response code: %i.", statusCode); |
| 193 |
} |
| 194 |
|
| 195 |
ParseCookies(file, ParseHostname(m_location)); |
| 196 |
|
| 197 |
m_location = file->GetPropertyValue(ADDON_FILE_PROPERTY_RESPONSE_HEADER, "Location"); |
| 198 |
kodi::Log(ADDON_LOG_DEBUG, "Location: %s.", m_location.c_str()); |
| 199 |
|
| 200 |
if (statusCode >= 301 && statusCode <= 303) |
| 201 |
{ |
| 202 |
// handle redirect |
| 203 |
redirect = true; |
| 204 |
kodi::Log(ADDON_LOG_DEBUG, "redirects remaining: %i", remaining_redirects); |
| 205 |
remaining_redirects--; |
| 206 |
delete file; |
| 207 |
file = PrepareRequest("GET", m_location, ""); |
| 208 |
} |
| 209 |
} while (redirect && remaining_redirects >= 0); |
| 210 |
|
| 211 |
// read the file |
| 212 |
static const unsigned int CHUNKSIZE = 16384; |
| 213 |
char buf[CHUNKSIZE + 1]; |
| 214 |
ssize_t nbRead; |
| 215 |
std::string body; |
| 216 |
while ((nbRead = file->Read(buf, CHUNKSIZE)) > 0 && ~nbRead) |
| 217 |
{ |
| 218 |
buf[nbRead] = 0x0; |
| 219 |
body += buf; |
| 220 |
} |
| 221 |
|
| 222 |
delete file; |
| 223 |
return body; |
| 224 |
} |
| 225 |
|
| 226 |
std::string Curl::Base64Encode(const std::string& str, bool urlEncode) |
| 227 |
{ |
| 228 |
std::string ret; |
| 229 |
int i = 3; |
| 230 |
unsigned char c_3[3]; |
| 231 |
unsigned char c_4[4]; |
| 232 |
|
| 233 |
const char* to_base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 234 |
|
| 235 |
int len = str.size(); |
| 236 |
int curr = 0; |
| 237 |
while (len) |
| 238 |
{ |
| 239 |
i = len > 2 ? 3 : len; |
| 240 |
len -= i; |
| 241 |
c_3[0] = str[curr++]; |
| 242 |
c_3[1] = i > 1 ? str[curr++] : 0; |
| 243 |
c_3[2] = i > 2 ? str[curr++] : 0; |
| 244 |
|
| 245 |
c_4[0] = (c_3[0] & 0xfc) >> 2; |
| 246 |
c_4[1] = ((c_3[0] & 0x03) << 4) + ((c_3[1] & 0xf0) >> 4); |
| 247 |
c_4[2] = ((c_3[1] & 0x0f) << 2) + ((c_3[2] & 0xc0) >> 6); |
| 248 |
c_4[3] = c_3[2] & 0x3f; |
| 249 |
|
| 250 |
for (int j = 0; (j < i + 1); ++j) |
| 251 |
{ |
| 252 |
if (urlEncode && to_base64[c_4[j]] == '+') |
| 253 |
ret += "%2B"; |
| 254 |
else if (urlEncode && to_base64[c_4[j]] == '/') |
| 255 |
ret += "%2F"; |
| 256 |
else |
| 257 |
ret += to_base64[c_4[j]]; |
| 258 |
} |
| 259 |
} |
| 260 |
while ((i++ < 3)) |
| 261 |
ret += urlEncode ? "%3D" : "="; |
| 262 |
return ret; |
| 263 |
} |