From d513c354f7295f618b794c76054362ce3e9b956c Mon Sep 17 00:00:00 2001
From: hamza zia <ziahamza2007@gmail.com>
Date: Wed, 17 Jul 2013 13:31:30 +0200
Subject: [PATCH] Run STAGE40 plugins if they have more pending data

Currently, STAGE40 plugins are only called once if monkey has no pending
data to send to the socket, but the plugin may want to send more data
over multiple cycles (to not block the socket). This patch fixes that.
Currently it ignores unknown return values to be backwards compatible
with old plugins
---
 src/mk_plugin.c  | 14 +++++++++++++-
 src/mk_request.c | 12 +++++++++---
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/mk_plugin.c b/src/mk_plugin.c
index c7105d0..7c9090e 100644
--- a/src/mk_plugin.c
+++ b/src/mk_plugin.c
@@ -647,7 +647,19 @@ int mk_plugin_stage_run(unsigned int hook,
         while (stm) {
             MK_TRACE("[%s] STAGE 40", stm->p->shortname);
 
-            stm->p->stage.s40(cs, sr);
+            ret = stm->p->stage.s40(cs, sr);
+
+            switch (ret) {
+                case MK_PLUGIN_RET_NOT_ME:
+                    break;
+                case MK_PLUGIN_RET_END:
+                case MK_PLUGIN_RET_CLOSE_CONX:
+                case MK_PLUGIN_RET_CONTINUE:
+                    return ret;
+                default:
+                    // break by default for backwards compatibility with old plugins
+                    break;
+            }
             stm = stm->next;
         }
     }
diff --git a/src/mk_request.c b/src/mk_request.c
index 579ed02..fb8bb4e 100644
--- a/src/mk_request.c
+++ b/src/mk_request.c
@@ -686,7 +686,7 @@ int mk_handler_read(int socket, struct client_session *cs)
 
 int mk_handler_write(int socket, struct client_session *cs)
 {
-    int final_status = 0;
+    int final_status = 0, plugin_ret = 0;
     struct session_request *sr_node;
     struct mk_list *sr_list, *sr_head;
 
@@ -716,15 +716,21 @@ int mk_handler_write(int socket, struct client_session *cs)
             return final_status;
         }
         else {
-            /* STAGE_40, request has ended */
-            mk_plugin_stage_run(MK_PLUGIN_STAGE_40, socket,
+            /* run STAGE_40 plugins, we have no data to send */
+            plugin_ret = mk_plugin_stage_run(MK_PLUGIN_STAGE_40, socket,
                                 NULL, cs, sr_node);
             switch (final_status) {
             case EXIT_NORMAL:
+                /* check if STAGE_40 plugin has some pending data to send */
+                if (plugin_ret == MK_PLUGIN_RET_CONTINUE) {
+                  return MK_PLUGIN_RET_CONTINUE;
+                }
+                break;
             case EXIT_ERROR:
                 if (sr_node->close_now == MK_TRUE) {
                     return -1;
                 }
+
                 break;
             case EXIT_ABORT:
                   return -1;
-- 
1.8.1.2

