1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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 Set HTTP header (repeat if necessary)\n");
21 printf(" -m method HTTP method\n");
22 printf(" -p file POSTs file at following url\n");
23 printf(" -u user/password Basic HTTP auth\n");
24 printf(" -x proxy HTTP proxy\n");
28 static char *get_file(const char *fname, size_t *len)
31 FILE *inf = fopen(fname, "rb");
34 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Could not open %s", fname);
37 if (fseek(inf, 0L, SEEK_END))
39 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fseek of %s failed", fname);
43 if (*len) /* zero length not considered an error */
46 fseek(inf, 0L, SEEK_SET);
47 fread(buf, 1, *len, inf);
53 int main(int argc, char **argv)
57 yaz_url_t p = yaz_url_create();
60 const char *method = "GET";
61 Z_HTTP_Response *http_response;
62 Z_HTTP_Header *http_headers = 0;
63 ODR odr = odr_createmem(ODR_ENCODE);
67 while ((ret = options("hH:m:p:u:x:", argv, argc, &arg))
76 if (!strchr(arg, ':'))
78 yaz_log(YLOG_FATAL, "bad header option (missing :) %s\n", arg);
83 char *cp = strchr(arg, ':');
84 char *name = odr_malloc(odr, 1 + cp - arg);
86 memcpy(name, arg, cp - arg);
87 name[cp - arg] = '\0';
88 while (*value == ' ') /* skip space after = */
90 z_HTTP_header_add(odr, &http_headers, name, value);
98 post_buf = get_file(arg, &post_len);
102 if (strchr(arg, '/'))
104 char *cp = strchr(arg, '/');
105 char *user = odr_malloc(odr, 1 + cp - arg);
106 char *password = cp + 1;
107 memcpy(user, arg, cp - arg);
108 user[cp - arg] = '\0';
109 z_HTTP_header_add_basic_auth(odr, &http_headers, user,
113 z_HTTP_header_add_basic_auth(odr, &http_headers, arg, 0);
116 yaz_url_set_proxy(p, arg);
119 http_response = yaz_url_exec(p, arg, method, http_headers,
125 fwrite(http_response->content_buf, 1,
126 http_response->content_len, stdout);
145 * c-file-style: "Stroustrup"
146 * indent-tabs-mode: nil
148 * vim: shiftwidth=4 tabstop=8 expandtab