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 <cstddef>
10#include <cstdint>
11#include <memory>
12#include <string>
13#include <vector>
14
15#include <packager/file/file_closer.h>
16#include <packager/file/http_file.h>
17#include <packager/status.h>
18
19namespace shaka {
20namespace media {
21
22namespace {
23
24const char kSoapActionHeader[] =
25 "SOAPAction: \"http://schemas.microsoft.com/DRM/2007/03/protocols/"
26 "AcquirePackagingData\"";
27const char kXmlContentType[] = "text/xml; charset=UTF-8";
28const char kJsonContentType[] = "application/json";
29constexpr size_t kBufferSize = 64 * 1024;
30
31} // namespace
32
33HttpKeyFetcher::HttpKeyFetcher() : timeout_in_seconds_(0) {}
34
35HttpKeyFetcher::HttpKeyFetcher(int32_t timeout_in_seconds)
36 : timeout_in_seconds_(timeout_in_seconds) {}
37
38HttpKeyFetcher::~HttpKeyFetcher() {}
39
40Status HttpKeyFetcher::FetchKeys(const std::string& url,
41 const std::string& request,
42 std::string* response) {
43 return Post(url, request, response);
44}
45
46Status HttpKeyFetcher::Get(const std::string& path, std::string* response) {
47 return FetchInternal(HttpMethod::kGet, path, "", response);
48}
49
50Status HttpKeyFetcher::Post(const std::string& path,
51 const std::string& data,
52 std::string* response) {
53 return FetchInternal(HttpMethod::kPost, path, data, response);
54}
55
56Status HttpKeyFetcher::FetchInternal(HttpMethod method,
57 const std::string& path,
58 const std::string& data,
59 std::string* response) {
60 std::string content_type;
61 std::vector<std::string> headers;
62 if (!content_type_.empty()) {
63 content_type = content_type_;
64 } else if (data.find("soap:Envelope") != std::string::npos) {
65 // Adds Http headers for SOAP requests.
66 content_type = kXmlContentType;
67 headers.push_back(kSoapActionHeader);
68 } else {
69 content_type = kJsonContentType;
70 }
71 headers.insert(headers.end(), extra_headers_.begin(), extra_headers_.end());
72
73 std::unique_ptr<HttpFile, FileCloser> file(
74 new HttpFile(method, path, content_type, headers, timeout_in_seconds_));
75 if (!file->Open()) {
76 return Status(error::INTERNAL_ERROR, "Cannot open URL");
77 }
78 file->Write(data.data(), data.size());
79 file->Flush();
80 file->CloseForWriting();
81
82 while (true) {
83 char temp[kBufferSize];
84 int64_t ret = file->Read(temp, kBufferSize);
85 if (ret <= 0)
86 break;
87 response->append(temp, ret);
88 }
89 return file.release()->CloseWithStatus();
90}
91
92} // namespace media
93} // 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.