Skip to content

Commit 5a31be5

Browse files
committed
1. ICurlForm → ICurlCustomForm + ICurlForm
2. ICurlField — fixed an interface
1 parent e9289b4 commit 5a31be5

File tree

5 files changed

+132
-121
lines changed

5 files changed

+132
-121
lines changed

Src/Curl.Easy.pas

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,33 @@ interface
7676
procedure SetFollowLocation(aData : boolean);
7777

7878
/// Gets/sets form data
79-
procedure SetForm(aForm : ICurlForm);
80-
function GetForm : ICurlForm;
79+
procedure SetForm(aForm : ICurlCustomForm);
80+
function GetForm : ICurlCustomForm;
8181

82-
/// For all these options the object stores a reference to an ICurlSList
82+
/// For all these options the object stores a reference to an ICurlCustomSList
8383
/// for itself.
8484

8585
/// This points to a linked list of headers. This
8686
/// list is also used for RTSP.
87-
procedure SetCustomHeaders(v : ICurlSList);
87+
procedure SetCustomHeaders(v : ICurlCustomSList);
8888
/// send linked-list of post-transfer QUOTE commands
89-
procedure SetPostQuote(v : ICurlSList);
89+
procedure SetPostQuote(v : ICurlCustomSList);
9090
/// Provide a pointer to a curl_slist with variables to pass to the telnet
9191
/// negotiations. The variables should be in the format <option=value>.
9292
/// libcurl supports the options 'TTYPE', 'XDISPLOC' and 'NEW_ENV'.
9393
/// See the TELNET standard for details.
94-
procedure SetTelnetOptions(v : ICurlSList);
94+
procedure SetTelnetOptions(v : ICurlCustomSList);
9595
/// send linked-list of pre-transfer QUOTE commands
96-
procedure SetPreQuote(v : ICurlSList);
96+
procedure SetPreQuote(v : ICurlCustomSList);
9797
/// Set aliases for HTTP 200 in the HTTP Response header
98-
procedure SetHttp200Aliases(v : ICurlSList);
98+
procedure SetHttp200Aliases(v : ICurlCustomSList);
9999
/// set the SMTP mail receiver(s)
100-
procedure SetMailRcpt(v : ICurlSList);
100+
procedure SetMailRcpt(v : ICurlCustomSList);
101101
/// send linked-list of name:port:address sets
102-
procedure SetResolveList(v : ICurlSList);
102+
procedure SetResolveList(v : ICurlCustomSList);
103103
/// This points to a linked list of headers used for proxy requests only,
104104
/// struct curl_slist kind
105-
procedure SetProxyHeader(v : ICurlSList);
105+
procedure SetProxyHeader(v : ICurlCustomSList);
106106

107107
/// Performs the action.
108108
/// Actually does RaiseIf(PerformNe).
@@ -140,22 +140,22 @@ interface
140140
/// should bother about destruction for himself.
141141
function Clone : ICurl;
142142

143-
property Form : ICurlForm read GetForm write SetForm;
143+
property Form : ICurlCustomForm read GetForm write SetForm;
144144
end;
145145

146146
TEasyCurlImpl = class (TInterfacedObject, ICurl)
147147
private
148148
fHandle : TCurlHandle;
149149
fCustomHeaders, fPostQuote, fTelnetOptions, fPreQuote,
150-
fHttp200Aliases, fMailRcpt, fResolveList, fProxyHeader : ICurlSList;
151-
fForm : ICurlForm;
150+
fHttp200Aliases, fMailRcpt, fResolveList, fProxyHeader : ICurlCustomSList;
151+
fForm : ICurlCustomForm;
152152
// We won’t save a few bytes of memory; repeatable code is more important.
153153
fRecvStream, fSendStream, fHeaderStream : TCurlAutoStream;
154154

155155
procedure SetSList(
156156
aOpt : TCurlSlistOption;
157-
var aOldValue : ICurlSList;
158-
aNewValue : ICurlSList);
157+
var aOldValue : ICurlCustomSList;
158+
aNewValue : ICurlCustomSList);
159159

160160
procedure RewindStreams;
161161
public
@@ -199,17 +199,17 @@ TEasyCurlImpl = class (TInterfacedObject, ICurl)
199199

200200
procedure SetFollowLocation(aData : boolean);
201201

202-
procedure SetForm(aForm : ICurlForm);
203-
function GetForm : ICurlForm;
202+
procedure SetForm(aForm : ICurlCustomForm);
203+
function GetForm : ICurlCustomForm;
204204

