libcurl (c api) READFUNCTION для блокировки HTTP PUT навсегда - PullRequest
0 голосов
/ 10 июня 2010

Я использую libcurl для библиотеки RESTful.У меня две проблемы с сообщением PUT, я просто пытаюсь отправить небольшой контент, такой как "привет", через put.

  1. Моя функция READFUNCTION для блоков PUT в течение очень большого количества времени(минут), когда я следую инструкциям на curl.haxx.se и возвращаю 0, указывающий, что я закончил содержание.(на ОС X) Когда я возвращаю что-то> 0, это происходит намного быстрее (<1 сек) </p>

  2. Когда я запускаю это на моей машине с Linux (Ubuntu 10.4), это событие блокировки НИКОГДА не появляетсявозвращать, когда я возвращаю 0, если я изменяю поведение, чтобы оно возвращало записанный размер. libcurl добавляет все данные в тело http, отправляя больше данных, и завершается неудачно с сообщением «слишком много данных» с сервера.моя функция чтения ниже, любая помощь будет принята с благодарностью.Я использую libcurl 7.20.1


    typedef struct{
        void *data;
        int body_size;
        int bytes_remaining;
        int bytes_written;
    } postdata;</p>

<pre><code>size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream) {

if(stream) {
    postdata *ud = (postdata*)stream;

    if(ud->bytes_remaining) {
        if(ud->body_size > size*nmemb) {
            memcpy(ptr, ud->data+ud->bytes_written, size*nmemb);
            ud->bytes_written+=size+nmemb;
     ud->bytes_remaining = ud->body_size-size*nmemb;
            return size*nmemb;
 } else {
     memcpy(ptr, ud->data+ud->bytes_written, ud->bytes_remaining);
            ud->bytes_remaining=0;
  return 0;
        }
    }

1 Ответ

0 голосов
/ 10 июня 2010

со страницы руководства (man curl_easy_setopt):

       CURLOPT_READFUNCTION
          Function pointer that should match the following prototype: size_t function( void *ptr, size_t size, size_t nmemb, void *stream); This function gets called by libcurl as soon as
          it  needs  to  read data in order to send it to the peer. The data area pointed at by the pointer ptr may be filled with at most size multiplied with nmemb number of bytes. Your
          function must return the actual number of bytes that you stored in that memory area. Returning 0 will signal end-of-file to the library and cause it to stop the  current  trans-
          fer.

          If  you  stop  the  current  transfer  by returning 0 "pre-maturely" (i.e before the server expected it, like when you've told you will upload N bytes and you upload less than N
          bytes), you may experience that the server "hangs" waiting for the rest of the data that won't come.

          The read callback may return CURL_READFUNC_ABORT to stop the current operation immediately, resulting in a CURLE_ABORTED_BY_CALLBACK error  code  from  the  transfer  (Added  in
          7.12.1)

          If  you  set the callback pointer to NULL, or doesn't set it at all, the default internal read function will be used. It is simply doing an fread() on the FILE * stream set with
          CURLOPT_READDATA.

, поэтому верните CURL_READFUNC_ABORT, чтобы остановить операцию

...