X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=util%2Fyaz-url.c;h=5f9a9be39179d4ca2b3f6242c5c4ac7d6b20a2a4;hp=b59b176950a22e14b2935bd70b32c33b5af97f57;hb=4c2a1989cfcdeba0338fce5fe9152a37837ef28a;hpb=2e45e866cd91bf96707fdc0a278e8481773c4c86 diff --git a/util/yaz-url.c b/util/yaz-url.c index b59b176..5f9a9be 100644 --- a/util/yaz-url.c +++ b/util/yaz-url.c @@ -17,10 +17,12 @@ static void usage(void) { printf("yaz-icu [options] url ..\n"); - printf(" -H name=value HTTP header\n"); - printf(" -p file POST content of file\n"); - printf(" -u user/password Basic HTTP auth\n"); - printf(" -x proxy HTTP proxy\n"); + printf(" -H name:value Sets HTTP header (repeat if necessary)\n"); + printf(" -m method Sets HTTP method\n"); + printf(" -O fname Writes HTTP content to file\n"); + printf(" -p fname POSTs file at following url\n"); + printf(" -u user/password Sets Basic HTTP auth\n"); + printf(" -x proxy Sets HTTP proxy\n"); exit(1); } @@ -39,9 +41,12 @@ static char *get_file(const char *fname, size_t *len) exit(1); } *len = ftell(inf); - buf = xmalloc(*len); - fseek(inf, 0L, SEEK_SET); - fread(buf, 1, *len, inf); + if (*len) /* zero length not considered an error */ + { + buf = xmalloc(*len); + fseek(inf, 0L, SEEK_SET); + fread(buf, 1, *len, inf); + } fclose(inf); return buf; } @@ -59,8 +64,9 @@ int main(int argc, char **argv) ODR odr = odr_createmem(ODR_ENCODE); int exit_code = 0; int no_urls = 0; + const char *outfname = 0; - while ((ret = options("hH:p:u:x:", argv, argc, &arg)) + while ((ret = options("hH:m:O:p:u:x:", argv, argc, &arg)) != YAZ_OPTIONS_EOF) { switch (ret) @@ -69,21 +75,29 @@ int main(int argc, char **argv) usage(); break; case 'H': - if (!strchr(arg, '=')) + if (!strchr(arg, ':')) { - yaz_log(YLOG_FATAL, "bad header option (missing =): %s\n", arg); + yaz_log(YLOG_FATAL, "bad header option (missing :) %s\n", arg); exit_code = 1; } else { - char *cp = strchr(arg, '='); + char *cp = strchr(arg, ':'); char *name = odr_malloc(odr, 1 + cp - arg); char *value = cp + 1; memcpy(name, arg, cp - arg); name[cp - arg] = '\0'; + while (*value == ' ') /* skip space after = */ + value++; z_HTTP_header_add(odr, &http_headers, name, value); } break; + case 'm': + method = arg; + break; + case 'O': + outfname = arg; + break; case 'p': xfree(post_buf); post_buf = get_file(arg, &post_len); @@ -113,8 +127,20 @@ int main(int argc, char **argv) exit_code = 1; else { + FILE *outf = stdout; + if (outfname) + { + outf = fopen(outfname, "w"); + if (!outf) + { + yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", outfname); + exit(1); + } + } fwrite(http_response->content_buf, 1, - http_response->content_len, stdout); + http_response->content_len, outf); + if (outfname) + fclose(outf); } no_urls++; break; @@ -122,6 +148,7 @@ int main(int argc, char **argv) usage(); } } + xfree(post_buf); yaz_url_destroy(p); odr_destroy(odr); if (no_urls == 0)