205-
procedure SetCustomHeaders(v : ICurlSList);
206-
procedure SetPostQuote(v : ICurlSList);
207-
procedure SetTelnetOptions(v : ICurlSList);
208-
procedure SetPreQuote(v : ICurlSList);
209-
procedure SetHttp200Aliases(v : ICurlSList);
210-
procedure SetMailRcpt(v : ICurlSList);
211-
procedure SetResolveList(v : ICurlSList);
212-
procedure SetProxyHeader(v : ICurlSList);
205+
procedure SetCustomHeaders(v : ICurlCustomSList);
206+
procedure SetPostQuote(v : ICurlCustomSList);
207+
procedure SetTelnetOptions(v : ICurlCustomSList);
208+
procedure SetPreQuote(v : ICurlCustomSList);
209+
procedure SetHttp200Aliases(v : ICurlCustomSList);
210+
procedure SetMailRcpt(v : ICurlCustomSList);
211+
procedure SetResolveList(v : ICurlCustomSList);
212+
procedure SetProxyHeader(v : ICurlCustomSList);
213213

214214
procedure Perform;
215215
function PerformNe : TCurlCode;
@@ -234,7 +234,7 @@ TEasyCurlImpl = class (TInterfacedObject, ICurl)
234234
Size, NItems : NativeUInt;
235235
OutStream : pointer) : NativeUInt; cdecl; static;
236236

237-
property Form : ICurlForm read GetForm write SetForm;
237+
property Form : ICurlCustomForm read GetForm write SetForm;
238238

239239
procedure CloseStreams;
240240
end;
@@ -525,8 +525,8 @@ function TEasyCurlImpl.GetResponseCode : longint;
525525

