Shaka Packager SDK
Loading...
Searching...
No Matches
http_key_fetcher.cc
1// Copyright 2014 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#include <packager/media/base/http_key_fetcher.h>
8
9#include <packager/file/file_closer.h>
10
11namespace shaka {
12namespace media {
13
14namespace {
15
16const char kSoapActionHeader[] =
17 "SOAPAction: \"http://schemas.microsoft.com/DRM/2007/03/protocols/"
18 "AcquirePackagingData\"";
19const char kXmlContentType[] = "text/xml; charset=UTF-8";
20const char kJsonContentType[] = "application/json";
21constexpr size_t kBufferSize = 64 * 1024;
22
23} // namespace
24
25HttpKeyFetcher::HttpKeyFetcher() : timeout_in_seconds_(0) {}
26
27HttpKeyFetcher::HttpKeyFetcher(int32_t timeout_in_seconds)
28 : timeout_in_seconds_(timeout_in_seconds) {}
29
30HttpKeyFetcher::~HttpKeyFetcher() {}
31
32Status HttpKeyFetcher::FetchKeys(const std::string& url,
33 const std::string& request,
34 std::string* response) {
35 return Post(url, request, response);
36}
37
38Status HttpKeyFetcher::Get(const std::string& path, std::string* response) {
39 return FetchInternal(HttpMethod::kGet, path, "", response);
40}
41
42Status HttpKeyFetcher::Post(const std::string& path,
43 const std::string& data,
44 std::string* response) {
45 return FetchInternal(HttpMethod::kPost, path, data, response);
46}
47
48Status HttpKeyFetcher::FetchInternal(HttpMethod method,
49 const std::string& path,
50 const std::string& data,
51 std::string* response) {
52 std::string content_type;
53 std::vector<std::string> headers;
54 if (data.find("soap:Envelope") != std::string::npos) {
55 // Adds Http headers for SOAP requests.
56 content_type = kXmlContentType;
57 headers.push_back(kSoapActionHeader);
58 } else {
59 content_type = kJsonContentType;
60 }
61
62 std::unique_ptr<HttpFile, FileCloser> file(
63 new HttpFile(method, path, content_type, headers, timeout_in_seconds_));
64 if (!file->Open()) {
65 return Status(error::INTERNAL_ERROR, "Cannot open URL");
66 }
67 file->Write(data.data(), data.size());
68 file->Flush();
69 file->CloseForWriting();
70
71 while (true) {
72 char temp[kBufferSize];
73 int64_t ret = file->Read(temp, kBufferSize);
74 if (ret <= 0)
75 break;
76 response->append(temp, ret);
77 }
78 return file.release()->CloseWithStatus();
79}
80
81} // namespace media
82} // namespace shaka
HttpKeyFetcher()
Creates a fetcher with no timeout.
virtual Status Post(const std::string &url, const std::string &data, std::string *response)
virtual Status Get(const std::string &url, std::string *response)
Status FetchKeys(const std::string &url, const std::string &request, std::string *response) override
All the methods that are virtual are virtual for mocking.