Added basic HTTP server logic
[pazpar2-moved-to-github.git] / http_command.c
1 /*
2  * $Id: http_command.c,v 1.1 2006-11-21 18:46:43 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
13 #include <yaz/yaz-util.h>
14
15 #include "command.h"
16 #include "util.h"
17 #include "eventl.h"
18 #include "pazpar2.h"
19 #include "http.h"
20 #include "http_command.h"
21
22 struct http_session {
23     struct session *psession;
24     char session_id[128];
25     int timestamp;
26     struct http_session *next;
27 };
28
29 static struct http_session *session_list = 0;
30
31 struct http_session *http_session_create()
32 {
33     struct http_session *r = xmalloc(sizeof(*r));
34     r->psession = 0;
35     *r->session_id = '\0';
36     r->timestamp = 0;
37     r->next = session_list;
38     session_list = r;
39     return r;
40 }
41
42 void http_session_destroy(struct http_session *s)
43 {
44     struct http_session **p;
45
46     for (p = &session_list; *p; p = &(*p)->next)
47         if (*p == s)
48         {
49             *p = (*p)->next;
50             break;
51         }
52     session_destroy(s->psession);
53     xfree(s);
54 }
55
56 static void error(struct http_response *rs, char *code, char *msg, char *txt)
57 {
58     struct http_channel *c = rs->channel;
59     char tmp[1024];
60
61     if (!txt)
62         txt = msg;
63     rs->msg = nmem_strdup(c->nmem, msg);
64     strcpy(rs->code, code);
65     sprintf(tmp, "<error code=\"general\">%s</error>", txt);
66     rs->payload = nmem_strdup(c->nmem, tmp);
67 }
68
69 static void cmd_init(struct http_request *rq, struct http_response *rs)
70 {
71 }
72
73 static void cmd_stat(struct http_request *rq, struct http_response *rs)
74 {
75 }
76
77 static void cmd_load(struct http_request *rq, struct http_response *rs)
78 {
79 }
80
81 struct {
82     char *name;
83     void (*fun)(struct http_request *rq, struct http_response *rs);
84 } commands[] = {
85     { "init", cmd_init },
86     { "stat", cmd_stat },
87     { "load", cmd_load },
88     {0,0}
89 };
90
91 struct http_response *http_command(struct http_request *rq)
92 {
93     char *command = argbyname(rq, "command");
94     struct http_channel *c = rq->channel;
95     struct http_response *rs = http_create_response(c);
96     int i;
97
98     if (!command)
99     {
100         error(rs, "417", "Must supply command", 0);
101         return rs;
102     }
103     for (i = 0; commands[i].name; i++)
104         if (!strcmp(commands[i].name, command))
105         {
106             (*commands[i].fun)(rq, rs);
107             break;
108         }
109     if (!commands[i].name)
110         error(rs, "417", "Unknown command", 0);
111
112     return rs;
113 }
114
115 /*
116  * Local variables:
117  * c-basic-offset: 4
118  * indent-tabs-mode: nil
119  * End:
120  * vim: shiftwidth=4 tabstop=8 expandtab
121  */