AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
CurlEasyHandleWrapper.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_CURLEASYHANDLEWRAPPER_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LIBCURLUTILS_CURLEASYHANDLEWRAPPER_H_
18 
19 #include <chrono>
20 #include <curl/curl.h>
21 #include <fstream>
22 #include <string>
23 
27 
29 #ifdef ACSDK_EMIT_SENSITIVE_LOGS
30 #define ACSDK_EMIT_CURL_LOGS
31 #endif
32 
33 namespace alexaClientSDK {
34 namespace avsCommon {
35 namespace utils {
36 namespace libcurlUtils {
37 // Forward declaration
38 class CurlEasyHandleWrapper;
39 
40 // This abstracts the setting of curl options from the CurlEasyHandleWrapper
42 public:
43  CurlEasyHandleWrapperOptionsSettingAdapter(CurlEasyHandleWrapper* wrapper) : m_easyHandleWrapper(wrapper) {
44  }
45 
53  template <typename ParamType>
54  bool setopt(CURLoption option, ParamType param);
55 
56 private:
57  CurlEasyHandleWrapper* m_easyHandleWrapper;
58 };
59 
64 public:
73  using CurlCallback = size_t (*)(char* buffer, size_t blockSize, size_t numBlocks, void* userData);
74 
84  using CurlDebugCallback =
85  int (*)(CURL* handle, curl_infotype infoType, char* buffer, size_t blockSize, void* userData);
86 
90  enum class TransferType {
92  kGET,
94  kPOST,
96  kPUT
97  };
98 
106 
111 
125  bool reset(std::string id = "");
126 
133  CURL* getCurlHandle();
134 
140  bool isValid();
141 
147  std::string getId() const;
148 
149  /*
150  * Adds an HTTP Header to the current easy handle
151  *
152  * @param header The HTTP header to add
153  * @returns true if the addition was successful
154  */
155  bool addHTTPHeader(const std::string& header);
156 
157  /*
158  * Adds a POST Header to the list of the headers to add to the future POST request
159  *
160  * @param header The HTTP header to add
161  * @returns true if the addition was successful
162  */
163  bool addPostHeader(const std::string& header);
164 
171  bool setURL(const std::string& url);
172 
179  bool setTransferType(TransferType type);
180 
188  bool setTransferTimeout(const long timeoutSeconds);
189 
196  bool setPostData(const std::string& data);
197 
206  bool setConnectionTimeout(const std::chrono::seconds timeoutSeconds);
207 
215  bool setWriteCallback(CurlCallback callback, void* userData);
216 
225  bool setHeaderCallback(CurlCallback callback, void* userData);
226 
234  bool setReadCallback(CurlCallback callback, void* userData);
235 
242  std::string urlEncode(const std::string& in) const;
243 
249  long getHTTPResponseCode();
250 
258  std::string getEffectiveUrl();
259 
265  CURLcode perform();
266 
274  CURLcode pause(int mask);
275 
288  static void setInterfaceName(const std::string& interfaceName);
289 
295  static std::string getInterfaceName();
296 
297  CurlEasyHandleWrapperOptionsSettingAdapter& curlOptionsSetter();
298 
299 private:
307  template <typename ParamType>
308  bool setopt(CURLoption option, ParamType param);
309 
318  void cleanupResources();
319 
325  bool setDefaultOptions();
326 
327 #ifdef ACSDK_EMIT_CURL_LOGS
328 
331  void initStreamLog();
332 
343  static int debugFunction(CURL* handle, curl_infotype type, char* data, size_t size, void* user);
344 #endif
345 
347  static void initializeNetworkInterfaceNameLocked();
348 
350  CURL* m_handle;
352  curl_slist* m_requestHeaders;
354  curl_slist* m_postHeaders;
356  curl_httppost* m_post;
358  curl_httppost* m_lastPost;
360  std::string m_id;
362  static std::mutex m_interfaceNameMutex;
364  static bool m_isInterfaceNameInitialized;
366  static std::string m_interfaceName;
368  static std::atomic<uint64_t> m_idGenerator;
369 
370  CurlEasyHandleWrapperOptionsSettingAdapter m_curlOptionsSettingAdapter;
371 
373  std::unique_ptr<std::ofstream> m_streamLog;
375  std::unique_ptr<std::ofstream> m_streamInDump;
377  std::unique_ptr<std::ofstream> m_streamOutDump;
380 
382 };
383 
384 template <typename ParamType>
385 bool CurlEasyHandleWrapper::setopt(CURLoption option, ParamType value) {
386  auto result = curl_easy_setopt(m_handle, option, value);
387  if (result != CURLE_OK) {
388  logger::acsdkError(logger::LogEntry("CurlEasyHandleWrapper", "setoptFailed")
389  .d("reason", "curl_easy_setopt failed")
390  .d("option", option)
391  .sensitive("value", value)
392  .d("result", result)
393  .d("error", curl_easy_strerror(result)));
394  return false;
395  }
396  return true;
397 }
398 
399 template <typename ParamType>
400 bool CurlEasyHandleWrapperOptionsSettingAdapter::setopt(CURLoption option, ParamType value) {
401  return m_easyHandleWrapper->setopt(option, std::forward<ParamType>(value));
402 }
403 
404 } // namespace libcurlUtils
405 } // namespace utils
406 } // namespace avsCommon
407 } // namespace alexaClientSDK
408 
409 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LIBCURLUTILS_CURLEASYHANDLEWRAPPER_H_
bool setopt(CURLoption option, ParamType param)
Definition: CurlEasyHandleWrapper.h:400
::std::string string
Definition: gtest-port.h:1097
CurlEasyHandleWrapperOptionsSettingAdapter(CurlEasyHandleWrapper *wrapper)
Definition: CurlEasyHandleWrapper.h:43
CURLcode curl_easy_setopt(CURL *curl, CURLoption option,...)
Definition: CurlWrapperMock.cpp:87
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
size_t(*)(char *buffer, size_t blockSize, size_t numBlocks, void *userData) CurlCallback
Definition: CurlEasyHandleWrapper.h:73
type
Definition: upload.py:443
void acsdkError(const LogEntry &entry)
int(*)(CURL *handle, curl_infotype infoType, char *buffer, size_t blockSize, void *userData) CurlDebugCallback
Definition: CurlEasyHandleWrapper.h:85
LogEntry is used to compile the log entry text to log via Logger.
Definition: LogEntry.h:33

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