60b021e9993c02dbd49cdd341f80d43d10ac8d0f
[project/uclient.git] / uclient.h
1 /*
2  * uclient - ustream based protocol client library
3  *
4  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #ifndef __LIBUBOX_UCLIENT_H
19 #define __LIBUBOX_UCLIENT_H
20
21 #include <libubox/blob.h>
22 #include <libubox/ustream.h>
23 #include <libubox/ustream-ssl.h>
24
25 struct uclient_cb;
26 struct uclient_backend;
27
28 enum uclient_error_code {
29         UCLIENT_ERROR_UNKNOWN,
30         UCLIENT_ERROR_CONNECT,
31         UCLIENT_ERROR_SSL_INVALID_CERT,
32         UCLIENT_ERROR_SSL_CN_MISMATCH,
33 };
34
35 struct uclient {
36         const struct uclient_backend *backend;
37         const struct uclient_cb *cb;
38
39         struct uclient_url *url;
40         void *priv;
41
42         bool eof;
43         int error_code;
44         int status_code;
45         struct blob_attr *meta;
46
47         struct uloop_timeout timeout;
48 };
49
50 struct uclient_cb {
51         void (*data_read)(struct uclient *cl);
52         void (*data_sent)(struct uclient *cl);
53         void (*data_eof)(struct uclient *cl);
54         void (*header_done)(struct uclient *cl);
55         void (*error)(struct uclient *cl, int code);
56 };
57
58 struct uclient *uclient_new(const char *url, const char *auth_str, const struct uclient_cb *cb);
59 void uclient_free(struct uclient *cl);
60
61 int uclient_connect_url(struct uclient *cl, const char *url_str);
62
63 static inline int uclient_connect(struct uclient *cl)
64 {
65         return uclient_connect_url(cl, NULL);
66 }
67
68
69 int uclient_read(struct uclient *cl, char *buf, int len);
70 int uclient_write(struct uclient *cl, char *buf, int len);
71 int uclient_request(struct uclient *cl);
72
73 /* HTTP */
74 extern const struct uclient_backend uclient_backend_http;
75
76 int uclient_http_set_header(struct uclient *cl, const char *name, const char *value);
77 int uclient_http_reset_headers(struct uclient *cl, const char *name, const char *value);
78 int uclient_http_set_request_type(struct uclient *cl, const char *type);
79 bool uclient_http_redirect(struct uclient *cl);
80
81 int uclient_http_set_ssl_ctx(struct uclient *cl, struct ustream_ssl_ctx *ctx, bool require_validation);
82
83 #endif