517ea10dacee7de20bfbd55a4f39dd1c6e6e7571
[pazpar2-moved-to-github.git] / http_command.c
1 /*_response(c, rs);
2  * $Id: http_command.c,v 1.7 2006-12-08 21:40:58 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     http_send_response(c);
69 }
70
71 int make_sessionid()
72 {
73     struct timeval t;
74     int res;
75     static int seq = 0;
76
77     seq++;
78     if (gettimeofday(&t, 0) < 0)
79         abort();
80     res = t.tv_sec;
81     res = (res << 8) | (seq & 0xff);
82     return res;
83 }
84
85 static struct http_session *locate_session(struct http_request *rq, struct http_response *rs)
86 {
87     struct http_session *p;
88     char *session = http_argbyname(rq, "session");
89     int id;
90
91     if (!session)
92     {
93         error(rs, "417", "Must supply session", 0);
94         return 0;
95     }
96     id = atoi(session);
97     for (p = session_list; p; p = p->next)
98         if (id == p->session_id)
99             return p;
100     error(rs, "417", "Session does not exist, or it has expired", 0);
101     return 0;
102 }
103
104 static void cmd_exit(struct http_channel *c)
105 {
106     yaz_log(YLOG_WARN, "exit");
107     exit(0);
108 }
109
110 static void cmd_init(struct http_channel *c)
111 {
112     int sesid;
113     char buf[1024];
114     struct http_session *s = http_session_create();
115     struct http_response *rs = c->response;
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(c->nmem, buf);
123     http_send_response(c);
124 }
125
126 static void cmd_termlist(struct http_channel *c)
127 {
128     struct http_response *rs = c->response;
129     struct http_request *rq = c->request;
130     struct http_session *s = locate_session(rq, rs);
131     struct termlist_score **p;
132     int len;
133     int i;
134
135     if (!s)
136         return;
137     wrbuf_rewind(c->wrbuf);
138
139     wrbuf_puts(c->wrbuf, "<termlist>");
140     p = termlist(s->psession, &len);
141     if (p)
142         for (i = 0; i < len; i++)
143         {
144             wrbuf_puts(c->wrbuf, "\n<term>");
145             wrbuf_printf(c->wrbuf, "<name>%s</name>", p[i]->term);
146             wrbuf_printf(c->wrbuf, "<frequency>%d</frequency>", p[i]->frequency);
147             wrbuf_puts(c->wrbuf, "</term>");
148         }
149     wrbuf_puts(c->wrbuf, "</termlist>");
150     rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_buf(c->wrbuf));
151     http_send_response(c);
152 }
153
154
155 static void cmd_bytarget(struct http_channel *c)
156 {
157     struct http_response *rs = c->response;
158     struct http_request *rq = c->request;
159     struct http_session *s = locate_session(rq, rs);
160     struct hitsbytarget *ht;
161     int count, i;
162
163     if (!s)
164         return;
165     if (!(ht = hitsbytarget(s->psession, &count)))
166     {
167         error(rs, "500", "Failed to retrieve hitcounts", 0);
168         return;
169     }
170     wrbuf_rewind(c->wrbuf);
171     wrbuf_puts(c->wrbuf, "<bytarget><status>OK</status>");
172
173     for (i = 0; i < count; i++)
174     {
175         wrbuf_puts(c->wrbuf, "\n<target>");
176         wrbuf_printf(c->wrbuf, "<id>%s</id>\n", ht[i].id);
177         wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", ht[i].hits);
178         wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
179         wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records);
180         wrbuf_printf(c->wrbuf, "<state>%s</state>\n", ht[i].state);
181         wrbuf_puts(c->wrbuf, "</target>");
182     }
183
184     wrbuf_puts(c->wrbuf, "</bytarget>");
185     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
186     http_send_response(c);
187 }
188
189 static void cmd_show(struct http_channel *c)
190 {
191     struct http_request *rq = c->request;
192     struct http_response *rs = c->response;
193     struct http_session *s = locate_session(rq, rs);
194     struct record **rl;
195     char *start = http_argbyname(rq, "start");
196     char *num = http_argbyname(rq, "num");
197     int startn = 0;
198     int numn = 20;
199     int total;
200     int total_hits;
201     int i;
202
203     if (!s)
204         return;
205
206     if (start)
207         startn = atoi(start);
208     if (num)
209         numn = atoi(num);
210
211     rl = show(s->psession, startn, &numn, &total, &total_hits);
212
213     wrbuf_rewind(c->wrbuf);
214     wrbuf_puts(c->wrbuf, "<show>\n<status>OK</status>\n");
215     wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
216     wrbuf_printf(c->wrbuf, "<total>%d</total>\n", total_hits);
217     wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
218     wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);
219
220     for (i = 0; i < numn; i++)
221     {
222         int ccount;
223         struct record *p;
224
225         wrbuf_puts(c->wrbuf, "<hit>\n");
226         wrbuf_printf(c->wrbuf, "<title>%s</title>\n", rl[i]->title);
227         for (ccount = 1, p = rl[i]->next_cluster; p;  p = p->next_cluster, ccount++)
228             ;
229         if (ccount > 1)
230             wrbuf_printf(c->wrbuf, "<count>%d</count>\n", ccount);
231         wrbuf_puts(c->wrbuf, "</hit>\n");
232     }
233
234     wrbuf_puts(c->wrbuf, "</show>\n");
235     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
236     http_send_response(c);
237 }
238
239 static void cmd_search(struct http_channel *c)
240 {
241     struct http_request *rq = c->request;
242     struct http_response *rs = c->response;
243     struct http_session *s = locate_session(rq, rs);
244     char *query = http_argbyname(rq, "query");
245     char *res;
246
247     if (!s)
248         return;
249     if (!query)
250     {
251         error(rs, "417", "Must supply query", 0);
252         return;
253     }
254     res = search(s->psession, query);
255     if (res)
256     {
257         error(rs, "417", res, res);
258         return;
259     }
260     rs->payload = "<search><status>OK</status></search>";
261     http_send_response(c);
262 }
263
264
265 static void cmd_stat(struct http_channel *c)
266 {
267     struct http_request *rq = c->request;
268     struct http_response *rs = c->response;
269     struct http_session *s = locate_session(rq, rs);
270     struct statistics stat;
271
272     if (!s)
273         return;
274
275     statistics(s->psession, &stat);
276
277     wrbuf_rewind(c->wrbuf);
278     wrbuf_puts(c->wrbuf, "<stat>");
279     wrbuf_printf(c->wrbuf, "<hits>%d</hits>\n", stat.num_hits);
280     wrbuf_printf(c->wrbuf, "<records>%d</records>\n", stat.num_records);
281     wrbuf_printf(c->wrbuf, "<clients>%d</clients>\n", stat.num_clients);
282     wrbuf_printf(c->wrbuf, "<unconnected>%d</unconnected>\n", stat.num_no_connection);
283     wrbuf_printf(c->wrbuf, "<connecting>%d</connecting>\n", stat.num_connecting);
284     wrbuf_printf(c->wrbuf, "<initializing>%d</initializing>\n", stat.num_initializing);
285     wrbuf_printf(c->wrbuf, "<searching>%d</searching>\n", stat.num_searching);
286     wrbuf_printf(c->wrbuf, "<presenting>%d</presenting>\n", stat.num_presenting);
287     wrbuf_printf(c->wrbuf, "<idle>%d</idle>\n", stat.num_idle);
288     wrbuf_printf(c->wrbuf, "<failed>%d</failed>\n", stat.num_failed);
289     wrbuf_printf(c->wrbuf, "<error>%d</error>\n", stat.num_error);
290     wrbuf_puts(c->wrbuf, "</stat>");
291     rs->payload = nmem_strdup(c->nmem, wrbuf_buf(c->wrbuf));
292     http_send_response(c);
293 }
294
295
296 struct {
297     char *name;
298     void (*fun)(struct http_channel *c);
299 } commands[] = {
300     { "init", cmd_init },
301     { "stat", cmd_stat },
302 #ifdef GAGA
303     { "load", cmd_load },
304 #endif
305     { "bytarget", cmd_bytarget },
306     { "show", cmd_show },
307     { "search", cmd_search },
308     { "termlist", cmd_termlist },
309     { "exit", cmd_exit },
310     {0,0}
311 };
312
313 void http_command(struct http_channel *c)
314 {
315     char *command = http_argbyname(c->request, "command");
316     struct http_response *rs = http_create_response(c);
317     int i;
318
319     c->response = rs;
320     if (!command)
321     {
322         error(rs, "417", "Must supply command", 0);
323         return;
324     }
325     for (i = 0; commands[i].name; i++)
326         if (!strcmp(commands[i].name, command))
327         {
328             (*commands[i].fun)(c);
329             break;
330         }
331     if (!commands[i].name)
332         error(rs, "417", "Unknown command", 0);
333
334     return;
335 }
336
337 /*
338  * Local variables:
339  * c-basic-offset: 4
340  * indent-tabs-mode: nil
341  * End:
342  * vim: shiftwidth=4 tabstop=8 expandtab
343  */