-/*_response(c, rs);
- * $Id: http_command.c,v 1.7 2006-12-08 21:40:58 quinn Exp $
+/*
+ * $Id: http_command.c,v 1.9 2006-12-17 13:42:47 quinn Exp $
*/
#include <stdio.h>
#include "http.h"
#include "http_command.h"
+extern struct parameters global_parameters;
+extern IOCHAN channel_list;
+
struct http_session {
+ IOCHAN timeout_iochan; // NOTE: This is NOT associated with a socket
struct session *psession;
- int session_id;
+ unsigned int session_id;
int timestamp;
struct http_session *next;
};
static struct http_session *session_list = 0;
+void http_session_destroy(struct http_session *s);
+
+static void session_timeout(IOCHAN i, int event)
+{
+ struct http_session *s = iochan_getdata(i);
+ http_session_destroy(s);
+}
+
struct http_session *http_session_create()
{
struct http_session *r = xmalloc(sizeof(*r));
r->timestamp = 0;
r->next = session_list;
session_list = r;
+ r->timeout_iochan = iochan_create(-1, session_timeout, 0);
+ iochan_setdata(r->timeout_iochan, r);
+ iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout);
+ r->timeout_iochan->next = channel_list;
+ channel_list = r->timeout_iochan;
return r;
}
*p = (*p)->next;
break;
}
- session_destroy(s->psession);
+ iochan_destroy(s->timeout_iochan);
+ destroy_session(s->psession);
xfree(s);
}
http_send_response(c);
}
-int make_sessionid()
+unsigned int make_sessionid()
{
struct timeval t;
- int res;
+ unsigned int res;
static int seq = 0;
seq++;
if (gettimeofday(&t, 0) < 0)
abort();
res = t.tv_sec;
- res = (res << 8) | (seq & 0xff);
+ res = ((res << 8) | (seq & 0xff)) & ((unsigned int) (1 << 31) - 1);
return res;
}
{
struct http_session *p;
char *session = http_argbyname(rq, "session");
- int id;
+ unsigned int id;
if (!session)
{
id = atoi(session);
for (p = session_list; p; p = p->next)
if (id == p->session_id)
+ {
+ iochan_activity(p->timeout_iochan);
return p;
+ }
error(rs, "417", "Session does not exist, or it has expired", 0);
return 0;
}
exit(0);
}
+
static void cmd_init(struct http_channel *c)
{
- int sesid;
+ unsigned int sesid;
char buf[1024];
struct http_session *s = http_session_create();
struct http_response *rs = c->response;
- // FIXME create a pazpar2 session
yaz_log(YLOG_DEBUG, "HTTP Session init");
sesid = make_sessionid();
s->session_id = sesid;
- sprintf(buf, "<init><status>OK</status><session>%d</session></init>", sesid);
+ sprintf(buf, "<init><status>OK</status><session>%u</session></init>", sesid);
rs->payload = nmem_strdup(c->nmem, buf);
http_send_response(c);
}
http_send_response(c);
}
-static void cmd_show(struct http_channel *c)
+static void show_records(struct http_channel *c)
{
struct http_request *rq = c->request;
struct http_response *rs = c->response;
http_send_response(c);
}
+static void show_records_ready(void *data)
+{
+ struct http_channel *c = (struct http_channel *) data;
+
+ show_records(c);
+}
+
+static void cmd_show(struct http_channel *c)
+{
+ struct http_request *rq = c->request;
+ struct http_response *rs = c->response;
+ struct http_session *s = locate_session(rq, rs);
+ char *block = http_argbyname(rq, "block");
+
+ if (!s)
+ return;
+
+ if (block)
+ {
+ if (!s->psession->reclist || !s->psession->reclist->num_records)
+ {
+ session_set_watch(s->psession, SESSION_WATCH_RECORDS, show_records_ready, c);
+ yaz_log(YLOG_DEBUG, "Blocking on cmd_show");
+ return;
+ }
+ }
+
+ show_records(c);
+}
+
+static void cmd_ping(struct http_channel *c)
+{
+ struct http_request *rq = c->request;
+ struct http_response *rs = c->response;
+ struct http_session *s = locate_session(rq, rs);
+ if (!s)
+ return;
+ rs->payload = "<ping><status>OK</status></ping>";
+ http_send_response(c);
+}
+
static void cmd_search(struct http_channel *c)
{
struct http_request *rq = c->request;
} commands[] = {
{ "init", cmd_init },
{ "stat", cmd_stat },
-#ifdef GAGA
- { "load", cmd_load },
-#endif
{ "bytarget", cmd_bytarget },
{ "show", cmd_show },
{ "search", cmd_search },
{ "termlist", cmd_termlist },
{ "exit", cmd_exit },
+ { "ping", cmd_ping },
{0,0}
};