• 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::set<std::string> & exclusionList)27 HttpProxy::HttpProxy(std::string host, uint16_t port, const std::set<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_.insert(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::set<std::string> HttpProxy::GetExclusionList() const
56 {
57     return exclusionList_;
58 }
59 
Marshalling(Parcel & parcel) const60 bool HttpProxy::Marshalling(Parcel &parcel) const
61 {
62     if (!parcel.WriteString(host_)) {
63         return false;
64     }
65 
66     if (!parcel.WriteUint16(port_)) {
67         return false;
68     }
69 
70     if (!parcel.WriteUint32(static_cast<uint32_t>(std::min(MAX_EXCLUSION_SIZE, exclusionList_.size())))) {
71         return false;
72     }
73 
74     uint32_t size = 0;
75     for (const auto &s : exclusionList_) {
76         if (!parcel.WriteString(s)) {
77             return false;
78         }
79         ++size;
80         if (size >= MAX_EXCLUSION_SIZE) {
81             return true;
82         }
83     }
84 
85     return true;
86 }
87 
Unmarshalling(Parcel & parcel,HttpProxy & httpProxy)88 bool HttpProxy::Unmarshalling(Parcel &parcel, HttpProxy &httpProxy)
89 {
90     std::string host;
91     if (!parcel.ReadString(host)) {
92         return false;
93     }
94     if (host.size() > MAX_URL_SIZE) {
95         NETMGR_LOG_E("HttpProxy: Unmarshalling: host length is invalid");
96         return false;
97     }
98 
99     uint16_t port = 0;
100     if (!parcel.ReadUint16(port)) {
101         return false;
102     }
103 
104     uint32_t size = 0;
105     if (!parcel.ReadUint32(size)) {
106         return false;
107     }
108 
109     if (size == 0) {
110         httpProxy = {host, port, {}};
111         return true;
112     }
113 
114     if (size > static_cast<uint32_t>(MAX_EXCLUSION_SIZE)) {
115         size = MAX_EXCLUSION_SIZE;
116     }
117 
118     std::set<std::string> exclusionList;
119     for (uint32_t i = 0; i < size; ++i) {
120         std::string s;
121         parcel.ReadString(s);
122         if (s.size() <= MAX_URL_SIZE) {
123             exclusionList.insert(s);
124         }
125     }
126 
127     httpProxy = {host, port, exclusionList};
128     return true;
129 }
130 
ToString() const131 std::string HttpProxy::ToString() const
132 {
133     std::string s;
134     std::string tab = "\t";
135     s.append(host_);
136     s.append(tab);
137     s.append(std::to_string(port_));
138     s.append(tab);
139     for (const auto &e : exclusionList_) {
140         s.append(e);
141         s.append(",");
142     }
143     return s;
144 }
145 } // namespace NetManagerStandard
146 } // namespace OHOS
147