AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
LibCurlHttpContentFetcher.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LIBCURLUTILS_LIBCURLHTTPCONTENTFETCHER_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LIBCURLUTILS_LIBCURLHTTPCONTENTFETCHER_H_
18 
19 #include <atomic>
20 #include <future>
21 #include <string>
22 #include <thread>
23 
27 
28 namespace alexaClientSDK {
29 namespace avsCommon {
30 namespace utils {
31 namespace libcurlUtils {
32 
38 public:
47  const std::string& url,
48  const std::shared_ptr<LibcurlSetCurlOptionsCallbackInterface>& setCurlOptionsCallback = nullptr);
49 
52  State getState() override;
53  std::string getUrl() const override;
54  std::string getEffectiveUrl() const override;
55  Header getHeader(std::atomic<bool>* shouldShutdown) override;
56  bool getBody(std::shared_ptr<avsCommon::avs::attachment::AttachmentWriter> writer) override;
57  void shutdown() override;
59 
63  std::unique_ptr<avsCommon::utils::HTTPContent> getContent(
64  FetchOptions option,
65  std::unique_ptr<avsCommon::avs::attachment::AttachmentWriter> writer = nullptr,
66  const std::vector<std::string>& customHeaders = std::vector<std::string>()) override;
67 
68  /*
69  * Destructor.
70  */
71  ~LibCurlHttpContentFetcher() override;
72 
73 private:
75  static size_t headerCallback(char* data, size_t size, size_t nmemb, void* userData);
76 
78  static size_t bodyCallback(char* data, size_t size, size_t nmemb, void* userData);
79 
81  static size_t noopCallback(char* data, size_t size, size_t nmemb, void* userData);
82 
84  State m_state;
85 
92  curl_slist* getCustomHeaderList(const std::vector<std::string>& customHeaders);
93 
95  const std::string m_url;
96 
98  mutable std::mutex m_effectiveUrlMutex;
99 
101  std::string m_effectiveUrl;
102 
104  CurlEasyHandleWrapper m_curlWrapper;
105 
107  std::promise<bool> m_headerPromise;
108 
110  std::shared_future<bool> m_headerFuture;
111 
114 
118  std::shared_ptr<avsCommon::avs::attachment::AttachmentWriter> m_streamWriter;
119 
121  ssize_t m_currentContentReceivedLength;
122 
124  ssize_t m_totalContentReceivedLength;
125 
127  std::atomic<bool> m_done;
128 
130  std::atomic<bool> m_isShutdown;
131 
136  std::thread m_thread;
137 
139  std::atomic<bool> m_hasObjectBeenUsed;
140 
142  std::mutex m_stateMutex;
143 
148  std::mutex m_getBodyMutex;
149 
156  void reportInvalidStateTransitionAttempt(State currentState, State newState);
157 
166  void stateTransition(State newState, bool value);
167 
172  bool waitingForBodyRequest();
173 
178  void updateEffectiveURL();
179 };
180 
181 } // namespace libcurlUtils
182 } // namespace utils
183 } // namespace avsCommon
184 } // namespace alexaClientSDK
185 
186 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LIBCURLUTILS_LIBCURLHTTPCONTENTFETCHER_H_
::std::string string
Definition: gtest-port.h:1097
bool getBody(std::shared_ptr< avsCommon::avs::attachment::AttachmentWriter > writer) override
std::unique_ptr< avsCommon::utils::HTTPContent > getContent(FetchOptions option, std::unique_ptr< avsCommon::avs::attachment::AttachmentWriter > writer=nullptr, const std::vector< std::string > &customHeaders=std::vector< std::string >()) override
State
The state of payload fetching.
Definition: HTTPContentFetcherInterface.h:43
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
LibCurlHttpContentFetcher(const std::string &url, const std::shared_ptr< LibcurlSetCurlOptionsCallbackInterface > &setCurlOptionsCallback=nullptr)
FetchOptions
Represents what HTTP content to fetch.
Definition: HTTPContentFetcherInterface.h:35
Header getHeader(std::atomic< bool > *shouldShutdown) override

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0