UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2013 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 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 // start with second character to skip leading '/' 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 // we have the path element name 77 char *name = uri+s+1; 78 size_t name_len = i-s-1; 79 80 // get matching admin page 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 // service admin page 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 // public admin API 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