Skip to content

Commit fa85781

Browse files
Fixed multipart form data boundries - uploading files works again.
1 parent 53eb2fa commit fa85781

3 files changed

Lines changed: 17 additions & 15 deletions

File tree

Source/VSoft.HttpClient.MultipartFormData.pas

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,8 @@ procedure TMultipartFormData.AddStream(const fieldName: string; stream: TStream;
797797
if sContentType = '' then
798798
sContentType := GetMimeType(fileName);
799799
WriteString(cContentTypeHeader + ': ' + sContentType + #13#10);
800-
FDataStream.CopyFrom(stream, 0);
801-
WriteString('');
800+
stream.Seek(0, soFromBeginning);
801+
FDataStream.CopyFrom(stream, stream.Size);
802802
end;
803803

804804
class procedure TMultipartFormData.AddWindowsMimeTypes;
@@ -889,17 +889,16 @@ destructor TMultipartFormData.Destroy;
889889
function TMultipartFormData.Generate: TStream;
890890
begin
891891
//add the final boundary
892-
WriteString('--' + FBoundary + '--');
892+
WriteString(#13#10 + '--' + FBoundary + '--');
893893
FDataStream.Seek(0,soFromBeginning); //this is required as the http request doesn't rewind the stream
894894

895895
result := FDataStream;
896-
// FDataStream := nil;
897-
GenerateUniqueBoundry;
896+
FDataStream := nil; //this is important as the caller takes ownership.
898897
end;
899898

900899
procedure TMultipartFormData.GenerateUniqueBoundry;
901900
begin
902-
FBoundary := '----------------------' + FormatDateTime('mmddyyhhnnsszzz', Now);
901+
FBoundary := '-----------' + FormatDateTime('mmddyyhhnnsszzz', Now) + '-----------';
903902
end;
904903

905904
function TMultipartFormData.GetBoundary: string;

Source/VSoft.HttpClient.WinHttpClient.pas

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,9 @@ function THttpClient.OnHeadersAvailable(hRequest : HINTERNET; dwInternetStatus:
411411
if statusCode = HTTP_STATUS_PROXY_AUTH_REQ then
412412
HandleProxyAuthResponse(hRequest)
413413
else if statusCode = HTTP_STATUS_DENIED then
414-
HandleAccessDeniedResponse(hRequest)
415-
else if ((statusCode div 100) <> 2) then //any 2xx is good
416-
exit(ERROR_WINHTTP_INVALID_HEADER);
414+
HandleAccessDeniedResponse(hRequest);
415+
// else if ((statusCode div 100) <> 2) then //any 2xx is good
416+
// exit(ERROR_WINHTTP_INVALID_HEADER);
417417

418418
FLastStatusCode := statusCode;
419419

@@ -676,6 +676,8 @@ function THttpClient.Send(const request: TRequest; const cancellationToken: ICan
676676
if Assigned(pCallBack) then
677677
raise Exception.Create('Callback was already set!');
678678

679+
FDataLength := request.ContentLength; //need to do this before configurerequest as if there are files it will set the content type.
680+
679681
hr := ConfigureWinHttpRequest(hRequest, request);
680682
if hr <> S_OK then
681683
raise Exception.Create('Could not configure request : ' + SysErrorMessage(GetLastError) );
@@ -688,7 +690,6 @@ function THttpClient.Send(const request: TRequest; const cancellationToken: ICan
688690
Inc(handleCount);
689691
end;
690692

691-
FDataLength := request.ContentLength;
692693

693694
if (request.HtttpMethod <> THttpMethod.GET) and (FDataLength > 0) then
694695
begin
@@ -842,7 +843,7 @@ function THttpClient.WriteHeaders(hRequest: HINTERNET; const headers: TStrings):
842843
else
843844
sHeaders := sHeaders + headers.Names[i] + ': ' +headers.ValueFromIndex[i];
844845

845-
if i < headers.count -1 then
846+
// if i < headers.count -1 then
846847
sHeaders := sHeaders + #13#10;
847848
end;
848849

Source/VSoft.HttpClient.pas

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ destructor TRequest.Destroy;
409409
FHeaders.Free;
410410
FRequestParams.Free;
411411
FUrlSegments.Free;
412+
if FContent <> nil then
413+
FContent.Free;
412414
inherited;
413415
end;
414416

@@ -489,11 +491,11 @@ function TRequest.GetBody: TStream;
489491
if j > 0 then
490492
formdata.AddFile(Copy(sFileName, 1, j-1), Copy(sFileName, j+1, Length(sFileName)),FFiles.ValueFromIndex[i])
491493
else
492-
formdata.AddFile('files', Copy(sFileName, j+1, Length(sFileName)),FFiles.ValueFromIndex[i]);
494+
formdata.AddFile('file' + IntToStr(i), Copy(sFileName, j+1, Length(sFileName)),FFiles.ValueFromIndex[i]);
493495
end;
494-
//generate creates a new boundary so we need to upate the contentype before generating
496+
FContent := formdata.Generate; //taking ownership here!
495497
SetContentType(formdata.ContentType);
496-
result := formdata.Generate;
498+
result := FContent;
497499
end
498500
else if FRequestParams.Count > 0 then
499501
begin
@@ -787,7 +789,7 @@ function TRequest.WithFile(const filePath, fieldName, contentType: string): TRe
787789
if fieldName <> '' then
788790
FFiles.Add(fieldName + ',' + filePath + '=' + contentType)
789791
else
790-
FFiles.Add('files,' + filePath + '=' + contentType);
792+
FFiles.Add(ExtractFileName(filePath) + ',' + filePath + '=' + contentType);
791793
end;
792794

793795
function TRequest.WithHeader(const name, value: string): TRequest;

0 commit comments

Comments
 (0)