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 session_destroy(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 session_destroy(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_ALREADY_BLOCKED, "Already blocked in session on: "},
233 { PAZPAR2_LAST_ERROR, "Last error"},
239 if (code == ar[i].code)
246 static void error(struct http_response *rs,
247 enum pazpar2_error_code code,
250 struct http_channel *c = rs->channel;
251 WRBUF text = wrbuf_alloc();
252 const char *http_status = "417";
253 const char *msg = get_msg(code);
255 rs->msg = nmem_strdup(c->nmem, msg);
256 strcpy(rs->code, http_status);
258 wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
261 wrbuf_xmlputs(text, addinfo);
262 wrbuf_puts(text, "</error>");
264 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
265 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
266 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
268 http_send_response(c);
271 static void response_open_no_status(struct http_channel *c, const char *command)
273 wrbuf_rewind(c->wrbuf);
274 wrbuf_printf(c->wrbuf, "%s<%s>",
275 HTTP_COMMAND_RESPONSE_PREFIX, command);
278 static void response_open(struct http_channel *c, const char *command)
280 response_open_no_status(c, command);
281 wrbuf_puts(c->wrbuf, "<status>OK</status>");
284 static void response_close(struct http_channel *c, const char *command)
286 struct http_response *rs = c->response;
288 wrbuf_printf(c->wrbuf, "</%s>", command);
289 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
290 http_send_response(c);
293 unsigned int make_sessionid(void)
295 static int seq = 0; /* thread pr */
299 if (global_parameters.predictable_sessions)
308 if (gettimeofday(&t, 0) < 0)
310 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
313 /* at most 256 sessions per second ..
314 (long long would be more appropriate)*/
316 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
322 static struct http_session *locate_session(struct http_channel *c)
324 struct http_request *rq = c->request;
325 struct http_response *rs = c->response;
326 struct http_session *p;
327 const char *session = http_argbyname(rq, "session");
328 http_sessions_t http_sessions = c->http_sessions;
333 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
337 yaz_mutex_enter(http_sessions->mutex);
338 for (p = http_sessions->session_list; p; p = p->next)
339 if (id == p->session_id)
342 p->activity_counter++;
343 yaz_mutex_leave(http_sessions->mutex);
345 iochan_activity(p->timeout_iochan);
347 error(rs, PAZPAR2_NO_SESSION, session);
351 // Call after use of locate_session, in order to increment the destroy_counter
352 static void release_session(struct http_channel *c,
353 struct http_session *session)
355 http_sessions_t http_sessions = c->http_sessions;
356 yaz_mutex_enter(http_sessions->mutex);
358 session->destroy_counter++;
359 yaz_mutex_leave(http_sessions->mutex);
362 // Decode settings parameters and apply to session
363 // Syntax: setting[target]=value
364 static int process_settings(struct session *se, struct http_request *rq,
365 struct http_response *rs)
367 struct http_argument *a;
369 for (a = rq->arguments; a; a = a->next)
370 if (strchr(a->name, '['))
377 // Nmem_strsplit *rules*!!!
378 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
381 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
386 session_apply_setting(se, dbname, setting,
387 nmem_strdup(se->session_nmem, a->value));
392 static void cmd_exit(struct http_channel *c)
394 yaz_log(YLOG_WARN, "exit");
396 response_open(c, "exit");
397 response_close(c, "exit");
398 http_close_server(c->server);
401 static void cmd_init(struct http_channel *c)
403 struct http_request *r = c->request;
404 const char *clear = http_argbyname(r, "clear");
405 const char *content_type = http_lookup_header(r->headers, "Content-Type");
407 struct http_session *s;
408 struct http_response *rs = c->response;
409 struct conf_service *service = 0; /* no service (yet) */
411 if (r->content_len && content_type &&
412 !yaz_strcmp_del("text/xml", content_type, "; "))
414 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
418 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
421 root_n = xmlDocGetRootElement(doc);
422 service = service_create(c->server, root_n);
426 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
433 const char *service_name = http_argbyname(c->request, "service");
434 service = locate_service(c->server, service_name);
437 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
441 sesid = make_sessionid();
442 s = http_session_create(service, c->http_sessions, sesid);
444 yaz_log(c->http_sessions->log_level, "%p Session init %u ", s, sesid);
445 if (!clear || *clear == '0')
446 session_init_databases(s->psession);
448 yaz_log(YLOG_LOG, "HTTP Session %u init: No databases preloaded", sesid);
450 if (process_settings(s->psession, c->request, c->response) < 0)
453 response_open(c, "init");
454 wrbuf_printf(c->wrbuf, "<session>%d", sesid);
455 if (c->server->server_id)
457 wrbuf_puts(c->wrbuf, ".");
458 wrbuf_puts(c->wrbuf, c->server->server_id);
460 wrbuf_puts(c->wrbuf, "</session>"
461 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol>");
463 response_close(c, "init");
466 static void apply_local_setting(void *client_data,
469 struct session *se = (struct session *) client_data;
471 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
472 nmem_strdup(se->session_nmem, set->name),
473 nmem_strdup(se->session_nmem, set->value));
476 static void cmd_settings(struct http_channel *c)
478 struct http_response *rs = c->response;
479 struct http_request *rq = c->request;
480 struct http_session *s = locate_session(c);
481 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
486 if (rq->content_len && content_type &&
487 !yaz_strcmp_del("text/xml", content_type, "; "))
489 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
493 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
496 root_n = xmlDocGetRootElement(doc);
498 settings_read_node_x(root_n, s->psession, apply_local_setting);
502 if (process_settings(s->psession, rq, rs) < 0)
504 release_session(c, s);
507 response_open(c, "settings");
508 response_close(c, "settings");
509 release_session(c, s);
512 static void termlist_response(struct http_channel *c)
514 struct http_request *rq = c->request;
515 struct http_session *s = locate_session(c);
516 const char *name = http_argbyname(rq, "name");
517 const char *nums = http_argbyname(rq, "num");
524 status = session_active_clients(s->psession);
526 response_open_no_status(c, "termlist");
527 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
529 perform_termlist(c, s->psession, name, num);
531 response_close(c, "termlist");
532 release_session(c, s);
535 static void termlist_result_ready(void *data)
537 struct http_channel *c = (struct http_channel *) data;
538 yaz_log(c->http_sessions->log_level, "termlist watch released");
539 termlist_response(c);
542 static void cmd_termlist(struct http_channel *c)
544 struct http_request *rq = c->request;
545 struct http_response *rs = c->response;
546 struct http_session *s = locate_session(c);
547 const char *block = http_argbyname(rq, "block");
552 active_clients = session_active_clients(s->psession);
554 if (block && !strcmp("1", block) && active_clients)
556 // if there is already a watch/block. we do not block this one
557 if (session_set_watch(s->psession, SESSION_WATCH_TERMLIST,
558 termlist_result_ready, c, c) != 0)
560 yaz_log(YLOG_WARN, "Attempt to block multiple times on termlist block. Not supported!");
561 error(rs, PAZPAR2_ALREADY_BLOCKED, "termlist");
565 yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on command termlist", s, s->session_id);
567 release_session(c, s);
571 termlist_response(c);
572 release_session(c, s);
575 size_t session_get_memory_status(struct session *session);
577 static void session_status(struct http_channel *c, struct http_session *s)
580 wrbuf_printf(c->wrbuf, "<http_count>%u</http_count>\n", s->activity_counter);
581 wrbuf_printf(c->wrbuf, "<http_nmem>%zu</http_nmem>\n", nmem_total(s->nmem) );
582 session_nmem = session_get_memory_status(s->psession);
583 wrbuf_printf(c->wrbuf, "<session_nmem>%zu</session_nmem>\n", session_nmem);
586 static void cmd_session_status(struct http_channel *c)
588 struct http_session *s = locate_session(c);
592 response_open(c, "session-status");
593 session_status(c, s);
594 response_close(c, "session-status");
595 release_session(c, s);
598 int sessions_count(void);
599 int clients_count(void);
600 #ifdef HAVE_RESULTSETS_COUNT
601 int resultsets_count(void);
603 #define resultsets_count() 0
606 static void cmd_server_status(struct http_channel *c)
608 int sessions = sessions_count();
609 int clients = clients_count();
610 int resultsets = resultsets_count();
612 response_open(c, "server-status");
613 wrbuf_printf(c->wrbuf, "\n <sessions>%u</sessions>\n", sessions);
614 wrbuf_printf(c->wrbuf, " <clients>%u</clients>\n", clients);
615 /* Only works if yaz has been compiled with enabling of this */
616 wrbuf_printf(c->wrbuf, " <resultsets>%u</resultsets>\n",resultsets);
617 print_meminfo(c->wrbuf);
619 /* TODO add all sessions status */
620 /* http_sessions_t http_sessions = c->http_sessions; */
621 /* struct http_session *p; */
623 yaz_mutex_enter(http_sessions->mutex);
624 for (p = http_sessions->session_list; p; p = p->next)
626 p->activity_counter++;
627 wrbuf_puts(c->wrbuf, "<session-status>\n");
628 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", p->session_id);
629 yaz_mutex_leave(http_sessions->mutex);
630 session_status(c, p);
631 wrbuf_puts(c->wrbuf, "</session-status>\n");
632 yaz_mutex_enter(http_sessions->mutex);
633 p->activity_counter--;
635 yaz_mutex_leave(http_sessions->mutex);
637 response_close(c, "server-status");
641 static void bytarget_response(struct http_channel *c) {
643 struct hitsbytarget *ht;
644 struct http_request *rq = c->request;
645 const char *settings = http_argbyname(rq, "settings");
646 struct http_session *s = locate_session(c);
648 ht = get_hitsbytarget(s->psession, &count, c->nmem);
649 response_open(c, "bytarget");
651 yaz_log(YLOG_WARN, "Empty bytarget Response. No targets found!");
652 for (i = 0; i < count; i++)
654 wrbuf_puts(c->wrbuf, "\n<target>");
656 wrbuf_puts(c->wrbuf, "<id>");
657 wrbuf_xmlputs(c->wrbuf, ht[i].id);
658 wrbuf_puts(c->wrbuf, "</id>\n");
660 if (ht[i].name && ht[i].name[0])
662 wrbuf_puts(c->wrbuf, "<name>");
663 wrbuf_xmlputs(c->wrbuf, ht[i].name);
664 wrbuf_puts(c->wrbuf, "</name>\n");
667 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
668 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
669 if (ht[i].diagnostic)
671 wrbuf_puts(c->wrbuf, "<addinfo>");
673 wrbuf_xmlputs(c->wrbuf, ht[i].addinfo);
674 wrbuf_puts(c->wrbuf, "</addinfo>\n");
677 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
679 wrbuf_puts(c->wrbuf, "<state>");
680 wrbuf_xmlputs(c->wrbuf, ht[i].state);
681 wrbuf_puts(c->wrbuf, "</state>\n");
682 if (settings && *settings == '1')
684 wrbuf_puts(c->wrbuf, "<settings>\n");
685 wrbuf_puts(c->wrbuf, ht[i].settings_xml);
686 wrbuf_puts(c->wrbuf, "</settings>\n");
688 if (ht[i].suggestions_xml && ht[i].suggestions_xml[0]) {
689 wrbuf_puts(c->wrbuf, "<suggestions>");
690 wrbuf_puts(c->wrbuf, ht[i].suggestions_xml);
691 wrbuf_puts(c->wrbuf, "</suggestions>");
693 wrbuf_puts(c->wrbuf, "</target>");
695 response_close(c, "bytarget");
696 release_session(c, s);
699 static void bytarget_result_ready(void *data)
701 struct http_channel *c = (struct http_channel *) data;
702 yaz_log(c->http_sessions->log_level, "bytarget watch released");
703 bytarget_response(c);
707 static void cmd_bytarget(struct http_channel *c)
709 struct http_request *rq = c->request;
710 struct http_response *rs = c->response;
711 struct http_session *s = locate_session(c);
712 const char *block = http_argbyname(rq, "block");
718 no_active = session_active_clients(s->psession);
720 if (block && !strcmp("1",block) && no_active)
722 // if there is already a watch/block. we do not block this one
723 if (session_set_watch(s->psession, SESSION_WATCH_BYTARGET,
724 bytarget_result_ready, c, c) != 0)
726 yaz_log(YLOG_WARN, "Attempt to block multiple times on bytarget block. Not supported!");
727 error(rs, PAZPAR2_ALREADY_BLOCKED, "bytarget");
731 yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on command bytarget", s, s->session_id);
733 release_session(c, s);
736 bytarget_response(c);
737 release_session(c, s);
740 static void write_metadata(WRBUF w, struct conf_service *service,
741 struct record_metadata **ml, int full)
745 for (imeta = 0; imeta < service->num_metadata; imeta++)
747 struct conf_metadata *cmd = &service->metadata[imeta];
748 struct record_metadata *md;
749 if (!cmd->brief && !full)
751 for (md = ml[imeta]; md; md = md->next)
753 struct record_metadata_attr *attr = md->attributes;
754 wrbuf_printf(w, "\n<md-%s", cmd->name);
756 for (; attr; attr = attr->next)
758 wrbuf_printf(w, " %s=\"", attr->name);
759 wrbuf_xmlputs(w, attr->value);
765 case Metadata_type_generic:
766 wrbuf_xmlputs(w, md->data.text.disp);
768 case Metadata_type_year:
769 wrbuf_printf(w, "%d", md->data.number.min);
770 if (md->data.number.min != md->data.number.max)
771 wrbuf_printf(w, "-%d", md->data.number.max);
774 wrbuf_puts(w, "[can't represent]");
776 wrbuf_printf(w, "</md-%s>", cmd->name);
781 static void write_subrecord(struct record *r, WRBUF w,
782 struct conf_service *service, int show_details)
784 const char *name = session_setting_oneval(
785 client_get_database(r->client), PZ_NAME);
787 wrbuf_puts(w, "<location id=\"");
788 wrbuf_xmlputs(w, client_get_id(r->client));
789 wrbuf_puts(w, "\" ");
791 wrbuf_puts(w, "name=\"");
792 wrbuf_xmlputs(w, *name ? name : "Unknown");
793 wrbuf_puts(w, "\">");
795 write_metadata(w, service, r->metadata, show_details);
796 wrbuf_puts(w, "</location>\n");
799 static void show_raw_record_error(void *data, const char *addinfo)
801 http_channel_observer_t obs = data;
802 struct http_channel *c = http_channel_observer_chan(obs);
803 struct http_response *rs = c->response;
805 http_remove_observer(obs);
807 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
810 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
812 http_channel_observer_t obs = data;
813 struct http_channel *c = http_channel_observer_chan(obs);
814 struct http_response *rs = c->response;
816 http_remove_observer(obs);
818 wrbuf_write(c->wrbuf, buf, sz);
819 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
820 http_send_response(c);
824 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
826 http_channel_observer_t obs = data;
827 struct http_channel *c = http_channel_observer_chan(obs);
828 struct http_response *rs = c->response;
830 http_remove_observer(obs);
832 wrbuf_write(c->wrbuf, buf, sz);
833 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
835 rs->content_type = "application/octet-stream";
836 http_send_response(c);
840 void show_raw_reset(void *data, struct http_channel *c, void *data2)
842 //struct client *client = data;
843 //client_show_raw_remove(client, data2);
846 static void cmd_record_ready(void *data);
848 static void cmd_record(struct http_channel *c)
850 struct http_response *rs = c->response;
851 struct http_request *rq = c->request;
852 struct http_session *s = locate_session(c);
853 struct record_cluster *rec, *prev_r, *next_r;
855 struct conf_service *service;
856 const char *idstr = http_argbyname(rq, "id");
857 const char *offsetstr = http_argbyname(rq, "offset");
858 const char *binarystr = http_argbyname(rq, "binary");
862 service = s->psession->service;
865 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
868 wrbuf_rewind(c->wrbuf);
869 if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
871 if (session_active_clients(s->psession) == 0)
873 error(rs, PAZPAR2_RECORD_MISSING, idstr);
875 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
876 cmd_record_ready, c, c) != 0)
878 error(rs, PAZPAR2_RECORD_MISSING, idstr);
880 release_session(c, s);
885 int offset = atoi(offsetstr);
886 const char *syntax = http_argbyname(rq, "syntax");
887 const char *esn = http_argbyname(rq, "esn");
889 struct record*r = rec->records;
891 const char *nativesyntax = http_argbyname(rq, "nativesyntax");
893 if (binarystr && *binarystr != '0')
896 for (i = 0; i < offset && r; r = r->next, i++)
900 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
904 http_channel_observer_t obs =
905 http_add_observer(c, r->client, show_raw_reset);
906 int ret = client_show_raw_begin(r->client, r->position,
909 show_raw_record_error,
911 show_raw_record_ok_binary :
917 http_remove_observer(obs);
918 error(rs, PAZPAR2_NO_SESSION, 0);
924 response_open_no_status(c, "record");
925 wrbuf_puts(c->wrbuf, "\n<recid>");
926 wrbuf_xmlputs(c->wrbuf, rec->recid);
927 wrbuf_puts(c->wrbuf, "</recid>\n");
930 wrbuf_puts(c->wrbuf, "<prevrecid>");
931 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
932 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
936 wrbuf_puts(c->wrbuf, "<nextrecid>");
937 wrbuf_xmlputs(c->wrbuf, next_r->recid);
938 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
940 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
941 session_active_clients(s->psession));
942 write_metadata(c->wrbuf, service, rec->metadata, 1);
943 for (r = rec->records; r; r = r->next)
944 write_subrecord(r, c->wrbuf, service, 1);
945 response_close(c, "record");
947 show_single_stop(s->psession, rec);
948 release_session(c, s);
951 static void cmd_record_ready(void *data)
953 struct http_channel *c = (struct http_channel *) data;
954 yaz_log(c->http_sessions->log_level, "record watch released");
958 static void show_records(struct http_channel *c, int active)
960 struct http_request *rq = c->request;
961 struct http_response *rs = c->response;
962 struct http_session *s = locate_session(c);
963 struct record_cluster **rl;
964 struct reclist_sortparms *sp;
965 const char *start = http_argbyname(rq, "start");
966 const char *num = http_argbyname(rq, "num");
967 const char *sort = http_argbyname(rq, "sort");
977 // We haven't counted clients yet if we're called on a block release
979 active = session_active_clients(s->psession);
982 startn = atoi(start);
987 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
989 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
990 release_session(c, s);
996 rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits);
998 response_open(c, "show");
999 wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>\n", active);
1000 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
1001 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
1002 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
1003 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
1005 for (i = 0; i < numn; i++)
1009 struct record_cluster *rec = rl[i];
1010 struct conf_service *service = s->psession->service;
1012 wrbuf_puts(c->wrbuf, "<hit>\n");
1013 write_metadata(c->wrbuf, service, rec->metadata, 0);
1014 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
1015 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
1017 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
1018 if (strstr(sort, "relevance"))
1019 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
1020 rec->relevance_score);
1021 wrbuf_puts(c->wrbuf, "<recid>");
1022 wrbuf_xmlputs(c->wrbuf, rec->recid);
1023 wrbuf_puts(c->wrbuf, "</recid>\n");
1024 wrbuf_puts(c->wrbuf, "</hit>\n");
1027 show_range_stop(s->psession, rl);
1029 response_close(c, "show");
1030 release_session(c, s);
1033 static void show_records_ready(void *data)
1035 struct http_channel *c = (struct http_channel *) data;
1036 yaz_log(c->http_sessions->log_level, "show watch released");
1037 show_records(c, -1);
1040 static void cmd_show(struct http_channel *c)
1042 struct http_request *rq = c->request;
1043 struct http_response *rs = c->response;
1044 struct http_session *s = locate_session(c);
1045 const char *block = http_argbyname(rq, "block");
1046 const char *sort = http_argbyname(rq, "sort");
1047 struct reclist_sortparms *sp;
1056 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
1058 error(c->response, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
1059 release_session(c, s);
1062 session_sort(s->psession, sp->name, sp->increasing);
1064 status = session_active_clients(s->psession);
1068 if (!strcmp(block, "preferred") && !session_is_preferred_clients_ready(s->psession) && reclist_get_num_records(s->psession->reclist) == 0)
1070 // if there is already a watch/block. we do not block this one
1071 if (session_set_watch(s->psession, SESSION_WATCH_SHOW_PREF,
1072 show_records_ready, c, c) == 0)
1074 yaz_log(c->http_sessions->log_level,
1075 "%p Session %u: Blocking on command show (preferred targets)", s, s->session_id);
1079 yaz_log(YLOG_WARN, "Attempt to block multiple times on show (preferred targets) block. Not supported!");
1080 error(rs, PAZPAR2_ALREADY_BLOCKED, "show (preferred targets)");
1082 release_session(c, s);
1088 // if there is already a watch/block. we do not block this one
1089 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
1090 show_records_ready, c, c) != 0
1093 yaz_log(YLOG_WARN, "Attempt to block multiple times on show block. Not supported!");
1094 error(rs, PAZPAR2_ALREADY_BLOCKED, "show");
1098 yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on command show", s, s->session_id);
1100 release_session(c, s);
1104 show_records(c, status);
1105 release_session(c, s);
1108 static void cmd_ping(struct http_channel *c)
1110 struct http_session *s = locate_session(c);
1113 response_open(c, "ping");
1114 response_close(c, "ping");
1115 release_session(c, s);
1118 static void cmd_search(struct http_channel *c)
1120 struct http_request *rq = c->request;
1121 struct http_response *rs = c->response;
1122 struct http_session *s = locate_session(c);
1123 const char *query = http_argbyname(rq, "query");
1124 const char *filter = http_argbyname(rq, "filter");
1125 const char *maxrecs = http_argbyname(rq, "maxrecs");
1126 const char *startrecs = http_argbyname(rq, "startrecs");
1127 const char *limit = http_argbyname(rq, "limit");
1128 enum pazpar2_error_code code;
1129 const char *addinfo = 0;
1135 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
1136 release_session(c, s);
1139 if (!yaz_utf8_check(query))
1141 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
1142 release_session(c, s);
1145 code = session_search(s->psession, query, startrecs, maxrecs, filter, limit,
1146 &addinfo, "relevance", 0);
1149 error(rs, code, addinfo);
1150 release_session(c, s);
1153 response_open(c, "search");
1154 response_close(c, "search");
1155 release_session(c, s);
1159 static void cmd_stat(struct http_channel *c)
1161 struct http_session *s = locate_session(c);
1162 struct statistics stat;
1170 clients = session_active_clients(s->psession);
1171 statistics(s->psession, &stat);
1173 if (stat.num_clients > 0)
1175 progress = (stat.num_clients - clients) / (float)stat.num_clients;
1178 response_open_no_status(c, "stat");
1179 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
1180 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1181 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
1182 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
1183 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
1184 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
1185 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
1186 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
1187 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
1188 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1189 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1190 response_close(c, "stat");
1191 release_session(c, s);
1194 static void cmd_info(struct http_channel *c)
1196 char yaz_version_str[20];
1198 response_open_no_status(c, "info");
1199 wrbuf_puts(c->wrbuf, " <version>\n");
1200 wrbuf_puts(c->wrbuf, " <pazpar2");
1201 #ifdef PAZPAR2_VERSION_SHA1
1202 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1204 wrbuf_puts(c->wrbuf, ">");
1205 wrbuf_xmlputs(c->wrbuf, VERSION);
1206 wrbuf_puts(c->wrbuf, "</pazpar2>");
1208 yaz_version(yaz_version_str, 0);
1209 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1210 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1211 wrbuf_puts(c->wrbuf, "\">");
1212 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1213 wrbuf_puts(c->wrbuf, "</yaz>\n");
1215 wrbuf_puts(c->wrbuf, " </version>\n");
1217 info_services(c->server, c->wrbuf);
1219 response_close(c, "info");
1224 void (*fun)(struct http_channel *c);
1226 { "init", cmd_init },
1227 { "settings", cmd_settings },
1228 { "stat", cmd_stat },
1229 { "bytarget", cmd_bytarget },
1230 { "show", cmd_show },
1231 { "search", cmd_search },
1232 { "termlist", cmd_termlist },
1233 { "exit", cmd_exit },
1234 { "session-status", cmd_session_status },
1235 { "server-status", cmd_server_status },
1236 { "ping", cmd_ping },
1237 { "record", cmd_record },
1238 { "info", cmd_info },
1242 void http_command(struct http_channel *c)
1244 const char *command = http_argbyname(c->request, "command");
1245 struct http_response *rs = http_create_response(c);
1250 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1251 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1255 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1258 for (i = 0; commands[i].name; i++)
1259 if (!strcmp(commands[i].name, command))
1261 (*commands[i].fun)(c);
1264 if (!commands[i].name)
1265 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1273 * c-file-style: "Stroustrup"
1274 * indent-tabs-mode: nil
1276 * vim: shiftwidth=4 tabstop=8 expandtab