AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
CurlWrapper.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 ACSDKASSETSCOMMON_CURLWRAPPER_H_
17 #define ACSDKASSETSCOMMON_CURLWRAPPER_H_
18 
21 #include <curl/curl.h>
22 
23 #include <atomic>
24 #include <memory>
25 #include <ostream>
26 #include <regex>
27 #include <string>
28 
29 #include "DownloadChunkQueue.h"
30 #include "DownloadStream.h"
31 #include "ResponseSink.h"
34 
35 namespace alexaClientSDK {
36 namespace acsdkAssets {
37 namespace common {
38 
43 class CurlWrapper {
44 public:
45  // Destructor takes care of freeing the CURL handle
46  virtual ~CurlWrapper();
47 
55  static std::unique_ptr<CurlWrapper> create(
56  bool isThrottled,
57  std::shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::AuthDelegateInterface> authDelegate,
58  const std::string& certPath = "");
68  const std::string& url,
69  std::ostream& response,
70  const std::weak_ptr<CurlProgressCallbackInterface>& callbackObj);
83  const std::string& url,
84  const std::string& path,
85  const std::weak_ptr<CurlProgressCallbackInterface>& callbackObj,
86  bool unpack = false,
87  size_t size = 0);
88 
91 
106 
118  const std::string& url,
119  std::shared_ptr<ResponseSink> sink,
120  const std::weak_ptr<CurlProgressCallbackInterface>& callbackObj);
121 
122  static std::string getValueFromHeaders(const std::string& headers, const std::string& key);
123 
124 private:
125  explicit CurlWrapper(
126  bool isThrottled,
127  std::shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::AuthDelegateInterface> authDelegate,
128  std::string certPath = "");
129 
130  // Initializes the object, returns true if successful.
131  bool init();
132 
138  bool getAuthorizationHeader(std::string& header);
139 
149  const std::string& fullUrl,
150  std::ostream& responseStream,
151  const std::weak_ptr<CurlProgressCallbackInterface>& callbackObj);
161  commonInterfaces::ResultCode streamToFile(
162  const std::string& downloadUrl,
163  const std::string& filePath,
164  size_t size,
165  const std::weak_ptr<CurlProgressCallbackInterface>& callbackObj);
166 
174  commonInterfaces::ResultCode streamToQueue(
175  const std::string& fullUrl,
176  std::shared_ptr<DownloadChunkQueue> downloadChunkQueue,
177  const std::weak_ptr<CurlProgressCallbackInterface>& callbackObj);
178 
185  bool unpack(std::shared_ptr<DownloadChunkQueue> downloadChunkQueue, const std::string& path);
186 
192  commonInterfaces::ResultCode checkHTTPStatusCode(bool logError = true);
193 
198  commonInterfaces::ResultCode setHTTPHEADER();
199 
200 private:
201  bool m_isThrottled;
202  std::string m_certPath;
203  CURLcode m_code;
204  CURL* m_handle;
205  char m_errorBuffer[CURL_ERROR_SIZE]{};
206  std::string m_header;
207  // the curl handle does not take ownership of the memory in headers, and the handle re-uses headers
208  std::unique_ptr<curl_slist, void (*)(curl_slist*)> m_headers;
209  std::shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::AuthDelegateInterface> m_authDelegate;
210 };
211 } // namespace common
212 } // namespace acsdkAssets
213 } // namespace alexaClientSDK
214 
215 #endif // ACSDKASSETSCOMMON_CURLWRAPPER_H_
commonInterfaces::ResultCode getAndDownloadMultipart(const std::string &url, std::shared_ptr< ResponseSink > sink, const std::weak_ptr< CurlProgressCallbackInterface > &callbackObj)
HeaderResults getHeaders(const std::string &url)
commonInterfaces::ResultCode download(const std::string &url, const std::string &path, const std::weak_ptr< CurlProgressCallbackInterface > &callbackObj, bool unpack=false, size_t size=0)
::std::string string
Definition: gtest-port.h:1097
static std::string getValueFromHeaders(const std::string &headers, const std::string &key)
HeaderResults getHeadersAuthorized(const std::string &url)
static std::unique_ptr< CurlWrapper > create(bool isThrottled, std::shared_ptr< alexaClientSDK::avsCommon::sdkInterfaces::AuthDelegateInterface > authDelegate, const std::string &certPath="")
Definition: common.py:1
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
static const std::string key
The database key to be used by the protocol given the METADATA object.
Definition: SharedAVSSettingProtocolTest.cpp:58

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