gorealis v2 refactor (#5)

* Changing default timeout for start maintenance.

* Upgrading dependencies to gorealis v2 and thrift  0.12.0

* Refactored to update to gorealis v2.
This commit is contained in:
Renan DelValle 2018-12-27 11:31:51 -08:00 committed by GitHub
parent ad4dd9606e
commit 6ab5c9334d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1335 changed files with 137431 additions and 61530 deletions

View file

@ -0,0 +1,957 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use byteorder::{BigEndian, ByteOrder, ReadBytesExt, WriteBytesExt};
use std::convert::From;
use try_from::TryFrom;
use super::{
TFieldIdentifier, TInputProtocol, TInputProtocolFactory, TListIdentifier, TMapIdentifier,
TMessageIdentifier, TMessageType,
};
use super::{TOutputProtocol, TOutputProtocolFactory, TSetIdentifier, TStructIdentifier, TType};
use transport::{TReadTransport, TWriteTransport};
use {ProtocolError, ProtocolErrorKind};
const BINARY_PROTOCOL_VERSION_1: u32 = 0x80010000;
/// Read messages encoded in the Thrift simple binary encoding.
///
/// There are two available modes: `strict` and `non-strict`, where the
/// `non-strict` version does not check for the protocol version in the
/// received message header.
///
/// # Examples
///
/// Create and use a `TBinaryInputProtocol`.
///
/// ```no_run
/// use thrift::protocol::{TBinaryInputProtocol, TInputProtocol};
/// use thrift::transport::TTcpChannel;
///
/// let mut channel = TTcpChannel::new();
/// channel.open("localhost:9090").unwrap();
///
/// let mut protocol = TBinaryInputProtocol::new(channel, true);
///
/// let recvd_bool = protocol.read_bool().unwrap();
/// let recvd_string = protocol.read_string().unwrap();
/// ```
#[derive(Debug)]
pub struct TBinaryInputProtocol<T>
where
T: TReadTransport,
{
strict: bool,
pub transport: T, // FIXME: shouldn't be public
}
impl<'a, T> TBinaryInputProtocol<T>
where
T: TReadTransport,
{
/// Create a `TBinaryInputProtocol` that reads bytes from `transport`.
///
/// Set `strict` to `true` if all incoming messages contain the protocol
/// version number in the protocol header.
pub fn new(transport: T, strict: bool) -> TBinaryInputProtocol<T> {
TBinaryInputProtocol {
strict: strict,
transport: transport,
}
}
}
impl<T> TInputProtocol for TBinaryInputProtocol<T>
where
T: TReadTransport,
{
#[cfg_attr(feature = "cargo-clippy", allow(collapsible_if))]
fn read_message_begin(&mut self) -> ::Result<TMessageIdentifier> {
let mut first_bytes = vec![0; 4];
self.transport.read_exact(&mut first_bytes[..])?;
// the thrift version header is intentionally negative
// so the first check we'll do is see if the sign bit is set
// and if so - assume it's the protocol-version header
if first_bytes[0] >= 8 {
// apparently we got a protocol-version header - check
// it, and if it matches, read the rest of the fields
if first_bytes[0..2] != [0x80, 0x01] {
Err(::Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::BadVersion,
message: format!("received bad version: {:?}", &first_bytes[0..2]),
}))
} else {
let message_type: TMessageType = TryFrom::try_from(first_bytes[3])?;
let name = self.read_string()?;
let sequence_number = self.read_i32()?;
Ok(TMessageIdentifier::new(name, message_type, sequence_number))
}
} else {
// apparently we didn't get a protocol-version header,
// which happens if the sender is not using the strict protocol
if self.strict {
// we're in strict mode however, and that always
// requires the protocol-version header to be written first
Err(::Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::BadVersion,
message: format!("received bad version: {:?}", &first_bytes[0..2]),
}))
} else {
// in the non-strict version the first message field
// is the message name. strings (byte arrays) are length-prefixed,
// so we've just read the length in the first 4 bytes
let name_size = BigEndian::read_i32(&first_bytes) as usize;
let mut name_buf: Vec<u8> = vec![0; name_size];
self.transport.read_exact(&mut name_buf)?;
let name = String::from_utf8(name_buf)?;
// read the rest of the fields
let message_type: TMessageType = self.read_byte().and_then(TryFrom::try_from)?;
let sequence_number = self.read_i32()?;
Ok(TMessageIdentifier::new(name, message_type, sequence_number))
}
}
}
fn read_message_end(&mut self) -> ::Result<()> {
Ok(())
}
fn read_struct_begin(&mut self) -> ::Result<Option<TStructIdentifier>> {
Ok(None)
}
fn read_struct_end(&mut self) -> ::Result<()> {
Ok(())
}
fn read_field_begin(&mut self) -> ::Result<TFieldIdentifier> {
let field_type_byte = self.read_byte()?;
let field_type = field_type_from_u8(field_type_byte)?;
let id = match field_type {
TType::Stop => Ok(0),
_ => self.read_i16(),
}?;
Ok(TFieldIdentifier::new::<Option<String>, String, i16>(
None, field_type, id,
))
}
fn read_field_end(&mut self) -> ::Result<()> {
Ok(())
}
fn read_bytes(&mut self) -> ::Result<Vec<u8>> {
let num_bytes = self.transport.read_i32::<BigEndian>()? as usize;
let mut buf = vec![0u8; num_bytes];
self.transport
.read_exact(&mut buf)
.map(|_| buf)
.map_err(From::from)
}
fn read_bool(&mut self) -> ::Result<bool> {
let b = self.read_i8()?;
match b {
0 => Ok(false),
_ => Ok(true),
}
}
fn read_i8(&mut self) -> ::Result<i8> {
self.transport.read_i8().map_err(From::from)
}
fn read_i16(&mut self) -> ::Result<i16> {
self.transport.read_i16::<BigEndian>().map_err(From::from)
}
fn read_i32(&mut self) -> ::Result<i32> {
self.transport.read_i32::<BigEndian>().map_err(From::from)
}
fn read_i64(&mut self) -> ::Result<i64> {
self.transport.read_i64::<BigEndian>().map_err(From::from)
}
fn read_double(&mut self) -> ::Result<f64> {
self.transport.read_f64::<BigEndian>().map_err(From::from)
}
fn read_string(&mut self) -> ::Result<String> {
let bytes = self.read_bytes()?;
String::from_utf8(bytes).map_err(From::from)
}
fn read_list_begin(&mut self) -> ::Result<TListIdentifier> {
let element_type: TType = self.read_byte().and_then(field_type_from_u8)?;
let size = self.read_i32()?;
Ok(TListIdentifier::new(element_type, size))
}
fn read_list_end(&mut self) -> ::Result<()> {
Ok(())
}
fn read_set_begin(&mut self) -> ::Result<TSetIdentifier> {
let element_type: TType = self.read_byte().and_then(field_type_from_u8)?;
let size = self.read_i32()?;
Ok(TSetIdentifier::new(element_type, size))
}
fn read_set_end(&mut self) -> ::Result<()> {
Ok(())
}
fn read_map_begin(&mut self) -> ::Result<TMapIdentifier> {
let key_type: TType = self.read_byte().and_then(field_type_from_u8)?;
let value_type: TType = self.read_byte().and_then(field_type_from_u8)?;
let size = self.read_i32()?;
Ok(TMapIdentifier::new(key_type, value_type, size))
}
fn read_map_end(&mut self) -> ::Result<()> {
Ok(())
}
// utility
//
fn read_byte(&mut self) -> ::Result<u8> {
self.transport.read_u8().map_err(From::from)
}
}
/// Factory for creating instances of `TBinaryInputProtocol`.
#[derive(Default)]
pub struct TBinaryInputProtocolFactory;
impl TBinaryInputProtocolFactory {
/// Create a `TBinaryInputProtocolFactory`.
pub fn new() -> TBinaryInputProtocolFactory {
TBinaryInputProtocolFactory {}
}
}
impl TInputProtocolFactory for TBinaryInputProtocolFactory {
fn create(&self, transport: Box<TReadTransport + Send>) -> Box<TInputProtocol + Send> {
Box::new(TBinaryInputProtocol::new(transport, true))
}
}
/// Write messages using the Thrift simple binary encoding.
///
/// There are two available modes: `strict` and `non-strict`, where the
/// `strict` version writes the protocol version number in the outgoing message
/// header and the `non-strict` version does not.
///
/// # Examples
///
/// Create and use a `TBinaryOutputProtocol`.
///
/// ```no_run
/// use thrift::protocol::{TBinaryOutputProtocol, TOutputProtocol};
/// use thrift::transport::TTcpChannel;
///
/// let mut channel = TTcpChannel::new();
/// channel.open("localhost:9090").unwrap();
///
/// let mut protocol = TBinaryOutputProtocol::new(channel, true);
///
/// protocol.write_bool(true).unwrap();
/// protocol.write_string("test_string").unwrap();
/// ```
#[derive(Debug)]
pub struct TBinaryOutputProtocol<T>
where
T: TWriteTransport,
{
strict: bool,
pub transport: T, // FIXME: do not make public; only public for testing!
}
impl<T> TBinaryOutputProtocol<T>
where
T: TWriteTransport,
{
/// Create a `TBinaryOutputProtocol` that writes bytes to `transport`.
///
/// Set `strict` to `true` if all outgoing messages should contain the
/// protocol version number in the protocol header.
pub fn new(transport: T, strict: bool) -> TBinaryOutputProtocol<T> {
TBinaryOutputProtocol {
strict: strict,
transport: transport,
}
}
}
impl<T> TOutputProtocol for TBinaryOutputProtocol<T>
where
T: TWriteTransport,
{
fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> ::Result<()> {
if self.strict {
let message_type: u8 = identifier.message_type.into();
let header = BINARY_PROTOCOL_VERSION_1 | (message_type as u32);
self.transport.write_u32::<BigEndian>(header)?;
self.write_string(&identifier.name)?;
self.write_i32(identifier.sequence_number)
} else {
self.write_string(&identifier.name)?;
self.write_byte(identifier.message_type.into())?;
self.write_i32(identifier.sequence_number)
}
}
fn write_message_end(&mut self) -> ::Result<()> {
Ok(())
}
fn write_struct_begin(&mut self, _: &TStructIdentifier) -> ::Result<()> {
Ok(())
}
fn write_struct_end(&mut self) -> ::Result<()> {
Ok(())
}
fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> ::Result<()> {
if identifier.id.is_none() && identifier.field_type != TType::Stop {
return Err(::Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::Unknown,
message: format!(
"cannot write identifier {:?} without sequence number",
&identifier
),
}));
}
self.write_byte(field_type_to_u8(identifier.field_type))?;
if let Some(id) = identifier.id {
self.write_i16(id)
} else {
Ok(())
}
}
fn write_field_end(&mut self) -> ::Result<()> {
Ok(())
}
fn write_field_stop(&mut self) -> ::Result<()> {
self.write_byte(field_type_to_u8(TType::Stop))
}
fn write_bytes(&mut self, b: &[u8]) -> ::Result<()> {
self.write_i32(b.len() as i32)?;
self.transport.write_all(b).map_err(From::from)
}
fn write_bool(&mut self, b: bool) -> ::Result<()> {
if b {
self.write_i8(1)
} else {
self.write_i8(0)
}
}
fn write_i8(&mut self, i: i8) -> ::Result<()> {
self.transport.write_i8(i).map_err(From::from)
}
fn write_i16(&mut self, i: i16) -> ::Result<()> {
self.transport.write_i16::<BigEndian>(i).map_err(From::from)
}
fn write_i32(&mut self, i: i32) -> ::Result<()> {
self.transport.write_i32::<BigEndian>(i).map_err(From::from)
}
fn write_i64(&mut self, i: i64) -> ::Result<()> {
self.transport.write_i64::<BigEndian>(i).map_err(From::from)
}
fn write_double(&mut self, d: f64) -> ::Result<()> {
self.transport.write_f64::<BigEndian>(d).map_err(From::from)
}
fn write_string(&mut self, s: &str) -> ::Result<()> {
self.write_bytes(s.as_bytes())
}
fn write_list_begin(&mut self, identifier: &TListIdentifier) -> ::Result<()> {
self.write_byte(field_type_to_u8(identifier.element_type))?;
self.write_i32(identifier.size)
}
fn write_list_end(&mut self) -> ::Result<()> {
Ok(())
}
fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> ::Result<()> {
self.write_byte(field_type_to_u8(identifier.element_type))?;
self.write_i32(identifier.size)
}
fn write_set_end(&mut self) -> ::Result<()> {
Ok(())
}
fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> ::Result<()> {
let key_type = identifier
.key_type
.expect("map identifier to write should contain key type");
self.write_byte(field_type_to_u8(key_type))?;
let val_type = identifier
.value_type
.expect("map identifier to write should contain value type");
self.write_byte(field_type_to_u8(val_type))?;
self.write_i32(identifier.size)
}
fn write_map_end(&mut self) -> ::Result<()> {
Ok(())
}
fn flush(&mut self) -> ::Result<()> {
self.transport.flush().map_err(From::from)
}
// utility
//
fn write_byte(&mut self, b: u8) -> ::Result<()> {
self.transport.write_u8(b).map_err(From::from)
}
}
/// Factory for creating instances of `TBinaryOutputProtocol`.
#[derive(Default)]
pub struct TBinaryOutputProtocolFactory;
impl TBinaryOutputProtocolFactory {
/// Create a `TBinaryOutputProtocolFactory`.
pub fn new() -> TBinaryOutputProtocolFactory {
TBinaryOutputProtocolFactory {}
}
}
impl TOutputProtocolFactory for TBinaryOutputProtocolFactory {
fn create(&self, transport: Box<TWriteTransport + Send>) -> Box<TOutputProtocol + Send> {
Box::new(TBinaryOutputProtocol::new(transport, true))
}
}
fn field_type_to_u8(field_type: TType) -> u8 {
match field_type {
TType::Stop => 0x00,
TType::Void => 0x01,
TType::Bool => 0x02,
TType::I08 => 0x03, // equivalent to TType::Byte
TType::Double => 0x04,
TType::I16 => 0x06,
TType::I32 => 0x08,
TType::I64 => 0x0A,
TType::String | TType::Utf7 => 0x0B,
TType::Struct => 0x0C,
TType::Map => 0x0D,
TType::Set => 0x0E,
TType::List => 0x0F,
TType::Utf8 => 0x10,
TType::Utf16 => 0x11,
}
}
fn field_type_from_u8(b: u8) -> ::Result<TType> {
match b {
0x00 => Ok(TType::Stop),
0x01 => Ok(TType::Void),
0x02 => Ok(TType::Bool),
0x03 => Ok(TType::I08), // Equivalent to TType::Byte
0x04 => Ok(TType::Double),
0x06 => Ok(TType::I16),
0x08 => Ok(TType::I32),
0x0A => Ok(TType::I64),
0x0B => Ok(TType::String), // technically, also a UTF7, but we'll treat it as string
0x0C => Ok(TType::Struct),
0x0D => Ok(TType::Map),
0x0E => Ok(TType::Set),
0x0F => Ok(TType::List),
0x10 => Ok(TType::Utf8),
0x11 => Ok(TType::Utf16),
unkn => Err(::Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::InvalidData,
message: format!("cannot convert {} to TType", unkn),
})),
}
}
#[cfg(test)]
mod tests {
use protocol::{
TFieldIdentifier, TInputProtocol, TListIdentifier, TMapIdentifier, TMessageIdentifier,
TMessageType, TOutputProtocol, TSetIdentifier, TStructIdentifier, TType,
};
use transport::{ReadHalf, TBufferChannel, TIoChannel, WriteHalf};
use super::*;
#[test]
fn must_write_strict_message_call_begin() {
let (_, mut o_prot) = test_objects(true);
let ident = TMessageIdentifier::new("test", TMessageType::Call, 1);
assert!(o_prot.write_message_begin(&ident).is_ok());
#[cfg_attr(rustfmt, rustfmt::skip)]
let expected: [u8; 16] = [
0x80,
0x01,
0x00,
0x01,
0x00,
0x00,
0x00,
0x04,
0x74,
0x65,
0x73,
0x74,
0x00,
0x00,
0x00,
0x01,
];
assert_eq_written_bytes!(o_prot, expected);
}
#[test]
fn must_write_non_strict_message_call_begin() {
let (_, mut o_prot) = test_objects(false);
let ident = TMessageIdentifier::new("test", TMessageType::Call, 1);
assert!(o_prot.write_message_begin(&ident).is_ok());
#[cfg_attr(rustfmt, rustfmt::skip)]
let expected: [u8; 13] = [
0x00,
0x00,
0x00,
0x04,
0x74,
0x65,
0x73,
0x74,
0x01,
0x00,
0x00,
0x00,
0x01,
];
assert_eq_written_bytes!(o_prot, expected);
}
#[test]
fn must_write_strict_message_reply_begin() {
let (_, mut o_prot) = test_objects(true);
let ident = TMessageIdentifier::new("test", TMessageType::Reply, 10);
assert!(o_prot.write_message_begin(&ident).is_ok());
#[cfg_attr(rustfmt, rustfmt::skip)]
let expected: [u8; 16] = [
0x80,
0x01,
0x00,
0x02,
0x00,
0x00,
0x00,
0x04,
0x74,
0x65,
0x73,
0x74,
0x00,
0x00,
0x00,
0x0A,
];
assert_eq_written_bytes!(o_prot, expected);
}
#[test]
fn must_write_non_strict_message_reply_begin() {
let (_, mut o_prot) = test_objects(false);
let ident = TMessageIdentifier::new("test", TMessageType::Reply, 10);
assert!(o_prot.write_message_begin(&ident).is_ok());
#[cfg_attr(rustfmt, rustfmt::skip)]
let expected: [u8; 13] = [
0x00,
0x00,
0x00,
0x04,
0x74,
0x65,
0x73,
0x74,
0x02,
0x00,
0x00,
0x00,
0x0A,
];
assert_eq_written_bytes!(o_prot, expected);
}
#[test]
fn must_round_trip_strict_message_begin() {
let (mut i_prot, mut o_prot) = test_objects(true);
let sent_ident = TMessageIdentifier::new("test", TMessageType::Call, 1);
assert!(o_prot.write_message_begin(&sent_ident).is_ok());
copy_write_buffer_to_read_buffer!(o_prot);
let received_ident = assert_success!(i_prot.read_message_begin());
assert_eq!(&received_ident, &sent_ident);
}
#[test]
fn must_round_trip_non_strict_message_begin() {
let (mut i_prot, mut o_prot) = test_objects(false);
let sent_ident = TMessageIdentifier::new("test", TMessageType::Call, 1);
assert!(o_prot.write_message_begin(&sent_ident).is_ok());
copy_write_buffer_to_read_buffer!(o_prot);
let received_ident = assert_success!(i_prot.read_message_begin());
assert_eq!(&received_ident, &sent_ident);
}
#[test]
fn must_write_message_end() {
assert_no_write(|o| o.write_message_end(), true);
}
#[test]
fn must_write_struct_begin() {
assert_no_write(
|o| o.write_struct_begin(&TStructIdentifier::new("foo")),
true,
);
}
#[test]
fn must_write_struct_end() {
assert_no_write(|o| o.write_struct_end(), true);
}
#[test]
fn must_write_field_begin() {
let (_, mut o_prot) = test_objects(true);
assert!(o_prot
.write_field_begin(&TFieldIdentifier::new("some_field", TType::String, 22))
.is_ok());
let expected: [u8; 3] = [0x0B, 0x00, 0x16];
assert_eq_written_bytes!(o_prot, expected);
}
#[test]
fn must_round_trip_field_begin() {
let (mut i_prot, mut o_prot) = test_objects(true);
let sent_field_ident = TFieldIdentifier::new("foo", TType::I64, 20);
assert!(o_prot.write_field_begin(&sent_field_ident).is_ok());
copy_write_buffer_to_read_buffer!(o_prot);
let expected_ident = TFieldIdentifier {
name: None,
field_type: TType::I64,
id: Some(20),
}; // no name
let received_ident = assert_success!(i_prot.read_field_begin());
assert_eq!(&received_ident, &expected_ident);
}
#[test]
fn must_write_stop_field() {
let (_, mut o_prot) = test_objects(true);
assert!(o_prot.write_field_stop().is_ok());
let expected: [u8; 1] = [0x00];
assert_eq_written_bytes!(o_prot, expected);
}
#[test]
fn must_round_trip_field_stop() {
let (mut i_prot, mut o_prot) = test_objects(true);
assert!(o_prot.write_field_stop().is_ok());
copy_write_buffer_to_read_buffer!(o_prot);
let expected_ident = TFieldIdentifier {
name: None,
field_type: TType::Stop,
id: Some(0),
}; // we get id 0
let received_ident = assert_success!(i_prot.read_field_begin());
assert_eq!(&received_ident, &expected_ident);
}
#[test]
fn must_write_field_end() {
assert_no_write(|o| o.write_field_end(), true);
}
#[test]
fn must_write_list_begin() {
let (_, mut o_prot) = test_objects(true);
assert!(o_prot
.write_list_begin(&TListIdentifier::new(TType::Bool, 5))
.is_ok());
let expected: [u8; 5] = [0x02, 0x00, 0x00, 0x00, 0x05];
assert_eq_written_bytes!(o_prot, expected);
}
#[test]
fn must_round_trip_list_begin() {
let (mut i_prot, mut o_prot) = test_objects(true);
let ident = TListIdentifier::new(TType::List, 900);
assert!(o_prot.write_list_begin(&ident).is_ok());
copy_write_buffer_to_read_buffer!(o_prot);
let received_ident = assert_success!(i_prot.read_list_begin());
assert_eq!(&received_ident, &ident);
}
#[test]
fn must_write_list_end() {
assert_no_write(|o| o.write_list_end(), true);
}
#[test]
fn must_write_set_begin() {
let (_, mut o_prot) = test_objects(true);
assert!(o_prot
.write_set_begin(&TSetIdentifier::new(TType::I16, 7))
.is_ok());
let expected: [u8; 5] = [0x06, 0x00, 0x00, 0x00, 0x07];
assert_eq_written_bytes!(o_prot, expected);
}
#[test]
fn must_round_trip_set_begin() {
let (mut i_prot, mut o_prot) = test_objects(true);
let ident = TSetIdentifier::new(TType::I64, 2000);
assert!(o_prot.write_set_begin(&ident).is_ok());
copy_write_buffer_to_read_buffer!(o_prot);
let received_ident_result = i_prot.read_set_begin();
assert!(received_ident_result.is_ok());
assert_eq!(&received_ident_result.unwrap(), &ident);
}
#[test]
fn must_write_set_end() {
assert_no_write(|o| o.write_set_end(), true);
}
#[test]
fn must_write_map_begin() {
let (_, mut o_prot) = test_objects(true);
assert!(o_prot
.write_map_begin(&TMapIdentifier::new(TType::I64, TType::Struct, 32))
.is_ok());
let expected: [u8; 6] = [0x0A, 0x0C, 0x00, 0x00, 0x00, 0x20];
assert_eq_written_bytes!(o_prot, expected);
}
#[test]
fn must_round_trip_map_begin() {
let (mut i_prot, mut o_prot) = test_objects(true);
let ident = TMapIdentifier::new(TType::Map, TType::Set, 100);
assert!(o_prot.write_map_begin(&ident).is_ok());
copy_write_buffer_to_read_buffer!(o_prot);
let received_ident = assert_success!(i_prot.read_map_begin());
assert_eq!(&received_ident, &ident);
}
#[test]
fn must_write_map_end() {
assert_no_write(|o| o.write_map_end(), true);
}
#[test]
fn must_write_bool_true() {
let (_, mut o_prot) = test_objects(true);
assert!(o_prot.write_bool(true).is_ok());
let expected: [u8; 1] = [0x01];
assert_eq_written_bytes!(o_prot, expected);
}
#[test]
fn must_write_bool_false() {
let (_, mut o_prot) = test_objects(true);
assert!(o_prot.write_bool(false).is_ok());
let expected: [u8; 1] = [0x00];
assert_eq_written_bytes!(o_prot, expected);
}
#[test]
fn must_read_bool_true() {
let (mut i_prot, _) = test_objects(true);
set_readable_bytes!(i_prot, &[0x01]);
let read_bool = assert_success!(i_prot.read_bool());
assert_eq!(read_bool, true);
}
#[test]
fn must_read_bool_false() {
let (mut i_prot, _) = test_objects(true);
set_readable_bytes!(i_prot, &[0x00]);
let read_bool = assert_success!(i_prot.read_bool());
assert_eq!(read_bool, false);
}
#[test]
fn must_allow_any_non_zero_value_to_be_interpreted_as_bool_true() {
let (mut i_prot, _) = test_objects(true);
set_readable_bytes!(i_prot, &[0xAC]);
let read_bool = assert_success!(i_prot.read_bool());
assert_eq!(read_bool, true);
}
#[test]
fn must_write_bytes() {
let (_, mut o_prot) = test_objects(true);
let bytes: [u8; 10] = [0x0A, 0xCC, 0xD1, 0x84, 0x99, 0x12, 0xAB, 0xBB, 0x45, 0xDF];
assert!(o_prot.write_bytes(&bytes).is_ok());
let buf = o_prot.transport.write_bytes();
assert_eq!(&buf[0..4], [0x00, 0x00, 0x00, 0x0A]); // length
assert_eq!(&buf[4..], bytes); // actual bytes
}
#[test]
fn must_round_trip_bytes() {
let (mut i_prot, mut o_prot) = test_objects(true);
#[cfg_attr(rustfmt, rustfmt::skip)]
let bytes: [u8; 25] = [
0x20,
0xFD,
0x18,
0x84,
0x99,
0x12,
0xAB,
0xBB,
0x45,
0xDF,
0x34,
0xDC,
0x98,
0xA4,
0x6D,
0xF3,
0x99,
0xB4,
0xB7,
0xD4,
0x9C,
0xA5,
0xB3,
0xC9,
0x88,
];
assert!(o_prot.write_bytes(&bytes).is_ok());
copy_write_buffer_to_read_buffer!(o_prot);
let received_bytes = assert_success!(i_prot.read_bytes());
assert_eq!(&received_bytes, &bytes);
}
fn test_objects(
strict: bool,
) -> (
TBinaryInputProtocol<ReadHalf<TBufferChannel>>,
TBinaryOutputProtocol<WriteHalf<TBufferChannel>>,
) {
let mem = TBufferChannel::with_capacity(40, 40);
let (r_mem, w_mem) = mem.split().unwrap();
let i_prot = TBinaryInputProtocol::new(r_mem, strict);
let o_prot = TBinaryOutputProtocol::new(w_mem, strict);
(i_prot, o_prot)
}
fn assert_no_write<F>(mut write_fn: F, strict: bool)
where
F: FnMut(&mut TBinaryOutputProtocol<WriteHalf<TBufferChannel>>) -> ::Result<()>,
{
let (_, mut o_prot) = test_objects(strict);
assert!(write_fn(&mut o_prot).is_ok());
assert_eq!(o_prot.transport.write_bytes().len(), 0);
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,969 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Types used to send and receive primitives between a Thrift client and server.
//!
//! # Examples
//!
//! Create and use a `TInputProtocol`.
//!
//! ```no_run
//! use thrift::protocol::{TBinaryInputProtocol, TInputProtocol};
//! use thrift::transport::TTcpChannel;
//!
//! // create the I/O channel
//! let mut channel = TTcpChannel::new();
//! channel.open("127.0.0.1:9090").unwrap();
//!
//! // create the protocol to decode bytes into types
//! let mut protocol = TBinaryInputProtocol::new(channel, true);
//!
//! // read types from the wire
//! let field_identifier = protocol.read_field_begin().unwrap();
//! let field_contents = protocol.read_string().unwrap();
//! let field_end = protocol.read_field_end().unwrap();
//! ```
//!
//! Create and use a `TOutputProtocol`.
//!
//! ```no_run
//! use thrift::protocol::{TBinaryOutputProtocol, TFieldIdentifier, TOutputProtocol, TType};
//! use thrift::transport::TTcpChannel;
//!
//! // create the I/O channel
//! let mut channel = TTcpChannel::new();
//! channel.open("127.0.0.1:9090").unwrap();
//!
//! // create the protocol to encode types into bytes
//! let mut protocol = TBinaryOutputProtocol::new(channel, true);
//!
//! // write types
//! protocol.write_field_begin(&TFieldIdentifier::new("string_thing", TType::String, 1)).unwrap();
//! protocol.write_string("foo").unwrap();
//! protocol.write_field_end().unwrap();
//! ```
use std::convert::From;
use std::fmt;
use std::fmt::{Display, Formatter};
use try_from::TryFrom;
use transport::{TReadTransport, TWriteTransport};
use {ProtocolError, ProtocolErrorKind};
#[cfg(test)]
macro_rules! assert_eq_written_bytes {
($o_prot:ident, $expected_bytes:ident) => {{
assert_eq!($o_prot.transport.write_bytes(), &$expected_bytes);
}};
}
// FIXME: should take both read and write
#[cfg(test)]
macro_rules! copy_write_buffer_to_read_buffer {
($o_prot:ident) => {{
$o_prot.transport.copy_write_buffer_to_read_buffer();
}};
}
#[cfg(test)]
macro_rules! set_readable_bytes {
($i_prot:ident, $bytes:expr) => {
$i_prot.transport.set_readable_bytes($bytes);
};
}
mod binary;
mod compact;
mod multiplexed;
mod stored;
pub use self::binary::{
TBinaryInputProtocol, TBinaryInputProtocolFactory, TBinaryOutputProtocol,
TBinaryOutputProtocolFactory,
};
pub use self::compact::{
TCompactInputProtocol, TCompactInputProtocolFactory, TCompactOutputProtocol,
TCompactOutputProtocolFactory,
};
pub use self::multiplexed::TMultiplexedOutputProtocol;
pub use self::stored::TStoredInputProtocol;
// Default maximum depth to which `TInputProtocol::skip` will skip a Thrift
// field. A default is necessary because Thrift structs or collections may
// contain nested structs and collections, which could result in indefinite
// recursion.
const MAXIMUM_SKIP_DEPTH: i8 = 64;
/// Converts a stream of bytes into Thrift identifiers, primitives,
/// containers, or structs.
///
/// This trait does not deal with higher-level Thrift concepts like structs or
/// exceptions - only with primitives and message or container boundaries. Once
/// bytes are read they are deserialized and an identifier (for example
/// `TMessageIdentifier`) or a primitive is returned.
///
/// All methods return a `thrift::Result`. If an `Err` is returned the protocol
/// instance and its underlying transport should be terminated.
///
/// # Examples
///
/// Create and use a `TInputProtocol`
///
/// ```no_run
/// use thrift::protocol::{TBinaryInputProtocol, TInputProtocol};
/// use thrift::transport::TTcpChannel;
///
/// let mut channel = TTcpChannel::new();
/// channel.open("127.0.0.1:9090").unwrap();
///
/// let mut protocol = TBinaryInputProtocol::new(channel, true);
///
/// let field_identifier = protocol.read_field_begin().unwrap();
/// let field_contents = protocol.read_string().unwrap();
/// let field_end = protocol.read_field_end().unwrap();
/// ```
pub trait TInputProtocol {
/// Read the beginning of a Thrift message.
fn read_message_begin(&mut self) -> ::Result<TMessageIdentifier>;
/// Read the end of a Thrift message.
fn read_message_end(&mut self) -> ::Result<()>;
/// Read the beginning of a Thrift struct.
fn read_struct_begin(&mut self) -> ::Result<Option<TStructIdentifier>>;
/// Read the end of a Thrift struct.
fn read_struct_end(&mut self) -> ::Result<()>;
/// Read the beginning of a Thrift struct field.
fn read_field_begin(&mut self) -> ::Result<TFieldIdentifier>;
/// Read the end of a Thrift struct field.
fn read_field_end(&mut self) -> ::Result<()>;
/// Read a bool.
fn read_bool(&mut self) -> ::Result<bool>;
/// Read a fixed-length byte array.
fn read_bytes(&mut self) -> ::Result<Vec<u8>>;
/// Read a word.
fn read_i8(&mut self) -> ::Result<i8>;
/// Read a 16-bit signed integer.
fn read_i16(&mut self) -> ::Result<i16>;
/// Read a 32-bit signed integer.
fn read_i32(&mut self) -> ::Result<i32>;
/// Read a 64-bit signed integer.
fn read_i64(&mut self) -> ::Result<i64>;
/// Read a 64-bit float.
fn read_double(&mut self) -> ::Result<f64>;
/// Read a fixed-length string (not null terminated).
fn read_string(&mut self) -> ::Result<String>;
/// Read the beginning of a list.
fn read_list_begin(&mut self) -> ::Result<TListIdentifier>;
/// Read the end of a list.
fn read_list_end(&mut self) -> ::Result<()>;
/// Read the beginning of a set.
fn read_set_begin(&mut self) -> ::Result<TSetIdentifier>;
/// Read the end of a set.
fn read_set_end(&mut self) -> ::Result<()>;
/// Read the beginning of a map.
fn read_map_begin(&mut self) -> ::Result<TMapIdentifier>;
/// Read the end of a map.
fn read_map_end(&mut self) -> ::Result<()>;
/// Skip a field with type `field_type` recursively until the default
/// maximum skip depth is reached.
fn skip(&mut self, field_type: TType) -> ::Result<()> {
self.skip_till_depth(field_type, MAXIMUM_SKIP_DEPTH)
}
/// Skip a field with type `field_type` recursively up to `depth` levels.
fn skip_till_depth(&mut self, field_type: TType, depth: i8) -> ::Result<()> {
if depth == 0 {
return Err(::Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::DepthLimit,
message: format!("cannot parse past {:?}", field_type),
}));
}
match field_type {
TType::Bool => self.read_bool().map(|_| ()),
TType::I08 => self.read_i8().map(|_| ()),
TType::I16 => self.read_i16().map(|_| ()),
TType::I32 => self.read_i32().map(|_| ()),
TType::I64 => self.read_i64().map(|_| ()),
TType::Double => self.read_double().map(|_| ()),
TType::String => self.read_string().map(|_| ()),
TType::Struct => {
self.read_struct_begin()?;
loop {
let field_ident = self.read_field_begin()?;
if field_ident.field_type == TType::Stop {
break;
}
self.skip_till_depth(field_ident.field_type, depth - 1)?;
}
self.read_struct_end()
}
TType::List => {
let list_ident = self.read_list_begin()?;
for _ in 0..list_ident.size {
self.skip_till_depth(list_ident.element_type, depth - 1)?;
}
self.read_list_end()
}
TType::Set => {
let set_ident = self.read_set_begin()?;
for _ in 0..set_ident.size {
self.skip_till_depth(set_ident.element_type, depth - 1)?;
}
self.read_set_end()
}
TType::Map => {
let map_ident = self.read_map_begin()?;
for _ in 0..map_ident.size {
let key_type = map_ident
.key_type
.expect("non-zero sized map should contain key type");
let val_type = map_ident
.value_type
.expect("non-zero sized map should contain value type");
self.skip_till_depth(key_type, depth - 1)?;
self.skip_till_depth(val_type, depth - 1)?;
}
self.read_map_end()
}
u => Err(::Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::Unknown,
message: format!("cannot skip field type {:?}", &u),
})),
}
}
// utility (DO NOT USE IN GENERATED CODE!!!!)
//
/// Read an unsigned byte.
///
/// This method should **never** be used in generated code.
fn read_byte(&mut self) -> ::Result<u8>;
}
/// Converts Thrift identifiers, primitives, containers or structs into a
/// stream of bytes.
///
/// This trait does not deal with higher-level Thrift concepts like structs or
/// exceptions - only with primitives and message or container boundaries.
/// Write methods take an identifier (for example, `TMessageIdentifier`) or a
/// primitive. Any or all of the fields in an identifier may be omitted when
/// writing to the transport. Write methods may even be noops. All of this is
/// transparent to the caller; as long as a matching `TInputProtocol`
/// implementation is used, received messages will be decoded correctly.
///
/// All methods return a `thrift::Result`. If an `Err` is returned the protocol
/// instance and its underlying transport should be terminated.
///
/// # Examples
///
/// Create and use a `TOutputProtocol`
///
/// ```no_run
/// use thrift::protocol::{TBinaryOutputProtocol, TFieldIdentifier, TOutputProtocol, TType};
/// use thrift::transport::TTcpChannel;
///
/// let mut channel = TTcpChannel::new();
/// channel.open("127.0.0.1:9090").unwrap();
///
/// let mut protocol = TBinaryOutputProtocol::new(channel, true);
///
/// protocol.write_field_begin(&TFieldIdentifier::new("string_thing", TType::String, 1)).unwrap();
/// protocol.write_string("foo").unwrap();
/// protocol.write_field_end().unwrap();
/// ```
pub trait TOutputProtocol {
/// Write the beginning of a Thrift message.
fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> ::Result<()>;
/// Write the end of a Thrift message.
fn write_message_end(&mut self) -> ::Result<()>;
/// Write the beginning of a Thrift struct.
fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> ::Result<()>;
/// Write the end of a Thrift struct.
fn write_struct_end(&mut self) -> ::Result<()>;
/// Write the beginning of a Thrift field.
fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> ::Result<()>;
/// Write the end of a Thrift field.
fn write_field_end(&mut self) -> ::Result<()>;
/// Write a STOP field indicating that all the fields in a struct have been
/// written.
fn write_field_stop(&mut self) -> ::Result<()>;
/// Write a bool.
fn write_bool(&mut self, b: bool) -> ::Result<()>;
/// Write a fixed-length byte array.
fn write_bytes(&mut self, b: &[u8]) -> ::Result<()>;
/// Write an 8-bit signed integer.
fn write_i8(&mut self, i: i8) -> ::Result<()>;
/// Write a 16-bit signed integer.
fn write_i16(&mut self, i: i16) -> ::Result<()>;
/// Write a 32-bit signed integer.
fn write_i32(&mut self, i: i32) -> ::Result<()>;
/// Write a 64-bit signed integer.
fn write_i64(&mut self, i: i64) -> ::Result<()>;
/// Write a 64-bit float.
fn write_double(&mut self, d: f64) -> ::Result<()>;
/// Write a fixed-length string.
fn write_string(&mut self, s: &str) -> ::Result<()>;
/// Write the beginning of a list.
fn write_list_begin(&mut self, identifier: &TListIdentifier) -> ::Result<()>;
/// Write the end of a list.
fn write_list_end(&mut self) -> ::Result<()>;
/// Write the beginning of a set.
fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> ::Result<()>;
/// Write the end of a set.
fn write_set_end(&mut self) -> ::Result<()>;
/// Write the beginning of a map.
fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> ::Result<()>;
/// Write the end of a map.
fn write_map_end(&mut self) -> ::Result<()>;
/// Flush buffered bytes to the underlying transport.
fn flush(&mut self) -> ::Result<()>;
// utility (DO NOT USE IN GENERATED CODE!!!!)
//
/// Write an unsigned byte.
///
/// This method should **never** be used in generated code.
fn write_byte(&mut self, b: u8) -> ::Result<()>; // FIXME: REMOVE
}
impl<P> TInputProtocol for Box<P>
where
P: TInputProtocol + ?Sized,
{
fn read_message_begin(&mut self) -> ::Result<TMessageIdentifier> {
(**self).read_message_begin()
}
fn read_message_end(&mut self) -> ::Result<()> {
(**self).read_message_end()
}
fn read_struct_begin(&mut self) -> ::Result<Option<TStructIdentifier>> {
(**self).read_struct_begin()
}
fn read_struct_end(&mut self) -> ::Result<()> {
(**self).read_struct_end()
}
fn read_field_begin(&mut self) -> ::Result<TFieldIdentifier> {
(**self).read_field_begin()
}
fn read_field_end(&mut self) -> ::Result<()> {
(**self).read_field_end()
}
fn read_bool(&mut self) -> ::Result<bool> {
(**self).read_bool()
}
fn read_bytes(&mut self) -> ::Result<Vec<u8>> {
(**self).read_bytes()
}
fn read_i8(&mut self) -> ::Result<i8> {
(**self).read_i8()
}
fn read_i16(&mut self) -> ::Result<i16> {
(**self).read_i16()
}
fn read_i32(&mut self) -> ::Result<i32> {
(**self).read_i32()
}
fn read_i64(&mut self) -> ::Result<i64> {
(**self).read_i64()
}
fn read_double(&mut self) -> ::Result<f64> {
(**self).read_double()
}
fn read_string(&mut self) -> ::Result<String> {
(**self).read_string()
}
fn read_list_begin(&mut self) -> ::Result<TListIdentifier> {
(**self).read_list_begin()
}
fn read_list_end(&mut self) -> ::Result<()> {
(**self).read_list_end()
}
fn read_set_begin(&mut self) -> ::Result<TSetIdentifier> {
(**self).read_set_begin()
}
fn read_set_end(&mut self) -> ::Result<()> {
(**self).read_set_end()
}
fn read_map_begin(&mut self) -> ::Result<TMapIdentifier> {
(**self).read_map_begin()
}
fn read_map_end(&mut self) -> ::Result<()> {
(**self).read_map_end()
}
fn read_byte(&mut self) -> ::Result<u8> {
(**self).read_byte()
}
}
impl<P> TOutputProtocol for Box<P>
where
P: TOutputProtocol + ?Sized,
{
fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> ::Result<()> {
(**self).write_message_begin(identifier)
}
fn write_message_end(&mut self) -> ::Result<()> {
(**self).write_message_end()
}
fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> ::Result<()> {
(**self).write_struct_begin(identifier)
}
fn write_struct_end(&mut self) -> ::Result<()> {
(**self).write_struct_end()
}
fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> ::Result<()> {
(**self).write_field_begin(identifier)
}
fn write_field_end(&mut self) -> ::Result<()> {
(**self).write_field_end()
}
fn write_field_stop(&mut self) -> ::Result<()> {
(**self).write_field_stop()
}
fn write_bool(&mut self, b: bool) -> ::Result<()> {
(**self).write_bool(b)
}
fn write_bytes(&mut self, b: &[u8]) -> ::Result<()> {
(**self).write_bytes(b)
}
fn write_i8(&mut self, i: i8) -> ::Result<()> {
(**self).write_i8(i)
}
fn write_i16(&mut self, i: i16) -> ::Result<()> {
(**self).write_i16(i)
}
fn write_i32(&mut self, i: i32) -> ::Result<()> {
(**self).write_i32(i)
}
fn write_i64(&mut self, i: i64) -> ::Result<()> {
(**self).write_i64(i)
}
fn write_double(&mut self, d: f64) -> ::Result<()> {
(**self).write_double(d)
}
fn write_string(&mut self, s: &str) -> ::Result<()> {
(**self).write_string(s)
}
fn write_list_begin(&mut self, identifier: &TListIdentifier) -> ::Result<()> {
(**self).write_list_begin(identifier)
}
fn write_list_end(&mut self) -> ::Result<()> {
(**self).write_list_end()
}
fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> ::Result<()> {
(**self).write_set_begin(identifier)
}
fn write_set_end(&mut self) -> ::Result<()> {
(**self).write_set_end()
}
fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> ::Result<()> {
(**self).write_map_begin(identifier)
}
fn write_map_end(&mut self) -> ::Result<()> {
(**self).write_map_end()
}
fn flush(&mut self) -> ::Result<()> {
(**self).flush()
}
fn write_byte(&mut self, b: u8) -> ::Result<()> {
(**self).write_byte(b)
}
}
/// Helper type used by servers to create `TInputProtocol` instances for
/// accepted client connections.
///
/// # Examples
///
/// Create a `TInputProtocolFactory` and use it to create a `TInputProtocol`.
///
/// ```no_run
/// use thrift::protocol::{TBinaryInputProtocolFactory, TInputProtocolFactory};
/// use thrift::transport::TTcpChannel;
///
/// let mut channel = TTcpChannel::new();
/// channel.open("127.0.0.1:9090").unwrap();
///
/// let factory = TBinaryInputProtocolFactory::new();
/// let protocol = factory.create(Box::new(channel));
/// ```
pub trait TInputProtocolFactory {
// Create a `TInputProtocol` that reads bytes from `transport`.
fn create(&self, transport: Box<TReadTransport + Send>) -> Box<TInputProtocol + Send>;
}
impl<T> TInputProtocolFactory for Box<T>
where
T: TInputProtocolFactory + ?Sized,
{
fn create(&self, transport: Box<TReadTransport + Send>) -> Box<TInputProtocol + Send> {
(**self).create(transport)
}
}
/// Helper type used by servers to create `TOutputProtocol` instances for
/// accepted client connections.
///
/// # Examples
///
/// Create a `TOutputProtocolFactory` and use it to create a `TOutputProtocol`.
///
/// ```no_run
/// use thrift::protocol::{TBinaryOutputProtocolFactory, TOutputProtocolFactory};
/// use thrift::transport::TTcpChannel;
///
/// let mut channel = TTcpChannel::new();
/// channel.open("127.0.0.1:9090").unwrap();
///
/// let factory = TBinaryOutputProtocolFactory::new();
/// let protocol = factory.create(Box::new(channel));
/// ```
pub trait TOutputProtocolFactory {
/// Create a `TOutputProtocol` that writes bytes to `transport`.
fn create(&self, transport: Box<TWriteTransport + Send>) -> Box<TOutputProtocol + Send>;
}
impl<T> TOutputProtocolFactory for Box<T>
where
T: TOutputProtocolFactory + ?Sized,
{
fn create(&self, transport: Box<TWriteTransport + Send>) -> Box<TOutputProtocol + Send> {
(**self).create(transport)
}
}
/// Thrift message identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TMessageIdentifier {
/// Service call the message is associated with.
pub name: String,
/// Message type.
pub message_type: TMessageType,
/// Ordered sequence number identifying the message.
pub sequence_number: i32,
}
impl TMessageIdentifier {
/// Create a `TMessageIdentifier` for a Thrift service-call named `name`
/// with message type `message_type` and sequence number `sequence_number`.
pub fn new<S: Into<String>>(
name: S,
message_type: TMessageType,
sequence_number: i32,
) -> TMessageIdentifier {
TMessageIdentifier {
name: name.into(),
message_type: message_type,
sequence_number: sequence_number,
}
}
}
/// Thrift struct identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TStructIdentifier {
/// Name of the encoded Thrift struct.
pub name: String,
}
impl TStructIdentifier {
/// Create a `TStructIdentifier` for a struct named `name`.
pub fn new<S: Into<String>>(name: S) -> TStructIdentifier {
TStructIdentifier { name: name.into() }
}
}
/// Thrift field identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TFieldIdentifier {
/// Name of the Thrift field.
///
/// `None` if it's not sent over the wire.
pub name: Option<String>,
/// Field type.
///
/// This may be a primitive, container, or a struct.
pub field_type: TType,
/// Thrift field id.
///
/// `None` only if `field_type` is `TType::Stop`.
pub id: Option<i16>,
}
impl TFieldIdentifier {
/// Create a `TFieldIdentifier` for a field named `name` with type
/// `field_type` and field id `id`.
///
/// `id` should be `None` if `field_type` is `TType::Stop`.
pub fn new<N, S, I>(name: N, field_type: TType, id: I) -> TFieldIdentifier
where
N: Into<Option<S>>,
S: Into<String>,
I: Into<Option<i16>>,
{
TFieldIdentifier {
name: name.into().map(|n| n.into()),
field_type: field_type,
id: id.into(),
}
}
}
/// Thrift list identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TListIdentifier {
/// Type of the elements in the list.
pub element_type: TType,
/// Number of elements in the list.
pub size: i32,
}
impl TListIdentifier {
/// Create a `TListIdentifier` for a list with `size` elements of type
/// `element_type`.
pub fn new(element_type: TType, size: i32) -> TListIdentifier {
TListIdentifier {
element_type: element_type,
size: size,
}
}
}
/// Thrift set identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TSetIdentifier {
/// Type of the elements in the set.
pub element_type: TType,
/// Number of elements in the set.
pub size: i32,
}
impl TSetIdentifier {
/// Create a `TSetIdentifier` for a set with `size` elements of type
/// `element_type`.
pub fn new(element_type: TType, size: i32) -> TSetIdentifier {
TSetIdentifier {
element_type: element_type,
size: size,
}
}
}
/// Thrift map identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TMapIdentifier {
/// Map key type.
pub key_type: Option<TType>,
/// Map value type.
pub value_type: Option<TType>,
/// Number of entries in the map.
pub size: i32,
}
impl TMapIdentifier {
/// Create a `TMapIdentifier` for a map with `size` entries of type
/// `key_type -> value_type`.
pub fn new<K, V>(key_type: K, value_type: V, size: i32) -> TMapIdentifier
where
K: Into<Option<TType>>,
V: Into<Option<TType>>,
{
TMapIdentifier {
key_type: key_type.into(),
value_type: value_type.into(),
size: size,
}
}
}
/// Thrift message types.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TMessageType {
/// Service-call request.
Call,
/// Service-call response.
Reply,
/// Unexpected error in the remote service.
Exception,
/// One-way service-call request (no response is expected).
OneWay,
}
impl Display for TMessageType {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
TMessageType::Call => write!(f, "Call"),
TMessageType::Reply => write!(f, "Reply"),
TMessageType::Exception => write!(f, "Exception"),
TMessageType::OneWay => write!(f, "OneWay"),
}
}
}
impl From<TMessageType> for u8 {
fn from(message_type: TMessageType) -> Self {
match message_type {
TMessageType::Call => 0x01,
TMessageType::Reply => 0x02,
TMessageType::Exception => 0x03,
TMessageType::OneWay => 0x04,
}
}
}
impl TryFrom<u8> for TMessageType {
type Err = ::Error;
fn try_from(b: u8) -> ::Result<Self> {
match b {
0x01 => Ok(TMessageType::Call),
0x02 => Ok(TMessageType::Reply),
0x03 => Ok(TMessageType::Exception),
0x04 => Ok(TMessageType::OneWay),
unkn => Err(::Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::InvalidData,
message: format!("cannot convert {} to TMessageType", unkn),
})),
}
}
}
/// Thrift struct-field types.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TType {
/// Indicates that there are no more serialized fields in this Thrift struct.
Stop,
/// Void (`()`) field.
Void,
/// Boolean.
Bool,
/// Signed 8-bit int.
I08,
/// Double-precision number.
Double,
/// Signed 16-bit int.
I16,
/// Signed 32-bit int.
I32,
/// Signed 64-bit int.
I64,
/// UTF-8 string.
String,
/// UTF-7 string. *Unsupported*.
Utf7,
/// Thrift struct.
Struct,
/// Map.
Map,
/// Set.
Set,
/// List.
List,
/// UTF-8 string.
Utf8,
/// UTF-16 string. *Unsupported*.
Utf16,
}
impl Display for TType {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
TType::Stop => write!(f, "STOP"),
TType::Void => write!(f, "void"),
TType::Bool => write!(f, "bool"),
TType::I08 => write!(f, "i08"),
TType::Double => write!(f, "double"),
TType::I16 => write!(f, "i16"),
TType::I32 => write!(f, "i32"),
TType::I64 => write!(f, "i64"),
TType::String => write!(f, "string"),
TType::Utf7 => write!(f, "UTF7"),
TType::Struct => write!(f, "struct"),
TType::Map => write!(f, "map"),
TType::Set => write!(f, "set"),
TType::List => write!(f, "list"),
TType::Utf8 => write!(f, "UTF8"),
TType::Utf16 => write!(f, "UTF16"),
}
}
}
/// Compare the expected message sequence number `expected` with the received
/// message sequence number `actual`.
///
/// Return `()` if `actual == expected`, `Err` otherwise.
pub fn verify_expected_sequence_number(expected: i32, actual: i32) -> ::Result<()> {
if expected == actual {
Ok(())
} else {
Err(::Error::Application(::ApplicationError {
kind: ::ApplicationErrorKind::BadSequenceId,
message: format!("expected {} got {}", expected, actual),
}))
}
}
/// Compare the expected service-call name `expected` with the received
/// service-call name `actual`.
///
/// Return `()` if `actual == expected`, `Err` otherwise.
pub fn verify_expected_service_call(expected: &str, actual: &str) -> ::Result<()> {
if expected == actual {
Ok(())
} else {
Err(::Error::Application(::ApplicationError {
kind: ::ApplicationErrorKind::WrongMethodName,
message: format!("expected {} got {}", expected, actual),
}))
}
}
/// Compare the expected message type `expected` with the received message type
/// `actual`.
///
/// Return `()` if `actual == expected`, `Err` otherwise.
pub fn verify_expected_message_type(expected: TMessageType, actual: TMessageType) -> ::Result<()> {
if expected == actual {
Ok(())
} else {
Err(::Error::Application(::ApplicationError {
kind: ::ApplicationErrorKind::InvalidMessageType,
message: format!("expected {} got {}", expected, actual),
}))
}
}
/// Check if a required Thrift struct field exists.
///
/// Return `()` if it does, `Err` otherwise.
pub fn verify_required_field_exists<T>(field_name: &str, field: &Option<T>) -> ::Result<()> {
match *field {
Some(_) => Ok(()),
None => Err(::Error::Protocol(::ProtocolError {
kind: ::ProtocolErrorKind::Unknown,
message: format!("missing required field {}", field_name),
})),
}
}
/// Extract the field id from a Thrift field identifier.
///
/// `field_ident` must *not* have `TFieldIdentifier.field_type` of type `TType::Stop`.
///
/// Return `TFieldIdentifier.id` if an id exists, `Err` otherwise.
pub fn field_id(field_ident: &TFieldIdentifier) -> ::Result<i16> {
field_ident.id.ok_or_else(|| {
::Error::Protocol(::ProtocolError {
kind: ::ProtocolErrorKind::Unknown,
message: format!("missing field in in {:?}", field_ident),
})
})
}
#[cfg(test)]
mod tests {
use std::io::Cursor;
use super::*;
use transport::{TReadTransport, TWriteTransport};
#[test]
fn must_create_usable_input_protocol_from_concrete_input_protocol() {
let r: Box<TReadTransport> = Box::new(Cursor::new([0, 1, 2]));
let mut t = TCompactInputProtocol::new(r);
takes_input_protocol(&mut t)
}
#[test]
fn must_create_usable_input_protocol_from_boxed_input() {
let r: Box<TReadTransport> = Box::new(Cursor::new([0, 1, 2]));
let mut t: Box<TInputProtocol> = Box::new(TCompactInputProtocol::new(r));
takes_input_protocol(&mut t)
}
#[test]
fn must_create_usable_output_protocol_from_concrete_output_protocol() {
let w: Box<TWriteTransport> = Box::new(vec![0u8; 10]);
let mut t = TCompactOutputProtocol::new(w);
takes_output_protocol(&mut t)
}
#[test]
fn must_create_usable_output_protocol_from_boxed_output() {
let w: Box<TWriteTransport> = Box::new(vec![0u8; 10]);
let mut t: Box<TOutputProtocol> = Box::new(TCompactOutputProtocol::new(w));
takes_output_protocol(&mut t)
}
fn takes_input_protocol<R>(t: &mut R)
where
R: TInputProtocol,
{
t.read_byte().unwrap();
}
fn takes_output_protocol<W>(t: &mut W)
where
W: TOutputProtocol,
{
t.flush().unwrap();
}
}

