1 /*
2 * Copyright (c) 2021-2022 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 #include <cstring>
17 #include <memory>
18
19 #include "http_constant.h"
20
21 #include "http_request_utils.h"
22
23 namespace OHOS {
24 namespace ACELite {
Split(const std::string & str,const std::string & sep)25 std::vector<std::string> Split(const std::string &str, const std::string &sep)
26 {
27 std::string s = str;
28 std::vector<std::string> res;
29 while (!s.empty()) {
30 auto pos = s.find(sep);
31 if (pos == std::string::npos) {
32 res.emplace_back(s);
33 break;
34 }
35 res.emplace_back(s.substr(0, pos));
36 s = s.substr(pos + sep.size());
37 }
38 return res;
39 }
40
Strip(const std::string & str,char ch)41 std::string Strip(const std::string &str, char ch)
42 {
43 int64_t i = 0;
44 while (i < str.size() && str[i] == ch) {
45 ++i;
46 }
47 int64_t j = static_cast<int64_t>(str.size()) - 1;
48 while (j > 0 && str[j] == ch) {
49 --j;
50 }
51 if (i >= 0 && i < str.size() && j >= 0 && j < str.size() && j - i + 1 > 0) {
52 return str.substr(i, j - i + 1);
53 }
54 return "";
55 }
56
MethodForGet(const std::string & method)57 bool MethodForGet(const std::string &method)
58 {
59 return (method == HttpConstant::HTTP_METHOD_HEAD || method == HttpConstant::HTTP_METHOD_OPTIONS ||
60 method == HttpConstant::HTTP_METHOD_DELETE || method == HttpConstant::HTTP_METHOD_TRACE ||
61 method == HttpConstant::HTTP_METHOD_GET);
62 }
63
MethodForPost(const std::string & method)64 bool MethodForPost(const std::string &method)
65 {
66 return (method == HttpConstant::HTTP_METHOD_POST || method == HttpConstant::HTTP_METHOD_PUT);
67 }
68
MakeUrl(const std::string & url,std::string param,const std::string & extraParam)69 std::string MakeUrl(const std::string &url, std::string param, const std::string &extraParam)
70 {
71 if (param.empty()) {
72 param += extraParam;
73 } else {
74 param += HttpConstant::HTTP_URL_PARAM_DELIMITER;
75 param += extraParam;
76 }
77
78 if (param.empty()) {
79 return url;
80 }
81
82 return url + HttpConstant::HTTP_URL_PARAM_SEPARATOR + param;
83 }
84
EncodeUrlParam(CURL * curl,std::string & url)85 bool EncodeUrlParam(CURL *curl, std::string &url)
86 {
87 auto index = url.find(HttpConstant::HTTP_URL_PARAM_SEPARATOR);
88 if (index == std::string::npos) {
89 return false;
90 }
91
92 std::string param = url.substr(index + 1);
93 if (param.empty()) {
94 return false;
95 }
96
97 std::unique_ptr<char, decltype(&curl_free)> encodeOut(
98 curl_easy_escape(curl, param.c_str(), static_cast<int>(strlen(param.c_str()))), curl_free);
99 if (encodeOut == nullptr || strlen(encodeOut.get()) == 0) {
100 return false;
101 }
102 url.resize(index);
103 url.append(HttpConstant::HTTP_URL_PARAM_SEPARATOR).append(encodeOut.get());
104 return true;
105 }
106 } // namespace ACELite
107 } // namespace OHOS