New utility yaz_url: fetches HTTP content
[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
62     while ((ret = options("hH:p:u:x:", argv, argc, &arg))
63            != YAZ_OPTIONS_EOF)
64     {
65         switch (ret)
66         {
67         case 'h':
68             usage();
69             break;
70         case 'H':
71             if (!strchr(arg, '='))
72             {
73                 yaz_log(YLOG_FATAL, "bad header option (missing =): %s\n", arg);
74                 exit_code = 1;
75             }
76             else
77             {
78                 char *cp = strchr(arg, '=');
79                 char *name = odr_malloc(odr, 1 + cp - arg);
80                 char *value = cp + 1;
81                 memcpy(name, arg, cp - arg);
82                 name[cp - arg] = '\0';
83                 z_HTTP_header_add(odr, &http_headers, name, value);
84             }
85             break;
86         case 'p':
87             xfree(post_buf);
88             post_buf = get_file(arg, &post_len);
89             method = "POST";
90             break;
91         case 'u':
92             if (strchr(arg, '/'))
93             {
94                 char *cp = strchr(arg, '/');
95                 char *user = odr_malloc(odr, 1 + cp - arg);
96                 char *password = cp + 1;
97                 memcpy(user, arg, cp - arg);
98                 user[cp - arg] = '\0';
99                 z_HTTP_header_add_basic_auth(odr, &http_headers, user,
100                                              password);
101             }
102             else
103                 z_HTTP_header_add_basic_auth(odr, &http_headers, arg, 0);
104             break;
105         case 'x':
106             yaz_url_set_proxy(p, arg);
107             break;
108         case 0:
109             http_response = yaz_url_exec(p, arg, method, http_headers,
110                                          post_buf, post_len);
111             if (!http_response)
112                 exit_code = 1;
113             else
114             {
115                 fwrite(http_response->content_buf, 1,
116                        http_response->content_len, stdout);
117             }
118             break;
119         default:
120             usage();
121         }
122     }
123     yaz_url_destroy(p);
124     odr_destroy(odr);
125     exit(exit_code);
126 }
127
128 /*
129  * Local variables:
130  * c-basic-offset: 4
131  * c-file-style: "Stroustrup"
132  * indent-tabs-mode: nil
133  * End:
134  * vim: shiftwidth=4 tabstop=8 expandtab
135  */
136