1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
14 #include <yaz/options.h>
17 static void usage(void)
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");
29 static char *get_file(const char *fname, size_t *len)
32 FILE *inf = fopen(fname, "rb");
35 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Could not open %s", fname);
38 if (fseek(inf, 0L, SEEK_END))
40 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fseek of %s failed", fname);
44 if (*len) /* zero length not considered an error */
48 fseek(inf, 0L, SEEK_SET);
49 r = fread(buf, 1, *len, inf);
52 yaz_log(YLOG_FATAL|YLOG_ERRNO, "short fread of %s", fname);
60 int main(int argc, char **argv)
64 yaz_url_t p = yaz_url_create();
67 const char *method = "GET";
68 Z_HTTP_Response *http_response;
69 Z_HTTP_Header *http_headers = 0;
70 ODR odr = odr_createmem(ODR_ENCODE);
73 const char *outfname = 0;
75 while ((ret = options("hH:m:O:p:u:x:", argv, argc, &arg))
84 if (!strchr(arg, ':'))
86 yaz_log(YLOG_FATAL, "bad header option (missing :) %s\n", arg);
91 char *cp = strchr(arg, ':');
92 char *name = odr_malloc(odr, 1 + cp - arg);
94 memcpy(name, arg, cp - arg);
95 name[cp - arg] = '\0';
96 while (*value == ' ') /* skip space after = */
98 z_HTTP_header_add(odr, &http_headers, name, value);
109 post_buf = get_file(arg, &post_len);
113 if (strchr(arg, '/'))
115 char *cp = strchr(arg, '/');
116 char *user = odr_malloc(odr, 1 + cp - arg);
117 char *password = cp + 1;
118 memcpy(user, arg, cp - arg);
119 user[cp - arg] = '\0';
120 z_HTTP_header_add_basic_auth(odr, &http_headers, user,
124 z_HTTP_header_add_basic_auth(odr, &http_headers, arg, 0);
127 yaz_url_set_proxy(p, arg);
130 http_response = yaz_url_exec(p, arg, method, http_headers,
139 outf = fopen(outfname, "w");
142 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", outfname);
146 fwrite(http_response->content_buf, 1,
147 http_response->content_len, outf);
168 * c-file-style: "Stroustrup"
169 * indent-tabs-mode: nil
171 * vim: shiftwidth=4 tabstop=8 expandtab