blob: 99986b92c25df8ba1bb6c5c2d28913eaf89e9abb [file] [log] [blame]
Dave Watson792db4e2015-01-16 11:22:01 -08001/*
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
cyy316723a2019-01-05 16:35:14 +080028#include <memory>
Konrad Grochowski517aa142015-11-12 16:28:12 +010029
Dave Watson792db4e2015-01-16 11:22:01 -080030using apache::thrift::transport::THeaderTransport;
31
32namespace apache {
33namespace thrift {
34namespace protocol {
35
36/**
37 * The header protocol for thrift. Reads unframed, framed, header format,
38 * and http
39 *
40 */
41class THeaderProtocol : public TVirtualProtocol<THeaderProtocol> {
42protected:
43public:
44 void resetProtocol();
45
cyy316723a2019-01-05 16:35:14 +080046 explicit THeaderProtocol(const std::shared_ptr<TTransport>& trans,
Dave Watson792db4e2015-01-16 11:22:01 -080047 uint16_t protoId = T_COMPACT_PROTOCOL)
cyy316723a2019-01-05 16:35:14 +080048 : TVirtualProtocol<THeaderProtocol>(std::shared_ptr<TTransport>(new THeaderTransport(trans))),
49 trans_(std::dynamic_pointer_cast<THeaderTransport>(getTransport())),
Dave Watson792db4e2015-01-16 11:22:01 -080050 protoId_(protoId) {
51 trans_->setProtocolId(protoId);
52 resetProtocol();
53 }
54
cyy316723a2019-01-05 16:35:14 +080055 THeaderProtocol(const std::shared_ptr<TTransport>& inTrans,
56 const std::shared_ptr<TTransport>& outTrans,
Dave Watson792db4e2015-01-16 11:22:01 -080057 uint16_t protoId = T_COMPACT_PROTOCOL)
58 : TVirtualProtocol<THeaderProtocol>(
cyy316723a2019-01-05 16:35:14 +080059 std::shared_ptr<TTransport>(new THeaderTransport(inTrans, outTrans))),
60 trans_(std::dynamic_pointer_cast<THeaderTransport>(getTransport())),
Dave Watson792db4e2015-01-16 11:22:01 -080061 protoId_(protoId) {
62 trans_->setProtocolId(protoId);
63 resetProtocol();
64 }
65
Sebastian Zenker042580f2019-01-29 15:48:12 +010066 ~THeaderProtocol() override = default;
Dave Watson792db4e2015-01-16 11:22:01 -080067
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
Carel Combrinkfbe685a2025-06-05 08:38:07 +0200138 uint32_t writeUUID(const TUuid& uuid);
139
Dave Watson792db4e2015-01-16 11:22:01 -0800140 /**
141 * Reading functions
142 */
143
144 /*ol*/ uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqId);
145
146 /*ol*/ uint32_t readMessageEnd();
147
148 uint32_t readStructBegin(std::string& name);
149
150 uint32_t readStructEnd();
151
152 uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId);
153
154 uint32_t readFieldEnd();
155
156 uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size);
157
158 uint32_t readMapEnd();
159
160 uint32_t readListBegin(TType& elemType, uint32_t& size);
161
162 uint32_t readListEnd();
163
164 uint32_t readSetBegin(TType& elemType, uint32_t& size);
165
166 uint32_t readSetEnd();
167
168 uint32_t readBool(bool& value);
169 // Provide the default readBool() implementation for std::vector<bool>
170 using TVirtualProtocol<THeaderProtocol>::readBool;
171
172 uint32_t readByte(int8_t& byte);
173
174 uint32_t readI16(int16_t& i16);
175
176 uint32_t readI32(int32_t& i32);
177
178 uint32_t readI64(int64_t& i64);
179
180 uint32_t readDouble(double& dub);
181
182 uint32_t readString(std::string& str);
183
184 uint32_t readBinary(std::string& binary);
185
Carel Combrinkfbe685a2025-06-05 08:38:07 +0200186 uint32_t readUUID(TUuid& uuid);
187
Dave Watson792db4e2015-01-16 11:22:01 -0800188protected:
cyy316723a2019-01-05 16:35:14 +0800189 std::shared_ptr<THeaderTransport> trans_;
Dave Watson792db4e2015-01-16 11:22:01 -0800190
cyy316723a2019-01-05 16:35:14 +0800191 std::shared_ptr<TProtocol> proto_;
Dave Watson792db4e2015-01-16 11:22:01 -0800192 uint32_t protoId_;
193};
194
195class THeaderProtocolFactory : public TProtocolFactory {
196public:
Sebastian Zenker042580f2019-01-29 15:48:12 +0100197 std::shared_ptr<TProtocol> getProtocol(std::shared_ptr<transport::TTransport> trans) override {
198 auto* headerProtocol
James E. King, III58402ff2017-11-17 14:41:46 -0500199 = new THeaderProtocol(trans, trans, T_BINARY_PROTOCOL);
cyy316723a2019-01-05 16:35:14 +0800200 return std::shared_ptr<TProtocol>(headerProtocol);
Dave Watson792db4e2015-01-16 11:22:01 -0800201 }
202
Sebastian Zenker042580f2019-01-29 15:48:12 +0100203 std::shared_ptr<TProtocol> getProtocol(
cyy316723a2019-01-05 16:35:14 +0800204 std::shared_ptr<transport::TTransport> inTrans,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100205 std::shared_ptr<transport::TTransport> outTrans) override {
206 auto* headerProtocol = new THeaderProtocol(inTrans, outTrans, T_BINARY_PROTOCOL);
cyy316723a2019-01-05 16:35:14 +0800207 return std::shared_ptr<TProtocol>(headerProtocol);
Dave Watson792db4e2015-01-16 11:22:01 -0800208 }
209};
210}
211}
212} // apache::thrift::protocol
213
214#endif // #ifndef THRIFT_PROTOCOL_THEADERPROTOCOL_H_