1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2011 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"
47 void print_meminfo(WRBUF wrbuf)
49 struct mallinfo minfo;
51 wrbuf_printf(wrbuf, " <memory>\n"
52 " <arena>%d</arena>\n"
53 " <uordblks>%d</uordblks>\n"
54 " <fordblks>%d</fordblks>\n"
55 " <ordblks>%d</ordblks>\n"
56 " <keepcost>%d</keepcost>\n"
57 " <hblks>%d</hblks>\n"
58 " <hblkhd>%d</hblkhd>\n"
60 " <virtuse>%d</virtuse>\n"
62 minfo.arena, minfo.uordblks, minfo.fordblks,minfo.ordblks, minfo.keepcost, minfo.hblks, minfo.hblkhd, minfo.arena + minfo.hblkhd, minfo.uordblks + minfo.hblkhd);
66 #define print_meminfo(x)
70 // Update this when the protocol changes
71 #define PAZPAR2_PROTOCOL_VERSION "1"
73 #define HTTP_COMMAND_RESPONSE_PREFIX "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
76 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
77 struct session *psession;
78 unsigned int session_id;
83 http_sessions_t http_sessions;
84 struct http_session *next;
87 struct http_sessions {
88 struct http_session *session_list;
93 static YAZ_MUTEX g_http_session_mutex = 0;
94 static int g_http_sessions = 0;
96 int http_session_use(int delta)
99 if (!g_http_session_mutex)
100 yaz_mutex_create(&g_http_session_mutex);
101 yaz_mutex_enter(g_http_session_mutex);
102 g_http_sessions += delta;
103 sessions = g_http_sessions;
104 yaz_mutex_leave(g_http_session_mutex);
105 yaz_log(YLOG_DEBUG, "%s sessions=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), sessions);
110 http_sessions_t http_sessions_create(void)
112 http_sessions_t hs = xmalloc(sizeof(*hs));
113 hs->session_list = 0;
115 pazpar2_mutex_create(&hs->mutex, "http_sessions");
116 hs->log_level = yaz_log_module_level("HTTP");
120 void http_sessions_destroy(http_sessions_t hs)
124 struct http_session *s = hs->session_list;
127 struct http_session *s_next = s->next;
128 iochan_destroy(s->timeout_iochan);
129 destroy_session(s->psession);
130 nmem_destroy(s->nmem);
133 yaz_mutex_destroy(&hs->mutex);
138 void http_session_destroy(struct http_session *s);
140 static void session_timeout(IOCHAN i, int event)
142 struct http_session *s = iochan_getdata(i);
143 http_session_destroy(s);
146 struct http_session *http_session_create(struct conf_service *service,
147 http_sessions_t http_sessions,
150 NMEM nmem = nmem_create();
151 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
154 sprintf(tmp_str, "session#%u", sesid);
155 r->psession = new_session(nmem, service, sesid);
156 r->session_id = sesid;
159 r->destroy_counter = r->activity_counter = 0;
160 r->http_sessions = http_sessions;
162 yaz_mutex_enter(http_sessions->mutex);
163 r->next = http_sessions->session_list;
164 http_sessions->session_list = r;
165 yaz_mutex_leave(http_sessions->mutex);
167 r->timeout_iochan = iochan_create(-1, session_timeout, 0, "http_session_timeout");
168 iochan_setdata(r->timeout_iochan, r);
169 yaz_log(http_sessions->log_level, "%p Session %u created. timeout chan=%p timeout=%d", r, sesid, r->timeout_iochan, service->session_timeout);
170 iochan_settimeout(r->timeout_iochan, service->session_timeout);
172 iochan_add(service->server->iochan_man, r->timeout_iochan);
177 void http_session_destroy(struct http_session *s)
179 int must_destroy = 0;
181 http_sessions_t http_sessions = s->http_sessions;
183 yaz_log(http_sessions->log_level, "%p HTTP Session %u destroyed", s, s->session_id);
184 yaz_mutex_enter(http_sessions->mutex);
185 /* only if http_session has no active http sessions on it can be destroyed */
186 if (s->destroy_counter == s->activity_counter)
188 struct http_session **p = 0;
190 for (p = &http_sessions->session_list; *p; p = &(*p)->next)
197 yaz_mutex_leave(http_sessions->mutex);
199 { /* destroying for real */
200 yaz_log(http_sessions->log_level, "%p HTTP Session %u destroyed", s, s->session_id);
201 iochan_destroy(s->timeout_iochan);
202 destroy_session(s->psession);
203 http_session_use(-1);
204 nmem_destroy(s->nmem);
207 yaz_log(http_sessions->log_level, "%p HTTP Session %u destroyed delayed. Active clients (%d-%d). Waiting for new timeout.",
208 s, s->session_id, s->activity_counter, s->destroy_counter);
213 static const char *get_msg(enum pazpar2_error_code code)
215 struct pazpar2_error_msg {
216 enum pazpar2_error_code code;
219 static const struct pazpar2_error_msg ar[] = {
220 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
221 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
222 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
223 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
224 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
225 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
226 { PAZPAR2_RECORD_MISSING, "Record missing"},
227 { PAZPAR2_NO_TARGETS, "No targets"},
228 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
229 { PAZPAR2_RECORD_FAIL, "Record command failed"},
230 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
231 { PAZPAR2_NO_SERVICE, "No service"},
232 { PAZPAR2_LAST_ERROR, "Last error"},
238 if (code == ar[i].code)
245 static void error(struct http_response *rs,
246 enum pazpar2_error_code code,
249 struct http_channel *c = rs->channel;
250 WRBUF text = wrbuf_alloc();
251 const char *http_status = "417";
252 const char *msg = get_msg(code);
254 rs->msg = nmem_strdup(c->nmem, msg);
255 strcpy(rs->code, http_status);
257 wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
260 wrbuf_xmlputs(text, addinfo);
261 wrbuf_puts(text, "</error>");
263 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
264 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
265 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
267 http_send_response(c);
270 unsigned int make_sessionid(void)
272 static int seq = 0; /* thread pr */
276 if (global_parameters.predictable_sessions)
285 if (gettimeofday(&t, 0) < 0)
287 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
290 /* at most 256 sessions per second ..
291 (long long would be more appropriate)*/
293 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
299 static struct http_session *locate_session(struct http_channel *c)
301 struct http_response *rs = c->response;
302 struct http_request *rq = c->request;
303 struct http_session *p;
304 const char *session = http_argbyname(rq, "session");
305 http_sessions_t http_sessions = c->http_sessions;
310 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
314 yaz_mutex_enter(http_sessions->mutex);
315 for (p = http_sessions->session_list; p; p = p->next)
316 if (id == p->session_id)
319 p->activity_counter++;
320 yaz_mutex_leave(http_sessions->mutex);
322 iochan_activity(p->timeout_iochan);
324 error(rs, PAZPAR2_NO_SESSION, session);
328 // Call after use of locate_session, in order to increment the destroy_counter
329 static void release_session(struct http_channel *c,
330 struct http_session *session)
332 http_sessions_t http_sessions = c->http_sessions;
333 yaz_mutex_enter(http_sessions->mutex);
335 session->destroy_counter++;
336 yaz_mutex_leave(http_sessions->mutex);
339 // Decode settings parameters and apply to session
340 // Syntax: setting[target]=value
341 static int process_settings(struct session *se, struct http_request *rq,
342 struct http_response *rs)
344 struct http_argument *a;
346 for (a = rq->arguments; a; a = a->next)
347 if (strchr(a->name, '['))
354 // Nmem_strsplit *rules*!!!
355 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
358 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
363 session_apply_setting(se, dbname, setting,
364 nmem_strdup(se->session_nmem, a->value));
369 static void cmd_exit(struct http_channel *c)
372 struct http_response *rs = c->response;
373 yaz_log(YLOG_WARN, "exit");
374 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX "<exit><status>OK</status></exit>");
375 rs->payload = nmem_strdup(c->nmem, buf);
376 http_send_response(c);
377 http_close_server(c->server);
380 static void cmd_init(struct http_channel *c)
383 struct http_request *r = c->request;
384 const char *clear = http_argbyname(r, "clear");
385 const char *content_type = http_lookup_header(r->headers, "Content-Type");
387 struct http_session *s;
388 struct http_response *rs = c->response;
389 struct conf_service *service = 0; /* no service (yet) */
391 if (r->content_len && content_type &&
392 !yaz_strcmp_del("text/xml", content_type, "; "))
394 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
398 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
401 root_n = xmlDocGetRootElement(doc);
402 service = service_create(c->server, root_n);
406 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
413 const char *service_name = http_argbyname(c->request, "service");
414 service = locate_service(c->server, service_name);
417 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
421 sesid = make_sessionid();
422 s = http_session_create(service, c->http_sessions, sesid);
424 yaz_log(c->http_sessions->log_level, "%p Session init %u ", s, sesid);
425 if (!clear || *clear == '0')
426 session_init_databases(s->psession);
428 yaz_log(YLOG_LOG, "HTTP Session %u init: No databases preloaded", sesid);
430 if (process_settings(s->psession, c->request, c->response) < 0)
433 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX
434 "<init><status>OK</status><session>%d", sesid);
435 if (c->server->server_id)
438 strcat(buf, c->server->server_id);
440 strcat(buf, "</session>"
441 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
442 rs->payload = nmem_strdup(c->nmem, buf);
443 http_send_response(c);
446 static void apply_local_setting(void *client_data,
449 struct session *se = (struct session *) client_data;
451 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
452 nmem_strdup(se->session_nmem, set->name),
453 nmem_strdup(se->session_nmem, set->value));
456 static void cmd_settings(struct http_channel *c)
458 struct http_response *rs = c->response;
459 struct http_request *rq = c->request;
460 struct http_session *s = locate_session(c);
461 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
466 if (rq->content_len && content_type &&
467 !yaz_strcmp_del("text/xml", content_type, "; "))
469 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
473 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
476 root_n = xmlDocGetRootElement(doc);
478 settings_read_node_x(root_n, s->psession, apply_local_setting);
482 if (process_settings(s->psession, rq, rs) < 0)
484 release_session(c, s);
487 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
488 http_send_response(c);
489 release_session(c, s);
492 static void cmd_termlist(struct http_channel *c)
494 struct http_response *rs = c->response;
495 struct http_request *rq = c->request;
496 struct http_session *s = locate_session(c);
497 const char *name = http_argbyname(rq, "name");
498 const char *nums = http_argbyname(rq, "num");
505 status = session_active_clients(s->psession);
510 wrbuf_rewind(c->wrbuf);
512 wrbuf_puts(c->wrbuf, "<termlist>\n");
513 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
515 perform_termlist(c, s->psession, name, num);
517 wrbuf_puts(c->wrbuf, "</termlist>\n");
518 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
519 http_send_response(c);
520 release_session(c, s);
523 size_t session_get_memory_status(struct session *session);
525 static void session_status(struct http_channel *c, struct http_session *s)
528 wrbuf_printf(c->wrbuf, "<http_count>%u</http_count>\n", s->activity_counter);
529 wrbuf_printf(c->wrbuf, "<http_nmem>%zu</http_nmem>\n", nmem_total(s->nmem) );
530 session_nmem = session_get_memory_status(s->psession);
531 wrbuf_printf(c->wrbuf, "<session_nmem>%zu</session_nmem>\n", session_nmem);
534 static void cmd_session_status(struct http_channel *c)
536 struct http_response *rs = c->response;
537 struct http_session *s = locate_session(c);
541 wrbuf_rewind(c->wrbuf);
542 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<sessionstatus><status>OK</status>\n");
543 session_status(c, s);
544 wrbuf_puts(c->wrbuf, "</sessionstatus>\n");
545 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
546 http_send_response(c);
547 release_session(c, s);
551 int sessions_count(void);
552 int clients_count(void);
553 #ifdef HAVE_RESULTSETS_COUNT
554 int resultsets_count(void);
556 #define resultsets_count() 0
559 static void cmd_server_status(struct http_channel *c)
561 struct http_response *rs = c->response;
562 int sessions = sessions_count();
563 int clients = clients_count();
564 int resultsets = resultsets_count();
565 wrbuf_rewind(c->wrbuf);
566 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<server-status>\n");
567 wrbuf_printf(c->wrbuf, " <sessions>%u</sessions>\n", sessions);
568 wrbuf_printf(c->wrbuf, " <clients>%u</clients>\n", clients);
569 /* Only works if yaz has been compiled with enabling of this */
570 wrbuf_printf(c->wrbuf, " <resultsets>%u</resultsets>\n",resultsets);
571 print_meminfo(c->wrbuf);
573 /* TODO add all sessions status */
574 /* http_sessions_t http_sessions = c->http_sessions; */
575 /* struct http_session *p; */
577 yaz_mutex_enter(http_sessions->mutex);
578 for (p = http_sessions->session_list; p; p = p->next)
580 p->activity_counter++;
581 wrbuf_puts(c->wrbuf, "<session-status>\n");
582 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", p->session_id);
583 yaz_mutex_leave(http_sessions->mutex);
584 session_status(c, p);
585 wrbuf_puts(c->wrbuf, "</session-status>\n");
586 yaz_mutex_enter(http_sessions->mutex);
587 p->activity_counter--;
589 yaz_mutex_leave(http_sessions->mutex);
591 wrbuf_puts(c->wrbuf, "</server-status>\n");
592 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
593 http_send_response(c);
598 static void cmd_bytarget(struct http_channel *c)
600 struct http_response *rs = c->response;
601 struct http_request *rq = c->request;
602 struct http_session *s = locate_session(c);
603 struct hitsbytarget *ht;
604 const char *settings = http_argbyname(rq, "settings");
609 ht = get_hitsbytarget(s->psession, &count, c->nmem);
610 wrbuf_rewind(c->wrbuf);
611 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
613 for (i = 0; i < count; i++)
615 wrbuf_puts(c->wrbuf, "\n<target>");
617 wrbuf_puts(c->wrbuf, "<id>");
618 wrbuf_xmlputs(c->wrbuf, ht[i].id);
619 wrbuf_puts(c->wrbuf, "</id>\n");
621 if (ht[i].name && ht[i].name[0])
623 wrbuf_puts(c->wrbuf, "<name>");
624 wrbuf_xmlputs(c->wrbuf, ht[i].name);
625 wrbuf_puts(c->wrbuf, "</name>\n");
628 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
629 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
630 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
632 wrbuf_puts(c->wrbuf, "<state>");
633 wrbuf_xmlputs(c->wrbuf, ht[i].state);
634 wrbuf_puts(c->wrbuf, "</state>\n");
635 if (settings && *settings == '1')
637 wrbuf_puts(c->wrbuf, "<settings>\n");
638 wrbuf_puts(c->wrbuf, ht[i].settings_xml);
639 wrbuf_puts(c->wrbuf, "</settings>\n");
641 wrbuf_puts(c->wrbuf, "</target>");
644 wrbuf_puts(c->wrbuf, "</bytarget>");
645 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
646 http_send_response(c);
647 release_session(c, s);
650 static void write_metadata(WRBUF w, struct conf_service *service,
651 struct record_metadata **ml, int full)
655 for (imeta = 0; imeta < service->num_metadata; imeta++)
657 struct conf_metadata *cmd = &service->metadata[imeta];
658 struct record_metadata *md;
659 if (!cmd->brief && !full)
661 for (md = ml[imeta]; md; md = md->next)
663 struct record_metadata_attr *attr = md->attributes;
664 wrbuf_printf(w, "\n<md-%s", cmd->name);
666 for (; attr; attr = attr->next)
668 wrbuf_printf(w, " %s=\"", attr->name);
669 wrbuf_xmlputs(w, attr->value);
675 case Metadata_type_generic:
676 wrbuf_xmlputs(w, md->data.text.disp);
678 case Metadata_type_year:
679 wrbuf_printf(w, "%d", md->data.number.min);
680 if (md->data.number.min != md->data.number.max)
681 wrbuf_printf(w, "-%d", md->data.number.max);
684 wrbuf_puts(w, "[can't represent]");
686 wrbuf_printf(w, "</md-%s>", cmd->name);
691 static void write_subrecord(struct record *r, WRBUF w,
692 struct conf_service *service, int show_details)
694 const char *name = session_setting_oneval(
695 client_get_database(r->client), PZ_NAME);
697 wrbuf_puts(w, "<location id=\"");
698 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
699 wrbuf_puts(w, "\" ");
701 wrbuf_puts(w, "name=\"");
702 wrbuf_xmlputs(w, *name ? name : "Unknown");
703 wrbuf_puts(w, "\">");
705 write_metadata(w, service, r->metadata, show_details);
706 wrbuf_puts(w, "</location>\n");
709 static void show_raw_record_error(void *data, const char *addinfo)
711 http_channel_observer_t obs = data;
712 struct http_channel *c = http_channel_observer_chan(obs);
713 struct http_response *rs = c->response;
715 http_remove_observer(obs);
717 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
720 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
722 http_channel_observer_t obs = data;
723 struct http_channel *c = http_channel_observer_chan(obs);
724 struct http_response *rs = c->response;
726 http_remove_observer(obs);
728 wrbuf_write(c->wrbuf, buf, sz);
729 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
730 http_send_response(c);
734 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
736 http_channel_observer_t obs = data;
737 struct http_channel *c = http_channel_observer_chan(obs);
738 struct http_response *rs = c->response;
740 http_remove_observer(obs);
742 wrbuf_write(c->wrbuf, buf, sz);
743 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
745 rs->content_type = "application/octet-stream";
746 http_send_response(c);
750 void show_raw_reset(void *data, struct http_channel *c, void *data2)
752 //struct client *client = data;
753 //client_show_raw_remove(client, data2);
756 static void cmd_record_ready(void *data);
758 static void cmd_record(struct http_channel *c)
760 struct http_response *rs = c->response;
761 struct http_request *rq = c->request;
762 struct http_session *s = locate_session(c);
763 struct record_cluster *rec, *prev_r, *next_r;
765 struct conf_service *service;
766 const char *idstr = http_argbyname(rq, "id");
767 const char *offsetstr = http_argbyname(rq, "offset");
768 const char *binarystr = http_argbyname(rq, "binary");
772 service = s->psession->service;
775 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
778 wrbuf_rewind(c->wrbuf);
779 if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
781 if (session_active_clients(s->psession) == 0)
783 error(rs, PAZPAR2_RECORD_MISSING, idstr);
785 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
786 cmd_record_ready, c, c) != 0)
788 error(rs, PAZPAR2_RECORD_MISSING, idstr);
790 release_session(c, s);
795 int offset = atoi(offsetstr);
796 const char *syntax = http_argbyname(rq, "syntax");
797 const char *esn = http_argbyname(rq, "esn");
799 struct record*r = rec->records;
802 if (binarystr && *binarystr != '0')
805 for (i = 0; i < offset && r; r = r->next, i++)
809 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
813 http_channel_observer_t obs =
814 http_add_observer(c, r->client, show_raw_reset);
815 int ret = client_show_raw_begin(r->client, r->position,
818 show_raw_record_error,
820 show_raw_record_ok_binary :
825 http_remove_observer(obs);
826 error(rs, PAZPAR2_NO_SESSION, 0);
832 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
833 wrbuf_puts(c->wrbuf, "<recid>");
834 wrbuf_xmlputs(c->wrbuf, rec->recid);
835 wrbuf_puts(c->wrbuf, "</recid>\n");
838 wrbuf_puts(c->wrbuf, "<prevrecid>");
839 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
840 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
844 wrbuf_puts(c->wrbuf, "<nextrecid>");
845 wrbuf_xmlputs(c->wrbuf, next_r->recid);
846 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
848 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
849 session_active_clients(s->psession));
850 write_metadata(c->wrbuf, service, rec->metadata, 1);
851 for (r = rec->records; r; r = r->next)
852 write_subrecord(r, c->wrbuf, service, 1);
853 wrbuf_puts(c->wrbuf, "</record>\n");
854 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
855 http_send_response(c);
857 show_single_stop(s->psession, rec);
858 release_session(c, s);
861 static void cmd_record_ready(void *data)
863 struct http_channel *c = (struct http_channel *) data;
868 static void show_records(struct http_channel *c, int active)
870 struct http_request *rq = c->request;
871 struct http_response *rs = c->response;
872 struct http_session *s = locate_session(c);
873 struct record_cluster **rl;
874 struct reclist_sortparms *sp;
875 const char *start = http_argbyname(rq, "start");
876 const char *num = http_argbyname(rq, "num");
877 const char *sort = http_argbyname(rq, "sort");
887 // We haven't counted clients yet if we're called on a block release
889 active = session_active_clients(s->psession);
892 startn = atoi(start);
897 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
899 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
900 release_session(c, s);
905 rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits);
907 wrbuf_rewind(c->wrbuf);
908 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
909 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
910 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
911 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
912 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
913 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
915 for (i = 0; i < numn; i++)
919 struct record_cluster *rec = rl[i];
920 struct conf_service *service = s->psession->service;
922 wrbuf_puts(c->wrbuf, "<hit>\n");
923 write_metadata(c->wrbuf, service, rec->metadata, 0);
924 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
925 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
927 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
928 if (strstr(sort, "relevance"))
929 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
930 rec->relevance_score);
931 wrbuf_puts(c->wrbuf, "<recid>");
932 wrbuf_xmlputs(c->wrbuf, rec->recid);
933 wrbuf_puts(c->wrbuf, "</recid>\n");
934 wrbuf_puts(c->wrbuf, "</hit>\n");
937 show_range_stop(s->psession, rl);
939 wrbuf_puts(c->wrbuf, "</show>\n");
940 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
941 http_send_response(c);
942 release_session(c, s);
945 static void show_records_ready(void *data)
947 struct http_channel *c = (struct http_channel *) data;
952 static void cmd_show(struct http_channel *c)
954 struct http_request *rq = c->request;
955 struct http_session *s = locate_session(c);
956 const char *block = http_argbyname(rq, "block");
962 status = session_active_clients(s->psession);
966 if (!strcmp(block, "preferred") && !session_is_preferred_clients_ready(s->psession) && reclist_get_num_records(s->psession->reclist) == 0)
968 // if there is already a watch/block. we do not block this one
969 if (session_set_watch(s->psession, SESSION_WATCH_SHOW_PREF,
970 show_records_ready, c, c) != 0)
972 yaz_log(c->http_sessions->log_level,
973 "%p Session %u: Blocking on cmd_show. Waiting for preferred targets", s, s->session_id);
975 release_session(c, s);
979 else if (status && reclist_get_num_records(s->psession->reclist) == 0)
981 // if there is already a watch/block. we do not block this one
982 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
983 show_records_ready, c, c) != 0)
985 yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on cmd_show", s, s->session_id);
987 release_session(c, s);
991 show_records(c, status);
992 release_session(c, s);
995 static void cmd_ping(struct http_channel *c)
997 struct http_response *rs = c->response;
998 struct http_session *s = locate_session(c);
1001 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
1002 http_send_response(c);
1003 release_session(c, s);
1006 static void cmd_search(struct http_channel *c)
1008 struct http_request *rq = c->request;
1009 struct http_response *rs = c->response;
1010 struct http_session *s = locate_session(c);
1011 const char *query = http_argbyname(rq, "query");
1012 const char *filter = http_argbyname(rq, "filter");
1013 const char *maxrecs = http_argbyname(rq, "maxrecs");
1014 const char *startrecs = http_argbyname(rq, "startrecs");
1015 const char *limit = http_argbyname(rq, "limit");
1016 enum pazpar2_error_code code;
1017 const char *addinfo = 0;
1023 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
1024 release_session(c, s);
1027 if (!yaz_utf8_check(query))
1029 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
1030 release_session(c, s);
1033 code = search(s->psession, query, startrecs, maxrecs, filter, limit,
1037 error(rs, code, addinfo);
1038 release_session(c, s);
1041 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
1042 http_send_response(c);
1043 release_session(c, s);
1047 static void cmd_stat(struct http_channel *c)
1049 struct http_response *rs = c->response;
1050 struct http_session *s = locate_session(c);
1051 struct statistics stat;
1059 clients = session_active_clients(s->psession);
1060 statistics(s->psession, &stat);
1062 if (stat.num_clients > 0)
1064 progress = (stat.num_clients - clients) / (float)stat.num_clients;
1067 wrbuf_rewind(c->wrbuf);
1068 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
1069 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
1070 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1071 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
1072 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
1073 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
1074 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
1075 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
1076 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
1077 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
1078 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1079 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1080 wrbuf_puts(c->wrbuf, "</stat>");
1081 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1082 http_send_response(c);
1083 release_session(c, s);
1086 static void cmd_info(struct http_channel *c)
1088 char yaz_version_str[20];
1089 struct http_response *rs = c->response;
1091 wrbuf_rewind(c->wrbuf);
1092 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
1093 wrbuf_puts(c->wrbuf, " <version>\n");
1094 wrbuf_puts(c->wrbuf, " <pazpar2");
1095 #ifdef PAZPAR2_VERSION_SHA1
1096 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1098 wrbuf_puts(c->wrbuf, ">");
1099 wrbuf_xmlputs(c->wrbuf, VERSION);
1100 wrbuf_puts(c->wrbuf, "</pazpar2>");
1103 yaz_version(yaz_version_str, 0);
1104 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1105 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1106 wrbuf_puts(c->wrbuf, "\">");
1107 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1108 wrbuf_puts(c->wrbuf, "</yaz>\n");
1110 wrbuf_puts(c->wrbuf, " </version>\n");
1112 info_services(c->server, c->wrbuf);
1114 wrbuf_puts(c->wrbuf, "</info>");
1115 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1116 http_send_response(c);
1121 void (*fun)(struct http_channel *c);
1123 { "init", cmd_init },
1124 { "settings", cmd_settings },
1125 { "stat", cmd_stat },
1126 { "bytarget", cmd_bytarget },
1127 { "show", cmd_show },
1128 { "search", cmd_search },
1129 { "termlist", cmd_termlist },
1130 { "exit", cmd_exit },
1131 { "session-status", cmd_session_status },
1132 { "server-status", cmd_server_status },
1133 { "ping", cmd_ping },
1134 { "record", cmd_record },
1135 { "info", cmd_info },
1139 void http_command(struct http_channel *c)
1141 const char *command = http_argbyname(c->request, "command");
1142 struct http_response *rs = http_create_response(c);
1147 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1148 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1152 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1155 for (i = 0; commands[i].name; i++)
1156 if (!strcmp(commands[i].name, command))
1158 (*commands[i].fun)(c);
1161 if (!commands[i].name)
1162 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1170 * c-file-style: "Stroustrup"
1171 * indent-tabs-mode: nil
1173 * vim: shiftwidth=4 tabstop=8 expandtab