projects
/
yaz-moved-to-github.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Remove CQL to Solr conversion and base on cql_transform_t
[yaz-moved-to-github.git]
/
src
/
url.c
diff --git
a/src/url.c
b/src/url.c
index
97e8247
..
f954bce
100644
(file)
--- a/
src/url.c
+++ b/
src/url.c
@@
-1,5
+1,5
@@
/* This file is part of the YAZ toolkit.
/* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2013 Index Data
+ * Copyright (C) Index Data
* See the file LICENSE for details.
*/
/**
* See the file LICENSE for details.
*/
/**
@@
-13,11
+13,17
@@
#include <yaz/url.h>
#include <yaz/comstack.h>
#include <yaz/log.h>
#include <yaz/url.h>
#include <yaz/comstack.h>
#include <yaz/log.h>
+#include <yaz/wrbuf.h>
+#include <yaz/cookie.h>
struct yaz_url {
ODR odr_in;
ODR odr_out;
char *proxy;
struct yaz_url {
ODR odr_in;
ODR odr_out;
char *proxy;
+ int max_redirects;
+ WRBUF w_error;
+ int verbose;
+ yaz_cookies_t cookies;
};
yaz_url_t yaz_url_create(void)
};
yaz_url_t yaz_url_create(void)
@@
-26,6
+32,10
@@
yaz_url_t yaz_url_create(void)
p->odr_in = odr_createmem(ODR_DECODE);
p->odr_out = odr_createmem(ODR_ENCODE);
p->proxy = 0;
p->odr_in = odr_createmem(ODR_DECODE);
p->odr_out = odr_createmem(ODR_ENCODE);
p->proxy = 0;
+ p->max_redirects = 10;
+ p->w_error = wrbuf_alloc();
+ p->verbose = 0;
+ p->cookies = yaz_cookies_create();
return p;
}
return p;
}
@@
-36,6
+46,8
@@
void yaz_url_destroy(yaz_url_t p)
odr_destroy(p->odr_in);
odr_destroy(p->odr_out);
xfree(p->proxy);
odr_destroy(p->odr_in);
odr_destroy(p->odr_out);
xfree(p->proxy);
+ wrbuf_destroy(p->w_error);
+ yaz_cookies_destroy(p->cookies);
xfree(p);
}
}
xfree(p);
}
}
@@
-48,6
+60,16
@@
void yaz_url_set_proxy(yaz_url_t p, const char *proxy)
p->proxy = xstrdup(proxy);
}
p->proxy = xstrdup(proxy);
}
+void yaz_url_set_max_redirects(yaz_url_t p, int num)
+{
+ p->max_redirects = num;
+}
+
+void yaz_url_set_verbose(yaz_url_t p, int num)
+{
+ p->verbose = num;
+}
+
static void extract_user_pass(NMEM nmem,
const char *uri,
char **uri_lean, char **http_user,
static void extract_user_pass(NMEM nmem,
const char *uri,
char **uri_lean, char **http_user,
@@
-86,6
+108,16
@@
static void extract_user_pass(NMEM nmem,
*uri_lean = nmem_strdup(nmem, uri);
}
*uri_lean = nmem_strdup(nmem, uri);
}
+const char *yaz_url_get_error(yaz_url_t p)
+{
+ return wrbuf_cstr(p->w_error);
+}
+
+static void log_warn(yaz_url_t p)
+{
+ yaz_log(YLOG_WARN, "yaz_url: %s", wrbuf_cstr(p->w_error));
+}
+
Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
const char *method,
Z_HTTP_Header *user_headers,
Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
const char *method,
Z_HTTP_Header *user_headers,
@@
-94,6
+126,8
@@
Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
Z_HTTP_Response *res = 0;
int number_of_redirects = 0;
Z_HTTP_Response *res = 0;
int number_of_redirects = 0;
+ yaz_cookies_reset(p->cookies);
+ wrbuf_rewind(p->w_error);
while (1)
{
void *add;
while (1)
{
void *add;
@@
-111,6
+145,7
@@
Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
gdu = z_get_HTTP_Request_uri(p->odr_out, uri_lean, 0, p->proxy ? 1 : 0);
gdu->u.HTTP_Request->method = odr_strdup(p->odr_out, method);
gdu = z_get_HTTP_Request_uri(p->odr_out, uri_lean, 0, p->proxy ? 1 : 0);
gdu->u.HTTP_Request->method = odr_strdup(p->odr_out, method);
+ yaz_cookies_request(p->cookies, p->odr_out, gdu->u.HTTP_Request);
for ( ; user_headers; user_headers = user_headers->next)
{
/* prefer new Host over user-supplied Host */
for ( ; user_headers; user_headers = user_headers->next)
{
/* prefer new Host over user-supplied Host */
@@
-137,25
+172,35
@@
Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
}
if (!z_GDU(p->odr_out, &gdu, 0, 0))
{
}
if (!z_GDU(p->odr_out, &gdu, 0, 0))
{
- yaz_log(YLOG_WARN, "Can not encode HTTP request URL:%s", uri);
+ wrbuf_printf(p->w_error, "Can not encode HTTP request for URL %s",
+ uri);
+ log_warn(p);
return 0;
}
conn = cs_create_host_proxy(uri_lean, 1, &add, p->proxy);
if (!conn)
{
return 0;
}
conn = cs_create_host_proxy(uri_lean, 1, &add, p->proxy);
if (!conn)
{
- yaz_log(YLOG_WARN, "Could not resolve URL: %s", uri);
+ wrbuf_printf(p->w_error, "Can not resolve URL %s", uri);
+ log_warn(p);
}
else if (cs_connect(conn, add) < 0)
{
}
else if (cs_connect(conn, add) < 0)
{
- yaz_log(YLOG_WARN, "Can not connect to URL: %s", uri);
+ wrbuf_printf(p->w_error, "Can not connect to URL %s", uri);
+ log_warn(p);
}
else
{
int len;
char *buf = odr_getbuf(p->odr_out, &len, 0);
}
else
{
int len;
char *buf = odr_getbuf(p->odr_out, &len, 0);
+ if (p->verbose)
+ fwrite(buf, 1, len, stdout);
+
if (cs_put(conn, buf, len) < 0)
if (cs_put(conn, buf, len) < 0)
- yaz_log(YLOG_WARN, "cs_put failed URL: %s", uri);
+ {
+ wrbuf_printf(p->w_error, "cs_put fail for URL %s", uri);
+ log_warn(p);
+ }
else
{
char *netbuffer = 0;
else
{
char *netbuffer = 0;
@@
-163,17
+208,21
@@
Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
int cs_res = cs_get(conn, &netbuffer, &netlen);
if (cs_res <= 0)
{
int cs_res = cs_get(conn, &netbuffer, &netlen);
if (cs_res <= 0)
{
- yaz_log(YLOG_WARN, "cs_get failed URL: %s", uri);
+ wrbuf_printf(p->w_error, "cs_get failed for URL %s", uri);
+ log_warn(p);
}
else
{
Z_GDU *gdu;
}
else
{
Z_GDU *gdu;
+ if (p->verbose)
+ fwrite(netbuffer, 1, cs_res, stdout);
odr_setbuf(p->odr_in, netbuffer, cs_res, 0);
if (!z_GDU(p->odr_in, &gdu, 0, 0)
|| gdu->which != Z_GDU_HTTP_Response)
{
odr_setbuf(p->odr_in, netbuffer, cs_res, 0);
if (!z_GDU(p->odr_in, &gdu, 0, 0)
|| gdu->which != Z_GDU_HTTP_Response)
{
- yaz_log(YLOG_WARN, "HTTP decoding failed "
- "URL:%s", uri);
+ wrbuf_printf(p->w_error, "HTTP decoding fail for "
+ "URL %s", uri);
+ log_warn(p);
}
else
{
}
else
{
@@
-189,15
+238,20
@@
Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
break;
code = res->code;
location = z_HTTP_header_lookup(res->headers, "Location");
break;
code = res->code;
location = z_HTTP_header_lookup(res->headers, "Location");
- if (++number_of_redirects < 10 &&
+ if (++number_of_redirects <= p->max_redirects &&
location && (code == 301 || code == 302 || code == 307))
{
location && (code == 301 || code == 302 || code == 307))
{
+ int host_change = 0;
+ const char *nlocation = yaz_check_location(p->odr_in, uri,
+ location, &host_change);
+
odr_reset(p->odr_out);
odr_reset(p->odr_out);
- uri = odr_strdup(p->odr_out, location);
- odr_reset(p->odr_in);
+ uri = odr_strdup(p->odr_out, nlocation);
}
else
break;
}
else
break;
+ yaz_cookies_response(p->cookies, res);
+ odr_reset(p->odr_in);
}
return res;
}
}
return res;
}