blob: 562dba16fefb68f9b0e3052ffd8f42465fde9cf3 [file] [log] [blame]
Jens Geyera019cda2019-11-09 23:24:52 +01001(*
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
20unit Thrift.Configuration;
21
22interface
23
24uses
25 SysUtils, Generics.Collections, Generics.Defaults;
26
27const
28 DEFAULT_RECURSION_LIMIT = 64;
29 DEFAULT_MAX_MESSAGE_SIZE = 100 * 1024 * 1024; // 100 MB
30 DEFAULT_MAX_FRAME_SIZE = 16384000; // this value is used consistently across all Thrift libraries
31
32 DEFAULT_THRIFT_TIMEOUT = 5 * 1000; // ms
33
34type
35 IThriftConfiguration = interface
Jens Geyerc854f642025-02-05 01:22:51 +010036 ['{666F7848-744A-4746-BDD5-43DC9B1D5520}']
37 function GetRecursionLimit : Integer;
38 procedure SetRecursionLimit( const value : Integer);
39 function GetMaxFrameSize : Integer;
40 procedure SetMaxFrameSize( const value : Integer);
41 function GetMaxMessageSize : Integer;
42 procedure SetMaxMessageSize( const value : Integer);
Jens Geyera019cda2019-11-09 23:24:52 +010043
Jens Geyerc854f642025-02-05 01:22:51 +010044 property RecursionLimit : Integer read GetRecursionLimit write SetRecursionLimit;
45 property MaxFrameSize : Integer read GetMaxFrameSize write SetMaxFrameSize;
46 property MaxMessageSize : Integer read GetMaxMessageSize write SetMaxMessageSize;
Jens Geyera019cda2019-11-09 23:24:52 +010047 end;
48
49
50 TThriftConfigurationImpl = class( TInterfacedObject, IThriftConfiguration)
Jens Geyerc854f642025-02-05 01:22:51 +010051 strict private
52 class procedure ValidateLimitArgument( const value : Integer); inline;
53
Jens Geyera019cda2019-11-09 23:24:52 +010054 strict protected
55 FRecursionLimit : Cardinal;
56 FMaxFrameSize : Cardinal;
57 FMaxMessageSize : Cardinal;
58
59 // IThriftConfiguration
Jens Geyerc854f642025-02-05 01:22:51 +010060 function GetRecursionLimit : Integer;
61 procedure SetRecursionLimit( const value : Integer);
62 function GetMaxFrameSize : Integer;
63 procedure SetMaxFrameSize( const value : Integer);
64 function GetMaxMessageSize : Integer;
65 procedure SetMaxMessageSize( const value : Integer);
Jens Geyera019cda2019-11-09 23:24:52 +010066
67 public
68 constructor Create;
69 end;
70
71
72implementation
73
74
75{ TThriftConfigurationImpl }
76
77
78constructor TThriftConfigurationImpl.Create;
79begin
80 inherited Create;
81
82 FRecursionLimit := DEFAULT_RECURSION_LIMIT;
83 FMaxFrameSize := DEFAULT_MAX_FRAME_SIZE;
84 FMaxMessageSize := DEFAULT_MAX_MESSAGE_SIZE;
85end;
86
87
Jens Geyerc854f642025-02-05 01:22:51 +010088class procedure TThriftConfigurationImpl.ValidateLimitArgument( const value : Integer);
Jens Geyera019cda2019-11-09 23:24:52 +010089begin
Jens Geyerc854f642025-02-05 01:22:51 +010090 if value <= 0 // zero makes not much sense either
91 then raise EArgumentOutOfRangeException.Create('Value must be positive');
Jens Geyera019cda2019-11-09 23:24:52 +010092end;
93
94
Jens Geyerc854f642025-02-05 01:22:51 +010095function TThriftConfigurationImpl.GetRecursionLimit: Integer;
Jens Geyera019cda2019-11-09 23:24:52 +010096begin
Jens Geyerc854f642025-02-05 01:22:51 +010097 result := FRecursionLimit and MAXINT;
98 ASSERT( result > 0);
Jens Geyera019cda2019-11-09 23:24:52 +010099end;
100
101
Jens Geyerc854f642025-02-05 01:22:51 +0100102procedure TThriftConfigurationImpl.SetRecursionLimit(const value: Integer);
Jens Geyera019cda2019-11-09 23:24:52 +0100103begin
Jens Geyerc854f642025-02-05 01:22:51 +0100104 ValidateLimitArgument( value);
105 ASSERT( value > 0);
106 FRecursionLimit := value and MAXINT;
Jens Geyera019cda2019-11-09 23:24:52 +0100107end;
108
109
Jens Geyerc854f642025-02-05 01:22:51 +0100110function TThriftConfigurationImpl.GetMaxFrameSize: Integer;
Jens Geyera019cda2019-11-09 23:24:52 +0100111begin
Jens Geyerc854f642025-02-05 01:22:51 +0100112 result := FMaxFrameSize and MAXINT;
113 ASSERT( result > 0);
Jens Geyera019cda2019-11-09 23:24:52 +0100114end;
115
116
Jens Geyerc854f642025-02-05 01:22:51 +0100117procedure TThriftConfigurationImpl.SetMaxFrameSize(const value: Integer);
Jens Geyera019cda2019-11-09 23:24:52 +0100118begin
Jens Geyerc854f642025-02-05 01:22:51 +0100119 ValidateLimitArgument( value);
120 ASSERT( value > 0);
121 FMaxFrameSize := value and MAXINT;
Jens Geyera019cda2019-11-09 23:24:52 +0100122end;
123
124
Jens Geyerc854f642025-02-05 01:22:51 +0100125function TThriftConfigurationImpl.GetMaxMessageSize: Integer;
Jens Geyera019cda2019-11-09 23:24:52 +0100126begin
Jens Geyerc854f642025-02-05 01:22:51 +0100127 result := FMaxMessageSize and MAXINT;
128 ASSERT( result > 0);
129end;
130
131
132procedure TThriftConfigurationImpl.SetMaxMessageSize(const value: Integer);
133begin
134 ValidateLimitArgument( value);
135 ASSERT( value > 0);
136 FMaxMessageSize := value and MAXINT;
Jens Geyera019cda2019-11-09 23:24:52 +0100137end;
138
139
140end.