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>
24 yaz_url_t yaz_url_create(void)
26 yaz_url_t p = xmalloc(sizeof(*p));
27 p->odr_in = odr_createmem(ODR_DECODE);
28 p->odr_out = odr_createmem(ODR_ENCODE);
30 p->max_redirects = 10;
34 void yaz_url_destroy(yaz_url_t p)
38 odr_destroy(p->odr_in);
39 odr_destroy(p->odr_out);
45 void yaz_url_set_proxy(yaz_url_t p, const char *proxy)
50 p->proxy = xstrdup(proxy);
53 void yaz_url_set_max_redirects(yaz_url_t p, int num)
55 p->max_redirects = num;
58 static void extract_user_pass(NMEM nmem,
60 char **uri_lean, char **http_user,
63 const char *cp1 = strchr(uri, '/');
71 if (!strncmp(cp1, "://", 3))
74 const char *cp2 = cp1 + 3;
75 while (*cp2 && *cp2 != '/' && *cp2 != '@')
81 if (*cp2 == '@' && cp3)
83 *uri_lean = nmem_malloc(nmem, strlen(uri) + 1);
84 memcpy(*uri_lean, uri, cp1 + 3 - uri);
85 strcpy(*uri_lean + (cp1 + 3 - uri), cp2 + 1);
87 *http_user = nmem_strdupn(nmem, cp1 + 3, cp3 - (cp1 + 3));
88 *http_pass = nmem_strdupn(nmem, cp3 + 1, cp2 - (cp3 + 1));
93 *uri_lean = nmem_strdup(nmem, uri);
96 Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
98 Z_HTTP_Header *user_headers,
99 const char *buf, size_t len)
101 Z_HTTP_Response *res = 0;
102 int number_of_redirects = 0;
109 const char *location = 0;
115 extract_user_pass(p->odr_out->mem, uri, &uri_lean,
116 &http_user, &http_pass);
118 gdu = z_get_HTTP_Request_uri(p->odr_out, uri_lean, 0, p->proxy ? 1 : 0);
119 gdu->u.HTTP_Request->method = odr_strdup(p->odr_out, method);
121 for ( ; user_headers; user_headers = user_headers->next)
123 /* prefer new Host over user-supplied Host */
124 if (!strcmp(user_headers->name, "Host"))
126 /* prefer user-supplied User-Agent over YAZ' own */
127 else if (!strcmp(user_headers->name, "User-Agent"))
128 z_HTTP_header_set(p->odr_out, &gdu->u.HTTP_Request->headers,
129 user_headers->name, user_headers->value);
131 z_HTTP_header_add(p->odr_out, &gdu->u.HTTP_Request->headers,
132 user_headers->name, user_headers->value);
134 if (http_user && http_pass)
135 z_HTTP_header_add_basic_auth(p->odr_out,
136 &gdu->u.HTTP_Request->headers,
137 http_user, http_pass);
142 gdu->u.HTTP_Request->content_buf = (char *) buf;
143 gdu->u.HTTP_Request->content_len = len;
145 if (!z_GDU(p->odr_out, &gdu, 0, 0))
147 yaz_log(YLOG_WARN, "Can not encode HTTP request URL:%s", uri);
150 conn = cs_create_host_proxy(uri_lean, 1, &add, p->proxy);
153 yaz_log(YLOG_WARN, "Could not resolve URL: %s", uri);
155 else if (cs_connect(conn, add) < 0)
157 yaz_log(YLOG_WARN, "Can not connect to URL: %s", uri);
162 char *buf = odr_getbuf(p->odr_out, &len, 0);
164 if (cs_put(conn, buf, len) < 0)
165 yaz_log(YLOG_WARN, "cs_put failed URL: %s", uri);
170 int cs_res = cs_get(conn, &netbuffer, &netlen);
173 yaz_log(YLOG_WARN, "cs_get failed URL: %s", uri);
178 odr_setbuf(p->odr_in, netbuffer, cs_res, 0);
179 if (!z_GDU(p->odr_in, &gdu, 0, 0)
180 || gdu->which != Z_GDU_HTTP_Response)
182 yaz_log(YLOG_WARN, "HTTP decoding failed "
187 res = gdu->u.HTTP_Response;
198 location = z_HTTP_header_lookup(res->headers, "Location");
199 if (++number_of_redirects <= p->max_redirects &&
200 location && (code == 301 || code == 302 || code == 307))
202 odr_reset(p->odr_out);
203 uri = odr_strdup(p->odr_out, location);
204 odr_reset(p->odr_in);
215 * c-file-style: "Stroustrup"
216 * indent-tabs-mode: nil
218 * vim: shiftwidth=4 tabstop=8 expandtab