2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2021-02-02 21:09:18 +00:00
|
|
|
#include "packager/file/file_closer.h"
|
2018-08-22 23:50:02 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2021-02-02 21:09:18 +00:00
|
|
|
namespace media {
|
2015-03-24 22:29:55 +00:00
|
|
|
|
2013-12-16 17:13:29 +00:00
|
|
|
namespace {
|
2021-02-02 21:09:18 +00:00
|
|
|
|
2017-01-05 17:32:17 +00:00
|
|
|
const char kSoapActionHeader[] =
|
|
|
|
"SOAPAction: \"http://schemas.microsoft.com/DRM/2007/03/protocols/"
|
|
|
|
"AcquirePackagingData\"";
|
2021-02-02 21:09:18 +00:00
|
|
|
const char kXmlContentType[] = "text/xml; charset=UTF-8";
|
|
|
|
const char kJsonContentType[] = "application/json";
|
|
|
|
constexpr size_t kBufferSize = 64 * 1024;
|
2015-03-23 19:55:58 +00:00
|
|
|
|
2013-12-16 17:13:29 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-03-23 19:55:58 +00:00
|
|
|
HttpKeyFetcher::HttpKeyFetcher() : timeout_in_seconds_(0) {}
|
2014-06-18 01:33:07 +00:00
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
HttpKeyFetcher::HttpKeyFetcher(int32_t timeout_in_seconds)
|
2023-05-01 23:56:46 +00:00
|
|
|
: timeout_in_seconds_(timeout_in_seconds), http_status_code_(0) {}
|
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
|
|
|
|
2023-05-01 23:56:46 +00:00
|
|
|
int HttpKeyFetcher::http_status_code() const {
|
|
|
|
return http_status_code_;
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-02-02 21:09:18 +00:00
|
|
|
return FetchInternal(HttpMethod::kGet, 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) {
|
2021-02-02 21:09:18 +00:00
|
|
|
return FetchInternal(HttpMethod::kPost, 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) {
|
2021-02-02 21:09:18 +00:00
|
|
|
std::string content_type;
|
|
|
|
std::vector<std::string> headers;
|
|
|
|
if (data.find("soap:Envelope") != std::string::npos) {
|
|
|
|
// Adds Http headers for SOAP requests.
|
|
|
|
content_type = kXmlContentType;
|
|
|
|
headers.push_back(kSoapActionHeader);
|
|
|
|
} else {
|
|
|
|
content_type = kJsonContentType;
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
2018-04-06 18:47:54 +00:00
|
|
|
|
2021-02-02 21:09:18 +00:00
|
|
|
std::unique_ptr<HttpFile, FileCloser> file(
|
|
|
|
new HttpFile(method, path, content_type, headers, timeout_in_seconds_));
|
|
|
|
if (!file->Open()) {
|
|
|
|
return Status(error::INTERNAL_ERROR, "Cannot open URL");
|
2018-04-06 18:47:54 +00:00
|
|
|
}
|
2021-02-02 21:09:18 +00:00
|
|
|
file->Write(data.data(), data.size());
|
|
|
|
file->Flush();
|
2018-04-06 18:47:54 +00:00
|
|
|
|
2021-02-02 21:09:18 +00:00
|
|
|
while (true) {
|
|
|
|
char temp[kBufferSize];
|
|
|
|
int64_t ret = file->Read(temp, kBufferSize);
|
|
|
|
if (ret <= 0)
|
|
|
|
break;
|
|
|
|
response->append(temp, ret);
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
2023-05-01 23:56:46 +00:00
|
|
|
|
|
|
|
http_status_code_ = file->http_status_code();
|
|
|
|
|
2021-02-02 21:09:18 +00:00
|
|
|
return file.release()->CloseWithStatus();
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|