File: yaz-url.c

package info (click to toggle)
yaz 5.27.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,184 kB
  • sloc: xml: 123,414; ansic: 72,530; sh: 5,007; tcl: 2,169; makefile: 1,321; yacc: 382
file content (183 lines) | stat: -rw-r--r-- 5,099 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* This file is part of the YAZ toolkit.
 * Copyright (C) Index Data
 * See the file LICENSE for details.
 */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <yaz/url.h>
#include <yaz/options.h>
#include <yaz/log.h>
#include <yaz/backtrace.h>

static void usage(void)
{
    printf("yaz-icu [options] url ..\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(" -R num              Set maximum number of HTTP redirects\n");
    printf(" -u user/password    Sets Basic HTTP auth\n");
    printf(" -v                  Verbose\n");
    printf(" -x proxy            Sets HTTP proxy\n");
    exit(1);
}

static char *get_file(const char *fname, size_t *len)
{
    char *buf = 0;
    FILE *inf = fopen(fname, "rb");
    if (!inf)
    {
        yaz_log(YLOG_FATAL|YLOG_ERRNO, "Could not open %s", fname);
        exit(1);
    }
    if (fseek(inf, 0L, SEEK_END))
    {
        yaz_log(YLOG_FATAL|YLOG_ERRNO, "fseek of %s failed", fname);
        exit(1);
    }
    *len = ftell(inf);
    if (*len)  /* zero length not considered an error */
    {
        size_t r;
        buf = xmalloc(*len);
        fseek(inf, 0L, SEEK_SET);
        r = fread(buf, 1, *len, inf);
        if (r != *len)
        {
            yaz_log(YLOG_FATAL|YLOG_ERRNO, "short fread of %s", fname);
            exit(1);
        }
    }
    fclose(inf);
    return buf;
}

int main(int argc, char **argv)
{
    int ret;
    char *arg;
    yaz_url_t p = yaz_url_create();
    char *post_buf = 0;
    size_t post_len = 0;
    const char *method = "GET";
    Z_HTTP_Response *http_response;
    Z_HTTP_Header *http_headers = 0;
    ODR odr = odr_createmem(ODR_ENCODE);
    int exit_code = 0;
    int no_urls = 0;
    const char *outfname = 0;

    yaz_enable_panic_backtrace(*argv);
    while ((ret = options("h{help}H:m:O:p:R{max-redirs}:u:vx:", argv, argc, &arg))
           != YAZ_OPTIONS_EOF)
    {
        switch (ret)
        {
        case 'h':
            usage();
            break;
        case 'H':
            if (!strchr(arg, ':'))
            {
                yaz_log(YLOG_FATAL, "bad header option (missing :) %s\n", arg);
                exit_code = 1;
            }
            else
            {
                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);
            method = "POST";
            break;
        case 'R':
            yaz_url_set_max_redirects(p, atoi(arg));
            break;
        case 'u':
            if (strchr(arg, '/'))
            {
                char *cp = strchr(arg, '/');
                char *user = odr_malloc(odr, 1 + cp - arg);
                char *password = cp + 1;
                memcpy(user, arg, cp - arg);
                user[cp - arg] = '\0';
                z_HTTP_header_add_basic_auth(odr, &http_headers, user,
                                             password);
            }
            else
                z_HTTP_header_add_basic_auth(odr, &http_headers, arg, 0);
            break;
        case 'v':
            yaz_url_set_verbose(p, 1);
            break;
        case 'x':
            yaz_url_set_proxy(p, arg);
            break;
        case 0:
            http_response = yaz_url_exec(p, arg, method, http_headers,
                                         post_buf, post_len);
            if (!http_response)
                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, outf);
                if (outfname)
                    fclose(outf);
            }
            no_urls++;
            break;
        default:
            usage();
        }
    }
    xfree(post_buf);
    yaz_url_destroy(p);
    odr_destroy(odr);
    if (no_urls == 0)
        usage();
    exit(exit_code);
}

/*
 * Local variables:
 * c-basic-offset: 4
 * c-file-style: "Stroustrup"
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */