aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2019-05-12 21:16:56 +0200
committerFlorian Dold <florian.dold@gmail.com>2019-05-12 21:29:51 +0200
commitd22f368443dabdca0de859f8e87b2be41d75ab1b (patch)
tree17b15a2097f9840917cb597001ff1ce0f0b0ab65 /src
parentd11bcece8423f034ae262c7c7cffc83ada39473a (diff)
downloadgnunet-d22f368443dabdca0de859f8e87b2be41d75ab1b.tar.gz
gnunet-d22f368443dabdca0de859f8e87b2be41d75ab1b.zip
curl: add GNUNET_CURL_job_add2
Diffstat (limited to 'src')
-rw-r--r--src/curl/curl.c63
-rw-r--r--src/include/gnunet_curl_lib.h24
2 files changed, 71 insertions, 16 deletions
diff --git a/src/curl/curl.c b/src/curl/curl.c
index 40e04a9af..0a6cde8a9 100644
--- a/src/curl/curl.c
+++ b/src/curl/curl.c
@@ -112,11 +112,6 @@ struct GNUNET_CURL_Job
112 */ 112 */
113 struct curl_slist *job_headers; 113 struct curl_slist *job_headers;
114 114
115 /**
116 * Header for the async scope id or NULL.
117 */
118 char *aid_header;
119
120}; 115};
121 116
122 117
@@ -274,30 +269,31 @@ download_cb (char *bufptr, size_t size, size_t nitems, void *cls)
274 * @param ctx context to execute the job in 269 * @param ctx context to execute the job in
275 * @param eh curl easy handle for the request, will 270 * @param eh curl easy handle for the request, will
276 * be executed AND cleaned up 271 * be executed AND cleaned up
277 * @param add_json add "application/json" content type header 272 * @param job_headers extra headers to add for this request
278 * @param jcc callback to invoke upon completion 273 * @param jcc callback to invoke upon completion
279 * @param jcc_cls closure for @a jcc 274 * @param jcc_cls closure for @a jcc
280 * @return NULL on error (in this case, @eh is still released!) 275 * @return NULL on error (in this case, @eh is still released!)
281 */ 276 */
282struct GNUNET_CURL_Job * 277struct GNUNET_CURL_Job *
283GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx, 278GNUNET_CURL_job_add2 (struct GNUNET_CURL_Context *ctx,
284 CURL *eh, 279 CURL *eh,
285 int add_json, 280 const struct curl_slist *job_headers,
286 GNUNET_CURL_JobCompletionCallback jcc, 281 GNUNET_CURL_JobCompletionCallback jcc,
287 void *jcc_cls) 282 void *jcc_cls)
288{ 283{
289 struct GNUNET_CURL_Job *job; 284 struct GNUNET_CURL_Job *job;
290 struct curl_slist *all_headers = NULL; 285 struct curl_slist *all_headers = NULL;
291 char *aid_header = NULL;
292 286
293 if (GNUNET_YES == add_json) 287 for (const struct curl_slist *curr = job_headers;
288 curr != NULL;
289 curr = curr->next)
294 { 290 {
295 GNUNET_assert ( 291 GNUNET_assert (
296 NULL != (all_headers = 292 NULL != (all_headers =
297 curl_slist_append (NULL, "Content-Type: application/json"))); 293 curl_slist_append (all_headers, curr->data)));
298 } 294 }
299 295
300 for (struct curl_slist *curr = ctx->common_headers; 296 for (const struct curl_slist *curr = ctx->common_headers;
301 curr != NULL; 297 curr != NULL;
302 curr = curr->next) 298 curr = curr->next)
303 { 299 {
@@ -313,9 +309,12 @@ GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
313 GNUNET_async_scope_get (&scope); 309 GNUNET_async_scope_get (&scope);
314 if (GNUNET_YES == scope.have_scope) 310 if (GNUNET_YES == scope.have_scope)
315 { 311 {
312 char *aid_header = NULL;
316 aid_header = GNUNET_STRINGS_data_to_string_alloc (&scope.scope_id, 313 aid_header = GNUNET_STRINGS_data_to_string_alloc (&scope.scope_id,
317 sizeof (struct GNUNET_AsyncScopeId)); 314 sizeof (struct GNUNET_AsyncScopeId));
318 GNUNET_assert (NULL != curl_slist_append(all_headers, aid_header)); 315 GNUNET_assert (NULL != aid_header);
316 GNUNET_assert (NULL != curl_slist_append (all_headers, aid_header));
317 GNUNET_free (aid_header);
319 } 318 }
320 } 319 }
321 320
@@ -323,14 +322,12 @@ GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
323 { 322 {
324 GNUNET_break (0); 323 GNUNET_break (0);
325 curl_slist_free_all (all_headers); 324 curl_slist_free_all (all_headers);
326 GNUNET_free_non_null (aid_header);
327 curl_easy_cleanup (eh); 325 curl_easy_cleanup (eh);
328 return NULL; 326 return NULL;
329 } 327 }
330 328
331 job = GNUNET_new (struct GNUNET_CURL_Job); 329 job = GNUNET_new (struct GNUNET_CURL_Job);
332 job->job_headers = all_headers; 330 job->job_headers = all_headers;
333 job->aid_header = aid_header;
334 331
335 if ((CURLE_OK != curl_easy_setopt (eh, CURLOPT_PRIVATE, job)) || 332 if ((CURLE_OK != curl_easy_setopt (eh, CURLOPT_PRIVATE, job)) ||
336 (CURLE_OK != 333 (CURLE_OK !=
@@ -356,6 +353,41 @@ GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
356 353
357 354
358/** 355/**
356 * Schedule a CURL request to be executed and call the given @a jcc
357 * upon its completion. Note that the context will make use of the
358 * CURLOPT_PRIVATE facility of the CURL @a eh.
359 *
360 * This function modifies the CURL handle to add the
361 * "Content-Type: application/json" header if @a add_json is set.
362 *
363 * @param ctx context to execute the job in
364 * @param eh curl easy handle for the request, will
365 * be executed AND cleaned up
366 * @param add_json add "application/json" content type header
367 * @param jcc callback to invoke upon completion
368 * @param jcc_cls closure for @a jcc
369 * @return NULL on error (in this case, @eh is still released!)
370 */
371struct GNUNET_CURL_Job *
372GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
373 CURL *eh,
374 int add_json,
375 GNUNET_CURL_JobCompletionCallback jcc,
376 void *jcc_cls)
377{
378 struct curl_slist *job_headers = NULL;
379 if (GNUNET_YES == add_json)
380 {
381 GNUNET_assert (
382 NULL != (job_headers =
383 curl_slist_append (NULL, "Content-Type: application/json")));
384 }
385
386 return GNUNET_CURL_job_add2 (ctx, eh, job_headers, jcc, jcc_cls);
387}
388
389
390/**
359 * Cancel a job. Must only be called before the job completion 391 * Cancel a job. Must only be called before the job completion
360 * callback is called for the respective job. 392 * callback is called for the respective job.
361 * 393 *
@@ -371,7 +403,6 @@ GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job)
371 curl_multi_remove_handle (ctx->multi, job->easy_handle)); 403 curl_multi_remove_handle (ctx->multi, job->easy_handle));
372 curl_easy_cleanup (job->easy_handle); 404 curl_easy_cleanup (job->easy_handle);
373 GNUNET_free_non_null (job->db.buf); 405 GNUNET_free_non_null (job->db.buf);
374 GNUNET_free_non_null (job->aid_header);
375 curl_slist_free_all (job->job_headers); 406 curl_slist_free_all (job->job_headers);
376 GNUNET_free (job); 407 GNUNET_free (job);
377} 408}
diff --git a/src/include/gnunet_curl_lib.h b/src/include/gnunet_curl_lib.h
index 84c8d59f8..0dbbc1741 100644
--- a/src/include/gnunet_curl_lib.h
+++ b/src/include/gnunet_curl_lib.h
@@ -233,6 +233,30 @@ GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
233 233
234 234
235/** 235/**
236 * Schedule a CURL request to be executed and call the given @a jcc
237 * upon its completion. Note that the context will make use of the
238 * CURLOPT_PRIVATE facility of the CURL @a eh.
239 *
240 * This function modifies the CURL handle to add the
241 * "Content-Type: application/json" header if @a add_json is set.
242 *
243 * @param ctx context to execute the job in
244 * @param eh curl easy handle for the request, will
245 * be executed AND cleaned up
246 * @param job_headers extra headers to add for this request
247 * @param jcc callback to invoke upon completion
248 * @param jcc_cls closure for @a jcc
249 * @return NULL on error (in this case, @eh is still released!)
250 */
251struct GNUNET_CURL_Job *
252GNUNET_CURL_job_add2 (struct GNUNET_CURL_Context *ctx,
253 CURL *eh,
254 const struct curl_slist *job_headers,
255 GNUNET_CURL_JobCompletionCallback jcc,
256 void *jcc_cls);
257
258
259/**
236 * Cancel a job. Must only be called before the job completion 260 * Cancel a job. Must only be called before the job completion
237 * callback is called for the respective job. 261 * callback is called for the respective job.
238 * 262 *