|
| 1 | +/***************************************************************************** |
| 2 | +* Open LiteSpeed is an open source HTTP server. * |
| 3 | +* Copyright (C) 2013 - 2015 LiteSpeed Technologies, Inc. * |
| 4 | +* * |
| 5 | +* This program is free software: you can redistribute it and/or modify * |
| 6 | +* it under the terms of the GNU General Public License as published by * |
| 7 | +* the Free Software Foundation, either version 3 of the License, or * |
| 8 | +* (at your option) any later version. * |
| 9 | +* * |
| 10 | +* This program is distributed in the hope that it will be useful, * |
| 11 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | +* GNU General Public License for more details. * |
| 14 | +* * |
| 15 | +* You should have received a copy of the GNU General Public License * |
| 16 | +* along with this program. If not, see http://www.gnu.org/licenses/. * |
| 17 | +*****************************************************************************/ |
| 18 | + |
| 19 | +#include "../include/ls.h" |
| 20 | + |
| 21 | +#define MNAME checksessionhooks |
| 22 | +lsi_module_t MNAME; |
| 23 | + |
| 24 | +/** |
| 25 | + * This module tests to make sure every session hook is reached. |
| 26 | + */ |
| 27 | + |
| 28 | +const char *s_pCheckHookNames[LSI_HKPT_TOTAL_COUNT] = |
| 29 | +{ |
| 30 | + "L4_BEGINSESSION", |
| 31 | + "L4_ENDSESSION", |
| 32 | + "L4_RECVING", |
| 33 | + "L4_SENDING", |
| 34 | + "HTTP_BEGIN", |
| 35 | + "RECV_REQ_HEADER", |
| 36 | + "URI_MAP", |
| 37 | + "HTTP_AUTH", |
| 38 | + "RECV_REQ_BODY", |
| 39 | + "RCVD_REQ_BODY", |
| 40 | + "RECV_RESP_HEADER", |
| 41 | + "RECV_RESP_BODY", |
| 42 | + "RCVD_RESP_BODY", |
| 43 | + "HANDLER_RESTART", |
| 44 | + "SEND_RESP_HEADER", |
| 45 | + "SEND_RESP_BODY", |
| 46 | + "HTTP_END", |
| 47 | + "MAIN_INITED", |
| 48 | + "MAIN_PREFORK", |
| 49 | + "MAIN_POSTFORK", |
| 50 | + "WORKER_POSTFORK", |
| 51 | + "WORKER_ATEXIT", |
| 52 | + "MAIN_ATEXIT" |
| 53 | +}; |
| 54 | + |
| 55 | + |
| 56 | +int enableNextL4Hook(lsi_param_t *rec) |
| 57 | +{ |
| 58 | + static int s_iL4Level = LSI_HKPT_L4_RECVING; |
| 59 | + g_api->log(NULL, LSI_LOG_DEBUG, |
| 60 | + "[Module:checkhttphooks] Try to enable level %s\n", |
| 61 | + s_pCheckHookNames[s_iL4Level]); |
| 62 | + g_api->enable_hook(rec->session, &MNAME, 1, &s_iL4Level, 1); |
| 63 | + if (s_iL4Level == LSI_HKPT_L4_ENDSESSION) |
| 64 | + { |
| 65 | + int disableHooks[] = { LSI_HKPT_L4_RECVING, LSI_HKPT_L4_SENDING }; |
| 66 | + g_api->log(NULL, LSI_LOG_DEBUG, |
| 67 | + "[Module:checkhttphooks] All hooks enabled, " |
| 68 | + "disable L4 recv/send to prevent infinite loop.\n"); |
| 69 | + g_api->enable_hook(rec->session, &MNAME, 0, disableHooks, 2); |
| 70 | + return 0; |
| 71 | + |
| 72 | + } |
| 73 | + s_iL4Level++; |
| 74 | + if (s_iL4Level == LSI_HKPT_HTTP_BEGIN) |
| 75 | + s_iL4Level = LSI_HKPT_L4_ENDSESSION; |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +int enableNextHttpHook(lsi_param_t *rec) |
| 81 | +{ |
| 82 | + static int s_iHttpLevel = LSI_HKPT_RCVD_REQ_HEADER; |
| 83 | + g_api->log(NULL, LSI_LOG_DEBUG, |
| 84 | + "[Module:checkhttphooks] Try to enable level %s\n", |
| 85 | + s_pCheckHookNames[s_iHttpLevel]); |
| 86 | + g_api->enable_hook(rec->session, &MNAME, 1, &s_iHttpLevel, 1); |
| 87 | + s_iHttpLevel++; |
| 88 | + // make sure to enable send resp header too, ignore req body. |
| 89 | + if (s_iHttpLevel == LSI_HKPT_RCVD_REQ_BODY |
| 90 | + || s_iHttpLevel == LSI_HKPT_RCVD_RESP_HEADER |
| 91 | + || s_iHttpLevel == LSI_HKPT_SEND_RESP_HEADER) |
| 92 | + { |
| 93 | + g_api->log(NULL, LSI_LOG_DEBUG, |
| 94 | + "[Module:checkhttphooks] Enable next level because " |
| 95 | + "%s may be skipped.\n", s_pCheckHookNames[s_iHttpLevel - 1]); |
| 96 | + enableNextHttpHook(rec); |
| 97 | + } |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +int skippedHook(lsi_param_t *rec) |
| 103 | +{ |
| 104 | + g_api->log(NULL, LSI_LOG_DEBUG, |
| 105 | + "[Module:checkhttphooks] This level is generally skipped because " |
| 106 | + "it requires something else (e.g. a request body). " |
| 107 | + "This function shows that this level was enabled.\n"); |
| 108 | + return 0; |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +int reachEnd(const char *pLevel) |
| 113 | +{ |
| 114 | + g_api->log(NULL, LSI_LOG_DEBUG, "[Module:checkhttphooks] Reached the %s end!\n", pLevel); |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +int reachHttpEnd(lsi_param_t *rec) { return reachEnd("Http"); } |
| 120 | +int reachL4End(lsi_param_t *rec) { return reachEnd("L4"); } |
| 121 | + |
| 122 | +static lsi_serverhook_t serverHooks[] = |
| 123 | +{ |
| 124 | + { LSI_HKPT_L4_BEGINSESSION, enableNextL4Hook, LSI_HOOK_NORMAL, LSI_FLAG_ENABLED}, |
| 125 | + { LSI_HKPT_L4_ENDSESSION, reachL4End, LSI_HOOK_NORMAL, 0}, |
| 126 | + { LSI_HKPT_L4_RECVING, enableNextL4Hook, LSI_HOOK_NORMAL, 0}, |
| 127 | + { LSI_HKPT_L4_SENDING, enableNextL4Hook, LSI_HOOK_NORMAL, 0}, |
| 128 | + { LSI_HKPT_HTTP_BEGIN, enableNextHttpHook, LSI_HOOK_NORMAL, LSI_FLAG_ENABLED}, |
| 129 | + { LSI_HKPT_RCVD_REQ_HEADER, enableNextHttpHook, LSI_HOOK_NORMAL, 0}, |
| 130 | + { LSI_HKPT_URI_MAP, enableNextHttpHook, LSI_HOOK_NORMAL, 0}, |
| 131 | + { LSI_HKPT_HTTP_AUTH, enableNextHttpHook, LSI_HOOK_NORMAL, 0}, |
| 132 | + { LSI_HKPT_RECV_REQ_BODY, skippedHook, LSI_HOOK_NORMAL, 0}, |
| 133 | + { LSI_HKPT_RCVD_REQ_BODY, skippedHook, LSI_HOOK_NORMAL, 0}, |
| 134 | + { LSI_HKPT_RCVD_RESP_HEADER, enableNextHttpHook, LSI_HOOK_NORMAL, 0}, |
| 135 | + { LSI_HKPT_RECV_RESP_BODY, enableNextHttpHook, LSI_HOOK_NORMAL, 0}, |
| 136 | + { LSI_HKPT_RCVD_RESP_BODY, enableNextHttpHook, LSI_HOOK_NORMAL, 0}, |
| 137 | + { LSI_HKPT_HANDLER_RESTART, skippedHook, LSI_HOOK_NORMAL, 0}, |
| 138 | + { LSI_HKPT_SEND_RESP_HEADER, enableNextHttpHook, LSI_HOOK_NORMAL, 0}, |
| 139 | + { LSI_HKPT_SEND_RESP_BODY, enableNextHttpHook, LSI_HOOK_NORMAL, 0}, |
| 140 | + { LSI_HKPT_HTTP_END, reachHttpEnd, LSI_HOOK_NORMAL, 0}, |
| 141 | + LSI_HOOK_END //Must put this at the end position |
| 142 | +}; |
| 143 | + |
| 144 | +static int _init(lsi_module_t *pModule) |
| 145 | +{ |
| 146 | + return 0; |
| 147 | +} |
| 148 | + |
| 149 | +lsi_module_t MNAME = { LSI_MODULE_SIGNATURE, _init, NULL, NULL, |
| 150 | + "checksessionhooks v1.0", serverHooks, {0}}; |
| 151 | + |
0 commit comments