1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief URL fetch utility
14 #include <yaz/comstack.h>
16 #include <yaz/wrbuf.h>
26 yaz_url_t yaz_url_create(void)
28 yaz_url_t p = xmalloc(sizeof(*p));
29 p->odr_in = odr_createmem(ODR_DECODE);
30 p->odr_out = odr_createmem(ODR_ENCODE);
32 p->max_redirects = 10;
33 p->w_error = wrbuf_alloc();
37 void yaz_url_destroy(yaz_url_t p)
41 odr_destroy(p->odr_in);
42 odr_destroy(p->odr_out);
44 wrbuf_destroy(p->w_error);
49 void yaz_url_set_proxy(yaz_url_t p, const char *proxy)
54 p->proxy = xstrdup(proxy);
57 void yaz_url_set_max_redirects(yaz_url_t p, int num)
59 p->max_redirects = num;
62 static void extract_user_pass(NMEM nmem,
64 char **uri_lean, char **http_user,
67 const char *cp1 = strchr(uri, '/');
75 if (!strncmp(cp1, "://", 3))
78 const char *cp2 = cp1 + 3;
79 while (*cp2 && *cp2 != '/' && *cp2 != '@')
85 if (*cp2 == '@' && cp3)
87 *uri_lean = nmem_malloc(nmem, strlen(uri) + 1);
88 memcpy(*uri_lean, uri, cp1 + 3 - uri);
89 strcpy(*uri_lean + (cp1 + 3 - uri), cp2 + 1);
91 *http_user = nmem_strdupn(nmem, cp1 + 3, cp3 - (cp1 + 3));
92 *http_pass = nmem_strdupn(nmem, cp3 + 1, cp2 - (cp3 + 1));
97 *uri_lean = nmem_strdup(nmem, uri);
100 const char *yaz_url_get_error(yaz_url_t p)
102 return wrbuf_cstr(p->w_error);
105 static void log_warn(yaz_url_t p)
107 yaz_log(YLOG_WARN, "yaz_url: %s", wrbuf_cstr(p->w_error));
110 Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
112 Z_HTTP_Header *user_headers,
113 const char *buf, size_t len)
115 Z_HTTP_Response *res = 0;
116 int number_of_redirects = 0;
118 wrbuf_rewind(p->w_error);
124 const char *location = 0;
130 extract_user_pass(p->odr_out->mem, uri, &uri_lean,
131 &http_user, &http_pass);
133 gdu = z_get_HTTP_Request_uri(p->odr_out, uri_lean, 0, p->proxy ? 1 : 0);
134 gdu->u.HTTP_Request->method = odr_strdup(p->odr_out, method);
136 for ( ; user_headers; user_headers = user_headers->next)
138 /* prefer new Host over user-supplied Host */
139 if (!strcmp(user_headers->name, "Host"))
141 /* prefer user-supplied User-Agent over YAZ' own */
142 else if (!strcmp(user_headers->name, "User-Agent"))
143 z_HTTP_header_set(p->odr_out, &gdu->u.HTTP_Request->headers,
144 user_headers->name, user_headers->value);
146 z_HTTP_header_add(p->odr_out, &gdu->u.HTTP_Request->headers,
147 user_headers->name, user_headers->value);
149 if (http_user && http_pass)
150 z_HTTP_header_add_basic_auth(p->odr_out,
151 &gdu->u.HTTP_Request->headers,
152 http_user, http_pass);
157 gdu->u.HTTP_Request->content_buf = (char *) buf;
158 gdu->u.HTTP_Request->content_len = len;
160 if (!z_GDU(p->odr_out, &gdu, 0, 0))
162 wrbuf_printf(p->w_error, "Can not encode HTTP request for URL %s",
167 conn = cs_create_host_proxy(uri_lean, 1, &add, p->proxy);
170 wrbuf_printf(p->w_error, "Can not resolve URL %s", uri);
173 else if (cs_connect(conn, add) < 0)
175 wrbuf_printf(p->w_error, "Can not connect to URL %s", uri);
181 char *buf = odr_getbuf(p->odr_out, &len, 0);
183 if (cs_put(conn, buf, len) < 0)
185 wrbuf_printf(p->w_error, "cs_put fail for URL %s", uri);
192 int cs_res = cs_get(conn, &netbuffer, &netlen);
195 wrbuf_printf(p->w_error, "cs_get failed for URL %s", uri);
201 odr_setbuf(p->odr_in, netbuffer, cs_res, 0);
202 if (!z_GDU(p->odr_in, &gdu, 0, 0)
203 || gdu->which != Z_GDU_HTTP_Response)
205 wrbuf_printf(p->w_error, "HTTP decoding fail for "
211 res = gdu->u.HTTP_Response;
222 location = z_HTTP_header_lookup(res->headers, "Location");
223 if (++number_of_redirects <= p->max_redirects &&
224 location && (code == 301 || code == 302 || code == 307))
226 odr_reset(p->odr_out);
227 uri = odr_strdup(p->odr_out, location);
228 odr_reset(p->odr_in);
239 * c-file-style: "Stroustrup"
240 * indent-tabs-mode: nil
242 * vim: shiftwidth=4 tabstop=8 expandtab