-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJsonClient.cpp
120 lines (102 loc) · 3.57 KB
/
JsonClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// Created by victor on 11/25/17.
//
#include "JsonClient.h"
#include <proxygen/lib/http/session/HTTPUpstreamSession.h>
namespace restdbxx {
void JsonClient::connectSuccess(proxygen::HTTPUpstreamSession *session) noexcept {
_trx = session->newTransaction(&trx_handler);
_trx->sendHeaders(*_request);
auto method = _request->getMethodString();
if (method == "POST" || method == "PUT") {
// we nned to send body
// not implemented yet.
}
_trx->sendEOM();
}
void JsonClient::connectError(const folly::AsyncSocketException &ex) noexcept {
}
JsonClient::JsonClient(folly::HHWheelTimer *timer) :
_trx{nullptr},
_connector{this, timer},
trx_handler(*this) {
}
void JsonClient::fetch(proxygen::HTTPMessage *req, folly::Promise<folly::dynamic> &promise) {
_request.reset(req);// = std::move(req);
_promise = std::move(promise);
auto evb = folly::EventBaseManager::get()->getEventBase();
proxygen::URL url{_request->getURL()};
_request->getHeaders().add(proxygen::HTTPHeaderCode::HTTP_HEADER_HOST, url.getHost());
_request->getHeaders().add(proxygen::HTTPHeaderCode::HTTP_HEADER_CONTENT_TYPE, "application/json");
folly::SocketAddress addr;
addr.setFromHostPort(url.getHost(), url.getPort());
const folly::AsyncSocket::OptionMap opts{
{{SOL_SOCKET, SO_REUSEADDR}, 1}};
if (url.isSecure()) {
auto sslContext_ = std::make_shared<folly::SSLContext>();
sslContext_->setOptions(SSL_OP_NO_COMPRESSION);
sslContext_->setCipherList(folly::ssl::SSLCommonOptions::kCipherList);
_connector.connectSSL(evb, addr, sslContext_, nullptr, std::chrono::milliseconds(1000), opts);
} else {
_connector.connect(evb, addr, std::chrono::milliseconds(1000), opts);
}
start_timer();
}
void JsonClient::onResponse(folly::dynamic &obj) {
finish_timer();
_promise.setValue(obj);
}
void JsonClient::onError(const std::exception &e) {
finish_timer();
_promise.setException(e);
}
JsonClient::~JsonClient() {
VLOG(google::GLOG_INFO) << "destroying JsonClient";
// if (_trx) delete _trx;
}
void JsonClient::CustomHandler::setTransaction(proxygen::HTTPTransaction *txn) noexcept {
}
void JsonClient::CustomHandler::detachTransaction() noexcept {
}
void JsonClient::CustomHandler::onHeadersComplete(std::unique_ptr<proxygen::HTTPMessage> msg) noexcept {
msg->dumpMessage(0);
}
void JsonClient::CustomHandler::onBody(std::unique_ptr<folly::IOBuf> chain) noexcept {
if (_response_body) {
_response_body->prependChain(std::move(chain));
} else {
_response_body = std::move(chain);
}
}
void JsonClient::CustomHandler::onTrailers(std::unique_ptr<proxygen::HTTPHeaders> trailers) noexcept {
}
void JsonClient::CustomHandler::onEOM()noexcept {
auto f = [b = std::move(_response_body)]() {
if (!b) throw std::runtime_error("body empty");
std::string str(reinterpret_cast<const char * >
(b->data()), b->length());
return folly::parseJson(str);
};
folly::Try<folly::dynamic> tryJson = folly::makeTryWith(f);
if (tryJson.hasException()) {
try {
tryJson.throwIfFailed();
} catch (std::exception &e) {
_parent.onError(e);
}
} else if (tryJson.hasValue()) {
_parent.onResponse(tryJson.value());
} else {
_parent.onError(std::runtime_error("failed to get response"));
}
// _req_end = std::clock();
}
void JsonClient::CustomHandler::onUpgrade(proxygen::UpgradeProtocol protocol) noexcept {
}
void JsonClient::CustomHandler::onError(const proxygen::HTTPException &error)noexcept {
}
void JsonClient::CustomHandler::onEgressPaused() noexcept {
}
void JsonClient::CustomHandler::onEgressResumed()noexcept {
}
}