David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 20 | #include <thrift/async/TEvhttpClientChannel.h> |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 21 | #include <evhttp.h> |
Ben Craig | 7207c22 | 2015-07-06 08:40:35 -0500 | [diff] [blame] | 22 | #include <event2/buffer.h> |
| 23 | #include <event2/buffer_compat.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 24 | #include <thrift/transport/TBufferTransports.h> |
| 25 | #include <thrift/protocol/TProtocolException.h> |
Roger Meier | 08077bf | 2011-09-11 07:28:54 +0000 | [diff] [blame] | 26 | |
| 27 | #include <iostream> |
| 28 | #include <sstream> |
| 29 | |
| 30 | using namespace apache::thrift::protocol; |
| 31 | using apache::thrift::transport::TTransportException; |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 32 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 33 | namespace apache { |
| 34 | namespace thrift { |
| 35 | namespace async { |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 36 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 37 | TEvhttpClientChannel::TEvhttpClientChannel(const std::string& host, |
| 38 | const std::string& path, |
| 39 | const char* address, |
| 40 | int port, |
zhouweikang | c4af633 | 2017-12-06 15:24:42 +0800 | [diff] [blame] | 41 | struct event_base* eb, |
| 42 | struct evdns_base* dnsbase) |
| 43 | |
Sebastian Zenker | 39e505c | 2015-12-18 16:15:08 +0100 | [diff] [blame] | 44 | : host_(host), path_(path), conn_(NULL) { |
zhouweikang | c4af633 | 2017-12-06 15:24:42 +0800 | [diff] [blame] | 45 | conn_ = evhttp_connection_base_new(eb, dnsbase, address, port); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 46 | if (conn_ == NULL) { |
Roger Meier | 08077bf | 2011-09-11 07:28:54 +0000 | [diff] [blame] | 47 | throw TException("evhttp_connection_new failed"); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 48 | } |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 49 | } |
| 50 | |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 51 | TEvhttpClientChannel::~TEvhttpClientChannel() { |
| 52 | if (conn_ != NULL) { |
| 53 | evhttp_connection_free(conn_); |
| 54 | } |
| 55 | } |
| 56 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 57 | void TEvhttpClientChannel::sendAndRecvMessage(const VoidCallback& cob, |
| 58 | apache::thrift::transport::TMemoryBuffer* sendBuf, |
| 59 | apache::thrift::transport::TMemoryBuffer* recvBuf) { |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 60 | struct evhttp_request* req = evhttp_request_new(response, this); |
| 61 | if (req == NULL) { |
Roger Meier | 08077bf | 2011-09-11 07:28:54 +0000 | [diff] [blame] | 62 | throw TException("evhttp_request_new failed"); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | int rv; |
| 66 | |
| 67 | rv = evhttp_add_header(req->output_headers, "Host", host_.c_str()); |
| 68 | if (rv != 0) { |
Roger Meier | 08077bf | 2011-09-11 07:28:54 +0000 | [diff] [blame] | 69 | throw TException("evhttp_add_header failed"); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | rv = evhttp_add_header(req->output_headers, "Content-Type", "application/x-thrift"); |
| 73 | if (rv != 0) { |
Roger Meier | 08077bf | 2011-09-11 07:28:54 +0000 | [diff] [blame] | 74 | throw TException("evhttp_add_header failed"); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | uint8_t* obuf; |
| 78 | uint32_t sz; |
| 79 | sendBuf->getBuffer(&obuf, &sz); |
| 80 | rv = evbuffer_add(req->output_buffer, obuf, sz); |
| 81 | if (rv != 0) { |
Roger Meier | 08077bf | 2011-09-11 07:28:54 +0000 | [diff] [blame] | 82 | throw TException("evbuffer_add failed"); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | rv = evhttp_make_request(conn_, req, EVHTTP_REQ_POST, path_.c_str()); |
| 86 | if (rv != 0) { |
Roger Meier | 08077bf | 2011-09-11 07:28:54 +0000 | [diff] [blame] | 87 | throw TException("evhttp_make_request failed"); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 88 | } |
Sebastian Zenker | 39e505c | 2015-12-18 16:15:08 +0100 | [diff] [blame] | 89 | |
| 90 | completionQueue_.push(Completion(cob, recvBuf)); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 93 | void TEvhttpClientChannel::sendMessage(const VoidCallback& cob, |
| 94 | apache::thrift::transport::TMemoryBuffer* message) { |
| 95 | (void)cob; |
| 96 | (void)message; |
Roger Meier | 08077bf | 2011-09-11 07:28:54 +0000 | [diff] [blame] | 97 | throw TProtocolException(TProtocolException::NOT_IMPLEMENTED, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 98 | "Unexpected call to TEvhttpClientChannel::sendMessage"); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 101 | void TEvhttpClientChannel::recvMessage(const VoidCallback& cob, |
| 102 | apache::thrift::transport::TMemoryBuffer* message) { |
| 103 | (void)cob; |
| 104 | (void)message; |
Roger Meier | 08077bf | 2011-09-11 07:28:54 +0000 | [diff] [blame] | 105 | throw TProtocolException(TProtocolException::NOT_IMPLEMENTED, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 106 | "Unexpected call to TEvhttpClientChannel::recvMessage"); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 107 | } |
| 108 | |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 109 | void TEvhttpClientChannel::finish(struct evhttp_request* req) { |
Sebastian Zenker | 39e505c | 2015-12-18 16:15:08 +0100 | [diff] [blame] | 110 | assert(!completionQueue_.empty()); |
| 111 | Completion completion = completionQueue_.front(); |
| 112 | completionQueue_.pop(); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 113 | if (req == NULL) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 114 | try { |
Sebastian Zenker | 39e505c | 2015-12-18 16:15:08 +0100 | [diff] [blame] | 115 | completion.first(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 116 | } catch (const TTransportException& e) { |
| 117 | if (e.getType() == TTransportException::END_OF_FILE) |
| 118 | throw TException("connect failed"); |
| 119 | else |
| 120 | throw; |
| 121 | } |
| 122 | return; |
Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame] | 123 | } else if (req->response_code != 200) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 124 | try { |
Sebastian Zenker | 39e505c | 2015-12-18 16:15:08 +0100 | [diff] [blame] | 125 | completion.first(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 126 | } catch (const TTransportException& e) { |
| 127 | std::stringstream ss; |
| 128 | ss << "server returned code " << req->response_code; |
| 129 | if (req->response_code_line) |
| 130 | ss << ": " << req->response_code_line; |
| 131 | if (e.getType() == TTransportException::END_OF_FILE) |
| 132 | throw TException(ss.str()); |
| 133 | else |
| 134 | throw; |
| 135 | } |
| 136 | return; |
Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame] | 137 | } |
Sebastian Zenker | 39e505c | 2015-12-18 16:15:08 +0100 | [diff] [blame] | 138 | completion.second->resetBuffer(EVBUFFER_DATA(req->input_buffer), |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 139 | static_cast<uint32_t>(EVBUFFER_LENGTH(req->input_buffer))); |
Sebastian Zenker | 39e505c | 2015-12-18 16:15:08 +0100 | [diff] [blame] | 140 | completion.first(); |
Roger Meier | 285cfaa | 2011-07-28 17:51:36 +0000 | [diff] [blame] | 141 | return; |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 142 | } |
| 143 | |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 144 | /* static */ void TEvhttpClientChannel::response(struct evhttp_request* req, void* arg) { |
| 145 | TEvhttpClientChannel* self = (TEvhttpClientChannel*)arg; |
Roger Meier | 08077bf | 2011-09-11 07:28:54 +0000 | [diff] [blame] | 146 | try { |
| 147 | self->finish(req); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 148 | } catch (std::exception& e) { |
Roger Meier | 08077bf | 2011-09-11 07:28:54 +0000 | [diff] [blame] | 149 | // don't propagate a C++ exception in C code (e.g. libevent) |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 150 | std::cerr << "TEvhttpClientChannel::response exception thrown (ignored): " << e.what() |
| 151 | << std::endl; |
Roger Meier | 08077bf | 2011-09-11 07:28:54 +0000 | [diff] [blame] | 152 | } |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 153 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } // apache::thrift::async |