+ m_client->m_last_ok = 0;
+ m_client->m_cache.clear();
+ m_client->m_resultSetStartPoint = 0;
+
+ xfree (m_client->m_last_resultSetId);
+ m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
+
+ m_client->m_last_databases.set(sr->num_databaseNames,
+ (const char **) sr->databaseNames);
+ }
+ return apdu;
+}
+
+
+void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu, int len)
+{
+ char *cp = strchr(m_session_str, ' ');
+ m_request_no++;
+ if (cp)
+ sprintf(cp+1, "%d ", m_request_no);
+
+ int reduce = 0;
+ m_bytes_recv += len;
+
+ yaz_log (LOG_DEBUG, "%sReceiving %s from client %d bytes", m_session_str,
+ apdu_name(apdu), len);
+
+ if (m_bw_hold_PDU) // double incoming PDU. shutdown now.
+ shutdown();
+
+ m_bw_stat.add_bytes(len);
+ m_pdu_stat.add_bytes(1);
+
+ int bw_total = m_bw_stat.get_total();
+ int pdu_total = m_pdu_stat.get_total();
+
+ yaz_log(LOG_LOG, "%sstat bw=%d pdu=%d limit-bw=%d limit-pdu=%d",
+ m_session_str, bw_total, pdu_total, m_bw_max, m_pdu_max);
+ if (m_bw_max)
+ {
+ if (bw_total > m_bw_max)
+ {
+ reduce = (bw_total/m_bw_max);
+ }
+ }
+ if (m_pdu_max)
+ {
+ if (pdu_total > m_pdu_max)
+ {
+ int nreduce = (60/m_pdu_max);
+ reduce = (reduce > nreduce) ? reduce : nreduce;
+ }
+ }
+ if (reduce)
+ {
+ yaz_log(LOG_LOG, "%sLimit delay=%d", m_session_str, reduce);
+ m_bw_hold_PDU = apdu; // save PDU and signal "on hold"
+ timeout(reduce); // call us reduce seconds later
+ }
+ else
+ recv_Z_PDU_0(apdu); // all fine. Proceed receive PDU as usual
+}
+
+void Yaz_Proxy::handle_max_record_retrieve(Z_APDU *apdu)
+{
+ if (m_max_record_retrieve)
+ {
+ if (apdu->which == Z_APDU_presentRequest)
+ {
+ Z_PresentRequest *pr = apdu->u.presentRequest;
+ if (pr->numberOfRecordsRequested &&
+ *pr->numberOfRecordsRequested > m_max_record_retrieve)
+ *pr->numberOfRecordsRequested = m_max_record_retrieve;
+ }
+ }
+}
+
+Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr,
+ int error,
+ const char *addinfo)
+{
+ Z_Records *rec = (Z_Records *)
+ odr_malloc (odr, sizeof(*rec));
+ int *err = (int *)
+ odr_malloc (odr, sizeof(*err));
+ Z_DiagRec *drec = (Z_DiagRec *)
+ odr_malloc (odr, sizeof(*drec));
+ Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
+ odr_malloc (odr, sizeof(*dr));
+ *err = error;
+ rec->which = Z_Records_NSD;
+ rec->u.nonSurrogateDiagnostic = dr;
+ dr->diagnosticSetId =
+ yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
+ dr->condition = err;
+ dr->which = Z_DefaultDiagFormat_v2Addinfo;
+ dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
+ return rec;
+}
+
+Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
+{
+ if (apdu->which == Z_APDU_searchRequest)
+ {
+ Z_SearchRequest *sr = apdu->u.searchRequest;
+ int err = 0;
+ char *addinfo = 0;
+
+ Yaz_ProxyConfig *cfg = check_reconfigure();
+ if (cfg)
+ err = cfg->check_query(odr_encode(), m_default_target,
+ sr->query, &addinfo);
+ if (err)
+ {
+ Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
+
+ new_apdu->u.searchResponse->referenceId = sr->referenceId;
+ new_apdu->u.searchResponse->records =
+ create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
+ *new_apdu->u.searchResponse->searchStatus = 0;
+
+ send_to_client(new_apdu);
+
+ return 0;
+ }
+ }
+ return apdu;
+}
+
+Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
+{
+ m_marcxml_flag = 0;
+ if (apdu->which == Z_APDU_searchRequest)
+ {
+ Z_SearchRequest *sr = apdu->u.searchRequest;
+ if (*sr->smallSetUpperBound > 0 || *sr->largeSetLowerBound > 1)
+ {
+ int err = 0;
+ char *addinfo = 0;
+ Yaz_ProxyConfig *cfg = check_reconfigure();
+
+ if (cfg)
+ err = cfg->check_syntax(odr_encode(),
+ m_default_target,
+ sr->preferredRecordSyntax,
+ &addinfo);
+ if (err == -1)
+ {
+ sr->preferredRecordSyntax =
+ yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN,
+ VAL_USMARC);
+ m_marcxml_flag = 1;
+ }
+ else if (err)
+ {
+ Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
+
+ new_apdu->u.searchResponse->referenceId = sr->referenceId;
+ new_apdu->u.searchResponse->records =
+ create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
+ *new_apdu->u.searchResponse->searchStatus = 0;
+
+ send_to_client(new_apdu);
+
+ return 0;
+ }
+ }
+ }
+ else if (apdu->which == Z_APDU_presentRequest)
+ {
+ Z_PresentRequest *pr = apdu->u.presentRequest;
+ int err = 0;
+ char *addinfo = 0;
+ Yaz_ProxyConfig *cfg = check_reconfigure();
+
+ if (cfg)
+ err = cfg->check_syntax(odr_encode(), m_default_target,
+ pr->preferredRecordSyntax,
+ &addinfo);
+ if (err == -1)
+ {
+ pr->preferredRecordSyntax =
+ yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN,
+ VAL_USMARC);
+ m_marcxml_flag = 1;
+ }
+ else if (err)
+ {
+ Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentResponse);
+
+ new_apdu->u.presentResponse->referenceId = pr->referenceId;
+ new_apdu->u.presentResponse->records =
+ create_nonSurrogateDiagnostics(odr_encode(), err, addinfo);
+ *new_apdu->u.presentResponse->presentStatus =
+ Z_PresentStatus_failure;
+
+ send_to_client(new_apdu);
+
+ return 0;
+ }