Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Apr : Re: Parsing a multipart form

www.cryer.info
Managed Newsgroup Archive

Re: Parsing a multipart form

Subject:Re: Parsing a multipart form
Posted by:"David Fealkoff" (fealko..@iatinc.net)
Date:Tue, 4 Apr 2006 01:01:24

Gambit,

(1) Not able to get it work yet.  The code I am using is taken from your
example (note: I'm using Indy 10 so I had to make some changes to get it to
compile) and it looks like this:

  procedure DecodeFormData(const Header: String; ASourceStream:TStream);
  var
    MsgEnd: Boolean;
    Decoder: TIdMessageDecoder;
    Tmp: String;
    Dest: TStream;
  begin
    MsgEnd := False;
    Decoder := TIdMessageDecoderMIME.Create(nil);

    try
      TIdMessageDecoderMIME(Decoder).MIMEBoundary :=
        TIdMIMEBoundary.FindBoundary(Header);
      Decoder.SourceStream := ASourceStream;
      Decoder.FreeSourceStream := False;
      Decoder.ReadLn;

      repeat
        Decoder.ReadHeader;
        case Decoder.PartType of
        mcptUnknown:
          raise Exception.Create('Unknown form data detected');
        mcptText:
        begin
          Tmp := Decoder.Headers.Values['Content-Type'];
          Dest := TMemoryStream.Create;
          try
            Decoder := Decoder.ReadBody(Dest,MsgEnd);
            if AnsiSameText(Fetch(Tmp, ';'),'multipart/mixed') then
              DecodeFormData(Tmp, Dest)
            else
              // use Dest as needed...
            finally
              FreeAndNil(Dest);
            end;//try
          end;
          mcptAttachment:
          begin
            Tmp := ExtractFileName(Decoder.FileName);
            if Tmp <> '' then
              Tmp := 'c:\some folder\' + Tmp
            else
              Tmp := MakeTempFilename('c:\somefolder\');

            Dest := TFileStream.Create(Tmp, fmCreate);
            try
              Decoder := Decoder.ReadBody(Dest,MsgEnd);
            finally
              FreeAndNil(Dest);
            end;//try
          end;
        end;
      until (Decoder = nil) or MsgEnd;
    finally
      FreeAndNil(Decoder);
    end;//try
  end;

<from procedure TfrmHTTPServerMain.HTTPServerCommandGet>
        S := ARequestInfo.ContentType;
        if AnsiSameText(Fetch(S, ';'), 'multipart/form-data') then
            DecodeFormData(S, ARequestInfo.PostStream)
        else
            // use request data as needed...


(2) Below I have the start of my post which I obtained from
ARequestInfo.unparsedparams.  I think it looks ok. let me know if you see
something wrong.

'-----------------------------040406002513382'#$D#$A'Content-Disposition:
form-data;
name="VType"'#$D#$A#$D#$A'Imp'#$D#$A'-----------------------------040406002513382'#$D#$A'Content-Disposition:
form-data;
name="ProfileID"'#$D#$A#$D#$A'221'#$D#$A'-----------------------------040406002513382'#$D#$A'Content-Disposition:
form-data; name="FName"'#$D#$A#$D#$A'Woody
Allen.wav'#$D#$A'-----------------------------040406002513382'#$D#$A'Content-Disposition:
form-data; name="Path"; filename="C:\Projects\PhaseII\HTTPClient\voice\Woody
Allen.wav"'#$D#$A'Content-Type: audio'#$D#$A#$D#$A'RIFF¦'#$1F#1#0'WAVEfmt
'#$12#0#0#0#1#0#1#0'@'#$1F#0#0'?>'#0#0#2#0#$10#0#0#0'data?'#...'

(3) I get to DecodeFormData and the first time I call 'ReadHeader', the
decoder.parttype is "Attachment" with no file name;  On the subsequent
decoder.readbody it fails on 'ReadLinefromStream' on Assert(AStream<>nil).
So I'm stepping through the code (very tedious) to try to understand why
DecodeFormData is failing.  If you see something or have an idea on what I
should try please let me know.

Thanks.

David


"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message news:44317125
$1@newsgroups.borland.com...
>
> "David Fealkoff" <fealkoff@iatinc.net> wrote in message
> news:442e3f4d$1@newsgroups.borland.com...
>
>> Is there an example of how to parse this multipart form stream?
>> After searching the Internet it seems that the TidMessageDecoderMIME
>> could be used to perform that function but I was unable to find an
> example.
>
> I have posted examples many times in both the C++ and Delphi newsgroups.
> Those examples are available in the newsgroup archives at
> http://www.deja.com.  For example:
>
> http://groups.google.com/group/borland.public.delphi.internet.winsock/browse_thread/thread/4b883bea8a84a366/95ef0ceb86f22e28?lnk=st&q=multipart+form-data+TIdMessageDecoderMIME+group%3A*delphi*&rnum=2#95ef0ceb86f22e28
>
>
> Gambit

Replies:

none

In response to:

www.cryer.info
Managed Newsgroup Archive