Changed the way yaz-client encodes charset negotiation information
[yaz-moved-to-github.git] / src / seshigh.c
index 5b2ecf6..999d1ba 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2007, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: seshigh.c,v 1.113 2007-04-12 13:52:57 adam Exp $
+ * $Id: seshigh.c,v 1.121 2007-05-30 21:56:59 adam Exp $
  */
 /**
  * \file seshigh.c
@@ -496,6 +496,8 @@ static void assoc_init_reset(association *assoc)
     assoc->init->implementation_version = 0;
     assoc->init->implementation_id = 0;
     assoc->init->implementation_name = 0;
+    assoc->init->query_charset = 0;
+    assoc->init->records_in_same_charset = 0;
     assoc->init->bend_sort = NULL;
     assoc->init->bend_search = NULL;
     assoc->init->bend_present = NULL;
@@ -532,14 +534,15 @@ static int srw_bend_init(association *assoc, Z_SRW_diagnostic **d, int *num, Z_S
         
         if (sr->username)
         {
-            Z_IdAuthentication *auth = odr_malloc(assoc->decode, sizeof(*auth));
+            Z_IdAuthentication *auth = (Z_IdAuthentication *)
+                odr_malloc(assoc->decode, sizeof(*auth));
             int len;
 
             len = strlen(sr->username) + 1;
             if (sr->password) 
                 len += strlen(sr->password) + 2;
             auth->which = Z_IdAuthentication_open;
-            auth->u.open = odr_malloc(assoc->decode, len);
+            auth->u.open = (char *) odr_malloc(assoc->decode, len);
             strcpy(auth->u.open, sr->username);
             if (sr->password && *sr->password)
             {
@@ -581,7 +584,7 @@ static int retrieve_fetch(association *assoc, bend_fetch_rr *rr)
 #if YAZ_HAVE_XML2
     yaz_record_conv_t rc = 0;
     const char *match_schema = 0;
-    int *match_syntax = 0;
+    Odr_oid *match_syntax = 0;
 
     if (assoc->server)
     {
@@ -625,7 +628,7 @@ static int retrieve_fetch(association *assoc, bend_fetch_rr *rr)
             if (input_syntax_raw)
             {
                 char oidbuf[OID_STR_MAX];
-                oid_to_dotstring(input_syntax_raw, oidbuf);
+                oid_oid_to_dotstring(input_syntax_raw, oidbuf);
                 rr->errstring = odr_strdup(rr->stream, oidbuf);
             }
             return -1;
@@ -652,7 +655,7 @@ static int retrieve_fetch(association *assoc, bend_fetch_rr *rr)
         else
         {
             rr->len = wrbuf_len(output_record);
-            rr->record = odr_malloc(rr->stream, rr->len);
+            rr->record = (char *) odr_malloc(rr->stream, rr->len);
             memcpy(rr->record, wrbuf_buf(output_record), rr->len);
         }
         wrbuf_destroy(output_record);
@@ -679,10 +682,8 @@ static int srw_bend_fetch(association *assoc, int pos,
     rr.setname = "default";
     rr.number = pos;
     rr.referenceId = 0;
-    rr.request_format = yaz_string_to_oid_odr(yaz_oid_std(),
-                                              CLASS_RECSYN,
-                                              OID_STR_XML,
-                                              assoc->decode);
+    rr.request_format = odr_oiddup(assoc->decode, yaz_oid_recsyn_xml);
+
     rr.comp = (Z_RecordComposition *)
             odr_malloc(assoc->decode, sizeof(*rr.comp));
     rr.comp->which = Z_RecordComp_complex;
@@ -742,7 +743,7 @@ static int srw_bend_fetch(association *assoc, int pos,
         if (rr.errstring)
             len += strlen(rr.errstring);
 
-        record->recordData_buf = odr_malloc(o, len);
+        record->recordData_buf = (char *) odr_malloc(o, len);
         
         sprintf(record->recordData_buf, "<diagnostic "
                 "xmlns=\"http://www.loc.gov/zing/srw/diagnostic/\">\n"
@@ -1151,7 +1152,7 @@ static void srw_bend_search(association *assoc, request *req,
 static char *srw_bend_explain_default(void *handle, bend_explain_rr *rr)
 {
 #if YAZ_HAVE_XML2
-    xmlNodePtr ptr = rr->server_node_ptr;
+    xmlNodePtr ptr = (xmlNode *) rr->server_node_ptr;
     if (!ptr)
         return 0;
     for (ptr = ptr->children; ptr; ptr = ptr->next)
@@ -1263,12 +1264,14 @@ static void srw_bend_scan(association *assoc, request *req,
         bsrr->print = assoc->print;
         bsrr->step_size = odr_intdup(assoc->decode, 0);
         bsrr->entries = 0;
+        bsrr->setname = 0;
 
         if (bsrr->num_entries > 0) 
         {
             int i;
-            bsrr->entries = odr_malloc(assoc->decode, sizeof(*bsrr->entries) *
-                                       bsrr->num_entries);
+            bsrr->entries = (struct scan_entry *) 
+                odr_malloc(assoc->decode, sizeof(*bsrr->entries) *
+                           bsrr->num_entries);
             for (i = 0; i<bsrr->num_entries; i++)
             {
                 bsrr->entries[i].term = 0;
@@ -1300,7 +1303,8 @@ static void srw_bend_scan(association *assoc, request *req,
             int srw_error;
             bsrr->scanClause = 0;
             bsrr->attributeset = 0;
-            bsrr->term = odr_malloc(assoc->decode, sizeof(*bsrr->term));
+            bsrr->term = (Z_AttributesPlusTerm *)
+                odr_malloc(assoc->decode, sizeof(*bsrr->term));
             srw_error = cql2pqf_scan(assoc->encode,
                                      srw_req->scanClause.cql,
                                      assoc->server->cql_transform,
@@ -1671,7 +1675,7 @@ static char *read_file(const char *fname, ODR o, int *sz)
     fseek(inf, 0L, SEEK_END);
     *sz = ftell(inf);
     rewind(inf);
-    buf = odr_malloc(o, *sz);
+    buf = (char *) odr_malloc(o, *sz);
     fread(buf, 1, *sz, inf);
     fclose(inf);
     return buf;     
@@ -2148,9 +2152,7 @@ static int process_z_response(association *assoc, request *req, Z_APDU *res)
 
 static char *get_vhost(Z_OtherInformation *otherInfo)
 {
-    const int *oid = yaz_string_to_oid(yaz_oid_std(),
-                                       CLASS_USERINFO, OID_STR_PROXY);
-    return yaz_oi_get_string_oid(&otherInfo, oid, 1, 0);
+    return yaz_oi_get_string_oid(&otherInfo, yaz_oid_userinfo_proxy, 1, 0);
 }
 
 /*
@@ -2221,7 +2223,8 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
     else
     {
         /* no backend. return error */
