cURL / Mailing Lists / curl-library / Single Mail

curl-library

HTTP PUT Resume

From: <lynn_scott_at_juno.com>
Date: Wed, 7 Jan 2004 17:48:43 GMT

Daniel,

Thanks to you and all the other contributors to cURL. I am quite busy but I thought I would pass on some sample code regarding the use of HTTP PUT resume reading from data blocks as opposed to reading from a file stream.

The example is based on some sample code I found on this mailing list. It is little rough and could be cleaned up. It should be straight forward though and was built under MSVC. It could be optimized but I hope it is benificial to you, those who have worked hard on this, and anyone else.

Thanks, Lynn

//Begin
#include "stdafx.h"

#include <stdio.h>

#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <io.h>

#include<stdlib.h>
#include<string.h>

#define BUFSIZE 0x10000
CURL *curl;
size_t dataSize = 0;
FILE * hd_src ;
char * mDataBuf = NULL;

size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
  size_t total = size * nmemb;
  //If the dataSize (incomming) is smaller then this buffer (0x4000 bytes),
  //then use the dataSize
  if (total > dataSize)
  {
    total = dataSize;
  }
  //If we have data to copy
  if (total > 0)
  {
    //Copy 0x4000 bytes to the ptr buffer.
    memcpy(ptr, stream, total);
    //Move the stream to point to 0x4000 bytes in plus the remaining data.
    memcpy(stream, ((char*)stream)+total, dataSize-total);
    dataSize -= total;

   //if we run out of data, refill the buffer
   if (dataSize < 1)
      dataSize = fread (mDataBuf,1, BUFSIZE, hd_src);
  }
    return total;
}

int main(int argc, char **argv)
{
  char *file;
  char *url;

  if(argc < 3)
    return 1;
 
  file= argv[1];
  url = argv[2];

  curl_global_init(CURL_GLOBAL_ALL);
  curl = curl_easy_init();
  
  /* get a FILE * of the same file, could also be made with
     fdopen() from the previous descriptor, but hey this is just
     an example! */
        hd_src = fopen(file, "rb");

        fseek(hd_src, 0, SEEK_END);
        long fileSize = ftell(hd_src);
        fseek(hd_src, 0, SEEK_SET);
        
        mDataBuf = (char *)malloc(BUFSIZE);
        long bytesRead = fread (mDataBuf,1,BUFSIZE,hd_src);
                
        dataSize = bytesRead;

        curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
        curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
        curl_easy_setopt(curl, CURLOPT_PUT, TRUE);
        curl_easy_setopt(curl,CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_INFILESIZE, fileSize);
        
        curl_easy_setopt(curl, CURLOPT_READDATA, mDataBuf);
        curl_easy_perform(curl);
    curl_easy_cleanup(curl);

  fclose(hd_src); /* close the local file */

  curl_global_cleanup();
  free(mDataBuf);
  return 0;

}
//End

-------------------------------------------------------
This SF.net email is sponsored by: Perforce Software.
Perforce is the Fast Software Configuration Management System offering
advanced branching capabilities and atomic changes on 50+ platforms.
Free Eval! http://www.perforce.com/perforce/loadprog.html
Received on 2004-01-08