2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
#include "packager/media/base/http_key_fetcher.h"
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2014-06-18 01:33:07 +00:00
|
|
|
#include <curl/curl.h>
|
2016-01-06 23:52:18 +00:00
|
|
|
|
|
|
|
#include "packager/base/logging.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/strings/stringprintf.h"
|
2015-03-23 19:55:58 +00:00
|
|
|
#include "packager/base/synchronization/lock.h"
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-03-24 22:29:55 +00:00
|
|
|
|
2013-12-16 17:13:29 +00:00
|
|
|
namespace {
|
2016-05-20 21:28:13 +00:00
|
|
|
const char kUserAgentString[] = "shaka-packager-http_fetcher/1.0";
|
2017-01-05 17:32:17 +00:00
|
|
|
const char kSoapActionHeader[] =
|
|
|
|
"SOAPAction: \"http://schemas.microsoft.com/DRM/2007/03/protocols/"
|
|
|
|
"AcquirePackagingData\"";
|
|
|
|
const char kXmlContentTypeHeader[] = "Content-Type: text/xml; charset=UTF-8";
|
2014-06-18 01:33:07 +00:00
|
|
|
|
|
|
|
// Scoped CURL implementation which cleans up itself when goes out of scope.
|
|
|
|
class ScopedCurl {
|
|
|
|
public:
|
|
|
|
ScopedCurl() { ptr_ = curl_easy_init(); }
|
|
|
|
~ScopedCurl() {
|
|
|
|
if (ptr_)
|
|
|
|
curl_easy_cleanup(ptr_);
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-18 01:33:07 +00:00
|
|
|
CURL* get() { return ptr_; }
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2014-06-18 01:33:07 +00:00
|
|
|
private:
|
|
|
|
CURL* ptr_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ScopedCurl);
|
|
|
|
};
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2014-06-18 01:33:07 +00:00
|
|
|
size_t AppendToString(char* ptr, size_t size, size_t nmemb, std::string* response) {
|
|
|
|
DCHECK(ptr);
|
|
|
|
DCHECK(response);
|
|
|
|
const size_t total_size = size * nmemb;
|
|
|
|
response->append(ptr, total_size);
|
|
|
|
return total_size;
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
2015-03-23 19:55:58 +00:00
|
|
|
|
|
|
|
class LibCurlInitializer {
|
|
|
|
public:
|
|
|
|
LibCurlInitializer() : initialized_(false) {
|
|
|
|
base::AutoLock lock(lock_);
|
|
|
|
if (!initialized_) {
|
|
|
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
|
|
|
initialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~LibCurlInitializer() {
|
|
|
|
base::AutoLock lock(lock_);
|
|
|
|
if (initialized_) {
|
|
|
|
curl_global_cleanup();
|
|
|
|
initialized_ = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
base::Lock lock_;
|
|
|
|
bool initialized_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(LibCurlInitializer);
|
|
|
|
};
|
|
|
|
|
2013-12-16 17:13:29 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
|
2015-03-23 19:55:58 +00:00
|
|
|
HttpKeyFetcher::HttpKeyFetcher() : timeout_in_seconds_(0) {}
|
2014-06-18 01:33:07 +00:00
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher::HttpKeyFetcher(uint32_t timeout_in_seconds)
|
2015-03-23 19:55:58 +00:00
|
|
|
: timeout_in_seconds_(timeout_in_seconds) {}
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2015-03-23 19:55:58 +00:00
|
|
|
HttpKeyFetcher::~HttpKeyFetcher() {}
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
Status HttpKeyFetcher::FetchKeys(const std::string& url,
|
|
|
|
const std::string& request,
|
|
|
|
std::string* response) {
|
|
|
|
return Post(url, request, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status HttpKeyFetcher::Get(const std::string& path, std::string* response) {
|
2014-06-18 01:33:07 +00:00
|
|
|
return FetchInternal(GET, path, "", response);
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
Status HttpKeyFetcher::Post(const std::string& path,
|
|
|
|
const std::string& data,
|
|
|
|
std::string* response) {
|
2014-06-18 01:33:07 +00:00
|
|
|
return FetchInternal(POST, path, data, response);
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
Status HttpKeyFetcher::FetchInternal(HttpMethod method,
|
|
|
|
const std::string& path,
|
|
|
|
const std::string& data,
|
|
|
|
std::string* response) {
|
2014-06-18 01:33:07 +00:00
|
|
|
DCHECK(method == GET || method == POST);
|
2015-03-23 19:55:58 +00:00
|
|
|
static LibCurlInitializer lib_curl_initializer;
|
|
|
|
|
2014-06-18 01:33:07 +00:00
|
|
|
ScopedCurl scoped_curl;
|
|
|
|
CURL* curl = scoped_curl.get();
|
|
|
|
if (!curl) {
|
|
|
|
LOG(ERROR) << "curl_easy_init() failed.";
|
|
|
|
return Status(error::HTTP_FAILURE, "curl_easy_init() failed.");
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
2014-06-18 01:33:07 +00:00
|
|
|
response->clear();
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, path.c_str());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, kUserAgentString);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout_in_seconds_);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, AppendToString);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
|
2017-01-05 17:32:17 +00:00
|
|
|
|
2017-10-11 21:34:04 +00:00
|
|
|
if (!client_cert_private_key_file_.empty() && !client_cert_file_.empty()) {
|
2017-01-05 17:32:17 +00:00
|
|
|
// Some PlayReady packaging servers only allow connects via HTTPS with
|
|
|
|
// client certificates.
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSLKEY,
|
|
|
|
client_cert_private_key_file_.data());
|
2017-10-11 21:34:04 +00:00
|
|
|
if (!client_cert_private_key_password_.empty()) {
|
|
|
|
curl_easy_setopt(curl, CURLOPT_KEYPASSWD,
|
|
|
|
client_cert_private_key_password_.data());
|
|
|
|
}
|
2017-01-05 17:32:17 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, "PEM");
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSLCERT, client_cert_file_.data());
|
|
|
|
}
|
|
|
|
if (!ca_file_.empty()) {
|
|
|
|
// Host validation needs to be off when using self-signed certificates.
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CAINFO, ca_file_.data());
|
|
|
|
}
|
2014-06-18 01:33:07 +00:00
|
|
|
if (method == POST) {
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.size());
|
2017-01-05 17:32:17 +00:00
|
|
|
if (data.find("soap:Envelope") > 0) {
|
|
|
|
// Adds Http headers for SOAP requests.
|
|
|
|
struct curl_slist *chunk = NULL;
|
|
|
|
chunk = curl_slist_append(chunk, kXmlContentTypeHeader);
|
|
|
|
chunk = curl_slist_append(chunk, kSoapActionHeader);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
|
|
|
|
}
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
2014-06-18 01:33:07 +00:00
|
|
|
CURLcode res = curl_easy_perform(curl);
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
std::string error_message = base::StringPrintf(
|
|
|
|
"curl_easy_perform() failed: %s.", curl_easy_strerror(res));
|
|
|
|
if (res == CURLE_HTTP_RETURNED_ERROR) {
|
|
|
|
long response_code = 0;
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
|
|
|
|
error_message += base::StringPrintf(" Response code: %ld.", response_code);
|
|
|
|
}
|
|
|
|
|
2013-12-16 17:13:29 +00:00
|
|
|
LOG(ERROR) << error_message;
|
2014-06-18 01:33:07 +00:00
|
|
|
return Status(
|
|
|
|
res == CURLE_OPERATION_TIMEDOUT ? error::TIME_OUT : error::HTTP_FAILURE,
|
|
|
|
error_message);
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|