Paging support, small bug fixes
[pazpar2-moved-to-github.git] / http_command.c
1 /*
2  * stat->num_hits = s->total_hits;
3  * stat->num_records = s->total_records;
4  * $Id: http_command.c,v 1.4 2006-11-27 19:44:26 quinn Exp $
5  */
6
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/uio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <strings.h>
13 #include <ctype.h>
14 #include <sys/time.h>
15
16 #include <yaz/yaz-util.h>
17
18 #include "command.h"
19 #include "util.h"
20 #include "eventl.h"
21 #include "pazpar2.h"
22 #include "http.h"
23 #include "http_command.h"
24
25 struct http_session {
26     struct session *psession;
27     int session_id;
28     int timestamp;
29     struct http_session *next;
30 };
31
32 static struct http_session *session_list = 0;
33
34 struct http_session *http_session_create()
35 {
36     struct http_session *r = xmalloc(sizeof(*r));
37     r->psession = new_session();
38     r->session_id = 0;
39     r->timestamp = 0;
40     r->next = session_list;
41     session_list = r;
42     return r;
43 }
44
45 void http_session_destroy(struct http_session *s)
46 {
47     struct http_session **p;
48
49     for (p = &session_list; *p; p = &(*p)->next)
50         if (*p == s)
51         {
52             *p = (*p)->next;
53             break;
54         }
55     session_destroy(s->psession);
56     xfree(s);
57 }
58
59 static void error(struct http_response *rs, char *code, char *msg, char *txt)
60 {
61     struct http_channel *c = rs->channel;
62     char tmp[1024];
63
64     if (!txt)
65         txt = msg;
66     rs->msg = nmem_strdup(c->nmem, msg);
67     strcpy(rs->code, code);
68     sprintf(tmp, "<error code=\"general\">%s</error>", txt);
69     rs->payload = nmem_strdup(c->nmem, tmp);
70 }
71
72 int make_sessionid()
73 {
74     struct timeval t;
75     int res;
76     static int seq = 0;
77
78     seq++;
79     if (gettimeofday(&t, 0) < 0)
80         abort();
81     res = t.tv_sec;
82     res = (res << 8) | (seq & 0xff);
83     return res;
84 }
85
86 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
87 {
88     struct http_session *p;
89     char *session = http_argbyname(rq, "session");
90     int id;
91
92     if (!session)
93     {
94         error(rs, "417", "Must supply session", 0);
95         return 0;
96     }
97     id = atoi(session);
98     for (p = session_list; p; p = p->next)
99         if (id == p->session_id)
100             return p;
101     error(rs, "417", "Session does not exist, or it has expired", 0);
102     return 0;
103 }
104
105 static void cmd_exit(struct http_request *rq, struct http_response *rs)
106 {
107     yaz_log(YLOG_WARN, "exit");
108     exit(0);
109 }
110
111 static void cmd_init(struct http_request *rq, struct http_response *rs)
112 {
113     int sesid;
114     char buf[1024];
115     struct http_session *s = http_session_create();
116
117     // FIXME create a pazpar2 session
118     yaz_log(YLOG_DEBUG, "HTTP Session init");
119     sesid = make_sessionid();
120     s->session_id = sesid;
121     sprintf(buf, "<init><status>OK</status><session>%d</session></init>", sesid);
122     rs->payload = nmem_strdup(rq->channel->nmem, buf);
123 }
124
125 static void cmd_termlist(struct http_request *rq, struct http_response *rs)
126 {
127     struct http_session *s = locate_session(rq, rs);
128     struct http_channel *c = rq->channel;
129     struct termlist_score **p;
130     int len;
131     int i;
132
133     if (!s)
134         return;
135     wrbuf_rewind(c->wrbuf);
136
137     wrbuf_puts(c->wrbuf, "<termlist>");
138     p = termlist(s->psession, &len);
139     if (p)
140         for (i = 0; i < len; i++)
141         {
142             wrbuf_puts(c->wrbuf, "\n<term>");
143             wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
144             wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
145             wrbuf_puts(c->wrbuf, "</term>");
146         }
147     wrbuf_puts(c->wrbuf, "</termlist>");
148     rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
149 }
150
151
152 static void cmd_bytarget(struct http_request *rq, struct http_response *rs)
153 {
154     struct http_session *s = locate_session(rq, rs);
155     struct http_channel *c = rq->channel;
156     struct hitsbytarget *ht;
157     int count, i;
158
159     if (!s)
160         return;
161     if (!(ht = hitsbytarget(s->psession, &count)))
162     {
163         error(rs, "500", "Failed to retrieve hitcounts", 0);
164         return;
165     }
166     wrbuf_rewind(c->wrbuf);
167     wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
168
169     for (i = 0; i < count; i++)
170     {
171         wrbuf_puts(c->wrbuf, "\n<target>");
172         wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
173         wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
174         wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
175         wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
176         wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
177         wrbuf_puts(c->wrbuf, "</target>");
178     }
179
180     wrbuf_puts(c->wrbuf, "</bytarget>");
181     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
182 }
183
184 static void cmd_show(struct http_request *rq, struct http_response *rs)
185 {
186     struct http_session *s = locate_session(rq, rs);
187     struct http_channel *c = rq->channel;
188     struct record **rl;
189     char *start = http_argbyname(rq, "start");
190     char *num = http_argbyname(rq, "num");
191     int startn = 0;
192     int numn = 20;
193     int total;
194     int total_hits;
195     int i;
196
197     if (!s)
198         return;
199
200     if (start)
201         startn = atoi(start);
202     if (num)
203         numn = atoi(num);
204
205     rl = show(s->psession, startn, &numn, &total, &total_hits);
206
207     wrbuf_rewind(c->wrbuf);
208     wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
209     wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
210     wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
211     wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
212     wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
213
214     for (i = 0; i < numn; i++)
215     {
216         int ccount;
217         struct record *p;
218
219         wrbuf_puts(c->wrbuf, "<hit>\n");
220         wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
221         for (ccount = 1, p = rl[i]->next_cluster; p;  p = p->next_cluster, ccount++)
222             ;
223         if (ccount > 1)
224             wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
225         wrbuf_puts(c->wrbuf, "</hit>\n");
226     }
227
228     wrbuf_puts(c->wrbuf, "</show>\n");
229     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
230 }
231
232 static void cmd_search(struct http_request *rq, struct http_response *rs)
233 {
234     struct http_session *s = locate_session(rq, rs);
235     char *query = http_argbyname(rq, "query");
236     char *res;
237
238     if (!s)
239         return;
240     if (!query)
241     {
242         error(rs, "417", "Must supply query", 0);
243         return;
244     }
245     res = search(s->psession, query);
246     if (res)
247     {
248         error(rs, "417", res, res);
249         return;
250     }
251     rs->payload = "<search><status>OK</status></search>";
252 }
253
254
255 static void cmd_stat(struct http_request *rq, struct http_response *rs)
256 {
257     struct http_session *s = locate_session(rq, rs);
258     struct http_channel *c = rq->channel;
259     struct statistics stat;
260
261     if (!s)
262         return;
263
264     statistics(s->psession, &stat);
265
266     wrbuf_rewind(c->wrbuf);
267     wrbuf_puts(c->wrbuf, "<stat>");
268     wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
269     wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
270     wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
271     wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
272     wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
273     wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
274     wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
275     wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
276     wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
277     wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
278     wrbuf_puts(c->wrbuf, "</stat>");
279     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
280 }
281
282 static void cmd_load(struct http_request *rq, struct http_response *rs)
283 {
284     struct http_session *s = locate_session(rq, rs);
285     char *fn = http_argbyname(rq, "name");
286
287     if (!s)
288         return;
289     if (!fn)
290     {
291         error(rs, "417", "Must suppply name", 0);
292         return;
293     }
294     if (load_targets(s->psession, fn) < 0)
295         error(rs, "417", "Failed to find targets", "Possibly wrong filename");
296     else
297         rs->payload = "<load><status>OK</status></load>";
298 }
299
300 struct {
301     char *name;
302     void (*fun)(struct http_request *rq, struct http_response *rs);
303 } commands[] = {
304     { "init", cmd_init },
305     { "stat", cmd_stat },
306     { "load", cmd_load },
307     { "bytarget", cmd_bytarget },
308     { "show", cmd_show },
309     { "search", cmd_search },
310     { "termlist", cmd_termlist },
311     { "exit", cmd_exit },
312     {0,0}
313 };
314
315 struct http_response *http_command(struct http_request *rq)
316 {
317     char *command = http_argbyname(rq, "command");
318     struct http_channel *c = rq->channel;
319     struct http_response *rs = http_create_response(c);
320     int i;
321
322     if (!command)
323     {
324         error(rs, "417", "Must supply command", 0);
325         return rs;
326     }
327     for (i = 0; commands[i].name; i++)
328         if (!strcmp(commands[i].name, command))
329         {
330             (*commands[i].fun)(rq, rs);
331             break;
332         }
333     if (!commands[i].name)
334         error(rs, "417", "Unknown command", 0);
335
336     return rs;
337 }
338
339 /*
340  * Local variables:
341  * c-basic-offset: 4
342  * indent-tabs-mode: nil
343  * End:
344  * vim: shiftwidth=4 tabstop=8 expandtab
345  */