Skip to content

Commit 5d73879

Browse files
committed
1 parent 946419a commit 5d73879

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

Src/Curl.Form.pas

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ procedure TCurlStorage<Intf>.Store(x : Intf);
129129
TCurlForm = class (TCurlStorage<ICurlField>, ICurlForm)
130130
private
131131
fStart, fEnd : PCurlHttpPost;
132+
class procedure RaiseIf(v : TCurlFormCode); static;
132133
public
133134
constructor Create;
134135
destructor Destroy; override;
@@ -164,23 +165,41 @@ destructor TCurlForm.Destroy;
164165
end;
165166

166167

168+
class procedure TCurlForm.RaiseIf(v : TCurlFormCode);
169+
const
170+
OptionNames : array [TCurlFormCode] of string = (
171+
'', // CURL_FORMADD_OK,
172+
'Not enough memory', // CURL_FORMADD_MEMORY,
173+
'An option os mentioned twice', // CURL_FORMADD_OPTION_TWICE,
174+
'Nil as an option', // CURL_FORMADD_NULL,
175+
'Unknown option', // CURL_FORMADD_UNKNOWN_OPTION,
176+
'Incomplete option set', // CURL_FORMADD_INCOMPLETE,
177+
'Illegal array', // CURL_FORMADD_ILLEGAL_ARRAY,
178+
'cURL is built w/o this feature' // CURL_FORMADD_DISABLED
179+
);
180+
begin
181+
if v = CURL_FORMADD_OK then Exit;
182+
raise ECurlInternal.Create(OptionNames[v]);
183+
end;
184+
185+
167186
function TCurlForm.Add(aName, aValue : PAnsiChar) : ICurlForm;
168187
begin
169-
curl_formadd(fStart, fEnd,
188+
RaiseIf(curl_formadd(fStart, fEnd,
170189
CURLFORM_COPYNAME, aName,
171190
CURLFORM_COPYCONTENTS, aValue,
172-
CURLFORM_END);
191+
CURLFORM_END));
173192
Result := Self;
174193
end;
175194

176195
function TCurlForm.Add(aName, aValue : RawByteString) : ICurlForm;
177196
begin
178-
curl_formadd(fStart, fEnd,
197+
RaiseIf(curl_formadd(fStart, fEnd,
179198
CURLFORM_COPYNAME, PAnsiChar(aName),
180199
CURLFORM_NAMELENGTH, PAnsiChar(length(aName)),
181200
CURLFORM_COPYCONTENTS, PAnsiChar(aValue),
182201
CURLFORM_CONTENTSLENGTH, PAnsiChar(length(aValue)),
183-
CURLFORM_END);
202+
CURLFORM_END));
184203
Result := Self;
185204
end;
186205

@@ -191,19 +210,19 @@ function TCurlForm.Add(aName, aValue : string) : ICurlForm;
191210

192211
function TCurlForm.Add(aArray : array of TCurlPostOption) : ICurlForm;
193212
begin
194-
curl_formadd(fStart, fEnd,
213+
RaiseIf(curl_formadd(fStart, fEnd,
195214
CURLFORM_ARRAY, PAnsiChar(@aArray[0]),
196-
CURLFORM_END);
215+
CURLFORM_END));
197216
Result := Self;
198217
end;
199218

200219
function TCurlForm.Add(aField : ICurlField) : ICurlForm;
201220
begin
202221
if aField.DoesStore
203222
then Store(aField);
204-
curl_formadd(fStart, fEnd,
223+
RaiseIf(curl_formadd(fStart, fEnd,
205224
CURLFORM_ARRAY, PAnsiChar(aField.Build),
206-
CURLFORM_END);
225+
CURLFORM_END));
207226
if aField.DoesUseStream
208227
then fDoesUseStream := true;
209228

@@ -227,14 +246,14 @@ function TCurlForm.AddFile(
227246
// CURLFORM_END);
228247
// CURLFORM_FILE is not Unicode-enabled, so we’ll do this thingy.
229248
stream := TFileStream.Create(aFileName, fmOpenRead + fmShareDenyWrite);
230-
curl_formadd(fStart, fEnd,
249+
RaiseIf(curl_formadd(fStart, fEnd,
231250
CURLFORM_COPYNAME, PAnsiChar(aFieldName),
232251
CURLFORM_NAMELENGTH, PAnsiChar(length(aFieldName)),
233252
CURLFORM_FILENAME, PAnsiChar(UTF8Encode(ExtractFileName(aFileName))),
234253
CURLFORM_STREAM, PAnsiChar(stream),
235254
CURLFORM_CONTENTSLENGTH, PAnsiChar(stream.Size),
236255
CURLFORM_CONTENTTYPE, PAnsiChar(aContentType),
237-
CURLFORM_END);
256+
CURLFORM_END));
238257
Store(stream, [csfAutoRewind, csfAutoDestroy]);
239258
fDoesUseStream := true;
240259
Result := Self;

0 commit comments

Comments
 (0)