View file

@ -0,0 +1,239 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use super::{
TFieldIdentifier, TListIdentifier, TMapIdentifier, TMessageIdentifier, TMessageType,
TOutputProtocol, TSetIdentifier, TStructIdentifier,
};
/// `TOutputProtocol` that prefixes the service name to all outgoing Thrift
/// messages.
///
/// A `TMultiplexedOutputProtocol` should be used when multiple Thrift services
/// send messages over a single I/O channel. By prefixing service identifiers
/// to outgoing messages receivers are able to demux them and route them to the
/// appropriate service processor. Rust receivers must use a `TMultiplexedProcessor`
/// to process incoming messages, while other languages must use their
/// corresponding multiplexed processor implementations.
///
/// For example, given a service `TestService` and a service call `test_call`,
/// this implementation would identify messages as originating from
/// `TestService:test_call`.
///
/// # Examples
///
/// Create and use a `TMultiplexedOutputProtocol`.
///
/// ```no_run
/// use thrift::protocol::{TMessageIdentifier, TMessageType, TOutputProtocol};
/// use thrift::protocol::{TBinaryOutputProtocol, TMultiplexedOutputProtocol};
/// use thrift::transport::TTcpChannel;
///
/// let mut channel = TTcpChannel::new();
/// channel.open("localhost:9090").unwrap();
///
/// let protocol = TBinaryOutputProtocol::new(channel, true);
/// let mut protocol = TMultiplexedOutputProtocol::new("service_name", protocol);
///
/// let ident = TMessageIdentifier::new("svc_call", TMessageType::Call, 1);
/// protocol.write_message_begin(&ident).unwrap();
/// ```
#[derive(Debug)]
pub struct TMultiplexedOutputProtocol<P>
where
P: TOutputProtocol,
{
service_name: String,
inner: P,
}
impl<P> TMultiplexedOutputProtocol<P>
where
P: TOutputProtocol,
{
/// Create a `TMultiplexedOutputProtocol` that identifies outgoing messages
/// as originating from a service named `service_name` and sends them over
/// the `wrapped` `TOutputProtocol`. Outgoing messages are encoded and sent
/// by `wrapped`, not by this instance.
pub fn new(service_name: &str, wrapped: P) -> TMultiplexedOutputProtocol<P> {
TMultiplexedOutputProtocol {
service_name: service_name.to_owned(),
inner: wrapped,
}
}
}
// FIXME: avoid passthrough methods
impl<P> TOutputProtocol for TMultiplexedOutputProtocol<P>
where
P: TOutputProtocol,
{
fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> ::Result<()> {
match identifier.message_type {
// FIXME: is there a better way to override identifier here?
TMessageType::Call | TMessageType::OneWay => {
let identifier = TMessageIdentifier {
name: format!("{}:{}", self.service_name, identifier.name),
..*identifier
};
self.inner.write_message_begin(&identifier)
}
_ => self.inner.write_message_begin(identifier),
}
}
fn write_message_end(&mut self) -> ::Result<()> {
self.inner.write_message_end()
}
fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> ::Result<()> {
self.inner.write_struct_begin(identifier)
}
fn write_struct_end(&mut self) -> ::Result<()> {
self.inner.write_struct_end()
}
fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> ::Result<()> {
self.inner.write_field_begin(identifier)
}
fn write_field_end(&mut self) -> ::Result<()> {
self.inner.write_field_end()
}
fn write_field_stop(&mut self) -> ::Result<()> {
self.inner.write_field_stop()
}
fn write_bytes(&mut self, b: &[u8]) -> ::Result<()> {
self.inner.write_bytes(b)
}
fn write_bool(&mut self, b: bool) -> ::Result<()> {
self.inner.write_bool(b)
}
fn write_i8(&mut self, i: i8) -> ::Result<()> {
self.inner.write_i8(i)
}
fn write_i16(&mut self, i: i16) -> ::Result<()> {
self.inner.write_i16(i)
}
fn write_i32(&mut self, i: i32) -> ::Result<()> {
self.inner.write_i32(i)
}
fn write_i64(&mut self, i: i64) -> ::Result<()> {
self.inner.write_i64(i)
}
fn write_double(&mut self, d: f64) -> ::Result<()> {
self.inner.write_double(d)
}
fn write_string(&mut self, s: &str) -> ::Result<()> {
self.inner.write_string(s)
}
fn write_list_begin(&mut self, identifier: &TListIdentifier) -> ::Result<()> {
self.inner.write_list_begin(identifier)
}
fn write_list_end(&mut self) -> ::Result<()> {
self.inner.write_list_end()
}
fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> ::Result<()> {
self.inner.write_set_begin(identifier)
}
fn write_set_end(&mut self) -> ::Result<()> {
self.inner.write_set_end()
}
fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> ::Result<()> {
self.inner.write_map_begin(identifier)
}
fn write_map_end(&mut self) -> ::Result<()> {
self.inner.write_map_end()
}
fn flush(&mut self) -> ::Result<()> {
self.inner.flush()
}
// utility
//
fn write_byte(&mut self, b: u8) -> ::Result<()> {
self.inner.write_byte(b)
}
}
#[cfg(test)]
mod tests {
use protocol::{TBinaryOutputProtocol, TMessageIdentifier, TMessageType, TOutputProtocol};
use transport::{TBufferChannel, TIoChannel, WriteHalf};
use super::*;
#[test]
fn must_write_message_begin_with_prefixed_service_name() {
let mut o_prot = test_objects();
let ident = TMessageIdentifier::new("bar", TMessageType::Call, 2);
assert_success!(o_prot.write_message_begin(&ident));
#[cfg_attr(rustfmt, rustfmt::skip)]
let expected: [u8; 19] = [
0x80,
0x01, /* protocol identifier */
0x00,
0x01, /* message type */
0x00,
0x00,
0x00,
0x07,
0x66,
0x6F,
0x6F, /* "foo" */
0x3A, /* ":" */
0x62,
0x61,
0x72, /* "bar" */
0x00,
0x00,
0x00,
0x02 /* sequence number */,
];
assert_eq!(o_prot.inner.transport.write_bytes(), expected);
}
fn test_objects() -> TMultiplexedOutputProtocol<TBinaryOutputProtocol<WriteHalf<TBufferChannel>>>
{
let c = TBufferChannel::with_capacity(40, 40);
let (_, w_chan) = c.split().unwrap();
let prot = TBinaryOutputProtocol::new(w_chan, true);
TMultiplexedOutputProtocol::new("foo", prot)
}
}