526526
procedure TEasyCurlImpl.SetSList(
527527
aOpt : TCurlSlistOption;
528-
var aOldValue : ICurlSList;
529-
aNewValue : ICurlSList);
528+
var aOldValue : ICurlCustomSList;
529+
aNewValue : ICurlCustomSList);
530530
var
531531
rawVal : PCurlSList;
532532
begin
@@ -543,42 +543,42 @@ procedure TEasyCurlImpl.SetSList(
543543
SetOpt(aOpt, rawVal);
544544
end;
545545

546-
procedure TEasyCurlImpl.SetCustomHeaders(v : ICurlSList);
546+
procedure TEasyCurlImpl.SetCustomHeaders(v : ICurlCustomSList);
547547
begin
548548
SetSList(CURLOPT_HTTPHEADER, fCustomHeaders, v);
549549
end;
550550

551-
procedure TEasyCurlImpl.SetPostQuote(v : ICurlSList);
551+
procedure TEasyCurlImpl.SetPostQuote(v : ICurlCustomSList);
552552
begin
553553
SetSList(CURLOPT_POSTQUOTE, fPostQuote, v);
554554
end;
555555

556-
procedure TEasyCurlImpl.SetTelnetOptions(v : ICurlSList);
556+
procedure TEasyCurlImpl.SetTelnetOptions(v : ICurlCustomSList);
557557
begin
558558
SetSList(CURLOPT_TELNETOPTIONS, fTelnetOptions, v);
559559
end;
560560

561-
procedure TEasyCurlImpl.SetPreQuote(v : ICurlSList);
561+
procedure TEasyCurlImpl.SetPreQuote(v : ICurlCustomSList);
562562
begin
563563
SetSList(CURLOPT_PREQUOTE, fPreQuote, v);
564564
end;
565565

566-
procedure TEasyCurlImpl.SetHttp200Aliases(v : ICurlSList);
566+
procedure TEasyCurlImpl.SetHttp200Aliases(v : ICurlCustomSList);
567567
begin
568568
SetSList(CURLOPT_HTTP200ALIASES, fHttp200Aliases, v);
569569
end;
570570

571-
procedure TEasyCurlImpl.SetMailRcpt(v : ICurlSList);
571+
procedure TEasyCurlImpl.SetMailRcpt(v : ICurlCustomSList);
572572
begin
573573
SetSList(CURLOPT_MAIL_RCPT, fMailRcpt, v);
574574
end;
575575

576-
procedure TEasyCurlImpl.SetResolveList(v : ICurlSList);
576+
procedure TEasyCurlImpl.SetResolveList(v : ICurlCustomSList);
577577
begin
578578
SetSList(CURLOPT_RESOLVE, fResolveList, v);
579579
end;
580580

581-
procedure TEasyCurlImpl.SetProxyHeader(v : ICurlSList);
581+
procedure TEasyCurlImpl.SetProxyHeader(v : ICurlCustomSList);
582582
begin
583583
SetSList(CURLOPT_PROXYHEADER, fProxyHeader, v);
584584
end;
@@ -600,7 +600,7 @@ procedure TEasyCurlImpl.SetSslVerifyPeer(aData : boolean);
600600
SetOpt(CURLOPT_SSL_VERIFYPEER, aData);
601601
end;
602602

603-
procedure TEasyCurlImpl.SetForm(aForm : ICurlForm);
603+
procedure TEasyCurlImpl.SetForm(aForm : ICurlCustomForm);
604604
begin
605605
if aForm <> nil then begin
606606
SetOpt(CURLOPT_HTTPPOST, aForm.RawValue);
@@ -612,7 +612,7 @@ procedure TEasyCurlImpl.SetForm(aForm : ICurlForm);
612612
fForm := aForm;
613613
end;
614614

615-
function TEasyCurlImpl.GetForm : ICurlForm;
615+
function TEasyCurlImpl.GetForm : ICurlCustomForm;
616616
begin
617617
Result := fForm;
618618
end;

Src/Curl.Form.pas

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,86 @@
33
interface
44

55
uses
6+
// System
7+
System.Classes,
68
// cURL
79
Curl.Lib, Curl.Interfaces;
810

11+
type
12+
ICurlField = interface (ICurlCustomField)
13+
function Name(const x : RawByteString) : ICurlField;
14+
15+
function ContentRaw(const x : RawByteString) : ICurlField;
16+
function Content(const x : string) : ICurlField; overload;
17+
function Content(length : integer; const data) : ICurlField; overload;
18+
19+
/// Sets content from a RawByteString.
20+
/// @warning This RawByteString should live until Perform ends.
21+
function PtrContent(const x : RawByteString) : ICurlField; overload;
22+
/// Sets content from a memory buffer.
23+
/// @warning This buffer should live until Perform ends.
24+
function PtrContent(length : integer; const data) : ICurlField; overload;
25+
26+
// Unused right now: FORM_FILECONTENT is not Unicode-aware
27+
//function FileContent(const x : string) : ICurlField;
28+
29+
/// @warning
30+
/// When you do UploadFile, you SHOULD use Delphi streams for all
31+
/// other reading operations of ICurl concerned!
32+
/// E.g. use SetSendStream, not SetOpt(CURLOPT_READFUNCTION).
33+
function UploadFile(const aFname : string) : ICurlField;
34+
35+
function ContentType(const aFname : RawByteString) : ICurlField;
36+
37+
// Sets a file name
38+
function FileName(const x : RawByteString) : ICurlField;
39+
// Sets file data, either as RawByteString or as data buffer
40+
function FileBuffer(
41+
const aFname, aData : RawByteString) : ICurlField; overload;
42+
function FileBuffer(
43+
const aFname : RawByteString;
44+
length : integer; const data) : ICurlField; overload;
45+
/// @warning
46+
/// When you assign FileStream, you SHOULD use Delphi streams for all
47+
/// other reading operations of ICurl concerned!
48+
/// E.g. use SetSendStream, not SetOpt(CURLOPT_READFUNCTION).
49+
function FileStream(x : TStream; aFlags : TCurlStreamFlags) : ICurlField;
50+
51+
function CustomHeaders(x : ICurlCustomSList) : ICurlField;
52+
end;
53+
54+
ICurlForm = interface (ICurlCustomForm)
55+
/// This is the simplest version of Add; use it if you want something
56+
/// like name=value.
57+
function Add(aName, aValue : RawByteString) : ICurlForm; overload;
58+
function Add(aName, aValue : string) : ICurlForm; overload;
59+
/// This is a rawmost version of Add.
60+
/// @warning Please have CURLFORM_END in the end.
61+
/// @warning Some options of Windows cURL request disk file name
62+
/// in a single-byte encoding.
63+
function Add(aArray : array of TCurlPostOption) : ICurlForm; overload;
64+
/// Adds a field using a special array-builder.
65+
function Add(aField : ICurlCustomField) : ICurlForm; overload;
66+
67+
/// Adds a single disk file for uploading.
68+
/// @warning Because of cURL bug, it uses a stream internally.
69+
/// You SHOULD use Delphi streams for all other reading operations
70+
/// of ICurl concerned!
71+
/// E.g. use SetSendStream, not SetOpt(CURLOPT_READFUNCTION).
72+
function AddFile(
73+
aFieldName : RawByteString;
74+
aFileName : string;
75+
aContentType : RawByteString) : ICurlForm; overload;
76+
end;
77+
978
function CurlGetForm : ICurlForm;
1079
function CurlGetField : ICurlField;
1180

1281
implementation
1382

1483
uses
1584
// System
16-
System.Classes, System.SysUtils, System.Generics.Collections,
85+
System.SysUtils, System.Generics.Collections,
1786
// Curl
1887
Curl.Easy;
1988

@@ -126,7 +195,7 @@ procedure TCurlStorage<Intf>.Store(x : Intf);
126195
///// TCurlForm ////////////////////////////////////////////////////////////////
127196

128197
type
129-
TCurlForm = class (TCurlStorage<ICurlField>, ICurlForm)
198+
TCurlForm = class (TCurlStorage<ICurlCustomField>, ICurlForm)
130199
private
131200
fStart, fEnd : PCurlHttpPost;
132201
class procedure RaiseIf(v : TCurlFormCode); static;
@@ -137,7 +206,7 @@ TCurlForm = class (TCurlStorage<ICurlField>, ICurlForm)
137206
function Add(aName, aValue : PAnsiChar) : ICurlForm; overload;
138207
function Add(aName, aValue : RawByteString) : ICurlForm; overload; inline;
139208
function Add(aName, aValue : string) : ICurlForm; overload;
140-
function Add(aField : ICurlField) : ICurlForm; overload;
209+
function Add(aField : ICurlCustomField) : ICurlForm; overload;
141210
function Add(aArray : array of TCurlPostOption) : ICurlForm; overload;
142211

143212
function AddFile(
@@ -216,7 +285,7 @@ function TCurlForm.Add(aArray : array of TCurlPostOption) : ICurlForm;
216285
Result := Self;
217286
end;
218287

219-
function TCurlForm.Add(aField : ICurlField) : ICurlForm;
288+
function TCurlForm.Add(aField : ICurlCustomField) : ICurlForm;
220289
begin
221290
if aField.DoesStore
222291
then Store(aField);
@@ -266,7 +335,7 @@ function TCurlForm.RawValue : PCurlHttpPost;
266335

267336
procedure TCurlForm.RewindStreams;
268337
var
269-
fld : ICurlField;
338+
fld : ICurlCustomField;
270339
begin
271340
inherited;
272341
if fStorage <> nil then
@@ -276,7 +345,7 @@ procedure TCurlForm.RewindStreams;
276345

277346
procedure TCurlForm.CloseStreams;
278347
var
279-
fld : ICurlField;
348+
fld : ICurlCustomField;
280349
begin
281350
inherited;
282351
if fStorage <> nil then
@@ -337,12 +406,12 @@ TCurlField = class (TCurlStorage<IInterface>, ICurlField)
337406
length : integer; const data) : ICurlField; overload;
338407
function FileStream(x : TStream; aFlags : TCurlStreamFlags) : ICurlField;
339408

340-
function CustomHeaders(x : ICurlSlist) : ICurlField;
409+
function CustomHeaders(x : ICurlCustomSList) : ICurlField;
341410

342411
function DoesUseStream : boolean;
343412
function DoesStore : boolean;
344413

345-
function Build : PCurlHttpPost;
414+
function Build : PCurlForms;
346415
end;
347416

348417
constructor TCurlField.Create;
@@ -496,7 +565,7 @@ function TCurlField.FileStream(x : TStream; aFlags : TCurlStreamFlags) : ICurlFi
496565
Result := Self;
497566
end;
498567

499-
function TCurlField.CustomHeaders(x : ICurlSlist) : ICurlField;
568+
function TCurlField.CustomHeaders(x : ICurlCustomSList) : ICurlField;
500569
begin
501570
Store(x);
502571
Add(CURLFORM_CONTENTHEADER, PAnsiChar(x.RawValue));
@@ -510,7 +579,7 @@ function TCurlField.ContentType(const x : RawByteString) : ICurlField;
510579
Result := Self;
511580
end;
512581

513-
function TCurlField.Build : PCurlHttpPost;
582+
function TCurlField.Build : PCurlForms;
514583
begin
515584
if not fIsLocked then begin
516585
Add(CURLFORM_END, nil);

0 commit comments

Comments
 (0)