Page 1 of 1

CURL 's readback function

Posted: Fri Sep 26, 2014 4:06 am
by lilzz

Code: Select all

        const char data[]="this is what we post to the silly web server";
         
        struct WriteThis {
          const char *readptr;
          long 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! */ 
          }
         
          return 0;                          /* no more data left to deliver */ 
        }
        
     struct WriteThis pooh;
     
      pooh.readptr = data;
      pooh.sizeleft = (long)strlen(data);
         curl = curl_easy_init();
          if(curl) {
            /* First set the URL that is about to receive our POST. */ 
            curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/index.cgi");
         
            /* Now specify we want to POST data */ 
            curl_easy_setopt(curl, CURLOPT_POST, 1L);
         
            /* we want to use our own read function */ 
            curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
        
        
        
        };
I do not see how the parameters of read_callback () got initialized. ptr, size and nmmemb and usurp.