uclient: fix http regression
[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 <netinet/in.h>
22
23 #include <libubox/blob.h>
24 #include <libubox/ustream.h>
25 #include <libubox/ustream-ssl.h>
26
27 #define UCLIENT_DEFAULT_TIMEOUT_MS 30000
28
29 struct uclient_cb;
30 struct uclient_backend;
31
32 enum uclient_error_code {
33 UCLIENT_ERROR_UNKNOWN,
34 UCLIENT_ERROR_CONNECT,
35 UCLIENT_ERROR_TIMEDOUT,
36 UCLIENT_ERROR_SSL_INVALID_CERT,
37 UCLIENT_ERROR_SSL_CN_MISMATCH,
38 UCLIENT_ERROR_MISSING_SSL_CONTEXT,
39 __UCLIENT_ERROR_MAX
40 };
41
42 enum uclient_log_type {
43 UCLIENT_LOG_SSL_ERROR,
44 UCLIENT_LOG_SSL_VERIFY_ERROR,
45 __UCLIENT_LOG_MAX
46 };
47
48 union uclient_addr {
49 struct sockaddr sa;
50 struct sockaddr_in sin;
51 struct sockaddr_in6 sin6;
52 };
53
54 struct uclient_url {
55 const struct uclient_backend *backend;
56 int prefix;
57
58 const char *host;
59 const char *port;
60 const char *location;
61
62 const char *auth;
63 };
64
65 struct uclient {
66 const struct uclient_backend *backend;
67 const struct uclient_cb *cb;
68
69 union uclient_addr local_addr, remote_addr;
70
71 struct uclient_url *proxy_url;
72 struct uclient_url *url;
73 int timeout_msecs;
74 void *priv;
75
76 bool eof;
77 bool data_eof;
78 int error_code;
79 int status_code;
80 int seq;
81 struct blob_attr *meta;
82
83 struct uloop_timeout connection_timeout;
84 struct uloop_timeout read_notify;
85 struct uloop_timeout timeout;
86 };
87
88 struct uclient_cb {
89 void (*data_read)(struct uclient *cl);
90 void (*data_sent)(struct uclient *cl);
91 void (*data_eof)(struct uclient *cl);
92 void (*header_done)(struct uclient *cl);
93 void (*error)(struct uclient *cl, int code);
94 void (*log_msg)(struct uclient *cl, enum uclient_log_type type, const char *msg);
95 };
96
97 struct uclient *uclient_new(const char *url, const char *auth_str, const struct uclient_cb *cb);
98 void uclient_free(struct uclient *cl);
99
100 int uclient_set_url(struct uclient *cl, const char *url, const char *auth);
101 int uclient_set_proxy_url(struct uclient *cl, const char *url_str, const char *auth_str);
102
103
104 /**
105 * Sets connection timeout.
106 *
107 * Provided timeout value will be used for:
108 * 1) Receiving HTTP response
109 * 2) Receiving data
110 *
111 * In case of timeout uclient will use error callback with
112 * UCLIENT_ERROR_TIMEDOUT code.
113 *
114 * @param msecs timeout in milliseconds
115 */
116 int uclient_set_timeout(struct uclient *cl, int msecs);
117
118 int uclient_connect(struct uclient *cl);
119 void uclient_disconnect(struct uclient *cl);
120
121 int uclient_read(struct uclient *cl, char *buf, int len);
122 int uclient_write(struct uclient *cl, const char *buf, int len);
123 int uclient_pending_bytes(struct uclient *cl, bool write);
124 int uclient_request(struct uclient *cl);
125
126 char *uclient_get_addr(char *dest, int *port, union uclient_addr *a);
127 struct ustream_ssl_ctx *uclient_new_ssl_context(const struct ustream_ssl_ops **ops);
128
129 /* HTTP */
130 extern const struct uclient_backend uclient_backend_http;
131
132 int uclient_http_reset_headers(struct uclient *cl);
133 int uclient_http_set_header(struct uclient *cl, const char *name, const char *value);
134 int uclient_http_set_request_type(struct uclient *cl, const char *type);
135 int uclient_http_redirect(struct uclient *cl);
136
137 static inline bool uclient_http_status_redirect(struct uclient *cl)
138 {
139 switch (cl->status_code) {
140 case 301:
141 case 302:
142 case 307:
143 return true;
144 default:
145 return false;
146 }
147 }
148
149 int uclient_http_set_ssl_ctx(struct uclient *cl, const struct ustream_ssl_ops *ops,
150 struct ustream_ssl_ctx *ctx, bool require_validation);
151 int uclient_http_set_address_family(struct uclient *cl, int af);
152 const char *uclient_strerror(unsigned err);
153
154 #endif