Shaka Packager SDK
http_key_fetcher.cc
1 // Copyright 2014 Google Inc. 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 
11 namespace shaka {
12 namespace media {
13 
14 namespace {
15 
16 const char kSoapActionHeader[] =
17  "SOAPAction: \"http://schemas.microsoft.com/DRM/2007/03/protocols/"
18  "AcquirePackagingData\"";
19 const char kXmlContentType[] = "text/xml; charset=UTF-8";
20 const char kJsonContentType[] = "application/json";
21 constexpr size_t kBufferSize = 64 * 1024;
22 
23 } // namespace
24 
25 HttpKeyFetcher::HttpKeyFetcher() : timeout_in_seconds_(0) {}
26 
27 HttpKeyFetcher::HttpKeyFetcher(uint32_t timeout_in_seconds)
28  : timeout_in_seconds_(timeout_in_seconds) {}
29 
30 HttpKeyFetcher::~HttpKeyFetcher() {}
31 
32 Status HttpKeyFetcher::FetchKeys(const std::string& url,
33  const std::string& request,
34  std::string* response) {
35  return Post(url, request, response);
36 }
37 
38 Status HttpKeyFetcher::Get(const std::string& path, std::string* response) {
39  return FetchInternal(HttpMethod::kGet, path, "", response);
40 }
41 
42 Status 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 
48 Status 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 
70  while (true) {
71  char temp[kBufferSize];
72  int64_t ret = file->Read(temp, kBufferSize);
73  if (ret <= 0)
74  break;
75  response->append(temp, ret);
76  }
77  return file.release()->CloseWithStatus();
78 }
79 
80 } // namespace media
81 } // namespace shaka
shaka::media::HttpKeyFetcher::Post
virtual Status Post(const std::string &url, const std::string &data, std::string *response)
Definition: http_key_fetcher.cc:42
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::Status
Definition: status.h:110
shaka::media::HttpKeyFetcher::Get
virtual Status Get(const std::string &url, std::string *response)
Definition: http_key_fetcher.cc:38
shaka::media::HttpKeyFetcher::HttpKeyFetcher
HttpKeyFetcher()
Creates a fetcher with no timeout.
Definition: http_key_fetcher.cc:25
shaka::media::HttpKeyFetcher::FetchKeys
Status FetchKeys(const std::string &url, const std::string &request, std::string *response) override
Definition: http_key_fetcher.cc:32