#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "admin.h"
#include "../util/pblock.h"
#include "../config/conf.h"
#include "../config/objconf.h"
static Page *root_page;
int admin_init(pblock *pb, Session *sn, Request *rq) {
log_ereport(
LOG_VERBOSE,
"admin-init");
pool_handle_t *pool = pool_create();
root_page = admin_page_create(pool,
NULL, admin_root);
Page *ls_page = admin_page_create(pool,
"listener", adm_listener);
admin_add_page(root_page, ls_page);
Page *cfgls_page = admin_page_create(pool,
NULL, adm_cfglistener);
admin_add_page(ls_page, cfgls_page);
Page *auth_page = admin_page_create(pool,
"auth", adm_auth);
admin_add_page(root_page, auth_page);
return REQ_PROCEED;
}
int admin_service(pblock *pb, Session *sn, Request *rq) {
pblock_removekey(pb_key_content_type, rq->srvhdrs);
pblock_nvinsert(
"content-type",
"text/plain", rq->srvhdrs);
protocol_status(sn, rq,
200,
NULL);
http_start_response(sn, rq);
char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
uri++;
uri = strchr(uri,
'/');
size_t uri_len = uri ? strlen(uri) :
0;
int s =
0;
int i =
1;
Page *page = root_page;
for(;i<=uri_len;i++) {
char c = uri[i];
if(c ==
'/' || c ==
'\0') {
if(i-s >
1) {
char *name = uri+s+
1;
size_t name_len = i-s-
1;
Page *child = page->children;
page =
NULL;
while(child) {
if(child->name) {
if(!strncmp(child->name, name, name_len)) {
page = child;
break;
}
}
else {
page = child;
break;
}
child = child->next;
}
if(!page) {
break;
}
s = i;
}
}
}
if(page) {
AdminRequest arq;
arq.sn = sn;
arq.rq = rq;
page->service(page, &arq);
}
else {
net_printf(sn->csd,
"page not found\n");
}
return REQ_PROCEED;
}
int admin_root(Page *page, AdminRequest *rq) {
return REQ_PROCEED;
}
int adm_listener(Page *page, AdminRequest *rq) {
SYS_NETFD out = rq->sn->csd;
return REQ_PROCEED;
}
int adm_cfglistener(Page *page, AdminRequest *rq) {
printf(
"adm_cfglistener\n");
return REQ_PROCEED;
}
int adm_auth(Page *page, AdminRequest *rq) {
printf(
"adm_auth\n");
return REQ_PROCEED;
}
Page* admin_page_create(
pool_handle_t *pool,
char *name, admin_service_f f) {
Page *p = pool_malloc(pool,
sizeof(Page));
ZERO(p,
sizeof(Page));
if(name) {
p->name = pool_strdup(pool, name);
}
p->service = f;
return p;
}
void admin_add_page(Page *parent, Page *child) {
child->parent = parent;
Page *ch = parent->children;
if(ch) {
while(ch->next) {
ch = ch->next;
}
ch->next = child;
}
else {
parent->children = child;
}
}