• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
95     return true;
96 }
97 
Unmarshalling(Parcel & parcel,HttpProxy & httpProxy)98 bool HttpProxy::Unmarshalling(Parcel &parcel, HttpProxy &httpProxy)
99 {
100     std::string host;
101     if (!parcel.ReadString(host)) {
102         return false;
103     }
104     if (host.size() > MAX_URL_SIZE) {
105         NETMGR_LOG_E("HttpProxy: Unmarshalling: host length is invalid");
106         return false;
107     }
108 
109     uint16_t port = 0;
110     if (!parcel.ReadUint16(port)) {
111         return false;
112     }
113 
114     uint32_t size = 0;
115     if (!parcel.ReadUint32(size)) {
116         return false;
117     }
118 
119     if (size == 0) {
120         httpProxy = {host, port, {}};
121         return true;
122     }
123 
124     if (size > static_cast<uint32_t>(MAX_EXCLUSION_SIZE)) {
125         size = MAX_EXCLUSION_SIZE;
126     }
127 
128     std::list<std::string> exclusionList;
129     for (uint32_t i = 0; i < size; ++i) {
130         std::string s;
131         parcel.ReadString(s);
132         if (s.size() <= MAX_URL_SIZE) {
133             exclusionList.push_back(s);
134         }
135     }
136 
137     httpProxy = {host, port, exclusionList};
138     return true;
139 }
140 
ToString() const141 std::string HttpProxy::ToString() const
142 {
143     std::string s;
144     std::string tab = "\t";
145     s.append(host_);
146     s.append(tab);
147     s.append(std::to_string(port_));
148     s.append(tab);
149     for (const auto &e : exclusionList_) {
150         s.append(e);
151         s.append(",");
152     }
153     return s;
154 }
155 } // namespace NetManagerStandard
156 } // namespace OHOS
157