Shaka Packager SDK
http_file.h
1 // Copyright 2020 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 #ifndef PACKAGER_FILE_HTTP_H_
8 #define PACKAGER_FILE_HTTP_H_
9 
10 #include <memory>
11 #include <string>
12 
13 #include "packager/base/synchronization/waitable_event.h"
14 #include "packager/file/file.h"
15 #include "packager/file/io_cache.h"
16 #include "packager/status.h"
17 
18 typedef void CURL;
19 struct curl_slist;
20 
21 namespace shaka {
22 
23 enum class HttpMethod {
24  kGet,
25  kPost,
26  kPut,
27 };
28 
38 class HttpFile : public File {
39  public:
40  HttpFile(HttpMethod method, const std::string& url);
41  HttpFile(HttpMethod method,
42  const std::string& url,
43  const std::string& upload_content_type,
44  const std::vector<std::string>& headers,
45  uint32_t timeout_in_seconds);
46 
47  HttpFile(const HttpFile&) = delete;
48  HttpFile& operator=(const HttpFile&) = delete;
49 
50  Status CloseWithStatus();
51 
54  bool Close() override;
55  int64_t Read(void* buffer, uint64_t length) override;
56  int64_t Write(const void* buffer, uint64_t length) override;
57  int64_t Size() override;
58  bool Flush() override;
59  bool Seek(uint64_t position) override;
60  bool Tell(uint64_t* position) override;
61  bool Open() override;
63 
64  protected:
65  ~HttpFile() override;
66 
67  private:
68  struct CurlDelete {
69  void operator()(CURL* curl);
70  void operator()(curl_slist* headers);
71  };
72 
73  void SetupRequest();
74  void ThreadMain();
75 
76  const std::string url_;
77  const std::string upload_content_type_;
78  const uint32_t timeout_in_seconds_;
79  const HttpMethod method_;
80  IoCache download_cache_;
81  IoCache upload_cache_;
82  std::unique_ptr<CURL, CurlDelete> curl_;
83  // The headers need to remain alive for the duration of the request.
84  std::unique_ptr<curl_slist, CurlDelete> request_headers_;
85  Status status_;
86  std::string user_agent_;
87 
88  // Signaled when the "curl easy perform" task completes.
89  base::WaitableEvent task_exit_event_;
90 };
91 
92 } // namespace shaka
93 
94 #endif // PACKAGER_FILE_HTTP_H_
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::HttpFile
Definition: http_file.h:38
shaka::Status
Definition: status.h:110
shaka::IoCache
Declaration of class which implements a thread-safe circular buffer.
Definition: io_cache.h:19
shaka::HttpFile::Tell
bool Tell(uint64_t *position) override
Definition: http_file.cc:265
shaka::HttpFile::Close
bool Close() override
Definition: http_file.cc:236
shaka::HttpFile::Size
int64_t Size() override
Definition: http_file.cc:250
shaka::File
Define an abstract file interface.
Definition: file.h:28
shaka::HttpFile::Seek
bool Seek(uint64_t position) override
Definition: http_file.cc:260
shaka::HttpFile::Read
int64_t Read(void *buffer, uint64_t length) override
Definition: http_file.cc:240
shaka::HttpFile::Open
bool Open() override
Internal open. Should not be used directly.
Definition: http_file.cc:203
shaka::HttpFile::Write
int64_t Write(const void *buffer, uint64_t length) override
Definition: http_file.cc:245
shaka::HttpFile::Flush
bool Flush() override
Definition: http_file.cc:255