Net-Z3950-SimpleServer-1.21/0000755000175000017500000000000012772451217013775 5ustar mikemikeNet-Z3950-SimpleServer-1.21/META.yml0000664000175000017500000000077212772451217015256 0ustar mikemike--- abstract: unknown author: - unknown build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.0401, CPAN::Meta::Converter version 2.150005' license: unknown meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Net-Z3950-SimpleServer no_index: directory: - t - inc requires: {} version: '1.21' x_serialization_backend: 'CPAN::Meta::YAML version 0.012' Net-Z3950-SimpleServer-1.21/MANIFEST0000644000175000017500000000054412772451217015131 0ustar mikemikeChanges GRS1.pm INSTALL LICENSE MANIFEST MANIFEST.SKIP Makefile.PL OID.pm README.md SimpleServer.pm SimpleServer.xs grs_test.pl samples/render-search.pl test.pl ztest.pl logging-server.pl META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Net-Z3950-SimpleServer-1.21/Makefile.PL0000644000175000017500000000257412224535264015754 0ustar mikemikeuse ExtUtils::MakeMaker; # Use: perl Makefile.PL OPTIMIZE="-O0 -g -Wdeclaration-after-statement" my $yazconf = "yaz-config"; my $yazver = `$yazconf --version`; my $yazinc = `$yazconf --cflags servers`; my $yazlibs = `$yazconf --libs server`; if (!$yazver || (!$yazinc && !$yazlibs)) { die qq[ ERROR: Unable to call script: yaz-config If you are using a YAZ installation from the Debian package "yaz", you will also need to install "libyaz-dev" in order to build the SimpleServer module. ]; } ## extra_args + extra_response_data for scan appeared in 4.2.51 chomp($yazver); my ($major, $minor, $trivial) = split(/\./, $yazver); my ($needMaj, $needMin, $needTriv) = (4, 2, 51); #print "major=$major, minor=$minor, trivial=$trivial\n"; die "You have YAZ version $major.$minor.$trivial; " . "you need $needMaj.$needMin.$needTriv or better." if ($major < $needMaj || $major == $needMaj && $minor < $needMin || $major == $needMaj && $minor == $needMin && $trivial < $needTriv); # For Windows use # $yazinc = '-Ic:\yaz\include' # $yazlibs = 'c:\yaz\lib\yaz3.lib' WriteMakefile( 'NAME' => 'Net::Z3950::SimpleServer', 'VERSION_FROM' => 'SimpleServer.pm', # finds $VERSION 'LIBS' => [$yazlibs], # e.g., '-lm' 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' 'INC' => $yazinc, # e.g., '-I/usr/include/other' # OPTIMIZE => "-Wdeclaration-after-statement -g -O0", ); Net-Z3950-SimpleServer-1.21/MANIFEST.SKIP0000644000175000017500000000015212470353007015662 0ustar mikemike.gitignore .git IDMETA TODO archive debian build-stamp install-stamp libnet-z3950-simpleserver-perl.spec Net-Z3950-SimpleServer-1.21/samples/0000755000175000017500000000000012772451217015441 5ustar mikemikeNet-Z3950-SimpleServer-1.21/samples/render-search.pl0000755000175000017500000000262211540126156020516 0ustar mikemike#!/usr/bin/perl -w # $Header: /home/cvsroot/simpleserver/samples/render-search.pl,v 1.2 2002-03-05 12:03:26 mike Exp $ # # Trivial example of programming using the "augmented classes" # paradigm. This tiny SimpleServer-based Z39.50 server logs Type-1 # searches in human-readable form. It works by augmenting existing # classes (the RPN-node types) with additional methods -- something # that most OO languages would definitely not allow, but Perl does. # And it's sort of cute. use Net::Z3950::SimpleServer; use strict; my $handler = Net::Z3950::SimpleServer->new(SEARCH => \&search_handler, FETCH => \&fetch_handler); $handler->launch_server("render-search.pl", @ARGV); sub search_handler { my($args) = @_; print "got search: ", $args->{RPN}->{query}->render(), "\n"; } sub fetch_handler {} # no-op package Net::Z3950::RPN::Term; sub render { my $self = shift; return '"' . $self->{term} . '"'; } package Net::Z3950::RPN::And; sub render { my $self = shift; return '(' . $self->[0]->render() . ' AND ' . $self->[1]->render() . ')'; } package Net::Z3950::RPN::Or; sub render { my $self = shift; return '(' . $self->[0]->render() . ' OR ' . $self->[1]->render() . ')'; } package Net::Z3950::RPN::AndNot; sub render { my $self = shift; return '(' . $self->[0]->render() . ' ANDNOT ' . $self->[1]->render() . ')'; } Net-Z3950-SimpleServer-1.21/OID.pm0000644000175000017500000000617512470353007014750 0ustar mikemike## This file is part of simpleserver ## Copyright (C) 2000-2015 Index Data. ## All rights reserved. ## Redistribution and use in source and binary forms, with or without ## modification, are permitted provided that the following conditions are met: ## ## * Redistributions of source code must retain the above copyright ## notice, this list of conditions and the following disclaimer. ## * Redistributions in binary form must reproduce the above copyright ## notice, this list of conditions and the following disclaimer in the ## documentation and/or other materials provided with the distribution. ## * Neither the name of Index Data nor the names of its contributors ## may be used to endorse or promote products derived from this ## software without specific prior written permission. ## ## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY ## EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ## DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY ## DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ## (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF ## THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package Net::Z3950::OID; my $prefix = "1.2.840.10003.5."; sub unimarc { $prefix . '1' } sub intermarc { $prefix . '2' } sub ccf { $prefix . '3' } sub usmarc { $prefix . '10' } sub ukmarc { $prefix . '11' } sub normarc { $prefix . '12' } sub librismarc { $prefix . '13' } sub danmarc { $prefix . '14' } sub finmarc { $prefix . '15' } sub mab { $prefix . '16' } sub canmarc { $prefix . '17' } sub sbn { $prefix . '18' } sub picamarc { $prefix . '19' } sub ausmarc { $prefix . '20' } sub ibermarc { $prefix . '21' } sub carmarc { $prefix . '22' } sub malmarc { $prefix . '23' } sub jpmarc { $prefix . '24' } sub swemarc { $prefix . '25' } sub siglemarc { $prefix . '26' } sub isdsmarc { $prefix . '27' } sub rusmarc { $prefix . '28' } sub explain { $prefix . '100' } sub sutrs { $prefix . '101' } sub opac { $prefix . '102' } sub summary { $prefix . '103' } sub grs0 { $prefix . '104' } sub grs1 { $prefix . '105' } sub extended { $prefix . '106' } sub fragment { $prefix . '107' } sub pdf { $prefix . '109.1' } sub postscript { $prefix . '109.2' } sub html { $prefix . '109.3' } sub tiff { $prefix . '109.4' } sub gif { $prefix . '109.5' } sub jpeg { $prefix . '109.6' } sub png { $prefix . '109.7' } sub mpeg { $prefix . '109.8' } sub sgml { $prefix . '109.9' } sub tiffb { $prefix . '110.1' } sub wav { $prefix . '110.2' } sub sqlrs { $prefix . '111' } sub soif { $prefix . '1000.81.2' } sub textxml { $prefix . '109.10' } sub xml { $prefix . '109.10' } sub appxml { $prefix . '109.11' } ## $Log: OID.pm,v $ ## Revision 1.2 2001-03-13 14:54:13 sondberg ## Started CVS logging ## Net-Z3950-SimpleServer-1.21/grs_test.pl0000755000175000017500000000622411540126156016164 0ustar mikemike#!/usr/bin/perl -w use ExtUtils::testlib; use Net::Z3950::SimpleServer; use Net::Z3950::OID; use Net::Z3950::GRS1; use strict; sub dump_hash { my $href = shift; my $key; foreach $key (keys %$href) { printf("%10s => %s\n", $key, $href->{$key}); } } sub my_init_handler { my $args = shift; my $session = {}; $args->{IMP_NAME} = "DemoServer"; $args->{IMP_VER} = "3.14159"; $args->{ERR_CODE} = 0; $args->{HANDLE} = $session; } sub my_search_handler { my $args = shift; my $data = [{ name => "Peter Dornan", title => "Spokesman", collaboration => "ATLAS" }, { name => "Jorn Dines Hansen", title => "Professor", collaboration => "HERA-B" }, { name => "Alain Blondel", title => "Head of coll.", collaboration => "ALEPH" }]; my $session = $args->{HANDLE}; my $set_id = $args->{SETNAME}; my @database_list = @{ $args->{DATABASES} }; my $query = $args->{QUERY}; my $hits = 3; print "------------------------------------------------------------\n"; print "Processing query : $query\n"; printf("Database set : %s\n", join(" ", @database_list)); print "Setname : $set_id\n"; print "------------------------------------------------------------\n"; $args->{HITS} = $hits; $session->{$set_id} = $data; $session->{__HITS} = $hits; } sub my_fetch_handler { my $args = shift; my $session = $args->{HANDLE}; my $set_id = $args->{SETNAME}; my $data = $session->{$set_id}; my $offset = $args->{OFFSET}; my $grs1 = new Net::Z3950::GRS1; my $grs2 = new Net::Z3950::GRS1; my $grs3 = new Net::Z3950::GRS1; my $grs4 = new Net::Z3950::GRS1; my $field; my $record; my $hits = $session->{__HITS}; my $href = $data->[$offset - 1]; $args->{REP_FORM} = Net::Z3950::OID::grs1; foreach $field (keys %$href) { $grs1->AddElement(1, $field, &Net::Z3950::GRS1::ElementData::String, $href->{$field}); } $grs4->AddElement(4,1, &Net::Z3950::GRS1::ElementData::String, "Level 4"); $grs4->AddElement(4,2, &Net::Z3950::GRS1::ElementData::String, "Lige et felt mere"); $grs3->AddElement(3,1, &Net::Z3950::GRS1::ElementData::String, "Mit navn er Svend Gønge"); $grs3->AddElement(3,1, &Net::Z3950::GRS1::ElementData::Subtree, $grs4); $grs3->AddElement(3,1, &Net::Z3950::GRS1::ElementData::String, "Og det er bare dejligt"); $grs2->AddElement(2,1, &Net::Z3950::GRS1::ElementData::Subtree, $grs3); $grs2->AddElement(2,2, &Net::Z3950::GRS1::ElementData::String, "Underfelt"); $grs1->AddElement(1, 'subfield', &Net::Z3950::GRS1::ElementData::Subtree, $grs2); $grs1->AddElement(1, 10, &Net::Z3950::GRS1::ElementData::String, 'Imle bimle bumle'); $grs1->Render(POOL => \$record); $args->{RECORD} = $record; if ($offset == $session->{__HITS}) { $args->{LAST} = 1; } } my $handler = new Net::Z3950::SimpleServer( INIT => \&my_init_handler, SEARCH => \&my_search_handler, FETCH => \&my_fetch_handler ); $handler->launch_server("ztest.pl", @ARGV); ## $Log: grs_test.pl,v $ ## Revision 1.2 2001-09-11 13:07:07 sondberg ## Minor changes. ## ## Revision 1.1 2001/03/13 14:19:28 sondberg ## Added a modified version of ztest.pl called grs_test.pl, which shows how to ## implement support of GRS-1 record syntax. ## Net-Z3950-SimpleServer-1.21/SimpleServer.xs0000644000175000017500000014101612772450675017003 0ustar mikemike/* This file is part of simpleserver. * Copyright (C) 2000-2016 Index Data. * All rights reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Index Data nor the names of its contributors * may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "EXTERN.h" #include "perl.h" #include "proto.h" #include "embed.h" #include "XSUB.h" #include #include #include #include #include #include #include #include #include #include #include #ifdef WIN32 #else #include #endif #include #include #define GRS_MAX_FIELDS 500 #ifdef ASN_COMPILED #include #endif #ifndef sv_undef /* To fix the problem with Perl 5.6.0 */ #define sv_undef PL_sv_undef #endif YAZ_MUTEX simpleserver_mutex; typedef struct { SV *ghandle; /* Global handle specified at creation */ SV *handle; /* Per-connection handle set at Init */ NMEM nmem; int stop_flag; /* is used to stop server prematurely .. */ } Zfront_handle; #define ENABLE_STOP_SERVER 0 SV *_global_ghandle = NULL; /* To be copied into zhandle then ignored */ SV *init_ref = NULL; SV *close_ref = NULL; SV *sort_ref = NULL; SV *search_ref = NULL; SV *fetch_ref = NULL; SV *present_ref = NULL; SV *esrequest_ref = NULL; SV *delete_ref = NULL; SV *scan_ref = NULL; SV *explain_ref = NULL; SV *start_ref = NULL; PerlInterpreter *root_perl_context; #define GRS_BUF_SIZE 8192 /* * Inspects the SV indicated by svp, and returns a null pointer if * it's an undefined value, or a string allocation from `stream' * otherwise. Using this when filling in addinfo avoids those * irritating "Use of uninitialized value in subroutine entry" * warnings from Perl. */ char *string_or_undef(SV **svp, ODR stream) { STRLEN len; char *ptr; if (!SvOK(*svp)) return 0; ptr = SvPV(*svp, len); return odr_strdupn(stream, ptr, len); } CV * simpleserver_sv2cv(SV *handler) { STRLEN len; char *buf; if (SvPOK(handler)) { CV *ret; buf = SvPV( handler, len); if ( !( ret = perl_get_cv(buf, FALSE ) ) ) { fprintf( stderr, "simpleserver_sv2cv: No such handler '%s'\n\n", buf ); exit(1); } return ret; } else { return (CV *) handler; } } /* debugging routine to check for destruction of Perl interpreters */ #ifdef USE_ITHREADS void tst_clones(void) { int i; PerlInterpreter *parent = PERL_GET_CONTEXT; for (i = 0; i<5000; i++) { PerlInterpreter *perl_interp; PERL_SET_CONTEXT(parent); PL_perl_destruct_level = 2; perl_interp = perl_clone(parent, CLONEf_CLONE_HOST); PL_perl_destruct_level = 2; PERL_SET_CONTEXT(perl_interp); perl_destruct(perl_interp); perl_free(perl_interp); } exit (0); } #endif int simpleserver_clone(void) { #ifdef USE_ITHREADS yaz_mutex_enter(simpleserver_mutex); if (1) { PerlInterpreter *current = PERL_GET_CONTEXT; /* if current is unset, then we're in a new thread with * no Perl interpreter for it. So we must create one . * This will only happen when threaded is used.. */ if (!current) { PerlInterpreter *perl_interp; PERL_SET_CONTEXT( root_perl_context ); perl_interp = perl_clone(root_perl_context, CLONEf_CLONE_HOST); PERL_SET_CONTEXT( perl_interp ); } } yaz_mutex_leave(simpleserver_mutex); #endif return 0; } void simpleserver_free(void) { yaz_mutex_enter(simpleserver_mutex); if (1) { PerlInterpreter *current_interp = PERL_GET_CONTEXT; /* If current Perl Interp is different from root interp, then * we're in threaded mode and we must destroy.. */ if (current_interp != root_perl_context) { PL_perl_destruct_level = 2; PERL_SET_CONTEXT(current_interp); perl_destruct(current_interp); perl_free(current_interp); } } yaz_mutex_leave(simpleserver_mutex); } Z_GenericRecord *read_grs1(char *str, ODR o) { int type, ivalue; char line[GRS_BUF_SIZE+1], *buf, *ptr, *original; char value[GRS_BUF_SIZE+1]; Z_GenericRecord *r = 0; original = str; r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r)); r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS); r->num_elements = 0; for (;;) { Z_TaggedElement *t; Z_ElementData *c; int len; ptr = strchr(str, '\n'); if (!ptr) { return r; } len = ptr - str; if (len > GRS_BUF_SIZE) { yaz_log(YLOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE); len = GRS_BUF_SIZE; } strncpy(line, str, len); line[len] = 0; buf = line; str = ptr + 1; while (*buf && isspace(*buf)) buf++; if (*buf == '}') { memmove(original, str, strlen(str)); return r; } if (sscanf(buf, "(%d,%[^)])", &type, value) != 2) { yaz_log(YLOG_WARN, "Bad data in '%s'", buf); return r; } if (!type && *value == '0') return r; if (!(buf = strchr(buf, ')'))) return r; buf++; while (*buf && isspace(*buf)) buf++; if (r->num_elements >= GRS_MAX_FIELDS) { yaz_log(YLOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS); exit(0); } r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement)); t->tagType = odr_intdup(o, type); t->tagValue = (Z_StringOrNumeric *) odr_malloc(o, sizeof(Z_StringOrNumeric)); if ((ivalue = atoi(value))) { t->tagValue->which = Z_StringOrNumeric_numeric; t->tagValue->u.numeric = odr_intdup(o, ivalue); } else { t->tagValue->which = Z_StringOrNumeric_string; t->tagValue->u.string = odr_strdup(o, value); } t->tagOccurrence = 0; t->metaData = 0; t->appliedVariant = 0; t->content = c = (Z_ElementData *) odr_malloc(o, sizeof(Z_ElementData)); if (*buf == '{') { c->which = Z_ElementData_subtree; c->u.subtree = read_grs1(str, o); } else { c->which = Z_ElementData_string; c->u.string = odr_strdup(o, buf); } r->num_elements++; } } static void oid2str(Odr_oid *o, WRBUF buf) { for (; *o >= 0; o++) { char ibuf[16]; sprintf(ibuf, "%d", *o); wrbuf_puts(buf, ibuf); if (o[1] > 0) wrbuf_putc(buf, '.'); } } WRBUF oid2dotted(Odr_oid *oid) { WRBUF buf = wrbuf_alloc(); oid2str(oid, buf); return buf; } WRBUF zquery2pquery(Z_Query *q) { WRBUF buf = wrbuf_alloc(); if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) return 0; yaz_rpnquery_to_wrbuf(buf, q->u.type_1); return buf; } /* Lifted verbatim from Net::Z3950 yazwrap/util.c */ #include void fatal(char *fmt, ...) { va_list ap; fprintf(stderr, "FATAL (SimpleServer): "); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fprintf(stderr, "\n"); abort(); } /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */ /* * Creates a new Perl object of type `class'; the newly-created scalar * that is a reference to the blessed thingy `referent' is returned. */ static SV *newObject(char *class, SV *referent) { HV *stash; SV *sv; sv = newRV_noinc((SV*) referent); stash = gv_stashpv(class, 0); if (stash == 0) fatal("attempt to create object of undefined class '%s'", class); /*assert(stash != 0);*/ sv_bless(sv, stash); return sv; } /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */ static void setMember(HV *hv, char *name, SV *val) { /* We don't increment `val's reference count -- I think this is * right because it's created with a refcount of 1, and in fact * the reference via this hash is the only reference to it in * general. */ if (!hv_store(hv, name, (U32) strlen(name), val, (U32) 0)) fatal("couldn't store member in hash"); } /* Lifted verbatim from Net::Z3950 yazwrap/receive.c */ static SV *translateOID(Odr_oid *x) { /* Yaz represents an OID by an int array terminated by a negative * value, typically -1; we represent it as a reference to a * blessed scalar string of "."-separated elements. */ char buf[1000]; int i; *buf = '\0'; for (i = 0; x[i] >= 0; i++) { sprintf(buf + strlen(buf), "%d", (int) x[i]); if (x[i+1] >- 0) strcat(buf, "."); } /* * ### We'd like to return a blessed scalar (string) here, but of * course you can't do that in Perl: only references can be * blessed, so we'd have to return a _reference_ to a string, and * bless _that_. Better to do without the blessing, I think. */ if (1) { return newSVpv(buf, 0); } else { return newObject("Net::Z3950::APDU::OID", newSVpv(buf, 0)); } } static SV *attributes2perl(Z_AttributeList *list) { AV *av; int i; SV *attrs = newObject("Net::Z3950::RPN::Attributes", (SV*) (av = newAV())); for (i = 0; i < list->num_attributes; i++) { Z_AttributeElement *elem = list->attributes[i]; HV *hv2; SV *tmp = newObject("Net::Z3950::RPN::Attribute", (SV*) (hv2 = newHV())); if (elem->attributeSet) setMember(hv2, "attributeSet", translateOID(elem->attributeSet)); setMember(hv2, "attributeType", newSViv(*elem->attributeType)); if (elem->which == Z_AttributeValue_numeric) { setMember(hv2, "attributeValue", newSViv(*elem->value.numeric)); } else { Z_ComplexAttribute *c; Z_StringOrNumeric *son; assert(elem->which == Z_AttributeValue_complex); c = elem->value.complex; /* We ignore semantic actions and multiple values */ assert(c->num_list > 0); son = c->list[0]; if (son->which == Z_StringOrNumeric_numeric) { setMember(hv2, "attributeValue", newSViv(*son->u.numeric)); } else { /*Z_StringOrNumeric_string*/ setMember(hv2, "attributeValue", newSVpv(son->u.string, 0)); } } av_push(av, tmp); } return attrs; } static SV *f_Term_to_SV(Z_Term *term, Z_AttributeList *attributes) { HV *hv; SV *sv = newObject("Net::Z3950::RPN::Term", (SV*) (hv = newHV())); if (term->which != Z_Term_general) fatal("can't handle RPN terms other than general"); setMember(hv, "term", newSVpv((char*) term->u.general->buf, term->u.general->len)); if (attributes) { setMember(hv, "attributes", attributes2perl(attributes)); } return sv; } static SV *rpn2perl(Z_RPNStructure *s) { SV *sv; HV *hv; AV *av; Z_Operand *o; switch (s->which) { case Z_RPNStructure_simple: o = s->u.simple; switch (o->which) { case Z_Operand_resultSetId: { /* This code causes a SIGBUS on my machine, and I have no idea why. It seems as clear as day to me */ SV *sv2; char *rsid = (char*) o->u.resultSetId; /*printf("Encoding resultSetId '%s'\n", rsid);*/ sv = newObject("Net::Z3950::RPN::RSID", (SV*) (hv = newHV())); /*printf("Made sv=0x%lx, hv=0x%lx\n", (unsigned long) sv ,(unsigned long) hv);*/ sv2 = newSVpv(rsid, strlen(rsid)); setMember(hv, "id", sv2); /*printf("Set hv{id} to 0x%lx\n", (unsigned long) sv2);*/ return sv; } case Z_Operand_APT: return f_Term_to_SV(o->u.attributesPlusTerm->term, o->u.attributesPlusTerm->attributes); default: fatal("unknown RPN simple type %d", (int) o->which); } case Z_RPNStructure_complex: { SV *tmp; Z_Complex *c = s->u.complex; char *type = 0; /* vacuous assignment satisfies gcc -Wall */ switch (c->roperator->which) { case Z_Operator_and: type = "Net::Z3950::RPN::And"; break; case Z_Operator_or: type = "Net::Z3950::RPN::Or"; break; case Z_Operator_and_not: type = "Net::Z3950::RPN::AndNot"; break; case Z_Operator_prox: type = "Net::Z3950::RPN::Prox"; break; default: fatal("unknown RPN operator %d", (int) c->roperator->which); } sv = newObject(type, (SV*) (av = newAV())); if ((tmp = rpn2perl(c->s1)) == 0) return 0; av_push(av, tmp); if ((tmp = rpn2perl(c->s2)) == 0) return 0; av_push(av, tmp); if (c->roperator->which == Z_Operator_prox) { Z_ProximityOperator prox = *c->roperator->u.prox; HV *hv; tmp = newObject("Net::Z3950::RPN::Prox::Attributes", (SV*) (hv = newHV())); setMember(hv, "exclusion", newSViv(*prox.exclusion)); setMember(hv, "distance", newSViv(*prox.distance)); setMember(hv, "ordered", newSViv(*prox.ordered)); setMember(hv, "relationType", newSViv(*prox.relationType)); if (prox.which == Z_ProximityOperator_known) { setMember(hv, "known", newSViv(*prox.u.known)); } else { setMember(hv, "zprivate", newSViv(*prox.u.zprivate)); } av_push(av, tmp); } return sv; } default: fatal("unknown RPN node type %d", (int) s->which); } return 0; } /* Decode the Z_SortAttributes struct and store the whole thing into the * hash by reference */ int simpleserver_ExpandSortAttributes (HV *sort_spec, Z_SortAttributes *sattr) { WRBUF attrset_wr = wrbuf_alloc(); AV *list = newAV(); Z_AttributeList *attr_list = sattr->list; int i; oid2str(sattr->id, attrset_wr); hv_store(sort_spec, "ATTRSET", 7, newSVpv(attrset_wr->buf, attrset_wr->pos), 0); wrbuf_destroy(attrset_wr); hv_store(sort_spec, "SORT_ATTR", 9, newRV( sv_2mortal( (SV*) list ) ), 0); for (i = 0; i < attr_list->num_attributes; i++) { Z_AttributeElement *attr = *attr_list->attributes++; HV *attr_spec = newHV(); av_push(list, newRV( sv_2mortal( (SV*) attr_spec ) )); hv_store(attr_spec, "ATTR_TYPE", 9, newSViv(*attr->attributeType), 0); if (attr->which == Z_AttributeValue_numeric) { hv_store(attr_spec, "ATTR_VALUE", 10, newSViv(*attr->value.numeric), 0); } else { return 0; } } return 1; } /* Decode the Z_SortKeySpec struct and store the whole thing in a perl hash */ int simpleserver_SortKeySpecToHash (HV *sort_spec, Z_SortKeySpec *spec) { Z_SortElement *element = spec->sortElement; hv_store(sort_spec, "RELATION", 8, newSViv(*spec->sortRelation), 0); hv_store(sort_spec, "CASE", 4, newSViv(*spec->caseSensitivity), 0); hv_store(sort_spec, "MISSING", 7, newSViv(spec->which), 0); if (element->which == Z_SortElement_generic) { Z_SortKey *key = element->u.generic; if (key->which == Z_SortKey_sortField) { hv_store(sort_spec, "SORTFIELD", 9, newSVpv((char *) key->u.sortField, 0), 0); } else if (key->which == Z_SortKey_elementSpec) { Z_Specification *zspec = key->u.elementSpec; hv_store(sort_spec, "ELEMENTSPEC_TYPE", 16, newSViv(zspec->which), 0); if (zspec->which == Z_Schema_oid) { WRBUF elementSpec = wrbuf_alloc(); oid2str(zspec->schema.oid, elementSpec); hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17, newSVpv(elementSpec->buf, elementSpec->pos), 0); wrbuf_destroy(elementSpec); } else if (zspec->which == Z_Schema_uri) { hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17, newSVpv((char *) zspec->schema.uri, 0), 0); } } else if (key->which == Z_SortKey_sortAttributes) { return simpleserver_ExpandSortAttributes(sort_spec, key->u.sortAttributes); } else { return 0; } } else { return 0; } return 1; } static SV *zquery2perl(Z_Query *q) { SV *sv; HV *hv; if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101) return 0; sv = newObject("Net::Z3950::APDU::Query", (SV*) (hv = newHV())); if (q->u.type_1->attributeSetId) setMember(hv, "attributeSet", translateOID(q->u.type_1->attributeSetId)); setMember(hv, "query", rpn2perl(q->u.type_1->RPNStructure)); return sv; } int bend_sort(void *handle, bend_sort_rr *rr) { HV *href; AV *aref; AV *sort_seq; SV **temp; SV *err_code; SV *err_str; SV *status; SV *point; STRLEN len; char *ptr; char **input_setnames; Zfront_handle *zhandle = (Zfront_handle *)handle; Z_SortKeySpecList *sort_spec = rr->sort_sequence; int i; dSP; ENTER; SAVETMPS; aref = newAV(); input_setnames = rr->input_setnames; for (i = 0; i < rr->num_input_setnames; i++) { av_push(aref, newSVpv(*input_setnames++, 0)); } sort_seq = newAV(); for (i = 0; i < sort_spec->num_specs; i++) { Z_SortKeySpec *spec = *sort_spec->specs++; HV *sort_spec = newHV(); if ( simpleserver_SortKeySpecToHash(sort_spec, spec) ) av_push(sort_seq, newRV( sv_2mortal( (SV*) sort_spec ) )); else { rr->errcode = 207; return 0; } } href = newHV(); hv_store(href, "INPUT", 5, newRV( (SV*) aref), 0); hv_store(href, "OUTPUT", 6, newSVpv(rr->output_setname, 0), 0); hv_store(href, "SEQUENCE", 8, newRV( (SV*) sort_seq), 0); hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0); hv_store(href, "HANDLE", 6, zhandle->handle, 0); hv_store(href, "STATUS", 6, newSViv(0), 0); hv_store(href, "ERR_CODE", 8, newSViv(0), 0); hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0); PUSHMARK(sp); XPUSHs(sv_2mortal(newRV( (SV*) href))); PUTBACK; perl_call_sv(sort_ref, G_SCALAR | G_DISCARD); SPAGAIN; temp = hv_fetch(href, "ERR_CODE", 8, 1); err_code = newSVsv(*temp); temp = hv_fetch(href, "ERR_STR", 7, 1); err_str = newSVsv(*temp); temp = hv_fetch(href, "STATUS", 6, 1); status = newSVsv(*temp); temp = hv_fetch(href, "HANDLE", 6, 1); point = newSVsv(*temp); hv_undef(href); av_undef(aref); av_undef(sort_seq); sv_free( (SV*) aref); sv_free( (SV*) href); sv_free( (SV*) sort_seq); rr->errcode = SvIV(err_code); rr->sort_status = SvIV(status); ptr = SvPV(err_str, len); rr->errstring = odr_strdupn(rr->stream, ptr, len); zhandle->handle = point; sv_free(err_code); sv_free(err_str); sv_free(status); PUTBACK; FREETMPS; LEAVE; return 0; } static SV *f_FacetField_to_SV(Z_FacetField *facet_field) { HV *hv; AV *av; SV *terms; int i; SV *sv = newObject("Net::Z3950::FacetField", (SV *) (hv = newHV())); if (facet_field->attributes) { setMember(hv, "attributes", attributes2perl(facet_field->attributes)); } terms = newObject("Net::Z3950::FacetTerms", (SV *) (av = newAV())); for (i = 0; i < facet_field->num_terms; i++) { Z_Term *z_term = facet_field->terms[i]->term; HV *hv; SV *sv_count = newSViv(*facet_field->terms[i]->count); SV *sv_term; SV *tmp; if (z_term->which == Z_Term_general) { sv_term = newSVpv((char*) z_term->u.general->buf, z_term->u.general->len); } else if (z_term->which == Z_Term_characterString) { sv_term = newSVpv(z_term->u.characterString, strlen(z_term->u.characterString)); } tmp = newObject("Net::Z3950::FacetTerm", (SV *) (hv = newHV())); setMember(hv, "count", sv_count); setMember(hv, "term", sv_term); av_push(av, tmp); } setMember(hv, "terms", terms); return sv; } static SV *f_FacetList_to_SV(Z_FacetList *facet_list) { SV *sv = 0; if (facet_list) { AV *av; int i; sv = newObject("Net::Z3950::FacetList", (SV *) (av = newAV())); for (i = 0; i < facet_list->num; i++) { SV *sv = f_FacetField_to_SV(facet_list->elements[i]); av_push(av, sv); } } return sv; } static void f_SV_to_FacetField(HV *facet_field_hv, Z_FacetField **fl, ODR odr) { int i; int num_terms, num_attributes; SV **temp; Z_AttributeList *attributes = odr_malloc(odr, sizeof(*attributes)); AV *sv_terms, *sv_attributes; temp = hv_fetch(facet_field_hv, "attributes", 10, 1); sv_attributes = (AV *) SvRV(*temp); num_attributes = av_len(sv_attributes) + 1; attributes->num_attributes = num_attributes; attributes->attributes = (Z_AttributeElement **) odr_malloc(odr, sizeof(*attributes->attributes) * num_attributes); for (i = 0; i < num_attributes; i++) { HV *hv_elem = (HV*) SvRV(sv_2mortal(av_shift(sv_attributes))); Z_AttributeElement *elem; elem = (Z_AttributeElement *) odr_malloc(odr, sizeof(*elem)); attributes->attributes[i] = elem; elem->attributeSet = 0; temp = hv_fetch(hv_elem, "attributeType", 13, 1); elem->attributeType = odr_intdup(odr, SvIV(*temp)); temp = hv_fetch(hv_elem, "attributeValue", 14, 1); if (SvIOK(*temp)) { elem->which = Z_AttributeValue_numeric; elem->value.numeric = odr_intdup(odr, SvIV(*temp)); } else { STRLEN s_len; char *s_buf = SvPV(*temp, s_len); Z_ComplexAttribute *c = odr_malloc(odr, sizeof *c); elem->which = Z_AttributeValue_complex; elem->value.complex = c; c->num_list = 1; c->list = (Z_StringOrNumeric **) odr_malloc(odr, sizeof(*c->list)); c->list[0] = (Z_StringOrNumeric *) odr_malloc(odr, sizeof(**c->list)); c->list[0]->which = Z_StringOrNumeric_string; c->list[0]->u.string = odr_strdupn(odr, s_buf, s_len); c->num_semanticAction = 0; c->semanticAction = 0; } hv_undef(hv_elem); } temp = hv_fetch(facet_field_hv, "terms", 5, 0); if (!temp) { num_terms = 0; } else { sv_terms = (AV *) SvRV(*temp); if (SvTYPE(sv_terms) == SVt_PVAV) { num_terms = av_len(sv_terms) + 1; } else { num_terms = 0; } } *fl = facet_field_create(odr, attributes, num_terms); for (i = 0; i < num_terms; i++) { STRLEN s_len; char *s_buf; HV *hv_elem = (HV*) SvRV(sv_2mortal(av_shift(sv_terms))); Z_FacetTerm *facet_term = (Z_FacetTerm *) odr_malloc(odr, sizeof(*facet_term)); (*fl)->terms[i] = facet_term; temp = hv_fetch(hv_elem, "count", 5, 1); facet_term->count = odr_intdup(odr, SvIV(*temp)); temp = hv_fetch(hv_elem, "term", 4, 1); s_buf = SvPV(*temp, s_len); facet_term->term = z_Term_create(odr, Z_Term_general, s_buf, s_len); hv_undef(hv_elem); } } static void f_SV_to_FacetList(SV *sv, Z_OtherInformation **oip, ODR odr) { AV *entries = (AV *) SvRV(sv); int num_facets; if (entries && SvTYPE(entries) == SVt_PVAV && (num_facets = av_len(entries) + 1) > 0) { Z_OtherInformation *oi; Z_OtherInformationUnit *oiu; Z_FacetList *facet_list = facet_list_create(odr, num_facets); int i; for (i = 0; i < num_facets; i++) { HV *facet_field = (HV*) SvRV(sv_2mortal(av_shift(entries))); f_SV_to_FacetField(facet_field, &facet_list->elements[i], odr); hv_undef(facet_field); } oi = odr_malloc(odr, sizeof(*oi)); oiu = odr_malloc(odr, sizeof(*oiu)); oi->num_elements = 1; oi->list = odr_malloc(odr, oi->num_elements * sizeof(*oi->list)); oiu->category = 0; oiu->which = Z_OtherInfo_externallyDefinedInfo; oiu->information.externallyDefinedInfo = odr_malloc(odr, sizeof(*oiu->information.externallyDefinedInfo)); oiu->information.externallyDefinedInfo->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1); oiu->information.externallyDefinedInfo->descriptor = 0; oiu->information.externallyDefinedInfo->indirect_reference = 0; oiu->information.externallyDefinedInfo->which = Z_External_userFacets; oiu->information.externallyDefinedInfo->u.facetList = facet_list; oi->list[0] = oiu; *oip = oi; } } static HV *parse_extra_args(Z_SRW_extra_arg *args) { HV *href = newHV(); for (; args; args = args->next) { hv_store(href, args->name, strlen(args->name), newSVpv(args->value, 0), 0); } return href; } int bend_search(void *handle, bend_search_rr *rr) { HV *href; AV *aref; SV **temp; int i; char **basenames; WRBUF query; SV *point; Zfront_handle *zhandle = (Zfront_handle *)handle; CV* handler_cv = 0; SV *rpnSV; SV *facetSV; char *ptr; STRLEN len; dSP; ENTER; SAVETMPS; aref = newAV(); basenames = rr->basenames; for (i = 0; i < rr->num_bases; i++) { av_push(aref, newSVpv(*basenames++, 0)); } #if ENABLE_STOP_SERVER if (rr->num_bases == 1 && !strcmp(rr->basenames[0], "XXstop")) { zhandle->stop_flag = 1; } #endif href = newHV(); hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0); if (rr->srw_sortKeys && *rr->srw_sortKeys) hv_store(href, "SRW_SORTKEYS", 12, newSVpv(rr->srw_sortKeys, 0), 0); hv_store(href, "REPL_SET", 8, newSViv(rr->replace_set), 0); hv_store(href, "ERR_CODE", 8, newSViv(0), 0); hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0); hv_store(href, "HITS", 4, newSViv(0), 0); hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0); hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0); hv_store(href, "HANDLE", 6, zhandle->handle, 0); hv_store(href, "PID", 3, newSViv(getpid()), 0); hv_store(href, "PRESENT_NUMBER", 14, newSViv(rr->present_number), 0); hv_store(href, "EXTRA_ARGS", 10, newRV( (SV*) parse_extra_args(rr->extra_args)), 0); if ((rpnSV = zquery2perl(rr->query)) != 0) { hv_store(href, "RPN", 3, rpnSV, 0); } facetSV = f_FacetList_to_SV(yaz_oi_get_facetlist(&rr->search_input)); if (facetSV) { hv_store(href, "INPUTFACETS", 11, facetSV, 0); } query = zquery2pquery(rr->query); if (query) { hv_store(href, "QUERY", 5, newSVpv((char *)query->buf, query->pos), 0); } else if (rr->query->which == Z_Query_type_104 && rr->query->u.type_104->which == Z_External_CQL) { hv_store(href, "CQL", 3, newSVpv(rr->query->u.type_104->u.cql, 0), 0); } else { rr->errcode = 108; return 0; } PUSHMARK(sp); XPUSHs(sv_2mortal(newRV( (SV*) href))); PUTBACK; handler_cv = simpleserver_sv2cv( search_ref ); perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD); SPAGAIN; temp = hv_fetch(href, "HITS", 4, 1); rr->hits = SvIV(*temp); temp = hv_fetch(href, "ERR_CODE", 8, 1); rr->errcode = SvIV(*temp); temp = hv_fetch(href, "ERR_STR", 7, 1); rr->errstring = string_or_undef(temp, rr->stream); temp = hv_fetch(href, "HANDLE", 6, 1); point = newSVsv(*temp); temp = hv_fetch(href, "OUTPUTFACETS", 12, 1); if (SvTYPE(*temp) != SVt_NULL) f_SV_to_FacetList(*temp, &rr->search_info, rr->stream); temp = hv_fetch(href, "EXTRA_RESPONSE_DATA", 19, 0); if (temp) { ptr = SvPV(*temp, len); rr->extra_response_data = odr_strdupn(rr->stream, ptr, len); } temp = hv_fetch(href, "ESTIMATED" "_HIT_" "COUNT", 19, 0); if (temp) { rr->estimated_hit_count = SvIV(*temp); } hv_undef(href); av_undef(aref); zhandle->handle = point; sv_free( (SV*) aref); sv_free( (SV*) href); if (query) wrbuf_destroy(query); PUTBACK; FREETMPS; LEAVE; return 0; } /* ### I am not 100% about the memory management in this handler */ int bend_delete(void *handle, bend_delete_rr *rr) { Zfront_handle *zhandle = (Zfront_handle *)handle; HV *href; CV* handler_cv; int i; SV **temp; SV *point; dSP; ENTER; SAVETMPS; href = newHV(); hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0); hv_store(href, "HANDLE", 6, zhandle->handle, 0); hv_store(href, "STATUS", 6, newSViv(0), 0); PUSHMARK(sp); XPUSHs(sv_2mortal(newRV( (SV*) href))); PUTBACK; handler_cv = simpleserver_sv2cv(delete_ref); if (rr->function == 1) { /* Delete all result sets in the session */ perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD); temp = hv_fetch(href, "STATUS", 6, 1); rr->delete_status = SvIV(*temp); } else { rr->delete_status = 0; /* * For some reason, deleting two or more result-sets in * one operation goes horribly wrong, and ### I don't have * time to debug it right now. */ if (rr->num_setnames > 1) { rr->delete_status = 3; /* "System problem at target" */ /* There's no way to sent delete-msg using the GFS */ return 0; } for (i = 0; i < rr->num_setnames; i++) { hv_store(href, "SETNAME", 7, newSVpv(rr->setnames[i], 0), 0); perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD); temp = hv_fetch(href, "STATUS", 6, 1); rr->statuses[i] = SvIV(*temp); if (rr->statuses[i] != 0) rr->delete_status = rr->statuses[i]; } } SPAGAIN; temp = hv_fetch(href, "HANDLE", 6, 1); point = newSVsv(*temp); hv_undef(href); zhandle->handle = point; sv_free( (SV*) href); PUTBACK; FREETMPS; LEAVE; return 0; } int bend_fetch(void *handle, bend_fetch_rr *rr) { HV *href; SV **temp; SV *basename; SV *last; SV *err_code; SV *err_string; SV *sur_flag; SV *point; SV *rep_form; SV *schema = 0; char *ptr; WRBUF oid_dotted; Zfront_handle *zhandle = (Zfront_handle *)handle; CV* handler_cv = 0; Z_RecordComposition *composition; Z_ElementSetNames *simple; Z_CompSpec *complex; STRLEN length; dSP; ENTER; SAVETMPS; rr->errcode = 0; href = newHV(); hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0); if (rr->schema) hv_store(href, "SCHEMA", 6, newSVpv(rr->schema, 0), 0); else hv_store(href, "SCHEMA", 6, newSVpv("", 0), 0); temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0); if (rr->request_format != 0) { oid_dotted = oid2dotted(rr->request_format); } else { /* Probably an SRU request: assume XML is required */ oid_dotted = wrbuf_alloc(); wrbuf_puts(oid_dotted, "1.2.840.10003.5.109.10"); } hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0); hv_store(href, "REP_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0); hv_store(href, "BASENAME", 8, newSVpv("", 0), 0); hv_store(href, "LAST", 4, newSViv(0), 0); hv_store(href, "ERR_CODE", 8, newSViv(0), 0); hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0); hv_store(href, "SUR_FLAG", 8, newSViv(0), 0); hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0); hv_store(href, "HANDLE", 6, zhandle->handle, 0); hv_store(href, "PID", 3, newSViv(getpid()), 0); if (rr->comp) { composition = rr->comp; if (composition->which == Z_RecordComp_simple) { simple = composition->u.simple; if (simple->which == Z_ElementSetNames_generic) { hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0); } else { rr->errcode = 26; rr->errstring = odr_strdup(rr->stream, "non-generic 'simple' composition"); return 0; } } else if (composition->which == Z_RecordComp_complex) { if (composition->u.complex->generic && composition->u.complex->generic && composition->u.complex->generic->elementSpec && composition->u.complex->generic->elementSpec->which == Z_ElementSpec_elementSetName) { complex = composition->u.complex; hv_store(href, "COMP", 4, newSVpv(complex->generic->elementSpec->u.elementSetName, 0), 0); } else { #if 0 /* For now ignore this error, which is ubiquitous in SRU */ rr->errcode = 26; rr->errstring = odr_strdup(rr->stream, "'complex' composition is not generic ESN"); return 0; #endif /*0*/ } } else { rr->errcode = 26; rr->errstring = odr_strdup(rr->stream, "composition neither simple nor complex"); return 0; } } PUSHMARK(sp); XPUSHs(sv_2mortal(newRV( (SV*) href))); PUTBACK; handler_cv = simpleserver_sv2cv( fetch_ref ); perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD); SPAGAIN; temp = hv_fetch(href, "BASENAME", 8, 1); basename = newSVsv(*temp); temp = hv_fetch(href, "LAST", 4, 1); last = newSVsv(*temp); temp = hv_fetch(href, "ERR_CODE", 8, 1); err_code = newSVsv(*temp); temp = hv_fetch(href, "ERR_STR", 7, 1), err_string = newSVsv(*temp); temp = hv_fetch(href, "SUR_FLAG", 8, 1); sur_flag = newSVsv(*temp); temp = hv_fetch(href, "REP_FORM", 8, 1); rep_form = newSVsv(*temp); temp = hv_fetch(href, "SCHEMA", 6, 0); if (temp != 0) { schema = newSVsv(*temp); ptr = SvPV(schema, length); if (length > 0) rr->schema = odr_strdupn(rr->stream, ptr, length); } temp = hv_fetch(href, "HANDLE", 6, 1); point = newSVsv(*temp); ptr = SvPV(basename, length); rr->basename = odr_strdupn(rr->stream, ptr, length); ptr = SvPV(rep_form, length); rr->output_format = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_RECSYN, ptr, rr->stream); if (!rr->output_format) { printf("Net::Z3950::SimpleServer: WARNING: Bad OID %s\n", ptr); rr->output_format = odr_oiddup(rr->stream, yaz_oid_recsyn_sutrs); } temp = hv_fetch(href, "RECORD", 6, 0); if (temp) { SV *record = newSVsv(*temp); ptr = SvPV(record, length); /* Treat GRS-1 records separately */ if (!oid_oidcmp(rr->output_format, yaz_oid_recsyn_grs_1)) { rr->record = (char *) read_grs1(ptr, rr->stream); rr->len = -1; } else { rr->record = odr_strdupn(rr->stream, ptr, length); rr->len = length; } sv_free(record); } hv_undef(href); zhandle->handle = point; handle = zhandle; rr->last_in_set = SvIV(last); if (!(rr->errcode)) { rr->errcode = SvIV(err_code); ptr = SvPV(err_string, length); rr->errstring = odr_strdupn(rr->stream, ptr, length); } rr->surrogate_flag = SvIV(sur_flag); wrbuf_destroy(oid_dotted); sv_free((SV*) href); sv_free(basename); sv_free(last); sv_free(err_string); sv_free(err_code), sv_free(sur_flag); sv_free(rep_form); if (schema) sv_free(schema); PUTBACK; FREETMPS; LEAVE; return 0; } int bend_present(void *handle, bend_present_rr *rr) { HV *href; SV **temp; SV *err_code; SV *err_string; SV *point; STRLEN len; Z_RecordComposition *composition; Z_ElementSetNames *simple; Z_CompSpec *complex; char *ptr; Zfront_handle *zhandle = (Zfront_handle *)handle; CV* handler_cv = 0; /* WRBUF oid_dotted; */ dSP; ENTER; SAVETMPS; href = newHV(); hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0); hv_store(href, "HANDLE", 6, zhandle->handle, 0); hv_store(href, "ERR_CODE", 8, newSViv(0), 0); hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0); hv_store(href, "START", 5, newSViv(rr->start), 0); hv_store(href, "SETNAME", 7, newSVpv(rr->setname, 0), 0); hv_store(href, "NUMBER", 6, newSViv(rr->number), 0); /*oid_dotted = oid2dotted(rr->request_format_raw); hv_store(href, "REQ_FORM", 8, newSVpv((char *)oid_dotted->buf, oid_dotted->pos), 0);*/ hv_store(href, "PID", 3, newSViv(getpid()), 0); if (rr->comp) { composition = rr->comp; if (composition->which == Z_RecordComp_simple) { simple = composition->u.simple; if (simple->which == Z_ElementSetNames_generic) { hv_store(href, "COMP", 4, newSVpv(simple->u.generic, 0), 0); } else { rr->errcode = 26; rr->errstring = odr_strdup(rr->stream, "non-generic 'simple' composition"); return 0; } } else if (composition->which == Z_RecordComp_complex) { if (composition->u.complex->generic && composition->u.complex->generic && composition->u.complex->generic->elementSpec && composition->u.complex->generic->elementSpec->which == Z_ElementSpec_elementSetName) { complex = composition->u.complex; hv_store(href, "COMP", 4, newSVpv(complex->generic->elementSpec->u.elementSetName, 0), 0); } else { rr->errcode = 26; rr->errstring = odr_strdup(rr->stream, "'complex' composition is not generic ESN"); return 0; } } else { rr->errcode = 26; rr->errstring = odr_strdup(rr->stream, "composition neither simple nor complex"); return 0; } } PUSHMARK(sp); XPUSHs(sv_2mortal(newRV( (SV*) href))); PUTBACK; handler_cv = simpleserver_sv2cv( present_ref ); perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD); SPAGAIN; temp = hv_fetch(href, "ERR_CODE", 8, 1); err_code = newSVsv(*temp); temp = hv_fetch(href, "ERR_STR", 7, 1); err_string = newSVsv(*temp); temp = hv_fetch(href, "HANDLE", 6, 1); point = newSVsv(*temp); PUTBACK; FREETMPS; LEAVE; hv_undef(href); rr->errcode = SvIV(err_code); ptr = SvPV(err_string, len); rr->errstring = odr_strdupn(rr->stream, ptr, len); /* wrbuf_free(oid_dotted, 1);*/ zhandle->handle = point; handle = zhandle; sv_free(err_code); sv_free(err_string); sv_free( (SV*) href); return 0; } int bend_esrequest(void *handle, bend_esrequest_rr *rr) { perl_call_sv(esrequest_ref, G_VOID | G_DISCARD | G_NOARGS); return 0; } int bend_scan(void *handle, bend_scan_rr *rr) { HV *href; AV *aref; AV *list; AV *entries; HV *scan_item; struct scan_entry *buffer; int *step_size = rr->step_size; int scan_list_size = rr->num_entries; int i; char **basenames; SV **temp; SV *err_code = sv_newmortal(); SV *err_str = sv_newmortal(); SV *point = sv_newmortal(); SV *status = sv_newmortal(); SV *number = sv_newmortal(); char *ptr; STRLEN len; SV *entries_ref; Zfront_handle *zhandle = (Zfront_handle *)handle; CV* handler_cv = 0; SV *rpnSV; dSP; ENTER; SAVETMPS; href = newHV(); list = newAV(); /* RPN is better than TERM since it includes attributes */ if ((rpnSV = f_Term_to_SV(rr->term->term, rr->term->attributes)) != 0) { setMember(href, "RPN", rpnSV); } if (rr->term->term->which == Z_Term_general) { Odr_oct *oterm = rr->term->term->u.general; hv_store(href, "TERM", 4, newSVpv((char*) oterm->buf, oterm->len), 0); } else { rr->errcode = 229; /* Unsupported term type */ return 0; } hv_store(href, "STEP", 4, newSViv(*step_size), 0); hv_store(href, "NUMBER", 6, newSViv(rr->num_entries), 0); hv_store(href, "POS", 3, newSViv(rr->term_position), 0); hv_store(href, "ERR_CODE", 8, newSViv(0), 0); hv_store(href, "ERR_STR", 7, newSVpv("", 0), 0); hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0); hv_store(href, "HANDLE", 6, zhandle->handle, 0); hv_store(href, "STATUS", 6, newSViv(BEND_SCAN_SUCCESS), 0); hv_store(href, "ENTRIES", 7, newRV((SV *) list), 0); hv_store(href, "EXTRA_ARGS", 10, newRV( (SV*) parse_extra_args(rr->extra_args)), 0); aref = newAV(); basenames = rr->basenames; for (i = 0; i < rr->num_bases; i++) { av_push(aref, newSVpv(*basenames++, 0)); } hv_store(href, "DATABASES", 9, newRV( (SV*) aref), 0); PUSHMARK(sp); XPUSHs(sv_2mortal(newRV( (SV*) href))); PUTBACK; handler_cv = simpleserver_sv2cv( scan_ref ); perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD); SPAGAIN; temp = hv_fetch(href, "ERR_CODE", 8, 1); err_code = newSVsv(*temp); temp = hv_fetch(href, "ERR_STR", 7, 1); err_str = newSVsv(*temp); temp = hv_fetch(href, "HANDLE", 6, 1); point = newSVsv(*temp); temp = hv_fetch(href, "STATUS", 6, 1); status = newSVsv(*temp); temp = hv_fetch(href, "NUMBER", 6, 1); number = newSVsv(*temp); temp = hv_fetch(href, "ENTRIES", 7, 1); entries_ref = newSVsv(*temp); temp = hv_fetch(href, "EXTRA_RESPONSE_DATA", 19, 0); if (temp) { ptr = SvPV(*temp, len); rr->extra_response_data = odr_strdupn(rr->stream, ptr, len); } PUTBACK; FREETMPS; LEAVE; ptr = SvPV(err_str, len); rr->errstring = odr_strdupn(rr->stream, ptr, len); rr->errcode = SvIV(err_code); rr->num_entries = SvIV(number); rr->status = SvIV(status); buffer = rr->entries; entries = (AV *)SvRV(entries_ref); if (rr->errcode == 0) for (i = 0; i < rr->num_entries; i++) { scan_item = (HV *)SvRV(sv_2mortal(av_shift(entries))); temp = hv_fetch(scan_item, "TERM", 4, 1); ptr = SvPV(*temp, len); buffer->term = odr_strdupn(rr->stream, ptr, len); temp = hv_fetch(scan_item, "OCCURRENCE", 10, 1); buffer->occurrences = SvIV(*temp); temp = hv_fetch(scan_item, "DISPLAY_TERM", 12, 0); if (temp) { ptr = SvPV(*temp, len); buffer->display_term = odr_strdupn(rr->stream, ptr,len); } buffer++; hv_undef(scan_item); } zhandle->handle = point; handle = zhandle; sv_free(err_code); sv_free(err_str); sv_free(status); sv_free(number); hv_undef(href); sv_free((SV *)href); av_undef(aref); sv_free((SV *)aref); av_undef(list); sv_free((SV *)list); av_undef(entries); /*sv_free((SV *)entries);*/ sv_free(entries_ref); return 0; } int bend_explain(void *handle, bend_explain_rr *q) { HV *href; CV *handler_cv = 0; SV **temp; char *explain; SV *explainsv; STRLEN len; Zfront_handle *zhandle = (Zfront_handle *)handle; dSP; ENTER; SAVETMPS; href = newHV(); hv_store(href, "EXPLAIN", 7, newSVpv("", 0), 0); hv_store(href, "DATABASE", 8, newSVpv(q->database, 0), 0); hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0); hv_store(href, "HANDLE", 6, zhandle->handle, 0); PUSHMARK(sp); XPUSHs(sv_2mortal(newRV((SV*) href))); PUTBACK; handler_cv = simpleserver_sv2cv(explain_ref); perl_call_sv((SV*) handler_cv, G_SCALAR | G_DISCARD); SPAGAIN; temp = hv_fetch(href, "EXPLAIN", 7, 1); explainsv = newSVsv(*temp); PUTBACK; FREETMPS; LEAVE; explain = SvPV(explainsv, len); q->explain_buf = odr_strdupn(q->stream, explain, len); return 0; } /* * You'll laugh when I tell you this ... Astonishingly, it turns out * that ActivePerl (which is widely used on Windows) has, in the * header file Perl\lib\CORE\XSUB.h, the following heinous crime: * # define open PerlLIO_open * This of course screws up the use of the "open" member of the * Z_IdAuthentication structure below, so we have to undo this * brain-damage. */ #ifdef open #undef open #endif bend_initresult *bend_init(bend_initrequest *q) { int dummy = simpleserver_clone(); bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r)); char *ptr; CV* handler_cv = 0; dSP; STRLEN len; NMEM nmem = nmem_create(); Zfront_handle *zhandle = (Zfront_handle *) nmem_malloc(nmem, sizeof(*zhandle)); SV *handle; HV *href; SV **temp; ENTER; SAVETMPS; zhandle->ghandle = _global_ghandle; zhandle->nmem = nmem; zhandle->stop_flag = 0; if (sort_ref) { q->bend_sort = bend_sort; } if (search_ref) { q->bend_search = bend_search; } if (present_ref) { q->bend_present = bend_present; } /*q->bend_esrequest = bend_esrequest;*/ if (delete_ref) { q->bend_delete = bend_delete; } if (fetch_ref) { q->bend_fetch = bend_fetch; } if (scan_ref) { q->bend_scan = bend_scan; } if (explain_ref) { q->bend_explain = bend_explain; } href = newHV(); /* ### These should be given initial values from the client */ hv_store(href, "IMP_ID", 6, newSVpv("", 0), 0); hv_store(href, "IMP_NAME", 8, newSVpv("", 0), 0); hv_store(href, "IMP_VER", 7, newSVpv("", 0), 0); hv_store(href, "ERR_CODE", 8, newSViv(0), 0); hv_store(href, "ERR_STR", 7, newSViv(0), 0); hv_store(href, "PEER_NAME", 9, newSVpv(q->peer_name, 0), 0); hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0); hv_store(href, "HANDLE", 6, newSVsv(&sv_undef), 0); hv_store(href, "PID", 3, newSViv(getpid()), 0); if (q->auth) { char *user = NULL; char *passwd = NULL; if (q->auth->which == Z_IdAuthentication_open) { char *cp; user = nmem_strdup (odr_getmem (q->stream), q->auth->u.open); cp = strchr (user, '/'); if (cp) { /* password after / given */ *cp = '\0'; passwd = cp+1; } } else if (q->auth->which == Z_IdAuthentication_idPass) { user = q->auth->u.idPass->userId; passwd = q->auth->u.idPass->password; } /* ### some code paths have user/password unassigned here */ if (user) hv_store(href, "USER", 4, newSVpv(user, 0), 0); if (passwd) hv_store(href, "PASS", 4, newSVpv(passwd, 0), 0); } PUSHMARK(sp); XPUSHs(sv_2mortal(newRV((SV*) href))); PUTBACK; if (init_ref != NULL) { handler_cv = simpleserver_sv2cv( init_ref ); perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD); } SPAGAIN; temp = hv_fetch(href, "IMP_ID", 6, 1); ptr = SvPV(*temp, len); q->implementation_id = nmem_strdup(nmem, ptr); temp = hv_fetch(href, "IMP_NAME", 8, 1); ptr = SvPV(*temp, len); q->implementation_name = nmem_strdup(nmem, ptr); temp = hv_fetch(href, "IMP_VER", 7, 1); ptr = SvPV(*temp, len); q->implementation_version = nmem_strdup(nmem, ptr); temp = hv_fetch(href, "ERR_CODE", 8, 1); r->errcode = SvIV(*temp); temp = hv_fetch(href, "ERR_STR", 7, 1); ptr = SvPV(*temp, len); r->errstring = odr_strdupn(q->stream, ptr, len); temp = hv_fetch(href, "HANDLE", 6, 1); handle= newSVsv(*temp); zhandle->handle = handle; r->handle = zhandle; hv_undef(href); sv_free((SV*) href); PUTBACK; FREETMPS; LEAVE; return r; } void bend_close(void *handle) { HV *href; Zfront_handle *zhandle = (Zfront_handle *)handle; CV* handler_cv = 0; int stop_flag = 0; dSP; ENTER; SAVETMPS; if (close_ref) { href = newHV(); hv_store(href, "GHANDLE", 7, newSVsv(zhandle->ghandle), 0); hv_store(href, "HANDLE", 6, zhandle->handle, 0); PUSHMARK(sp); XPUSHs(sv_2mortal(newRV((SV *)href))); PUTBACK; handler_cv = simpleserver_sv2cv( close_ref ); perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD); SPAGAIN; sv_free((SV*) href); } else sv_free(zhandle->handle); PUTBACK; FREETMPS; LEAVE; stop_flag = zhandle->stop_flag; nmem_destroy(zhandle->nmem); simpleserver_free(); if (stop_flag) exit(0); return; } static void start_stop(struct statserv_options_block *sob, SV *handler_ref) { HV *href; dSP; ENTER; SAVETMPS; href = newHV(); hv_store(href, "CONFIG", 6, newSVpv(sob->configname, 0), 0); PUSHMARK(sp); XPUSHs(sv_2mortal(newRV((SV*) href))); PUTBACK; if (handler_ref != NULL) { CV* handler_cv = simpleserver_sv2cv( handler_ref ); perl_call_sv( (SV *) handler_cv, G_SCALAR | G_DISCARD); } SPAGAIN; PUTBACK; FREETMPS; LEAVE; } void bend_start(struct statserv_options_block *sob) { start_stop(sob, start_ref); } MODULE = Net::Z3950::SimpleServer PACKAGE = Net::Z3950::SimpleServer PROTOTYPES: DISABLE void set_ghandle(arg) SV *arg CODE: _global_ghandle = newSVsv(arg); void set_init_handler(arg) SV *arg CODE: init_ref = newSVsv(arg); void set_close_handler(arg) SV *arg CODE: close_ref = newSVsv(arg); void set_sort_handler(arg) SV *arg CODE: sort_ref = newSVsv(arg); void set_search_handler(arg) SV *arg CODE: search_ref = newSVsv(arg); void set_fetch_handler(arg) SV *arg CODE: fetch_ref = newSVsv(arg); void set_present_handler(arg) SV *arg CODE: present_ref = newSVsv(arg); void set_esrequest_handler(arg) SV *arg CODE: esrequest_ref = newSVsv(arg); void set_delete_handler(arg) SV *arg CODE: delete_ref = newSVsv(arg); void set_scan_handler(arg) SV *arg CODE: scan_ref = newSVsv(arg); void set_explain_handler(arg) SV *arg CODE: explain_ref = newSVsv(arg); void set_start_handler(arg) SV *arg CODE: start_ref = newSVsv(arg); int start_server(...) PREINIT: char **argv; char **argv_buf; char *ptr; int i; STRLEN len; struct statserv_options_block *sob; CODE: argv_buf = (char **)xmalloc((items + 1) * sizeof(char *)); argv = argv_buf; for (i = 0; i < items; i++) { ptr = SvPV(ST(i), len); *argv_buf = (char *)xmalloc(len + 1); strcpy(*argv_buf++, ptr); } *argv_buf = NULL; sob = statserv_getcontrol(); sob->bend_start = bend_start; statserv_setcontrol(sob); root_perl_context = PERL_GET_CONTEXT; yaz_mutex_create(&simpleserver_mutex); #if 0 /* only for debugging perl_clone .. */ tst_clones(); #endif RETVAL = statserv_main(items, argv, bend_init, bend_close); OUTPUT: RETVAL int ScanSuccess() CODE: RETVAL = BEND_SCAN_SUCCESS; OUTPUT: RETVAL int ScanPartial() CODE: RETVAL = BEND_SCAN_PARTIAL; OUTPUT: RETVAL void yazlog(arg) SV *arg CODE: STRLEN len; char *ptr; ptr = SvPV(arg, len); yaz_log(YLOG_LOG, "%.*s", (int) len, ptr); int yaz_diag_srw_to_bib1(srw_code) int srw_code int yaz_diag_bib1_to_srw(bib1_code) int bib1_code Net-Z3950-SimpleServer-1.21/test.pl0000644000175000017500000000552412470353007015310 0ustar mikemike# Before `make install' is performed this script should be runnable with # `make test'. After `make install' it should work as `perl test.pl' ######################### We start with some black magic to print on failure. # Change 1..1 below to 1..last_test_to_print . # (It may become useful if the test is moved to ./t subdirectory.) BEGIN { $| = 1; print "1..4\n"; } END {print "not ok 1\n" unless $loaded;} use Net::Z3950::SimpleServer; $loaded = 1; print "ok 1\n"; ######################### End of black magic. print "not " if Net::Z3950::SimpleServer::yaz_diag_srw_to_bib1(11) != 107; print "ok 2\n"; print "not " if Net::Z3950::SimpleServer::yaz_diag_bib1_to_srw(3) != 48; print "ok 3\n"; # Insert your test code below (better if it prints "ok 13" # (correspondingly "not ok 13") depending on the success of chunk 13 # of the test code): sub my_init_handler { my $href = shift; my %log = (); $log{"init"} = "Ok"; $href->{HANDLE} = \%log; } sub my_search_handler { my $href = shift; my %log = %{$href->{HANDLE}}; $log{"search"} = "Ok"; $href->{HANDLE} = \%log; $href->{HITS} = 1; } sub my_fetch_handler { my $href = shift; my %log = %{$href->{HANDLE}}; my $record = "HeadlineI am a record"; $log{"fetch"} = "Ok"; $href->{HANDLE} = \%log; $href->{RECORD} = $record; $href->{LEN} = length($record); $href->{NUMBER} = 1; $href->{BASENAME} = "Test"; } sub my_close_handler { my @services = ("init", "search", "fetch", "close"); my $href = shift; my %log = %{$href->{HANDLE}}; my $status; my $service; my $error = 0; $log{"close"} = "Ok"; print "\n-----------------------------------------------\n"; print "Available Z39.50 services:\n\n"; foreach $service (@services) { print "Called $service: "; if (defined($status = $log{$service})) { print "$status\n"; } else { print "FAILED!!!\n"; $error = 1; } } if ($error) { print "make test: Failed due to lack of required Z39.50 service\n"; } else { print "\nEverything is ok!\n"; } print "-----------------------------------------------\n"; print "not " if $error; print "ok 4\n"; } my $socketFile = "/tmp/SimpleServer-test-$$"; my $socket = "unix:$socketFile"; if (!defined($pid = fork() )) { die "Cannot fork: $!\n"; } elsif ($pid) { ## Parent launches server my $handler = Net::Z3950::SimpleServer->new( INIT => \&my_init_handler, CLOSE => \&my_close_handler, SEARCH => \&my_search_handler, FETCH => \&my_fetch_handler); $handler->launch_server("test.pl", "-1", $socket); } else { ## Child starts the client sleep(1); open(CLIENT, "| yaz-client $socket > /dev/null") or die "Couldn't fork client: $!\n"; print CLIENT "f test\n"; print CLIENT "s\n"; print CLIENT "close\n"; print CLIENT "quit\n"; close(CLIENT) or die "Couldn't close: $!\n"; unlink($socketFile); } Net-Z3950-SimpleServer-1.21/logging-server.pl0000755000175000017500000000212111620205472017252 0ustar mikemike#!/usr/bin/perl -w # This is just about the simplest possible SimpleServer-based Z39.50 # server. It exists only to log the data-structures that are handed # to the back-end functions, and does only enough work otherwise to # hand the client a coherent (if useless) response to its requests. use strict; use warnings; use Net::Z3950::SimpleServer; use Data::Dumper; my $handler = new Net::Z3950::SimpleServer(INIT => \&init_handler, CLOSE => \&close_handler, SEARCH => \&search_handler, FETCH => \&fetch_handler); $handler->launch_server("logging-server.pl", @ARGV); sub init_handler { my $href = shift; print "INIT: ", Dumper($href); } sub search_handler { my $href = shift; print "Search: ", Dumper($href); $href->{HITS} = 1; } sub fetch_handler { my $href = shift; print "Fetch: ", Dumper($href); my $record = "foo"; $href->{RECORD} = $record; $href->{LEN} = length($record); $href->{NUMBER} = 1; $href->{BASENAME} = "Test"; } sub close_handler { my $href = shift; print "Close: ", Dumper($href); } Net-Z3950-SimpleServer-1.21/META.json0000664000175000017500000000153512772451217015424 0ustar mikemike{ "abstract" : "unknown", "author" : [ "unknown" ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.0401, CPAN::Meta::Converter version 2.150005", "license" : [ "unknown" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Net-Z3950-SimpleServer", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : {} } }, "release_status" : "stable", "version" : "1.21", "x_serialization_backend" : "JSON::PP version 2.27300" } Net-Z3950-SimpleServer-1.21/GRS1.pm0000644000175000017500000002423312470353007015044 0ustar mikemike## This file is part of simpleserver ## Copyright (C) 2000-2015 Index Data. ## All rights reserved. ## Redistribution and use in source and binary forms, with or without ## modification, are permitted provided that the following conditions are met: ## ## * Redistributions of source code must retain the above copyright ## notice, this list of conditions and the following disclaimer. ## * Redistributions in binary form must reproduce the above copyright ## notice, this list of conditions and the following disclaimer in the ## documentation and/or other materials provided with the distribution. ## * Neither the name of Index Data nor the names of its contributors ## may be used to endorse or promote products derived from this ## software without specific prior written permission. ## ## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY ## EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ## DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY ## DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ## (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF ## THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package Net::Z3950::GRS1; use strict; use IO::Handle; use Carp; sub new { my ($class, $href, $map) = @_; my $self = {}; $self->{ELEMENTS} = []; $self->{FH} = *STDOUT; ## Default output handle is STDOUT $self->{MAP} = $map; bless $self, $class; if (defined($href) && ref($href) eq 'HASH') { if (!defined($map)) { croak 'Usage: new Net::Z3950::GRS1($href, $map);'; } $self->Hash2grs($href, $map); } return $self; } sub Hash2grs { my ($self, $href, $mapping) = @_; my $key; my $content; my $aref; my $issue; $mapping = defined($mapping) ? $mapping : $self->{MAP}; $self->{MAP} = $mapping; foreach $key (keys %$href) { $content = $href->{$key}; next unless defined($content); if (!defined($aref = $mapping->{$key})) { print STDERR "Hash2grs: Unmapped key: '$key'\n"; next; } if (ref($content) eq 'HASH') { ## Subtree? my $subtree = new Net::Z3950::GRS1($content, $mapping); $self->AddElement($aref->[0], $aref->[1], &Net::Z3950::GRS1::ElementData::Subtree, $subtree); } elsif (!ref($content)) { ## Regular string? $self->AddElement($aref->[0], $aref->[1], &Net::Z3950::GRS1::ElementData::String, $content); } elsif (ref($content) eq 'ARRAY') { my $issues = new Net::Z3950::GRS1; foreach $issue (@$content) { my $entry = new Net::Z3950::GRS1($issue, $mapping); $issues->AddElement(5, 1, &Net::Z3950::GRS1::ElementData::Subtree, $entry); } $self->AddElement($aref->[0], $aref->[1], &Net::Z3950::GRS1::ElementData::Subtree, $issues); } else { print STDERR "Hash2grs: Unsupported content type\n"; next; } } } sub GetElementList { my $self = shift; return $self->{ELEMENTS}; } sub CreateTaggedElement { my ($self, $type, $value, $element_data) = @_; my $tagged = {}; $tagged->{TYPE} = $type; $tagged->{VALUE} = $value; $tagged->{OCCURANCE} = undef; $tagged->{META} = undef; $tagged->{VARIANT} = undef; $tagged->{ELEMENTDATA} = $element_data; return $tagged; } sub GetTypeValue { my ($self, $TaggedElement) = @_; return ($TaggedElement->{TYPE}, $TaggedElement->{VALUE}); } sub GetElementData { my ($self, $TaggedElement) = @_; return $TaggedElement->{ELEMENTDATA}; } sub CheckTypes { my ($self, $which, $content) = @_; if ($which == &Net::Z3950::GRS1::ElementData::String) { if (ref($content) eq '') { return 1; } else { croak "Wrong content type, expected a scalar"; } } elsif ($which == &Net::Z3950::GRS1::ElementData::Subtree) { if (ref($content) eq __PACKAGE__) { return 1; } else { croak "Wrong content type, expected a blessed reference"; } } else { croak "Content type currently not supported"; } } sub CreateElementData { my ($self, $which, $content) = @_; my $ElementData = {}; $self->CheckTypes($which, $content); $ElementData->{WHICH} = $which; $ElementData->{CONTENT} = $content; return $ElementData; } sub AddElement { my ($self, $type, $value, $which, $content) = @_; my $Elements = $self->GetElementList; my $ElmData = $self->CreateElementData($which, $content); my $TaggedElm = $self->CreateTaggedElement($type, $value, $ElmData); push(@$Elements, $TaggedElm); } sub _Indent { my ($self, $level) = @_; my $space = ""; foreach (1..$level - 1) { $space .= " "; } return $space; } sub _RecordLine { my ($self, $level, $pool, @args) = @_; my $fh = $self->{FH}; my $str = sprintf($self->_Indent($level) . shift(@args), @args); print $fh $str; if (defined($pool)) { $$pool .= $str; } } sub Render { my $self = shift; my %args = ( FORMAT => &Net::Z3950::GRS1::Render::Plain, FILE => '/dev/null', LEVEL => 0, HANDLE => undef, POOL => undef, @_ ); my @Elements = @{$self->GetElementList}; my $TaggedElement; my $fh = $args{HANDLE}; my $level = ++$args{LEVEL}; my $ref = $args{POOL}; if (!defined($fh) && defined($args{FILE})) { open(FH, '> ' . $args{FILE}) or croak "Render: Unable to open file '$args{FILE}' for writing: $!"; FH->autoflush(1); $fh = *FH; } $self->{FH} = defined($fh) ? $fh : $self->{FH}; $args{HANDLE} = $fh; foreach $TaggedElement (@Elements) { my ($type, $value) = $self->GetTypeValue($TaggedElement); if ($self->GetElementData($TaggedElement)->{WHICH} == &Net::Z3950::GRS1::ElementData::String) { $self->_RecordLine($level, $ref, "(%s,%s) %s\n", $type, $value, $self->GetElementData($TaggedElement)->{CONTENT}); } elsif ($self->GetElementData($TaggedElement)->{WHICH} == &Net::Z3950::GRS1::ElementData::Subtree) { $self->_RecordLine($level, $ref, "(%s,%s) {\n", $type, $value); $self->GetElementData($TaggedElement)->{CONTENT}->Render(%args); $self->_RecordLine($level, $ref, "}\n"); } } if ($level == 1) { $self->_RecordLine($level, $ref, "(0,0)\n"); } } package Net::Z3950::GRS1::ElementData; ## Define some constants according to the GRS-1 specification sub Octets { 1 } sub Numeric { 2 } sub Date { 3 } sub Ext { 4 } sub String { 5 } sub TrueOrFalse { 6 } sub OID { 7 } sub IntUnit { 8 } sub ElementNotThere { 9 } sub ElementEmpty { 10 } sub NoDataRequested { 11 } sub Diagnostic { 12 } sub Subtree { 13 } package Net::Z3950::GRS1::Render; ## Define various types of rendering formats sub Plain { 1 } sub XML { 2 } sub Raw { 3 } 1; __END__ =head1 NAME Net::Z3950::Record::GRS1 - Perl package used to encode GRS-1 records. =head1 SYNOPSIS use Net::Z3950::GRS1; my $a_grs1_record = new Net::Z3950::Record::GRS1; my $another_grs1_record = new Net::Z3950::Record::GRS1; $a_grs1_record->AddElement($type, $value, $content); $a_grs1_record->Render(); =head1 DESCRIPTION This Perl module helps you to create and manipulate GRS-1 records (generic record syntax). So far, you have only access to three methods: =head2 new Creates a new GRS-1 object, my $grs1 = new Net::Z3950::GRS1; =head2 AddElement Lets you add entries to a GRS-1 object. The method should be called this way, $grs1->AddElement($type, $value, $which, $content); where $type should be an integer, and $value is free text. The $which argument should contain one of the constants listed in Appendix A. Finally, $content contains the "thing" that should be stored in this entry. The structure of $content should match the chosen element data type. For $which == Net::Z3950::GRS1::ElementData::String; $content should be some kind of scalar. If on the other hand, $which == Net::Z3950::GRS1::ElementData::Subtree; $content should be a GRS1 object. =head2 Render This method digs through the GRS-1 data structure and renders the record. You call it this way, $grs1->Render(); If you want to access the rendered record through a variable, you can do it like this, my $record_as_string; $grs1->Render(POOL => \$record_as_string); If you want it stored in a file, Render should be called this way, $grs1->Render(FILE => 'record.grs1'); When no file name is specified, you can choose to stream the rendered record, for instance, $grs1->Render(HANDLE => *STDOUT); ## or $grs1->Render(HANDLE => *STDERR); ## or $grs1->Render(HANDLE => *MY_HANDLE); =head2 Hash2grs This method converts a hash into a GRS-1 object. Scalar entries within the hash are converted into GRS-1 string elements. A hash entry can itself be a reference to another hash. In this case, the new referenced hash will be converted into a GRS-1 subtree. The method is called this way, $grs1->Hash2grs($href, $mapping); where $href is the hash to be converted and $mapping is referenced hash specifying the mapping between keys in $href and (type, value) pairs in the $grs1 object. The $mapping hash could for instance look like this, my $mapping = { title => [2, 1], author => [1, 1], issn => [3, 1] }; If the $grs1 object contains data prior to the invocation of Hash2grs, the new data represented by the hash is simply added. =head1 APPENDIX A These element data types are specified in the Z39.50 protocol: Net::Z3950::GRS1::ElementData::Octets Net::Z3950::GRS1::ElementData::Numeric Net::Z3950::GRS1::ElementData::Date Net::Z3950::GRS1::ElementData::Ext Net::Z3950::GRS1::ElementData::String <--- Net::Z3950::GRS1::ElementData::TrueOrFalse Net::Z3950::GRS1::ElementData::OID Net::Z3950::GRS1::ElementData::IntUnit Net::Z3950::GRS1::ElementData::ElementNotThere Net::Z3950::GRS1::ElementData::ElementEmpty Net::Z3950::GRS1::ElementData::NoDataRequested Net::Z3950::GRS1::ElementData::Diagnostic Net::Z3950::GRS1::ElementData::Subtree <--- Only the '<---' marked types are so far supported in this package. =head1 AUTHOR Anders Sønderberg Mortensen Index Data ApS, Copenhagen, Denmark. 2001/03/09 =head1 SEE ALSO Specification of the GRS-1 standard, for instance in the Z39.50 protocol specification. =cut Net-Z3950-SimpleServer-1.21/Changes0000644000175000017500000002530412772450675015303 0ustar mikemikeRevision history for Perl extension Net::Z3950::SimpleServer 1.21 Tue Sep 27 09:40:12 UTC 2016 * Better support for facets. Cope with absent terms. * Sample server ztest.pl demonstrates facets implemention. * The ztest.pl sample server now emits all INIT parameter when a connection is made. * Add Travis continuous integration configuration (modified from that of ZOOM-Perl). * Fix a bizarre RPM package-building bug whereby the lower-case letter 'n' was stripped from the name of the person building the package. Fixes SA-743 * Major overhaul of search documentation, which was missing many parameters including facet requests. Part of SUP-946. * Document CQL queries. * Document the Explain Handler. * Fix obsolete Library-of-Congress URL in documentation. * Fix links into Index Data's own documentation to use .html extensions rather than the old .tkl. * Fix some typos in documentation and change-log. * README file converted to README.md (Markdown). * Many other improvements and clarifications to documentation. * Update which distributions we build for. 1.20 Thu Jan 29 07:53:46 UTC 2015 - Add support for Type-1's proximity operator. Patch from Simon Jacob of the National Library of Australia. 1.19 Fri Nov 22 12:31:24 CET 2013 - ESTIMATED_HIT_COUNT = 1 from search_handler signal. - fetch handler: partial present response for undef RECORD The old behavior was to return empty record (if RECORD was not modified by handler). RECORD is now undefined upon entry to fetch handler and handler must set it to return a record or just leave it undefined to signal "no record". 1.18 Mon Oct 14 11:23:17 CEST 2013 - Debian/RPM packages now built with YAZ 5. 1.17 Tue Feb 12 13:30:52 CET 2013 - Scan and Search handler gets EXTRA_ARGS . It's represented as a hash. It holds the extra arguments for SRU (URL). - Scan and Search handler may return extra response data in EXTRA_RESPONSE_DATA. That's an XML fragment for SRU extra response data. - Scan Term may include a DISPLAY_TERM (Z39.50 / SRU display term). Thanks to Simon Jacob for patch. 1.16 Thu Jan 10 13:22:01 CET 2013 - Create packages for Ubuntu quantal precise oneiric. - Add support for GFS start handler (START) to allow handling of GFS config file (-c). - Handler search now gets PRESENT_NUMBER - a hint for how many records are to be fetched following search (piggyback). 1.15 Fri Oct 14 14:01:18 CEST 2011 - Fix decoding of OUTPUTFACETS (optinal) ; crashed on some Perl versions 1.14 Thu Aug 18 08:47:13 UTC 2011 - Support for facets in search handler - Corrections to manual, kindly supplied by Ben Webb - Resolve contradictory licence terms: SimpleServer is now definitely distributed under the Revised BSD licence, whereas earlier versions claimed in the source code to be Revised BSD but in the README to be under the same terms as Perl. 1.13 Wed Mar 16 17:07:10 GMT 2011 - The SimpleServer test-script now uses a Unix-domain socket, with a filename generated from the process-ID, rather than the default Inet-document socket on port 9999. Hopefully this should resolve the race-condition problems that have been affecting the "make test" part of the build cycle when building packages for four systems simultaneously under id-pbuild.sh 1.12 Thu Feb 4 16:33:19 GMT 2010 - Remove handling of "hits" parameter in the present handler. That should never have been there (and was probably sloppy copy), and prevents compilation under YAZ 4, which has removed the member from the bend_present_rr structure. 1.11 Wed Mar 4 15:12:53 GMT 2009 - Add explicit statement of license (same terms as Perl). No functional changes. 1.10 Tue Mar 3 22:47:16 GMT 2009 - Document the init-handler's PEER_NAME argument. - Update URL into YAZ documentation. - bend_delete() no longer returns without value. - Route around ActivePerl's damage to the "open" symbol. 1.09 Mon Sep 10 15:54:38 BST 2007 - *Argh* Another mixed statement/declaration. 1.08 Mon Sep 10 12:15:29 BST 2007 - *Sigh* Fix mixed statement/declaration. 1.07 Sat Sep 1 10:31:26 BST 2007 - When the scan-handler callback returns, do not attempt to copy the terms from Perl structures if the error-code is non-zero (i.e. if an error has occurred). This protects against a segmentation fault when the Perl callback does not explicitly set $args->{NUMBER} = 0 on error. - Correct transcription of string-valued attributes in $args->{RPN}. - Scan handler is now passed RPN as well as TERM, a tree of Perl structures representing the entire scan clause including access-points and other attributes (which are discarded from TERM). - The various classes used to represent nodes in the RPN query tree (Net::Z3950::APDU::Query, Net::Z3950::RPN::And, Net::Z3950::RPN::Or, Net::Z3950::RPN::AndNot, Net::Z3950::RPN::Term and Net::Z3950::RPN::RSID) now all share a newly introduced superclass Net::Z3950::RPN::Node, making it possible to write methods that apply to all node types. - A utility method toPQF() is provided for Net::Z3950::RPN::Node, enabling the RPN tree to be converted back into a flat PQF query. - Add support for the Delete Result Set service. - Add documentation for the Sort service. - Some clarifications to documentation. 1.06 Fri Aug 10 23:30:00 BST 2007 - New global-handle element can be specified when creating a simple-server object and will be passed to all callback functions. This allows global state to be eliminated from SimpleServer applications ... finally! - Search handler now deals correctly with undefined addinfo: previously a (harmless) error message was emitted. - Add Perl API to yaz_diag_srw_to_bib1(), which SimpleServer applications will need if they access SRU/SRW back-end databases and need to report errors. - Add Perl API to yaz_diag_bib1_to_srw(), because it would seem churlish not to. 1.05 Wed Dec 27 13:19:13 CET 2006 - Taking new naming convention for YAZ constants into account. 1.04 Fri Dec 1 10:48:32 CET 2006 - Build such that SimpleServer links to new yaz shared object. 1.03 Tue Aug 8 17:27:16 BST 2006 - Rely on version 2.1.14 or later of YAZ; this is the first version that reliably passes through the additional information associated with errors generated while serving SRU/W requests. No functional differences since 1.02. 1.02 Wed Jul 26 12:09:50 BST 2006 - Better support for Open and User/Password authentication. - SimpleServer.xs's rpn2pquery() is now discarded, and YAZ's yaz_rpnquery_to_wrbuf() used instead. This is more robust in dealing with unusual cases such as string-valued attributes. - Support for SCHEMA element when fetching records. 1.01 Fri Mar 24 12:09:32 GMT 2006 - Documentation of release 1.00's SRU/SRW facilities. - Makefile.PL now fails if YAZ version is earlier than 2.0.0, which was the first with SRU/SRW support. 1.00 Fri Mar 24 01:20:24 GMT 2006 - Support for SRU and SRW. Mostly this is provided by the YAZ GFS, but changes are needed to allow for the case where there is no RPN query (due to absent on invalid element in GFS configuration) so that CQL is passed through natively; and also to fake up an {REQ_FORM} and {REP_FORM} parameters set to the "text/xml" OID when this information is not specified by the GFS. (The jump in version number is due to the SRU/W support.) - Include "logging-server.pl" in the distribution: the simplest possible SimpleServer application, which merely logs the client-request data structures. - Makefile.PL is more helpful if yaz-config isn't found. - Explicitly disable prototypes in SimpleServer.xs: makes no difference but suppresses an error message for a cleaner build. 0.08 Mon Jun 14 14:51:01 2004 - SimpleServer is now perl 5.8 thread proof - Support for IMP_ID parameter in Init responses. This was actually written a long time ago, but left commented out as the underlying YAZ back-end server didn't support implementation-ID setting. Now that it does (and has done for eighteen months -- since YAZ release 1.8.6 of 2002/03/25!), I've finally removed the comments. - Init handler now understands the setting of {ERR_CODE} as more than a boolean success indicator, and also {ERR_STR}. They are now passed back to the client (thanks to recent changes to the YAZ generic front-end server) in accordance with Z39.50 Implementor Agreement 5, found at http://www.loc.gov/z3950/agency/agree/initdiag.html 0.07 Fri Jan 03 10:12:15 2003 - Applied Dave Mitchell's (davem@fdgroup.com) GRS-1 parsing patch. Thanks Dave, and sorry it didn't find its way to release 0.06, completely my fault. 0.06 Thu Jan 02 11:15:01 2003 - Added support for authentication - Add documentation for the object tree passed as the RPN member of the search-handler's argument hash. - We actually removed the vacuous Changelog and TODO files back in 0.05. They should never have been here :-) 0.05 Tue Feb 05 21:54:30 2002 - Add brief documentation of the new handling of RPN. 0.04 Tue Feb 05 21:49:56 2002 - Add Changelog (Why? We already have this file!) - Add TODO file (although it's empty!) - Change interface to constructor, and fix test.pl script to use the new interface. - Add support for Scan. - Add support for building GRS-1 records. - Add grs_test.pl test suite for new GRS-1 code. - Add RPN structure to search-handler argument hash. - Add PID element to init, search, fetch and present-handler argument hashes (but not the sort, scan and close-handlers, for some reason.) - Fix typos in documentation. 0.03 Thu Nov 09 16:22:00 2000 - Add the INSTALL file. - Add support for a present-handler (distinct from fetch). - Remove `$args->{LEN} = length($record)' from the example fetch-handler in the documentation. - Minor corrections to documentation, e.g. add commas after elements in anonymous hash of arguments. - Record syntaxes (formats) are now specified as ASCII OIDs (e.g. "1.2.840.10003.5.10") rather than human-readable strings (e.g. "usmarc") - Add some XS code to support sorting, though it doesn't seem to be finished yet, and is not wired out. - Use symbolic constants (e.g. Z_ElementSetNames_generic instead of hard-wired magic number 1). - Add PEER_NAME element to init-handler argument hash. - Minor changes to ztest.pl. 0.02 Mon Sep 11 12:32:00 2000 - First released versions 0.01 Wed Aug 30 14:54:01 2000 - original version; created by h2xs 1.19 ### To do - When invoking Init callback, set initial values of IMP_ID, IMP_NAME and IMP_VER from the client's Init request. Net-Z3950-SimpleServer-1.21/SimpleServer.pm0000644000175000017500000011024112772450675016761 0ustar mikemike## This file is part of simpleserver ## Copyright (C) 2000-2016 Index Data. ## All rights reserved. ## Redistribution and use in source and binary forms, with or without ## modification, are permitted provided that the following conditions are met: ## ## * Redistributions of source code must retain the above copyright ## notice, this list of conditions and the following disclaimer. ## * Redistributions in binary form must reproduce the above copyright ## notice, this list of conditions and the following disclaimer in the ## documentation and/or other materials provided with the distribution. ## * Neither the name of Index Data nor the names of its contributors ## may be used to endorse or promote products derived from this ## software without specific prior written permission. ## ## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY ## EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ## DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY ## DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ## (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF ## THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package Net::Z3950::SimpleServer; use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); use Carp; require Exporter; require DynaLoader; require AutoLoader; @ISA = qw(Exporter AutoLoader DynaLoader); @EXPORT = qw( ); $VERSION = '1.21'; bootstrap Net::Z3950::SimpleServer $VERSION; # Preloaded methods go here. my $count = 0; sub new { my $class = shift; my %args = @_; my $self = \%args; if ($count) { carp "SimpleServer.pm: WARNING: Multithreaded server unsupported"; } $count = 1; croak "SimpleServer.pm: ERROR: Unspecified search handler" unless defined($self->{SEARCH}); croak "SimpleServer.pm: ERROR: Unspecified fetch handler" unless defined($self->{FETCH}); bless $self, $class; return $self; } sub launch_server { my $self = shift; my @args = @_; ### This modal internal interface, in which we set a bunch of # globals and then call start_server(), is asking for # trouble. Instead, we should just pass the $self object # as a parameter into start_server(). if (defined($self->{GHANDLE})) { set_ghandle($self->{GHANDLE}); } if (defined($self->{INIT})) { set_init_handler($self->{INIT}); } set_search_handler($self->{SEARCH}); set_fetch_handler($self->{FETCH}); if (defined($self->{CLOSE})) { set_close_handler($self->{CLOSE}); } if (defined($self->{PRESENT})) { set_present_handler($self->{PRESENT}); } if (defined($self->{SCAN})) { set_scan_handler($self->{SCAN}); } if (defined($self->{SORT})) { set_sort_handler($self->{SORT}); } if (defined($self->{EXPLAIN})) { set_explain_handler($self->{EXPLAIN}); } if (defined($self->{DELETE})) { set_delete_handler($self->{DELETE}); } if (defined($self->{START})) { set_start_handler($self->{START}); } start_server(@args); } # Register packages that we will use in translated RPNs package Net::Z3950::RPN::Node; package Net::Z3950::APDU::Query; our @ISA = qw(Net::Z3950::RPN::Node); package Net::Z3950::APDU::OID; package Net::Z3950::RPN::And; our @ISA = qw(Net::Z3950::RPN::Node); package Net::Z3950::RPN::Or; our @ISA = qw(Net::Z3950::RPN::Node); package Net::Z3950::RPN::AndNot; our @ISA = qw(Net::Z3950::RPN::Node); package Net::Z3950::RPN::Prox; our @ISA = qw(Net::Z3950::RPN::Node); package Net::Z3950::RPN::Term; our @ISA = qw(Net::Z3950::RPN::Node); package Net::Z3950::RPN::RSID; our @ISA = qw(Net::Z3950::RPN::Node); package Net::Z3950::RPN::Attributes; package Net::Z3950::RPN::Attribute; package Net::Z3950::RPN::Prox::Attributes; package Net::Z3950::FacetList; package Net::Z3950::FacetField; package Net::Z3950::FacetTerms; package Net::Z3950::FacetTerm; # Utility method for re-rendering Type-1 query back down to PQF package Net::Z3950::RPN::Node; sub toPQF { my $this = shift(); my $class = ref $this; if ($class eq "Net::Z3950::APDU::Query") { my $res = ""; my $set = $this->{attributeSet}; $res .= "\@attrset $set " if defined $set; return $res . $this->{query}->toPQF(); } elsif ($class eq "Net::Z3950::RPN::Or") { return '@or ' . $this->[0]->toPQF() . ' ' . $this->[1]->toPQF(); } elsif ($class eq "Net::Z3950::RPN::And") { return '@and ' . $this->[0]->toPQF() . ' ' . $this->[1]->toPQF(); } elsif ($class eq "Net::Z3950::RPN::AndNot") { return '@not ' . $this->[0]->toPQF() . ' ' . $this->[1]->toPQF(); } elsif ($class eq "Net::Z3950::RPN::Prox") { my $pattrs = $this->[3]; return '@prox ' . $pattrs->{exclusion} . ' ' . $pattrs->{distance} . ' ' . $pattrs->{ordered} . ' ' . $pattrs->{relationType} . (defined $pattrs->{known} ? ' k ' . $pattrs->{known} : ' p ' . $pattrs->{zprivate}) . ' ' . $this->[0]->toPQF() . ' ' . $this->[1]->toPQF(); } elsif ($class eq "Net::Z3950::RPN::RSID") { return '@set ' . $this->{id}; } elsif ($class ne "Net::Z3950::RPN::Term") { die "unknown PQF node-type '$class'"; } my $res = ""; foreach my $attr (@{ $this->{attributes} }) { $res .= "\@attr "; my $set = $attr->{attributeSet}; $res .= "$set " if defined $set; $res .= $attr->{attributeType} . "=" . $attr->{attributeValue} . " "; } return $res . $this->{term}; } # Must revert to original package for Autoloader's benefit package Net::Z3950::SimpleServer; # Autoload methods go after =cut, and are processed by the autosplit program. 1; __END__ # Below is the stub of documentation for your module. You better edit it! =head1 NAME Net::Z3950::SimpleServer - Simple Perl API for building Z39.50 servers. =head1 SYNOPSIS use Net::Z3950::SimpleServer; sub my_search_handler { my $args = shift; my $set_id = $args->{SETNAME}; my @database_list = @{ $args->{DATABASES} }; my $query = $args->{QUERY}; ## Perform the query on the specified set of databases ## and return the number of hits: $args->{HITS} = $hits; } sub my_fetch_handler { # Get a record for the user my $args = shift; my $set_id = $args->{SETNAME}; my $record = fetch_a_record($args->{OFFSET}); $args->{RECORD} = $record; if (number_of_hits() == $args->{OFFSET}) { ## Last record in set? $args->{LAST} = 1; } else { $args->{LAST} = 0; } } ## Register custom event handlers: my $z = new Net::Z3950::SimpleServer(GHANDLE = $someObject, INIT => \&my_init_handler, CLOSE => \&my_close_handler, SEARCH => \&my_search_handler, FETCH => \&my_fetch_handler); ## Launch server: $z->launch_server("ztest.pl", @ARGV); =head1 DESCRIPTION The SimpleServer module is a tool for constructing Z39.50 "Information Retrieval" servers in Perl. The module is easy to use, but it does help to have an understanding of the Z39.50 query structure and the construction of structured retrieval records. Z39.50 is a network protocol for searching remote databases and retrieving the results in the form of structured "records". It is widely used in libraries around the world, as well as in the US Federal Government. In addition, it is generally useful whenever you wish to integrate a number of different database systems around a shared, abstract data model. The model of the module is simple: It implements a "generic" Z39.50 server, which invokes callback functions supplied by you to search for content in your database. You can use any tools available in Perl to supply the content, including modules like DBI and WWW::Search. The server will take care of managing the network connections for you, and it will spawn a new process (or thread, in some environments) whenever a new connection is received. The programmer can specify subroutines to take care of the following type of events: - Start service (called once). - Initialize request - Search request - Present request - Fetching of records - Scan request (browsing) - Closing down connection Note that only the Search and Fetch handler functions are required. The module can supply default responses to the other on its own. After the launching of the server, all control is given away from the Perl script to the server. The server calls the registered subroutines to field incoming requests from Z39.50 clients. A reference to an anonymous hash is passed to each handler. Some of the entries of these hashes are to be considered input and others output parameters. The Perl programmer specifies the event handlers for the server by means of the SimpleServer object constructor my $z = new Net::Z3950::SimpleServer( START => \&my_start_handler, INIT => \&my_init_handler, CLOSE => \&my_close_handler, SEARCH => \&my_search_handler, PRESENT => \&my_present_handler, SCAN => \&my_scan_handler, FETCH => \&my_fetch_handler, EXPLAIN => \&my_explain_handler, DELETE => \&my_delete_handler, SORT => \&my_sort_handler); In addition, the arguments to the constructor may include GHANDLE, a global handle which is made available to each invocation of every callback function. This is typically a reference to either a hash or an object. If you want your SimpleServer to start a thread (threaded mode) to handle each incoming Z39.50 request instead of forking a process (forking mode), you need to register the handlers by symbol rather than by code reference. Thus, in threaded mode, you will need to register your handlers this way: my $z = new Net::Z3950::SimpleServer( INIT => "my_package::my_init_handler", CLOSE => "my_package::my_close_handler", .... .... ); where my_package is the Perl package in which your handler is located. After the custom event handlers are declared, the server is launched by means of the method $z->launch_server("MyServer.pl", @ARGV); Notice, the first argument should be the name of your server script (for logging purposes), while the rest of the arguments are documented in the YAZ toolkit manual: The section on application invocation: In particular, you need to use the -T switch to start your SimpleServer in threaded mode. =head2 Start handler The start handler is called when service is started. The argument hash passed to the start handler has the form $args = { CONFIG => "default-config" ## GFS config (as given by -c) }; The purpose of the start handler is to read the configuration file for the Generic Frontend Server . This is specified by option -c. If -c is omitted, the configuration file is set to "default-config". The start handler is optional. It is supported in Simpleserver 1.16 and later. =head2 Init handler The init handler is called whenever a Z39.50 client is attempting to logon to the server. The exchange of parameters between the server and the handler is carried out via an anonymous hash reached by a reference, i.e. $args = shift; The argument hash passed to the init handler has the form $args = { ## Response parameters: PEER_NAME => "", ## Name or IP address of connecting client IMP_ID => "", ## Z39.50 Implementation ID IMP_NAME => "", ## Z39.50 Implementation name IMP_VER => "", ## Z39.50 Implementation version ERR_CODE => 0, ## Error code, cnf. Z39.50 manual ERR_STR => "", ## Error string (additional info.) USER => "xxx" ## If Z39.50 authentication is used, ## this member contains user name PASS => "yyy" ## Under same conditions, this member ## contains the password in clear text GHANDLE => $obj ## Global handle specified at creation HANDLE => undef ## Handler of Perl data structure }; The HANDLE member can be used to store any scalar value which will then be provided as input to all subsequent calls (i.e. for searching, record retrieval, etc.). A common use of the handle is to store a reference to a hash which may then be used to store session-specific parameters. If you have any session-specific information (such as a list of result sets or a handle to a back-end search engine of some sort), it is always best to store them in a private session structure - rather than leaving them in global variables in your script. The Implementation ID, name and version are only really used by Z39.50 client developers to see what kind of server they're dealing with. Filling these in is optional. The ERR_CODE should be left at 0 (the default value) if you wish to accept the connection. Any other value is interpreted as a failure and the client will be shown the door, with the code and the associated additional information, ERR_STR returned. =head2 Search handler Similarly, the search handler is called with a reference to an anonymous hash. The structure is the following: $args = { ## Request parameters: GHANDLE => $obj # Global handle specified at creation HANDLE => ref, # Your session reference. SETNAME => "id", # ID of the result set REPL_SET => 0, # Replace set if already existing? DATABASES => ["xxx"], # Reference to a list of databases to search QUERY => "query", # The query expression as a PQF string RPN => $obj, # Reference to a Net::Z3950::APDU::Query CQL => $x, # A CQL query, if this is provided instead of Type-1 SRW_SORTKEYS => $x, # XXX to be described PID => $x, # XXX to be described PRESENT_NUMBER => $x, # XXX to be described EXTRA_ARGS => $x, # XXX to be described INPUTFACETS => $x, # Specification of facets required: see below. ## Response parameters: ERR_CODE => 0, # Error code (0=Successful search) ERR_STR => "", # Error string HITS => 0, # Number of matches ESTIMATED_HIT_COUNT => $x, # XXX to be described EXTRA_RESPONSE_DATA => $x, # XXX to be described OUTPUTFACETS => $x # Facets returned: see below. }; Note that a search which finds 0 hits is considered successful in Z39.50 terms - you should only set the ERR_CODE to a non-zero value if there was a problem processing the request. The Z39.50 standard provides a comprehensive list of standard diagnostic codes, and you should use these whenever possible. =head3 Query structures In Z39.50, the most comment kind of query is the so-called Type-1 _query, a tree-structure of terms combined by operators, the terms being qualified by lists of attributes. The QUERY parameter presented this tree to the search function in the Prefix Query Format (PQF) which is used in many applications based on the YAZ toolkit. The full grammar is described in the YAZ manual. The following are all examples of valid queries in the PQF. dylan "bob dylan" @or "dylan" "zimmerman" @set Result-1 @or @and bob dylan @set Result-1 @and @attr 1=1 "bob dylan" @attr 1=4 "slow train coming" @attrset @attr 4=1 @attr 1=4 "self portrait" You will need to write a recursive function or something similar to parse incoming query expressions, and this is usually where a lot of the work in writing a database-backend happens. Fortunately, you don't need to support any more functionality than you want to. For instance, it is perfectly legal to not accept boolean operators, but you should try to return good error codes if you run into something you can't or won't support. A more convenient alternative to the QUERY member is the RPN member, which is a reference to a Net::Z3950::APDU::Query object representing the RPN query tree. The structure of that object is supposed to be self-documenting, but here's a brief summary of what you get: =over 4 =item * C is a hash with two fields: Z<> =over 4 =item C Optional. If present, it is a reference to a C. This is a string of dot-separated integers representing the OID of the query's top-level attribute set. =item C Mandatory: a reference to the RPN tree itself. =back =item * Each node of the tree is an object of one of the following types: Z<> =over 4 =item C =item C =item C These three classes are all arrays of two elements, each of which is a node. =item C A query term. See below for details. =item C A reference to a result-set ID indicating a previous search. The ID of the result-set is in the C element. =back =back =over 4 =item * C is a hash with two fields: Z<> =over 4 =item C A string containing the search term itself. =item C A reference to a C object. =back =item * C is an array of references to C objects. (Note the plural/singular distinction.) =item * C is a hash with three elements: Z<> =over 4 =item C Optional. If present, it is dot-separated OID string, as above. =item C An integer indicating the type of the attribute - for example, under the BIB-1 attribute set, type 1 indicates a ``use'' attribute, type 2 a ``relation'' attribute, etc. =item C An integer or string indicating the value of the attribute - for example, under BIB-1, if the attribute type is 1, then value 4 indicates a title search and 7 indicates an ISBN search; but if the attribute type is 2, then value 4 indicates a ``greater than or equal'' search, and 102 indicates a relevance match. =back =back All of these classes except C and C are subclasses of the abstract class C. That class has a single method, C, which may be used to turn an RPN tree, or part of one, back into a textual prefix query. Note that, apart to C, none of these classes have any methods at all: the blessing into classes is largely just a documentation thing so that, for example, if you do { use Data::Dumper; print Dumper($args->{RPN}) } you get something fairly human-readable. But of course, the type distinction between the three different kinds of boolean node is important. By adding your own methods to these classes (building what I call ``augmented classes''), you can easily build code that walks the tree of the incoming RPN. Take a look at C for a sample implementation of such an augmented classes technique. Finally, when SimpleServer is invoked using SRU/SRW (and indeed using Z39.50 if the unusual type-104 query is used), the query that is _passed is expressed in CQL, the Contextual Query Language. In this case, the query string is made available in the CQL argument. =head3 Facets Servers may support the provision of facets -- counted lists of field values which may subsequently be be used as query terms to narrow the search. In SRU, facets may be requested by the C parameter, L. Its value is a string consisting of a comma-separated list of facet specifications. Each facet specification consists of of a count, a colon and a fieldname. For example, C asks for ten title facets and five author facets. =head4 Request format The facet request is passed to the search-handler function in the INPUTFACETS parameter. Its value is rather complex, due to backwards compatibility with Z39.50: =over 4 =item * The top-level value is a C array. =item * This is an array of C objects. =item * Each of these is an object with two members, C and C. =item * C has type C and is a list of objects of type C. =item * Each attribute has two elements, C and C. Each value is interpreted according to its type. The meanings of the types are as follows: =over 4 =item 1 The name of the field to provide values of the facets. =item 2 The order in which to sort the values. (But it's not clear how this is to be interpreted: it may be implementation dependent.) =item 3 The number of facets to include for the specified field. =item 4 The first facet to include in the response: for example, if this is 11, then the first ten facts should be skipped. =back =back So for example, the SRU facet specification C would be translated as a C list of two Cs. The C of the first would be [1="title", 3=10], and those of the second would be [1="author", 3=5]. It is not clear what the purpose of C is, but for the record, this is how it is represented: =over 4 =item * C is a C array. =item * This is an array of C objects. =item * Each of these is an object with two members, C and C. The first of these is an integer, the second a string. =back =head4 Response format Having generated facets corresponding to the request, the search handler should return them in the C argument. The structure of this response is similar to that of the request: =over 4 =item * The top-level value is a C array. =item * This is an array of C objects. =item * Each of these is an object with two members, C and C. =item * C has type C and is a list of objects of type C. =item * Each attribute has two elements, C and C. Each value is interpreted according to its type. The meanings of the types are as follows: =over 4 =item 1 The name of the field for which terms are provided. =back (That is the only type used.) =item * C is a C array. =item * This is an array of C objects. =item * Each of these is an object with two members, C and C. The first of these is a string containing one of the facet terms, and the second is an integer indicating how many times it occurs in the records that were found by the search. =back The example SimpleServer applicaation server C includes code that shows how to examine the INPUTFACETS data structure and create the OUTPUTFACETS structure. =head2 Present handler The presence of a present handler in a SimpleServer front-end is optional. Each time a client wishes to retrieve records, the present service is called. The present service allows the origin to request a certain number of records retrieved from a given result set. When the present handler is called, the front-end server should prepare a result set for fetching. In practice, this means to get access to the data from the backend database and store the data in a temporary fashion for fast and efficient fetching. The present handler does *not* fetch anything. This task is taken care of by the fetch handler, which will be called the correct number of times by the YAZ library. More about this below. If no present handler is implemented in the front-end, the YAZ toolkit will take care of a minimum of preparations itself. This default present handler is sufficient in many situations, where only a small amount of records are expected to be retrieved. If on the other hand, large result sets are likely to occur, the implementation of a reasonable present handler can gain performance significantly. The information exchanged between client and present handle is: $args = { ## Client/server request: GHANDLE => $obj ## Global handle specified at creation HANDLE => ref, ## Reference to datastructure SETNAME => "id", ## Result set ID START => xxx, ## Start position COMP => "", ## Desired record composition NUMBER => yyy, ## Number of requested records ## Response parameters: HITS => zzz, ## Number of returned records ERR_CODE => 0, ## Error code ERR_STR => "" ## Error message }; =head2 Fetch handler The fetch handler is asked to retrieve a SINGLE record from a given result set (the front-end server will automatically call the fetch handler as many times as required). The parameters exchanged between the server and the fetch handler are: $args = { ## Client/server request: GHANDLE => $obj ## Global handle specified at creation HANDLE => ref ## Reference to data structure SETNAME => "id" ## ID of the requested result set OFFSET => nnn ## Record offset number REQ_FORM => "n.m.k.l"## Client requested format OID COMP => "xyz" ## Formatting instructions SCHEMA => "abc" ## Requested schema, if any ## Handler response: RECORD => "" ## Record string BASENAME => "" ## Origin of returned record LAST => 0 ## Last record in set? ERR_CODE => 0 ## Error code ERR_STR => "" ## Error string SUR_FLAG => 0 ## Surrogate diagnostic flag REP_FORM => "n.m.k.l"## Provided format OID SCHEMA => "abc" ## Provided schema, if any }; The REP_FORM value has by default the REQ_FORM value, but can be set to something different if the handler desires. The BASENAME value should contain the name of the database from where the returned record originates. The ERR_CODE and ERR_STR works the same way they do in the search handler. If there is an error condition, the SUR_FLAG is used to indicate whether the error condition pertains to the record currently being retrieved, or whether it pertains to the operation as a whole (e.g. the client has specified a result set which does not exist.) If you need to return USMARC records, you might want to have a look at the MARC module on CPAN, if you don't already have a way of generating these. NOTE: The record offset is 1-indexed, so 1 is the offset of the first record in the set. =head2 Scan handler A full featured Z39.50 server should support scan (or in some literature browse). The client specifies a starting term of the scan, and the server should return an ordered list of specified length consisting of terms actually occurring in the data base. Each of these terms should be close to or equal to the term originally specified. The quality of scan compared to simple search is a guarantee of hits. It is simply like browsing through an index of a book, you always find something! The parameters exchanged are: $args = { ## Client request GHANDLE => $obj, ## Global handle specified at creation HANDLE => $ref, ## Reference to data structure DATABASES => ["xxx"], ## Reference to a list of data- ## bases to search TERM => 'start', ## The start term RPN => $obj, ## Reference to a Net::Z3950::RPN::Term NUMBER => xx, ## Number of requested terms POS => yy, ## Position of starting point ## within returned list STEP => 0, ## Step size ## Server response ERR_CODE => 0, ## Error code ERR_STR => '', ## Diagnostic message NUMBER => zz, ## Number of returned terms STATUS => $status, ## ScanSuccess/ScanFailure ENTRIES => $entries ## Referenced list of terms }; where the term list is returned by reference in the scalar $entries, which should point at a data structure of this kind, my $entries = [ { TERM => 'energy', OCCURRENCE => 5 }, { TERM => 'energy density', OCCURRENCE => 6, }, { TERM => 'energy flow', OCCURRENCE => 3 }, ... ... ]; The $status flag is only meaningful after a successful scan, and should be assigned one of two values: Net::Z3950::SimpleServer::ScanSuccess Full success (default) Net::Z3950::SimpleServer::ScanPartial Fewer terms returned than requested The STEP member contains the requested number of entries in the term-list between two adjacent entries in the response. A better alternative to the TERM member is the the RPN member, which is a reference to a Net::Z3950::RPN::Term object representing the scan clause. The structure of that object is the same as for Term objects included as part of the RPN tree passed to search handlers. This is more useful than the simple TERM because it includes attributes (e.g. access points associated with the term), which are discarded by the TERM element. =head2 Close handler The argument hash received by the close handler has two elements only: $args = { ## Server provides: GHANDLE => $obj ## Global handle specified at creation HANDLE => ref ## Reference to data structure }; What ever data structure the HANDLE value points at goes out of scope after this call. If you need to close down a connection to your server or something similar, this is the place to do it. =head2 Explain handler The argument hash received by the explain handler has the following elements: $args = { ## Request parameters: GHANDLE => $obj, # Global handle specified at creation HANDLE => ref, # Reference to data structure DATABASE => $dbname, # Name of database to be explained ## Response parameters: EXPLAIN => $zeerex # ZeeRex record for specified database }; The handler should return a string containing the ZeeRex XML that describes that nominated database. =head2 Delete handler The argument hash received by the delete handler has the following elements: $args = { ## Client request: GHANDLE => $obj, ## Global handle specified at creation HANDLE => ref, ## Reference to data structure SETNAME => "id", ## Result set ID ## Server response: STATUS => 0 ## Deletion status }; The SETNAME element of the argument hash may or may not be defined. If it is, then SETNAME is the name of a result set to be deleted; if not, then all result-sets associated with the current session should be deleted. In either case, the callback function should report on success or failure by setting the STATUS element either to zero, on success, or to an integer from 1 to 10, to indicate one of the ten possible failure codes described in section 3.2.4.1.4 of the Z39.50 standard -- see http://www.loc.gov/z3950/agency/markup/05.html#Delete-list-statuses1 =head2 Sort handler The argument hash received by the sort handler has the following elements: $args = { ## Client request: GHANDLE => $obj, ## Global handle specified at creation HANDLE => ref, ## Reference to data structure INPUT => [ a, b ... ], ## Names of result-sets to sort OUTPUT => "name", ## Name of result-set to sort into SEQUENCE ## Sort specification: see below ## Server response: STATUS => 0, ## Success, Partial or Failure ERR_CODE => 0, ## Error code ERR_STR => '', ## Diagnostic message }; The SEQUENCE element is a reference to an array, each element of which is a hash representing a sort key. Each hash contains the following elements: =over 4 =item RELATION 0 for an ascending sort, 1 for descending, 3 for ascending by frequency, or 4 for descending by frequency. =item CASE 0 for a case-sensitive sort, 1 for case-insensitive =item MISSING How to respond if one or more records in the set to be sorted are missing the fields indicated in the sort specification. 1 to abort the sort, 2 to use a "null value", 3 if a value is provided to use in place of the missing data (although in the latter case, the actual value to use is currently not made available, so this is useless). =back And one or other of the following: =over 4 =item SORTFIELD A string indicating the field to be sorted, which the server may interpret as it sees fit (presumably by an out-of-band agreement with the client). =item ELEMENTSPEC_TYPE and ELEMENTSPEC_VALUE I have no idea what this is. =item ATTRSET and SORT_ATTR ATTRSET is the attribute set from which the attributes are taken, and SORT_ATTR is a reference to an array containing the attributes themselves. Each attribute is represented by (are you following this carefully?) yet another hash, this one containing the elements ATTR_TYPE and ATTR_VALUE: for example, type=1 and value=4 in the BIB-1 attribute set would indicate access-point 4 which is title, so that a sort of title is requested. =back Precisely why all of the above is so is not clear, but goes some way to explain why, in the Z39.50 world, the developers of the standard are not so much worshiped as blamed. The backend function should set STATUS to 0 on success, 1 for "partial success" (don't ask) or 2 on failure, in which case ERR_CODE and ERR_STR should be set. =head2 Support for SRU and SRW Since release 1.0, SimpleServer includes support for serving the SRU and SRW protocols as well as Z39.50. These ``web-friendly'' protocols enable similar functionality to that of Z39.50, but by means of rich URLs in the case of SRU, and a SOAP-based web-service in the case of SRW. These protocols are described at http://www.loc.gov/standards/sru/ In order to serve these protocols from a SimpleServer-based application, it is necessary to launch the application with a YAZ Generic Frontend Server (GFS) configuration file, which can be specified using the command-line argument C<-f> I. A minimal configuration file looks like this: pqf.properties This file specifies only that C should be used to translate the CQL queries of SRU and SRW into corresponding Z39.50 Type-1 queries. For more information about YAZ GFS configuration, including how to specify an Explain record, see the I section of the YAZ manual at http://www.indexdata.com/yaz/doc/server.vhosts.html The mapping of CQL queries into Z39.50 Type-1 queries is specified by a file that indicates which BIB-1 attributes should be generated for each CQL index, relation, modifiers, etc. A typical section of this file looks like this: index.dc.title = 1=4 index.dc.subject = 1=21 index.dc.creator = 1=1003 relation.< = 2=1 relation.le = 2=2 This file specifies the BIB-1 access points (type=1) for the Dublin Core indexes C, C<subject> and C<creator>, and the BIB-1 relations (type=2) corresponding to the CQL relations C<E<lt>> and C<E<lt>=>. For more information about the format of this file, see the I<CQL> section of the YAZ manual at http://www.indexdata.com/yaz/doc/tools.html#cql The YAZ distribution includes a sample CQL-to-PQF mapping configuration file called C<pqf.properties>; this is sufficient for many applications, and a good base to work from for most others. If a SimpleServer-based application is run without this SRU-specific configuration, it can still serve SRU; however, CQL queries will not be translated, but passed straight through to the search-handler function, as the C<CQL> member of the parameters hash. It is then the responsibility of the back-end application to parse and handle the CQL query, which is most easily done using Ed Summers' fine C<CQL::Parser> module, available from CPAN at http://search.cpan.org/~esummers/CQL-Parser/ =head1 AUTHORS Anders Sønderberg (sondberg@indexdata.dk), Sebastian Hammer (quinn@indexdata.dk), Mike Taylor (indexdata.com). =head1 COPYRIGHT AND LICENCE Copyright (C) 2000-2016 by Index Data. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available. =head1 SEE ALSO Any Perl module which is useful for accessing the data source of your choice. =cut ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Net-Z3950-SimpleServer-1.21/README.md���������������������������������������������������������������0000644�0001750�0001750�00000002513�12741405100�015237� 0����������������������������������������������������������������������������������������������������ustar �mike����������������������������mike�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������## Net::Z3950::SimpleServer - Simple Perl API for building Z39.50 servers. The SimpleServer module is a tool for constructing Z39.50 "Information Retrieval" servers in Perl. The module is easy to use, but it does help to have an understanding of the Z39.50 query structure and the construction of structured retrieval records. Z39.50 is a network protocol for searching remote databases and retrieving the results in the form of structured "records". It is widely used in libraries around the world, as well as in the US Federal Government. In addition, it is generally useful whenever you wish to integrate a number of different database systems around a shared, abstract data model. The model of the module is simple: It implements a "generic" Z39.50 server, which invokes callback functions supplied by you to search for content in your database. You can use any tools available in Perl to supply the content, including modules like DBI and WWW::Search. The server will take care of managing the network connections for you, and it will spawn a new process (or thread, in some environments) whenever a new connection is received. ### AUTHORS Anders Sønderberg <sondberg@indexdata.dk> Sebastian Hammer <quinn@indexdata.com> Mike Taylor <mike@indexdata.com> Adam Dickmeiss <adam@indexdata.com> ### COPYRIGHT AND LICENCE See file LICENSE �������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Net-Z3950-SimpleServer-1.21/ztest.pl����������������������������������������������������������������0000755�0001750�0001750�00000020351�12772450675�015516� 0����������������������������������������������������������������������������������������������������ustar �mike����������������������������mike�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������#!/usr/bin/perl -w ## This file is part of simpleserver ## Copyright (C) 2000-2016 Index Data. ## All rights reserved. ## Redistribution and use in source and binary forms, with or without ## modification, are permitted provided that the following conditions are met: ## ## * Redistributions of source code must retain the above copyright ## notice, this list of conditions and the following disclaimer. ## * Redistributions in binary form must reproduce the above copyright ## notice, this list of conditions and the following disclaimer in the ## documentation and/or other materials provided with the distribution. ## * Neither the name of Index Data nor the names of its contributors ## may be used to endorse or promote products derived from this ## software without specific prior written permission. ## ## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY ## EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ## DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY ## DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ## (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF ## THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use ExtUtils::testlib; use Data::Dumper; use Net::Z3950::SimpleServer; use Net::Z3950::OID; use strict; sub dump_hash { my $href = shift; my $key; foreach $key (keys %$href) { printf("%10s => %s\n", $key, $href->{$key}); } } sub my_init_handler { my $args = shift; my $session = {}; print("IMP_ID = '", $args->{IMP_ID}, "'\n"); print("IMP_NAME = '", $args->{IMP_NAME}, "'\n"); print("IMP_VER = '", $args->{IMP_VER}, "'\n"); print("ERR_CODE = '", $args->{ERR_CODE}, "'\n"); print("ERR_STR = '", $args->{ERR_STR}, "'\n"); print("PEER_NAME = '", $args->{PEER_NAME}, "'\n"); print("GHANDLE = '", $args->{GHANDLE}, "'\n"); print("HANDLE = '", $args->{HANDLE}, "'\n"); print("PID = '", $args->{PID}, "'\n"); if (defined($args->{USER})) { printf("Received USER=%s\n", $args->{USER}); } if (defined($args->{PASS})) { printf("Received PASS=%s\n", $args->{PASS}); } $args->{IMP_NAME} = "DemoServer"; $args->{IMP_ID} = "81"; $args->{IMP_VER} = "3.14159"; $args->{ERR_CODE} = 0; $args->{HANDLE} = $session; } sub my_sort_handler { my ($args) = @_; print "Sort handler called\n"; print Dumper( $args ); } sub my_scan_handler { my $args = shift; my $term = $args->{TERM}; my $entries = [ { TERM => 'Number 1', DISPLAY_TERM => 'Number .1', OCCURRENCE => 10 }, { TERM => 'Number 2', OCCURRENCE => 8 }, { TERM => 'Number 3', OCCURRENCE => 8 }, { TERM => 'Number 4', OCCURRENCE => 8 }, { TERM => 'Number 5', OCCURRENCE => 8 }, { TERM => 'Number 6', OCCURRENCE => 8 }, { TERM => 'Number 7', OCCURRENCE => 8 }, { TERM => 'Number 8', OCCURRENCE => 8 }, { TERM => 'Number 9', OCCURRENCE => 8 }, { TERM => 'Number 10', OCCURRENCE => 4 }, ]; $args->{NUMBER} = 10; $args->{ENTRIES} = $entries; $args->{STATUS} = Net::Z3950::SimpleServer::ScanPartial; print "Welcome to scan....\n"; $args->{EXTRA_RESPONSE_DATA} = '<scanextra>b</scanextra>'; print "You scanned for term '$term'\n"; } my $_fail_frequency = 0; my $_counter = 0; sub my_search_handler { my $args = shift; my $data = [{ name => "Peter Dornan", title => "Spokesman", collaboration => "ATLAS" }, { name => "Jorn Dines Hansen", title => "Professor", collaboration => "HERA-B" }, { name => "Alain Blondel", title => "Head of coll.", collaboration => "ALEPH" }]; my $session = $args->{HANDLE}; my $set_id = $args->{SETNAME}; my $rpn = $args->{RPN}; my @database_list = @{ $args->{DATABASES} }; my $query = $args->{QUERY}; my $facets = my_facets_response($args->{INPUTFACETS}); my $hits = 3; print "------------------------------------------------------------\n"; print "Processing query : $query\n"; printf("Database set : %s\n", join(" ", @database_list)); print "Setname : $set_id\n"; print " inputfacets:\n"; print Dumper($facets); print " extra args:\n"; print Dumper($args->{EXTRA_ARGS}); print "------------------------------------------------------------\n"; $args->{OUTPUTFACETS} = $facets; $args->{EXTRA_RESPONSE_DATA} = '<searchextra>b</searchextra>'; $args->{HITS} = $hits; $session->{$set_id} = $data; $session->{__HITS} = $hits; if ($_fail_frequency != 0 && ++$_counter % $_fail_frequency == 0) { print "Exiting to be nasty to client\n"; exit(1); } } sub my_facets_response { my $inputfacets = shift; if (!$inputfacets) { # no facets requested: generate default input facets $inputfacets = [ { attributes => [ { attributeType => 1, attributeValue => 'author' }, { attributeType => 2, attributeValue => 0 }, { attributeType => 3, attributeValue => 5 }, { attributeType => 4, attributeValue => 1 } ] }, { attributes => [ { attributeType => 1, attributeValue => 'title' }, { attributeType => 2, attributeValue => 0 }, { attributeType => 3, attributeValue => 5 }, { attributeType => 4, attributeValue => 1 } ] } ]; } # generate facets response. we use inputfacets as basis my $zfacetlist = []; bless $zfacetlist, 'Net::Z3950::FacetList'; my $i = 0; foreach my $x (@$inputfacets) { my $facetname = "unknown"; my $sortorder = 0; my $count = 5; my $offset = 1; foreach my $attr (@{$x->{'attributes'}}) { my $type = $attr->{'attributeType'}; my $value = $attr->{'attributeValue'}; print "attr " . $type . "=" . $value . "\n"; if ($type == 1) { $facetname = $value; } if ($type == 2) { $sortorder = $value; } if ($type == 3) { $count = $value; } if ($type == 4) { $offset = $value; } } my $zfacetfield = {}; bless $zfacetfield, 'Net::Z3950::FacetField'; if ($count > 0) { $zfacetlist->[$i++] = $zfacetfield; } my $zattributes = []; bless $zattributes, 'Net::Z3950::RPN::Attributes'; $zfacetfield->{'attributes'} = $zattributes; my $zattribute = {}; bless $zattribute, 'Net::Z3950::RPN::Attribute'; $zattribute->{'attributeType'} = 1; $zattribute->{'attributeValue'} = $facetname; $zattributes->[0] = $zattribute; my $zfacetterms = []; bless $zfacetterms, 'Net::Z3950::FacetTerms'; $zfacetfield->{'terms'} = $zfacetterms; my $j = 0; while ($j < $count) { my $zfacetterm = {}; bless $zfacetterm, 'Net::Z3950::FacetTerm'; # just a fake term .. $zfacetterm->{'term'} = "t" . $j; # most frequent first (fake count) my $freq = $count - $j; $zfacetterm->{'count'} = $freq; $zfacetterms->[$j++] = $zfacetterm; } } return $zfacetlist; } sub my_fetch_handler { my $args = shift; my $session = $args->{HANDLE}; my $set_id = $args->{SETNAME}; my $data = $session->{$set_id}; my $offset = $args->{OFFSET}; my $record = "<xml>"; my $field; my $hits = $session->{__HITS}; my $href = $data->[$offset - 1]; $args->{REP_FORM} = Net::Z3950::OID::xml; foreach $field (keys %$href) { $record .= "<" . $field . ">" . $href->{$field} . "</" . $field . ">"; } $record .= "</xml>"; $args->{RECORD} = $record; if ($offset == $session->{__HITS}) { $args->{LAST} = 1; } } sub my_start_handler { my $args = shift; my $config = $args->{CONFIG}; } Net::Z3950::SimpleServer::yazlog("hello"); my $handler = new Net::Z3950::SimpleServer( START => "main::my_start_handler", INIT => "main::my_init_handler", SEARCH => "main::my_search_handler", SCAN => "main::my_scan_handler", SORT => "main::my_sort_handler", FETCH => "main::my_fetch_handler" ); if (@ARGV >= 2 && $ARGV[0] eq "-n") { $_fail_frequency = $ARGV[1]; shift; shift; } $handler->launch_server("ztest.pl", @ARGV); ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Net-Z3950-SimpleServer-1.21/INSTALL�����������������������������������������������������������������0000644�0001750�0001750�00000001663�11620205472�015023� 0����������������������������������������������������������������������������������������������������ustar �mike����������������������������mike�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Building and installation ------------------------- 1. Requirements The Net::Z3950::SimpleServer Perl module requires the YAZ toolkit. Version 4 or later is recommended. You can download it via this link: http://ftp.indexdata.dk/pub/yaz/ You need to configure the YAZ toolkit with the command % ./configure Then compile and install % make # make install You don't *need* to install YAZ globally on your machine if you prefer not to. In this case, you just have to update the Makefile.PL with the locations of the libraries. 2. Installation Unpack the tar ball this way % gunzip Net-Z3950-SimpleServer-xxxx.tar.gz % tar xvf Net-Z3950-SimpleServer-xxxx.tar.gz and cd to the root directory of the module package % cd Net-Z3950-SimpleServer-xxxx The installation takes place by issuing the following commands: % perl Makefile.PL % make % make test # make install The last command has to be issued as super user. Good luck! �����������������������������������������������������������������������������Net-Z3950-SimpleServer-1.21/LICENSE�����������������������������������������������������������������0000644�0001750�0001750�00000002731�12772450675�015014� 0����������������������������������������������������������������������������������������������������ustar �mike����������������������������mike�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Copyright (c) 2000-2016, Index Data All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Index Data nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������