Zaid Amir | 2 Nov 2009 08:26
Favicon

Put request with postfields libcurl c++

Hi

 

I am somewhat new to libcurl and I was recently faced with a problem that I hope someone can have the answer to it. My problem is that I’m supposed to send a PUT request to a website and that request should also include an XML as raw data. The problem is I can’t seem to be able to configure my read callback to read from a string if that is actually how its done. Basically I’m trying to send this request to Amazon S3 to enable their new versioning feature, and as described by Amazon, I am supposed to do something like this:

 

PUT /?versioning HTTP/1.1

Host: bucket.s3.amazonaws.com

Date: Wed, 12 Oct 2009 17:50:00 GMT

Authorization: AWS 15B4D3461F177624206A:xQE0diMbLRepdf3YB+FIEXAMPLE=

Content-Type: text/plain

Content-Length: 124

 

<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">

   <Versioning>true</Versioning>

</VersioningConfiguration>

 

 

Now the first part of the request is straight forward and I don’t really have any problem with it; the xml data part however is giving me nightmares as I have tried almost everything to make it work, I even tried adding it manually via the HTTPHEADER and it did not work.

 

And as I mentioned above, I tried reading the data directly from a string although I don’t think it’s the right approach but still here is my READFUNCTION:

 

size_t readTextCallback(void* ptr,size_t size, size_t nmemb, void* stream)

{

      #ifdef _DEBUG

            cout << "read_data being called" << endl;

      #endif

      string* strptrdst = static_cast<string*>(ptr);

      strptrdst = static_cast<string*>(stream);

      return nmemb*size;

}

 

Now for some reason, when I use a read function Curl keeps calling the function until the request gets terminated by the remote host.

 

Can someone please help..

 

Regards

 

 

Zaid Al-Amir

C++ Software Developer

...........................................

Email: z.alamir <at> genie-soft.com

Web: www.genie-soft.com

 http://www.linkedin.com/in/zaidamir

 

 

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
Daniel Stenberg | 2 Nov 2009 09:53
Picon
Favicon
Gravatar

Re: Put request with postfields libcurl c++

On Mon, 2 Nov 2009, Zaid Amir wrote:

> size_t readTextCallback(void* ptr,size_t size, size_t nmemb, void* stream)
> {
>      #ifdef _DEBUG
>            cout << "read_data being called" << endl;
>      #endif

>      string* strptrdst = static_cast<string*>(ptr);
>      strptrdst = static_cast<string*>(stream);
>      return nmemb*size;
> }

Don't get sucked up by all the fancy C++isms you can make in a program. Try to 
focus on what the docs actually say the callback is supposed to do.

The read callback is supposed to copy data to the buffer, data that libcurl is 
supposed to send away.

'ptr' points to the buffer. size*nmemb is the (maximum) amount of data you 
should copy there.

Just copy data to the buffer in every invoke until there is no more data to 
copy.

A slightly different way to do PUT with a fixed string, is to instead: use 
CURLOPT_POSTFIELDS and CURLOPT_CUSTOMREQUEST to change the request to a "PUT" 
and then you modify the Content-Type: to whatever you want it to be.

--

-- 

  / daniel.haxx.se
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Karel K | 2 Nov 2009 12:22
Picon

CURLINFO_LASTSOCKET returns error 56

Hi,

