struct http_header * http_header_append(struct http_channel *ch, struct http_header * hp, const char *name, const char *value) { struct http_header *hpnew = 0; if (!hp | !ch) return 0; while (hp && hp->next) hp = hp->next; if(name && strlen(name)&& value && strlen(value)){ hpnew = nmem_malloc(ch->nmem, sizeof *hpnew); hpnew->name = nmem_strdup(ch->nmem, name); hpnew->value = nmem_strdup(ch->nmem, value); hpnew->next = 0; hp->next = hpnew; hp = hp->next; return hpnew; } return hp; }
static void apply_local_setting(void *client_data, struct setting *set) { struct session *se = (struct session *) client_data; session_apply_setting(se, nmem_strdup(se->session_nmem, set->target), nmem_strdup(se->session_nmem, set->name), nmem_strdup(se->session_nmem, set->value)); }
void http_addheader(struct http_response *r, const char *name, const char *value) { struct http_channel *c = r->channel; struct http_header *h = nmem_malloc(c->nmem, sizeof *h); h->name = nmem_strdup(c->nmem, name); h->value = nmem_strdup(c->nmem, value); h->next = r->headers; r->headers = h; }
static struct gfs_listen * gfs_listen_new(const char *id, const char *address) { struct gfs_listen *n = (struct gfs_listen *) nmem_malloc(gfs_nmem, sizeof(*n)); if (id) n->id = nmem_strdup(gfs_nmem, id); else n->id = 0; n->next = 0; n->address = nmem_strdup(gfs_nmem, address); return n; }
struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len) { char tmp[MAX_HTTP_HEADER]; struct http_response *r = http_create_response(c); char *p, *p2; struct http_header **hp = &r->headers; if (len >= MAX_HTTP_HEADER) return 0; memcpy(tmp, buf, len); for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version ; p++; // Response code for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++) r->code[p2 - p] = *p2; if (!(p = strstr(tmp, "\r\n"))) return 0; p += 2; while (*p) { if (!(p2 = strstr(p, "\r\n"))) return 0; if (p == p2) // End of headers break; else { struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h)); char *value = strchr(p, ':'); if (!value) return 0; *(value++) = '\0'; h->name = nmem_strdup(c->nmem, p); while (isspace(*(const unsigned char *) value)) value++; if (value >= p2) // Empty header; { h->value = ""; p = p2 + 2; continue; } *p2 = '\0'; h->value = nmem_strdup(c->nmem, value); h->next = 0; hp = &h->next; p = p2 + 2; } } return r; }
int yaz_match_xsd_string_n_nmem(xmlNodePtr ptr, const char *elem, NMEM nmem, char **val, int *len) { #if CHECK_TYPE struct _xmlAttr *attr; #endif if (!yaz_match_xsd_element(ptr, elem)) return 0; #if CHECK_TYPE for (attr = ptr->properties; attr; attr = attr->next) if (!strcmp(attr->name, "type") && attr->children && attr->children->type == XML_TEXT_NODE) { const char *t = strchr(attr->children->content, ':'); if (t) t = t + 1; else t = attr->children->content; if (!strcmp(t, "string")) break; } if (!attr) return 0; #endif ptr = ptr->children; if (!ptr || ptr->type != XML_TEXT_NODE) { *val = ""; return 1; } *val = nmem_strdup(nmem, (const char *) ptr->content); if (len) *len = xmlStrlen(ptr->content); return 1; }
normalize_record_t normalize_cache_get(normalize_cache_t nc, struct conf_service *service, const char *spec) { normalize_record_t nt; struct cached_item *ci; yaz_mutex_enter(nc->mutex); for (ci = nc->items; ci; ci = ci->next) if (!strcmp(spec, ci->spec)) break; if (ci) nt = ci->nt; else { nt = normalize_record_create(service, spec); if (nt) { ci = nmem_malloc(nc->nmem, sizeof(*ci)); ci->next = nc->items; nc->items = ci; ci->nt = nt; ci->spec = nmem_strdup(nc->nmem, spec); } } yaz_mutex_leave(nc->mutex); return nt; }
struct database *new_database_inherit_settings(const char *id, NMEM nmem, struct settings_array *service_settings) { struct database *db; struct setting *idset; db = nmem_malloc(nmem, sizeof(*db)); db->id = nmem_strdup(nmem, id); db->next = 0; if (service_settings && service_settings->num_settings > 0) { yaz_log(YLOG_DEBUG, "copying settings from service to database %s settings", db->id); db->num_settings = service_settings->num_settings; db->settings = nmem_malloc(nmem, sizeof(*db->settings) * db->num_settings); // Initialize database settings with service settings memcpy(db->settings, service_settings->settings, sizeof(*db->settings) * db->num_settings); } else { yaz_log(YLOG_DEBUG, "No service settings to database %s ", db->id); db->num_settings = PZ_MAX_EOF; db->settings = nmem_malloc(nmem, sizeof(*db->settings) * db->num_settings); memset(db->settings, 0, sizeof(*db->settings) * db->num_settings); } idset = nmem_malloc(nmem, sizeof(*idset)); idset->precedence = 0; idset->name = "pz:id"; idset->target = idset->value = db->id; idset->next = db->settings[PZ_ID]; db->settings[PZ_ID] = idset; return db; }
// Decode settings parameters and apply to session // Syntax: setting[target]=value static int process_settings(struct session *se, struct http_request *rq, struct http_response *rs) { struct http_argument *a; for (a = rq->arguments; a; a = a->next) if (strchr(a->name, '[')) { char **res; int num; char *dbname; char *setting; // Nmem_strsplit *rules*!!! nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num); if (num != 2) { error(rs, PAZPAR2_MALFORMED_SETTING, a->name); return -1; } setting = res[0]; dbname = res[1]; session_apply_setting(se, dbname, setting, nmem_strdup(se->session_nmem, a->value)); } return 0; }
static void *construct_select(const xmlNode *ptr, const char *path, WRBUF wr_error) { if (strcmp((const char *) ptr->name, "select")) return 0; else { NMEM nmem = nmem_create(); struct select_info *info = nmem_malloc(nmem, sizeof(*info)); const char *attr_str; const char *xpath = 0; info->nmem = nmem; info->xpath_expr = 0; attr_str = yaz_xml_get_prop(ptr, "path%s", &xpath); if (attr_str) { wrbuf_printf(wr_error, "Bad attribute '%s'" "Expected xpath.", attr_str); nmem_destroy(nmem); return 0; } if (xpath) info->xpath_expr = nmem_strdup(nmem, xpath); return info; } }
/* Return charset, lang, selected from negotiation.. Client side */ void yaz_get_response_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p, char **charset, char **lang, int *selected) { Z_TargetResponse *res = p->u.response; if (charset && res->which == Z_TargetResponse_private && res->u.zprivate->which == Z_PrivateCharacterSet_externallySpecified) { Z_External *pext = res->u.zprivate->u.externallySpecified; if (pext->which == Z_External_octet) { *charset = (char *) nmem_malloc(mem, (1+pext->u.octet_aligned->len)*sizeof(char)); memcpy(*charset, pext->u.octet_aligned->buf, pext->u.octet_aligned->len); (*charset)[pext->u.octet_aligned->len] = 0; } } if (charset && res->which == Z_TargetResponse_iso10646) *charset = set_form(res->u.iso10646->encodingLevel); if (lang && res->selectedLanguage) *lang = nmem_strdup(mem, res->selectedLanguage); if (selected && res->recordsInSelectedCharSets) *selected = *res->recordsInSelectedCharSets; }
static void response_close(struct http_channel *c, const char *command) { struct http_response *rs = c->response; wrbuf_printf(c->wrbuf, "</%s>", command); rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf)); http_send_response(c); }
static void pull_terms(struct relevance *res, struct ccl_rpn_node *n) { char **words; int numwords; char *ccl_field; int i; switch (n->kind) { case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT: case CCL_RPN_PROX: pull_terms(res, n->u.p[0]); pull_terms(res, n->u.p[1]); break; case CCL_RPN_TERM: nmem_strsplit(res->nmem, " ", n->u.t.term, &words, &numwords); for (i = 0; i < numwords; i++) { const char *norm_str; ccl_field = nmem_strdup_null(res->nmem, n->u.t.qual); pp2_charset_token_first(res->prt, words[i], 0); while ((norm_str = pp2_charset_token_next(res->prt))) { struct word_entry **e = &res->entries; while (*e) e = &(*e)->next; *e = nmem_malloc(res->nmem, sizeof(**e)); (*e)->norm_str = nmem_strdup(res->nmem, norm_str); (*e)->ccl_field = ccl_field; (*e)->termno = res->vec_len++; (*e)->display_str = nmem_strdup(res->nmem, words[i]); (*e)->next = 0; } } break; default: break; } }
void yaz_set_esn(Z_RecordComposition **comp_p, const char *esn, NMEM nmem) { Z_RecordComposition *comp = (Z_RecordComposition *) nmem_malloc(nmem, sizeof(*comp)); comp->which = Z_RecordComp_simple; comp->u.simple = (Z_ElementSetNames *) nmem_malloc(nmem, sizeof(*comp->u.simple)); comp->u.simple->which = Z_ElementSetNames_generic; comp->u.simple->u.generic = nmem_strdup(nmem, esn); *comp_p = comp; }
static void show_raw_record_ok(void *data, const char *buf, size_t sz) { http_channel_observer_t obs = data; struct http_channel *c = http_channel_observer_chan(obs); struct http_response *rs = c->response; http_remove_observer(obs); wrbuf_write(c->wrbuf, buf, sz); rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf)); http_send_response(c); }
void yaz_marc_add_datafield(yaz_marc_t mt, const char *tag, const char *indicator, size_t indicator_len) { struct yaz_marc_node *n = yaz_marc_add_node(mt); n->which = YAZ_MARC_DATAFIELD; n->u.datafield.tag = nmem_strdup(mt->nmem, tag); n->u.datafield.indicator = nmem_strdupn(mt->nmem, indicator, indicator_len); n->u.datafield.subfields = 0; /* make subfield_pp the current (last one) */ mt->subfield_pp = &n->u.datafield.subfields; }
static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz) { http_channel_observer_t obs = data; struct http_channel *c = http_channel_observer_chan(obs); struct http_response *rs = c->response; http_remove_observer(obs); wrbuf_write(c->wrbuf, buf, sz); rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf)); rs->content_type = "application/octet-stream"; http_send_response(c); }
static void error(struct http_response *rs, enum pazpar2_error_code code, const char *addinfo) { struct http_channel *c = rs->channel; WRBUF text = wrbuf_alloc(); const char *http_status = "417"; const char *msg = get_msg(code); rs->msg = nmem_strdup(c->nmem, msg); strcpy(rs->code, http_status); wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code, msg); if (addinfo) wrbuf_xmlputs(text, addinfo); wrbuf_puts(text, "</error>"); yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status, msg, addinfo ? ": " : "" , addinfo ? addinfo : ""); rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text)); wrbuf_destroy(text); http_send_response(c); }
static void http_error(struct http_channel *hc, int no, const char *msg) { struct http_response *rs = http_create_response(hc); hc->response = rs; hc->keep_alive = 0; // not keeping this HTTP session alive sprintf(rs->code, "%d", no); rs->msg = nmem_strdup(hc->nmem, msg); rs->payload = nmem_malloc(hc->nmem, 100); yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n", no, msg); http_send_response(hc); }
void yaz_marc_add_controlfield(yaz_marc_t mt, const char *tag, const char *data, size_t data_len) { struct yaz_marc_node *n = yaz_marc_add_node(mt); n->which = YAZ_MARC_CONTROLFIELD; n->u.controlfield.tag = nmem_strdup(mt->nmem, tag); n->u.controlfield.data = nmem_strdupn(mt->nmem, data, data_len); if (mt->debug) { size_t i; char msg[80]; sprintf(msg, "controlfield:"); for (i = 0; i < 16 && i < data_len; i++) sprintf(msg + strlen(msg), " %02X", data[i] & 0xff); if (i < data_len) sprintf(msg + strlen(msg), " .."); yaz_marc_add_comment(mt, msg); } }
static void extract_user_pass(NMEM nmem, const char *uri, char **uri_lean, char **http_user, char **http_pass) { const char *cp1 = strchr(uri, '/'); *uri_lean = 0; *http_user = 0; *http_pass = 0; if (cp1 && cp1 > uri) { cp1--; if (!strncmp(cp1, "://", 3)) { const char *cp3 = 0; const char *cp2 = cp1 + 3; while (*cp2 && *cp2 != '/' && *cp2 != '@') { if (*cp2 == ':') cp3 = cp2; cp2++; } if (*cp2 == '@' && cp3) { *uri_lean = nmem_malloc(nmem, strlen(uri) + 1); memcpy(*uri_lean, uri, cp1 + 3 - uri); strcpy(*uri_lean + (cp1 + 3 - uri), cp2 + 1); *http_user = nmem_strdupn(nmem, cp1 + 3, cp3 - (cp1 + 3)); *http_pass = nmem_strdupn(nmem, cp3 + 1, cp2 - (cp3 + 1)); } } } if (*uri_lean == 0) *uri_lean = nmem_strdup(nmem, uri); }
void yaz_marc_add_comment(yaz_marc_t mt, char *comment) { struct yaz_marc_node *n = yaz_marc_add_node(mt); n->which = YAZ_MARC_COMMENT; n->u.comment = nmem_strdup(mt->nmem, comment); }
static void *construct_marc(const xmlNode *ptr, const char *path, WRBUF wr_error) { NMEM nmem = nmem_create(); struct marc_info *info = nmem_malloc(nmem, sizeof(*info)); struct _xmlAttr *attr; const char *input_format = 0; const char *output_format = 0; if (strcmp((const char *) ptr->name, "marc")) { nmem_destroy(nmem); return 0; } info->nmem = nmem; info->input_charset = 0; info->output_charset = 0; info->input_format_mode = 0; info->output_format_mode = 0; info->leader_spec = 0; for (attr = ptr->properties; attr; attr = attr->next) { if (!xmlStrcmp(attr->name, BAD_CAST "inputcharset") && attr->children && attr->children->type == XML_TEXT_NODE) info->input_charset = (const char *) attr->children->content; else if (!xmlStrcmp(attr->name, BAD_CAST "outputcharset") && attr->children && attr->children->type == XML_TEXT_NODE) info->output_charset = (const char *) attr->children->content; else if (!xmlStrcmp(attr->name, BAD_CAST "inputformat") && attr->children && attr->children->type == XML_TEXT_NODE) input_format = (const char *) attr->children->content; else if (!xmlStrcmp(attr->name, BAD_CAST "outputformat") && attr->children && attr->children->type == XML_TEXT_NODE) output_format = (const char *) attr->children->content; else if (!xmlStrcmp(attr->name, BAD_CAST "leaderspec") && attr->children && attr->children->type == XML_TEXT_NODE) info->leader_spec = nmem_strdup(info->nmem,(const char *) attr->children->content); else { wrbuf_printf(wr_error, "Element <marc>: expected attributes" "'inputformat', 'inputcharset', 'outputformat' or" " 'outputcharset', got attribute '%s'", attr->name); nmem_destroy(info->nmem); return 0; } } if (!input_format) { wrbuf_printf(wr_error, "Element <marc>: " "attribute 'inputformat' required"); nmem_destroy(info->nmem); return 0; } else if (!strcmp(input_format, "marc")) { info->input_format_mode = YAZ_MARC_ISO2709; } else if (!strcmp(input_format, "xml")) { info->input_format_mode = YAZ_MARC_MARCXML; /** Libxml2 generates UTF-8 encoding by default . So we convert from UTF-8 to outputcharset (if defined) */ if (!info->input_charset && info->output_charset) info->input_charset = "utf-8"; } else if (!strcmp(input_format, "json")) { info->input_format_mode = YAZ_MARC_JSON; } else { wrbuf_printf(wr_error, "Element <marc inputformat='%s'>: " " Unsupported input format" " defined by attribute value", input_format); nmem_destroy(info->nmem); return 0; } if (!output_format) { wrbuf_printf(wr_error, "Element <marc>: attribute 'outputformat' required"); nmem_destroy(info->nmem); return 0; } else if (!strcmp(output_format, "line")) { info->output_format_mode = YAZ_MARC_LINE; } else if (!strcmp(output_format, "marcxml")) { info->output_format_mode = YAZ_MARC_MARCXML; if (info->input_charset && !info->output_charset) info->output_charset = "utf-8"; } else if (!strcmp(output_format, "turbomarc")) { info->output_format_mode = YAZ_MARC_TURBOMARC; if (info->input_charset && !info->output_charset) info->output_charset = "utf-8"; } else if (!strcmp(output_format, "marc")) { info->output_format_mode = YAZ_MARC_ISO2709; } else if (!strcmp(output_format, "marcxchange")) { info->output_format_mode = YAZ_MARC_XCHANGE; if (info->input_charset && !info->output_charset) info->output_charset = "utf-8"; } else if (!strcmp(output_format, "json")) { info->output_format_mode = YAZ_MARC_JSON; if (info->input_charset && !info->output_charset) info->output_charset = "utf-8"; } else { wrbuf_printf(wr_error, "Element <marc outputformat='%s'>: " " Unsupported output format" " defined by attribute value", output_format); nmem_destroy(info->nmem); return 0; } if (info->input_charset && info->output_charset) { yaz_iconv_t cd = yaz_iconv_open(info->output_charset, info->input_charset); if (!cd) { wrbuf_printf(wr_error, "Element <marc inputcharset='%s' outputcharset='%s'>:" " Unsupported character set mapping" " defined by attribute values", info->input_charset, info->output_charset); nmem_destroy(info->nmem); return 0; } yaz_iconv_close(cd); } else if (!info->output_charset) { wrbuf_printf(wr_error, "Element <marc>: " "attribute 'outputcharset' missing"); nmem_destroy(info->nmem); return 0; } else if (!info->input_charset) { wrbuf_printf(wr_error, "Element <marc>: " "attribute 'inputcharset' missing"); nmem_destroy(info->nmem); return 0; } info->input_charset = nmem_strdup(info->nmem, info->input_charset); info->output_charset = nmem_strdup(info->nmem, info->output_charset); return info; }
static void *construct_xslt(const xmlNode *ptr, const char *path, WRBUF wr_error) { struct _xmlAttr *attr; const char *stylesheet = 0; struct xslt_info *info = 0; NMEM nmem = 0; int max_parms = 10; int no_parms = 0; if (strcmp((const char *) ptr->name, "xslt")) return 0; for (attr = ptr->properties; attr; attr = attr->next) { if (!xmlStrcmp(attr->name, BAD_CAST "stylesheet") && attr->children && attr->children->type == XML_TEXT_NODE) stylesheet = (const char *) attr->children->content; else { wrbuf_printf(wr_error, "Bad attribute '%s'" "Expected stylesheet.", attr->name); return 0; } } nmem = nmem_create(); info = nmem_malloc(nmem, sizeof(*info)); info->nmem = nmem; info->xsl_parms = nmem_malloc( nmem, (2 * max_parms + 1) * sizeof(*info->xsl_parms)); for (ptr = ptr->children; ptr; ptr = ptr->next) { const char *name = 0; const char *value = 0; char *qvalue = 0; if (ptr->type != XML_ELEMENT_NODE) continue; if (strcmp((const char *) ptr->name, "param")) { wrbuf_printf(wr_error, "Bad element '%s'" "Expected param.", ptr->name); nmem_destroy(nmem); return 0; } for (attr = ptr->properties; attr; attr = attr->next) { if (!xmlStrcmp(attr->name, BAD_CAST "name") && attr->children && attr->children->type == XML_TEXT_NODE) name = (const char *) attr->children->content; else if (!xmlStrcmp(attr->name, BAD_CAST "value") && attr->children && attr->children->type == XML_TEXT_NODE) value = (const char *) attr->children->content; else { wrbuf_printf(wr_error, "Bad attribute '%s'" "Expected name or value.", attr->name); nmem_destroy(nmem); return 0; } } if (!name || !value) { wrbuf_printf(wr_error, "Missing attributes name or value"); nmem_destroy(nmem); return 0; } if (no_parms >= max_parms) { wrbuf_printf(wr_error, "Too many parameters given"); nmem_destroy(nmem); return 0; } qvalue = nmem_malloc(nmem, strlen(value) + 3); strcpy(qvalue, "\'"); strcat(qvalue, value); strcat(qvalue, "\'"); info->xsl_parms[2 * no_parms] = nmem_strdup(nmem, name); info->xsl_parms[2 * no_parms + 1] = qvalue; no_parms++; } info->xsl_parms[2 * no_parms] = 0; if (!stylesheet) { wrbuf_printf(wr_error, "Element <xslt>: " "attribute 'stylesheet' expected"); nmem_destroy(nmem); } else { char fullpath[1024]; xsltStylesheetPtr xsp; if (!yaz_filepath_resolve(stylesheet, path, 0, fullpath)) { wrbuf_printf(wr_error, "Element <xslt stylesheet=\"%s\"/>:" " could not locate stylesheet '%s'", stylesheet, stylesheet); if (path) wrbuf_printf(wr_error, " with path '%s'", path); nmem_destroy(nmem); return 0; } info->xsp_doc = xmlParseFile(fullpath); if (!info->xsp_doc) { wrbuf_printf(wr_error, "Element: <xslt stylesheet=\"%s\"/>:" " xml parse failed: %s", stylesheet, fullpath); if (path) wrbuf_printf(wr_error, " with path '%s'", path); nmem_destroy(nmem); return 0; } /* need to copy this before passing it to the processor. It will be encapsulated in the xsp and destroyed by xsltFreeStylesheet */ xsp = xsltParseStylesheetDoc(xmlCopyDoc(info->xsp_doc, 1)); if (!xsp) { wrbuf_printf(wr_error, "Element: <xslt stylesheet=\"%s\"/>:" " xslt parse failed: %s", stylesheet, fullpath); if (path) wrbuf_printf(wr_error, " with path '%s'", path); wrbuf_printf(wr_error, " (" #if YAZ_HAVE_EXSLT "EXSLT enabled" #else "EXSLT not supported" #endif ")"); xmlFreeDoc(info->xsp_doc); nmem_destroy(info->nmem); } else { xsltFreeStylesheet(xsp); return info; } } return 0; }
/* Get charsets, langs, selected from negotiation.. Server side */ void yaz_get_proposal_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p, char ***charsets, int *num_charsets, char ***langs, int *num_langs, int *selected) { int i; Z_OriginProposal *pro = p->u.proposal; if (num_charsets && charsets) { if (pro->num_proposedCharSets) { *num_charsets = pro->num_proposedCharSets; (*charsets) = (char **) nmem_malloc(mem, pro->num_proposedCharSets * sizeof(char *)); for (i = 0; i < pro->num_proposedCharSets; i++) { (*charsets)[i] = 0; if (pro->proposedCharSets[i]->which == Z_OriginProposal_0_private && pro->proposedCharSets[i]->u.zprivate->which == Z_PrivateCharacterSet_externallySpecified) { Z_External *pext = pro->proposedCharSets[i]->u.zprivate->u.externallySpecified; if (pext->which == Z_External_octet) { (*charsets)[i] = (char *) nmem_malloc(mem, (1+pext->u.octet_aligned->len) * sizeof(char)); memcpy((*charsets)[i], pext->u.octet_aligned->buf, pext->u.octet_aligned->len); (*charsets)[i][pext->u.octet_aligned->len] = 0; } } else if (pro->proposedCharSets[i]->which == Z_OriginProposal_0_iso10646) (*charsets)[i] = set_form( pro->proposedCharSets[i]->u.iso10646->encodingLevel); } } else *num_charsets = 0; } if (langs && num_langs) { if (pro->num_proposedlanguages) { *num_langs = pro->num_proposedlanguages; (*langs) = (char **) nmem_malloc(mem, pro->num_proposedlanguages * sizeof(char *)); for (i = 0; i < pro->num_proposedlanguages; i++) (*langs)[i] = nmem_strdup(mem, pro->proposedlanguages[i]); } else *num_langs = 0; } if (pro->recordsInSelectedCharSets && selected) *selected = *pro->recordsInSelectedCharSets; }
static int cql_transform_parse_tok_line(cql_transform_t ct, const char *pattern, yaz_tok_parse_t tp) { int ae_num = 0; Z_AttributeElement *ae[20]; int ret = 0; /* 0=OK, != 0 FAIL */ int t; t = yaz_tok_move(tp); while (t == YAZ_TOK_STRING && ae_num < 20) { WRBUF type_str = wrbuf_alloc(); WRBUF set_str = 0; Z_AttributeElement *elem = 0; const char *value_str = 0; /* attset type=value OR type=value */ elem = (Z_AttributeElement *) nmem_malloc(ct->nmem, sizeof(*elem)); elem->attributeSet = 0; ae[ae_num] = elem; wrbuf_puts(ct->w, yaz_tok_parse_string(tp)); wrbuf_puts(type_str, yaz_tok_parse_string(tp)); t = yaz_tok_move(tp); if (t == YAZ_TOK_EOF) { wrbuf_destroy(type_str); if (set_str) wrbuf_destroy(set_str); break; } if (t == YAZ_TOK_STRING) { wrbuf_puts(ct->w, " "); wrbuf_puts(ct->w, yaz_tok_parse_string(tp)); set_str = type_str; elem->attributeSet = yaz_string_to_oid_nmem(yaz_oid_std(), CLASS_ATTSET, wrbuf_cstr(set_str), ct->nmem); type_str = wrbuf_alloc(); wrbuf_puts(type_str, yaz_tok_parse_string(tp)); t = yaz_tok_move(tp); } elem->attributeType = nmem_intdup(ct->nmem, 0); if (sscanf(wrbuf_cstr(type_str), ODR_INT_PRINTF, elem->attributeType) != 1) { wrbuf_destroy(type_str); if (set_str) wrbuf_destroy(set_str); yaz_log(YLOG_WARN, "Expected numeric attribute type"); ret = -1; break; } wrbuf_destroy(type_str); if (set_str) wrbuf_destroy(set_str); if (t != '=') { yaz_log(YLOG_WARN, "Expected = after after attribute type"); ret = -1; break; } t = yaz_tok_move(tp); if (t != YAZ_TOK_STRING) /* value */ { yaz_log(YLOG_WARN, "Missing attribute value"); ret = -1; break; } value_str = yaz_tok_parse_string(tp); if (yaz_isdigit(*value_str)) { elem->which = Z_AttributeValue_numeric; elem->value.numeric = nmem_intdup(ct->nmem, atoi(value_str)); } else { Z_ComplexAttribute *ca = (Z_ComplexAttribute *) nmem_malloc(ct->nmem, sizeof(*ca)); elem->which = Z_AttributeValue_complex; elem->value.complex = ca; ca->num_list = 1; ca->list = (Z_StringOrNumeric **) nmem_malloc(ct->nmem, sizeof(Z_StringOrNumeric *)); ca->list[0] = (Z_StringOrNumeric *) nmem_malloc(ct->nmem, sizeof(Z_StringOrNumeric)); ca->list[0]->which = Z_StringOrNumeric_string; ca->list[0]->u.string = nmem_strdup(ct->nmem, value_str); ca->num_semanticAction = 0; ca->semanticAction = 0; } wrbuf_puts(ct->w, "="); wrbuf_puts(ct->w, yaz_tok_parse_string(tp)); t = yaz_tok_move(tp); wrbuf_puts(ct->w, " "); ae_num++; } if (ret == 0) /* OK? */ { struct cql_prop_entry **pp = &ct->entry; while (*pp) pp = &(*pp)->next; *pp = (struct cql_prop_entry *) xmalloc(sizeof(**pp)); (*pp)->pattern = xstrdup(pattern); (*pp)->value = xstrdup(wrbuf_cstr(ct->w)); (*pp)->attr_list.num_attributes = ae_num; if (ae_num == 0) (*pp)->attr_list.attributes = 0; else { (*pp)->attr_list.attributes = (Z_AttributeElement **) nmem_malloc(ct->nmem, ae_num * sizeof(Z_AttributeElement *)); memcpy((*pp)->attr_list.attributes, ae, ae_num * sizeof(Z_AttributeElement *)); } (*pp)->next = 0; if (0) { ODR pr = odr_createmem(ODR_PRINT); Z_AttributeList *alp = &(*pp)->attr_list; odr_setprint(pr, yaz_log_file()); z_AttributeList(pr, &alp, 0, 0); odr_setprint(pr, 0); odr_destroy(pr); } } return ret; }
char *odr_strdup(ODR o, const char *str) { return nmem_strdup(o->mem, str); }
struct http_request *http_parse_request(struct http_channel *c, struct http_buf **queue, int len) { struct http_request *r = nmem_malloc(c->nmem, sizeof(*r)); char *p, *p2; char *start = nmem_malloc(c->nmem, len+1); char *buf = start; if (http_buf_read(c->http_server, queue, buf, len) < len) { yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len); return 0; } r->search = ""; r->channel = c; r->arguments = 0; r->headers = 0; r->content_buf = 0; r->content_len = 0; // Parse first line for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++) *(p2++) = *p; if (*p != ' ') { yaz_log(YLOG_WARN, "Unexpected HTTP method in request"); return 0; } *p2 = '\0'; if (!(buf = strchr(buf, ' '))) { yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request"); return 0; } buf++; if (!(p = strchr(buf, ' '))) { yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)"); return 0; } *(p++) = '\0'; if ((p2 = strchr(buf, '?'))) // Do we have arguments? *(p2++) = '\0'; r->path = nmem_strdup(c->nmem, buf); if (p2) { r->search = nmem_strdup(c->nmem, p2); // Parse Arguments http_parse_arguments(r, c->nmem, p2); } buf = p; if (strncmp(buf, "HTTP/", 5)) strcpy(r->http_version, "1.0"); else { size_t skipped; buf += 5; // strlen("HTTP/") p = (char*) next_crlf(buf, &skipped); if (!p || skipped < 3 || skipped > 5) return 0; memcpy(r->http_version, buf, skipped); r->http_version[skipped] = '\0'; buf = p; } strcpy(c->version, r->http_version); r->headers = 0; while (*buf) { size_t skipped; p = (char *) next_crlf(buf, &skipped); if (!p) { return 0; } else if (skipped == 0) { buf = p; break; } else { char *cp; char *n_v = nmem_malloc(c->nmem, skipped+1); struct http_header *h = nmem_malloc(c->nmem, sizeof(*h)); memcpy(n_v, buf, skipped); n_v[skipped] = '\0'; if (!(cp = strchr(n_v, ':'))) return 0; h->name = nmem_strdupn(c->nmem, n_v, cp - n_v); cp++; while (isspace(*cp)) cp++; h->value = nmem_strdup(c->nmem, cp); h->next = r->headers; r->headers = h; buf = p; } } // determine if we do keep alive if (!strcmp(c->version, "1.0")) { const char *v = http_lookup_header(r->headers, "Connection"); if (v && !strcmp(v, "Keep-Alive")) c->keep_alive = 1; else c->keep_alive = 0; } else { const char *v = http_lookup_header(r->headers, "Connection"); if (v && !strcmp(v, "close")) c->keep_alive = 0; else c->keep_alive = 1; } if (buf < start + len) { const char *content_type = http_lookup_header(r->headers, "Content-Type"); r->content_len = start + len - buf; r->content_buf = buf; if (!yaz_strcmp_del("application/x-www-form-urlencoded", content_type, "; ")) { http_parse_arguments(r, c->nmem, r->content_buf); } } return r; }
static void tst_convert3(void) { NMEM nmem = nmem_create(); int ret; yaz_record_conv_t p = 0; const char *iso2709_rec = "\x30\x30\x30\x37\x37\x6E\x61\x6D\x20\x20\x32\x32\x30\x30\x30\x34" "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30" "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x34\x30\x30\x30\x31\x33" "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20" "\x1F\x61\x6b\xb2\x62\x65\x6e\x68\x61\x76\x6e\x1E\x1D"; const char *opacxml_rec = "<opacRecord>\n" " <bibliographicRecord>\n" "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n" " <leader>00077nam a22000498a 4500</leader>\n" " <controlfield tag=\"001\"> 11224466 </controlfield>\n" " <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n" " <subfield code=\"a\">k" "\xc3" "\xb8" /* oslash in UTF_8 */ "benhavn</subfield>\n" " </datafield>\n" "</record>\n" " </bibliographicRecord>\n" "<holdings>\n" " <holding>\n" " <typeOfRecord>u</typeOfRecord>\n" " <encodingLevel>U</encodingLevel>\n" " <receiptAcqStatus>0</receiptAcqStatus>\n" " <dateOfReport>000000</dateOfReport>\n" " <nucCode>s-FM/GC</nucCode>\n" " <localLocation>Main or Science/Business Reading Rms - STORED OFFSITE</localLocation>\n" " <callNumber>MLCM 89/00602 (N)</callNumber>\n" " <shelvingData>FT MEADE</shelvingData>\n" " <copyNumber>Copy 1</copyNumber>\n" " <volumes>\n" " <volume>\n" " <enumeration>1</enumeration>\n" " <chronology>2</chronology>\n" " <enumAndChron>3</enumAndChron>\n" " </volume>\n" " <volume>\n" " <enumeration>1</enumeration>\n" " <chronology>2</chronology>\n" " <enumAndChron>3</enumAndChron>\n" " </volume>\n" " </volumes>\n" " <circulations>\n" " <circulation>\n" " <availableNow value=\"1\"/>\n" " <availabilityDate>20130129</availabilityDate>\n" " <itemId>1226176</itemId>\n" " <renewable value=\"0\"/>\n" " <onHold value=\"0\"/>\n" " </circulation>\n" " </circulations>\n" " </holding>\n" "</holdings>\n" "</opacRecord>\n"; Z_OPACRecord *z_opac = nmem_malloc(nmem, sizeof(*z_opac)); Z_HoldingsAndCircData *h; Z_CircRecord *circ; z_opac->bibliographicRecord = z_ext_record_oid_nmem(nmem, yaz_oid_recsyn_usmarc, iso2709_rec, strlen(iso2709_rec)); z_opac->num_holdingsData = 1; z_opac->holdingsData = (Z_HoldingsRecord **) nmem_malloc(nmem, sizeof(Z_HoldingsRecord *) * 1); z_opac->holdingsData[0] = (Z_HoldingsRecord *) nmem_malloc(nmem, sizeof(Z_HoldingsRecord)); z_opac->holdingsData[0]->which = Z_HoldingsRecord_holdingsAndCirc; h = z_opac->holdingsData[0]->u.holdingsAndCirc = (Z_HoldingsAndCircData *) nmem_malloc(nmem, sizeof(*h)); h->typeOfRecord = nmem_strdup(nmem, "u"); h->encodingLevel = nmem_strdup(nmem, "U"); h->format = 0; h->receiptAcqStatus = nmem_strdup(nmem, "0"); h->generalRetention = 0; h->completeness = 0; h->dateOfReport = nmem_strdup(nmem, "000000"); h->nucCode = nmem_strdup(nmem, "s-FM/GC"); h->localLocation = nmem_strdup(nmem, "Main or Science/Business Reading " "Rms - STORED OFFSITE"); h->shelvingLocation = 0; h->callNumber = nmem_strdup(nmem, "MLCM 89/00602 (N)"); h->shelvingData = nmem_strdup(nmem, "FT MEADE"); h->copyNumber = nmem_strdup(nmem, "Copy 1"); h->publicNote = 0; h->reproductionNote = 0; h->termsUseRepro = 0; h->enumAndChron = 0; h->num_volumes = 2; h->volumes = 0; h->volumes = (Z_Volume **) nmem_malloc(nmem, 2 * sizeof(Z_Volume *)); h->volumes[0] = (Z_Volume *) nmem_malloc(nmem, sizeof(Z_Volume)); h->volumes[1] = h->volumes[0]; h->volumes[0]->enumeration = nmem_strdup(nmem, "1"); h->volumes[0]->chronology = nmem_strdup(nmem, "2"); h->volumes[0]->enumAndChron = nmem_strdup(nmem, "3"); h->num_circulationData = 1; h->circulationData = (Z_CircRecord **) nmem_malloc(nmem, 1 * sizeof(Z_CircRecord *)); circ = h->circulationData[0] = (Z_CircRecord *) nmem_malloc(nmem, sizeof(Z_CircRecord)); circ->availableNow = nmem_booldup(nmem, 1); circ->availablityDate = nmem_strdup(nmem, "20130129"); circ->availableThru = 0; circ->restrictions = 0; circ->itemId = nmem_strdup(nmem, "1226176"); circ->renewable = nmem_booldup(nmem, 0); circ->onHold = nmem_booldup(nmem, 0); circ->enumAndChron = 0; circ->midspine = 0; circ->temporaryLocation = 0; YAZ_CHECK(conv_configure_test("<backend>" "<marc" " inputcharset=\"marc-8\"" " outputcharset=\"utf-8\"" " inputformat=\"marc\"" " outputformat=\"marcxml\"" "/>" "</backend>", 0, &p)); if (p) { WRBUF output_record = wrbuf_alloc(); ret = yaz_record_conv_opac_record(p, z_opac, output_record); YAZ_CHECK(ret == 0); if (ret == 0) { ret = strcmp(wrbuf_cstr(output_record), opacxml_rec); YAZ_CHECK(ret == 0); if (ret) { printf("got-output_record len=%ld: %s\n", (long) wrbuf_len(output_record), wrbuf_cstr(output_record)); printf("output_expect_record len=%ld %s\n", (long) strlen(opacxml_rec), opacxml_rec); } } yaz_record_conv_destroy(p); wrbuf_destroy(output_record); } { Z_OPACRecord *opac = 0; yaz_marc_t mt = yaz_marc_create(); ret = yaz_xml_to_opac(mt, opacxml_rec, strlen(opacxml_rec), &opac, 0 /* iconv */, nmem, 0); YAZ_CHECK(ret); YAZ_CHECK(opac); if (opac) { WRBUF output_record = wrbuf_alloc(); char *p; yaz_marc_xml(mt, YAZ_MARC_MARCXML); yaz_opac_decode_wrbuf(mt, opac, output_record); /* change MARC size to 00077 from 00078, due to encoding of the aring (two bytes in UTF-8) */ p = strstr(wrbuf_buf(output_record), "00078"); YAZ_CHECK(p); if (p) p[4] = '7'; ret = strcmp(wrbuf_cstr(output_record), opacxml_rec); YAZ_CHECK(ret == 0); if (ret) { printf("got-output_record len=%ld: %s\n", (long) wrbuf_len(output_record), wrbuf_cstr(output_record)); printf("output_expect_record len=%ld %s\n", (long) strlen(opacxml_rec), opacxml_rec); } wrbuf_destroy(output_record); } yaz_marc_destroy(mt); } nmem_destroy(nmem); }