fetch: remove http header output
[project/uclient.git] / uclient-fetch.c
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
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <dlfcn.h>
22 #include <getopt.h>
23 #include <fcntl.h>
24
25 #include <libubox/blobmsg.h>
26
27 #include "uclient.h"
28
29 #ifdef __APPLE__
30 #define LIB_EXT "dylib"
31 #else
32 #define LIB_EXT "so"
33 #endif
34
35 static struct ustream_ssl_ctx *ssl_ctx;
36 static const struct ustream_ssl_ops *ssl_ops;
37 static int quiet = false;
38 static bool verify = true;
39 static const char *output_file;
40 static int output_fd = -1;
41 static int error_ret;
42
43 static int open_output_file(const char *path, bool create)
44 {
45         const char *str;
46         char *filename;
47         int len;
48         int flags = O_WRONLY;
49
50         if (create)
51                 flags |= O_CREAT;
52
53         if (output_file) {
54                 if (!strcmp(output_file, "-"))
55                         return STDOUT_FILENO;
56
57                 return open(output_file, flags, 0644);
58         }
59
60         /* Don't automatically overwrite files if the name is derived from the URL */
61         if (create)
62                 flags |= O_EXCL;
63
64         len = strcspn(path, ";&");
65         while (len > 0 && path[len - 1] == '/')
66                 len--;
67
68         for (str = path + len - 1; str >= path; str--) {
69                 if (*str == '/')
70                         break;
71         }
72
73         str++;
74         len -= str - path;
75
76         if (len > 0) {
77                 filename = alloca(len + 1);
78                 strncpy(filename, str, len);
79                 filename[len] = 0;
80         } else {
81                 filename = "index.html";
82         }
83
84         return open(filename, flags, 0644);
85 }
86
87 static void request_done(struct uclient *cl)
88 {
89         if (output_fd >= 0 && !output_file) {
90                 close(output_fd);
91                 output_fd = -1;
92         }
93         uclient_disconnect(cl);
94         uloop_end();
95 }
96
97 static void header_done_cb(struct uclient *cl)
98 {
99         static int retries;
100
101         if (retries < 10 && uclient_http_redirect(cl)) {
102                 if (!quiet)
103                         fprintf(stderr, "Redirected to %s on %s\n", cl->url->location, cl->url->host);
104
105                 retries++;
106                 return;
107         }
108
109         retries = 0;
110         switch (cl->status_code) {
111         case 204:
112         case 200:
113                 output_fd = open_output_file(cl->url->location, true);
114                 if (output_fd < 0) {
115                         if (!quiet)
116                                 perror("Cannot open output file");
117                         error_ret = 3;
118                         request_done(cl);
119                 }
120                 break;
121
122         default:
123                 request_done(cl);
124                 error_ret = 8;
125                 break;
126         }
127 }
128
129 static void read_data_cb(struct uclient *cl)
130 {
131         char buf[256];
132         int len;
133
134         if (output_fd < 0)
135                 return;
136
137         while (1) {
138                 len = uclient_read(cl, buf, sizeof(buf));
139                 if (!len)
140                         return;
141
142                 write(output_fd, buf, len);
143         }
144 }
145
146 static void msg_connecting(struct uclient *cl)
147 {
148         char addr[INET6_ADDRSTRLEN];
149         int port;
150
151         if (quiet)
152                 return;
153
154         uclient_get_addr(addr, &port, &cl->remote_addr);
155         fprintf(stderr, "Connecting to %s %s:%d\n", cl->url->host, addr, port);
156 }
157
158 static void init_request(struct uclient *cl)
159 {
160         uclient_connect(cl);
161         msg_connecting(cl);
162         uclient_http_set_request_type(cl, "GET");
163         uclient_request(cl);
164 }
165
166 static void eof_cb(struct uclient *cl)
167 {
168         request_done(cl);
169 }
170
171 static void handle_uclient_error(struct uclient *cl, int code)
172 {
173         const char *type = "Unknown error";
174         bool ignore = false;
175
176         switch(code) {
177         case UCLIENT_ERROR_CONNECT:
178                 type = "Connection failed";
179                 error_ret = 4;
180                 break;
181         case UCLIENT_ERROR_SSL_INVALID_CERT:
182                 type = "Invalid SSL certificate";
183                 ignore = !verify;
184                 error_ret = 5;
185                 break;
186         case UCLIENT_ERROR_SSL_CN_MISMATCH:
187                 type = "Server hostname does not match SSL certificate";
188                 ignore = !verify;
189                 error_ret = 5;
190                 break;
191         default:
192                 error_ret = 1;
193                 break;
194         }
195
196         if (!quiet)
197                 fprintf(stderr, "Connection error: %s%s\n", type, ignore ? " (ignored)" : "");
198
199         if (ignore)
200                 error_ret = 0;
201         else
202                 request_done(cl);
203 }
204
205 static const struct uclient_cb cb = {
206         .header_done = header_done_cb,
207         .data_read = read_data_cb,
208         .data_eof = eof_cb,
209         .error = handle_uclient_error,
210 };
211
212 static int usage(const char *progname)
213 {
214         fprintf(stderr,
215                 "Usage: %s [options] <URL>\n"
216                 "Options:\n"
217                 "       -q:                             Turn off status messages\n"
218                 "       -O <file>:                      Redirect output to file (use \"-\" for stdout)\n"
219                 "\n"
220                 "HTTPS options:\n"
221                 "       --ca-certificate=<cert>:        Load CA certificates from file <cert>\n"
222                 "       --no-check-certificate:         don't validate the server's certificate\n"
223                 "\n", progname);
224         return 1;
225 }
226
227
228 static void init_ustream_ssl(void)
229 {
230         void *dlh;
231
232         dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
233         if (!dlh)
234                 return;
235
236         ssl_ops = dlsym(dlh, "ustream_ssl_ops");
237         if (!ssl_ops)
238                 return;
239
240         ssl_ctx = ssl_ops->context_new(false);
241 }
242
243 static int no_ssl(const char *progname)
244 {
245         fprintf(stderr, "%s: SSL support not available, please install ustream-ssl\n", progname);
246         return 1;
247 }
248
249 enum {
250         L_NO_CHECK_CERTIFICATE,
251         L_CA_CERTIFICATE,
252 };
253
254 static const struct option longopts[] = {
255         [L_NO_CHECK_CERTIFICATE] = { "no-check-certificate", no_argument },
256         [L_CA_CERTIFICATE] = { "ca-certificate", required_argument },
257         {}
258 };
259
260 int main(int argc, char **argv)
261 {
262         const char *progname = argv[0];
263         struct uclient *cl;
264         int ch;
265         int longopt_idx = 0;
266
267         init_ustream_ssl();
268
269         while ((ch = getopt_long(argc, argv, "qO:", longopts, &longopt_idx)) != -1) {
270                 switch(ch) {
271                 case 0:
272                         switch (longopt_idx) {
273                         case L_NO_CHECK_CERTIFICATE:
274                                 verify = false;
275                                 break;
276                         case L_CA_CERTIFICATE:
277                                 if (ssl_ctx)
278                                         ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg);
279                                 break;
280                         default:
281                                 return usage(progname);
282                         }
283                         break;
284                 case 'O':
285                         output_file = optarg;
286                         break;
287                 case 'q':
288                         quiet = true;
289                         break;
290                 default:
291                         return usage(progname);
292                 }
293         }
294
295         argv += optind;
296         argc -= optind;
297
298         if (argc != 1)
299                 return usage(progname);
300
301         if (!strncmp(argv[0], "https", 5) && !ssl_ctx)
302                 return no_ssl(progname);
303
304         uloop_init();
305
306         cl = uclient_new(argv[0], NULL, &cb);
307         if (!cl) {
308                 fprintf(stderr, "Failed to allocate uclient context\n");
309                 return 1;
310         }
311
312         if (ssl_ctx)
313                 uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
314
315         init_request(cl);
316         uloop_run();
317         uloop_done();
318
319         uclient_free(cl);
320
321         if (output_fd >= 0 && output_fd != STDOUT_FILENO)
322                 close(output_fd);
323
324         if (ssl_ctx)
325                 ssl_ops->context_free(ssl_ctx);
326
327         return error_ret;
328 }