-        binitres = odr_malloc(assoc->encode, sizeof(*binitres));
+        binitres = (bend_initresult *)
+            odr_malloc(assoc->encode, sizeof(*binitres));
         binitres->errstring = 0;
         binitres->errcode = YAZ_BIB1_PERMANENT_SYSTEM_ERROR;
         iochan_settimeout(assoc->client_chan, 10);
@@ -2286,26 +2289,34 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
         ODR_MASK_SET(resp->options, Z_Options_sort);
         strcat(options, " sort");
     }
-
-    if (ODR_MASK_GET(req->options, Z_Options_negotiationModel)
-        && assoc->init->charneg_response)
+    
+    if (ODR_MASK_GET(req->options, Z_Options_negotiationModel))
     {
-        Z_OtherInformation **p;
         Z_OtherInformationUnit *p0;
-        
-        yaz_oi_APDU(apdu, &p);
-        
-        if ((p0=yaz_oi_update(p, assoc->encode, NULL, 0, 0))) {
-            ODR_MASK_SET(resp->options, Z_Options_negotiationModel);
-            
+
+        if (!assoc->init->charneg_response)
+        {
+            if (assoc->init->query_charset)
+            {
+                assoc->init->charneg_response = yaz_set_response_charneg(
+                    assoc->encode, assoc->init->query_charset, 0, 
+                    assoc->init->records_in_same_charset);
+            }
+            else
+            {
+                yaz_log(YLOG_WARN, "default query_charset not defined by backend");
+            }
+        }
+        if (assoc->init->charneg_response
+            && (p0=yaz_oi_update(&resp->otherInfo, assoc->encode, NULL, 0, 0)))
+        {
             p0->which = Z_OtherInfo_externallyDefinedInfo;
             p0->information.externallyDefinedInfo =
                 assoc->init->charneg_response;
+            ODR_MASK_SET(resp->options, Z_Options_negotiationModel);
+            strcat(options, " negotiation");
         }
-        ODR_MASK_SET(resp->options, Z_Options_negotiationModel);
-        strcat(options, " negotiation");
     }
-        
     if (ODR_MASK_GET(req->options, Z_Options_triggerResourceCtrl))
         ODR_MASK_SET(resp->options, Z_Options_triggerResourceCtrl);
 
@@ -2344,7 +2355,7 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
                 assoc->init->implementation_name,
                 odr_prepend(assoc->encode, "GFS", resp->implementationName));
 
