/*
- * $Id: SimpleServer.xs,v 1.50 2006-07-24 23:05:22 mike Exp $
+ * $Id: SimpleServer.xs,v 1.63 2007-05-23 10:22:00 sondberg Exp $
* ----------------------------------------------------------------------
*
* Copyright (c) 2000-2004, Index Data.
#include <yaz/backend.h>
#include <yaz/log.h>
#include <yaz/wrbuf.h>
+#include <yaz/querytowrbuf.h>
#include <stdio.h>
+#include <yaz/mutex.h>
+#include <yaz/oid_db.h>
#ifdef WIN32
#else
#include <unistd.h>
#define sv_undef PL_sv_undef
#endif
-NMEM_MUTEX simpleserver_mutex;
+YAZ_MUTEX simpleserver_mutex;
typedef struct {
SV *handle;
SV *scan_ref = NULL;
SV *explain_ref = NULL;
PerlInterpreter *root_perl_context;
-int MAX_OID = 15;
-#define GRS_BUF_SIZE 512
+#define GRS_BUF_SIZE 8192
CV * simpleserver_sv2cv(SV *handler) {
STRLEN len;
int simpleserver_clone(void) {
#ifdef USE_ITHREADS
- nmem_mutex_enter(simpleserver_mutex);
+ yaz_mutex_enter(simpleserver_mutex);
if (1)
{
PerlInterpreter *current = PERL_GET_CONTEXT;
PERL_SET_CONTEXT( perl_interp );
}
}
- nmem_mutex_leave(simpleserver_mutex);
+ yaz_mutex_leave(simpleserver_mutex);
#endif
return 0;
}
void simpleserver_free(void) {
- nmem_mutex_enter(simpleserver_mutex);
+ yaz_mutex_enter(simpleserver_mutex);
if (1)
{
PerlInterpreter *current_interp = PERL_GET_CONTEXT;
perl_free(current_interp);
}
}
- nmem_mutex_leave(simpleserver_mutex);
+ yaz_mutex_leave(simpleserver_mutex);
}
}
len = ptr - str;
if (len > GRS_BUF_SIZE) {
- yaz_log(LOG_WARN, "GRS string too long - truncating (%d > %d)", 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);
}
if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
{
- yaz_log(LOG_WARN, "Bad data in '%s'", buf);
+ yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
return r;
}
if (!type && *value == '0')
buf++;
if (r->num_elements >= GRS_MAX_FIELDS)
{
- yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", 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 = (int *)odr_malloc(o, sizeof(int));
- *t->tagType = type;
+ 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 = (int *)odr_malloc(o, sizeof(int));
- *t->tagValue->u.numeric = ivalue;
+ t->tagValue->u.numeric = odr_intdup(o, ivalue);
}
else
{
t->tagValue->which = Z_StringOrNumeric_string;
- t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
- strcpy(t->tagValue->u.string, value);
+ t->tagValue->u.string = odr_strdup(o, value);
}
t->tagOccurrence = 0;
t->metaData = 0;
-
static void oid2str(Odr_oid *o, WRBUF buf)
{
for (; *o >= 0; o++) {
}
}
+WRBUF oid2dotted(Odr_oid *oid)
+{
+ WRBUF buf = wrbuf_alloc();
+ oid2str(oid, buf);
+ return buf;
+}
+
WRBUF zquery2pquery(Z_Query *q)
{
oid2str(sattr->id, attrset_wr);
hv_store(sort_spec, "ATTRSET", 7,
newSVpv(attrset_wr->buf, attrset_wr->pos), 0);
- wrbuf_free(attrset_wr, 1);
+ wrbuf_destroy(attrset_wr);
hv_store(sort_spec, "SORT_ATTR", 9, newRV( sv_2mortal( (SV*) list ) ), 0);
oid2str(zspec->schema.oid, elementSpec);
hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
newSVpv(elementSpec->buf, elementSpec->pos), 0);
- wrbuf_free(elementSpec, 1);
+ wrbuf_destroy(elementSpec);
}
else if (zspec->which == Z_Schema_uri)
{
sv_free( (SV*) aref);
sv_free( (SV*) href);
if (query)
- wrbuf_free(query, 1);
+ wrbuf_destroy(query);
PUTBACK;
FREETMPS;
LEAVE;
}
-/* ### this is worryingly similar to oid2str() */
-WRBUF oid2dotted(int *oid)
-{
-
- WRBUF buf = wrbuf_alloc();
- int dot = 0;
-
- for (; *oid != -1 ; oid++)
- {
- char ibuf[16];
- if (dot)
- {
- wrbuf_putc(buf, '.');
- }
- else
- {
- dot = 1;
- }
- sprintf(ibuf, "%d", *oid);
- wrbuf_puts(buf, ibuf);
- }
- return buf;
-}
-
-
-int dotted2oid(char *dotted, int *buffer)
-{
- int *oid;
- char ibuf[16];
- char *ptr;
- int n = 0;
-
- ptr = ibuf;
- oid = buffer;
- while (*dotted)
- {
- if (*dotted == '.')
- {
- n++;
- if (n == MAX_OID) /* Terminate if more than MAX_OID entries */
- {
- *oid = -1;
- return -1;
- }
- *ptr = 0;
- sscanf(ibuf, "%d", oid++);
- ptr = ibuf;
- dotted++;
-
- }
- else
- {
- *ptr++ = *dotted++;
- }
- }
- if (n < MAX_OID)
- {
- *ptr = 0;
- sscanf(ibuf, "%d", oid++);
- }
- *oid = -1;
- return 0;
-}
-
-
int bend_fetch(void *handle, bend_fetch_rr *rr)
{
HV *href;
SV *sur_flag;
SV *point;
SV *rep_form;
- SV *schema;
+ SV *schema = 0;
char *ptr;
char *ODR_record;
char *ODR_basename;
char *ODR_errstr;
- int *ODR_oid_buf;
- oident *oid;
WRBUF oid_dotted;
Zfront_handle *zhandle = (Zfront_handle *)handle;
CV* handler_cv = 0;
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_raw != 0) {
- oid_dotted = oid2dotted(rr->request_format_raw);
+ if (rr->request_format != 0) {
+ oid_dotted = oid2dotted(rr->request_format);
} else {
/* Probably an SRU request: assume XML is required */
oid_dotted = wrbuf_alloc();
else
{
rr->errcode = 26;
- return;
+ return 0;
}
}
temp = hv_fetch(href, "REP_FORM", 8, 1);
rep_form = newSVsv(*temp);
- temp = hv_fetch(href, "SCHEMA", 8, 1);
- schema = newSVsv(*temp);
+ temp = hv_fetch(href, "SCHEMA", 6, 1);
+ if (temp != 0) {
+ schema = newSVsv(*temp);
+ ptr = SvPV(schema, length);
+ if (length > 0) {
+ rr->schema = (char *)odr_malloc(rr->stream, length + 1);
+ strcpy(rr->schema, ptr);
+ }
+ }
temp = hv_fetch(href, "HANDLE", 6, 1);
point = newSVsv(*temp);
strcpy(ODR_basename, ptr);
rr->basename = ODR_basename;
- ptr = SvPV(schema, length);
- rr->schema = (char *)odr_malloc(rr->stream, length + 1);
- strcpy(rr->schema, ptr);
-
ptr = SvPV(rep_form, length);
- ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
- if (dotted2oid(ptr, ODR_oid_buf) == -1) /* Maximum number of OID elements exceeded */
+
+ 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: OID structure too long, max length is %d\n", MAX_OID);
+ printf("Net::Z3950::SimpleServer: WARNING: Bad OID %s\n", ptr);
+ rr->output_format =
+ odr_oiddup(rr->stream, yaz_oid_recsyn_sutrs);
}
- rr->output_format_raw = ODR_oid_buf;
-
ptr = SvPV(record, length);
- oid = oid_getentbyoid(ODR_oid_buf);
- if (oid->value == VAL_GRS1) /* Treat GRS-1 records separately */
+ /* 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;
}
rr->surrogate_flag = SvIV(sur_flag);
- wrbuf_free(oid_dotted, 1);
+ wrbuf_destroy(oid_dotted);
sv_free((SV*) href);
sv_free(basename);
sv_free(record);
sv_free(err_code),
sv_free(sur_flag);
sv_free(rep_form);
- sv_free(schema);
+
+ if (schema)
+ sv_free(schema);
PUTBACK;
FREETMPS;
else
{
rr->errcode = 26;
- return;
+ return 0;
}
}
if (rr->term->term->which == Z_Term_general)
{
term_len = rr->term->term->u.general->len;
- hv_store(href, "TERM", 4, newSVpv(rr->term->term->u.general->buf, term_len), 0);
+ hv_store(href, "TERM", 4, newSVpv((char*) rr->term->term->u.general->buf, term_len), 0);
} else {
rr->errcode = 229; /* Unsupported term type */
return 0;
}
*argv_buf = NULL;
root_perl_context = PERL_GET_CONTEXT;
- nmem_mutex_create(&simpleserver_mutex);
+ yaz_mutex_create(&simpleserver_mutex);
#if 0
/* only for debugging perl_clone .. */
tst_clones();