forked from Al-Muhandis/fp-telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgfclhttpclientbroker.pas
253 lines (209 loc) · 6.7 KB
/
tgfclhttpclientbroker.pas
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
unit tgfclhttpclientbroker;
{$mode objfpc}{$H+}
interface
{$IF FPC_FULLVERSION < 30200}{$DEFINE ExplSSL}{$else}{$DEFINE SSLOpenSockets}{$ENDIF}
uses
Classes, SysUtils{$IFDEF ExplSSL}, ssockets{$ENDIF}, tgbasehttpclient, fphttpclient;
type
{ TFCLHTTPClient }
TFCLHTTPClient=class(TBaseHTTPClient)
private
FHTTPClient: TFPHTTPClient;
procedure PrepareHeaders; {$IFDEF ExplSSL}
procedure HttpClientGetSocketHandler(Sender: TObject; const {%H-}UseSSL: Boolean;
out {%H-}AHandler: TSocketHandler);{$ENDIF}
protected
function GetAllowRedirect: Boolean; override;
function GetCookies: TStrings; override;
function GetHTTPProxyHost: String; override;
function GetHTTPProxyPassword: String; override;
function GetHTTPProxyPort: Word; override;
function GetHTTPProxyUsername: String; override;
function GetInternalHTTPClient: TObject; override;
function GetIOTimeout: Integer; override;
function GetRequestBody: TStream; override;
function GetRequestHeaders: TStrings; override;
function GetResponseHeaders: TStrings; override;
function GetResponseStatusCode: Integer; override;
function GetResponseStatusText: String; override;
procedure SetAllowRedirect(AValue: Boolean); override;
procedure SetCookies(AValue: TStrings); override;
procedure SetHTTPProxyHost(AValue: String); override;
procedure SetHTTPProxyPassword(AValue: String); override;
procedure SetHTTPProxyPort(AValue: Word); override;
procedure SetHTTPProxyUsername(AValue: String); override;
procedure SetIOTimeout(AValue: Integer); override;
procedure SetRequestBody(AValue: TStream); override;
procedure SetRequestHeaders(AValue: TStrings); override;
public
procedure AddHeader(const AHeader, AValue: String); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
class function EncodeUrlElement(S: String): String; override;
procedure FileFormPost(const AURL: string; FormData: TStrings; AFieldName, AFileName: string;
const Response: TStream); override;
function FormPost(const URL: string; FormData: TStrings): String; override;
function Get(const AUrl: String): String; override;
function Post(const URL: string): String; override;
procedure StreamFormPost(const AURL: string; FormData: TStrings; const AFieldName,
AFileName: string; const AStream: TStream; const Response: TStream); override;
end;
implementation
uses
{$IFDEF ExplSSL}sslsockets, fpopenssl{$ENDIF}
{$IFDEF SSLOpenSockets}opensslsockets{$endif}
;
{ TFCLHTTPClient }
procedure TFCLHTTPClient.PrepareHeaders;
begin
FHTTPClient.AddHeader('User-Agent', UserAgent);
end;
{$IFDEF ExplSSL}
procedure TFCLHTTPClient.HttpClientGetSocketHandler(Sender: TObject;
const UseSSL: Boolean; out AHandler: TSocketHandler);
begin
{$IFDEF LINUX}
if UseSSL then begin
AHandler:=TSSLSocketHandler.Create;
TSSLSocketHandler(AHandler).SSLType:=stTLSv1_2; // <--
end;
{$ENDIF}
end;
{$ENDIF}
function TFCLHTTPClient.GetAllowRedirect: Boolean;
begin
Result:=FHTTPClient.AllowRedirect;
end;
function TFCLHTTPClient.GetCookies: TStrings;
begin
Result:=FHTTPClient.Cookies;
end;
function TFCLHTTPClient.GetHTTPProxyHost: String;
begin
Result:=FHTTPClient.Proxy.Host;
end;
function TFCLHTTPClient.GetHTTPProxyPassword: String;
begin
Result:=FHTTPClient.Proxy.Password;
end;
function TFCLHTTPClient.GetHTTPProxyPort: Word;
begin
Result:=FHTTPClient.Proxy.Port;
end;
function TFCLHTTPClient.GetHTTPProxyUsername: String;
begin
Result:=FHTTPClient.Proxy.UserName;
end;
function TFCLHTTPClient.GetInternalHTTPClient: TObject;
begin
Result:=FHTTPClient;
end;
function TFCLHTTPClient.GetIOTimeout: Integer;
begin
Result:=FHTTPClient.IOTimeout;
end;
function TFCLHTTPClient.GetRequestBody: TStream;
begin
Result:=FHTTPClient.RequestBody;
end;
function TFCLHTTPClient.GetRequestHeaders: TStrings;
begin
Result:=FHTTPClient.RequestHeaders;
end;
function TFCLHTTPClient.GetResponseHeaders: TStrings;
begin
Result:=FHTTPClient.ResponseHeaders;
end;
function TFCLHTTPClient.GetResponseStatusCode: Integer;
begin
Result:=FHTTPClient.ResponseStatusCode;
end;
function TFCLHTTPClient.GetResponseStatusText: String;
begin
Result:=FHTTPClient.ResponseStatusText;
end;
procedure TFCLHTTPClient.SetAllowRedirect(AValue: Boolean);
begin
FHTTPClient.AllowRedirect:=AValue;
end;
procedure TFCLHTTPClient.SetCookies(AValue: TStrings);
begin
FHTTPClient.Cookies:=AValue;
end;
procedure TFCLHTTPClient.SetHTTPProxyHost(AValue: String);
begin
FHTTPClient.Proxy.Host:=AValue;
end;
procedure TFCLHTTPClient.SetHTTPProxyPassword(AValue: String);
begin
FHTTPClient.Proxy.Password:=AValue;
end;
procedure TFCLHTTPClient.SetHTTPProxyPort(AValue: Word);
begin
FHTTPClient.Proxy.Port:=AValue;
end;
procedure TFCLHTTPClient.SetHTTPProxyUsername(AValue: String);
begin
FHTTPClient.Proxy.UserName:=AValue;
end;
procedure TFCLHTTPClient.SetIOTimeout(AValue: Integer);
begin
FHTTPClient.IOTimeout:=AValue;
end;
procedure TFCLHTTPClient.SetRequestBody(AValue: TStream);
begin
FHTTPClient.RequestBody:=AValue;
end;
procedure TFCLHTTPClient.SetRequestHeaders(AValue: TStrings);
begin
FHTTPClient.RequestHeaders:=AValue;
end;
procedure TFCLHTTPClient.AddHeader(const AHeader, AValue: String);
begin
FHTTPClient.AddHeader(AHeader, AValue);
end;
constructor TFCLHTTPClient.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FHTTPClient:=TFPHTTPClient.Create(AOwner);
{$IFDEF ExplSSL}FHTTPClient.OnGetSocketHandler:=@HttpClientGetSocketHandler;{$ENDIF}
end;
destructor TFCLHTTPClient.Destroy;
begin
FreeAndNil(FHTTPClient);
inherited Destroy;
end;
class function TFCLHTTPClient.EncodeUrlElement(S: String): String;
begin
Result:=fphttpclient.EncodeURLElement(S);
end;
procedure TFCLHTTPClient.FileFormPost(const AURL: string; FormData: TStrings; AFieldName,
AFileName: string; const Response: TStream);
begin
FHTTPClient.FileFormPost(AURL, FormData, AFieldName, AFileName, Response);
end;
function TFCLHTTPClient.FormPost(const URL: string; FormData: TStrings
): String;
begin
PrepareHeaders;
Result:=FHTTPClient.FormPost(URL, FormData);
end;
function TFCLHTTPClient.Get(const AUrl: String): String;
begin
PrepareHeaders;
Result:=FHTTPClient.Get(AUrl);
end;
function TFCLHTTPClient.Post(const URL: string): String;
begin
PrepareHeaders;
Result:=FHTTPClient.Post(URL);
end;
procedure TFCLHTTPClient.StreamFormPost(const AURL: string; FormData: TStrings; const AFieldName,
AFileName: string; const AStream: TStream; const Response: TStream);
begin
FHTTPClient.StreamFormPost(AURL, FormData, AFieldName, AFileName, AStream, Response);
end;
initialization
TFCLHTTPClient.UnregisterClientClass;
TFCLHTTPClient.RegisterClientClass;
end.