Various features added
[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.3 2006-11-27 14:35:15 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 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);
204
205     wrbuf_rewind(c->wrbuf);
206     wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
207
208     for (i = 0; i < numn; i++)
209     {
210         int ccount;
211         struct record *p;
212
213         wrbuf_puts(c->wrbuf, "<hit>\n");
214         wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
215         for (ccount = 1, p = rl[i]->next_cluster; p;  p = p->next_cluster, ccount++)
216             ;
217         if (ccount > 1)
218             wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
219         wrbuf_puts(c->wrbuf, "</hit>\n");
220     }
221
222     wrbuf_puts(c->wrbuf, "</show>\n");
223     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
224 }
225
226 static void cmd_search(struct http_request *rq, struct http_response *rs)
227 {
228     struct http_session *s = locate_session(rq, rs);
229     char *query = http_argbyname(rq, "query");
230     char *res;
231
232     if (!s)
233         return;
234     if (!query)
235     {
236         error(rs, "417", "Must supply query", 0);
237         return;
238     }
239     res = search(s->psession, query);
240     if (res)
241     {
242         error(rs, "417", res, res);
243         return;
244     }
245     rs->payload = "<search><status>OK</status></search>";
246 }
247
248
249 static void cmd_stat(struct http_request *rq, struct http_response *rs)
250 {
251     struct http_session *s = locate_session(rq, rs);
252     struct http_channel *c = rq->channel;
253     struct statistics stat;
254
255     if (!s)
256         return;
257
258     statistics(s->psession, &stat);
259
260     wrbuf_rewind(c->wrbuf);
261     wrbuf_puts(c->wrbuf, "<stat>");
262     wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
263     wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
264     wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
265     wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
266     wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
267     wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
268     wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
269     wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
270     wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
271     wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
272     wrbuf_puts(c->wrbuf, "</stat>");
273     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
274 }
275
276 static void cmd_load(struct http_request *rq, struct http_response *rs)
277 {
278     struct http_session *s = locate_session(rq, rs);
279     char *fn = http_argbyname(rq, "name");
280
281     if (!s)
282         return;
283     if (!fn)
284     {
285         error(rs, "417", "Must suppply name", 0);
286         return;
287     }
288     if (load_targets(s->psession, fn) < 0)
289         error(rs, "417", "Failed to find targets", "Possibly wrong filename");
290     else
291         rs->payload = "<load><status>OK</status></load>";
292 }
293
294 struct {
295     char *name;
296     void (*fun)(struct http_request *rq, struct http_response *rs);
297 } commands[] = {
298     { "init", cmd_init },
299     { "stat", cmd_stat },
300     { "load", cmd_load },
301     { "bytarget", cmd_bytarget },
302     { "show", cmd_show },
303     { "search", cmd_search },
304     { "termlist", cmd_termlist },
305     { "exit", cmd_exit },
306     {0,0}
307 };
308
309 struct http_response *http_command(struct http_request *rq)
310 {
311     char *command = http_argbyname(rq, "command");
312     struct http_channel *c = rq->channel;
313     struct http_response *rs = http_create_response(c);
314     int i;
315
316     if (!command)
317     {
318         error(rs, "417", "Must supply command", 0);
319         return rs;
320     }
321     for (i = 0; commands[i].name; i++)
322         if (!strcmp(commands[i].name, command))
323         {
324             (*commands[i].fun)(rq, rs);
325             break;
326         }
327     if (!commands[i].name)
328         error(rs, "417", "Unknown command", 0);
329
330     return rs;
331 }
332
333 /*
334  * Local variables:
335  * c-basic-offset: 4
336  * indent-tabs-mode: nil
337  * End:
338  * vim: shiftwidth=4 tabstop=8 expandtab
339  */