Check result of fread
[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         size_t r = fread(buf, 1, *len, inf);
49         if (r != *len)
50         {
51             yaz_log(YLOG_FATAL|YLOG_ERRNO, "short fread of %s", fname);
52             exit(1);
53         }
54     }
55     fclose(inf);
56     return buf;
57 }
58
59 int main(int argc, char **argv)
60 {
61     int ret;
62     char *arg;
63     yaz_url_t p = yaz_url_create();
64     char *post_buf = 0;
65     size_t post_len = 0;
66     const char *method = "GET";
67     Z_HTTP_Response *http_response;
68     Z_HTTP_Header *http_headers = 0;
69     ODR odr = odr_createmem(ODR_ENCODE);
70     int exit_code = 0;
71     int no_urls = 0;
72     const char *outfname = 0;
73
74     while ((ret = options("hH:m:O:p:u:x:", argv, argc, &arg))
75            != YAZ_OPTIONS_EOF)
76     {
77         switch (ret)
78         {
79         case 'h':
80             usage();
81             break;
82         case 'H':
83             if (!strchr(arg, ':'))
84             {
85                 yaz_log(YLOG_FATAL, "bad header option (missing :) %s\n", arg);
86                 exit_code = 1;
87             }
88             else
89             {
90                 char *cp = strchr(arg, ':');
91                 char *name = odr_malloc(odr, 1 + cp - arg);
92                 char *value = cp + 1;
93                 memcpy(name, arg, cp - arg);
94                 name[cp - arg] = '\0';
95                 while (*value == ' ') /* skip space after = */
96                     value++;
97                 z_HTTP_header_add(odr, &http_headers, name, value);
98             }
99             break;
100         case 'm':
101             method = arg;
102             break;
103         case 'O':
104             outfname = arg;
105             break;
106         case 'p':
107             xfree(post_buf);
108             post_buf = get_file(arg, &post_len);
109             method = "POST";
110             break;
111         case 'u':
112             if (strchr(arg, '/'))
113             {
114                 char *cp = strchr(arg, '/');
115                 char *user = odr_malloc(odr, 1 + cp - arg);
116                 char *password = cp + 1;
117                 memcpy(user, arg, cp - arg);
118                 user[cp - arg] = '\0';
119                 z_HTTP_header_add_basic_auth(odr, &http_headers, user,
120                                              password);
121             }
122             else
123                 z_HTTP_header_add_basic_auth(odr, &http_headers, arg, 0);
124             break;
125         case 'x':
126             yaz_url_set_proxy(p, arg);
127             break;
128         case 0:
129             http_response = yaz_url_exec(p, arg, method, http_headers,
130                                          post_buf, post_len);
131             if (!http_response)
132                 exit_code = 1;
133             else
134             {
135                 FILE *outf = stdout;
136                 if (outfname)
137                 {
138                     outf = fopen(outfname, "w");
139                     if (!outf)
140                     {
141                         yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", outfname);
142                         exit(1);
143                     }
144                 }
145                 fwrite(http_response->content_buf, 1,
146                        http_response->content_len, outf);
147                 if (outfname)
148                     fclose(outf);
149             }
150             no_urls++;
151             break;
152         default:
153             usage();
154         }
155     }
156     xfree(post_buf);
157     yaz_url_destroy(p);
158     odr_destroy(odr);
159     if (no_urls == 0)
160         usage();
161     exit(exit_code);
162 }
163
164 /*
165  * Local variables:
166  * c-basic-offset: 4
167  * c-file-style: "Stroustrup"
168  * indent-tabs-mode: nil
169  * End:
170  * vim: shiftwidth=4 tabstop=8 expandtab
171  */
172