forked from ricardoelices/TGBotLazarus
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtgfclhttpclientbroker.pas
417 lines (359 loc) · 11.3 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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 FilesFormPost(const AURL: string; FormData: TStrings; const AFiles: TStrings): String; override;
function StreamsFormPost(const AURL: string; FormData: TStrings; const AFiles: TStrings): String; 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;
{$IF FPC_FULLVERSION <= 30004}
{ TRawByteStringStream }
TRawByteStringStream = Class(TBytesStream)
public
Constructor Create (const aData : RawByteString); overload;
function DataString: RawByteString;
function ReadString(Count: Longint): RawByteString;
procedure WriteString(const AString: RawByteString);
end;
{$ENDIF}
implementation
uses
{$IFDEF ExplSSL}sslsockets, fpopenssl{$ENDIF}
{$IFDEF SSLOpenSockets}opensslsockets{$endif}
;
Const
CRLF = #13#10;
{$IF FPC_FULLVERSION <= 30004}
{ TRawByteStringStream }
constructor TRawByteStringStream.Create(const aData: RawByteString);
begin
Inherited Create;
If Length(aData)>0 then
begin
WriteBuffer(aData[1],Length(aData));
Position:=0;
end;
end;
function TRawByteStringStream.DataString: RawByteString;
begin
Result:='';
SetLength(Result,Size);
if Size>0 then
Move(Memory^, Result[1], Size);
end;
function TRawByteStringStream.ReadString(Count: Longint): RawByteString;
Var
NewLen : Longint;
begin
NewLen:=Size-Position;
If NewLen>Count then NewLen:=Count;
Result:='';
if NewLen>0 then
begin
SetLength(Result, NewLen);
Move(Bytes[Position],Result[1],NewLen);
Position:=Position+Newlen;
end;
end;
procedure TRawByteStringStream.WriteString(const AString: RawByteString);
begin
if Length(AString)>0 then
WriteBuffer(AString[1],Length(AString));
end;
{$ENDIF}
{ TFCLHTTPClient }
function TFCLHTTPClient.FilesFormPost(const AURL: string; FormData: TStrings; const AFiles: TStrings): String;
Var
S, Sep : string;
SS : TRawByteStringStream;
I: Integer;
N,V, aFieldName, aFileName: String;
F: TFileStream;
begin
Sep:=Format('%.8x_multipart_boundary',[Random($ffffff)]);
AddHeader('Content-Type','multipart/form-data; boundary='+Sep);
SS:=TRawByteStringStream.Create();
try
if (FormData<>Nil) then
for I:=0 to FormData.Count -1 do
begin
// not url encoded
FormData.GetNameValue(I,N,V);
S :='--'+Sep+CRLF;
S:=S+Format('Content-Disposition: form-data; name="%s"'+CRLF+CRLF+'%s'+CRLF,[N, V]);
SS.WriteBuffer(S[1],Length(S));
end;
if (AFiles<>Nil) then
for I:=0 to AFiles.Count-1 do
begin
AFiles.GetNameValue(I,aFieldName,aFileName);
S:='--'+Sep+CRLF;
s:=s+Format('Content-Disposition: form-data; name="%s"; filename="%s"'+CRLF,
[aFieldName,ExtractFileName(aFileName)]);
s:=s+'Content-Type: application/octet-string'+CRLF+CRLF;
SS.WriteBuffer(S[1],Length(S));
F:=TFileStream.Create(aFileName,fmOpenRead or fmShareDenyWrite);
try
F.Seek(0, soFromBeginning);
SS.CopyFrom(F,F.Size);
S:=CRLF;
SS.WriteBuffer(S[1],Length(S));
finally
F.Free;
end;
end;
S:='--'+Sep+'--'+CRLF;
SS.WriteBuffer(S[1],Length(S));
SS.Position:=0;
RequestBody:=SS;
Result:=Post(AURL);
finally
RequestBody:=Nil;
SS.Free;
end;
end;
function TFCLHTTPClient.StreamsFormPost(const AURL: string; FormData: TStrings;
const AFiles: TStrings): String;
Var
S, Sep : string;
SS : TRawByteStringStream;
I: Integer;
N,V, aFieldName, aFileName: String;
F: TStream;
begin
Sep:=Format('%.8x_multipart_boundary',[Random($ffffff)]);
AddHeader('Content-Type','multipart/form-data; boundary='+Sep);
SS:=TRawByteStringStream.Create();
try
if (FormData<>Nil) then
for I:=0 to FormData.Count -1 do
begin
// not url encoded
FormData.GetNameValue(I,N,V);
S :='--'+Sep+CRLF;
S:=S+Format('Content-Disposition: form-data; name="%s"'+CRLF+CRLF+'%s'+CRLF,[N, V]);
SS.WriteBuffer(S[1],Length(S));
end;
if (AFiles<>Nil) then
for I:=0 to AFiles.Count-1 do
begin
if not (AFiles.Objects[i] is TStream) then continue;
AFiles.GetNameValue(I,aFieldName,aFileName);
S:='--'+Sep+CRLF;
s:=s+Format('Content-Disposition: form-data; name="%s"; filename="%s"'+CRLF,
[aFieldName,ExtractFileName(aFileName)]);
s:=s+'Content-Type: application/octet-string'+CRLF+CRLF;
SS.WriteBuffer(S[1],Length(S));
F:=AFiles.Objects[i] as TStream;
F.Seek(0, soFromBeginning);
SS.CopyFrom(F,F.Size);
S:=CRLF;
SS.WriteBuffer(S[1],Length(S));
end;
S:='--'+Sep+'--'+CRLF;
SS.WriteBuffer(S[1],Length(S));
SS.Position:=0;
RequestBody:=SS;
Result:=Post(AURL);
finally
RequestBody:=Nil;
SS.Free;
end;
end;
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.