1 /*
2 * Copyright (c) 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 "net_mgr_log_wrapper.h"
17
18 #include "http_proxy.h"
19
20 namespace OHOS {
21 namespace NetManagerStandard {
22 static const size_t MAX_EXCLUSION_SIZE = 500;
23 static const size_t MAX_URL_SIZE = 2048;
24
HttpProxy()25 HttpProxy::HttpProxy() : port_(0) {}
26
HttpProxy(std::string host,uint16_t port,const std::list<std::string> & exclusionList)27 HttpProxy::HttpProxy(std::string host, uint16_t port, const std::list<std::string> &exclusionList) : port_(0)
28 {
29 if (host.size() <= MAX_URL_SIZE) {
30 host_ = std::move(host);
31 port_ = port;
32 for (const auto &s : exclusionList) {
33 if (s.size() <= MAX_URL_SIZE) {
34 exclusionList_.push_back(s);
35 }
36 if (exclusionList_.size() >= MAX_EXCLUSION_SIZE) {
37 break;
38 }
39 }
40 } else {
41 NETMGR_LOG_E("HttpProxy: host length is invalid");
42 }
43 }
44
GetHost() const45 std::string HttpProxy::GetHost() const
46 {
47 return host_;
48 }
49
GetPort() const50 uint16_t HttpProxy::GetPort() const
51 {
52 return port_;
53 }
54
GetExclusionList() const55 std::list<std::string> HttpProxy::GetExclusionList() const
56 {
57 return exclusionList_;
58 }
59
operator ==(const HttpProxy & httpProxy) const60 bool HttpProxy::operator==(const HttpProxy &httpProxy) const
61 {
62 return (host_ == httpProxy.host_ && port_ == httpProxy.port_ && exclusionList_ == httpProxy.exclusionList_);
63 }
64
operator !=(const HttpProxy & httpProxy) const65 bool HttpProxy::operator!=(const HttpProxy &httpProxy) const
66 {
67 return !(httpProxy == *this);
68 }
69
Marshalling(Parcel & parcel) const70 bool HttpProxy::Marshalling(Parcel &parcel) const
71 {
72 if (!parcel.WriteString(host_)) {
73 return false;
74 }
75
76 if (!parcel.WriteUint16(port_)) {
77 return false;
78 }
79
80 if (!parcel.WriteUint32(static_cast<uint32_t>(std::min(MAX_EXCLUSION_SIZE, exclusionList_.size())))) {
81 return false;
82 }
83
84 uint32_t size = 0;
85 for (const auto &s : exclusionList_) {
86 if (!parcel.WriteString(s)) {
87 return false;
88 }
89 ++size;
90 if (size >= MAX_EXCLUSION_SIZE) {
91 return true;
92 }
93 }
94 parcel.WriteString(username_);
95 parcel.WriteString(password_);
96 return true;
97 }
98
Unmarshalling(Parcel & parcel,HttpProxy & httpProxy)99 bool HttpProxy::Unmarshalling(Parcel &parcel, HttpProxy &httpProxy)
100 {
101 std::string host;
102 if (!parcel.ReadString(host)) {
103 return false;
104 }
105 if (host.size() > MAX_URL_SIZE) {
106 NETMGR_LOG_E("HttpProxy: Unmarshalling: host length is invalid");
107 return false;
108 }
109
110 uint16_t port = 0;
111 if (!parcel.ReadUint16(port)) {
112 return false;
113 }
114
115 uint32_t size = 0;
116 if (!parcel.ReadUint32(size)) {
117 return false;
118 }
119
120 if (size == 0) {
121 httpProxy = {host, port, {}};
122 return true;
123 }
124
125 if (size > static_cast<uint32_t>(MAX_EXCLUSION_SIZE)) {
126 size = MAX_EXCLUSION_SIZE;
127 }
128
129 std::list<std::string> exclusionList;
130 for (uint32_t i = 0; i < size; ++i) {
131 std::string s;
132 if (!parcel.ReadString(s)) {
133 return false;
134 }
135 if (s.size() <= MAX_URL_SIZE) {
136 exclusionList.push_back(s);
137 }
138 }
139
140 httpProxy = {host, port, exclusionList};
141 parcel.ReadString(httpProxy.username_);
142 parcel.ReadString(httpProxy.password_);
143 return true;
144 }
145
ToString() const146 std::string HttpProxy::ToString() const
147 {
148 std::string s;
149 std::string tab = "\t";
150 s.append(host_);
151 s.append(tab);
152 s.append(std::to_string(port_));
153 s.append(tab);
154 for (const auto &e : exclusionList_) {
155 s.append(e);
156 s.append(",");
157 }
158 return s;
159 }
160 } // namespace NetManagerStandard
161 } // namespace OHOS
162