Download JSON using libcurl. More...
Data Structures | |
| struct | GNUNET_CURL_DownloadBuffer |
| Buffer data structure we use to buffer the HTTP download before giving it to the JSON parser. More... | |
| struct | GNUNET_CURL_StreamHandlers |
| What to call for a job added with GNUNET_CURL_job_add_stream(). More... | |
Macros | |
| #define | GNUNET_CURL_STREAM_PAUSE CURL_WRITEFUNC_PAUSE |
| Return value from a GNUNET_CURL_StreamCallback asking for delivery to stop until GNUNET_CURL_job_set_paused() is called. | |
Typedefs | |
| typedef void(* | GNUNET_CURL_RescheduleCallback) (void *cls) |
| Function called by the context to ask for the event loop to be rescheduled, that is the application should call GNUNET_CURL_get_select_info() as the set of sockets we care about just changed. | |
| typedef void *(* | GNUNET_CURL_RawParser) (struct GNUNET_CURL_DownloadBuffer *db, CURL *eh, long *response_code) |
| Parses the raw response we got from the Web server. | |
| typedef void(* | GNUNET_CURL_ResponseCleaner) (void *response) |
| Deallocate the response. | |
| typedef void(* | GNUNET_CURL_JobCompletionCallback) (void *cls, long response_code, const void *response) |
| Function to call upon completion of a job. | |
| typedef void(* | GNUNET_CURL_RawJobCompletionCallback) (void *cls, long response_code, const void *body, size_t body_size) |
| Function to call upon completion of a raw job. | |
| typedef enum GNUNET_GenericReturnValue(* | GNUNET_CURL_StreamHeaderCallback) (void *cls, const char *line, size_t line_size) |
| Function called with one raw header line of the response, as it arrives. | |
| typedef size_t(* | GNUNET_CURL_StreamCallback) (void *cls, const void *data, size_t data_size) |
| Function called with a chunk of the response body as it arrives. | |
Functions | |
| struct GNUNET_CURL_Context * | GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb, void *cb_cls) |
| Initialise this library. | |
| void | GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx, fd_set *read_fd_set, fd_set *write_fd_set, fd_set *except_fd_set, int *max_fd, long *timeout) |
| Obtain the information for a select() call to wait until GNUNET_CURL_perform() is ready again. | |
| enum GNUNET_GenericReturnValue | GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx, const char *header) |
| Add custom request header. | |
| void | GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx) |
| Run the main event loop for the CURL interaction. | |
| void | GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx, GNUNET_CURL_RawParser rp, GNUNET_CURL_ResponseCleaner rc) |
| Run the main event loop for the HTTP interaction. | |
| void | GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx) |
| Cleanup library initialisation resources. | |
| struct GNUNET_CURL_Job * | GNUNET_CURL_job_add_stream (struct GNUNET_CURL_Context *ctx, CURL *eh, const struct curl_slist *job_headers, const struct GNUNET_CURL_StreamHandlers *sh) |
| Schedule a CURL request whose response is delivered incrementally, rather than buffered in full and handed over at the end. | |
| void | GNUNET_CURL_job_set_paused (struct GNUNET_CURL_Job *job, bool recv_paused, bool send_paused) |
| Say which directions of job are to be paused from now on. | |
| struct GNUNET_CURL_Job * | GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx, CURL *eh, GNUNET_CURL_JobCompletionCallback jcc, void *jcc_cls) |
| Schedule a CURL request to be executed and call the given jcc upon its completion. | |
| struct GNUNET_CURL_Job * | GNUNET_CURL_job_add_with_ct_json (struct GNUNET_CURL_Context *ctx, CURL *eh, GNUNET_CURL_JobCompletionCallback jcc, void *jcc_cls) |
| Schedule a CURL request to be executed and call the given jcc upon its completion. | |
| void | GNUNET_CURL_set_userpass (struct GNUNET_CURL_Context *ctx, const char *userpass) |
| Force use of the provided username and password for client authentication for all operations performed with ctx. | |
| void | GNUNET_CURL_set_tlscert (struct GNUNET_CURL_Context *ctx, const char *certtype, const char *certfile, const char *keyfile, const char *keypass) |
| Force use of the provided TLS client certificate for client authentication for all operations performed with ctx. | |
| struct GNUNET_CURL_Job * | GNUNET_CURL_job_add2 (struct GNUNET_CURL_Context *ctx, CURL *eh, const struct curl_slist *job_headers, GNUNET_CURL_JobCompletionCallback jcc, void *jcc_cls) |
| Schedule a CURL request to be executed and call the given jcc upon its completion. | |
| struct GNUNET_CURL_Job * | GNUNET_CURL_job_add_raw (struct GNUNET_CURL_Context *ctx, CURL *eh, const struct curl_slist *job_headers, GNUNET_CURL_RawJobCompletionCallback jcc, void *jcc_cls) |
| Schedule a CURL request to be executed and call the given jcc upon its completion. | |
| void | GNUNET_CURL_extend_headers (struct GNUNET_CURL_Job *job, const struct curl_slist *extra_headers) |
| Add extra_headers to the HTTP headers for job. | |
| void | GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job) |
| Cancel a job. | |
| struct GNUNET_CURL_RescheduleContext * | GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx) |
| Initialize reschedule context. | |
| struct GNUNET_CURL_RescheduleContext * | GNUNET_CURL_gnunet_rc_create_with_parser (struct GNUNET_CURL_Context *ctx, GNUNET_CURL_RawParser rp, GNUNET_CURL_ResponseCleaner rc) |
| Initialize reschedule context; with custom response parser. | |
| void | GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc) |
| Destroy reschedule context. | |
| void | GNUNET_CURL_gnunet_scheduler_reschedule (void *cls) |
| Implementation of the GNUNET_CURL_RescheduleCallback for GNUnet's scheduler. | |
| void | GNUNET_CURL_enable_async_scope_header (struct GNUNET_CURL_Context *ctx, const char *header_name) |
| Enable sending the async scope ID as a header. | |
| enum GNUNET_GenericReturnValue | GNUNET_CURL_is_valid_scope_id (const char *scope_id) |
| Return GNUNET_YES if given a valid scope ID and GNUNET_NO otherwise. | |
Download JSON using libcurl.
| #define GNUNET_CURL_STREAM_PAUSE CURL_WRITEFUNC_PAUSE |
Return value from a GNUNET_CURL_StreamCallback asking for delivery to stop until GNUNET_CURL_job_set_paused() is called.
Nothing is lost: the same bytes are offered again after the job is resumed.
Definition at line 257 of file gnunet_curl_lib.h.
| typedef void(* GNUNET_CURL_RescheduleCallback) (void *cls) |
Function called by the context to ask for the event loop to be rescheduled, that is the application should call GNUNET_CURL_get_select_info() as the set of sockets we care about just changed.
| cls | closure |
Definition at line 48 of file gnunet_curl_lib.h.
| typedef void *(* GNUNET_CURL_RawParser) (struct GNUNET_CURL_DownloadBuffer *db, CURL *eh, long *response_code) |
Parses the raw response we got from the Web server.
| db | the raw data |
| eh | handle |
| response_code | HTTP response code |
Definition at line 83 of file gnunet_curl_lib.h.
| typedef void(* GNUNET_CURL_ResponseCleaner) (void *response) |
Deallocate the response.
| response | object to clean |
Definition at line 93 of file gnunet_curl_lib.h.
| typedef void(* GNUNET_CURL_JobCompletionCallback) (void *cls, long response_code, const void *response) |
Function to call upon completion of a job.
| cls | closure |
| response_code | HTTP response code from server, 0 on hard error |
| response | in JSON, NULL if response was not in JSON format |
Definition at line 200 of file gnunet_curl_lib.h.
| typedef void(* GNUNET_CURL_RawJobCompletionCallback) (void *cls, long response_code, const void *body, size_t body_size) |
Function to call upon completion of a raw job.
| cls | closure |
| response_code | HTTP response code from server, 0 on hard error |
| body | http body of the response |
| body_size | number of bytes in body |
Definition at line 214 of file gnunet_curl_lib.h.
| typedef enum GNUNET_GenericReturnValue(* GNUNET_CURL_StreamHeaderCallback) (void *cls, const char *line, size_t line_size) |
Function called with one raw header line of the response, as it arrives.
Only used by GNUNET_CURL_job_add_stream().
Everything the server sends in a header section is passed on, which is more than just the fields of the final response:
A caller that wants "the headers of the final response" therefore has to track which section it is in; there is no other way to tell a trailer from a header, or a 1xx field from a real one.
| cls | closure |
| line | the header line; NOT 0-terminated, and still carrying its CRLF |
| line_size | number of bytes in line |
Definition at line 214 of file gnunet_curl_lib.h.
Function called with a chunk of the response body as it arrives.
Only used by GNUNET_CURL_job_add_stream().
| cls | closure |
| data | the bytes |
| data_size | number of bytes in data |
Definition at line 272 of file gnunet_curl_lib.h.
| struct GNUNET_CURL_Context * GNUNET_CURL_init | ( | GNUNET_CURL_RescheduleCallback | cb, |
| void * | cb_cls | ||
| ) |
Initialise this library.
This function should be called before using any of the following functions.
| cb | function to call when rescheduling is required |
| cb_cls | closure for cb |
Definition at line 259 of file curl.c.
References GNUNET_CURL_Context::cb, GNUNET_CURL_Context::cb_cls, ctx, curl_fail, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_new, multi, and GNUNET_CURL_Context::share.
| void GNUNET_CURL_get_select_info | ( | struct GNUNET_CURL_Context * | ctx, |
| fd_set * | read_fd_set, | ||
| fd_set * | write_fd_set, | ||
| fd_set * | except_fd_set, | ||
| int * | max_fd, | ||
| long * | timeout | ||
| ) |
Obtain the information for a select() call to wait until GNUNET_CURL_perform() is ready again.
Basically, a client should use this API to prepare for select(), then block on select(), then call GNUNET_CURL_perform() and then start again until the work with the context is done.
This function will NOT zero out the sets and assumes that max_fd and timeout are already set to minimal applicable values. It is safe to give this API FD-sets and max_fd and timeout that are already initialized to some other descriptors that need to go into the select() call.
| ctx | context to get the event loop information for |
| read_fd_set | will be set for any pending read operations |
| write_fd_set | will be set for any pending write operations |
| except_fd_set | is here because curl_multi_fdset() has this argument |
| max_fd | set to the highest FD included in any set; if the existing sets have no FDs in it, the initial value should be "-1". (Note that max_fd + 1 will need to be passed to select().) |
| timeout | set to the timeout in milliseconds (!); -1 means no timeout (NULL, blocking forever is OK), 0 means to proceed immediately with GNUNET_CURL_perform(). |
Definition at line 1040 of file curl.c.
References ctx, GNUNET_assert, GNUNET_MAX, m, and timeout.
Referenced by context_task().
| enum GNUNET_GenericReturnValue GNUNET_CURL_append_header | ( | struct GNUNET_CURL_Context * | ctx, |
| const char * | header | ||
| ) |
Add custom request header.
| ctx | cURL context. |
| header | header string; will be given to the context AS IS. |
Definition at line 909 of file curl.c.
References ctx, GNUNET_break, GNUNET_OK, and GNUNET_SYSERR.
| void GNUNET_CURL_perform | ( | struct GNUNET_CURL_Context * | ctx | ) |
Run the main event loop for the CURL interaction.
| ctx | the library context |
Definition at line 1031 of file curl.c.
References ctx, GNUNET_CURL_download_get_result_(), and GNUNET_CURL_perform2().
| void GNUNET_CURL_perform2 | ( | struct GNUNET_CURL_Context * | ctx, |
| GNUNET_CURL_RawParser | rp, | ||
| GNUNET_CURL_ResponseCleaner | rc | ||
| ) |
Run the main event loop for the HTTP interaction.
| ctx | the library context |
| rp | parses the raw response returned from the Web server. |
| rc | cleans/frees the response |
Definition at line 927 of file curl.c.
References ctx, duration, GNUNET_assert, GNUNET_break, GNUNET_CURL_job_cancel(), GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_TIME_absolute_get_duration(), GNUNET_TIME_relative2s(), job, response, and rp.
Referenced by context_task(), and GNUNET_CURL_perform().
| void GNUNET_CURL_fini | ( | struct GNUNET_CURL_Context * | ctx | ) |
Cleanup library initialisation resources.
This function should be called after using this library to cleanup the resources occupied during library's initialisation.
| ctx | the library context |
Definition at line 1073 of file curl.c.
References ctx, GNUNET_assert, and GNUNET_free.
| struct GNUNET_CURL_Job * GNUNET_CURL_job_add_stream | ( | struct GNUNET_CURL_Context * | ctx, |
| CURL * | eh, | ||
| const struct curl_slist * | job_headers, | ||
| const struct GNUNET_CURL_StreamHandlers * | sh | ||
| ) |
Schedule a CURL request whose response is delivered incrementally, rather than buffered in full and handed over at the end.
Use this when the response may be larger than memory, or when it has to start reaching its own consumer before the server has finished sending it. GNUNET_CURL_job_add_raw() is the buffering counterpart and remains the right choice for anything that fits.
The job takes over CURLOPT_WRITEFUNCTION, CURLOPT_WRITEDATA, CURLOPT_HEADERFUNCTION, CURLOPT_HEADERDATA and CURLOPT_PRIVATE on eh; setting any of them on the caller's side has no effect. CURLOPT_READFUNCTION is left alone, so a request body is still the caller's to supply, and it may pause the upload with CURL_READFUNC_PAUSE and resume it with GNUNET_CURL_job_set_paused().
| ctx | context to execute the job in |
| eh | curl easy handle for the request, will be executed AND cleaned up |
| job_headers | extra headers to add for this request |
| sh | what to call as the response arrives; copied, so it need not outlive this call |
Definition at line 633 of file curl.c.
References ctx, GNUNET_assert, job, setup_job(), setup_job_headers(), and sh.
| void GNUNET_CURL_job_set_paused | ( | struct GNUNET_CURL_Job * | job, |
| bool | recv_paused, | ||
| bool | send_paused | ||
| ) |
Say which directions of job are to be paused from now on.
A job pauses itself by returning GNUNET_CURL_STREAM_PAUSE from its GNUNET_CURL_StreamCallback, or CURL_READFUNC_PAUSE from its CURLOPT_READFUNCTION; this is how it is let go again. The state is absolute, not a change: pass what should be true afterwards, so resuming everything is (false, false) and resuming only the receive side of a job whose send side is still paused is (false, true).
Deliberately not a "which direction(s) to resume" bitmask, which is how curl_easy_pause() reads at first glance and is not what it means. Its CURLPAUSE_RECV_CONT, CURLPAUSE_SEND_CONT and CURLPAUSE_CONT are all defined as 0 — a bitmask names the directions that stay paused — so "resume just the receive side" has no spelling there at all, and the natural-looking one silently resumes both. Two booleans cannot be got backwards without saying something visibly wrong.
Two further things are easy to get wrong:
curl_easy_pause(), so the stream callback (or the read callback) can run, and can pause again, before this function returns. A caller that is itself inside a callback of some other event source should defer this to its own event loop rather than call it there.| job | job whose pause state to set |
| recv_paused | true to hold the receive side paused |
| send_paused | true to hold the send side paused |
Definition at line 657 of file curl.c.
References GNUNET_break, and job.
| struct GNUNET_CURL_Job * GNUNET_CURL_job_add | ( | struct GNUNET_CURL_Context * | ctx, |
| CURL * | eh, | ||
| GNUNET_CURL_JobCompletionCallback | jcc, | ||
| void * | jcc_cls | ||
| ) |
Schedule a CURL request to be executed and call the given jcc upon its completion.
Note that the context will make use of the CURLOPT_PRIVATE facility of the CURL eh.
| ctx | context to execute the job in |
| eh | curl easy handle for the request, will be executed AND cleaned up |
| jcc | callback to invoke upon completion |
| jcc_cls | closure for jcc |
Definition at line 753 of file curl.c.
References ctx, GNUNET_CURL_job_add2(), GNUNET_CURL_Job::jcc, and GNUNET_CURL_Job::jcc_cls.
| struct GNUNET_CURL_Job * GNUNET_CURL_job_add_with_ct_json | ( | struct GNUNET_CURL_Context * | ctx, |
| CURL * | eh, | ||
| GNUNET_CURL_JobCompletionCallback | jcc, | ||
| void * | jcc_cls | ||
| ) |
Schedule a CURL request to be executed and call the given jcc upon its completion.
Note that the context will make use of the CURLOPT_PRIVATE facility of the CURL eh.
This function modifies the CURL handle to add the "Content-Type: application/json" header.
| ctx | context to execute the job in |
| eh | curl easy handle for the request, will be executed AND cleaned up |
| jcc | callback to invoke upon completion |
| jcc_cls | closure for jcc |
Definition at line 730 of file curl.c.
References ctx, GNUNET_assert, GNUNET_CURL_job_add2(), and job.
| void GNUNET_CURL_set_userpass | ( | struct GNUNET_CURL_Context * | ctx, |
| const char * | userpass | ||
| ) |
Force use of the provided username and password for client authentication for all operations performed with ctx.
| ctx | context to set authentication data for |
| userpass | string with "$USERNAME:$PASSWORD" |
Definition at line 227 of file curl.c.
References ctx, GNUNET_free, and GNUNET_strdup.
| void GNUNET_CURL_set_tlscert | ( | struct GNUNET_CURL_Context * | ctx, |
| const char * | certtype, | ||
| const char * | certfile, | ||
| const char * | keyfile, | ||
| const char * | keypass | ||
| ) |
Force use of the provided TLS client certificate for client authentication for all operations performed with ctx.
Note that if the provided information is incorrect, the earliest operation that could fail is GNUNET_CURL_job_add() or GNUNET_CURL_job_add2()!
| ctx | context to set authentication data for |
| certtype | type of the certificate |
| certfile | file with the certificate |
| keyfile | file with the private key |
| keypass | passphrase to decrypt keyfile (or NULL) |
Definition at line 237 of file curl.c.
References ctx, GNUNET_free, and GNUNET_strdup.
| struct GNUNET_CURL_Job * GNUNET_CURL_job_add2 | ( | struct GNUNET_CURL_Context * | ctx, |
| CURL * | eh, | ||
| const struct curl_slist * | job_headers, | ||
| GNUNET_CURL_JobCompletionCallback | jcc, | ||
| void * | jcc_cls | ||
| ) |
Schedule a CURL request to be executed and call the given jcc upon its completion.
Note that the context will make use of the CURLOPT_PRIVATE facility of the CURL eh.
| ctx | context to execute the job in |
| eh | curl easy handle for the request, will be executed AND cleaned up |
| job_headers | extra headers to add for this request |
| jcc | callback to invoke upon completion |
| jcc_cls | closure for jcc |
Definition at line 678 of file curl.c.
References ctx, GNUNET_assert, job, setup_job(), and setup_job_headers().
Referenced by GNUNET_CURL_job_add(), and GNUNET_CURL_job_add_with_ct_json().
| struct GNUNET_CURL_Job * GNUNET_CURL_job_add_raw | ( | struct GNUNET_CURL_Context * | ctx, |
| CURL * | eh, | ||
| const struct curl_slist * | job_headers, | ||
| GNUNET_CURL_RawJobCompletionCallback | jcc, | ||
| void * | jcc_cls | ||
| ) |
Schedule a CURL request to be executed and call the given jcc upon its completion.
Note that the context will make use of the CURLOPT_PRIVATE facility of the CURL eh. Used to download resources that are NOT in JSON. The raw body will be returned.
| ctx | context to execute the job in |
| eh | curl easy handle for the request, will be executed AND cleaned up |
| job_headers | extra headers to add for this request |
| jcc | callback to invoke upon completion |
| jcc_cls | closure for jcc |
Definition at line 608 of file curl.c.
References ctx, GNUNET_assert, job, setup_job(), and setup_job_headers().
| void GNUNET_CURL_extend_headers | ( | struct GNUNET_CURL_Job * | job, |
| const struct curl_slist * | extra_headers | ||
| ) |
Add extra_headers to the HTTP headers for job.
| [in,out] | job | the job to modify |
| extra_headers | headers to append |
Definition at line 586 of file curl.c.
References GNUNET_assert, GNUNET_break, and job.
| void GNUNET_CURL_job_cancel | ( | struct GNUNET_CURL_Job * | job | ) |
Cancel a job.
Must only be called before the job completion callback is called for the respective job.
| job | job to cancel |
Definition at line 767 of file curl.c.
References ctx, GNUNET_break, GNUNET_CONTAINER_DLL_remove, GNUNET_free, and job.
Referenced by GNUNET_CURL_perform2().
| struct GNUNET_CURL_RescheduleContext * GNUNET_CURL_gnunet_rc_create | ( | struct GNUNET_CURL_Context * | ctx | ) |
Initialize reschedule context.
| ctx | context to manage |
Definition at line 87 of file curl_reschedule.c.
References clean_result(), GNUNET_CURL_RescheduleContext::cleaner, ctx, GNUNET_CURL_RescheduleContext::ctx, GNUNET_CURL_download_get_result_(), GNUNET_new, and GNUNET_CURL_RescheduleContext::parser.
| struct GNUNET_CURL_RescheduleContext * GNUNET_CURL_gnunet_rc_create_with_parser | ( | struct GNUNET_CURL_Context * | ctx, |
| GNUNET_CURL_RawParser | rp, | ||
| GNUNET_CURL_ResponseCleaner | rc | ||
| ) |
Initialize reschedule context; with custom response parser.
| ctx | context to manage |
Definition at line 59 of file curl_reschedule.c.
References GNUNET_CURL_RescheduleContext::cleaner, ctx, GNUNET_CURL_RescheduleContext::ctx, GNUNET_new, GNUNET_CURL_RescheduleContext::parser, and rp.
| void GNUNET_CURL_gnunet_rc_destroy | ( | struct GNUNET_CURL_RescheduleContext * | rc | ) |
Destroy reschedule context.
| rc | context to destroy |
Definition at line 100 of file curl_reschedule.c.
References GNUNET_free, GNUNET_SCHEDULER_cancel(), and GNUNET_CURL_RescheduleContext::task.
| void GNUNET_CURL_gnunet_scheduler_reschedule | ( | void * | cls | ) |
Implementation of the GNUNET_CURL_RescheduleCallback for GNUnet's scheduler.
Will run the CURL context using GNUnet's scheduler. Note that you MUST immediately destroy the reschedule context after calling GNUNET_CURL_fini().
| cls | must point to a struct GNUNET_CURL_RescheduleContext * (pointer to a pointer!) |
Definition at line 166 of file curl_reschedule.c.
References context_task(), GNUNET_SCHEDULER_add_now(), GNUNET_SCHEDULER_cancel(), and GNUNET_CURL_RescheduleContext::task.
| void GNUNET_CURL_enable_async_scope_header | ( | struct GNUNET_CURL_Context * | ctx, |
| const char * | header_name | ||
| ) |
| enum GNUNET_GenericReturnValue GNUNET_CURL_is_valid_scope_id | ( | const char * | scope_id | ) |
Return GNUNET_YES if given a valid scope ID and GNUNET_NO otherwise.
See GNUNET_CURL_enable_async_scope_header() for the code that generates such a scope_id in an HTTP header.
Definition at line 302 of file curl.c.
References GNUNET_NO, and GNUNET_YES.