| 260 | } |
| 261 | |
| 262 | bool HttpDataStream::Open(const char *rawUri, OpenFlags flags) { |
| 263 | if ((flags & OpenFlags::Write) != 0) { |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | this->precacheSizeBytes = prefs->GetInt(kPreCacheBufferSizeBytesKey.c_str(), kDefaultPreCacheSizeBytes); |
| 268 | this->chunkSizeBytes = prefs->GetInt(kChunkSizeBytesKey.c_str(), kDefaultChunkSizeBytes); |
| 269 | this->maxCacheFiles = prefs->GetInt(kMaxCacheFiles.c_str(), kDefaultMaxCacheFiles); |
| 270 | |
| 271 | std::unordered_map<std::string, std::string> requestHeaders; |
| 272 | |
| 273 | { |
| 274 | std::unique_lock<std::mutex> lock(this->stateMutex); |
| 275 | |
| 276 | diskCache.Init(cachePath, this->maxCacheFiles); |
| 277 | |
| 278 | this->httpUri = rawUri; |
| 279 | |
| 280 | if (this->httpUri.find(kRemoteTrackHost) == 0) { |
| 281 | try { |
| 282 | nlohmann::json options = nlohmann::json::parse( |
| 283 | this->httpUri.substr(kRemoteTrackHost.size())); |
| 284 | this->httpUri = options["uri"].get<std::string>(); |
| 285 | this->originalUri = options["originalUri"].get<std::string>(); |
| 286 | this->type = options.value("type", ".mp3"); |
| 287 | |
| 288 | std::string password = options.value("password", ""); |
| 289 | std::string headerValue = "Basic " + websocketpp::base64_encode("default:" + password); |
| 290 | requestHeaders["Authorization"] = headerValue; |
| 291 | } |
| 292 | catch (...) { |
| 293 | /* malformed payload. not much we can do. */ |
| 294 | return false; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | auto const id = cacheId(httpUri); |
| 299 | |
| 300 | if (diskCache.Cached(id)) { |
| 301 | FILE* file = diskCache.Open(id, this->instanceId, "rb", this->type, this->length); |
| 302 | if (file) { |
| 303 | this->reader = std::make_shared<FileReadStream>(file, this->length); |
| 304 | this->state = State::Cached; |
| 305 | return true; |
| 306 | } |
| 307 | else { |
| 308 | diskCache.Delete(id, this->instanceId); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | this->ResetFileHandles(); |
| 313 | } |
| 314 | |
| 315 | if (this->writeFile && this->reader) { |
| 316 | this->curlEasy = curl_easy_init(); |
| 317 | |
| 318 | // curl_easy_setopt (this->curlEasy, CURLOPT_VERBOSE, verbose); |
| 319 | |