X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fzoom-c.c;h=7fd780dc4bcdab24e7391d6182bf0b542e65e539;hp=e7f25c29f4736629fcfc948048e615f59641bd37;hb=871c6b4f403790572c3501ec68d8ea7d12e883f7;hpb=f3fa080453f6e57618216f6e58d871fb06f0d1b6 diff --git a/src/zoom-c.c b/src/zoom-c.c index e7f25c2..7fd780d 100644 --- a/src/zoom-c.c +++ b/src/zoom-c.c @@ -49,7 +49,7 @@ static void initlog(void) } } -void ZOOM_connection_remove_tasks(ZOOM_connection c); +static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri); void ZOOM_set_dset_error(ZOOM_connection c, int error, const char *dset, @@ -83,7 +83,6 @@ void ZOOM_set_dset_error(ZOOM_connection c, int error, c, c->host_port ? c->host_port : "<>", dset, error, addinfo ? addinfo : "", addinfo2 ? addinfo2 : ""); - ZOOM_connection_remove_tasks(c); } } @@ -265,6 +264,7 @@ ZOOM_API(ZOOM_connection) c->host_port = 0; c->proxy = 0; c->tproxy = 0; + c->proxy_mode = 0; c->charset = c->lang = 0; @@ -296,6 +296,7 @@ ZOOM_API(ZOOM_connection) c->sru_version = 0; c->no_redirects = 0; c->cookies = 0; + c->location = 0; c->saveAPDU_wrbuf = 0; ZOOM_memcached_init(c); @@ -370,19 +371,6 @@ ZOOM_API(void) ZOOM_set_error(c, ZOOM_ERROR_NONE, 0); ZOOM_connection_remove_tasks(c); - if (c->odr_print) - { - odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */ - odr_destroy(c->odr_print); - } - if (ZOOM_options_get_bool(c->options, "apdulog", 0)) - { - c->odr_print = odr_createmem(ODR_PRINT); - odr_setprint(c->odr_print, yaz_log_file()); - } - else - c->odr_print = 0; - if (c->cs) { yaz_log(c->log_details, "%p ZOOM_connection_connect reconnect ok", c); @@ -556,6 +544,33 @@ ZOOM_API(void) return; } + if (c->odr_print) + odr_destroy(c->odr_print); + c->odr_print = 0; + val = ZOOM_options_get(c->options, "apdufile"); + if (val) + { + c->odr_print = odr_createmem(ODR_PRINT); + if (strcmp(val, "-")) + { + FILE *f = fopen(val, "a"); + if (!f) + { + WRBUF w = wrbuf_alloc(); + wrbuf_printf(w, "fopen: %s", val); + ZOOM_set_error(c, ZOOM_ERROR_INTERNAL, wrbuf_cstr(w)); + wrbuf_destroy(w); + return; + } + odr_setprint(c->odr_print, f); + } + } + else if (ZOOM_options_get_bool(c->options, "apdulog", 0)) + { + c->odr_print = odr_createmem(ODR_PRINT); + odr_setprint_noclose(c->odr_print, yaz_log_file()); + } + yaz_log(c->log_details, "%p ZOOM_connection_connect async=%d", c, c->async); ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT); @@ -609,10 +624,7 @@ ZOOM_API(void) if (c->odr_save) odr_destroy(c->odr_save); if (c->odr_print) - { - odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */ odr_destroy(c->odr_print); - } ZOOM_options_destroy(c->options); ZOOM_connection_remove_tasks(c); ZOOM_connection_remove_events(c); @@ -628,6 +640,7 @@ ZOOM_API(void) xfree(c->group); xfree(c->password); xfree(c->sru_version); + xfree(c->location); yaz_cookies_destroy(c->cookies); wrbuf_destroy(c->saveAPDU_wrbuf); xfree(c); @@ -1064,8 +1077,11 @@ static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url) if (c->cs) cs_close(c->cs); - c->cs = cs_create_host_proxy(logical_url, CS_FLAGS_DNS_NO_BLOCK, &add, - c->tproxy ? c->tproxy : c->proxy); + c->cs = cs_create_host2(logical_url, CS_FLAGS_DNS_NO_BLOCK, &add, + c->tproxy ? c->tproxy : c->proxy, + &c->proxy_mode); + if (!c->proxy) + c->proxy_mode = 0; if (c->cs && c->cs->protocol == PROTO_HTTP) { @@ -1093,7 +1109,11 @@ static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url) assert(c->tasks->which == ZOOM_TASK_CONNECT); ZOOM_connection_remove_task(c); ZOOM_connection_set_mask(c, 0); - ZOOM_connection_exec_task(c); + + if (c->cs && c->location) + send_HTTP_redirect(c, c->location); + else + ZOOM_connection_exec_task(c); } c->state = STATE_ESTABLISHED; return zoom_pending; @@ -1483,7 +1503,6 @@ ZOOM_API(int) return 1; } -#if YAZ_HAVE_XML2 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri) { @@ -1498,9 +1517,12 @@ static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri) z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers, c->user, c->password); } + xfree(c->location); + c->location = 0; return ZOOM_send_GDU(c, gdu); } + zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu) { ZOOM_Event event; @@ -1521,15 +1543,13 @@ zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu) return ZOOM_send_buf(c); } -#if YAZ_HAVE_XML2 void ZOOM_set_HTTP_error(ZOOM_connection c, int error, const char *addinfo, const char *addinfo2) { ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2); } -#endif - +#if YAZ_HAVE_XML2 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres) { zoom_ret cret = zoom_complete; @@ -1561,9 +1581,9 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres) int host_change = 0; location = yaz_check_location(c->odr_in, c->host_port, location, &host_change); - if (do_connect_host(c, location) == zoom_complete) - return; /* connect failed.. */ - send_HTTP_redirect(c, location); + xfree(c->location); + c->location = xstrdup(location); + do_connect_host(c, location); return; } } @@ -1657,22 +1677,26 @@ static int do_read(ZOOM_connection c) if (!z_GDU(c->odr_in, &gdu, 0, 0)) { - int x; - int err = odr_geterrorx(c->odr_in, &x); - char msg[100]; - const char *element = odr_getelement(c->odr_in); - yaz_snprintf(msg, sizeof(msg), - "ODR code %d:%d element=%s offset=%d", - err, x, element ? element : "", - odr_offset(c->odr_in)); - ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg); - if (c->log_api) + /* even on failures we try to re-connect (HTTP) */ + if (!ZOOM_test_reconnect(c)) { - FILE *ber_file = yaz_log_file(); - if (ber_file) - odr_dumpBER(ber_file, c->buf_in, r); + int x; + int err = odr_geterrorx(c->odr_in, &x); + char msg[100]; + const char *element = odr_getelement(c->odr_in); + yaz_snprintf(msg, sizeof(msg), + "ODR code %d:%d element=%s offset=%d", + err, x, element ? element : "", + odr_offset(c->odr_in)); + ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg); + if (c->log_api) + { + FILE *ber_file = yaz_log_file(); + if (ber_file) + odr_dumpBER(ber_file, c->buf_in, r); + } + ZOOM_connection_close(c); } - ZOOM_connection_close(c); } else { @@ -1960,7 +1984,7 @@ static void ZOOM_connection_do_io(ZOOM_connection c, int mask) if (c->cs->io_pending & CS_WANT_READ) mask += ZOOM_SELECT_READ; ZOOM_connection_set_mask(c, mask); - event = ZOOM_Event_create(ZOOM_EVENT_NONE); + event = ZOOM_Event_create(ZOOM_EVENT_CONNECT); ZOOM_connection_put_event(c, event); } else if (ret == 0) @@ -1973,10 +1997,15 @@ static void ZOOM_connection_do_io(ZOOM_connection c, int mask) else { /* no init request for SRW .. */ - assert(c->tasks->which == ZOOM_TASK_CONNECT); - ZOOM_connection_remove_task(c); - ZOOM_connection_set_mask(c, 0); - ZOOM_connection_exec_task(c); + if (c->tasks->which == ZOOM_TASK_CONNECT) + { + ZOOM_connection_remove_task(c); + ZOOM_connection_set_mask(c, 0); + } + if (c->cs && c->location) + send_HTTP_redirect(c, c->location); + else + ZOOM_connection_exec_task(c); } c->state = STATE_ESTABLISHED; }