-    version = odr_strdup(assoc->encode, "$Revision: 1.113 $");
+    version = odr_strdup(assoc->encode, "$Revision: 1.121 $");
     if (strlen(version) > 10)   /* check for unexpanded CVS strings */
         version[strlen(version)-2] = '\0';
     resp->implementationVersion = odr_prepend(assoc->encode,
@@ -2458,7 +2469,7 @@ static Z_Records *pack_records(association *a, char *setname, int start,
                                int *num, Z_RecordComposition *comp,
                                int *next, int *pres,
                                Z_ReferenceId *referenceId,
-                               int *oid, int *errcode)
+                               Odr_oid *oid, int *errcode)
 {
     int recno, total_length = 0, toget = *num, dumped_records = 0;
     Z_Records *records =
@@ -3000,6 +3011,8 @@ static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
     bsrr->stream = assoc->encode;
     bsrr->print = assoc->print;
     bsrr->step_size = res->stepSize;
+    bsrr->setname = yaz_oi_get_string_oid(&req->otherInfo, 
+                                          yaz_oid_userinfo_scan_set, 1, 0);
     bsrr->entries = 0;
     /* For YAZ 2.0 and earlier it was the backend handler that
        initialized entries (member display_term did not exist)
@@ -3011,8 +3024,9 @@ static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
     if (bsrr->num_entries > 0) 
     {
         int i;
-        bsrr->entries = odr_malloc(assoc->decode, sizeof(*bsrr->entries) *
-                                   bsrr->num_entries);
+        bsrr->entries = (struct scan_entry *)
+            odr_malloc(assoc->decode, sizeof(*bsrr->entries) *
+                       bsrr->num_entries);
         for (i = 0; i<bsrr->num_entries; i++)
         {
             bsrr->entries[i].term = 0;
@@ -3112,35 +3126,32 @@ static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
         int i;
         WRBUF wr = wrbuf_alloc();
         wrbuf_printf(wr, "Scan ");
-        for (i = 0 ; i < req->num_databaseNames; i++){
+        for (i = 0 ; i < req->num_databaseNames; i++)
+        {
             if (i)
                 wrbuf_printf(wr, "+");
             wrbuf_printf(wr, req->databaseNames[i]);
         }
+
         wrbuf_printf(wr, " ");
         
-        if (bsrr->errcode){
+        if (bsrr->errcode)
             wr_diag(wr, bsrr->errcode, bsrr->errstring);
-            wrbuf_printf(wr, " ");
-        }
-        else
-            wrbuf_printf(wr, "OK "); 
-        /* else if (*res->scanStatus == Z_Scan_success) */
-        /*    wrbuf_printf(wr, "OK "); */
-        /* else */
-        /* wrbuf_printf(wr, "Partial "); */
-
-        if (*res->numberOfEntriesReturned)
-            wrbuf_printf(wr, "%d - ", *res->numberOfEntriesReturned);
         else
-            wrbuf_printf(wr, "0 - ");
+            wrbuf_printf(wr, "OK"); 
 
-        wrbuf_printf(wr, "%d+%d+%d ",
+        wrbuf_printf(wr, " %d - %d+%d+%d",
+                     res->numberOfEntriesReturned ?
+                     *res->numberOfEntriesReturned : 0,
                      (req->preferredPositionInResponse ?
                       *req->preferredPositionInResponse : 1),
                      *req->numberOfTermsRequested,
                      (res->stepSize ? *res->stepSize : 1));
+        
+        if (bsrr->setname)
+            wrbuf_printf(wr, "+%s", bsrr->setname);
 
+        wrbuf_printf(wr, " ");
         yaz_scan_to_wrbuf(wr, req->termListAndStartPoint, 
                           bsrr->attributeset);
         yaz_log(log_request, "%s", wrbuf_cstr(wr) );
@@ -3499,11 +3510,10 @@ static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd)
     /* Do something with the members of bend_extendedservice */
     if (esrequest.taskPackage)
     {
-        const int *oid = yaz_string_to_oid(yaz_oid_std(),
-                                           CLASS_EXTSERV, OID_STR_EXTENDED);
-        resp->taskPackage = z_ext_record_oid(assoc->encode, oid,
-                                             (const char *)  esrequest.taskPackage,
-                                             -1);
+        resp->taskPackage = z_ext_record_oid(
+            assoc->encode, yaz_oid_recsyn_extended,
+            (const char *)  esrequest.taskPackage, -1
+            );
     }
     yaz_log(YLOG_DEBUG,"Send the result apdu");
     return apdu;