diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 76 |
1 files changed, 66 insertions, 10 deletions
@@ -7,9 +7,11 @@ #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> +#include <dlfcn.h> #include <errno.h> #include "http.h" #include "memory.h" +#include "plugin.h" #include "util.h" @@ -37,27 +39,81 @@ static void signal_handler(int sig){ } +typedef struct Plugin{ + char *name; + Handler handler; + struct Plugin *next; +} Plugin; + +Plugin *pluginlist=NULL,*pluginlist_last=NULL; + +static void plugin_register_callback(const char *name,Handler handler){ + if(pluginlist==NULL){ + pluginlist=pluginlist_last=malloc(1,Plugin); + pluginlist->name=copy_str(name); + pluginlist->handler=handler; + pluginlist->next=NULL; + } else { + Plugin *newplugin=malloc(1,Plugin); + newplugin->name=copy_str(name); + newplugin->handler=handler; + newplugin->next=NULL; + pluginlist_last->next=newplugin; + pluginlist_last=newplugin; + } + printf("Registered plugin '%s' (handler %p)\n",name,handler); +} + +typedef void (*register_function)(register_callback_t callback); + +static void load_plugin(const char *name){ + void *lib=dlopen(name,RTLD_NOW|RTLD_LOCAL); + if(lib==NULL){ + fprintf(stderr,"dlopen(%s): %s\n",name,dlerror()); + exit(1); + } + register_function regfunc=(register_function)dlsym(lib,"plugin_register_yourself"); + if(regfunc==NULL){ + fprintf(stderr,"Cannot find plugin_register_yourself symbol in library '%s'\n",name); + exit(1); + } + regfunc(&plugin_register_callback); +} + + static void connection_handler(int sock){ Headers *headers=http_get_headers(sock); if(headers==NULL){ return; } - sendall(sock,"HTTP/1.1 200 OK\r\n" - "Content-Type: text/plain; charset=UTF-8\r\n" - "Server: cserver\r\n" - "\r\n",-1); + for(Plugin *pl=pluginlist;pl!=NULL;pl=pl->next){ + switch(pl->handler(sock,headers)){ + case HR_HANDLED: + printf("Request handled by %s\n",pl->name); + break; + + case HR_NEXT: + continue; - sendallf(sock,"Method: %s\n",headers->method); - sendallf(sock,"URL: %s\n",headers->url); - sendallf(sock,"Version: %s\n",headers->version); - for(int i=0;i<headers->nheaders;i++){ - sendallf(sock,"Header '%s': '%s'\n",headers->headers[i][0],headers->headers[i][1]); + case HR_ERROR: + printf("Request handler '%s' threw error\n",pl->name); + break; + } + break; } } -int main(void){ +int main(int argc,char **argv){ + if(argc==1){ + printf("Loading no plugin files\n"); + } else { + for(int i=1;i<argc;i++){ + load_plugin(argv[i]); + } + } + int sock=socket(PF_INET,SOCK_STREAM,0); if(sock<0){ perror("socket"); |