| Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [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 | |
| 20 | #ifndef THRIFT_PROTOCOL_THEADERPROTOCOL_H_ |
| 21 | #define THRIFT_PROTOCOL_THEADERPROTOCOL_H_ 1 |
| 22 | |
| 23 | #include <thrift/protocol/TProtocol.h> |
| 24 | #include <thrift/protocol/TProtocolTypes.h> |
| 25 | #include <thrift/protocol/TVirtualProtocol.h> |
| 26 | #include <thrift/transport/THeaderTransport.h> |
| 27 | |
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 28 | #include <memory> |
| Konrad Grochowski | 517aa14 | 2015-11-12 16:28:12 +0100 | [diff] [blame] | 29 | |
| Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 30 | using apache::thrift::transport::THeaderTransport; |
| 31 | |
| 32 | namespace apache { |
| 33 | namespace thrift { |
| 34 | namespace protocol { |
| 35 | |
| 36 | /** |
| 37 | * The header protocol for thrift. Reads unframed, framed, header format, |
| 38 | * and http |
| 39 | * |
| 40 | */ |
| 41 | class THeaderProtocol : public TVirtualProtocol<THeaderProtocol> { |
| 42 | protected: |
| 43 | public: |
| 44 | void resetProtocol(); |
| 45 | |
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 46 | explicit THeaderProtocol(const std::shared_ptr<TTransport>& trans, |
| Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 47 | uint16_t protoId = T_COMPACT_PROTOCOL) |
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 48 | : TVirtualProtocol<THeaderProtocol>(std::shared_ptr<TTransport>(new THeaderTransport(trans))), |
| 49 | trans_(std::dynamic_pointer_cast<THeaderTransport>(getTransport())), |
| Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 50 | protoId_(protoId) { |
| 51 | trans_->setProtocolId(protoId); |
| 52 | resetProtocol(); |
| 53 | } |
| 54 | |
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 55 | THeaderProtocol(const std::shared_ptr<TTransport>& inTrans, |
| 56 | const std::shared_ptr<TTransport>& outTrans, |
| Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 57 | uint16_t protoId = T_COMPACT_PROTOCOL) |
| 58 | : TVirtualProtocol<THeaderProtocol>( |
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 59 | std::shared_ptr<TTransport>(new THeaderTransport(inTrans, outTrans))), |
| 60 | trans_(std::dynamic_pointer_cast<THeaderTransport>(getTransport())), |
| Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 61 | protoId_(protoId) { |
| 62 | trans_->setProtocolId(protoId); |
| 63 | resetProtocol(); |
| 64 | } |
| 65 | |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame^] | 66 | ~THeaderProtocol() override = default; |
| Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Functions to work with headers by calling into THeaderTransport |
| 70 | */ |
| 71 | void setProtocolId(uint16_t protoId) { |
| 72 | trans_->setProtocolId(protoId); |
| 73 | resetProtocol(); |
| 74 | } |
| 75 | |
| 76 | typedef THeaderTransport::StringToStringMap StringToStringMap; |
| 77 | |
| 78 | // these work with write headers |
| 79 | void setHeader(const std::string& key, const std::string& value) { |
| 80 | trans_->setHeader(key, value); |
| 81 | } |
| 82 | |
| 83 | void clearHeaders() { trans_->clearHeaders(); } |
| 84 | |
| 85 | StringToStringMap& getWriteHeaders() { return trans_->getWriteHeaders(); } |
| 86 | |
| 87 | // these work with read headers |
| 88 | const StringToStringMap& getHeaders() const { return trans_->getHeaders(); } |
| 89 | |
| 90 | /** |
| 91 | * Writing functions. |
| 92 | */ |
| 93 | |
| 94 | /*ol*/ uint32_t writeMessageBegin(const std::string& name, |
| 95 | const TMessageType messageType, |
| 96 | const int32_t seqId); |
| 97 | |
| 98 | /*ol*/ uint32_t writeMessageEnd(); |
| 99 | |
| 100 | uint32_t writeStructBegin(const char* name); |
| 101 | |
| 102 | uint32_t writeStructEnd(); |
| 103 | |
| 104 | uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId); |
| 105 | |
| 106 | uint32_t writeFieldEnd(); |
| 107 | |
| 108 | uint32_t writeFieldStop(); |
| 109 | |
| 110 | uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size); |
| 111 | |
| 112 | uint32_t writeMapEnd(); |
| 113 | |
| 114 | uint32_t writeListBegin(const TType elemType, const uint32_t size); |
| 115 | |
| 116 | uint32_t writeListEnd(); |
| 117 | |
| 118 | uint32_t writeSetBegin(const TType elemType, const uint32_t size); |
| 119 | |
| 120 | uint32_t writeSetEnd(); |
| 121 | |
| 122 | uint32_t writeBool(const bool value); |
| 123 | |
| 124 | uint32_t writeByte(const int8_t byte); |
| 125 | |
| 126 | uint32_t writeI16(const int16_t i16); |
| 127 | |
| 128 | uint32_t writeI32(const int32_t i32); |
| 129 | |
| 130 | uint32_t writeI64(const int64_t i64); |
| 131 | |
| 132 | uint32_t writeDouble(const double dub); |
| 133 | |
| 134 | uint32_t writeString(const std::string& str); |
| 135 | |
| 136 | uint32_t writeBinary(const std::string& str); |
| 137 | |
| 138 | /** |
| 139 | * Reading functions |
| 140 | */ |
| 141 | |
| 142 | /*ol*/ uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqId); |
| 143 | |
| 144 | /*ol*/ uint32_t readMessageEnd(); |
| 145 | |
| 146 | uint32_t readStructBegin(std::string& name); |
| 147 | |
| 148 | uint32_t readStructEnd(); |
| 149 | |
| 150 | uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId); |
| 151 | |
| 152 | uint32_t readFieldEnd(); |
| 153 | |
| 154 | uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size); |
| 155 | |
| 156 | uint32_t readMapEnd(); |
| 157 | |
| 158 | uint32_t readListBegin(TType& elemType, uint32_t& size); |
| 159 | |
| 160 | uint32_t readListEnd(); |
| 161 | |
| 162 | uint32_t readSetBegin(TType& elemType, uint32_t& size); |
| 163 | |
| 164 | uint32_t readSetEnd(); |
| 165 | |
| 166 | uint32_t readBool(bool& value); |
| 167 | // Provide the default readBool() implementation for std::vector<bool> |
| 168 | using TVirtualProtocol<THeaderProtocol>::readBool; |
| 169 | |
| 170 | uint32_t readByte(int8_t& byte); |
| 171 | |
| 172 | uint32_t readI16(int16_t& i16); |
| 173 | |
| 174 | uint32_t readI32(int32_t& i32); |
| 175 | |
| 176 | uint32_t readI64(int64_t& i64); |
| 177 | |
| 178 | uint32_t readDouble(double& dub); |
| 179 | |
| 180 | uint32_t readString(std::string& str); |
| 181 | |
| 182 | uint32_t readBinary(std::string& binary); |
| 183 | |
| 184 | protected: |
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 185 | std::shared_ptr<THeaderTransport> trans_; |
| Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 186 | |
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 187 | std::shared_ptr<TProtocol> proto_; |
| Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 188 | uint32_t protoId_; |
| 189 | }; |
| 190 | |
| 191 | class THeaderProtocolFactory : public TProtocolFactory { |
| 192 | public: |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame^] | 193 | std::shared_ptr<TProtocol> getProtocol(std::shared_ptr<transport::TTransport> trans) override { |
| 194 | auto* headerProtocol |
| James E. King, III | 58402ff | 2017-11-17 14:41:46 -0500 | [diff] [blame] | 195 | = new THeaderProtocol(trans, trans, T_BINARY_PROTOCOL); |
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 196 | return std::shared_ptr<TProtocol>(headerProtocol); |
| Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame^] | 199 | std::shared_ptr<TProtocol> getProtocol( |
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 200 | std::shared_ptr<transport::TTransport> inTrans, |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame^] | 201 | std::shared_ptr<transport::TTransport> outTrans) override { |
| 202 | auto* headerProtocol = new THeaderProtocol(inTrans, outTrans, T_BINARY_PROTOCOL); |
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 203 | return std::shared_ptr<TProtocol>(headerProtocol); |
| Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 204 | } |
| 205 | }; |
| 206 | } |
| 207 | } |
| 208 | } // apache::thrift::protocol |
| 209 | |
| 210 | #endif // #ifndef THRIFT_PROTOCOL_THEADERPROTOCOL_H_ |