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>
37 #include "parameters.h"
43 // Update this when the protocol changes
44 #define PAZPAR2_PROTOCOL_VERSION "1"
46 #define HTTP_COMMAND_RESPONSE_PREFIX "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
49 IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
50 struct session *psession;
51 unsigned int session_id;
54 http_sessions_t http_sessions;
55 struct http_session *next;
58 struct http_sessions {
59 struct http_session *session_list;
63 http_sessions_t http_sessions_create(void)
65 http_sessions_t hs = xmalloc(sizeof(*hs));
68 yaz_mutex_create(&hs->mutex);
72 void http_sessions_destroy(http_sessions_t hs)
76 struct http_session *s = hs->session_list;
79 struct http_session *s_next = s->next;
80 iochan_destroy(s->timeout_iochan);
81 destroy_session(s->psession);
82 nmem_destroy(s->nmem);
85 yaz_mutex_destroy(&hs->mutex);
90 void http_session_destroy(struct http_session *s);
92 static void session_timeout(IOCHAN i, int event)
94 struct http_session *s = iochan_getdata(i);
95 http_session_destroy(s);
98 struct http_session *http_session_create(struct conf_service *service,
99 http_sessions_t http_sessions)
101 NMEM nmem = nmem_create();
102 struct http_session *r = nmem_malloc(nmem, sizeof(*r));
104 r->psession = new_session(nmem, service);
108 r->http_sessions = http_sessions;
110 yaz_mutex_enter(http_sessions->mutex);
111 r->next = http_sessions->session_list;
112 http_sessions->session_list = r;
113 yaz_mutex_leave(http_sessions->mutex);
115 r->timeout_iochan = iochan_create(-1, session_timeout, 0);
116 iochan_setdata(r->timeout_iochan, r);
117 iochan_settimeout(r->timeout_iochan, service->session_timeout);
119 iochan_add(service->server->iochan_man, r->timeout_iochan);
123 void http_session_destroy(struct http_session *s)
125 struct http_session **p;
127 http_sessions_t http_sessions = s->http_sessions;
129 yaz_mutex_enter(http_sessions->mutex);
130 for (p = &http_sessions->session_list; *p; p = &(*p)->next)
136 yaz_mutex_leave(http_sessions->mutex);
137 yaz_log(YLOG_LOG, "Destroying session %u", s->session_id);
138 iochan_destroy(s->timeout_iochan);
139 destroy_session(s->psession);
140 nmem_destroy(s->nmem);
143 static const char *get_msg(enum pazpar2_error_code code)
145 struct pazpar2_error_msg {
146 enum pazpar2_error_code code;
149 static const struct pazpar2_error_msg ar[] = {
150 { PAZPAR2_NO_SESSION, "Session does not exist or it has expired"},
151 { PAZPAR2_MISSING_PARAMETER, "Missing parameter"},
152 { PAZPAR2_MALFORMED_PARAMETER_VALUE, "Malformed parameter value"},
153 { PAZPAR2_MALFORMED_PARAMETER_ENCODING, "Malformed parameter encoding"},
154 { PAZPAR2_MALFORMED_SETTING, "Malformed setting argument"},
155 { PAZPAR2_HITCOUNTS_FAILED, "Failed to retrieve hitcounts"},
156 { PAZPAR2_RECORD_MISSING, "Record missing"},
157 { PAZPAR2_NO_TARGETS, "No targets"},
158 { PAZPAR2_CONFIG_TARGET, "Target cannot be configured"},
159 { PAZPAR2_RECORD_FAIL, "Record command failed"},
160 { PAZPAR2_NOT_IMPLEMENTED, "Not implemented"},
161 { PAZPAR2_NO_SERVICE, "No service"},
162 { PAZPAR2_LAST_ERROR, "Last error"},
168 if (code == ar[i].code)
175 static void error(struct http_response *rs,
176 enum pazpar2_error_code code,
179 struct http_channel *c = rs->channel;
180 WRBUF text = wrbuf_alloc();
181 const char *http_status = "417";
182 const char *msg = get_msg(code);
184 rs->msg = nmem_strdup(c->nmem, msg);
185 strcpy(rs->code, http_status);
187 wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
190 wrbuf_xmlputs(text, addinfo);
191 wrbuf_puts(text, "</error>");
193 yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
194 msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
195 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
197 http_send_response(c);
200 unsigned int make_sessionid(void)
202 static int seq = 0; /* thread pr */
206 if (global_parameters.debug_mode)
215 if (gettimeofday(&t, 0) < 0)
217 yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday");
220 /* at most 256 sessions per second ..
221 (long long would be more appropriate)*/
223 res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1);
229 static struct http_session *locate_session(struct http_channel *c)
231 struct http_response *rs = c->response;
232 struct http_request *rq = c->request;
233 struct http_session *p;
234 const char *session = http_argbyname(rq, "session");
235 http_sessions_t http_sessions = c->http_sessions;
240 error(rs, PAZPAR2_MISSING_PARAMETER, "session");
244 yaz_mutex_enter(http_sessions->mutex);
245 for (p = http_sessions->session_list; p; p = p->next)
246 if (id == p->session_id)
248 yaz_mutex_leave(http_sessions->mutex);
250 iochan_activity(p->timeout_iochan);
252 error(rs, PAZPAR2_NO_SESSION, session);
256 // Decode settings parameters and apply to session
257 // Syntax: setting[target]=value
258 static int process_settings(struct session *se, struct http_request *rq,
259 struct http_response *rs)
261 struct http_argument *a;
263 for (a = rq->arguments; a; a = a->next)
264 if (strchr(a->name, '['))
271 // Nmem_strsplit *rules*!!!
272 nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num);
275 error(rs, PAZPAR2_MALFORMED_SETTING, a->name);
280 session_apply_setting(se, dbname, setting,
281 nmem_strdup(se->session_nmem, a->value));
286 static void cmd_exit(struct http_channel *c)
288 yaz_log(YLOG_WARN, "exit");
289 http_close_server(c->server);
292 static void cmd_init(struct http_channel *c)
295 struct http_request *r = c->request;
296 const char *clear = http_argbyname(r, "clear");
297 const char *content_type = http_lookup_header(r->headers, "Content-Type");
299 struct http_session *s;
300 struct http_response *rs = c->response;
301 struct conf_service *service = 0; /* no service (yet) */
303 if (r->content_len && content_type &&
304 !yaz_strcmp_del("text/xml", content_type, "; "))
306 xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
310 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
313 root_n = xmlDocGetRootElement(doc);
314 service = service_create(c->server, root_n);
318 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
325 const char *service_name = http_argbyname(c->request, "service");
326 service = locate_service(c->server, service_name);
329 error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
333 s = http_session_create(service, c->http_sessions);
335 yaz_log(YLOG_DEBUG, "HTTP Session init");
336 if (!clear || *clear == '0')
337 session_init_databases(s->psession);
339 yaz_log(YLOG_LOG, "No databases preloaded");
341 sesid = make_sessionid();
342 s->session_id = sesid;
343 if (process_settings(s->psession, c->request, c->response) < 0)
346 sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX
347 "<init><status>OK</status><session>%u", sesid);
348 if (c->server->server_id)
351 strcat(buf, c->server->server_id);
353 strcat(buf, "</session>"
354 "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol></init>");
355 rs->payload = nmem_strdup(c->nmem, buf);
356 http_send_response(c);
359 static void apply_local_setting(void *client_data,
362 struct session *se = (struct session *) client_data;
364 session_apply_setting(se, nmem_strdup(se->session_nmem, set->target),
365 nmem_strdup(se->session_nmem, set->name),
366 nmem_strdup(se->session_nmem, set->value));
369 static void cmd_settings(struct http_channel *c)
371 struct http_response *rs = c->response;
372 struct http_request *rq = c->request;
373 struct http_session *s = locate_session(c);
374 const char *content_type = http_lookup_header(rq->headers, "Content-Type");
379 if (rq->content_len && content_type &&
380 !yaz_strcmp_del("text/xml", content_type, "; "))
382 xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
386 error(rs, PAZPAR2_MALFORMED_SETTING, 0);
389 root_n = xmlDocGetRootElement(doc);
391 settings_read_node_x(root_n, s->psession, apply_local_setting);
395 if (process_settings(s->psession, rq, rs) < 0)
397 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<settings><status>OK</status></settings>";
398 http_send_response(c);
401 // Compares two hitsbytarget nodes by hitcount
402 static int cmp_ht(const void *p1, const void *p2)
404 const struct hitsbytarget *h1 = p1;
405 const struct hitsbytarget *h2 = p2;
406 return h2->hits - h1->hits;
409 // This implements functionality somewhat similar to 'bytarget', but in a termlist form
410 static void targets_termlist(WRBUF wrbuf, struct session *se, int num,
413 struct hitsbytarget *ht;
416 ht = hitsbytarget(se, &count, nmem);
417 qsort(ht, count, sizeof(struct hitsbytarget), cmp_ht);
418 for (i = 0; i < count && i < num && ht[i].hits > 0; i++)
421 // do only print terms which have display names
423 wrbuf_puts(wrbuf, "<term>\n");
425 wrbuf_puts(wrbuf, "<id>");
426 wrbuf_xmlputs(wrbuf, ht[i].id);
427 wrbuf_puts(wrbuf, "</id>\n");
429 wrbuf_puts(wrbuf, "<name>");
430 if (!ht[i].name || !ht[i].name[0])
431 wrbuf_xmlputs(wrbuf, "NO TARGET NAME");
433 wrbuf_xmlputs(wrbuf, ht[i].name);
434 wrbuf_puts(wrbuf, "</name>\n");
436 wrbuf_printf(wrbuf, "<frequency>" ODR_INT_PRINTF "</frequency>\n",
439 wrbuf_puts(wrbuf, "<state>");
440 wrbuf_xmlputs(wrbuf, ht[i].state);
441 wrbuf_puts(wrbuf, "</state>\n");
443 wrbuf_printf(wrbuf, "<diagnostic>%d</diagnostic>\n",
445 wrbuf_puts(wrbuf, "</term>\n");
449 static void cmd_termlist(struct http_channel *c)
451 struct http_response *rs = c->response;
452 struct http_request *rq = c->request;
453 struct http_session *s = locate_session(c);
454 struct termlist_score **p;
457 const char *name = http_argbyname(rq, "name");
458 const char *nums = http_argbyname(rq, "num");
465 status = session_active_clients(s->psession);
469 if (strlen(name) > 255)
474 wrbuf_rewind(c->wrbuf);
476 wrbuf_puts(c->wrbuf, "<termlist>\n");
477 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", status);
483 if (!(tp = strchr(name, ',')))
484 tp = name + strlen(name);
485 strncpy(tname, name, tp - name);
486 tname[tp - name] = '\0';
488 wrbuf_puts(c->wrbuf, "<list name=\"");
489 wrbuf_xmlputs(c->wrbuf, tname);
490 wrbuf_puts(c->wrbuf, "\">\n");
491 if (!strcmp(tname, "xtargets"))
492 targets_termlist(c->wrbuf, s->psession, num, c->nmem);
495 p = termlist(s->psession, tname, &len);
497 for (i = 0; i < len && i < num; i++){
498 // prevnt sending empty term elements
499 if (!p[i]->term || !p[i]->term[0])
502 wrbuf_puts(c->wrbuf, "<term>");
503 wrbuf_puts(c->wrbuf, "<name>");
504 wrbuf_xmlputs(c->wrbuf, p[i]->term);
505 wrbuf_puts(c->wrbuf, "</name>");
507 wrbuf_printf(c->wrbuf,
508 "<frequency>%d</frequency>",
510 wrbuf_puts(c->wrbuf, "</term>\n");
513 wrbuf_puts(c->wrbuf, "</list>\n");
518 wrbuf_puts(c->wrbuf, "</termlist>\n");
519 rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf));
520 http_send_response(c);
524 static void cmd_bytarget(struct http_channel *c)
526 struct http_response *rs = c->response;
527 struct http_request *rq = c->request;
528 struct http_session *s = locate_session(c);
529 struct hitsbytarget *ht;
530 const char *settings = http_argbyname(rq, "settings");
535 ht = hitsbytarget(s->psession, &count, c->nmem);
536 wrbuf_rewind(c->wrbuf);
537 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<bytarget><status>OK</status>");
539 for (i = 0; i < count; i++)
541 wrbuf_puts(c->wrbuf, "\n<target>");
543 wrbuf_puts(c->wrbuf, "<id>");
544 wrbuf_xmlputs(c->wrbuf, ht[i].id);
545 wrbuf_puts(c->wrbuf, "</id>\n");
547 if (ht[i].name && ht[i].name[0])
549 wrbuf_puts(c->wrbuf, "<name>");
550 wrbuf_xmlputs(c->wrbuf, ht[i].name);
551 wrbuf_puts(c->wrbuf, "</name>\n");
554 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
555 wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
556 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
558 wrbuf_puts(c->wrbuf, "<state>");
559 wrbuf_xmlputs(c->wrbuf, ht[i].state);
560 wrbuf_puts(c->wrbuf, "</state>\n");
561 if (settings && *settings == '1')
563 wrbuf_puts(c->wrbuf, "<settings>\n");
564 wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml));
565 wrbuf_puts(c->wrbuf, "</settings>\n");
567 wrbuf_puts(c->wrbuf, "</target>");
568 wrbuf_destroy(ht[i].settings_xml);
571 wrbuf_puts(c->wrbuf, "</bytarget>");
572 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
573 http_send_response(c);
576 static void write_metadata(WRBUF w, struct conf_service *service,
577 struct record_metadata **ml, int full)
581 for (imeta = 0; imeta < service->num_metadata; imeta++)
583 struct conf_metadata *cmd = &service->metadata[imeta];
584 struct record_metadata *md;
585 if (!cmd->brief && !full)
587 for (md = ml[imeta]; md; md = md->next)
589 struct record_metadata_attr *attr = md->attributes;
590 wrbuf_printf(w, "\n<md-%s", cmd->name);
592 for (; attr; attr = attr->next)
594 wrbuf_printf(w, " %s=\"", attr->name);
595 wrbuf_xmlputs(w, attr->value);
601 case Metadata_type_generic:
602 wrbuf_xmlputs(w, md->data.text.disp);
604 case Metadata_type_year:
605 wrbuf_printf(w, "%d", md->data.number.min);
606 if (md->data.number.min != md->data.number.max)
607 wrbuf_printf(w, "-%d", md->data.number.max);
610 wrbuf_puts(w, "[can't represent]");
612 wrbuf_printf(w, "</md-%s>", cmd->name);
617 static void write_subrecord(struct record *r, WRBUF w,
618 struct conf_service *service, int show_details)
620 const char *name = session_setting_oneval(
621 client_get_database(r->client), PZ_NAME);
623 wrbuf_puts(w, "<location id=\"");
624 wrbuf_xmlputs(w, client_get_database(r->client)->database->url);
625 wrbuf_puts(w, "\" ");
627 wrbuf_puts(w, "name=\"");
628 wrbuf_xmlputs(w, *name ? name : "Unknown");
629 wrbuf_puts(w, "\">");
631 write_metadata(w, service, r->metadata, show_details);
632 wrbuf_puts(w, "</location>\n");
635 static void show_raw_record_error(void *data, const char *addinfo)
637 http_channel_observer_t obs = data;
638 struct http_channel *c = http_channel_observer_chan(obs);
639 struct http_response *rs = c->response;
641 http_remove_observer(obs);
643 error(rs, PAZPAR2_RECORD_FAIL, addinfo);
646 static void show_raw_record_ok(void *data, const char *buf, size_t sz)
648 http_channel_observer_t obs = data;
649 struct http_channel *c = http_channel_observer_chan(obs);
650 struct http_response *rs = c->response;
652 http_remove_observer(obs);
654 wrbuf_write(c->wrbuf, buf, sz);
655 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
656 http_send_response(c);
660 static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz)
662 http_channel_observer_t obs = data;
663 struct http_channel *c = http_channel_observer_chan(obs);
664 struct http_response *rs = c->response;
666 http_remove_observer(obs);
668 wrbuf_write(c->wrbuf, buf, sz);
669 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
671 rs->content_type = "application/octet-stream";
672 http_send_response(c);
676 void show_raw_reset(void *data, struct http_channel *c, void *data2)
678 //struct client *client = data;
679 //client_show_raw_remove(client, data2);
682 static void cmd_record_ready(void *data);
684 static void cmd_record(struct http_channel *c)
686 struct http_response *rs = c->response;
687 struct http_request *rq = c->request;
688 struct http_session *s = locate_session(c);
689 struct record_cluster *rec, *prev_r, *next_r;
691 struct conf_service *service;
692 const char *idstr = http_argbyname(rq, "id");
693 const char *offsetstr = http_argbyname(rq, "offset");
694 const char *binarystr = http_argbyname(rq, "binary");
698 service = s->psession->service;
701 error(rs, PAZPAR2_MISSING_PARAMETER, "id");
704 wrbuf_rewind(c->wrbuf);
705 if (!(rec = show_single(s->psession, idstr, &prev_r, &next_r)))
707 if (session_active_clients(s->psession) == 0)
709 error(rs, PAZPAR2_RECORD_MISSING, idstr);
711 else if (session_set_watch(s->psession, SESSION_WATCH_RECORD,
712 cmd_record_ready, c, c) != 0)
714 error(rs, PAZPAR2_RECORD_MISSING, idstr);
720 int offset = atoi(offsetstr);
721 const char *syntax = http_argbyname(rq, "syntax");
722 const char *esn = http_argbyname(rq, "esn");
724 struct record*r = rec->records;
727 if (binarystr && *binarystr != '0')
730 for (i = 0; i < offset && r; r = r->next, i++)
734 error(rs, PAZPAR2_RECORD_FAIL, "no record at offset given");
739 http_channel_observer_t obs =
740 http_add_observer(c, r->client, show_raw_reset);
741 int ret = client_show_raw_begin(r->client, r->position,
744 show_raw_record_error,
746 show_raw_record_ok_binary :
751 http_remove_observer(obs);
752 error(rs, PAZPAR2_NO_SESSION, 0);
758 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<record>\n");
759 wrbuf_puts(c->wrbuf, "<recid>");
760 wrbuf_xmlputs(c->wrbuf, rec->recid);
761 wrbuf_puts(c->wrbuf, "</recid>\n");
764 wrbuf_puts(c->wrbuf, "<prevrecid>");
765 wrbuf_xmlputs(c->wrbuf, prev_r->recid);
766 wrbuf_puts(c->wrbuf, "</prevrecid>\n");
770 wrbuf_puts(c->wrbuf, "<nextrecid>");
771 wrbuf_xmlputs(c->wrbuf, next_r->recid);
772 wrbuf_puts(c->wrbuf, "</nextrecid>\n");
774 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n",
775 session_active_clients(s->psession));
776 write_metadata(c->wrbuf, service, rec->metadata, 1);
777 for (r = rec->records; r; r = r->next)
778 write_subrecord(r, c->wrbuf, service, 1);
779 wrbuf_puts(c->wrbuf, "</record>\n");
780 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
781 http_send_response(c);
785 static void cmd_record_ready(void *data)
787 struct http_channel *c = (struct http_channel *) data;
792 static void show_records(struct http_channel *c, int active)
794 struct http_request *rq = c->request;
795 struct http_response *rs = c->response;
796 struct http_session *s = locate_session(c);
797 struct record_cluster **rl;
798 struct reclist_sortparms *sp;
799 const char *start = http_argbyname(rq, "start");
800 const char *num = http_argbyname(rq, "num");
801 const char *sort = http_argbyname(rq, "sort");
811 // We haven't counted clients yet if we're called on a block release
813 active = session_active_clients(s->psession);
816 startn = atoi(start);
821 if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
823 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
827 rl = show(s->psession, sp, startn, &numn, &total, &total_hits, c->nmem);
829 wrbuf_rewind(c->wrbuf);
830 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<show>\n<status>OK</status>\n");
831 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", active);
832 wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
833 wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
834 wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
835 wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
837 for (i = 0; i < numn; i++)
841 struct record_cluster *rec = rl[i];
842 struct conf_service *service = s->psession->service;
844 wrbuf_puts(c->wrbuf, "<hit>\n");
845 write_metadata(c->wrbuf, service, rec->metadata, 0);
846 for (ccount = 0, p = rl[i]->records; p; p = p->next, ccount++)
847 write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
849 wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
850 if (strstr(sort, "relevance"))
851 wrbuf_printf(c->wrbuf, "<relevance>%d</relevance>\n", rec->relevance);
852 wrbuf_puts(c->wrbuf, "<recid>");
853 wrbuf_xmlputs(c->wrbuf, rec->recid);
854 wrbuf_puts(c->wrbuf, "</recid>\n");
855 wrbuf_puts(c->wrbuf, "</hit>\n");
858 wrbuf_puts(c->wrbuf, "</show>\n");
859 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
860 http_send_response(c);
863 static void show_records_ready(void *data)
865 struct http_channel *c = (struct http_channel *) data;
870 static void cmd_show(struct http_channel *c)
872 struct http_request *rq = c->request;
873 struct http_session *s = locate_session(c);
874 const char *block = http_argbyname(rq, "block");
880 status = session_active_clients(s->psession);
884 if (status && reclist_get_num_records(s->psession->reclist) == 0)
886 // if there is already a watch/block. we do not block this one
887 if (session_set_watch(s->psession, SESSION_WATCH_SHOW,
888 show_records_ready, c, c) != 0)
890 yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
896 show_records(c, status);
899 static void cmd_ping(struct http_channel *c)
901 struct http_response *rs = c->response;
902 struct http_session *s = locate_session(c);
905 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<ping><status>OK</status></ping>";
906 http_send_response(c);
909 static int utf_8_valid(const char *str)
911 yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8");
914 /* check that query is UTF-8 encoded */
915 char *inbuf = (char *) str; /* we know iconv does not alter this */
916 size_t inbytesleft = strlen(inbuf);
918 size_t outbytesleft = strlen(inbuf) + 10;
919 char *out = xmalloc(outbytesleft);
921 size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
923 /* if OK, try flushing the rest */
924 if (r != (size_t) (-1))
925 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
928 if (r == (size_t) (-1))
934 static void cmd_search(struct http_channel *c)
936 struct http_request *rq = c->request;
937 struct http_response *rs = c->response;
938 struct http_session *s = locate_session(c);
939 const char *query = http_argbyname(rq, "query");
940 const char *filter = http_argbyname(rq, "filter");
941 const char *maxrecs = http_argbyname(rq, "maxrecs");
942 const char *startrecs = http_argbyname(rq, "startrecs");
943 enum pazpar2_error_code code;
944 const char *addinfo = 0;
950 error(rs, PAZPAR2_MISSING_PARAMETER, "query");
953 if (!utf_8_valid(query))
955 error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
958 code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo);
961 error(rs, code, addinfo);
964 rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "<search><status>OK</status></search>";
965 http_send_response(c);
969 static void cmd_stat(struct http_channel *c)
971 struct http_response *rs = c->response;
972 struct http_session *s = locate_session(c);
973 struct statistics stat;
981 clients = session_active_clients(s->psession);
982 statistics(s->psession, &stat);
984 if (stat.num_clients > 0) {
985 progress = (stat.num_clients - clients) / (float)stat.num_clients;
988 wrbuf_rewind(c->wrbuf);
989 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<stat>");
990 wrbuf_printf(c->wrbuf, "<activeclients>%d</activeclients>\n", clients);
991 wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", stat.num_hits);
992 wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
993 wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
994 wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
995 wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
996 wrbuf_printf(c->wrbuf, "<working>%d</working>\n", stat.num_working);
997 wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
998 wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
999 wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
1000 wrbuf_printf(c->wrbuf, "<progress>%.2f</progress>\n", progress);
1001 wrbuf_puts(c->wrbuf, "</stat>");
1002 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1003 http_send_response(c);
1006 static void cmd_info(struct http_channel *c)
1008 char yaz_version_str[20];
1009 struct http_response *rs = c->response;
1011 wrbuf_rewind(c->wrbuf);
1012 wrbuf_puts(c->wrbuf, HTTP_COMMAND_RESPONSE_PREFIX "<info>\n");
1013 wrbuf_puts(c->wrbuf, " <version>\n");
1014 wrbuf_puts(c->wrbuf, "<pazpar2");
1015 #ifdef PAZPAR2_VERSION_SHA1
1016 wrbuf_printf(c->wrbuf, " sha1=\"%s\"", PAZPAR2_VERSION_SHA1);
1018 wrbuf_puts(c->wrbuf, ">");
1019 wrbuf_xmlputs(c->wrbuf, VERSION);
1020 wrbuf_puts(c->wrbuf, "</pazpar2>");
1023 yaz_version(yaz_version_str, 0);
1024 wrbuf_puts(c->wrbuf, " <yaz compiled=\"");
1025 wrbuf_xmlputs(c->wrbuf, YAZ_VERSION);
1026 wrbuf_puts(c->wrbuf, "\">");
1027 wrbuf_xmlputs(c->wrbuf, yaz_version_str);
1028 wrbuf_puts(c->wrbuf, "</yaz>\n");
1030 wrbuf_puts(c->wrbuf, " </version>\n");
1032 wrbuf_puts(c->wrbuf, "</info>");
1033 rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf));
1034 http_send_response(c);
1039 void (*fun)(struct http_channel *c);
1041 { "init", cmd_init },
1042 { "settings", cmd_settings },
1043 { "stat", cmd_stat },
1044 { "bytarget", cmd_bytarget },
1045 { "show", cmd_show },
1046 { "search", cmd_search },
1047 { "termlist", cmd_termlist },
1048 { "exit", cmd_exit },
1049 { "ping", cmd_ping },
1050 { "record", cmd_record },
1051 { "info", cmd_info },
1055 void http_command(struct http_channel *c)
1057 const char *command = http_argbyname(c->request, "command");
1058 struct http_response *rs = http_create_response(c);
1063 http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
1064 http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
1068 error(rs, PAZPAR2_MISSING_PARAMETER, "command");
1071 for (i = 0; commands[i].name; i++)
1072 if (!strcmp(commands[i].name, command))
1074 (*commands[i].fun)(c);
1077 if (!commands[i].name)
1078 error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command");
1086 * c-file-style: "Stroustrup"
1087 * indent-tabs-mode: nil
1089 * vim: shiftwidth=4 tabstop=8 expandtab