1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <proxygen/lib/http/ProxyStatus.h>
10 
11 #include <glog/logging.h>
12 
13 namespace {
14 
15 constexpr folly::StringPiece kProxyParam{"e_proxy"};
16 constexpr folly::StringPiece kUpstreamIPParam{"e_upip"};
17 constexpr folly::StringPiece kIsProxyErrorParam{"e_isproxyerr"};
18 constexpr folly::StringPiece kClientAddrParam{"e_clientaddr"};
19 
20 } // namespace
21 
22 namespace proxygen {
23 
ProxyStatus(StatusType statusType)24 ProxyStatus::ProxyStatus(StatusType statusType) : statusType_{statusType} {
25   pIdent_.identifier = getStatusTypeString(statusType_);
26 }
27 
getStatusType() const28 StatusType ProxyStatus::getStatusType() const {
29   return statusType_;
30 }
31 
setStatusType(StatusType statusType)32 void ProxyStatus::setStatusType(StatusType statusType) {
33   statusType_ = statusType;
34   pIdent_.identifier = getStatusTypeString(statusType_);
35 };
36 
setProxyStatusParameter(folly::StringPiece name,const std::string & text)37 ProxyStatus& ProxyStatus::setProxyStatusParameter(folly::StringPiece name,
38                                                   const std::string& text) {
39   if (text.empty()) {
40     return *this;
41   }
42 
43   pIdent_.parameterMap[name.str()] =
44       StructuredHeaderItem(StructuredHeaderItem::Type::STRING, text);
45   return *this;
46 }
47 
setProxy(const std::string & proxy)48 ProxyStatus& ProxyStatus::setProxy(const std::string& proxy) {
49   return setProxyStatusParameter(kProxyParam, proxy);
50 }
51 
setUpstreamIP(const std::string & upstreamIP)52 ProxyStatus& ProxyStatus::setUpstreamIP(const std::string& upstreamIP) {
53   return setProxyStatusParameter(kUpstreamIPParam, upstreamIP);
54 }
55 
setProxyError(const bool isProxyError)56 ProxyStatus& ProxyStatus::setProxyError(const bool isProxyError) {
57   if (isProxyError) {
58     return setProxyStatusParameter(kIsProxyErrorParam, "true");
59   } else {
60     return setProxyStatusParameter(kIsProxyErrorParam, "false");
61   }
62 }
63 
setClientAddress(const std::string & clientAddr)64 ProxyStatus& ProxyStatus::setClientAddress(const std::string& clientAddr) {
65   return setProxyStatusParameter(kClientAddrParam, clientAddr);
66 }
67 
hasUpstreamIP() const68 bool ProxyStatus::hasUpstreamIP() const {
69   return pIdent_.parameterMap.find(std::string(kUpstreamIPParam)) !=
70          pIdent_.parameterMap.end();
71 }
72 
toString() const73 std::string ProxyStatus::toString() const {
74   StructuredHeaders::ParameterisedList plist;
75   StructuredHeadersEncoder encoder;
76   plist.emplace_back(std::move(pIdent_));
77   encoder.encodeParameterisedList(plist);
78 
79   return encoder.get();
80 }
81 
isEmpty() const82 bool ProxyStatus::isEmpty() const {
83   return statusType_ == StatusType::ENUM_COUNT;
84 }
85 
86 } // namespace proxygen
87