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