Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Functions
Received Data Callback

Data Structures

struct  curl_write_data
 Context structure passed to ast_curl_write_default_cb. More...
 

Functions

size_t curl_write_cb (char *data, size_t size, size_t nmemb, void *clientp)
 A default implementation of a write data callback. More...
 
void curl_write_data_free (void *obj)
 

Detailed Description

If you need to do something with the data received other than save it in a memory buffer, you can define a callback that curl will call for each "chunk" of data it receives from the server.

Your callback must follow the specification defined for CURLOPT_WRITEFUNCTION and implement the 'curl_write_callback' prototype.

The following ast_curl_write objects compose a default implementation that will write the data to any FILE * descriptor you choose.

Function Documentation

size_t curl_write_cb ( char *  data,
size_t  size,
size_t  nmemb,
void *  clientp 
)

A default implementation of a write data callback.

This is a default implementation of the function described by CURLOPT_WRITEFUNCTION that writes data received to a user-provided FILE *. This function is called by curl itself when it determines it has enough data to warrant a write. This may be influenced by the value of ast_curl_optional_data.per_write_buffer_size. See the CURLOPT_WRITEFUNCTION documentation for more info.

The curl prototype for this function is 'curl_write_callback'

Parameters
dataData read by curl.
sizeAlways 1.
nitemsThe number of bytes read.
client_dataA pointer to whatever structure you passed to ast_curler in the curl_write_data parameter.
Returns
Number of bytes handled. Must be (size * nitems) or an error is signalled.

Definition at line 150 of file curl_utils.c.

References curl_write_data::bytes_downloaded, curl_write_data::debug_info, curl_write_data::max_download_bytes, curl_write_data::output, S_OR, curl_write_data::stream_buffer, and curl_write_data::stream_bytes_downloaded.

Referenced by curler().

152 {
153  struct curl_write_data *cb_data = client_data;
154  size_t realsize = size * nmemb;
155  size_t bytes_written = 0;
156  char *debug_info = S_OR(cb_data->debug_info, "");
157  SCOPE_ENTER(5, "'%s': Writing data chunk of %zu bytes\n",
158  debug_info, realsize);
159 
160  if (!cb_data->output) {
161  cb_data->output = open_memstream(
162  &cb_data->stream_buffer,
163  &cb_data->stream_bytes_downloaded);
164  if (!cb_data->output) {
165  SCOPE_EXIT_LOG_RTN_VALUE(CURL_WRITEFUNC_ERROR, LOG_WARNING,
166  "'%s': Xfer failed. "
167  "open_memstream failed: %s\n", debug_info, strerror(errno));
168  }
169  cb_data->_internal_memstream = 1;
170  }
171 
172  if (cb_data->max_download_bytes > 0 &&
173  cb_data->stream_bytes_downloaded + realsize >
174  cb_data->max_download_bytes) {
175  SCOPE_EXIT_LOG_RTN_VALUE(CURL_WRITEFUNC_ERROR, LOG_WARNING,
176  "'%s': Xfer failed. "
177  "Exceeded maximum %zu bytes transferred\n", debug_info,
178  cb_data->max_download_bytes);
179  }
180 
181  bytes_written = fwrite(data, 1, realsize, cb_data->output);
182  cb_data->bytes_downloaded += bytes_written;
183  if (bytes_written != realsize) {
184  SCOPE_EXIT_LOG_RTN_VALUE(CURL_WRITEFUNC_ERROR, LOG_WARNING,
185  "'%s': Xfer failed. "
186  "Expected to write %zu bytes but wrote %zu\n",
187  debug_info, realsize, bytes_written);
188  }
189 
190  SCOPE_EXIT_RTN_VALUE(realsize, "Wrote %zu bytes\n", bytes_written);
191 }
char * stream_buffer
Definition: curl_utils.h:276
Context structure passed to ast_curl_write_default_cb.
Definition: curl_utils.h:245
size_t max_download_bytes
Definition: curl_utils.h:250
size_t bytes_downloaded
Definition: curl_utils.h:267
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:80
char * debug_info
Definition: curl_utils.h:260
size_t stream_bytes_downloaded
Definition: curl_utils.h:281