On a hint of Daniel on the curl-users list, I took a look at
curltunnel (see http://repo.or.cz/w/curltunnel.git). This is a progam
that uses libcurl to achieve the same functionality as proxytunnel
(http://proxytunnel.sf.net) has, that is tunneling ssh over a proxy
server. The advantage of curltunnel is that it can offer better proxy
authentication methods than proxytunnel.

After removing a small bug in the upstream code, it did actually work
perfect on my linux box.

However when run on windows is breaks. The line

ret = curl_easy_getinfo(hnd, CURLINFO_LASTSOCKET, &sckt);

returns a ret of 56, which is, according to strerror(ret):  File
locking deadlock error?

As a newbie user of libcurl I have no idea where to go further. Any clues?

Thanks in advance,

Karel.
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Daniel Stenberg | 2 Nov 2009 13:37
Picon
Favicon
Gravatar

Re: CURLINFO_LASTSOCKET returns error 56

On Mon, 2 Nov 2009, Karel K wrote:

> On a hint of Daniel on the curl-users list, I took a look at curltunnel (see 
> http://repo.or.cz/w/curltunnel.git). This is a progam that uses libcurl to 
> achieve the same functionality as proxytunnel (http://proxytunnel.sf.net) 
> has, that is tunneling ssh over a proxy server. The advantage of curltunnel 
> is that it can offer better proxy authentication methods than proxytunnel.
>
> After removing a small bug in the upstream code, it did actually work 
> perfect on my linux box.

Oh, nice!

> However when run on windows is breaks. The line
>
> ret = curl_easy_getinfo(hnd, CURLINFO_LASTSOCKET, &sckt);
>
> returns a ret of 56, which is, according to strerror(ret):  File locking 
> deadlock error?
>
> As a newbie user of libcurl I have no idea where to go further. Any clues?

You need to use curl_easy_strerror() to get the error message, it is a libcurl 
error not a libc one.

Or you can just read the include/curl.h header to figure out what 56 is.

--

-- 

  / daniel.haxx.se
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

David Byron | 2 Nov 2009 17:48

RE: undefined reference to `dlsym'

> It looks like your OpenSSL build needs -ldl (dynamically
> linked) and the configure script does not know about
> it. You are specifying the path to OpenSSL and I think
> this prevent the configure script from using pkg-config.
> Does your system provide a working pkg-config?

Yes.

> If yes try the following and paste the output here:
> 
> $ pkg-config openssl --libs

$ pkg-config openssl --libs
-lssl -lcrypto -ldl -lz

openssl.pc for the installed package and for the one I built both contain:

Libs: -L${libdir} -lssl -lcrypto -ldl

> Eventually you can try to set the PKG_CONFIG_PATH
> environment variable to force your local build of OpenSSL
> and see what happens.

Makes sense, though I grepped for pkgconfig in the root curl directory and
m4/ and only see things that I think are related to libcurl.pc.

-DB

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Daniel Stenberg | 2 Nov 2009 19:52
Picon
Favicon
Gravatar

RE: undefined reference to `dlsym'

On Mon, 2 Nov 2009, David Byron wrote:

>> Eventually you can try to set the PKG_CONFIG_PATH environment variable to 
>> force your local build of OpenSSL and see what happens.
>
> Makes sense, though I grepped for pkgconfig in the root curl directory and 
> m4/ and only see things that I think are related to libcurl.pc.

grep for pkg-config instead...

--

-- 

  / daniel.haxx.se
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Karel K | 2 Nov 2009 20:05
Picon

Re: CURLINFO_LASTSOCKET returns error 56

>> After removing a small bug in the upstream code, it did actually work
>> perfect on my linux box.
>
> Oh, nice!
I am trying to contact the author to keep upstream in shape.

>> However when run on windows is breaks. The line
>>
>> ret = curl_easy_getinfo(hnd, CURLINFO_LASTSOCKET, &sckt);
>>
>> returns a ret of 56, which is, according to strerror(ret):  File locking
>> deadlock error?

> You need to use curl_easy_strerror() to get the error message, it is a
> libcurl error not a libc one.

Makes sense. I did copy this (strerror) from an example in the curl
distribution...
doc/examples/sendrecv.c, line 75. Maybe you want to update this line.

    /* Extract the socket from the curl handle - we'll need it
     * for waiting */
    res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockfd);

    if(CURLE_OK != res)
    {
      printf("Error: %s\n", strerror(res));
      return 1;
    }

> Or you can just read the include/curl.h header to figure out what 56 is.
CURLE_RECV_ERROR,              /* 56 - failure in receiving network data */

Any clue how I can further debug this problem??

Thanks,

Karel.
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
Kamil Dudka | 2 Nov 2009 20:56
Picon
Favicon
Gravatar

Re: undefined reference to `dlsym'

On Monday 02 of November 2009 17:48:07 David Byron wrote:
> openssl.pc for the installed package and for the one I built both contain:
>
> Libs: -L${libdir} -lssl -lcrypto -ldl

I think --with-ssl without any extra argument is the way to go in that case.

> > Eventually you can try to set the PKG_CONFIG_PATH
> > environment variable to force your local build of OpenSSL
> > and see what happens.
>
> Makes sense, though I grepped for pkgconfig in the root curl directory and
> m4/ and only see things that I think are related to libcurl.pc.

Just set your PKG_CONFIG_PATH variable to the directory containing openssl.pc 
from your build of OpenSSL and run the configure script with --with-ssl only. 
I believe it solves the problem.

Kamil
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Daniel Stenberg | 2 Nov 2009 22:29
Picon
Favicon
Gravatar

Re: CURLINFO_LASTSOCKET returns error 56

On Mon, 2 Nov 2009, Karel K wrote:

>> Or you can just read the include/curl.h header to figure out what 56 is.
> CURLE_RECV_ERROR,              /* 56 - failure in receiving network data */
>
> Any clue how I can further debug this problem??

Repeat the case, single-step through the code with a debugger. Figure out why 
it fails to receive the data you ask it to.

--

-- 

  / daniel.haxx.se
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Christian Schmitz | 2 Nov 2009 22:32
Picon
Favicon

Progress while connecting?

Hello,

I'm using CURL library in one of my applications.
It looks like for a FTP address the time needed for the login and  
changing path and other setup stuff takes a few seconds where my  
application is blocked.

Do you know how to get some callback for updating the GUI while  
connecting?

This is before the progress events start so I thought about modifying  
the library to call some new callback for progress while connecting.

Greetings
Christian

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html


Gmane