blob: d2c89d3407663e6c0618cc545ab8f4d721c52058 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001%%
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%%
David Reissac549552008-06-10 22:56:59 +000019
20-module(thrift_binary_protocol).
21
David Reiss6d0be722010-08-30 22:05:09 +000022-behaviour(thrift_protocol).
David Reissac549552008-06-10 22:56:59 +000023
24-include("thrift_constants.hrl").
25-include("thrift_protocol.hrl").
26
Sergei Elin45764092022-09-23 23:21:31 +030027-export([
28 new/1, new/2,
29 read/2,
30 write/2,
31 flush_transport/1,
32 close_transport/1,
David Reissfc427af2008-06-11 01:11:57 +000033
Sergei Elin45764092022-09-23 23:21:31 +030034 new_protocol_factory/2
35]).
David Reissac549552008-06-10 22:56:59 +000036
Sergei Elin45764092022-09-23 23:21:31 +030037-record(binary_protocol, {
38 transport :: term(),
39 strict_read = true :: boolean(),
40 strict_write = true :: boolean()
41}).
David Reissac549552008-06-10 22:56:59 +000042
43-define(VERSION_MASK, 16#FFFF0000).
44-define(VERSION_1, 16#80010000).
David Reiss914ebb42008-06-11 01:01:48 +000045-define(TYPE_MASK, 16#000000ff).
David Reissac549552008-06-10 22:56:59 +000046
47new(Transport) ->
David Reiss914ebb42008-06-11 01:01:48 +000048 new(Transport, _Options = []).
49
50new(Transport, Options) ->
Sergei Elin45764092022-09-23 23:21:31 +030051 State = #binary_protocol{transport = Transport},
David Reiss914ebb42008-06-11 01:01:48 +000052 State1 = parse_options(Options, State),
53 thrift_protocol:new(?MODULE, State1).
54
55parse_options([], State) ->
56 State;
57parse_options([{strict_read, Bool} | Rest], State) when is_boolean(Bool) ->
Sergei Elin45764092022-09-23 23:21:31 +030058 parse_options(Rest, State#binary_protocol{strict_read = Bool});
David Reiss914ebb42008-06-11 01:01:48 +000059parse_options([{strict_write, Bool} | Rest], State) when is_boolean(Bool) ->
Sergei Elin45764092022-09-23 23:21:31 +030060 parse_options(Rest, State#binary_protocol{strict_write = Bool}).
David Reissac549552008-06-10 22:56:59 +000061
David Reiss63f61262010-08-30 22:05:34 +000062flush_transport(This = #binary_protocol{transport = Transport}) ->
David Reiss639e1cf2010-08-30 22:05:43 +000063 {NewTransport, Result} = thrift_transport:flush(Transport),
64 {This#binary_protocol{transport = NewTransport}, Result}.
David Reissac549552008-06-10 22:56:59 +000065
David Reiss63f61262010-08-30 22:05:34 +000066close_transport(This = #binary_protocol{transport = Transport}) ->
David Reiss639e1cf2010-08-30 22:05:43 +000067 {NewTransport, Result} = thrift_transport:close(Transport),
68 {This#binary_protocol{transport = NewTransport}, Result}.
David Reissc11734e2008-06-11 00:59:48 +000069
David Reissac549552008-06-10 22:56:59 +000070%%%
71%%% instance methods
72%%%
73
David Reiss63f61262010-08-30 22:05:34 +000074write(This0, #protocol_message_begin{
Sergei Elin45764092022-09-23 23:21:31 +030075 name = Name,
76 type = Type,
77 seqid = Seqid
78}) ->
David Reiss63f61262010-08-30 22:05:34 +000079 case This0#binary_protocol.strict_write of
David Reiss914ebb42008-06-11 01:01:48 +000080 true ->
David Reiss63f61262010-08-30 22:05:34 +000081 {This1, ok} = write(This0, {i32, ?VERSION_1 bor Type}),
82 {This2, ok} = write(This1, {string, Name}),
83 {This3, ok} = write(This2, {i32, Seqid}),
84 {This3, ok};
David Reiss914ebb42008-06-11 01:01:48 +000085 false ->
David Reiss63f61262010-08-30 22:05:34 +000086 {This1, ok} = write(This0, {string, Name}),
87 {This2, ok} = write(This1, {byte, Type}),
88 {This3, ok} = write(This2, {i32, Seqid}),
89 {This3, ok}
90 end;
Sergei Elin45764092022-09-23 23:21:31 +030091write(This, message_end) ->
92 {This, ok};
David Reiss63f61262010-08-30 22:05:34 +000093write(This0, #protocol_field_begin{
Sergei Elin45764092022-09-23 23:21:31 +030094 name = _Name,
95 type = Type,
96 id = Id
97}) ->
David Reiss63f61262010-08-30 22:05:34 +000098 {This1, ok} = write(This0, {byte, Type}),
99 {This2, ok} = write(This1, {i16, Id}),
100 {This2, ok};
David Reissac549552008-06-10 22:56:59 +0000101write(This, field_stop) ->
David Reiss63f61262010-08-30 22:05:34 +0000102 write(This, {byte, ?tType_STOP});
Sergei Elin45764092022-09-23 23:21:31 +0300103write(This, field_end) ->
104 {This, ok};
David Reiss63f61262010-08-30 22:05:34 +0000105write(This0, #protocol_map_begin{
Sergei Elin45764092022-09-23 23:21:31 +0300106 ktype = Ktype,
107 vtype = Vtype,
108 size = Size
109}) ->
David Reiss63f61262010-08-30 22:05:34 +0000110 {This1, ok} = write(This0, {byte, Ktype}),
111 {This2, ok} = write(This1, {byte, Vtype}),
112 {This3, ok} = write(This2, {i32, Size}),
113 {This3, ok};
Sergei Elin45764092022-09-23 23:21:31 +0300114write(This, map_end) ->
115 {This, ok};
David Reiss63f61262010-08-30 22:05:34 +0000116write(This0, #protocol_list_begin{
Sergei Elin45764092022-09-23 23:21:31 +0300117 etype = Etype,
118 size = Size
119}) ->
David Reiss63f61262010-08-30 22:05:34 +0000120 {This1, ok} = write(This0, {byte, Etype}),
121 {This2, ok} = write(This1, {i32, Size}),
122 {This2, ok};
Sergei Elin45764092022-09-23 23:21:31 +0300123write(This, list_end) ->
124 {This, ok};
David Reiss63f61262010-08-30 22:05:34 +0000125write(This0, #protocol_set_begin{
Sergei Elin45764092022-09-23 23:21:31 +0300126 etype = Etype,
127 size = Size
128}) ->
David Reiss63f61262010-08-30 22:05:34 +0000129 {This1, ok} = write(This0, {byte, Etype}),
130 {This2, ok} = write(This1, {i32, Size}),
131 {This2, ok};
Sergei Elin45764092022-09-23 23:21:31 +0300132write(This, set_end) ->
133 {This, ok};
134write(This, #protocol_struct_begin{}) ->
135 {This, ok};
136write(This, struct_end) ->
137 {This, ok};
138write(This, {bool, true}) ->
139 write(This, {byte, 1});
140write(This, {bool, false}) ->
141 write(This, {byte, 0});
David Reissac549552008-06-10 22:56:59 +0000142write(This, {byte, Byte}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000143 write(This, <<Byte:8/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000144write(This, {i16, I16}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000145 write(This, <<I16:16/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000146write(This, {i32, I32}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000147 write(This, <<I32:32/big-signed>>);
David Reiss07a725f2008-06-10 22:57:59 +0000148write(This, {i64, I64}) ->
149 write(This, <<I64:64/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000150write(This, {double, Double}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000151 write(This, <<Double:64/big-signed-float>>);
David Reiss63f61262010-08-30 22:05:34 +0000152write(This0, {string, Str}) when is_list(Str) ->
153 {This1, ok} = write(This0, {i32, length(Str)}),
154 {This2, ok} = write(This1, list_to_binary(Str)),
155 {This2, ok};
David Reiss63f61262010-08-30 22:05:34 +0000156write(This0, {string, Bin}) when is_binary(Bin) ->
157 {This1, ok} = write(This0, {i32, size(Bin)}),
158 {This2, ok} = write(This1, Bin),
159 {This2, ok};
David Reiss914ebb42008-06-11 01:01:48 +0000160%% Data :: iolist()
David Reiss63f61262010-08-30 22:05:34 +0000161write(This = #binary_protocol{transport = Trans}, Data) ->
David Reiss639e1cf2010-08-30 22:05:43 +0000162 {NewTransport, Result} = thrift_transport:write(Trans, Data),
163 {This#binary_protocol{transport = NewTransport}, Result}.
David Reissac549552008-06-10 22:56:59 +0000164
165%%
166
David Reiss63f61262010-08-30 22:05:34 +0000167read(This0, message_begin) ->
168 {This1, Initial} = read(This0, ui32),
169 case Initial of
David Reiss914ebb42008-06-11 01:01:48 +0000170 {ok, Sz} when Sz band ?VERSION_MASK =:= ?VERSION_1 ->
171 %% we're at version 1
Sergei Elin45764092022-09-23 23:21:31 +0300172 {This2, {ok, Name}} = read(This1, string),
David Reiss63f61262010-08-30 22:05:34 +0000173 {This3, {ok, SeqId}} = read(This2, i32),
Sergei Elin45764092022-09-23 23:21:31 +0300174 Type = Sz band ?TYPE_MASK,
175 {This3, #protocol_message_begin{
176 name = binary_to_list(Name),
177 type = Type,
178 seqid = SeqId
179 }};
David Reiss914ebb42008-06-11 01:01:48 +0000180 {ok, Sz} when Sz < 0 ->
181 %% there's a version number but it's unexpected
David Reiss63f61262010-08-30 22:05:34 +0000182 {This1, {error, {bad_binary_protocol_version, Sz}}};
David Reiss5ed313d2010-08-30 22:05:57 +0000183 {ok, _Sz} when This1#binary_protocol.strict_read =:= true ->
David Reiss914ebb42008-06-11 01:01:48 +0000184 %% strict_read is true and there's no version header; that's an error
David Reiss63f61262010-08-30 22:05:34 +0000185 {This1, {error, no_binary_protocol_version}};
David Reiss63f61262010-08-30 22:05:34 +0000186 {ok, Sz} when This1#binary_protocol.strict_read =:= false ->
David Reiss914ebb42008-06-11 01:01:48 +0000187 %% strict_read is false, so just read the old way
Sergei Elin45764092022-09-23 23:21:31 +0300188 {This2, {ok, Name}} = read_data(This1, Sz),
189 {This3, {ok, Type}} = read(This2, byte),
David Reiss63f61262010-08-30 22:05:34 +0000190 {This4, {ok, SeqId}} = read(This3, i32),
Sergei Elin45764092022-09-23 23:21:31 +0300191 {This4, #protocol_message_begin{
192 name = binary_to_list(Name),
193 type = Type,
194 seqid = SeqId
195 }};
David Reiss63f61262010-08-30 22:05:34 +0000196 Else ->
197 {This1, Else}
David Reissac549552008-06-10 22:56:59 +0000198 end;
Sergei Elin45764092022-09-23 23:21:31 +0300199read(This, message_end) ->
200 {This, ok};
201read(This, struct_begin) ->
202 {This, ok};
203read(This, struct_end) ->
204 {This, ok};
David Reiss63f61262010-08-30 22:05:34 +0000205read(This0, field_begin) ->
206 {This1, Result} = read(This0, byte),
207 case Result of
David Reissac549552008-06-10 22:56:59 +0000208 {ok, Type = ?tType_STOP} ->
David Reiss63f61262010-08-30 22:05:34 +0000209 {This1, #protocol_field_begin{type = Type}};
David Reissac549552008-06-10 22:56:59 +0000210 {ok, Type} ->
David Reiss63f61262010-08-30 22:05:34 +0000211 {This2, {ok, Id}} = read(This1, i16),
Sergei Elin45764092022-09-23 23:21:31 +0300212 {This2, #protocol_field_begin{
213 type = Type,
214 id = Id
215 }}
David Reissac549552008-06-10 22:56:59 +0000216 end;
Sergei Elin45764092022-09-23 23:21:31 +0300217read(This, field_end) ->
218 {This, ok};
David Reiss63f61262010-08-30 22:05:34 +0000219read(This0, map_begin) ->
220 {This1, {ok, Ktype}} = read(This0, byte),
221 {This2, {ok, Vtype}} = read(This1, byte),
Sergei Elin45764092022-09-23 23:21:31 +0300222 {This3, {ok, Size}} = read(This2, i32),
223 {This3, #protocol_map_begin{
224 ktype = Ktype,
225 vtype = Vtype,
226 size = Size
227 }};
228read(This, map_end) ->
229 {This, ok};
David Reiss63f61262010-08-30 22:05:34 +0000230read(This0, list_begin) ->
231 {This1, {ok, Etype}} = read(This0, byte),
Sergei Elin45764092022-09-23 23:21:31 +0300232 {This2, {ok, Size}} = read(This1, i32),
233 {This2, #protocol_list_begin{
234 etype = Etype,
235 size = Size
236 }};
237read(This, list_end) ->
238 {This, ok};
David Reiss63f61262010-08-30 22:05:34 +0000239read(This0, set_begin) ->
240 {This1, {ok, Etype}} = read(This0, byte),
Sergei Elin45764092022-09-23 23:21:31 +0300241 {This2, {ok, Size}} = read(This1, i32),
242 {This2, #protocol_set_begin{
243 etype = Etype,
244 size = Size
245 }};
246read(This, set_end) ->
247 {This, ok};
David Reiss63f61262010-08-30 22:05:34 +0000248read(This0, field_stop) ->
249 {This1, {ok, ?tType_STOP}} = read(This0, byte),
250 {This1, ok};
David Reissac549552008-06-10 22:56:59 +0000251%%
252
David Reiss63f61262010-08-30 22:05:34 +0000253read(This0, bool) ->
254 {This1, Result} = read(This0, byte),
255 case Result of
256 {ok, Byte} -> {This1, {ok, Byte /= 0}};
257 Else -> {This1, Else}
Kevin Clark9a863ee2009-03-24 16:04:36 +0000258 end;
David Reiss63f61262010-08-30 22:05:34 +0000259read(This0, byte) ->
260 {This1, Bytes} = read_data(This0, 1),
261 case Bytes of
262 {ok, <<Val:8/integer-signed-big, _/binary>>} -> {This1, {ok, Val}};
263 Else -> {This1, Else}
David Reissac549552008-06-10 22:56:59 +0000264 end;
David Reiss63f61262010-08-30 22:05:34 +0000265read(This0, i16) ->
266 {This1, Bytes} = read_data(This0, 2),
267 case Bytes of
268 {ok, <<Val:16/integer-signed-big, _/binary>>} -> {This1, {ok, Val}};
269 Else -> {This1, Else}
David Reissac549552008-06-10 22:56:59 +0000270 end;
David Reiss63f61262010-08-30 22:05:34 +0000271read(This0, i32) ->
272 {This1, Bytes} = read_data(This0, 4),
273 case Bytes of
274 {ok, <<Val:32/integer-signed-big, _/binary>>} -> {This1, {ok, Val}};
275 Else -> {This1, Else}
David Reissac549552008-06-10 22:56:59 +0000276 end;
David Reiss9ad6a312008-06-11 01:12:45 +0000277%% unsigned ints aren't used by thrift itself, but it's used for the parsing
278%% of the packet version header. Without this special function BEAM works fine
279%% but hipe thinks it received a bad version header.
David Reiss63f61262010-08-30 22:05:34 +0000280read(This0, ui32) ->
281 {This1, Bytes} = read_data(This0, 4),
282 case Bytes of
283 {ok, <<Val:32/integer-unsigned-big, _/binary>>} -> {This1, {ok, Val}};
284 Else -> {This1, Else}
David Reiss9ad6a312008-06-11 01:12:45 +0000285 end;
David Reiss63f61262010-08-30 22:05:34 +0000286read(This0, i64) ->
287 {This1, Bytes} = read_data(This0, 8),
288 case Bytes of
289 {ok, <<Val:64/integer-signed-big, _/binary>>} -> {This1, {ok, Val}};
290 Else -> {This1, Else}
David Reissac549552008-06-10 22:56:59 +0000291 end;
David Reiss63f61262010-08-30 22:05:34 +0000292read(This0, double) ->
293 {This1, Bytes} = read_data(This0, 8),
294 case Bytes of
295 {ok, <<Val:64/float-signed-big, _/binary>>} -> {This1, {ok, Val}};
296 Else -> {This1, Else}
David Reissac549552008-06-10 22:56:59 +0000297 end;
David Reiss4ec777e2008-06-11 01:01:29 +0000298% returns a binary directly, call binary_to_list if necessary
David Reiss63f61262010-08-30 22:05:34 +0000299read(This0, string) ->
Sergei Elin45764092022-09-23 23:21:31 +0300300 {This1, {ok, Sz}} = read(This0, i32),
David Reiss63f61262010-08-30 22:05:34 +0000301 read_data(This1, Sz).
David Reissac549552008-06-10 22:56:59 +0000302
David Reiss63f61262010-08-30 22:05:34 +0000303-spec read_data(#binary_protocol{}, non_neg_integer()) ->
304 {#binary_protocol{}, {ok, binary()} | {error, _Reason}}.
Sergei Elin45764092022-09-23 23:21:31 +0300305read_data(This, 0) ->
306 {This, {ok, <<>>}};
David Reiss63f61262010-08-30 22:05:34 +0000307read_data(This = #binary_protocol{transport = Trans}, Len) when is_integer(Len) andalso Len > 0 ->
David Reiss639e1cf2010-08-30 22:05:43 +0000308 {NewTransport, Result} = thrift_transport:read(Trans, Len),
309 {This#binary_protocol{transport = NewTransport}, Result}.
David Reissfc427af2008-06-11 01:11:57 +0000310
David Reissfc427af2008-06-11 01:11:57 +0000311%%%% FACTORY GENERATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312
Sergei Elin45764092022-09-23 23:21:31 +0300313-record(tbp_opts, {
314 strict_read = true :: boolean(),
315 strict_write = true :: boolean()
316}).
David Reissfc427af2008-06-11 01:11:57 +0000317
318parse_factory_options([], Opts) ->
319 Opts;
320parse_factory_options([{strict_read, Bool} | Rest], Opts) when is_boolean(Bool) ->
Sergei Elin45764092022-09-23 23:21:31 +0300321 parse_factory_options(Rest, Opts#tbp_opts{strict_read = Bool});
David Reissfc427af2008-06-11 01:11:57 +0000322parse_factory_options([{strict_write, Bool} | Rest], Opts) when is_boolean(Bool) ->
Sergei Elin45764092022-09-23 23:21:31 +0300323 parse_factory_options(Rest, Opts#tbp_opts{strict_write = Bool}).
David Reissfc427af2008-06-11 01:11:57 +0000324
325%% returns a (fun() -> thrift_protocol())
326new_protocol_factory(TransportFactory, Options) ->
327 ParsedOpts = parse_factory_options(Options, #tbp_opts{}),
328 F = fun() ->
Sergei Elin45764092022-09-23 23:21:31 +0300329 case TransportFactory() of
330 {ok, Transport} ->
331 thrift_binary_protocol:new(
332 Transport,
333 [
334 {strict_read, ParsedOpts#tbp_opts.strict_read},
335 {strict_write, ParsedOpts#tbp_opts.strict_write}
336 ]
337 );
338 {error, Error} ->
339 {error, Error}
340 end
341 end,
David Reissfc427af2008-06-11 01:11:57 +0000342 {ok, F}.