验证中...
IdMultipartFormDataReader
Raw Copy
unit IdMultipartFormDataReader;
interface
uses
SysUtils, Classes, IdGlobal, Dialogs,IdCoder, //IdGlobalProtocols,
idMultipartFormData, IdCustomHTTPServer, IdCoderQuotedPrintable, IdCoderMIME, IdHeaderList;
type
TIdMultiPartFormDataStreamReader = class(TIdMultiPartFormDataStream)
private
FRequestInfo: TIdHTTPRequestInfo;
FPostStream:TStream;
protected
procedure DecodeStream;
public
property Fields: TIdFormDataFields read FFields;
constructor Create(ARequestInfo: TIdHTTPRequestInfo;APostStream:TStream);
end;
implementation
{ TIdMultiPartFormDataStreamReader }
constructor TIdMultiPartFormDataStreamReader.Create(ARequestInfo: TIdHTTPRequestInfo;APostStream:TStream);
begin
inherited Create;
FSize := 0;
FInitialized := False;
FFields := TIdFormDataFields.Create(Self);
FRequestInfo := ARequestInfo;
FPostStream := APostStream;
DecodeStream;
end;
procedure TIdMultiPartFormDataStreamReader.DecodeStream;
var
delimi, cadStream: string;
cabeceras: TIdHeaderList;
stInfo: TMemoryStream;
posIniPost, posFinPost, posIniRetorno: Integer;
cadenaOri: string;
fieldName: string;
fieldFileName: string;
contentType: string;
charSet: string;
transferEnconding: string;
function ReadStringAsCharset(AStream: TStream; const ACharset: String): String;
var
sStream:TStringStream;
begin
Result := '';
sStream:=TStringStream.Create('');
sStream.CopyFrom(AStream,AStream.Size);
Result := sStream.DataString;
FreeAndNil(sStream);
end;
function ExtractHeaderItem(const AHeaderLine: String): String;
var
s: string;
begin
s := AHeaderLine;
Result := Trim(Fetch(s, ';'));
end;
function ReadStringLn(StreamOri: TStream; var cadenaBus: string; char_set: string): Boolean;
var
candidato: Boolean;
posIniSO: Integer;
posFinSO: Integer;
StreamDes: TMemoryStream;
bleer: Byte;
begin
candidato := False;
StreamDes := TMemoryStream.Create;
posIniSO := StreamOri.Position;
cadenaBus := '';
while (not candidato) and (StreamOri.Position < StreamOri.Size) do
begin
StreamOri.Read(bleer, 1);
if (bleer = 13) then
begin
StreamOri.Read(bleer, 1);
if (bleer = 10) then
begin
candidato := True;
posFinSO := StreamOri.Position;
StreamOri.Position := posIniSO;
if (posFinSO - 2 - posIniSO > 0) then
begin
StreamDes.CopyFrom(StreamOri, posFinSO - 2 - posIniSO);
StreamDes.Position := 0;
cadenaBus := ReadStringAsCharset(StreamDes, char_set);
end;
StreamOri.Position := posFinSO;
end;
end;
end;
StreamDes.Free;
Result := candidato;
end;
function FindValue(AContentType,conditonstr: string;nUpperCase:Boolean=False): string;
var
s,s1: string;
begin
s := (AContentType);
s1:= (conditonstr);
if nUpperCase then
begin
s := UpperCase(AContentType);
s1:= UpperCase(conditonstr);
end;
//Fetch(s, 'BOUNDARY=');
Fetch(s, s1);
if (Length(s) > 0) and (s[1] = '"') then
begin
Delete(s, 1, 1);
Result := Fetch(s, '"');
end
else
begin
Result := s;
end;
end;
begin
//showmessage(FRequestInfo.RawHeaders.Text);
if (ExtractHeaderItem(FRequestInfo.RawHeaders.Values['Content-Type']) <> 'multipart/form-data') then
Exit;
cabeceras := TIdHeaderList.Create();
//delimi := FRequestInfo.RawHeaders.Params['Content-Type', 'boundary'];
delimi := FindValue(FRequestInfo.ContentType,'BOUNDARY=',True);
FBoundary := delimi;
FRequestContentType := sContentType + FBoundary;
FPostStream.Position := 0;
if (ReadStringLn(FPostStream, cadenaOri, '')) then
begin
while (cadenaOri = '--' + delimi) and (FPostStream.Position < FPostStream.Size) do
begin
if (ReadStringLn(FPostStream, cadenaOri, '')) then
begin
while (cadenaOri <> '') and (FPostStream.Position < FPostStream.Size) do
begin
cabeceras.Add(cadenaOri);
ReadStringLn(FPostStream, cadenaOri, '');
end;
//fieldName := cabeceras.Params['Content-Disposition', 'name'];
//fieldFileName := cabeceras.Params['Content-Disposition', 'filename'];
//contentType := ExtractHeaderItem(cabeceras.Values['Content-Type']);
//charSet := cabeceras.Params['Content-Type', 'charset'];
//transferEnconding := cabeceras.Values['Content-Transfer-Encoding'];
fieldName := FindValue(cabeceras.Values['Content-Disposition'],'name=');
fieldFileName := FindValue(cabeceras.Values['Content-Disposition'],'filename=');
stInfo := TMemoryStream.Create;
posIniPost := FPostStream.Position;
posFinPost := posIniPost;
ReadStringLn(FPostStream, cadenaOri, '');
while (cadenaOri <> '--' + delimi) and (cadenaOri <> '--' + delimi + '--')
and (FPostStream.Position < FPostStream.Size) do
begin
posFinPost := FPostStream.Position;
ReadStringLn(FPostStream, cadenaOri, '');
end;
posIniRetorno := FPostStream.Position;
FPostStream.Position := posIniPost;
if (posFinPost - 2 - posIniPost > 0) then
begin
if (fieldFileName <> '') then
begin
stInfo.CopyFrom(FPostStream, posFinPost - 2 - posIniPost);
{
if (LowerCase(transferEnconding) = 'quoted-printable') then
begin
stInfo.Position := 0;
cadStream := ReadStringAsCharset(stInfo,'');
stInfo.Clear;
stInfo.Position := 0;
TIdDecoderQuotedPrintable.DecodeToStream(cadStream, stInfo);
end
else if (LowerCase(transferEnconding) = 'base64') then
begin
stInfo.Position := 0;
cadStream := ReadStringAsCharset(stInfo,'');
stInfo.Clear;
stInfo.Position := 0;
TIdDecoderMIME.DecodeToStream(cadStream, stInfo);
end;
stInfo.Position := 0;
AddFormField(fieldName, contentType, charSet, stInfo, fieldFileName);
}
cadStream := '';
stInfo.Position := 0;
cadStream := ReadStringAsCharset(stInfo,'');
cadStream := TIdDecoderQuotedPrintable.DecodeString(cadStream);
AddFormField(fieldName, cadStream);
stInfo.Free;
end
else
begin
stInfo.CopyFrom(FPostStream, posFinPost - 2 - posIniPost);
cadStream := '';
stInfo.Position := 0;
cadStream := ReadStringAsCharset(stInfo,'');
cadStream := TIdDecoderQuotedPrintable.DecodeString(cadStream);
cadStream := UTF8Decode(cadStream);
AddFormField(fieldName, cadStream);
stInfo.Free;
end;
end;
FPostStream.Position := posIniRetorno;
cabeceras.Clear;
end;
end;
end;
end;
end.

Comment list( 0 )

You need to Sign in for post a comment

Help Search

Gitee_you_jiang_zheng_wen Zheng_wen_close