curl.h (2996B)
1 /*
2 * Copyright (c) 2019-2022 Joris Vink <joris@coders.se>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef __H_CURL_H
18 #define __H_CURL_H
19
20 #if defined(__cplusplus)
21 extern "C" {
22 #endif
23
24 #include <curl/curl.h>
25
26 #include "http.h"
27
28 #define KORE_CURL_TIMEOUT 60
29 #define KORE_CURL_RECV_MAX (1024 * 1024 * 2)
30
31 #define KORE_CURL_FLAG_HTTP_PARSED_HEADERS 0x0001
32 #define KORE_CURL_FLAG_BOUND 0x0002
33
34 #define KORE_CURL_SYNC 0x1000
35 #define KORE_CURL_ASYNC 0x2000
36
37 #define KORE_CURL_TYPE_CUSTOM 1
38 #define KORE_CURL_TYPE_HTTP_CLIENT 2
39
40 struct kore_curl {
41 int type;
42 int flags;
43 CURLcode result;
44
45 char *url;
46 CURL *handle;
47 struct kore_buf *response;
48
49 struct http_request *req;
50 void *arg;
51 void (*cb)(struct kore_curl *, void *);
52
53 char errbuf[CURL_ERROR_SIZE];
54
55 /* For the simplified HTTP api. */
56 struct {
57 long status;
58 struct curl_slist *hdrlist;
59
60 struct kore_buf *tosend;
61 struct kore_buf *headers;
62
63 TAILQ_HEAD(, http_header) resp_hdrs;
64 } http;
65
66 LIST_ENTRY(kore_curl) list;
67 };
68
69 extern u_int16_t kore_curl_timeout;
70 extern u_int64_t kore_curl_recv_max;
71
72 void kore_curl_sysinit(void);
73 void kore_curl_do_timeout(void);
74 void kore_curl_run_scheduled(void);
75 void kore_curl_run(struct kore_curl *);
76 void kore_curl_cleanup(struct kore_curl *);
77 int kore_curl_success(struct kore_curl *);
78 void kore_curl_run_sync(struct kore_curl *);
79 void kore_curl_logerror(struct kore_curl *);
80 int kore_curl_init(struct kore_curl *, const char *, int);
81
82 size_t kore_curl_tobuf(char *, size_t, size_t, void *);
83 size_t kore_curl_frombuf(char *, size_t, size_t, void *);
84
85 void kore_curl_http_parse_headers(struct kore_curl *);
86 void kore_curl_http_set_header(struct kore_curl *, const char *,
87 const char *);
88 int kore_curl_http_get_header(struct kore_curl *, const char *,
89 const char **);
90 void kore_curl_http_setup(struct kore_curl *, int, const void *, size_t);
91
92 char *kore_curl_response_as_string(struct kore_curl *);
93 void kore_curl_response_as_bytes(struct kore_curl *,
94 const u_int8_t **, size_t *);
95
96 void kore_curl_bind_request(struct kore_curl *, struct http_request *);
97 void kore_curl_bind_callback(struct kore_curl *,
98 void (*cb)(struct kore_curl *, void *), void *);
99
100 const char *kore_curl_strerror(struct kore_curl *);
101
102 #if defined(__cplusplus)
103 }
104 #endif
105
106 #endif