79ec0b7e0185390f28a4de2e4afb9b82a5ccebcc
[project/uclient.git] / uclient.h
1 #ifndef __LIBUBOX_UCLIENT_H
2 #define __LIBUBOX_UCLIENT_H
3
4 #include <libubox/blob.h>
5 #include <libubox/ustream.h>
6 #include <libubox/ustream-ssl.h>
7
8 struct uclient_cb;
9 struct uclient_backend;
10
11 enum uclient_error_code {
12         UCLIENT_ERROR_UNKNOWN,
13         UCLIENT_ERROR_CONNECT,
14         UCLIENT_ERROR_SSL_INVALID_CERT,
15         UCLIENT_ERROR_SSL_CN_MISMATCH,
16 };
17
18 struct uclient {
19         const struct uclient_backend *backend;
20         const struct uclient_cb *cb;
21
22         struct uclient_url *url;
23         void *priv;
24
25         bool eof;
26         int error_code;
27         int status_code;
28         struct blob_attr *meta;
29
30         struct uloop_timeout timeout;
31 };
32
33 struct uclient_cb {
34         void (*data_read)(struct uclient *cl);
35         void (*data_sent)(struct uclient *cl);
36         void (*data_eof)(struct uclient *cl);
37         void (*header_done)(struct uclient *cl);
38         void (*error)(struct uclient *cl, int code);
39 };
40
41 struct uclient *uclient_new(const char *url, const struct uclient_cb *cb);
42 void uclient_free(struct uclient *cl);
43
44 int uclient_connect_url(struct uclient *cl, const char *url_str);
45
46 static inline int uclient_connect(struct uclient *cl)
47 {
48         return uclient_connect_url(cl, NULL);
49 }
50
51
52 int uclient_read(struct uclient *cl, char *buf, int len);
53 int uclient_write(struct uclient *cl, char *buf, int len);
54 int uclient_request(struct uclient *cl);
55
56 /* HTTP */
57 extern const struct uclient_backend uclient_backend_http;
58
59 int uclient_http_set_header(struct uclient *cl, const char *name, const char *value);
60 int uclient_http_reset_headers(struct uclient *cl, const char *name, const char *value);
61 int uclient_http_set_request_type(struct uclient *cl, const char *type);
62 bool uclient_http_redirect(struct uclient *cl);
63
64 int uclient_http_set_ssl_ctx(struct uclient *cl, struct ustream_ssl_ctx *ctx, bool require_validation);
65
66 #endif