/src/thrift/lib/rs/src/protocol/stored.rs
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | use std::convert::Into; |
19 | | |
20 | | use super::{ |
21 | | TFieldIdentifier, TInputProtocol, TListIdentifier, TMapIdentifier, TMessageIdentifier, |
22 | | TSetIdentifier, TStructIdentifier, |
23 | | }; |
24 | | use crate::ProtocolErrorKind; |
25 | | |
26 | | /// `TInputProtocol` required to use a `TMultiplexedProcessor`. |
27 | | /// |
28 | | /// A `TMultiplexedProcessor` reads incoming message identifiers to determine to |
29 | | /// which `TProcessor` requests should be forwarded. However, once read, those |
30 | | /// message identifier bytes are no longer on the wire. Since downstream |
31 | | /// processors expect to read message identifiers from the given input protocol |
32 | | /// we need some way of supplying a `TMessageIdentifier` with the service-name |
33 | | /// stripped. This implementation stores the received `TMessageIdentifier` |
34 | | /// (without the service name) and passes it to the wrapped `TInputProtocol` |
35 | | /// when `TInputProtocol::read_message_begin(...)` is called. It delegates all |
36 | | /// other calls directly to the wrapped `TInputProtocol`. |
37 | | /// |
38 | | /// This type **should not** be used by application code. |
39 | | /// |
40 | | /// # Examples |
41 | | /// |
42 | | /// Create and use a `TStoredInputProtocol`. |
43 | | /// |
44 | | /// ```no_run |
45 | | /// use thrift::protocol::{TInputProtocol, TMessageIdentifier, TMessageType, TOutputProtocol}; |
46 | | /// use thrift::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol, TStoredInputProtocol}; |
47 | | /// use thrift::server::TProcessor; |
48 | | /// use thrift::transport::{TIoChannel, TTcpChannel}; |
49 | | /// |
50 | | /// // sample processor |
51 | | /// struct ActualProcessor; |
52 | | /// impl TProcessor for ActualProcessor { |
53 | | /// fn process( |
54 | | /// &self, |
55 | | /// _: &mut dyn TInputProtocol, |
56 | | /// _: &mut dyn TOutputProtocol |
57 | | /// ) -> thrift::Result<()> { |
58 | | /// unimplemented!() |
59 | | /// } |
60 | | /// } |
61 | | /// let processor = ActualProcessor {}; |
62 | | /// |
63 | | /// // construct the shared transport |
64 | | /// let mut channel = TTcpChannel::new(); |
65 | | /// channel.open("localhost:9090").unwrap(); |
66 | | /// |
67 | | /// let (i_chan, o_chan) = channel.split().unwrap(); |
68 | | /// |
69 | | /// // construct the actual input and output protocols |
70 | | /// let mut i_prot = TBinaryInputProtocol::new(i_chan, true); |
71 | | /// let mut o_prot = TBinaryOutputProtocol::new(o_chan, true); |
72 | | /// |
73 | | /// // message identifier received from remote and modified to remove the service name |
74 | | /// let new_msg_ident = TMessageIdentifier::new("service_call", TMessageType::Call, 1); |
75 | | /// |
76 | | /// // construct the proxy input protocol |
77 | | /// let mut proxy_i_prot = TStoredInputProtocol::new(&mut i_prot, new_msg_ident); |
78 | | /// let res = processor.process(&mut proxy_i_prot, &mut o_prot); |
79 | | /// ``` |
80 | | // FIXME: implement Debug |
81 | | pub struct TStoredInputProtocol<'a> { |
82 | | inner: &'a mut dyn TInputProtocol, |
83 | | message_ident: Option<TMessageIdentifier>, |
84 | | } |
85 | | |
86 | | // Erroneous suggestion by clippy |
87 | | #[allow(clippy::needless_lifetimes)] |
88 | | impl<'a> TStoredInputProtocol<'a> { |
89 | | /// Create a `TStoredInputProtocol` that delegates all calls other than |
90 | | /// `TInputProtocol::read_message_begin(...)` to a `wrapped` |
91 | | /// `TInputProtocol`. `message_ident` is the modified message identifier - |
92 | | /// with service name stripped - that will be passed to |
93 | | /// `wrapped.read_message_begin(...)`. |
94 | 0 | pub fn new( |
95 | 0 | wrapped: &mut dyn TInputProtocol, |
96 | 0 | message_ident: TMessageIdentifier, |
97 | 0 | ) -> TStoredInputProtocol<'_> { |
98 | 0 | TStoredInputProtocol { |
99 | 0 | inner: wrapped, |
100 | 0 | message_ident: message_ident.into(), |
101 | 0 | } |
102 | 0 | } |
103 | | } |
104 | | |
105 | | // Erroneous suggestion by clippy |
106 | | #[allow(clippy::needless_lifetimes)] |
107 | | impl<'a> TInputProtocol for TStoredInputProtocol<'a> { |
108 | 0 | fn read_message_begin(&mut self) -> crate::Result<TMessageIdentifier> { |
109 | 0 | self.message_ident.take().ok_or_else(|| { |
110 | 0 | crate::errors::new_protocol_error( |
111 | 0 | ProtocolErrorKind::Unknown, |
112 | | "message identifier already read", |
113 | | ) |
114 | 0 | }) |
115 | 0 | } |
116 | | |
117 | 0 | fn read_message_end(&mut self) -> crate::Result<()> { |
118 | 0 | self.inner.read_message_end() |
119 | 0 | } |
120 | | |
121 | 0 | fn read_struct_begin(&mut self) -> crate::Result<Option<TStructIdentifier>> { |
122 | 0 | self.inner.read_struct_begin() |
123 | 0 | } |
124 | | |
125 | 0 | fn read_struct_end(&mut self) -> crate::Result<()> { |
126 | 0 | self.inner.read_struct_end() |
127 | 0 | } |
128 | | |
129 | 0 | fn read_field_begin(&mut self) -> crate::Result<TFieldIdentifier> { |
130 | 0 | self.inner.read_field_begin() |
131 | 0 | } |
132 | | |
133 | 0 | fn read_field_end(&mut self) -> crate::Result<()> { |
134 | 0 | self.inner.read_field_end() |
135 | 0 | } |
136 | | |
137 | 0 | fn read_bytes(&mut self) -> crate::Result<Vec<u8>> { |
138 | 0 | self.inner.read_bytes() |
139 | 0 | } |
140 | | |
141 | 0 | fn skip_binary(&mut self) -> crate::Result<()> { |
142 | 0 | self.inner.skip_binary() |
143 | 0 | } |
144 | | |
145 | 0 | fn read_bool(&mut self) -> crate::Result<bool> { |
146 | 0 | self.inner.read_bool() |
147 | 0 | } |
148 | | |
149 | 0 | fn read_i8(&mut self) -> crate::Result<i8> { |
150 | 0 | self.inner.read_i8() |
151 | 0 | } |
152 | | |
153 | 0 | fn read_i16(&mut self) -> crate::Result<i16> { |
154 | 0 | self.inner.read_i16() |
155 | 0 | } |
156 | | |
157 | 0 | fn read_i32(&mut self) -> crate::Result<i32> { |
158 | 0 | self.inner.read_i32() |
159 | 0 | } |
160 | | |
161 | 0 | fn read_i64(&mut self) -> crate::Result<i64> { |
162 | 0 | self.inner.read_i64() |
163 | 0 | } |
164 | | |
165 | 0 | fn read_double(&mut self) -> crate::Result<f64> { |
166 | 0 | self.inner.read_double() |
167 | 0 | } |
168 | | |
169 | 0 | fn read_uuid(&mut self) -> crate::Result<uuid::Uuid> { |
170 | 0 | self.inner.read_uuid() |
171 | 0 | } |
172 | | |
173 | 0 | fn read_string(&mut self) -> crate::Result<String> { |
174 | 0 | self.inner.read_string() |
175 | 0 | } |
176 | | |
177 | 0 | fn read_list_begin(&mut self) -> crate::Result<TListIdentifier> { |
178 | 0 | self.inner.read_list_begin() |
179 | 0 | } |
180 | | |
181 | 0 | fn read_list_end(&mut self) -> crate::Result<()> { |
182 | 0 | self.inner.read_list_end() |
183 | 0 | } |
184 | | |
185 | 0 | fn read_set_begin(&mut self) -> crate::Result<TSetIdentifier> { |
186 | 0 | self.inner.read_set_begin() |
187 | 0 | } |
188 | | |
189 | 0 | fn read_set_end(&mut self) -> crate::Result<()> { |
190 | 0 | self.inner.read_set_end() |
191 | 0 | } |
192 | | |
193 | 0 | fn read_map_begin(&mut self) -> crate::Result<TMapIdentifier> { |
194 | 0 | self.inner.read_map_begin() |
195 | 0 | } |
196 | | |
197 | 0 | fn read_map_end(&mut self) -> crate::Result<()> { |
198 | 0 | self.inner.read_map_end() |
199 | 0 | } |
200 | | |
201 | | // utility |
202 | | // |
203 | | |
204 | 0 | fn read_byte(&mut self) -> crate::Result<u8> { |
205 | 0 | self.inner.read_byte() |
206 | 0 | } |
207 | | } |