cURL
Haxx ad
libcurl

curl's project page on SourceForge.net

Sponsors:
Haxx

cURL > Mailing List > Monthly Index > Single Mail

curlpp mailing list Archives

[cURLpp] HTTP POST and WriteCallback

From: Nazario Cipriani <NazarioCipriani_at_web.de>
Date: Thu, 28 Jul 2005 17:31:13 +0200

Hi!

There is a strange problem I encountered with the WriteCallback.
The class I'm using looks like this (as posted in my previous mail, I
put it here for better understanding):

     class WriterMemoryClass
     {
     public:
         // Helper Class for reading result from remote host
         WriterMemoryClass()
         {
             this->m_pBuffer = NULL;
             this->m_pBuffer = (char*) malloc(MAX_FILE_LENGTH *
sizeof(char));
             this->m_Size = 0;
         };
         ~WriterMemoryClass()
         {
             if (this->m_pBuffer)
                 free(this->m_pBuffer);
         };
         static void* Realloc(void* ptr, size_t size)
         {
             if(ptr)
                 return realloc(ptr, size);
             else
                 return malloc(size);
         };
         // Callback must be declared static, otherwise it won't link...
         static size_t WriteMemoryCallback(char* ptr, size_t size,
size_t nmemb, void* data)
         {
             // Calculate the real size of the incoming buffer
             size_t realsize = size * nmemb;

             // Cast the void* data pointer to WriterMemoryClass...
             WriterMemoryClass* Memory = (WriterMemoryClass*) data;

             // (Re)Allocate memory for the buffer
             Memory->m_pBuffer = (char*)
Memory->Realloc(Memory->m_pBuffer, Memory->m_Size + realsize + 1);

             // Test if Buffer is initialized correctly & copy memory
             if (Memory->m_pBuffer)
             {
                 memcpy(&(Memory->m_pBuffer[Memory->m_Size]), ptr,
realsize);
                 Memory->m_Size += realsize;
                 Memory->m_pBuffer[Memory->m_Size] = 0;
             }

             // return the real size of the buffer...
             return realsize;
         };

         // Public member vars
         char* m_pBuffer;
         size_t m_Size;
     };

Now comes the problem description.
When I make an HTTP POST request, all works good till the moment I step
into the WriteCallback. The data pointer is casted in this way:

WriterMemoryClass* Memory = (WriterMemoryClass*) data;

The Memory pointer looks strange to me.
When you try to access Memory, for example by

memcpy(&(Memory->m_pBuffer[Memory->m_Size]), ptr, realsize);

it aborts and exits with a memory access violation.

As I use the same class for my HTTP GET requests (where it works) I
assumed I can use it for my HTTP Post requests too.
Is this right or am I wrong?

Thanks in advice for your help!
Regards, Nazario.

-- 
Spanne den Bogen, aber schieße nicht los!
Noch gefürchtet zu sein ist wirksamer!
(altes chinesisches Sprichwort)
_______________________________________________
cURLpp mailing list
cURLpp_at_rrette.com
http://www.rrette.com/mailman/listinfo/curlpp
Received on 2005-07-28

These mail archives are generated by hypermail.

donate! Page updated November 12, 2010.
web site info

File upload with ASP.NET