Improve plugin handler return values and actions
authorEduardo Silva <edsiper@gmail.com>
Thu, 21 Jan 2010 16:18:05 +0000 (13:18 -0300)
committerEduardo Silva <edsiper@gmail.com>
Thu, 21 Jan 2010 16:18:05 +0000 (13:18 -0300)
plugins/README
src/http.c
src/include/plugin.h
src/include/request.h
src/plugin.c
src/request.c

index c36cf4b..cb11cb5 100644 (file)
@@ -12,5 +12,17 @@ MK_PLUGIN_STAGE_10: Server has not yet entered in the server loop, no
 MK_PLUGIN_STAGE_20: Accepted connection has not been assigned to worker thread
 MK_PLUGIN_STAGE_30: HTTP Request received
 MK_PLUGIN_STAGE_40: Object Handler
+
+                    Return Values:
+                    --------------
+                    * MK_PLUGIN_RET_NOT_ME: Plugin will not handle this request.
+
+                    * MK_PLUGIN_RET_END: Plugin has taken some action and 
+                    has finished the work, the handler will no take the request
+                    again.
+
+                    * MK_PLUGIN_RET_CONTINUE:: Plugin has taken some action and 
+                    will continue in the next loop.
+
 MK_PLUGIN_STAGE_50: Request ended
 MK_PLUGIN_STAGE_60: Connection closed
index e4bf252..d847eea 100644 (file)
@@ -2,7 +2,7 @@
 
 /*  Monkey HTTP Daemon
  *  ------------------
- *  Copyright (C) 2001-2008, Eduardo Silva P.
+ *  Copyright (C) 2001-2010, Eduardo Silva P.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
index 6bc9cc5..6d42309 100644 (file)
@@ -41,8 +41,9 @@
 #define MK_PLUGIN_STAGE_50 ((__uint32_t) 16) /* Request ended */
 #define MK_PLUGIN_STAGE_60 ((__uint32_t) 32) /* Connection closed */
 
-#define MK_PLUGIN_RET_CLOSE_CONX 100
-#define MK_PLUGIN_RET_OWNER 200
+#define MK_PLUGIN_RET_NOT_ME -1
+#define MK_PLUGIN_RET_OWNER 100
+#define MK_PLUGIN_RET_CLOSE_CONX 200
 #define MK_PLUGIN_RET_CONTINUE 300
 
 struct plugin_stages {
index f1f439e..ac64e1f 100644 (file)
@@ -147,6 +147,13 @@ struct header_toc {
         struct header_toc *next;
 };
 
+/* Request plugin Handler, each request can be handled by 
+ * several plugins, we handle list in a simple list */
+struct handler {
+        struct plugin *p;
+        struct handler *next;
+};
+
 struct request {
        int status;
        int pipelined; /* Pipelined request */
@@ -213,6 +220,9 @@ struct request {
         long loop;
        long bytes_to_send;
         off_t bytes_offset;
+
+        /* Plugin handlers */
+        struct handler *handled_by;
 };
 
 struct header_values {
index fe1d918..351eccf 100644 (file)
@@ -306,8 +306,15 @@ int mk_plugin_stage_run(mk_plugin_stage_t stage,
                 p = config->plugins->stage_40;
                 while(p){
                         ret = p->call_stage_40(cr, sr);
-                        if(ret == 0){
-                                return 0;
+                        switch(ret){
+                        case MK_PLUGIN_RET_NOT_ME:
+                                break;
+                        case MK_PLUGIN_RET_END:
+                                break;
+                        case MK_PLUGIN_RET_CONTINUE:
+                                /* Register plugin for next loops */
+                                mk_request_handler_register(sr, p);
+                                break;
                         }
                         p = p->next;
                 }
index 54bd6a7..b60c4e8 100644 (file)
@@ -766,6 +766,7 @@ struct request *mk_request_alloc()
        /* Response Headers */
        request->headers = mk_header_create();
 
+        request->handled_by = NULL;
        return request;
 }
 
@@ -1025,3 +1026,50 @@ void mk_request_ka_next(struct client_request *cr)
         cr->body_length = 0;
         cr->counter_connections++;
 }
+
+void mk_request_handler_register(struct request *sr, struct plugin *p)
+{
+        struct handler *new, *aux;
+
+        new = mk_mem_malloc(sizeof(struct handler));
+        new->p = p;
+        new->next = NULL;
+
+        if(!sr->handled_by){
+                sr->handled_by = new;
+                return;
+        }
+
+        aux = sr->handled_by;
+        while(aux){
+                if(!aux->next){
+                        aux->next = new;
+                        return;
+                }
+                aux = aux->next;
+        }
+        
+        printf("\nMK_REQUEST_REGISTER_HANDLER: NEVER ASSIGNED");
+        fflush(stdout);
+}
+
+void mk_request_handler_clear(struct request *sr)
+{
+        struct handler *prev=0, *aux;
+
+        if(!sr->handled_by){
+                return;
+        }
+
+        while(sr->handled_by){
+                aux = sr->handled_by;
+                while(aux->next){
+                        prev = aux;
+                        aux = aux->next;
+                }
+                mk_mem_free(aux);
+                prev->next = NULL;
+        }
+
+        return;
+}