AlexaClientSDK  1.25.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 <string>
22 
25 
27 #ifdef ACSDK_EMIT_SENSITIVE_LOGS
28 
29 #define ACSDK_EMIT_CURL_LOGS
30 #include <fstream>
32 
33 #endif
34 
35 namespace alexaClientSDK {
36 namespace avsCommon {
37 namespace utils {
38 namespace libcurlUtils {
39 // Forward declaration
40 class CurlEasyHandleWrapper;
41 
42 // This abstracts the setting of curl options from the CurlEasyHandleWrapper
44 public:
45  CurlEasyHandleWrapperOptionsSettingAdapter(CurlEasyHandleWrapper* wrapper) : m_easyHandleWrapper(wrapper) {
46  }
47 
55  template <typename ParamType>
56  bool setopt(CURLoption option, ParamType param);
57 
58 private:
59  CurlEasyHandleWrapper* m_easyHandleWrapper;
60 };
61 
66 public:
75  using CurlCallback = size_t (*)(char* buffer, size_t blockSize, size_t numBlocks, void* userData);
76 
86  using CurlDebugCallback =
87  int (*)(CURL* handle, curl_infotype infoType, char* buffer, size_t blockSize, void* userData);
88 
92  enum class TransferType {
94  kGET,
96  kPOST,
98  kPUT
99  };
100 
107  CurlEasyHandleWrapper(std::string id = "");
108 
113 
127  bool reset(std::string id = "");
128 
135  CURL* getCurlHandle();
136 
142  bool isValid();
143 
149  std::string getId() const;
150 
151  /*
152  * Adds an HTTP Header to the current easy handle
153  *
154  * @param header The HTTP header to add
155  * @returns true if the addition was successful
156  */
157  bool addHTTPHeader(const std::string& header);
158 
159  /*
160  * Adds a POST Header to the list of the headers to add to the future POST request
161  *
162  * @param header The HTTP header to add
163  * @returns true if the addition was successful
164  */
165  bool addPostHeader(const std::string& header);
166 
173  bool setURL(const std::string& url);
174 
181  bool setTransferType(TransferType type);
182 
190  bool setTransferTimeout(const long timeoutSeconds);
191 
198  bool setPostData(const std::string& data);
199 
208  bool setConnectionTimeout(const std::chrono::seconds timeoutSeconds);
209 
217  bool setWriteCallback(CurlCallback callback, void* userData);
218 
227  bool setHeaderCallback(CurlCallback callback, void* userData);
228 
236  bool setReadCallback(CurlCallback callback, void* userData);
237 
244  std::string urlEncode(const std::string& in) const;
245 
251  long getHTTPResponseCode();
252 
260  std::string getEffectiveUrl();
261 
267  CURLcode perform();
268 
276  CURLcode pause(int mask);
277 
290  static void setInterfaceName(const std::string& interfaceName);
291 
297  static std::string getInterfaceName();
298 
299  CurlEasyHandleWrapperOptionsSettingAdapter& curlOptionsSetter();
300 
301 private:
309  template <typename ParamType>
310  bool setopt(CURLoption option, ParamType param);
311 
320  void cleanupResources();
321 
327  bool setDefaultOptions();
328 
329 #ifdef ACSDK_EMIT_CURL_LOGS
330 
333  void initStreamLog();
334 
345  static int debugFunction(CURL* handle, curl_infotype type, char* data, size_t size, void* user);
346 
348  std::unique_ptr<std::ofstream> m_streamLog;
350  std::unique_ptr<std::ofstream> m_streamInDump;
352  std::unique_ptr<std::ofstream> m_streamOutDump;
355 #endif
356 
358  static void initializeNetworkInterfaceNameLocked();
359 
361  CURL* m_handle;
363  curl_slist* m_requestHeaders;
365  curl_slist* m_postHeaders;
367  curl_httppost* m_post;
369  curl_httppost* m_lastPost;
371  std::string m_id;
373  static std::mutex m_interfaceNameMutex;
375  static bool m_isInterfaceNameInitialized;
377  static std::string m_interfaceName;
379  static std::atomic<uint64_t> m_idGenerator;
380 
381  CurlEasyHandleWrapperOptionsSettingAdapter m_curlOptionsSettingAdapter;
382 
384 };
385 
386 template <typename ParamType>
387 bool CurlEasyHandleWrapper::setopt(CURLoption option, ParamType value) {
388  auto result = curl_easy_setopt(m_handle, option, value);
389  if (result != CURLE_OK) {
390  logger::acsdkError(logger::LogEntry("CurlEasyHandleWrapper", "setoptFailed")
391  .d("reason", "curl_easy_setopt failed")
392  .d("option", option)
393  .sensitive("value", value)
394  .d("result", result)
395  .d("error", curl_easy_strerror(result)));
396  return false;
397  }
398  return true;
399 }
400 
401 template <typename ParamType>
402 bool CurlEasyHandleWrapperOptionsSettingAdapter::setopt(CURLoption option, ParamType value) {
403  return m_easyHandleWrapper->setopt(option, std::forward<ParamType>(value));
404 }
405 
406 } // namespace libcurlUtils
407 } // namespace utils
408 } // namespace avsCommon
409 } // namespace alexaClientSDK
410 
411 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LIBCURLUTILS_CURLEASYHANDLEWRAPPER_H_
static bool setopt(CURL *handle, CURLoption option, ValueType value, const char *optionName, const char *valueAsString)
Definition: LibcurlUtils.cpp:76
bool setopt(CURLoption option, ParamType param)
Definition: CurlEasyHandleWrapper.h:402
CurlEasyHandleWrapperOptionsSettingAdapter(CurlEasyHandleWrapper *wrapper)
Definition: CurlEasyHandleWrapper.h:45
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:75
void acsdkError(const LogEntry &entry)
Definition: LoggerUtils.cpp:80
int(*)(CURL *handle, curl_infotype infoType, char *buffer, size_t blockSize, void *userData) CurlDebugCallback
Definition: CurlEasyHandleWrapper.h:87
LogEntry is used to compile the log entry text to log via Logger.
Definition: LogEntry.h:32

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