b59b176950a22e14b2935bd70b32c33b5af97f57
[yaz-moved-to-github.git] / util / yaz-url.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 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       HTTP header\n");
21     printf(" -p file             POST content of file\n");
22     printf(" -u user/password    Basic HTTP auth\n");
23     printf(" -x proxy            HTTP proxy\n");
24     exit(1);
25 }
26
27 static char *get_file(const char *fname, size_t *len)
28 {
29     char *buf = 0;
30     FILE *inf = fopen(fname, "rb");
31     if (!inf)
32     {
33         yaz_log(YLOG_FATAL|YLOG_ERRNO, "Could not open %s", fname);
34         exit(1);
35     }
36     if (fseek(inf, 0L, SEEK_END))
37     {
38         yaz_log(YLOG_FATAL|YLOG_ERRNO, "fseek of %s failed", fname);
39         exit(1);
40     }
41     *len = ftell(inf);
42     buf = xmalloc(*len);
43     fseek(inf, 0L, SEEK_SET);
44     fread(buf, 1, *len, inf);
45     fclose(inf);
46     return buf;
47 }
48
49 int main(int argc, char **argv)
50 {
51     int ret;
52     char *arg;
53     yaz_url_t p = yaz_url_create();
54     char *post_buf = 0;
55     size_t post_len = 0;
56     const char *method = "GET";
57     Z_HTTP_Response *http_response;
58     Z_HTTP_Header *http_headers = 0;
59     ODR odr = odr_createmem(ODR_ENCODE);
60     int exit_code = 0;
61     int no_urls = 0;
62
63     while ((ret = options("hH:p:u:x:", argv, argc, &arg))
64            != YAZ_OPTIONS_EOF)
65     {
66         switch (ret)
67         {
68         case 'h':
69             usage();
70             break;
71         case 'H':
72             if (!strchr(arg, '='))
73             {
74                 yaz_log(YLOG_FATAL, "bad header option (missing =): %s\n", arg);
75                 exit_code = 1;
76             }
77             else
78             {
79                 char *cp = strchr(arg, '=');
80                 char *name = odr_malloc(odr, 1 + cp - arg);
81                 char *value = cp + 1;
82                 memcpy(name, arg, cp - arg);
83                 name[cp - arg] = '\0';
84                 z_HTTP_header_add(odr, &http_headers, name, value);
85             }
86             break;
87         case 'p':
88             xfree(post_buf);
89             post_buf = get_file(arg, &post_len);
90             method = "POST";
91             break;
92         case 'u':
93             if (strchr(arg, '/'))
94             {
95                 char *cp = strchr(arg, '/');
96                 char *user = odr_malloc(odr, 1 + cp - arg);
97                 char *password = cp + 1;
98                 memcpy(user, arg, cp - arg);
99                 user[cp - arg] = '\0';
100                 z_HTTP_header_add_basic_auth(odr, &http_headers, user,
101                                              password);
102             }
103             else
104                 z_HTTP_header_add_basic_auth(odr, &http_headers, arg, 0);
105             break;
106         case 'x':
107             yaz_url_set_proxy(p, arg);
108             break;
109         case 0:
110             http_response = yaz_url_exec(p, arg, method, http_headers,
111                                          post_buf, post_len);
112             if (!http_response)
113                 exit_code = 1;
114             else
115             {
116                 fwrite(http_response->content_buf, 1,
117                        http_response->content_len, stdout);
118             }
119             no_urls++;
120             break;
121         default:
122             usage();
123         }
124     }
125     yaz_url_destroy(p);
126     odr_destroy(odr);
127     if (no_urls == 0)
128         usage();
129     exit(exit_code);
130 }
131
132 /*
133  * Local variables:
134  * c-basic-offset: 4
135  * c-file-style: "Stroustrup"
136  * indent-tabs-mode: nil
137  * End:
138  * vim: shiftwidth=4 tabstop=8 expandtab
139  */
140