1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HTTP_REQUEST_H 17 #define HTTP_REQUEST_H 18 19 #include <string> 20 #include <stdint.h> 21 22 namespace OHOS { 23 namespace NetManagerStandard { 24 class HttpRequest { 25 public: 26 HttpRequest(); 27 ~HttpRequest(); 28 /** 29 * @brief :set the interface name for HTTP request. 30 * 31 * @param ifaceName - interface name.[in] 32 */ 33 void SetIfaceName(const std::string &ifaceName); 34 /** 35 * @brief : HTTP get header request. 36 * 37 * @param strUrl - HTTP request URL.[in] 38 * @param strHeader - HTTP request header.[out] 39 * @return int32_t 0:OK,otherwise:failure 40 */ 41 int32_t HttpGetHeader(const std::string &strUrl, std::string &strHeader); 42 /** 43 * @brief : HttpGet request. 44 * 45 * @param strUrl - HTTP request URL.[in] 46 * @param strResponse - HTTP request response.[out] 47 * @return int32_t 0:OK,otherwise:failure 48 */ 49 int32_t HttpGet(const std::string &strUrl, std::string &strResponse); 50 /** 51 * @brief : HttpPost request. 52 * 53 * @param strUrl - HTTP request URL.[in] 54 * @param strData - data send in a POST request.[in] 55 * @param strResponse - HTTP request response.[out] 56 * @return int32_t 0:OK,otherwise:failure 57 */ 58 int32_t HttpPost(const std::string &strUrl, const std::string &strData, std::string &strResponse); 59 /** 60 * @brief : Set the transfer timeout in milliseconds. 61 * 62 * @param timeout - timeout in milliseconds.[in] 63 */ 64 void SetTransportTimeout(int64_t timeout); 65 /** 66 * @brief : Get total time of last transfer in milliseconds. 67 */ 68 int64_t GetLastTotalTime() const; 69 private: 70 enum class HttpReqType { 71 HTTP_REQUEST_TYPE_GET, 72 HTTP_REQUEST_TYPE_POST, 73 }; 74 /** 75 * @brief : Executes HTTP requests, GET or POST. 76 * 77 * @param type - Http request type.[in] 78 * @param strUrl - HTTP request URL.[in] 79 * @param strData - Data send in a POST request.[in] 80 * @param strResponse - HTTP request response.[out] 81 * @return int32_t 0:OK,otherwise:failure 82 */ 83 int32_t HttpRequestExec( 84 HttpReqType type, const std::string &strUrl, const std::string &strData, std::string &strResponse); 85 /** 86 * @brief : Only get HTTP header. 87 * 88 * @param strUrl - HTTP request URL.[in] 89 * @param strHeader - HTTP request header.[out] 90 * @return int32_t 0:OK,otherwise:failure 91 */ 92 int32_t HttpRequestHeaderExec(const std::string &strUrl, std::string &strHeader); 93 94 typedef void CURL; 95 class CURLClean { 96 public: 97 void operator()(CURL *p) const; 98 }; 99 /** 100 * @brief : Parse http exec result. 101 * 102 * @param curl - curl smart pointer.[in] 103 * @param rlt - result of HTTP request.[in] 104 * @return int32_t 0:OK,otherwise:failure 105 */ 106 int32_t ParseExecResult(const std::unique_ptr<CURL, CURLClean> &curl, int32_t rlt); 107 /** 108 * @brief : Set the HTTP request option. 109 * 110 * @param curl - curl smart pointer.[in] 111 * @param type - HTTP request type.[in] 112 * @param strUrl - HTTP request URL.[in] 113 * @param strData - Data send in a POST request.[in] 114 * @param strResponse - HTTP request response.[out] 115 * @return int32_t 0:OK,otherwise:failure 116 */ 117 int32_t SetCurlOpt(const std::unique_ptr<CURL, CURLClean> &curl, HttpReqType type, const std::string &strUrl, 118 const std::string &strData, std::string &strResponse); 119 /** 120 * @brief : Set the HTTP header option. 121 * 122 * @param curl - curl smart pointer.[in] 123 * @param strUrl - HTTP request URL.[in] 124 * @param strHeader - HTTP request header response.[out] 125 * @return int32_t 0:OK,otherwise:failure 126 */ 127 int32_t SetCurlOptHeader( 128 const std::unique_ptr<CURL, CURLClean> &curl, const std::string &strUrl, std::string &strHeader); 129 /** 130 * @brief : Set the HTTP request common option. 131 * 132 * @param curl - curl smart pointer.[in] 133 * @param strUrl - HTTP request URL.[in] 134 * @return int32_t 0:OK,otherwise:failure 135 */ 136 int32_t SetCurlOptCommon(const std::unique_ptr<CURL, CURLClean> &curl, const std::string &strUrl); 137 /** 138 * @brief : data callback function 139 * 140 * @param data - data pointer.[in] 141 * @param size - the size of each data element.[in] 142 * @param nmemb - the size of data element.[in] 143 * @param strBuffer - the buffer that writes the data.[out] 144 * @return int32_t - the size of data in bytes. 145 */ 146 static int32_t DataCallback(char *data, size_t size, size_t nmemb, std::string *strBuffer); 147 private: 148 static constexpr int32_t URL_SIZE = 1024; 149 static constexpr int64_t CONNECTION_TIMEOUT = 1500; 150 static constexpr int64_t TRANS_OP_TIMEOUT = 2000; 151 static constexpr int32_t MAX_CONNECT_NUM = 8; 152 static constexpr int32_t DEFAULT_ERROR_SIZE = 256; 153 int64_t connectionTimeout_ = CONNECTION_TIMEOUT; 154 int64_t transOpTimeout_ = TRANS_OP_TIMEOUT; 155 char errorBuffer_[DEFAULT_ERROR_SIZE] = {0}; 156 std::string ifaceName_; 157 int64_t lastTransTime_ = 0; 158 }; 159 } // namespace NetManagerStandard 160 } // namespace OHOS 161 #endif // HTTP_REQUEST_H