1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit WebSite.ServerSettings;
interface
uses
Boolean,
Dom.Element,
Object_.Base,
Socket.Address.Internet,
Text,
Uri;
type
TServerSettings = class(TBaseObject)
private
FUri: TUri;
FSocketAddress: TInternetSocketAddress;
FSecureSocketAddress: TInternetSocketAddress;
procedure SetSocketAddress(const Value: TInternetSocketAddress);
procedure SetSecureSocketAddress(const Value: TInternetSocketAddress);
public
SecureSocketEnabled: TBoolean;
destructor Destroy(); override;
function ToText(): TText; override;
procedure LoadFromConfiguration(Element: TDomElement);
procedure SetDefaultUri();
published
property Uri: TUri read FUri write FUri;
property SocketAddress: TInternetSocketAddress read FSocketAddress write SetSocketAddress;
property SecureSocketAddress: TInternetSocketAddress read FSecureSocketAddress write SetSecureSocketAddress;
end;
implementation
uses
Dom.Attribute,
Internet.Address;
{ TServerSettings }
destructor TServerSettings.Destroy();
begin
SocketAddress.ReleaseReference();
SecureSocketAddress.ReleaseReference();
inherited Destroy();
end;
procedure TServerSettings.LoadFromConfiguration(Element: TDomElement);
var
InternetAddress: TInternetAddress;
UriAttribute: TDomAttribute;
begin
InternetAddress := TInternetAddress.FromIp(Element.Attributes.Get('Ip').AsText());
SocketAddress := TInternetSocketAddress.Create(InternetAddress, Element.Attributes.Get('Port').AsInteger());
SecureSocketAddress := TInternetSocketAddress.Create(InternetAddress, Element.Attributes.GetOptional('SecurePort').AsIntegerWithDefault(443));
SecureSocketEnabled := Element.Attributes.GetOptional('Ssl').AsBooleanWithDefault();
UriAttribute := Element.Attributes.GetOptional('Uri');
if UriAttribute <> nil then
Uri := TUri.New(UriAttribute.AsText());
end;
procedure TServerSettings.SetDefaultUri();
var
Uri: TUri;
begin
Uri := TUri.New();
if SecureSocketEnabled then
begin
Uri.Scheme := 'https';
if SecureSocketAddress.Port <> 443 then
Uri.Authority.Port := SecureSocketAddress.Port;
end
else
begin
Uri.Scheme := 'http';
if SocketAddress.Port <> 80 then
Uri.Authority.Port := SocketAddress.Port;
end;
Uri.Authority.HostName := SocketAddress.InternetAddress.IpText;
Uri.Path.RootType := TUriRootType.Absolute;
Uri.Path.ResourceType := TUriResourceType.Directory;
Self.Uri := Uri;
end;
procedure TServerSettings.SetSecureSocketAddress(const Value: TInternetSocketAddress);
begin
if Value <> SecureSocketAddress then
begin
Value.AddReference();
SecureSocketAddress.ReleaseReference();
FSecureSocketAddress := Value;
end;
end;
procedure TServerSettings.SetSocketAddress(const Value: TInternetSocketAddress);
begin
if Value <> SocketAddress then
begin
Value.AddReference();
SocketAddress.ReleaseReference();
FSocketAddress := Value;
end;
end;
function TServerSettings.ToText(): TText;
begin
Result := SocketAddress.ToText() + '/' + SecureSocketAddress.ToText();
end;
end.
|