Asterisk - The Open Source Telephony Project GIT-master-abe0018
Data Structures | Functions
Received Data Callback
Collaboration diagram for 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

◆ curl_write_cb()

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.

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) {
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) {
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) {
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}
#define CURL_WRITEFUNC_ERROR
Definition: curl_utils.h:28
#define SCOPE_EXIT_RTN_VALUE(__return_value,...)
#define SCOPE_EXIT_LOG_RTN_VALUE(__value, __log_level,...)
#define SCOPE_ENTER(level,...)
#define LOG_WARNING
int errno
#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
Context structure passed to ast_curl_write_default_cb.
Definition: curl_utils.h:245
size_t bytes_downloaded
Definition: curl_utils.h:267
char * stream_buffer
Definition: curl_utils.h:276
size_t max_download_bytes
Definition: curl_utils.h:250
int _internal_memstream
Definition: curl_utils.h:286
char * debug_info
Definition: curl_utils.h:260
size_t stream_bytes_downloaded
Definition: curl_utils.h:281

References curl_write_data::_internal_memstream, curl_write_data::bytes_downloaded, CURL_WRITEFUNC_ERROR, curl_write_data::debug_info, errno, LOG_WARNING, curl_write_data::max_download_bytes, curl_write_data::output, S_OR, SCOPE_ENTER, SCOPE_EXIT_LOG_RTN_VALUE, SCOPE_EXIT_RTN_VALUE, curl_write_data::stream_buffer, and curl_write_data::stream_bytes_downloaded.

Referenced by curler().

◆ curl_write_data_free()

void curl_write_data_free ( void *  obj)

Definition at line 134 of file curl_utils.c.

135{
136 struct curl_write_data *cb_data = obj;
137 if (!cb_data) {
138 return;
139 }
140 if (cb_data->output) {
141 fclose(cb_data->output);
142 }
143 if (cb_data->debug_info) {
144 ast_free(cb_data->debug_info);
145 }
146 ast_std_free(cb_data->stream_buffer);
147 ast_free(cb_data);
148}
void ast_std_free(void *ptr)
Definition: astmm.c:1734
#define ast_free(a)
Definition: astmm.h:180

References ast_free, ast_std_free(), curl_write_data::debug_info, curl_write_data::output, and curl_write_data::stream_buffer.

Referenced by retrieve_cert_from_url().