Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1 | (* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | *) |
| 19 | |
| 20 | unit Thrift.Configuration; |
| 21 | |
| 22 | interface |
| 23 | |
| 24 | uses |
| 25 | SysUtils, Generics.Collections, Generics.Defaults; |
| 26 | |
| 27 | const |
| 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 | |
| 34 | type |
| 35 | IThriftConfiguration = interface |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 36 | ['{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 Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 43 | |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 44 | property RecursionLimit : Integer read GetRecursionLimit write SetRecursionLimit; |
| 45 | property MaxFrameSize : Integer read GetMaxFrameSize write SetMaxFrameSize; |
| 46 | property MaxMessageSize : Integer read GetMaxMessageSize write SetMaxMessageSize; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 47 | end; |
| 48 | |
| 49 | |
| 50 | TThriftConfigurationImpl = class( TInterfacedObject, IThriftConfiguration) |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 51 | strict private |
| 52 | class procedure ValidateLimitArgument( const value : Integer); inline; |
| 53 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 54 | strict protected |
| 55 | FRecursionLimit : Cardinal; |
| 56 | FMaxFrameSize : Cardinal; |
| 57 | FMaxMessageSize : Cardinal; |
| 58 | |
| 59 | // IThriftConfiguration |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 60 | 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 Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 66 | |
| 67 | public |
| 68 | constructor Create; |
| 69 | end; |
| 70 | |
| 71 | |
| 72 | implementation |
| 73 | |
| 74 | |
| 75 | { TThriftConfigurationImpl } |
| 76 | |
| 77 | |
| 78 | constructor TThriftConfigurationImpl.Create; |
| 79 | begin |
| 80 | inherited Create; |
| 81 | |
| 82 | FRecursionLimit := DEFAULT_RECURSION_LIMIT; |
| 83 | FMaxFrameSize := DEFAULT_MAX_FRAME_SIZE; |
| 84 | FMaxMessageSize := DEFAULT_MAX_MESSAGE_SIZE; |
| 85 | end; |
| 86 | |
| 87 | |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 88 | class procedure TThriftConfigurationImpl.ValidateLimitArgument( const value : Integer); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 89 | begin |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 90 | if value <= 0 // zero makes not much sense either |
| 91 | then raise EArgumentOutOfRangeException.Create('Value must be positive'); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 92 | end; |
| 93 | |
| 94 | |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 95 | function TThriftConfigurationImpl.GetRecursionLimit: Integer; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 96 | begin |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 97 | result := FRecursionLimit and MAXINT; |
| 98 | ASSERT( result > 0); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 99 | end; |
| 100 | |
| 101 | |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 102 | procedure TThriftConfigurationImpl.SetRecursionLimit(const value: Integer); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 103 | begin |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 104 | ValidateLimitArgument( value); |
| 105 | ASSERT( value > 0); |
| 106 | FRecursionLimit := value and MAXINT; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 107 | end; |
| 108 | |
| 109 | |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 110 | function TThriftConfigurationImpl.GetMaxFrameSize: Integer; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 111 | begin |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 112 | result := FMaxFrameSize and MAXINT; |
| 113 | ASSERT( result > 0); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 114 | end; |
| 115 | |
| 116 | |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 117 | procedure TThriftConfigurationImpl.SetMaxFrameSize(const value: Integer); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 118 | begin |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 119 | ValidateLimitArgument( value); |
| 120 | ASSERT( value > 0); |
| 121 | FMaxFrameSize := value and MAXINT; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 122 | end; |
| 123 | |
| 124 | |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 125 | function TThriftConfigurationImpl.GetMaxMessageSize: Integer; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 126 | begin |
Jens Geyer | c854f64 | 2025-02-05 01:22:51 +0100 | [diff] [blame] | 127 | result := FMaxMessageSize and MAXINT; |
| 128 | ASSERT( result > 0); |
| 129 | end; |
| 130 | |
| 131 | |
| 132 | procedure TThriftConfigurationImpl.SetMaxMessageSize(const value: Integer); |
| 133 | begin |
| 134 | ValidateLimitArgument( value); |
| 135 | ASSERT( value > 0); |
| 136 | FMaxMessageSize := value and MAXINT; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 137 | end; |
| 138 | |
| 139 | |
| 140 | end. |