1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <sys/types.h>
33 #include <yaz/snprintf.h>
34 #include <yaz/yaz-util.h>
38 #include "parameters.h"
44 // Update this when the protocol changes
45 #define PAZPAR2_PROTOCOL_VERSION "1"
47 #define HTTP_COMMAND_RESPONSE_PREFIX "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
50 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
51 struct session *psession;
52 unsigned int session_id;
57 http_sessions_t http_sessions;
58 struct http_session *next;
61 struct http_sessions {
62 struct http_session *session_list;
66 http_sessions_t http_sessions_create(void)
68 http_sessions_t hs = xmalloc(sizeof(*hs));
71 pazpar2_mutex_create(&hs->mutex, "http_sessions");
75 void http_sessions_destroy(http_sessions_t hs)
79 struct http_session *s = hs->session_list;
82 struct http_session *s_next = s->next;
83 iochan_destroy(s->timeout_iochan);
84 destroy_session(s->psession);
85 nmem_destroy(s->nmem);
88 yaz_mutex_destroy(&hs->mutex);
93 void http_session_destroy(struct http_session *s);
95 static void session_timeout(IOCHAN i, int event)
97 struct http_session *s = iochan_getdata(i);
98 http_session_destroy(s);
101 struct http_session *http_session_create(struct conf_service *service,
102 http_sessions_t http_sessions,
105 NMEM nmem = nmem_create();
106 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
109 sprintf(tmp_str, "session#%u", sesid);
110 r->psession = new_session(nmem, service, tmp_str);
111 r->session_id = sesid;
114 r->destroy_counter = r->activity_counter = 0;
115 r->http_sessions = http_sessions;
117 yaz_mutex_enter(http_sessions->mutex);
118 r->next = http_sessions->session_list;
119 http_sessions->session_list = r;
120 yaz_mutex_leave(http_sessions->mutex);
122 r->timeout_iochan = iochan_create(-1, session_timeout, 0, "http_session_timeout");
123 iochan_setdata(r->timeout_iochan, r);
124 yaz_log(YLOG_LOG, "%p HTTP session %u created. timeout chan=%p timeout=%d", r, sesid, r->timeout_iochan, service->session_timeout);
125 iochan_settimeout(r->timeout_iochan, service->session_timeout);
127 iochan_add(service->server->iochan_man, r->timeout_iochan);
131 void http_session_destroy(struct http_session *s)
133 int must_destroy = 1;
135 http_sessions_t http_sessions = s->http_sessions;
137 yaz_log(YLOG_LOG, "%p HTTP session destroy %u", s, s->session_id);
138 yaz_mutex_enter(http_sessions->mutex);
140 /* only if http_session destroy was already called, we will allow it
142 if (s->destroy_counter != s->activity_counter)
145 s->destroy_counter = s->activity_counter = 0;
148 struct http_session **p = 0;
149 for (p = &http_sessions->session_list; *p; p = &(*p)->next)
156 yaz_mutex_leave(http_sessions->mutex);
158 { /* destroying for real */
159 yaz_log(YLOG_LOG, "%p HTTP session destroying. session id %u", s, s->session_id); iochan_destroy(s->timeout_iochan);
160 destroy_session(s->psession);
161 nmem_destroy(s->nmem);
164 yaz_log(YLOG_DEBUG, "%p HTTP Session. Active clients on session %u. Waiting for new timeout.", s, s->session_id);
169 static const char *get_msg(enum pazpar2_error_code code)
171 struct pazpar2_error_msg {
172 enum pazpar2_error_code code;
175 static const struct pazpar2_error_msg ar[] = {
176 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
177 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
178 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
179 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
180 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
181 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
182 { PAZPAR2_RECORD_MISSING, "Record missing"},
183 { PAZPAR2_NO_TARGETS, "No targets"},
184 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
185 { PAZPAR2_RECORD_FAIL, "Record command failed"},
186 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
187 { PAZPAR2_NO_SERVICE, "No service"},
188 { PAZPAR2_LAST_ERROR, "Last error"},
194 if (code == ar[i].code)
201 static void error(struct http_response *rs,
202 enum pazpar2_error_code code,
205 struct http_channel *c = rs->channel;
206 WRBUF text = wrbuf_alloc();
207 const char *http_status = "417";
208 const char *msg = get_msg(code);
210 rs->msg = nmem_strdup(c->nmem, msg);
211 strcpy(rs->code, http_status);
213 wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
216 wrbuf_xmlputs(text, addinfo);
217 wrbuf_puts(text, "</error>");
219 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
220 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
221 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
223 http_send_response(c);
226 unsigned int make_sessionid(void)
228 static int seq = 0; /* thread pr */
232 if (global_parameters.debug_mode)
241 if (gettimeofday(&t, 0) < 0)
243 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
246 /* at most 256 sessions per second ..
247 (long long would be more appropriate)*/
249 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
255 static struct http_session *locate_session(struct http_channel *c)
257 struct http_response *rs = c->response;
258 struct http_request *rq = c->request;
259 struct http_session *p;
260 const char *session = http_argbyname(rq, "session");
261 http_sessions_t http_sessions = c->http_sessions;
266 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
270 yaz_mutex_enter(http_sessions->mutex);
271 for (p = http_sessions->session_list; p; p = p->next)
272 if (id == p->session_id)
275 p->activity_counter++;
276 yaz_mutex_leave(http_sessions->mutex);
278 iochan_activity(p->timeout_iochan);
280 error(rs, PAZPAR2_NO_SESSION, session);
284 // Decode settings parameters and apply to session
285 // Syntax: setting[target]=value
286 static int process_settings(struct session *se, struct http_request *rq,
287 struct http_response *rs)
289 struct http_argument *a;
291 for (a = rq->arguments; a; a = a->next)
292 if (strchr(a->name, '['))
299 // Nmem_strsplit *rules*!!!
300 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
303 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
308 session_apply_setting(se, dbname, setting,
309 nmem_strdup(se->session_nmem, a->value));
314 static void cmd_exit(struct http_channel *c)
316 yaz_log(YLOG_WARN, "exit");
317 http_close_server(c->server);
320 static void cmd_init(struct http_channel *c)
323 struct http_request *r = c->request;
324 const char *clear = http_argbyname(r, "clear");
325 const char *content_type = http_lookup_header(r->headers, "Content-Type");
327 struct http_session *s;
328 struct http_response *rs = c->response;
329 struct conf_service *service = 0; /* no service (yet) */
331 if (r->content_len && content_type &&
332 !yaz_strcmp_del("text/xml", content_type, "; "))
334 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
338 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
341 root_n = xmlDocGetRootElement(doc);
342 service = service_create(c->server, root_n);
346 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
353 const char *service_name = http_argbyname(c->request, "service");
354 service = locate_service(c->server, service_name);
357 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
361 sesid = make_sessionid();
362 s = http_session_create(service, c->http_sessions, sesid);
364 yaz_log(YLOG_DEBUG, "HTTP Session init");
365 if (!clear || *clear == '0')
366 session_init_databases(s->psession);
368 yaz_log(YLOG_LOG, "No databases preloaded");
370 if (process_settings(s->psession, c->request, c->response) < 0)
373 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX
374 "<init><status>OK</status><session>%d", sesid);
375 if (c->server->server_id)
378 strcat(buf, c->server->server_id);
380 strcat(buf, "</session>"
381 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
382 rs->payload = nmem_strdup(c->nmem, buf);
383 http_send_response(c);
386 static void apply_local_setting(void *client_data,
389 struct session *se = (struct session *) client_data;
391 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
392 nmem_strdup(se->session_nmem, set->name),
393 nmem_strdup(se->session_nmem, set->value));
396 static void cmd_settings(struct http_channel *c)
398 struct http_response *rs = c->response;
399 struct http_request *rq = c->request;
400 struct http_session *s = locate_session(c);
401 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
406 if (rq->content_len && content_type &&
407 !yaz_strcmp_del("text/xml", content_type, "; "))
409 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
413 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
416 root_n = xmlDocGetRootElement(doc);
418 settings_read_node_x(root_n, s->psession, apply_local_setting);
422 if (process_settings(s->psession, rq, rs) < 0)
424 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
425 http_send_response(c);
428 // Compares two hitsbytarget nodes by hitcount
429 static int cmp_ht(const void *p1, const void *p2)
431 const struct hitsbytarget *h1 = p1;
432 const struct hitsbytarget *h2 = p2;
433 return h2->hits - h1->hits;
436 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
437 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
440 struct hitsbytarget *ht;
443 ht = hitsbytarget(se, &count, nmem);
444 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
445 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
448 // do only print terms which have display names
450 wrbuf_puts(wrbuf, "<term>\n");
452 wrbuf_puts(wrbuf, "<id>");
453 wrbuf_xmlputs(wrbuf, ht[i].id);
454 wrbuf_puts(wrbuf, "</id>\n");
456 wrbuf_puts(wrbuf, "<name>");
457 if (!ht[i].name || !ht[i].name[0])
458 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
460 wrbuf_xmlputs(wrbuf, ht[i].name);
461 wrbuf_puts(wrbuf, "</name>\n");
463 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
466 wrbuf_puts(wrbuf, "<state>");
467 wrbuf_xmlputs(wrbuf, ht[i].state);
468 wrbuf_puts(wrbuf, "</state>\n");
470 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
472 wrbuf_puts(wrbuf, "</term>\n");
476 static void cmd_termlist(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 struct termlist_score **p;
484 const char *name = http_argbyname(rq, "name");
485 const char *nums = http_argbyname(rq, "num");
492 status = session_active_clients(s->psession);
496 if (strlen(name) > 255)
501 wrbuf_rewind(c->wrbuf);
503 wrbuf_puts(c->wrbuf, "<termlist>\n");
504 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
510 if (!(tp = strchr(name, ',')))
511 tp = name + strlen(name);
512 strncpy(tname, name, tp - name);
513 tname[tp - name] = '\0';
515 wrbuf_puts(c->wrbuf, "<list name=\"");
516 wrbuf_xmlputs(c->wrbuf, tname);
517 wrbuf_puts(c->wrbuf, "\">\n");
518 if (!strcmp(tname, "xtargets"))
519 targets_termlist(c->wrbuf, s->psession, num, c->nmem);
522 p = termlist(s->psession, tname, &len);
524 for (i = 0; i < len && i < num; i++){
525 // prevnt sending empty term elements
526 if (!p[i]->term || !p[i]->term[0])
529 wrbuf_puts(c->wrbuf, "<term>");
530 wrbuf_puts(c->wrbuf, "<name>");
531 wrbuf_xmlputs(c->wrbuf, p[i]->term);
532 wrbuf_puts(c->wrbuf, "</name>");
534 wrbuf_printf(c->wrbuf,
535 "<frequency>%d</frequency>",
537 wrbuf_puts(c->wrbuf, "</term>\n");
540 wrbuf_puts(c->wrbuf, "</list>\n");
545 wrbuf_puts(c->wrbuf, "</termlist>\n");
546 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
547 http_send_response(c);
551 static void cmd_bytarget(struct http_channel *c)
553 struct http_response *rs = c->response;
554 struct http_request *rq = c->request;
555 struct http_session *s = locate_session(c);
556 struct hitsbytarget *ht;
557 const char *settings = http_argbyname(rq, "settings");
562 ht = hitsbytarget(s->psession, &count, c->nmem);
563 wrbuf_rewind(c->wrbuf);
564 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
566 for (i = 0; i < count; i++)
568 wrbuf_puts(c->wrbuf, "\n<target>");
570 wrbuf_puts(c->wrbuf, "<id>");
571 wrbuf_xmlputs(c->wrbuf, ht[i].id);
572 wrbuf_puts(c->wrbuf, "</id>\n");
574 if (ht[i].name && ht[i].name[0])
576 wrbuf_puts(c->wrbuf, "<name>");
577 wrbuf_xmlputs(c->wrbuf, ht[i].name);
578 wrbuf_puts(c->wrbuf, "</name>\n");
581 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
582 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
583 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
585 wrbuf_puts(c->wrbuf, "<state>");
586 wrbuf_xmlputs(c->wrbuf, ht[i].state);
587 wrbuf_puts(c->wrbuf, "</state>\n");
588 if (settings && *settings == '1')
590 wrbuf_puts(c->wrbuf, "<settings>\n");
591 wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
592 wrbuf_puts(c->wrbuf, "</settings>\n");
594 wrbuf_puts(c->wrbuf, "</target>");
595 wrbuf_destroy(ht[i].settings_xml);
598 wrbuf_puts(c->wrbuf, "</bytarget>");
599 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
600 http_send_response(c);
603 static void write_metadata(WRBUF w, struct conf_service *service,
604 struct record_metadata **ml, int full)
608 for (imeta = 0; imeta < service->num_metadata; imeta++)
610 struct conf_metadata *cmd = &service->metadata[imeta];
611 struct record_metadata *md;
612 if (!cmd->brief && !full)
614 for (md = ml[imeta]; md; md = md->next)
616 struct record_metadata_attr *attr = md->attributes;
617 wrbuf_printf(w, "\n<md-%s", cmd->name);
619 for (; attr; attr = attr->next)
621 wrbuf_printf(w, " %s=\"", attr->name);
622 wrbuf_xmlputs(w, attr->value);
628 case Metadata_type_generic:
629 wrbuf_xmlputs(w, md->data.text.disp);
631 case Metadata_type_year:
632 wrbuf_printf(w, "%d", md->data.number.min);
633 if (md->data.number.min != md->data.number.max)
634 wrbuf_printf(w, "-%d", md->data.number.max);
637 wrbuf_puts(w, "[can't represent]");
639 wrbuf_printf(w, "</md-%s>", cmd->name);
644 static void write_subrecord(struct record *r, WRBUF w,
645 struct conf_service *service, int show_details)
647 const char *name = session_setting_oneval(
648 client_get_database(r->client), PZ_NAME);
650 wrbuf_puts(w, "<location id=\"");
651 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
652 wrbuf_puts(w, "\" ");
654 wrbuf_puts(w, "name=\"");
655 wrbuf_xmlputs(w, *name ? name : "Unknown");
656 wrbuf_puts(w, "\">");
658 write_metadata(w, service, r->metadata, show_details);
659 wrbuf_puts(w, "</location>\n");
662 static void show_raw_record_error(void *data, const char *addinfo)
664 http_channel_observer_t obs = data;
665 struct http_channel *c = http_channel_observer_chan(obs);
666 struct http_response *rs = c->response;
668 http_remove_observer(obs);
670 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
673 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
675 http_channel_observer_t obs = data;
676 struct http_channel *c = http_channel_observer_chan(obs);
677 struct http_response *rs = c->response;
679 http_remove_observer(obs);
681 wrbuf_write(c->wrbuf, buf, sz);
682 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
683 http_send_response(c);
687 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
689 http_channel_observer_t obs = data;
690 struct http_channel *c = http_channel_observer_chan(obs);
691 struct http_response *rs = c->response;
693 http_remove_observer(obs);
695 wrbuf_write(c->wrbuf, buf, sz);
696 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
698 rs->content_type = "application/octet-stream";
699 http_send_response(c);
703 void show_raw_reset(void *data, struct http_channel *c, void *data2)
705 //struct client *client = data;
706 //client_show_raw_remove(client, data2);
709 static void cmd_record_ready(void *data);
711 static void cmd_record(struct http_channel *c)
713 struct http_response *rs = c->response;
714 struct http_request *rq = c->request;
715 struct http_session *s = locate_session(c);
716 struct record_cluster *rec, *prev_r, *next_r;
718 struct conf_service *service;
719 const char *idstr = http_argbyname(rq, "id");
720 const char *offsetstr = http_argbyname(rq, "offset");
721 const char *binarystr = http_argbyname(rq, "binary");
725 service = s->psession->service;
728 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
731 wrbuf_rewind(c->wrbuf);
732 if (!(rec = show_single_start(s->psession, idstr, &prev_r, &next_r)))
734 if (session_active_clients(s->psession) == 0)
736 error(rs, PAZPAR2_RECORD_MISSING, idstr);
738 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
739 cmd_record_ready, c, c) != 0)
741 error(rs, PAZPAR2_RECORD_MISSING, idstr);
747 int offset = atoi(offsetstr);
748 const char *syntax = http_argbyname(rq, "syntax");
749 const char *esn = http_argbyname(rq, "esn");
751 struct record*r = rec->records;
754 if (binarystr && *binarystr != '0')
757 for (i = 0; i < offset && r; r = r->next, i++)
761 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
765 http_channel_observer_t obs =
766 http_add_observer(c, r->client, show_raw_reset);
767 int ret = client_show_raw_begin(r->client, r->position,
770 show_raw_record_error,
772 show_raw_record_ok_binary :
777 http_remove_observer(obs);
778 error(rs, PAZPAR2_NO_SESSION, 0);
784 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
785 wrbuf_puts(c->wrbuf, "<recid>");
786 wrbuf_xmlputs(c->wrbuf, rec->recid);
787 wrbuf_puts(c->wrbuf, "</recid>\n");
790 wrbuf_puts(c->wrbuf, "<prevrecid>");
791 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
792 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
796 wrbuf_puts(c->wrbuf, "<nextrecid>");
797 wrbuf_xmlputs(c->wrbuf, next_r->recid);
798 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
800 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
801 session_active_clients(s->psession));
802 write_metadata(c->wrbuf, service, rec->metadata, 1);
803 for (r = rec->records; r; r = r->next)
804 write_subrecord(r, c->wrbuf, service, 1);
805 wrbuf_puts(c->wrbuf, "</record>\n");
806 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
807 http_send_response(c);
809 show_single_stop(s->psession, rec);
812 static void cmd_record_ready(void *data)
814 struct http_channel *c = (struct http_channel *) data;
819 static void show_records(struct http_channel *c, int active)
821 struct http_request *rq = c->request;
822 struct http_response *rs = c->response;
823 struct http_session *s = locate_session(c);
824 struct record_cluster **rl;
825 struct reclist_sortparms *sp;
826 const char *start = http_argbyname(rq, "start");
827 const char *num = http_argbyname(rq, "num");
828 const char *sort = http_argbyname(rq, "sort");
838 // We haven't counted clients yet if we're called on a block release
840 active = session_active_clients(s->psession);
843 startn = atoi(start);
848 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
850 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
855 rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits);
857 wrbuf_rewind(c->wrbuf);
858 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
859 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
860 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
861 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
862 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
863 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
865 for (i = 0; i < numn; i++)
869 struct record_cluster *rec = rl[i];
870 struct conf_service *service = s->psession->service;
872 wrbuf_puts(c->wrbuf, "<hit>\n");
873 write_metadata(c->wrbuf, service, rec->metadata, 0);
874 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
875 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
877 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
878 if (strstr(sort, "relevance"))
879 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n",
880 rec->relevance_score);
881 wrbuf_puts(c->wrbuf, "<recid>");
882 wrbuf_xmlputs(c->wrbuf, rec->recid);
883 wrbuf_puts(c->wrbuf, "</recid>\n");
884 wrbuf_puts(c->wrbuf, "</hit>\n");
887 show_range_stop(s->psession, rl);
889 wrbuf_puts(c->wrbuf, "</show>\n");
890 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
891 http_send_response(c);
894 static void show_records_ready(void *data)
896 struct http_channel *c = (struct http_channel *) data;
901 static void cmd_show(struct http_channel *c)
903 struct http_request *rq = c->request;
904 struct http_session *s = locate_session(c);
905 const char *block = http_argbyname(rq, "block");
911 status = session_active_clients(s->psession);
915 if (status && reclist_get_num_records(s->psession->reclist) == 0)
917 // if there is already a watch/block. we do not block this one
918 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
919 show_records_ready, c, c) != 0)
921 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
927 show_records(c, status);
930 static void cmd_ping(struct http_channel *c)
932 struct http_response *rs = c->response;
933 struct http_session *s = locate_session(c);
936 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
937 http_send_response(c);
940 static int utf_8_valid(const char *str)
942 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
945 /* check that query is UTF-8 encoded */
946 char *inbuf = (char *) str; /* we know iconv does not alter this */
947 size_t inbytesleft = strlen(inbuf);
949 size_t outbytesleft = strlen(inbuf) + 10;
950 char *out = xmalloc(outbytesleft);
952 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
954 /* if OK, try flushing the rest */
955 if (r != (size_t) (-1))
956 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
959 if (r == (size_t) (-1))
965 static void cmd_search(struct http_channel *c)
967 struct http_request *rq = c->request;
968 struct http_response *rs = c->response;
969 struct http_session *s = locate_session(c);
970 const char *query = http_argbyname(rq, "query");
971 const char *filter = http_argbyname(rq, "filter");
972 const char *maxrecs = http_argbyname(rq, "maxrecs");
973 const char *startrecs = http_argbyname(rq, "startrecs");
974 enum pazpar2_error_code code;
975 const char *addinfo = 0;
981 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
984 if (!utf_8_valid(query))
986 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
989 code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
992 error(rs, code, addinfo);
995 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
996 http_send_response(c);
1000 static void cmd_stat(struct http_channel *c)
1002 struct http_response *rs = c->response;
1003 struct http_session *s = locate_session(c);
1004 struct statistics stat;
1012 clients = session_active_clients(s->psession);
1013 statistics(s->psession, &stat);
1015 if (stat.num_clients > 0) {
1016 progress = (stat.num_clients - clients) / (float)stat.num_clients;
1019 wrbuf_rewind(c->wrbuf);
1020 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
1021 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
1022 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
1023 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
1024 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
1025 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
1026 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
1027 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
1028 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
1029 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
1030 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1031 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1032 wrbuf_puts(c->wrbuf, "</stat>");
1033 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1034 http_send_response(c);
1037 static void cmd_info(struct http_channel *c)
1039 char yaz_version_str[20];
1040 struct http_response *rs = c->response;
1042 wrbuf_rewind(c->wrbuf);
1043 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
1044 wrbuf_puts(c->wrbuf, " <version>\n");
1045 wrbuf_puts(c->wrbuf, "<pazpar2");
1046 #ifdef PAZPAR2_VERSION_SHA1
1047 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1049 wrbuf_puts(c->wrbuf, ">");
1050 wrbuf_xmlputs(c->wrbuf, VERSION);
1051 wrbuf_puts(c->wrbuf, "</pazpar2>");
1054 yaz_version(yaz_version_str, 0);
1055 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1056 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1057 wrbuf_puts(c->wrbuf, "\">");
1058 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1059 wrbuf_puts(c->wrbuf, "</yaz>\n");
1061 wrbuf_puts(c->wrbuf, " </version>\n");
1063 wrbuf_puts(c->wrbuf, "</info>");
1064 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1065 http_send_response(c);
1070 void (*fun)(struct http_channel *c);
1072 { "init", cmd_init },
1073 { "settings", cmd_settings },
1074 { "stat", cmd_stat },
1075 { "bytarget", cmd_bytarget },
1076 { "show", cmd_show },
1077 { "search", cmd_search },
1078 { "termlist", cmd_termlist },
1079 { "exit", cmd_exit },
1080 { "ping", cmd_ping },
1081 { "record", cmd_record },
1082 { "info", cmd_info },
1086 void http_command(struct http_channel *c)
1088 const char *command = http_argbyname(c->request, "command");
1089 struct http_response *rs = http_create_response(c);
1094 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1095 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1099 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1102 for (i = 0; commands[i].name; i++)
1103 if (!strcmp(commands[i].name, command))
1105 (*commands[i].fun)(c);
1108 if (!commands[i].name)
1109 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1117 * c-file-style: "Stroustrup"
1118 * indent-tabs-mode: nil
1120 * vim: shiftwidth=4 tabstop=8 expandtab