aboutsummaryrefslogtreecommitdiff
path: root/src/curl/curl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/curl/curl.c')
-rw-r--r--src/curl/curl.c58
1 files changed, 52 insertions, 6 deletions
diff --git a/src/curl/curl.c b/src/curl/curl.c
index da486ecc1..b7452330f 100644
--- a/src/curl/curl.c
+++ b/src/curl/curl.c
@@ -25,6 +25,7 @@
25 */ 25 */
26#include "platform.h" 26#include "platform.h"
27#include <jansson.h> 27#include <jansson.h>
28#include <microhttpd.h>
28#include "gnunet_curl_lib.h" 29#include "gnunet_curl_lib.h"
29 30
30#if ENABLE_BENCHMARK 31#if ENABLE_BENCHMARK
@@ -181,7 +182,8 @@ struct GNUNET_CURL_Context
181 * @return library context 182 * @return library context
182 */ 183 */
183struct GNUNET_CURL_Context * 184struct GNUNET_CURL_Context *
184GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb, void *cb_cls) 185GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
186 void *cb_cls)
185{ 187{
186 struct GNUNET_CURL_Context *ctx; 188 struct GNUNET_CURL_Context *ctx;
187 CURLM *multi; 189 CURLM *multi;
@@ -263,7 +265,10 @@ GNUNET_CURL_is_valid_scope_id (const char *scope_id)
263 * @return number of bytes processed from @a bufptr 265 * @return number of bytes processed from @a bufptr
264 */ 266 */
265static size_t 267static size_t
266download_cb (char *bufptr, size_t size, size_t nitems, void *cls) 268download_cb (char *bufptr,
269 size_t size,
270 size_t nitems,
271 void *cls)
267{ 272{
268 struct GNUNET_CURL_DownloadBuffer *db = cls; 273 struct GNUNET_CURL_DownloadBuffer *db = cls;
269 size_t msize; 274 size_t msize;
@@ -376,7 +381,9 @@ setup_job (CURL *eh,
376 } 381 }
377 job->easy_handle = eh; 382 job->easy_handle = eh;
378 job->ctx = ctx; 383 job->ctx = ctx;
379 GNUNET_CONTAINER_DLL_insert (ctx->jobs_head, ctx->jobs_tail, job); 384 GNUNET_CONTAINER_DLL_insert (ctx->jobs_head,
385 ctx->jobs_tail,
386 job);
380 return job; 387 return job;
381} 388}
382 389
@@ -497,7 +504,11 @@ GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
497 curl_slist_append (NULL, "Content-Type: application/json"))); 504 curl_slist_append (NULL, "Content-Type: application/json")));
498 } 505 }
499 506
500 job = GNUNET_CURL_job_add2 (ctx, eh, job_headers, jcc, jcc_cls); 507 job = GNUNET_CURL_job_add2 (ctx,
508 eh,
509 job_headers,
510 jcc,
511 jcc_cls);
501 curl_slist_free_all (job_headers); 512 curl_slist_free_all (job_headers);
502 return job; 513 return job;
503} 514}
@@ -526,6 +537,40 @@ GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job)
526 537
527 538
528/** 539/**
540 * Test if the given content type @a ct is JSON
541 *
542 * @param ct a content type, i.e. "application/json; charset=UTF-8"
543 * @return true if @a ct denotes JSON
544 */
545static bool
546is_json (const char *ct)
547{
548 const char *semi;
549
550 /* check for "application/json" exact match */
551 if (0 == strcasecmp (ct,
552 "application/json"))
553 return true;
554 /* check for "application/json;[ANYTHING]" */
555 semi = strchr (ct,
556 ';');
557 /* also allow "application/json [ANYTHING]" (note the space!) */
558 if (NULL == semi)
559 semi = strchr (ct,
560 ' ');
561 if (NULL == semi)
562 return false; /* no delimiter we accept, forget it */
563 if (semi - ct != strlen ("application/json"))
564 return false; /* delimiter past desired length, forget it */
565 if (0 == strncasecmp (ct,
566 "application/json",
567 strlen ("application/json")))
568 return true; /* OK */
569 return false;
570}
571
572
573/**
529 * Obtain information about the final result about the 574 * Obtain information about the final result about the
530 * HTTP download. If the download was successful, parses 575 * HTTP download. If the download was successful, parses
531 * the JSON in the @a db and returns it. Also returns 576 * the JSON in the @a db and returns it. Also returns
@@ -562,8 +607,7 @@ GNUNET_CURL_download_get_result_ (struct GNUNET_CURL_DownloadBuffer *db,
562 CURLINFO_CONTENT_TYPE, 607 CURLINFO_CONTENT_TYPE,
563 &ct)) || 608 &ct)) ||
564 (NULL == ct) || 609 (NULL == ct) ||
565 (0 != strcasecmp (ct, 610 (! is_json (ct)))
566 "application/json")))
567 { 611 {
568 /* No content type or explicitly not JSON, refuse to parse 612 /* No content type or explicitly not JSON, refuse to parse
569 (but keep response code) */ 613 (but keep response code) */
@@ -583,6 +627,8 @@ GNUNET_CURL_download_get_result_ (struct GNUNET_CURL_DownloadBuffer *db,
583 (const char *) db->buf); 627 (const char *) db->buf);
584 return NULL; 628 return NULL;
585 } 629 }
630 if (MHD_HTTP_NO_CONTENT == *response_code)
631 return NULL;
586 json = NULL; 632 json = NULL;
587 if (0 == db->eno) 633 if (0 == db->eno)
588 { 634 {