\param buf content buffer for HTTP request, NULL for empty content
\param len content length for HTTP request
\returns HTTP response; NULL on ERROR.
+
+ Use yaz_url_get_error to get details if NULL is returned.
*/
YAZ_EXPORT Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
const char *method,
Z_HTTP_Header *headers,
const char *buf, size_t len);
+
+/** \brief get last error from yaz_url_exec
+ \param p handle
+ \returns message (possibly empty string no error occurred)
+*/
+YAZ_EXPORT const char *yaz_url_get_error(yaz_url_t p);
+
YAZ_END_CDECL
#endif
#include <yaz/url.h>
#include <yaz/comstack.h>
#include <yaz/log.h>
+#include <yaz/wrbuf.h>
struct yaz_url {
ODR odr_in;
ODR odr_out;
char *proxy;
int max_redirects;
+ WRBUF w_error;
};
yaz_url_t yaz_url_create(void)
p->odr_out = odr_createmem(ODR_ENCODE);
p->proxy = 0;
p->max_redirects = 10;
+ p->w_error = wrbuf_alloc();
return p;
}
odr_destroy(p->odr_in);
odr_destroy(p->odr_out);
xfree(p->proxy);
+ wrbuf_destroy(p->w_error);
xfree(p);
}
}
*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 *res = 0;
int number_of_redirects = 0;
+ wrbuf_rewind(p->w_error);
while (1)
{
void *add;
}
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)
{
- 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)
{
- 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
{
char *buf = odr_getbuf(p->odr_out, &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;
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
{
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
{