cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: download with http

From: Pingkui HOU <kou_h_at_sunflare.co.jp>
Date: Mon, 29 Mar 2004 09:51:45 +0900

hi Richard,
Thanks for your response.
In fact, my problem is the newbie question. I want to get a 1.4M file from
the Tomcat with a servlet.
This servlet sends the file to my http client using the COS Library.
In the IE browser, a "Save As" download dialog will popup when I access this
servlet by typing the URL: http://localhost:8080/servlet/download

The file can be saved to my disk correctly.
My question is :
When I access the servlet with libCURL library, I can get the first 1.8
Kilobytes of the specified file.

I use the write callback function to get the response. Should I add a loop
in somewhere in order to get the entire file?

Thank you very much.

Any suggestion will be appriciated.

the Code seems like the following:

///CODE////////////
struct OutPool {
  char *headptr;
  size_t sizeleft;
  size_t sizecontent;
};

size_t write_callback( void *ptr, size_t size, size_t nmemb, void *stream)
{
 printf("\n%s\n", "I was called");
 struct OutPool *pool = (struct OutPool *)stream;
 size_t nrcv ;
 nrcv = size*nmemb;
 printf("\n%d\t%d\n", nrcv, pool->sizeleft );
 if( nrcv > pool->sizeleft )
  return -1; //not enough buf

 char* rcvptr = (char*) ptr;
 for( ; nrcv > 0; nrcv-- )
 {
  pool->headptr[0] = *rcvptr;
  rcvptr++;
  pool->headptr++;
  pool->sizecontent++;
 }
 pool->headptr[0] = 0; // zero terminated
 return nrcv;
};

int main(int argc, char *argv[])
{
  CURL *curl;
  CURLcode res;

  CURLM *multi_handle;
  int still_running;

 // struct HttpPost *formpost=NULL;
 // struct HttpPost *lastptr=NULL;
  struct curl_httppost *formpost = NULL;
  struct curl_httppost *lastptr=NULL;
  struct curl_slist *headerlist=NULL;
  char buf[] = "Expect:";

  char *outbuf = new char[1024*2000 + 1];

  struct OutPool pool;

  pool.headptr = outbuf;
  pool.sizeleft = 1024*2000;
  pool.sizecontent = 0;
  /* Fill in the file upload field */
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "DocumentToBeInserted",
               CURLFORM_FILE, "test.cpp",
               CURLFORM_END);

  /* Fill in the filename field */
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "filename",
               CURLFORM_COPYCONTENTS, "test.cpp",
               CURLFORM_END);

  /**/
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "TranDirection",
               CURLFORM_COPYCONTENTS, "E2J",
               CURLFORM_END);

  /* Fill in the submit field too, even if this is rarely needed */
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "submit",
               CURLFORM_COPYCONTENTS, "send",
               CURLFORM_END);

  curl = curl_easy_init();
  multi_handle = curl_multi_init();

  /* initalize custom header list (stating that Expect: 100-continue is not
     wanted */
  headerlist = curl_slist_append(headerlist, buf);
  if(curl && multi_handle) {
    int perform=0;

    /* what URL that receives this POST */
    curl_easy_setopt(curl, CURLOPT_URL,
                     "http://172.16.16.66:8080/servlet/testlibcurl");
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

 ///* we want to use our own write function */
 //modified 20030324 by kou_h
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
 /* pointer to pass to our write function */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &pool);
 ///
    curl_multi_add_handle(multi_handle, curl);

    while(CURLM_CALL_MULTI_PERFORM ==
          curl_multi_perform(multi_handle, &still_running));

    while(still_running) {
      struct timeval timeout;
      int rc; /* select() return code */

      fd_set fdread;
      fd_set fdwrite;
      fd_set fdexcep;
      int maxfd;

      FD_ZERO(&fdread);
      FD_ZERO(&fdwrite);
      FD_ZERO(&fdexcep);

      /* set a suitable timeout to play around with */
      timeout.tv_sec = 1;
      timeout.tv_usec = 0;

      /* get file descriptors from the transfers */
      curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);

      rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);

      switch(rc) {
      case -1:
        /* select error */
  printf("%s\n", "Error");
        break;
      case 0:
        printf("timeout!\n");
      default:
        /* timeout or readable/writable sockets */
        printf("perform!\n");
        while(CURLM_CALL_MULTI_PERFORM ==
              curl_multi_perform(multi_handle, &still_running));
        printf("running: %d!\n", still_running);
        break;
      }
    }

    curl_multi_cleanup(multi_handle);

    /* always cleanup */
    curl_easy_cleanup(curl);

    /* then cleanup the formpost chain */
    curl_formfree(formpost);

    /* free slist */
    curl_slist_free_all (headerlist);
  }
  printf("\nlength: %d \nreponse content:----- \n\n%s\n", pool.sizecontent,
outbuf);
  FILE *fp = fopen("return.doc", "wb");

  if(fopen == NULL) return -1;
  size_t wrtNum = fwrite(outbuf, sizeof(char), pool.sizecontent , fp);
  if(wrtNum != strlen(outbuf))
   return -1;
  fclose(fp);

  if(outbuf) delete[] outbuf;
  return 0;
}

};
///////////////////////END CODE

----- Original Message -----
From: "Richard Bramante" <rbramante_at_hotmail.com>
To: <curl-library_at_cool.haxx.se>
Sent: Friday, March 26, 2004 9:44 PM
Subject: Re: download with http

> In older versions of Tomcat, I know it used a signed 32-bit value for
> Content-Length, so anything over 2GB was problematic. I'm not sure if
this
> is still a problem in the current version of Tomcat, but it could possibly
> explain the issue. Also, are you using the latest curl with the largefile
> fixes?
>
> ----- Original Message -----
>
> > I use libcurl to send message to my Tomcat server to get a file.
> > But I can not get file larger than 2KB.
> > I use Servlet COS to send the file from the server.
> > My servlet program run properly if I use the IE browser.
>
Received on 2004-03-29