X-Git-Url: http://git.indexdata.com/?p=yazproxy-moved-to-github.git;a=blobdiff_plain;f=src%2Fyaz-proxy.cpp;h=adc52086ef16a1554f71727e0d614bd04370280d;hp=151a57c79f1bc82341cd98b34accd81ceebacaf6;hb=700507bf7216c231697bbb600ccea74f186dff2a;hpb=b276287e0e21b0bb267c5d5a7d87254ee1f5e29f diff --git a/src/yaz-proxy.cpp b/src/yaz-proxy.cpp index 151a57c..adc5208 100644 --- a/src/yaz-proxy.cpp +++ b/src/yaz-proxy.cpp @@ -1,4 +1,4 @@ -/* $Id: yaz-proxy.cpp,v 1.52 2006-04-06 17:23:14 adam Exp $ +/* $Id: yaz-proxy.cpp,v 1.56 2006-04-13 00:41:11 adam Exp $ Copyright (c) 1998-2006, Index Data. This file is part of the yazproxy. @@ -295,7 +295,6 @@ Yaz_Proxy::Yaz_Proxy(IPDU_Observable *the_PDU_Observable, m_ref_count = 1; m_main_ptr_dec = false; m_peername = 0; - m_initial_reduce = 0; } void Yaz_Proxy::inc_ref() @@ -376,7 +375,7 @@ void Yaz_Proxy::set_default_target(const char *target) void Yaz_Proxy::set_proxy_negotiation (const char *charset, const char *lang, const char *default_charset) { - yaz_log(YLOG_LOG, "%sSet the proxy negotiation: charset to '%s', " + yaz_log(YLOG_DEBUG, "%sSet the proxy negotiation: charset to '%s', " "default charset to '%s', language to '%s'", m_session_str, charset?charset:"none", default_charset?default_charset:"none", @@ -434,35 +433,17 @@ IPDU_Observer *Yaz_Proxy::sessionNotify(IPDU_Observable char session_str[200]; const char *peername = the_PDU_Observable->getpeername(); if (m_log_mask & PROXY_LOG_IP_CLIENT) - sprintf(session_str, "%ld:%d %s 0 ", - (long) time(0), m_session_no, peername); + sprintf(session_str, "%ld:%d %.80s %d ", + (long) time(0), m_session_no, peername, 0); else - sprintf(session_str, "%ld:%d 0 ", - (long) time(0), m_session_no); - m_session_no++; + sprintf(session_str, "%ld:%d %d ", + (long) time(0), m_session_no, 0); yaz_log (YLOG_LOG, "%sNew session %s", session_str, peername); - m_connect.cleanup(false); - m_connect.add_connect(peername); - - int connect_total = m_connect.get_total(peername); - int connect_max = m_max_connect; - if (connect_max && connect_total > connect_max) - { - yaz_log(YLOG_LOG, "%sconnect not accepted total=%d max=%d", - session_str, connect_total, connect_max); - return 0; - } - yaz_log(YLOG_LOG, "%sconnect accepted total=%d", session_str, - connect_total); - Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable, m_socket_observable, this); - if (m_limit_connect) - new_proxy->m_initial_reduce = connect_total / m_limit_connect; - new_proxy->m_config = 0; new_proxy->m_config_fname = 0; new_proxy->timeout(m_client_idletime); @@ -470,8 +451,12 @@ IPDU_Observer *Yaz_Proxy::sessionNotify(IPDU_Observable new_proxy->set_default_target(m_default_target); new_proxy->m_max_clients = m_max_clients; new_proxy->m_log_mask = m_log_mask; + + if (!strcmp(peername, "tcp:163.121.19.82")) // NIS GROUP + new_proxy->m_log_mask = 255; + new_proxy->set_APDU_log(get_APDU_log()); - if (m_log_mask & PROXY_LOG_APDU_CLIENT) + if (new_proxy->m_log_mask & PROXY_LOG_APDU_CLIENT) new_proxy->set_APDU_yazlog(1); else new_proxy->set_APDU_yazlog(0); @@ -748,8 +733,7 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie, yaz_log (YLOG_LOG, "%sMAXCLIENTS %d Destroy %d", m_session_str, parent->m_max_clients, c->m_seqno); if (c->m_server && c->m_server != this) - delete c->m_server; // PROBLEM: m_ref_count! - c->m_server = 0; + c->m_server->dec_ref(true); } else { @@ -764,7 +748,7 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie, if (c->m_server && c->m_server != this) { c->m_server->m_client = 0; - delete c->m_server; // PROBLEM: m_ref_count! + c->m_server->dec_ref(true); } (parent->m_seqno)++; c->m_target_idletime = m_target_idletime; @@ -1843,6 +1827,7 @@ Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu) void Yaz_Proxy::inc_request_no() { + m_request_no++; char *cp = m_session_str + strlen(m_session_str)-1; if (*cp == ' ') cp--; @@ -1874,10 +1859,77 @@ void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len) recv_GDU_more(false); } +void Yaz_Proxy::HTTP_Forwarded(Z_GDU *z_gdu) +{ + if (z_gdu->which == Z_GDU_HTTP_Request) + { + Z_HTTP_Request *hreq = z_gdu->u.HTTP_Request; + const char *x_forwarded_for = + z_HTTP_header_lookup(hreq->headers, "X-Forwarded-For"); + if (x_forwarded_for) + { + xfree(m_peername); + m_peername = (char*) xmalloc(strlen(x_forwarded_for)+5); + sprintf(m_peername, "tcp:%s", x_forwarded_for); + + yaz_log(YLOG_LOG, "%sHTTP Forwarded from %s", m_session_str, + m_peername); + if (m_log_mask & PROXY_LOG_IP_CLIENT) + sprintf(m_session_str, "%ld:%d %.80s %d ", + (long) time(0), m_session_no, m_peername, m_request_no); + else + sprintf(m_session_str, "%ld:%d %d ", + (long) time(0), m_session_no, m_request_no); + } + } +} + +void Yaz_Proxy::connect_stat(bool &block, int &reduce) +{ + + m_parent->m_connect.cleanup(false); + m_parent->m_connect.add_connect(m_peername); + + int connect_total = m_parent->m_connect.get_total(m_peername); + int max_connect = m_parent->m_max_connect; + + if (max_connect && connect_total > max_connect) + { + yaz_log(YLOG_LOG, "%sconnect not accepted total=%d max=%d", + m_session_str, connect_total, max_connect); + block = true; + } + else + block = false; + yaz_log(YLOG_LOG, "%sconnect accepted total=%d", m_session_str, + connect_total); + + int limit_connect = m_parent->m_limit_connect; + if (limit_connect) + reduce = connect_total / limit_connect; + else + reduce = 0; +} + void Yaz_Proxy::recv_GDU_reduce(GDU *gdu) { - int reduce = m_initial_reduce; // initial reduce from connect phase.. - m_initial_reduce = 0; // reset it.. + HTTP_Forwarded(gdu->get()); + + int reduce = 0; + + if (m_request_no == 1) + { + bool block = false; + + connect_stat(block, reduce); + + if (block) + { + m_timeout_mode = timeout_busy; + timeout(0); + return; + } + } int bw_total = m_bw_stat.get_total(); int pdu_total = m_pdu_stat.get_total(); @@ -2496,11 +2548,7 @@ int Yaz_Proxy::file_access(Z_HTTP_Request *hreq) while (*cp) { if (*cp == '/' && strchr("/.", cp[1])) - { - yaz_log(YLOG_LOG, "%sRejecting path %s", m_session_str, - hreq->path); return 0; - } cp++; } @@ -2610,6 +2658,8 @@ void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq) Z_SRW_diagnostic *diagnostic = 0; int num_diagnostic = 0; + yaz_log(YLOG_LOG, "%s%s %s", m_session_str, hreq->method, hreq->path); + if (file_access(hreq)) { return; @@ -3075,7 +3125,7 @@ void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu) else { // Z39.50 just shutdown - delete this; + timeout(0); return; } } @@ -3194,6 +3244,7 @@ bool Yaz_Proxy::dec_ref(bool main_ptr) assert(m_ref_count > 0); if (main_ptr) { + yaz_log(YLOG_LOG, "%sdec_ref", m_session_str); if (m_main_ptr_dec) return false; m_main_ptr_dec = true; @@ -3476,9 +3527,12 @@ void Yaz_ProxyClient::timeoutNotify() if (m_server) m_server->send_response_fail_client(get_hostname()); + + Yaz_Proxy *proxy_root = m_root; + shutdown(); - m_root->pre_init(); + proxy_root->pre_init(); } Yaz_ProxyClient::Yaz_ProxyClient(IPDU_Observable *the_PDU_Observable,