View file

@ -0,0 +1,196 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::convert::Into;
use super::{
TFieldIdentifier, TInputProtocol, TListIdentifier, TMapIdentifier, TMessageIdentifier,
TSetIdentifier, TStructIdentifier,
};
use ProtocolErrorKind;
/// `TInputProtocol` required to use a `TMultiplexedProcessor`.
///
/// A `TMultiplexedProcessor` reads incoming message identifiers to determine to
/// which `TProcessor` requests should be forwarded. However, once read, those
/// message identifier bytes are no longer on the wire. Since downstream
/// processors expect to read message identifiers from the given input protocol
/// we need some way of supplying a `TMessageIdentifier` with the service-name
/// stripped. This implementation stores the received `TMessageIdentifier`
/// (without the service name) and passes it to the wrapped `TInputProtocol`
/// when `TInputProtocol::read_message_begin(...)` is called. It delegates all
/// other calls directly to the wrapped `TInputProtocol`.
///
/// This type **should not** be used by application code.
///
/// # Examples
///
/// Create and use a `TStoredInputProtocol`.
///
/// ```no_run
/// use thrift;
/// use thrift::protocol::{TInputProtocol, TMessageIdentifier, TMessageType, TOutputProtocol};
/// use thrift::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol, TStoredInputProtocol};
/// use thrift::server::TProcessor;
/// use thrift::transport::{TIoChannel, TTcpChannel};
///
/// // sample processor
/// struct ActualProcessor;
/// impl TProcessor for ActualProcessor {
/// fn process(
/// &self,
/// _: &mut TInputProtocol,
/// _: &mut TOutputProtocol
/// ) -> thrift::Result<()> {
/// unimplemented!()
/// }
/// }
/// let processor = ActualProcessor {};
///
/// // construct the shared transport
/// let mut channel = TTcpChannel::new();
/// channel.open("localhost:9090").unwrap();
///
/// let (i_chan, o_chan) = channel.split().unwrap();
///
/// // construct the actual input and output protocols
/// let mut i_prot = TBinaryInputProtocol::new(i_chan, true);
/// let mut o_prot = TBinaryOutputProtocol::new(o_chan, true);
///
/// // message identifier received from remote and modified to remove the service name
/// let new_msg_ident = TMessageIdentifier::new("service_call", TMessageType::Call, 1);
///
/// // construct the proxy input protocol
/// let mut proxy_i_prot = TStoredInputProtocol::new(&mut i_prot, new_msg_ident);
/// let res = processor.process(&mut proxy_i_prot, &mut o_prot);
/// ```
// FIXME: implement Debug
pub struct TStoredInputProtocol<'a> {
inner: &'a mut TInputProtocol,
message_ident: Option<TMessageIdentifier>,
}
impl<'a> TStoredInputProtocol<'a> {
/// Create a `TStoredInputProtocol` that delegates all calls other than
/// `TInputProtocol::read_message_begin(...)` to a `wrapped`
/// `TInputProtocol`. `message_ident` is the modified message identifier -
/// with service name stripped - that will be passed to
/// `wrapped.read_message_begin(...)`.
pub fn new(
wrapped: &mut TInputProtocol,
message_ident: TMessageIdentifier,
) -> TStoredInputProtocol {
TStoredInputProtocol {
inner: wrapped,
message_ident: message_ident.into(),
}
}
}
impl<'a> TInputProtocol for TStoredInputProtocol<'a> {
fn read_message_begin(&mut self) -> ::Result<TMessageIdentifier> {
self.message_ident.take().ok_or_else(|| {
::errors::new_protocol_error(
ProtocolErrorKind::Unknown,
"message identifier already read",
)
})
}
fn read_message_end(&mut self) -> ::Result<()> {
self.inner.read_message_end()
}
fn read_struct_begin(&mut self) -> ::Result<Option<TStructIdentifier>> {
self.inner.read_struct_begin()
}
fn read_struct_end(&mut self) -> ::Result<()> {
self.inner.read_struct_end()
}
fn read_field_begin(&mut self) -> ::Result<TFieldIdentifier> {
self.inner.read_field_begin()
}
fn read_field_end(&mut self) -> ::Result<()> {
self.inner.read_field_end()
}
fn read_bytes(&mut self) -> ::Result<Vec<u8>> {
self.inner.read_bytes()
}
fn read_bool(&mut self) -> ::Result<bool> {
self.inner.read_bool()
}
fn read_i8(&mut self) -> ::Result<i8> {
self.inner.read_i8()
}
fn read_i16(&mut self) -> ::Result<i16> {
self.inner.read_i16()
}
fn read_i32(&mut self) -> ::Result<i32> {
self.inner.read_i32()
}
fn read_i64(&mut self) -> ::Result<i64> {
self.inner.read_i64()
}
fn read_double(&mut self) -> ::Result<f64> {
self.inner.read_double()
}
fn read_string(&mut self) -> ::Result<String> {
self.inner.read_string()
}
fn read_list_begin(&mut self) -> ::Result<TListIdentifier> {
self.inner.read_list_begin()
}
fn read_list_end(&mut self) -> ::Result<()> {
self.inner.read_list_end()
}
fn read_set_begin(&mut self) -> ::Result<TSetIdentifier> {
self.inner.read_set_begin()
}
fn read_set_end(&mut self) -> ::Result<()> {
self.inner.read_set_end()
}
fn read_map_begin(&mut self) -> ::Result<TMapIdentifier> {
self.inner.read_map_begin()
}
fn read_map_end(&mut self) -> ::Result<()> {
self.inner.read_map_end()
}
// utility
//
fn read_byte(&mut self) -> ::Result<u8> {
self.inner.read_byte()
}
}