1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32
33 #include "admin.h"
34 #include "../util/pblock.h"
35
36 #include "../config/conf.h"
37 #include "../config/serverconf.h"
38 #include "../config/objconf.h"
39
40 static Page *root_page;
41
42 int admin_init(pblock *pb, Session *sn, Request *rq) {
43 log_ereport(
LOG_VERBOSE,
"admin-init");
44 pool_handle_t *pool = pool_create();
45 root_page = admin_page_create(pool,
NULL, admin_root);
46
47 Page *ls_page = admin_page_create(pool,
"listener", adm_listener);
48 admin_add_page(root_page, ls_page);
49 Page *cfgls_page = admin_page_create(pool,
NULL, adm_cfglistener);
50 admin_add_page(ls_page, cfgls_page);
51
52 Page *auth_page = admin_page_create(pool,
"auth", adm_auth);
53 admin_add_page(root_page, auth_page);
54
55 return REQ_PROCEED;
56 }
57
58 int admin_service(pblock *pb, Session *sn, Request *rq) {
59 pblock_removekey(pb_key_content_type, rq->srvhdrs);
60 pblock_nvinsert(
"content-type",
"text/plain", rq->srvhdrs);
61 protocol_status(sn, rq,
200,
NULL);
62 http_start_response(sn, rq);
63
64 char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
65 uri++;
66 uri = strchr(uri,
'/');
67 size_t uri_len = uri ? strlen(uri) :
0;
68
69 int s =
0;
70 int i =
1;
71 Page *page = root_page;
72 for(;i<=uri_len;i++) {
73 char c = uri[i];
74 if(c ==
'/' || c ==
'\0') {
75 if(i-s >
1) {
76
77 char *name = uri+s+
1;
78 size_t name_len = i-s-
1;
79
80
81 Page *child = page->children;
82 page =
NULL;
83 while(child) {
84 if(child->name) {
85 if(!strncmp(child->name, name, name_len)) {
86 page = child;
87 break;
88 }
89 }
else {
90 page = child;
91 break;
92 }
93 child = child->next;
94 }
95
96 if(!page) {
97 break;
98 }
99
100 s = i;
101 }
102 }
103 }
104
105
106 if(page) {
107 AdminRequest arq;
108 arq.sn = sn;
109 arq.rq = rq;
110 page->service(page, &arq);
111 }
else {
112 net_printf(sn->csd,
"page not found\n");
113 }
114
115 return REQ_PROCEED;
116 }
117
118 int admin_root(Page *page, AdminRequest *rq) {
119 return REQ_PROCEED;
120 }
121
122 int adm_listener(Page *page, AdminRequest *rq) {
123 SYS_NETFD out = rq->sn->csd;
124
125
126
127 return REQ_PROCEED;
128 }
129
130 int adm_cfglistener(Page *page, AdminRequest *rq) {
131 printf(
"adm_cfglistener\n");
132 return REQ_PROCEED;
133 }
134
135 int adm_auth(Page *page, AdminRequest *rq) {
136 printf(
"adm_auth\n");
137 return REQ_PROCEED;
138 }
139
140
141
142
143 Page* admin_page_create(
pool_handle_t *pool,
char *name, admin_service_f f) {
144 Page *p = pool_malloc(pool,
sizeof(Page));
145 ZERO(p,
sizeof(Page));
146 if(name) {
147 p->name = pool_strdup(pool, name);
148 }
149 p->service = f;
150 return p;
151 }
152
153 void admin_add_page(Page *parent, Page *child) {
154 child->parent = parent;
155 Page *ch = parent->children;
156 if(ch) {
157 while(ch->next) {
158 ch = ch->next;
159 }
160 ch->next = child;
161 }
else {
162 parent->children = child;
163 }
164 }
165