1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
14 #include <yaz/options.h>
16 #include <yaz/backtrace.h>
18 static void usage(void)
20 printf("yaz-icu [options] url ..\n");
21 printf(" -H name:value Sets HTTP header (repeat if necessary)\n");
22 printf(" -m method Sets HTTP method\n");
23 printf(" -O fname Writes HTTP content to file\n");
24 printf(" -p fname POSTs file at following url\n");
25 printf(" -R num Set maximum number of HTTP redirects\n");
26 printf(" -u user/password Sets Basic HTTP auth\n");
27 printf(" -v Verbose\n");
28 printf(" -x proxy Sets HTTP proxy\n");
32 static char *get_file(const char *fname, size_t *len)
35 FILE *inf = fopen(fname, "rb");
38 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Could not open %s", fname);
41 if (fseek(inf, 0L, SEEK_END))
43 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fseek of %s failed", fname);
47 if (*len) /* zero length not considered an error */
51 fseek(inf, 0L, SEEK_SET);
52 r = fread(buf, 1, *len, inf);
55 yaz_log(YLOG_FATAL|YLOG_ERRNO, "short fread of %s", fname);
63 int main(int argc, char **argv)
67 yaz_url_t p = yaz_url_create();
70 const char *method = "GET";
71 Z_HTTP_Response *http_response;
72 Z_HTTP_Header *http_headers = 0;
73 ODR odr = odr_createmem(ODR_ENCODE);
76 const char *outfname = 0;
78 yaz_enable_panic_backtrace(*argv);
79 while ((ret = options("h{help}H:m:O:p:R{max-redirs}:u:vx:", argv, argc, &arg))
88 if (!strchr(arg, ':'))
90 yaz_log(YLOG_FATAL, "bad header option (missing :) %s\n", arg);
95 char *cp = strchr(arg, ':');
96 char *name = odr_malloc(odr, 1 + cp - arg);
98 memcpy(name, arg, cp - arg);
99 name[cp - arg] = '\0';
100 while (*value == ' ') /* skip space after = */
102 z_HTTP_header_add(odr, &http_headers, name, value);
113 post_buf = get_file(arg, &post_len);
117 yaz_url_set_max_redirects(p, atoi(arg));
120 if (strchr(arg, '/'))
122 char *cp = strchr(arg, '/');
123 char *user = odr_malloc(odr, 1 + cp - arg);
124 char *password = cp + 1;
125 memcpy(user, arg, cp - arg);
126 user[cp - arg] = '\0';
127 z_HTTP_header_add_basic_auth(odr, &http_headers, user,
131 z_HTTP_header_add_basic_auth(odr, &http_headers, arg, 0);
134 yaz_url_set_verbose(p, 1);
137 yaz_url_set_proxy(p, arg);
140 http_response = yaz_url_exec(p, arg, method, http_headers,
149 outf = fopen(outfname, "w");
152 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", outfname);
156 fwrite(http_response->content_buf, 1,
157 http_response->content_len, outf);
178 * c-file-style: "Stroustrup"
179 * indent-tabs-mode: nil
181 * vim: shiftwidth=4 tabstop=8 expandtab