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"
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;
67 static YAZ_MUTEX g_http_session_mutex = 0;
68 static int g_http_sessions = 0;
70 int http_session_use(int delta)
73 if (!g_http_session_mutex)
74 yaz_mutex_create(&g_http_session_mutex);
75 yaz_mutex_enter(g_http_session_mutex);
76 g_http_sessions += delta;
77 sessions = g_http_sessions;
78 yaz_mutex_leave(g_http_session_mutex);
79 yaz_log(YLOG_DEBUG, "%s sesions=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), sessions);
84 http_sessions_t http_sessions_create(void)
86 http_sessions_t hs = xmalloc(sizeof(*hs));
89 pazpar2_mutex_create(&hs->mutex, "http_sessions");
90 hs->log_level = yaz_log_module_level("HTTP");
94 void http_sessions_destroy(http_sessions_t hs)
98 struct http_session *s = hs->session_list;
101 struct http_session *s_next = s->next;
102 iochan_destroy(s->timeout_iochan);
103 destroy_session(s->psession);
104 nmem_destroy(s->nmem);
107 yaz_mutex_destroy(&hs->mutex);
112 void http_session_destroy(struct http_session *s);
114 static void session_timeout(IOCHAN i, int event)
116 struct http_session *s = iochan_getdata(i);
117 http_session_destroy(s);
120 struct http_session *http_session_create(struct conf_service *service,
121 http_sessions_t http_sessions,
124 NMEM nmem = nmem_create();
125 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
128 sprintf(tmp_str, "session#%u", sesid);
129 r->psession = new_session(nmem, service, sesid);
130 r->session_id = sesid;
133 r->destroy_counter = r->activity_counter = 0;
134 r->http_sessions = http_sessions;
136 yaz_mutex_enter(http_sessions->mutex);
137 r->next = http_sessions->session_list;
138 http_sessions->session_list = r;
139 yaz_mutex_leave(http_sessions->mutex);
141 r->timeout_iochan = iochan_create(-1, session_timeout, 0, "http_session_timeout");
142 iochan_setdata(r->timeout_iochan, r);
143 yaz_log(http_sessions->log_level, "%p Session %u created. timeout chan=%p timeout=%d", r, sesid, r->timeout_iochan, service->session_timeout);
144 iochan_settimeout(r->timeout_iochan, service->session_timeout);
146 iochan_add(service->server->iochan_man, r->timeout_iochan);
151 void http_session_destroy(struct http_session *s)
153 int must_destroy = 0;
155 http_sessions_t http_sessions = s->http_sessions;
157 yaz_log(http_sessions->log_level, "%p HTTP Session %u destroyed", s, s->session_id);
158 yaz_mutex_enter(http_sessions->mutex);
159 /* only if http_session has no active http sessions on it can be destroyed */
160 if (s->destroy_counter == s->activity_counter) {
161 struct http_session **p = 0;
163 for (p = &http_sessions->session_list; *p; p = &(*p)->next)
170 yaz_mutex_leave(http_sessions->mutex);
172 { /* destroying for real */
173 yaz_log(http_sessions->log_level, "%p HTTP Session %u destroyed", s, s->session_id);
174 iochan_destroy(s->timeout_iochan);
175 destroy_session(s->psession);
176 http_session_use(-1);
177 nmem_destroy(s->nmem);
180 yaz_log(http_sessions->log_level, "%p HTTP Session %u destroyed delayed. Active clients (%d-%d). Waiting for new timeout.",
181 s, s->session_id, s->activity_counter, s->destroy_counter);
186 static const char *get_msg(enum pazpar2_error_code code)
188 struct pazpar2_error_msg {
189 enum pazpar2_error_code code;
192 static const struct pazpar2_error_msg ar[] = {
193 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
194 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
195 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
196 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
197 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
198 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
199 { PAZPAR2_RECORD_MISSING, "Record missing"},
200 { PAZPAR2_NO_TARGETS, "No targets"},
201 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
202 { PAZPAR2_RECORD_FAIL, "Record command failed"},
203 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
204 { PAZPAR2_NO_SERVICE, "No service"},
205 { PAZPAR2_LAST_ERROR, "Last error"},
211 if (code == ar[i].code)
218 static void error(struct http_response *rs,
219 enum pazpar2_error_code code,
222 struct http_channel *c = rs->channel;
223 WRBUF text = wrbuf_alloc();
224 const char *http_status = "417";
225 const char *msg = get_msg(code);
227 rs->msg = nmem_strdup(c->nmem, msg);
228 strcpy(rs->code, http_status);
230 wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
233 wrbuf_xmlputs(text, addinfo);
234 wrbuf_puts(text, "</error>");
236 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
237 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
238 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
240 http_send_response(c);
243 unsigned int make_sessionid(void)
245 static int seq = 0; /* thread pr */
249 if (global_parameters.debug_mode)
258 if (gettimeofday(&t, 0) < 0)
260 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
263 /* at most 256 sessions per second ..
264 (long long would be more appropriate)*/
266 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
272 static struct http_session *locate_session(struct http_channel *c)
274 struct http_response *rs = c->response;
275 struct http_request *rq = c->request;
276 struct http_session *p;
277 const char *session = http_argbyname(rq, "session");
278 http_sessions_t http_sessions = c->http_sessions;
283 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
287 yaz_mutex_enter(http_sessions->mutex);
288 for (p = http_sessions->session_list; p; p = p->next)
289 if (id == p->session_id)
292 p->activity_counter++;
293 yaz_mutex_leave(http_sessions->mutex);
295 iochan_activity(p->timeout_iochan);
297 error(rs, PAZPAR2_NO_SESSION, session);
301 // Call after use of locate_session, in order to increment the destroy_counter
302 static void release_session(struct http_channel *c, struct http_session *session) {
303 http_sessions_t http_sessions = c->http_sessions;
304 yaz_mutex_enter(http_sessions->mutex);
306 session->destroy_counter++;
307 yaz_mutex_leave(http_sessions->mutex);
310 // Decode settings parameters and apply to session
311 // Syntax: setting[target]=value
312 static int process_settings(struct session *se, struct http_request *rq,
313 struct http_response *rs)
315 struct http_argument *a;
317 for (a = rq->arguments; a; a = a->next)
318 if (strchr(a->name, '['))
325 // Nmem_strsplit *rules*!!!
326 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
329 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
334 session_apply_setting(se, dbname, setting,
335 nmem_strdup(se->session_nmem, a->value));
340 static void cmd_exit(struct http_channel *c)
342 yaz_log(YLOG_WARN, "exit");
343 http_close_server(c->server);
346 static void cmd_init(struct http_channel *c)
349 struct http_request *r = c->request;
350 const char *clear = http_argbyname(r, "clear");
351 const char *content_type = http_lookup_header(r->headers, "Content-Type");
353 struct http_session *s;
354 struct http_response *rs = c->response;
355 struct conf_service *service = 0; /* no service (yet) */
357 if (r->content_len && content_type &&
358 !yaz_strcmp_del("text/xml", content_type, "; "))
360 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
364 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
367 root_n = xmlDocGetRootElement(doc);
368 service = service_create(c->server, root_n);
372 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
379 const char *service_name = http_argbyname(c->request, "service");
380 service = locate_service(c->server, service_name);
383 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
387 sesid = make_sessionid();
388 s = http_session_create(service, c->http_sessions, sesid);
390 yaz_log(c->http_sessions->log_level, "%p Session init %u ", s, sesid);
391 if (!clear || *clear == '0')
392 session_init_databases(s->psession);
394 yaz_log(YLOG_LOG, "HTTP Session %u init: No databases preloaded", sesid);
396 if (process_settings(s->psession, c->request, c->response) < 0)
399 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX
400 "<init><status>OK</status><session>%d", sesid);
401 if (c->server->server_id)
404 strcat(buf, c->server->server_id);
406 strcat(buf, "</session>"
407 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
408 rs->payload = nmem_strdup(c->nmem, buf);
409 http_send_response(c);
412 static void apply_local_setting(void *client_data,
415 struct session *se = (struct session *) client_data;
417 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
418 nmem_strdup(se->session_nmem, set->name),
419 nmem_strdup(se->session_nmem, set->value));
422 static void cmd_settings(struct http_channel *c)
424 struct http_response *rs = c->response;
425 struct http_request *rq = c->request;
426 struct http_session *s = locate_session(c);
427 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
432 if (rq->content_len && content_type &&
433 !yaz_strcmp_del("text/xml", content_type, "; "))
435 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
439 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
442 root_n = xmlDocGetRootElement(doc);
444 settings_read_node_x(root_n, s->psession, apply_local_setting);
448 if (process_settings(s->psession, rq, rs) < 0) {
449 release_session(c,s);
452 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
453 http_send_response(c);
454 release_session(c,s);
457 // Compares two hitsbytarget nodes by hitcount
458 static int cmp_ht(const void *p1, const void *p2)
460 const struct hitsbytarget *h1 = p1;
461 const struct hitsbytarget *h2 = p2;
462 return h2->hits - h1->hits;
465 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
466 static int targets_termlist(WRBUF wrbuf, struct session *se, int num,
469 struct hitsbytarget *ht;
472 ht = hitsbytarget(se, &count, nmem);
473 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
474 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
477 // do only print terms which have display names
479 wrbuf_puts(wrbuf, "<term>\n");
481 wrbuf_puts(wrbuf, "<id>");
482 wrbuf_xmlputs(wrbuf, ht[i].id);
483 wrbuf_puts(wrbuf, "</id>\n");
485 wrbuf_puts(wrbuf, "<name>");
486 if (!ht[i].name || !ht[i].name[0])
487 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
489 wrbuf_xmlputs(wrbuf, ht[i].name);
490 wrbuf_puts(wrbuf, "</name>\n");
492 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
495 wrbuf_puts(wrbuf, "<state>");
496 wrbuf_xmlputs(wrbuf, ht[i].state);
497 wrbuf_puts(wrbuf, "</state>\n");
499 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
501 wrbuf_puts(wrbuf, "</term>\n");
506 static void cmd_termlist(struct http_channel *c)
508 struct http_response *rs = c->response;
509 struct http_request *rq = c->request;
510 struct http_session *s = locate_session(c);
511 struct termlist_score **p;
514 const char *name = http_argbyname(rq, "name");
515 const char *nums = http_argbyname(rq, "num");
518 WRBUF debug_log = wrbuf_alloc();
523 status = session_active_clients(s->psession);
527 if (strlen(name) > 255)
532 wrbuf_rewind(c->wrbuf);
534 wrbuf_puts(c->wrbuf, "<termlist>\n");
535 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
541 if (!(tp = strchr(name, ',')))
542 tp = name + strlen(name);
543 strncpy(tname, name, tp - name);
544 tname[tp - name] = '\0';
545 wrbuf_puts(c->wrbuf, "<list name=\"");
546 wrbuf_xmlputs(c->wrbuf, tname);
547 wrbuf_puts(c->wrbuf, "\">\n");
548 if (!strcmp(tname, "xtargets")) {
549 int targets = targets_termlist(c->wrbuf, s->psession, num, c->nmem);
550 wrbuf_printf(debug_log, " xtargets: %d", targets);
554 p = termlist(s->psession, tname, &len);
556 wrbuf_printf(debug_log, " %s: %d", tname, len);
558 for (i = 0; i < len && i < num; i++){
559 // prevnt sending empty term elements
560 if (!p[i]->term || !p[i]->term[0])
563 wrbuf_puts(c->wrbuf, "<term>");
564 wrbuf_puts(c->wrbuf, "<name>");
565 wrbuf_xmlputs(c->wrbuf, p[i]->term);
566 wrbuf_puts(c->wrbuf, "</name>");
568 wrbuf_printf(c->wrbuf,
569 "<frequency>%d</frequency>",
571 wrbuf_puts(c->wrbuf, "</term>\n");
575 wrbuf_puts(c->wrbuf, "</list>\n");
580 wrbuf_puts(c->wrbuf, "</termlist>\n");
581 yaz_log(YLOG_DEBUG, "termlist response: %s ", wrbuf_cstr(debug_log));
582 wrbuf_destroy(debug_log);
583 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
584 http_send_response(c);
585 release_session(c,s);
588 size_t session_get_memory_status(struct session *session);
590 static void session_status(struct http_channel *c, struct http_session *s)
593 wrbuf_printf(c->wrbuf, "<http_count>%u</http_count>\n", s->activity_counter);
594 wrbuf_printf(c->wrbuf, "<http_nmem>%zu</http_nmem>\n", nmem_total(s->nmem) );
595 session_nmem = session_get_memory_status(s->psession);
596 wrbuf_printf(c->wrbuf, "<session_nmem>%zu</session_nmem>\n", session_nmem);
599 static void cmd_session_status(struct http_channel *c) {
600 struct http_response *rs = c->response;
601 struct http_session *s = locate_session(c);
605 wrbuf_rewind(c->wrbuf);
606 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<sessionstatus><status>OK</status>\n");
607 session_status(c, s);
608 wrbuf_puts(c->wrbuf, "</sessionstatus>\n");
609 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
610 http_send_response(c);
611 release_session(c,s);
615 int sessions_count(void);
616 int clients_count(void);
617 #ifdef HAVE_RESULTSETS_COUNT
618 int resultsets_count(void);
620 #define resultsets_count() 0
623 static void cmd_server_status(struct http_channel *c)
625 struct http_response *rs = c->response;
626 int sessions = sessions_count();
627 int clients = clients_count();
628 int resultsets = resultsets_count();
629 wrbuf_rewind(c->wrbuf);
630 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<server-status>\n");
631 wrbuf_printf(c->wrbuf, " <sessions>%u</sessions>\n", sessions);
632 wrbuf_printf(c->wrbuf, " <clients>%u</clients>\n", clients);
633 /* Only works if yaz has been compiled with enabling of this */
634 wrbuf_printf(c->wrbuf, " <resultsets>%u</resultsets>\n",resultsets);
636 /* TODO add all sessions status */
637 /* http_sessions_t http_sessions = c->http_sessions; */
638 /* struct http_session *p; */
640 yaz_mutex_enter(http_sessions->mutex);
641 for (p = http_sessions->session_list; p; p = p->next) {
642 p->activity_counter++;
643 wrbuf_puts(c->wrbuf, "<session-status>\n");
644 wrbuf_printf(c->wrbuf, "<id>%s</id>\n", p->session_id);
645 yaz_mutex_leave(http_sessions->mutex);
646 session_status(c, p);
647 wrbuf_puts(c->wrbuf, "</session-status>\n");
648 yaz_mutex_enter(http_sessions->mutex);
649 p->activity_counter--;
651 yaz_mutex_leave(http_sessions->mutex);
653 wrbuf_puts(c->wrbuf, "</server-status>\n");
654 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
655 http_send_response(c);
660 static void cmd_bytarget(struct http_channel *c)
662 struct http_response *rs = c->response;
663 struct http_request *rq = c->request;
664 struct http_session *s = locate_session(c);
665 struct hitsbytarget *ht;
666 const char *settings = http_argbyname(rq, "settings");
671 ht = hitsbytarget(s->psession, &count, c->nmem);
672 wrbuf_rewind(c->wrbuf);
673 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
675 for (i = 0; i < count; i++)
677 wrbuf_puts(c->wrbuf, "\n<target>");
679 wrbuf_puts(c->wrbuf, "<id>");
680 wrbuf_xmlputs(c->wrbuf, ht[i].id);
681 wrbuf_puts(c->wrbuf, "</id>\n");
683 if (ht[i].name && ht[i].name[0])
685 wrbuf_puts(c->wrbuf, "<name>");
686 wrbuf_xmlputs(c->wrbuf, ht[i].name);
687 wrbuf_puts(c->wrbuf, "</name>\n");
690 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
691 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
692 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
694 wrbuf_puts(c->wrbuf, "<state>");
695 wrbuf_xmlputs(c->wrbuf, ht[i].state);
696 wrbuf_puts(c->wrbuf, "</state>\n");
697 if (settings && *settings == '1')
699 wrbuf_puts(c->wrbuf, "<settings>\n");
700 wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
701 wrbuf_puts(c->wrbuf, "</settings>\n");
703 wrbuf_puts(c->wrbuf, "</target>");
704 wrbuf_destroy(ht[i].settings_xml);
707 wrbuf_puts(c->wrbuf, "</bytarget>");
708 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
709 http_send_response(c);
710 release_session(c,s);
713 static void write_metadata(WRBUF w, struct conf_service *service,
714 struct record_metadata **ml, int full)
718 for (imeta = 0; imeta < service->num_metadata; imeta++)
720 struct conf_metadata *cmd = &service->metadata[imeta];
721 struct record_metadata *md;
722 if (!cmd->brief && !full)
724 for (md = ml[imeta]; md; md = md->next)
726 struct record_metadata_attr *attr = md->attributes;
727 wrbuf_printf(w, "\n<md-%s", cmd->name);
729 for (; attr; attr = attr->next)
731 wrbuf_printf(w, " %s=\"", attr->name);
732 wrbuf_xmlputs(w, attr->value);
738 case Metadata_type_generic:
739 wrbuf_xmlputs(w, md->data.text.disp);
741 case Metadata_type_year:
742 wrbuf_printf(w, "%d", md->data.number.min);
743 if (md->data.number.min != md->data.number.max)
744 wrbuf_printf(w, "-%d", md->data.number.max);
747 wrbuf_puts(w, "[can't represent]");
749 wrbuf_printf(w, "</md-%s>", cmd->name);
754 static void write_subrecord(struct record *r, WRBUF w,
755 struct conf_service *service, int show_details)
757 const char *name = session_setting_oneval(
758 client_get_database(r->client), PZ_NAME);
760 wrbuf_puts(w, "<location id=\"");
761 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
762 wrbuf_puts(w, "\" ");
764 wrbuf_puts(w, "name=\"");
765 wrbuf_xmlputs(w, *name ? name : "Unknown");
766 wrbuf_puts(w, "\">");
768 write_metadata(w, service, r->metadata, show_details);
769 wrbuf_puts(w, "</location>\n");
772 static void show_raw_record_error(void *data, const char *addinfo)
774 http_channel_observer_t obs = data;
775 struct http_channel *c = http_channel_observer_chan(obs);
776 struct http_response *rs = c->response;
778 http_remove_observer(obs);
780 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
783 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
785 http_channel_observer_t obs = data;
786 struct http_channel *c = http_channel_observer_chan(obs);
787 struct http_response *rs = c->response;
789 http_remove_observer(obs);
791 wrbuf_write(c->wrbuf, buf, sz);
792 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
793 http_send_response(c);
797 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
799 http_channel_observer_t obs = data;
800 struct http_channel *c = http_channel_observer_chan(obs);
801 struct http_response *rs = c->response;
803 http_remove_observer(obs);
805 wrbuf_write(c->wrbuf, buf, sz);
806 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
808 rs->content_type = "application/octet-stream";
809 http_send_response(c);
813 void show_raw_reset(void *data, struct http_channel *c, void *data2)
815 //struct client *client = data;
816 //client_show_raw_remove(client, data2);
819 static void cmd_record_ready(void *data);
821 static void cmd_record(struct http_channel *c)
823 struct http_response *rs = c->response;
824 struct http_request *rq = c->request;
825 struct http_session *s = locate_session(c);
826 struct record_cluster *rec, *prev_r, *next_r;
828 struct conf_service *service;
829 const char *idstr = http_argbyname(rq, "id");
830 const char *offsetstr = http_argbyname(rq, "offset");
831 const char *binarystr = http_argbyname(rq, "binary");
835 service = s->psession->service;
838 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
841 wrbuf_rewind(c->wrbuf);
842 if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
844 if (session_active_clients(s->psession) == 0)
846 error(rs, PAZPAR2_RECORD_MISSING, idstr);
848 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
849 cmd_record_ready, c, c) != 0)
851 error(rs, PAZPAR2_RECORD_MISSING, idstr);
853 release_session(c, s);
858 int offset = atoi(offsetstr);
859 const char *syntax = http_argbyname(rq, "syntax");
860 const char *esn = http_argbyname(rq, "esn");
862 struct record*r = rec->records;
865 if (binarystr && *binarystr != '0')
868 for (i = 0; i < offset && r; r = r->next, i++)
872 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
876 http_channel_observer_t obs =
877 http_add_observer(c, r->client, show_raw_reset);
878 int ret = client_show_raw_begin(r->client, r->position,
881 show_raw_record_error,
883 show_raw_record_ok_binary :
888 http_remove_observer(obs);
889 error(rs, PAZPAR2_NO_SESSION, 0);
895 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
896 wrbuf_puts(c->wrbuf, "<recid>");
897 wrbuf_xmlputs(c->wrbuf, rec->recid);
898 wrbuf_puts(c->wrbuf, "</recid>\n");
901 wrbuf_puts(c->wrbuf, "<prevrecid>");
902 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
903 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
907 wrbuf_puts(c->wrbuf, "<nextrecid>");
908 wrbuf_xmlputs(c->wrbuf, next_r->recid);
909 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
911 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
912 session_active_clients(s->psession));
913 write_metadata(c->wrbuf, service, rec->metadata, 1);
914 for (r = rec->records; r; r = r->next)
915 write_subrecord(r, c->wrbuf, service, 1);
916 wrbuf_puts(c->wrbuf, "</record>\n");
917 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
918 http_send_response(c);
920 show_single_stop(s->psession, rec);
921 release_session(c, s);
924 static void cmd_record_ready(void *data)
926 struct http_channel *c = (struct http_channel *) data;
931 static void show_records(struct http_channel *c, int active)
933 struct http_request *rq = c->request;
934 struct http_response *rs = c->response;
935 struct http_session *s = locate_session(c);
936 struct record_cluster **rl;
937 struct reclist_sortparms *sp;
938 const char *start = http_argbyname(rq, "start");
939 const char *num = http_argbyname(rq, "num");
940 const char *sort = http_argbyname(rq, "sort");
950 // We haven't counted clients yet if we're called on a block release
952 active = session_active_clients(s->psession);
955 startn = atoi(start);
960 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
962 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
963 release_session(c, s);
968 rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits);
970 wrbuf_rewind(c->wrbuf);
971 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
972 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
973 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
974 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
975 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
976 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
978 for (i = 0; i < numn; i++)
982 struct record_cluster *rec = rl[i];
983 struct conf_service *service = s->psession->service;
985 wrbuf_puts(c->wrbuf, "<hit>\n");
986 write_metadata(c->wrbuf, service, rec->metadata, 0);
987 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
988 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
990 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
991 if (strstr(sort, "relevance"))
992 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
993 rec->relevance_score);
994 wrbuf_puts(c->wrbuf, "<recid>");
995 wrbuf_xmlputs(c->wrbuf, rec->recid);
996 wrbuf_puts(c->wrbuf, "</recid>\n");
997 wrbuf_puts(c->wrbuf, "</hit>\n");
1000 show_range_stop(s->psession, rl);
1002 wrbuf_puts(c->wrbuf, "</show>\n");
1003 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1004 http_send_response(c);
1005 release_session(c, s);
1008 static void show_records_ready(void *data)
1010 struct http_channel *c = (struct http_channel *) data;
1012 show_records(c, -1);
1015 static void cmd_show(struct http_channel *c)
1017 struct http_request *rq = c->request;
1018 struct http_session *s = locate_session(c);
1019 const char *block = http_argbyname(rq, "block");
1025 status = session_active_clients(s->psession);
1029 if (!strcmp(block, "preferred") && !session_is_preferred_clients_ready(s->psession) && reclist_get_num_records(s->psession->reclist) == 0) {
1030 // if there is already a watch/block. we do not block this one
1031 if (session_set_watch(s->psession, SESSION_WATCH_SHOW_PREF,
1032 show_records_ready, c, c) != 0)
1034 yaz_log(c->http_sessions->log_level,
1035 "%p Session %u: Blocking on cmd_show. Waiting for preferred targets", s, s->session_id);
1037 release_session(c,s);
1041 else if (status && reclist_get_num_records(s->psession->reclist) == 0)
1043 // if there is already a watch/block. we do not block this one
1044 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
1045 show_records_ready, c, c) != 0)
1047 yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on cmd_show", s, s->session_id);
1049 release_session(c,s);
1053 show_records(c, status);
1054 release_session(c,s);
1057 static void cmd_ping(struct http_channel *c)
1059 struct http_response *rs = c->response;
1060 struct http_session *s = locate_session(c);
1063 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
1064 http_send_response(c);
1065 release_session(c, s);
1068 static int utf_8_valid(const char *str)
1070 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
1073 /* check that query is UTF-8 encoded */
1074 char *inbuf = (char *) str; /* we know iconv does not alter this */
1075 size_t inbytesleft = strlen(inbuf);
1077 size_t outbytesleft = strlen(inbuf) + 10;
1078 char *out = xmalloc(outbytesleft);
1080 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
1082 /* if OK, try flushing the rest */
1083 if (r != (size_t) (-1))
1084 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
1085 yaz_iconv_close(cd);
1087 if (r == (size_t) (-1))
1093 static void cmd_search(struct http_channel *c)
1095 struct http_request *rq = c->request;
1096 struct http_response *rs = c->response;
1097 struct http_session *s = locate_session(c);
1098 const char *query = http_argbyname(rq, "query");
1099 const char *filter = http_argbyname(rq, "filter");
1100 const char *maxrecs = http_argbyname(rq, "maxrecs");
1101 const char *startrecs = http_argbyname(rq, "startrecs");
1102 enum pazpar2_error_code code;
1103 const char *addinfo = 0;
1109 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
1110 release_session(c,s);
1113 if (!utf_8_valid(query))
1115 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
1116 release_session(c,s);
1119 code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
1122 error(rs, code, addinfo);
1123 release_session(c,s);
1126 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
1127 http_send_response(c);
1128 release_session(c,s);
1132 static void cmd_stat(struct http_channel *c)
1134 struct http_response *rs = c->response;
1135 struct http_session *s = locate_session(c);
1136 struct statistics stat;
1144 clients = session_active_clients(s->psession);
1145 statistics(s->psession, &stat);
1147 if (stat.num_clients > 0) {
1148 progress = (stat.num_clients - clients) / (float)stat.num_clients;
1151 wrbuf_rewind(c->wrbuf);
1152 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
1153 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
1154 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1155 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
1156 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
1157 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
1158 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
1159 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
1160 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
1161 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
1162 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1163 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1164 wrbuf_puts(c->wrbuf, "</stat>");
1165 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1166 http_send_response(c);
1167 release_session(c,s);
1170 static void cmd_info(struct http_channel *c)
1172 char yaz_version_str[20];
1173 struct http_response *rs = c->response;
1175 wrbuf_rewind(c->wrbuf);
1176 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
1177 wrbuf_puts(c->wrbuf, " <version>\n");
1178 wrbuf_puts(c->wrbuf, " <pazpar2");
1179 #ifdef PAZPAR2_VERSION_SHA1
1180 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1182 wrbuf_puts(c->wrbuf, ">");
1183 wrbuf_xmlputs(c->wrbuf, VERSION);
1184 wrbuf_puts(c->wrbuf, "</pazpar2>");
1187 yaz_version(yaz_version_str, 0);
1188 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1189 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1190 wrbuf_puts(c->wrbuf, "\">");
1191 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1192 wrbuf_puts(c->wrbuf, "</yaz>\n");
1194 wrbuf_puts(c->wrbuf, " </version>\n");
1196 info_services(c->server, c->wrbuf);
1198 wrbuf_puts(c->wrbuf, "</info>");
1199 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1200 http_send_response(c);
1205 void (*fun)(struct http_channel *c);
1207 { "init", cmd_init },
1208 { "settings", cmd_settings },
1209 { "stat", cmd_stat },
1210 { "bytarget", cmd_bytarget },
1211 { "show", cmd_show },
1212 { "search", cmd_search },
1213 { "termlist", cmd_termlist },
1214 { "exit", cmd_exit },
1215 { "session-status", cmd_session_status },
1216 { "server-status", cmd_server_status },
1217 { "ping", cmd_ping },
1218 { "record", cmd_record },
1219 { "info", cmd_info },
1223 void http_command(struct http_channel *c)
1225 const char *command = http_argbyname(c->request, "command");
1226 struct http_response *rs = http_create_response(c);
1231 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1232 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1236 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1239 for (i = 0; commands[i].name; i++)
1240 if (!strcmp(commands[i].name, command))
1242 (*commands[i].fun)(c);
1245 if (!commands[i].name)
1246 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1254 * c-file-style: "Stroustrup"
1255 * indent-tabs-mode: nil
1257 * vim: shiftwidth=4 tabstop=8 expandtab