cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Closing connection #0

From: Peter Evertz <leo2_at_pec.homeip.net>
Date: Fri, 11 Apr 2008 18:42:10 +0200

Daniel Stenberg schrieb:
> On Fri, 11 Apr 2008, Peter Evertz wrote:
>
>> Are you using a POST ? If yes, you are running into the same problem
>> I have:
>
> libcurl doesn't close connections just because you POST.
>
>> But libcurl always closes the connection after sending the post.
>
> Can you show us a source code repeating this problem?
>
>> Is this a bug or did I miss a switch ?
>
> I'm not aware of any such bug.
>
With "verbose on" I see a "* Closing connection #0" directly after the
post. With the "#define UGLYHACK" the connection stays open ( because
libcurl thinks it has to post more data) and a response call is received.

The server is a tomcat as part of a jboss server.

Thank you for your time !

Peter

/*****************************************************************************
 * _ _ ____ _
 * Project ___| | | | _ \| |
 * / __| | | | |_) | |
 * | (__| |_| | _ <| |___
 * \___|\___/|_| \_\_____|
 *
 * $Id: post-callback.c,v 1.8 2008-03-13 12:36:22 bagder Exp $
 *
 * An example source code that issues a HTTP POST and we provide the actual
 * data through a read callback.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

//#define UGLYHACK

struct MemoryStruct {
  char *memory;
  size_t size;
};

static void *myrealloc(void *ptr, size_t size)
{
  /* There might be a realloc() out there that doesn't like reallocing
     NULL pointers, so we take care of it here */
  if(ptr)
    return realloc(ptr, size);
  else
    return malloc(size);
}

CURL *curl;

static size_t
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
    printf("write_callback\n");
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)data;

  mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
  if (mem->memory) {
    memcpy(&(mem->memory[mem->size]), ptr, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;
  }
#ifdef UGLYHACK
    curl_easy_pause(curl , CURLPAUSE_CONT );
#endif
  return realsize;
}

const char data[]="login!host=localhost;system=ApricoDev;user=foo;pwd=baa";

struct WriteThis {
  const char *readptr;
  int sizeleft;
};

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void
*userp)
{
  struct WriteThis *pooh = (struct WriteThis *)userp;

  if(size*nmemb < 1)
    return 0;

  if(pooh->sizeleft) {
    *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
    pooh->readptr++; /* advance pointer */
    pooh->sizeleft--; /* less data left */
    return 1; /* we return 1 byte at a time! */
  }
#ifdef UGLYHACK
    printf("done and pause\n");
    curl_easy_pause(curl , CURLPAUSE_SEND );
  return 0; /* more data left to deliver */
#else
  return -1; /* no more data left to deliver */
#endif
}

int main(void)
{
  CURLcode res;

    curl_global_init(CURL_GLOBAL_ALL);

  struct WriteThis pooh;

  pooh.readptr = data;
  pooh.sizeleft = strlen(data);

  struct MemoryStruct chunk;
 
  chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
  chunk.size = 0; /* no data at this point */

  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. */
    curl_easy_setopt(curl, CURLOPT_URL,
"http://10.40.137.21:8080/OSAServiceBrokerServlet/ServiceBroker");

    /* Now specify we want to POST data */
    curl_easy_setopt(curl, CURLOPT_POST, 1);

    curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");

    /* we want to use our own read function */
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

    /* pointer to pass to our read function */
    curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);

  /* send all data to this function */
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

  /* we pass our 'chunk' struct to the callback function */
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

    /* get verbose debug output please */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

   curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 0) ;

    struct curl_slist *h = NULL;

    h = curl_slist_append(h, "Content-Type: text/plain");
    h = curl_slist_append(h, "Connection: keep-alive");
 

    /*
      If you use POST to a HTTP 1.1 server, you can send data without
knowing
      the size before starting the POST if you use chunked encoding. You
      enable this by adding a header like "Transfer-Encoding: chunked" with
      CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you
must
      specify the size in the request.
    */
//#define USE_CHUNKED
#ifdef USE_CHUNKED
    {

      h = curl_slist_append(h, "Transfer-Encoding: chunked");
      /* use curl_slist_free_all() after the *perform() call to free this
         list again */
    }
#else
    /* Set the expected POST size. If you want to POST large amounts of
data,
       consider CURLOPT_POSTFIELDSIZE_LARGE */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
#endif
    res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, h);

//#define DISABLE_EXPECT
#ifdef DISABLE_EXPECT
    /*
      Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
      header. You can disable this header with CURLOPT_HTTPHEADER as usual.
      NOTE: if you want chunked transfer too, you need to combine these two
      since you can only set one list of headers with CURLOPT_HTTPHEADER. */

    /* A less good option would be to enforce HTTP 1.0, but that might also
       have other implications. */
    {
      struct curl_slist *chunk = NULL;

      chunk = curl_slist_append(chunk, "Expect:");
      res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
      /* use curl_slist_free_all() after the *perform() call to free this
         list again */
    }
#endif

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);

        if(chunk.size )
            printf("END: %d <%s>\n",chunk.size,chunk.memory);
        else
            printf("END: <null response>\n");

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}
Received on 2008-04-11