Happy new year
[yaz-moved-to-github.git] / util / yaz-url.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2012 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #if HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <yaz/url.h>
14 #include <yaz/options.h>
15 #include <yaz/log.h>
16
17 static void usage(void)
18 {
19     printf("yaz-icu [options] url ..\n");
20     printf(" -H name:value       Sets HTTP header (repeat if necessary)\n");
21     printf(" -m method           Sets HTTP method\n");
22     printf(" -O fname            Writes HTTP content to file\n");
23     printf(" -p fname            POSTs file at following url\n");
24     printf(" -u user/password    Sets Basic HTTP auth\n");
25     printf(" -x proxy            Sets HTTP proxy\n");
26     exit(1);
27 }
28
29 static char *get_file(const char *fname, size_t *len)
30 {
31     char *buf = 0;
32     FILE *inf = fopen(fname, "rb");
33     if (!inf)
34     {
35         yaz_log(YLOG_FATAL|YLOG_ERRNO, "Could not open %s", fname);
36         exit(1);
37     }
38     if (fseek(inf, 0L, SEEK_END))
39     {
40         yaz_log(YLOG_FATAL|YLOG_ERRNO, "fseek of %s failed", fname);
41         exit(1);
42     }
43     *len = ftell(inf);
44     if (*len)  /* zero length not considered an error */
45     {
46         buf = xmalloc(*len);
47         fseek(inf, 0L, SEEK_SET);
48         (void) fread(buf, 1, *len, inf);
49     }
50     fclose(inf);
51     return buf;
52 }
53
54 int main(int argc, char **argv)
55 {
56     int ret;
57     char *arg;
58     yaz_url_t p = yaz_url_create();
59     char *post_buf = 0;
60     size_t post_len = 0;
61     const char *method = "GET";
62     Z_HTTP_Response *http_response;
63     Z_HTTP_Header *http_headers = 0;
64     ODR odr = odr_createmem(ODR_ENCODE);
65     int exit_code = 0;
66     int no_urls = 0;
67     const char *outfname = 0;
68
69     while ((ret = options("hH:m:O:p:u:x:", argv, argc, &arg))
70            != YAZ_OPTIONS_EOF)
71     {
72         switch (ret)
73         {
74         case 'h':
75             usage();
76             break;
77         case 'H':
78             if (!strchr(arg, ':'))
79             {
80                 yaz_log(YLOG_FATAL, "bad header option (missing :) %s\n", arg);
81                 exit_code = 1;
82             }
83             else
84             {
85                 char *cp = strchr(arg, ':');
86                 char *name = odr_malloc(odr, 1 + cp - arg);
87                 char *value = cp + 1;
88                 memcpy(name, arg, cp - arg);
89                 name[cp - arg] = '\0';
90                 while (*value == ' ') /* skip space after = */
91                     value++;
92                 z_HTTP_header_add(odr, &http_headers, name, value);
93             }
94             break;
95         case 'm':
96             method = arg;
97             break;
98         case 'O':
99             outfname = arg;
100             break;
101         case 'p':
102             xfree(post_buf);
103             post_buf = get_file(arg, &post_len);
104             method = "POST";
105             break;
106         case 'u':
107             if (strchr(arg, '/'))
108             {
109                 char *cp = strchr(arg, '/');
110                 char *user = odr_malloc(odr, 1 + cp - arg);
111                 char *password = cp + 1;
112                 memcpy(user, arg, cp - arg);
113                 user[cp - arg] = '\0';
114                 z_HTTP_header_add_basic_auth(odr, &http_headers, user,
115                                              password);
116             }
117             else
118                 z_HTTP_header_add_basic_auth(odr, &http_headers, arg, 0);
119             break;
120         case 'x':
121             yaz_url_set_proxy(p, arg);
122             break;
123         case 0:
124             http_response = yaz_url_exec(p, arg, method, http_headers,
125                                          post_buf, post_len);
126             if (!http_response)
127                 exit_code = 1;
128             else
129             {
130                 FILE *outf = stdout;
131                 if (outfname)
132                 {
133                     outf = fopen(outfname, "w");
134                     if (!outf)
135                     {
136                         yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", outfname);
137                         exit(1);
138                     }
139                 }
140                 fwrite(http_response->content_buf, 1,
141                        http_response->content_len, outf);
142                 if (outfname)
143                     fclose(outf);
144             }
145             no_urls++;
146             break;
147         default:
148             usage();
149         }
150     }
151     xfree(post_buf);
152     yaz_url_destroy(p);
153     odr_destroy(odr);
154     if (no_urls == 0)
155         usage();
156     exit(exit_code);
157 }
158
159 /*
160  * Local variables:
161  * c-basic-offset: 4
162  * c-file-style: "Stroustrup"
163  * indent-tabs-mode: nil
164  * End:
165  * vim: shiftwidth=4 tabstop=8 expandtab
166  */
167