1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <sys/types.h>
33 #include <yaz/snprintf.h>
34 #include <yaz/yaz-util.h>
38 #include "parameters.h"
44 // Update this when the protocol changes
45 #define PAZPAR2_PROTOCOL_VERSION "1"
47 #define HTTP_COMMAND_RESPONSE_PREFIX "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
50 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
51 struct session *psession;
52 unsigned int session_id;
57 http_sessions_t http_sessions;
58 struct http_session *next;
61 struct http_sessions {
62 struct http_session *session_list;
66 http_sessions_t http_sessions_create(void)
68 http_sessions_t hs = xmalloc(sizeof(*hs));
71 pazpar2_mutex_create(&hs->mutex, "http_sessions");
75 void http_sessions_destroy(http_sessions_t hs)
79 struct http_session *s = hs->session_list;
82 struct http_session *s_next = s->next;
83 iochan_destroy(s->timeout_iochan);
84 destroy_session(s->psession);
85 nmem_destroy(s->nmem);
88 yaz_mutex_destroy(&hs->mutex);
93 void http_session_destroy(struct http_session *s);
95 static void session_timeout(IOCHAN i, int event)
97 struct http_session *s = iochan_getdata(i);
98 http_session_destroy(s);
101 struct http_session *http_session_create(struct conf_service *service,
102 http_sessions_t http_sessions,
105 NMEM nmem = nmem_create();
106 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
109 sprintf(tmp_str, "session#%u", sesid);
110 r->psession = new_session(nmem, service, tmp_str);
111 r->session_id = sesid;
114 r->destroy_counter = r->activity_counter = 0;
115 r->http_sessions = http_sessions;
117 yaz_mutex_enter(http_sessions->mutex);
118 r->next = http_sessions->session_list;
119 http_sessions->session_list = r;
120 yaz_mutex_leave(http_sessions->mutex);
122 r->timeout_iochan = iochan_create(-1, session_timeout, 0, "http_session_timeout");
123 iochan_setdata(r->timeout_iochan, r);
124 yaz_log(YLOG_LOG, "timeout=%d", service->session_timeout);
125 iochan_settimeout(r->timeout_iochan, service->session_timeout);
127 iochan_add(service->server->iochan_man, r->timeout_iochan);
131 void http_session_destroy(struct http_session *s)
133 int must_destroy = 1;
135 http_sessions_t http_sessions = s->http_sessions;
137 yaz_log(YLOG_LOG, "http_session_destroy %u", s->session_id);
138 yaz_mutex_enter(http_sessions->mutex);
140 /* only if http_session destroy was already called, we will allow it
142 if (s->destroy_counter != s->activity_counter)
145 /* only if there are no active Z39.50 clients we will allow it to be
147 if (session_active_clients(s->psession))
150 s->destroy_counter = s->activity_counter = 0;
153 struct http_session **p = 0;
154 for (p = &http_sessions->session_list; *p; p = &(*p)->next)
161 yaz_mutex_leave(http_sessions->mutex);
163 { /* destroying for real */
164 yaz_log(YLOG_LOG, "Destroying session %u", s->session_id);
165 iochan_destroy(s->timeout_iochan);
166 destroy_session(s->psession);
167 nmem_destroy(s->nmem);
170 yaz_log(YLOG_LOG, "Active clients on session %u. Waiting for new timeout.", s->session_id);
175 static const char *get_msg(enum pazpar2_error_code code)
177 struct pazpar2_error_msg {
178 enum pazpar2_error_code code;
181 static const struct pazpar2_error_msg ar[] = {
182 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
183 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
184 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
185 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
186 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
187 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
188 { PAZPAR2_RECORD_MISSING, "Record missing"},
189 { PAZPAR2_NO_TARGETS, "No targets"},
190 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
191 { PAZPAR2_RECORD_FAIL, "Record command failed"},
192 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
193 { PAZPAR2_NO_SERVICE, "No service"},
194 { PAZPAR2_LAST_ERROR, "Last error"},
200 if (code == ar[i].code)
207 static void error(struct http_response *rs,
208 enum pazpar2_error_code code,
211 struct http_channel *c = rs->channel;
212 WRBUF text = wrbuf_alloc();
213 const char *http_status = "417";
214 const char *msg = get_msg(code);
216 rs->msg = nmem_strdup(c->nmem, msg);
217 strcpy(rs->code, http_status);
219 wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
222 wrbuf_xmlputs(text, addinfo);
223 wrbuf_puts(text, "</error>");
225 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
226 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
227 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
229 http_send_response(c);
232 unsigned int make_sessionid(void)
234 static int seq = 0; /* thread pr */
238 if (global_parameters.debug_mode)
247 if (gettimeofday(&t, 0) < 0)
249 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
252 /* at most 256 sessions per second ..
253 (long long would be more appropriate)*/
255 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
261 static struct http_session *locate_session(struct http_channel *c)
263 struct http_response *rs = c->response;
264 struct http_request *rq = c->request;
265 struct http_session *p;
266 const char *session = http_argbyname(rq, "session");
267 http_sessions_t http_sessions = c->http_sessions;
272 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
276 yaz_mutex_enter(http_sessions->mutex);
277 for (p = http_sessions->session_list; p; p = p->next)
278 if (id == p->session_id)
281 p->activity_counter++;
282 yaz_mutex_leave(http_sessions->mutex);
284 iochan_activity(p->timeout_iochan);
286 error(rs, PAZPAR2_NO_SESSION, session);
290 // Decode settings parameters and apply to session
291 // Syntax: setting[target]=value
292 static int process_settings(struct session *se, struct http_request *rq,
293 struct http_response *rs)
295 struct http_argument *a;
297 for (a = rq->arguments; a; a = a->next)
298 if (strchr(a->name, '['))
305 // Nmem_strsplit *rules*!!!
306 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
309 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
314 session_apply_setting(se, dbname, setting,
315 nmem_strdup(se->session_nmem, a->value));
320 static void cmd_exit(struct http_channel *c)
322 yaz_log(YLOG_WARN, "exit");
323 http_close_server(c->server);
326 static void cmd_init(struct http_channel *c)
329 struct http_request *r = c->request;
330 const char *clear = http_argbyname(r, "clear");
331 const char *content_type = http_lookup_header(r->headers, "Content-Type");
333 struct http_session *s;
334 struct http_response *rs = c->response;
335 struct conf_service *service = 0; /* no service (yet) */
337 if (r->content_len && content_type &&
338 !yaz_strcmp_del("text/xml", content_type, "; "))
340 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
344 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
347 root_n = xmlDocGetRootElement(doc);
348 service = service_create(c->server, root_n);
352 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
359 const char *service_name = http_argbyname(c->request, "service");
360 service = locate_service(c->server, service_name);
363 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
367 sesid = make_sessionid();
368 s = http_session_create(service, c->http_sessions, sesid);
370 yaz_log(YLOG_DEBUG, "HTTP Session init");
371 if (!clear || *clear == '0')
372 session_init_databases(s->psession);
374 yaz_log(YLOG_LOG, "No databases preloaded");
376 if (process_settings(s->psession, c->request, c->response) < 0)
379 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX
380 "<init><status>OK</status><session>%d", sesid);
381 if (c->server->server_id)
384 strcat(buf, c->server->server_id);
386 strcat(buf, "</session>"
387 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
388 rs->payload = nmem_strdup(c->nmem, buf);
389 http_send_response(c);
392 static void apply_local_setting(void *client_data,
395 struct session *se = (struct session *) client_data;
397 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
398 nmem_strdup(se->session_nmem, set->name),
399 nmem_strdup(se->session_nmem, set->value));
402 static void cmd_settings(struct http_channel *c)
404 struct http_response *rs = c->response;
405 struct http_request *rq = c->request;
406 struct http_session *s = locate_session(c);
407 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
412 if (rq->content_len && content_type &&
413 !yaz_strcmp_del("text/xml", content_type, "; "))
415 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
419 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
422 root_n = xmlDocGetRootElement(doc);
424 settings_read_node_x(root_n, s->psession, apply_local_setting);
428 if (process_settings(s->psession, rq, rs) < 0)
430 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
431 http_send_response(c);
434 // Compares two hitsbytarget nodes by hitcount
435 static int cmp_ht(const void *p1, const void *p2)
437 const struct hitsbytarget *h1 = p1;
438 const struct hitsbytarget *h2 = p2;
439 return h2->hits - h1->hits;
442 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
443 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
446 struct hitsbytarget *ht;
449 ht = hitsbytarget(se, &count, nmem);
450 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
451 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
454 // do only print terms which have display names
456 wrbuf_puts(wrbuf, "<term>\n");
458 wrbuf_puts(wrbuf, "<id>");
459 wrbuf_xmlputs(wrbuf, ht[i].id);
460 wrbuf_puts(wrbuf, "</id>\n");
462 wrbuf_puts(wrbuf, "<name>");
463 if (!ht[i].name || !ht[i].name[0])
464 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
466 wrbuf_xmlputs(wrbuf, ht[i].name);
467 wrbuf_puts(wrbuf, "</name>\n");
469 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
472 wrbuf_puts(wrbuf, "<state>");
473 wrbuf_xmlputs(wrbuf, ht[i].state);
474 wrbuf_puts(wrbuf, "</state>\n");
476 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
478 wrbuf_puts(wrbuf, "</term>\n");
482 static void cmd_termlist(struct http_channel *c)
484 struct http_response *rs = c->response;
485 struct http_request *rq = c->request;
486 struct http_session *s = locate_session(c);
487 struct termlist_score **p;
490 const char *name = http_argbyname(rq, "name");
491 const char *nums = http_argbyname(rq, "num");
498 status = session_active_clients(s->psession);
502 if (strlen(name) > 255)
507 wrbuf_rewind(c->wrbuf);
509 wrbuf_puts(c->wrbuf, "<termlist>\n");
510 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
516 if (!(tp = strchr(name, ',')))
517 tp = name + strlen(name);
518 strncpy(tname, name, tp - name);
519 tname[tp - name] = '\0';
521 wrbuf_puts(c->wrbuf, "<list name=\"");
522 wrbuf_xmlputs(c->wrbuf, tname);
523 wrbuf_puts(c->wrbuf, "\">\n");
524 if (!strcmp(tname, "xtargets"))
525 targets_termlist(c->wrbuf, s->psession, num, c->nmem);
528 p = termlist(s->psession, tname, &len);
530 for (i = 0; i < len && i < num; i++){
531 // prevnt sending empty term elements
532 if (!p[i]->term || !p[i]->term[0])
535 wrbuf_puts(c->wrbuf, "<term>");
536 wrbuf_puts(c->wrbuf, "<name>");
537 wrbuf_xmlputs(c->wrbuf, p[i]->term);
538 wrbuf_puts(c->wrbuf, "</name>");
540 wrbuf_printf(c->wrbuf,
541 "<frequency>%d</frequency>",
543 wrbuf_puts(c->wrbuf, "</term>\n");
546 wrbuf_puts(c->wrbuf, "</list>\n");
551 wrbuf_puts(c->wrbuf, "</termlist>\n");
552 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
553 http_send_response(c);
557 static void cmd_bytarget(struct http_channel *c)
559 struct http_response *rs = c->response;
560 struct http_request *rq = c->request;
561 struct http_session *s = locate_session(c);
562 struct hitsbytarget *ht;
563 const char *settings = http_argbyname(rq, "settings");
568 ht = hitsbytarget(s->psession, &count, c->nmem);
569 wrbuf_rewind(c->wrbuf);
570 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
572 for (i = 0; i < count; i++)
574 wrbuf_puts(c->wrbuf, "\n<target>");
576 wrbuf_puts(c->wrbuf, "<id>");
577 wrbuf_xmlputs(c->wrbuf, ht[i].id);
578 wrbuf_puts(c->wrbuf, "</id>\n");
580 if (ht[i].name && ht[i].name[0])
582 wrbuf_puts(c->wrbuf, "<name>");
583 wrbuf_xmlputs(c->wrbuf, ht[i].name);
584 wrbuf_puts(c->wrbuf, "</name>\n");
587 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
588 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
589 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
591 wrbuf_puts(c->wrbuf, "<state>");
592 wrbuf_xmlputs(c->wrbuf, ht[i].state);
593 wrbuf_puts(c->wrbuf, "</state>\n");
594 if (settings && *settings == '1')
596 wrbuf_puts(c->wrbuf, "<settings>\n");
597 wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
598 wrbuf_puts(c->wrbuf, "</settings>\n");
600 wrbuf_puts(c->wrbuf, "</target>");
601 wrbuf_destroy(ht[i].settings_xml);
604 wrbuf_puts(c->wrbuf, "</bytarget>");
605 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
606 http_send_response(c);
609 static void write_metadata(WRBUF w, struct conf_service *service,
610 struct record_metadata **ml, int full)
614 for (imeta = 0; imeta < service->num_metadata; imeta++)
616 struct conf_metadata *cmd = &service->metadata[imeta];
617 struct record_metadata *md;
618 if (!cmd->brief && !full)
620 for (md = ml[imeta]; md; md = md->next)
622 struct record_metadata_attr *attr = md->attributes;
623 wrbuf_printf(w, "\n<md-%s", cmd->name);
625 for (; attr; attr = attr->next)
627 wrbuf_printf(w, " %s=\"", attr->name);
628 wrbuf_xmlputs(w, attr->value);
634 case Metadata_type_generic:
635 wrbuf_xmlputs(w, md->data.text.disp);
637 case Metadata_type_year:
638 wrbuf_printf(w, "%d", md->data.number.min);
639 if (md->data.number.min != md->data.number.max)
640 wrbuf_printf(w, "-%d", md->data.number.max);
643 wrbuf_puts(w, "[can't represent]");
645 wrbuf_printf(w, "</md-%s>", cmd->name);
650 static void write_subrecord(struct record *r, WRBUF w,
651 struct conf_service *service, int show_details)
653 const char *name = session_setting_oneval(
654 client_get_database(r->client), PZ_NAME);
656 wrbuf_puts(w, "<location id=\"");
657 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
658 wrbuf_puts(w, "\" ");
660 wrbuf_puts(w, "name=\"");
661 wrbuf_xmlputs(w, *name ? name : "Unknown");
662 wrbuf_puts(w, "\">");
664 write_metadata(w, service, r->metadata, show_details);
665 wrbuf_puts(w, "</location>\n");
668 static void show_raw_record_error(void *data, const char *addinfo)
670 http_channel_observer_t obs = data;
671 struct http_channel *c = http_channel_observer_chan(obs);
672 struct http_response *rs = c->response;
674 http_remove_observer(obs);
676 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
679 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
681 http_channel_observer_t obs = data;
682 struct http_channel *c = http_channel_observer_chan(obs);
683 struct http_response *rs = c->response;
685 http_remove_observer(obs);
687 wrbuf_write(c->wrbuf, buf, sz);
688 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
689 http_send_response(c);
693 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
695 http_channel_observer_t obs = data;
696 struct http_channel *c = http_channel_observer_chan(obs);
697 struct http_response *rs = c->response;
699 http_remove_observer(obs);
701 wrbuf_write(c->wrbuf, buf, sz);
702 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
704 rs->content_type = "application/octet-stream";
705 http_send_response(c);
709 void show_raw_reset(void *data, struct http_channel *c, void *data2)
711 //struct client *client = data;
712 //client_show_raw_remove(client, data2);
715 static void cmd_record_ready(void *data);
717 static void cmd_record(struct http_channel *c)
719 struct http_response *rs = c->response;
720 struct http_request *rq = c->request;
721 struct http_session *s = locate_session(c);
722 struct record_cluster *rec, *prev_r, *next_r;
724 struct conf_service *service;
725 const char *idstr = http_argbyname(rq, "id");
726 const char *offsetstr = http_argbyname(rq, "offset");
727 const char *binarystr = http_argbyname(rq, "binary");
731 service = s->psession->service;
734 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
737 wrbuf_rewind(c->wrbuf);
738 if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
740 if (session_active_clients(s->psession) == 0)
742 error(rs, PAZPAR2_RECORD_MISSING, idstr);
744 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
745 cmd_record_ready, c, c) != 0)
747 error(rs, PAZPAR2_RECORD_MISSING, idstr);
753 int offset = atoi(offsetstr);
754 const char *syntax = http_argbyname(rq, "syntax");
755 const char *esn = http_argbyname(rq, "esn");
757 struct record*r = rec->records;
760 if (binarystr && *binarystr != '0')
763 for (i = 0; i < offset && r; r = r->next, i++)
767 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
771 http_channel_observer_t obs =
772 http_add_observer(c, r->client, show_raw_reset);
773 int ret = client_show_raw_begin(r->client, r->position,
776 show_raw_record_error,
778 show_raw_record_ok_binary :
783 http_remove_observer(obs);
784 error(rs, PAZPAR2_NO_SESSION, 0);
790 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
791 wrbuf_puts(c->wrbuf, "<recid>");
792 wrbuf_xmlputs(c->wrbuf, rec->recid);
793 wrbuf_puts(c->wrbuf, "</recid>\n");
796 wrbuf_puts(c->wrbuf, "<prevrecid>");
797 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
798 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
802 wrbuf_puts(c->wrbuf, "<nextrecid>");
803 wrbuf_xmlputs(c->wrbuf, next_r->recid);
804 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
806 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
807 session_active_clients(s->psession));
808 write_metadata(c->wrbuf, service, rec->metadata, 1);
809 for (r = rec->records; r; r = r->next)
810 write_subrecord(r, c->wrbuf, service, 1);
811 wrbuf_puts(c->wrbuf, "</record>\n");
812 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
813 http_send_response(c);
815 show_single_stop(s->psession, rec);
818 static void cmd_record_ready(void *data)
820 struct http_channel *c = (struct http_channel *) data;
825 static void show_records(struct http_channel *c, int active)
827 struct http_request *rq = c->request;
828 struct http_response *rs = c->response;
829 struct http_session *s = locate_session(c);
830 struct record_cluster **rl;
831 struct reclist_sortparms *sp;
832 const char *start = http_argbyname(rq, "start");
833 const char *num = http_argbyname(rq, "num");
834 const char *sort = http_argbyname(rq, "sort");
844 // We haven't counted clients yet if we're called on a block release
846 active = session_active_clients(s->psession);
849 startn = atoi(start);
854 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
856 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
861 rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits);
863 wrbuf_rewind(c->wrbuf);
864 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
865 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
866 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
867 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
868 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
869 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
871 for (i = 0; i < numn; i++)
875 struct record_cluster *rec = rl[i];
876 struct conf_service *service = s->psession->service;
878 wrbuf_puts(c->wrbuf, "<hit>\n");
879 write_metadata(c->wrbuf, service, rec->metadata, 0);
880 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
881 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
883 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
884 if (strstr(sort, "relevance"))
885 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
886 rec->relevance_score);
887 wrbuf_puts(c->wrbuf, "<recid>");
888 wrbuf_xmlputs(c->wrbuf, rec->recid);
889 wrbuf_puts(c->wrbuf, "</recid>\n");
890 wrbuf_puts(c->wrbuf, "</hit>\n");
893 show_range_stop(s->psession, rl);
895 wrbuf_puts(c->wrbuf, "</show>\n");
896 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
897 http_send_response(c);
900 static void show_records_ready(void *data)
902 struct http_channel *c = (struct http_channel *) data;
907 static void cmd_show(struct http_channel *c)
909 struct http_request *rq = c->request;
910 struct http_session *s = locate_session(c);
911 const char *block = http_argbyname(rq, "block");
917 status = session_active_clients(s->psession);
921 if (status && reclist_get_num_records(s->psession->reclist) == 0)
923 // if there is already a watch/block. we do not block this one
924 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
925 show_records_ready, c, c) != 0)
927 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
933 show_records(c, status);
936 static void cmd_ping(struct http_channel *c)
938 struct http_response *rs = c->response;
939 struct http_session *s = locate_session(c);
942 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
943 http_send_response(c);
946 static int utf_8_valid(const char *str)
948 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
951 /* check that query is UTF-8 encoded */
952 char *inbuf = (char *) str; /* we know iconv does not alter this */
953 size_t inbytesleft = strlen(inbuf);
955 size_t outbytesleft = strlen(inbuf) + 10;
956 char *out = xmalloc(outbytesleft);
958 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
960 /* if OK, try flushing the rest */
961 if (r != (size_t) (-1))
962 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
965 if (r == (size_t) (-1))
971 static void cmd_search(struct http_channel *c)
973 struct http_request *rq = c->request;
974 struct http_response *rs = c->response;
975 struct http_session *s = locate_session(c);
976 const char *query = http_argbyname(rq, "query");
977 const char *filter = http_argbyname(rq, "filter");
978 const char *maxrecs = http_argbyname(rq, "maxrecs");
979 const char *startrecs = http_argbyname(rq, "startrecs");
980 enum pazpar2_error_code code;
981 const char *addinfo = 0;
987 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
990 if (!utf_8_valid(query))
992 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
995 code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
998 error(rs, code, addinfo);
1001 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
1002 http_send_response(c);
1006 static void cmd_stat(struct http_channel *c)
1008 struct http_response *rs = c->response;
1009 struct http_session *s = locate_session(c);
1010 struct statistics stat;
1018 clients = session_active_clients(s->psession);
1019 statistics(s->psession, &stat);
1021 if (stat.num_clients > 0) {
1022 progress = (stat.num_clients - clients) / (float)stat.num_clients;
1025 wrbuf_rewind(c->wrbuf);
1026 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
1027 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
1028 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1029 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
1030 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
1031 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
1032 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
1033 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
1034 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
1035 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
1036 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1037 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1038 wrbuf_puts(c->wrbuf, "</stat>");
1039 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1040 http_send_response(c);
1043 static void cmd_info(struct http_channel *c)
1045 char yaz_version_str[20];
1046 struct http_response *rs = c->response;
1048 wrbuf_rewind(c->wrbuf);
1049 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
1050 wrbuf_puts(c->wrbuf, " <version>\n");
1051 wrbuf_puts(c->wrbuf, "<pazpar2");
1052 #ifdef PAZPAR2_VERSION_SHA1
1053 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1055 wrbuf_puts(c->wrbuf, ">");
1056 wrbuf_xmlputs(c->wrbuf, VERSION);
1057 wrbuf_puts(c->wrbuf, "</pazpar2>");
1060 yaz_version(yaz_version_str, 0);
1061 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1062 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1063 wrbuf_puts(c->wrbuf, "\">");
1064 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1065 wrbuf_puts(c->wrbuf, "</yaz>\n");
1067 wrbuf_puts(c->wrbuf, " </version>\n");
1069 wrbuf_puts(c->wrbuf, "</info>");
1070 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1071 http_send_response(c);
1076 void (*fun)(struct http_channel *c);
1078 { "init", cmd_init },
1079 { "settings", cmd_settings },
1080 { "stat", cmd_stat },
1081 { "bytarget", cmd_bytarget },
1082 { "show", cmd_show },
1083 { "search", cmd_search },
1084 { "termlist", cmd_termlist },
1085 { "exit", cmd_exit },
1086 { "ping", cmd_ping },
1087 { "record", cmd_record },
1088 { "info", cmd_info },
1092 void http_command(struct http_channel *c)
1094 const char *command = http_argbyname(c->request, "command");
1095 struct http_response *rs = http_create_response(c);
1100 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1101 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1105 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1108 for (i = 0; commands[i].name; i++)
1109 if (!strcmp(commands[i].name, command))
1111 (*commands[i].fun)(c);
1114 if (!commands[i].name)
1115 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1123 * c-file-style: "Stroustrup"
1124 * indent-tabs-mode: nil
1126 * vim: shiftwidth=4 tabstop=8 expandtab