🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #3368

Re: Help on how to decode multipart/form-data

#3368
From: Remy Lebeau (TeamB)
Date: Mon, 24 Oct 2005 23:59
119 lines
5189 bytes
"noel" <noel@teledata.qc.ca> wrote in message
news:435d2270$1@newsgroups.borland.com...

> the TIdHTTPRequestInfo -> Params does not contain
> the parameters as usual

Correct, because it is not supposed to.  The Params property is not meant to
be used with "multipart/form-data" transmissions.

You need to use the OnCreatePostStream event to create an instance of your
own TStream object.  If you do not, then TIdHTTPServer uses its own TStream
to receive the data and then frees the TStream prior to triggering the
OnCommandGet event.  You need access to that TStream in order to process a
"multipart/form-data" transmission.  You can access it via the
Request->PostStream in the OnCommandGet event handler.

> My question is how do i decode the different parts from the
> TidHTTPServer OnCommandGet handler ...

You have to decode the data manually.  You can use TIdMessageCoderMIME for
that.  For example (untested):


    void __fastcall TForm1::IdHTTPServer1CreatePostStream(TIdPeerThread
*AThread, TStream* &VPostStream)
    {
        VPostStream = new TMemoryStream;
    }

    bool __fastcall TForm1::DecodeFormData(const AnsiString &Header, TStream
*Stream)
    {
        bool MsgEnd = false;

        TIdMessageDecoderMIME *decoder = new TIdMessageDecoderMIME(NULL);
        try
        {
            decoder->MIMEBoundary =
TIdMIMEBoundary::FindBoundary(__classid(TIdMIMEBoundary), Header);

            ((TIdStream*)Stream)->ReadLn();

            do
            {
                decoder->ReadHeader();
                switch( decoder->PartType )
                {
                    case mcptUnknown:
                        throw Exception("Unknown form data detected");
                        break;

                    case mcptText:
                    {
                        AnsiString tmp =
decoder->Headers->Values["Content-Type"];
                        AnsiString ContentType = Fetch(tmp, ";");

                        TMemoryStream *DataStrm = new TMemoryStream;
                        try
                        {
                            decoder = decoder->ReadBody(DataStrm, MsgEnd);

                            if( AnsiSameText(ContentType,
"multipart/mixed") )
                                DecodeFormData(tmp, DataStrm);
                            else
                                // use DataStrm as needed...
                        }
                        __finally {
                            delete DataStrm;
                        }
                        break;
                    }

                    case mcptAttachment:
                    {
                        AnsiString FileName =
ExtractFileName(decoder->FileName);
                        if( !FileName.IsEmpty() )
                            FileName = ("c:\\some folder\\" + FileName);
                        else
                            FileName = MakeTempFilename("c:\\some
folder\\");

                        TFileStream *FileStrm = new TFileStream(FileName,
fmCreate);
                        try {
                            decoder = decoder->ReadBody(FileStrm, MsgEnd);
                        }
                        __finally {
                            delete FileStrm;
                        }
                        break;
                }
                while( (decoder) && (!MsgEnd) );
            }
            __finally
            {
                if( decoder )
                    delete decoder;
            }
        }
    }

    void __fastcall TForm1::IdHTTPServer1CommandGet(TIdPeerThread *AThread,
TIdHTTPRequestInfo *ARequestInfo, TIdHTTPResponseInfo *AResponseInfo)
    {
        AnsiString tmp = ARequestInfo->Headers->Values["Content-Type"];
        AnsiString ContentType = Fetch(tmp, ";");

        if( AnsiSameText(ContentType, "multipart/form-data") )
            DecodeFormData(hdr, ARequestInfo->PostStream);
        else
            // use request data as needed...
    }


Gambit
Technical Details
Message-ID: <435d74b1$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!atl-c05.usenetserver.com!news.usenetserver.com!pink.octanews.net!indigo.octanews.net!news-out.octanews.net!teal.octanews.net!nx02.iad01.newshosting.com!newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <435d2270$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,a70bb79561966dc0
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <435d2270$1@newsgroups.borland.com>
Subject: Re: Help on how to decode multipart/form-data 
Date: Mon, 24 Oct 2005 16:59:01 -0700
Lines: 121
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <435d74b1$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1130198193 68.69.17.41 (24 Oct 2005 16:56:33 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!atl-c05.usenetserver.com!news.usenetserver.com!pink.octanews.net!indigo.octanews.net!news-out.octanews.net!teal.octanews.net!nx02.iad01.newshosting.com!newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail