|
1 | | -use std::ffi::{c_char, c_void}; |
| 1 | +use std::ffi::c_char; |
| 2 | +use std::ffi::c_void; |
| 3 | +use std::ffi::CStr; |
| 4 | +use std::marker::PhantomData; |
2 | 5 | use std::ptr; |
3 | 6 |
|
4 | 7 | use crate::core::NGX_CONF_ERROR; |
5 | 8 | use crate::core::*; |
6 | 9 | use crate::ffi::*; |
| 10 | +use crate::module::CycleDelegate; |
| 11 | +use crate::module::PreCycleDelegate; |
| 12 | +use crate::util::StaticRefMut; |
| 13 | + |
| 14 | +use super::ConfigurationDelegate; |
| 15 | +use super::HttpModule; |
| 16 | +use super::InitConfSetting; |
| 17 | +use super::MergeConfSetting; |
| 18 | +use super::NgxHttpModule; |
| 19 | +use super::NgxHttpModuleCommandsRefMut; |
7 | 20 |
|
8 | 21 | /// MergeConfigError - configuration cannot be merged with levels above. |
9 | 22 | #[derive(Debug)] |
@@ -131,3 +144,113 @@ pub trait HTTPModule { |
131 | 144 | } |
132 | 145 | } |
133 | 146 | } |
| 147 | + |
| 148 | +pub trait HTTPModuleSupplement<M: HTTPModule + 'static>: 'static + Sized { |
| 149 | + const SELF: StaticRefMut<NgxHttpModule<(M, Self)>>; |
| 150 | + const NAME: &'static CStr; |
| 151 | + const COMMANDS: NgxHttpModuleCommandsRefMut<(M, Self)>; |
| 152 | + |
| 153 | + /// exexutor type deligating `init_master` (not called now). |
| 154 | + type MasterInitializer: PreCycleDelegate; |
| 155 | + /// exexutor type deligating `init_module` and `exit_master`. |
| 156 | + type ModuleDelegate: CycleDelegate; |
| 157 | + /// exexutor type deligating `init_process` and `exit_process`. |
| 158 | + type ProcessDelegate: CycleDelegate; |
| 159 | + /// exexutor type deligating `init_thread` and `exit_thread` (not called now). |
| 160 | + type ThreadDelegate: CycleDelegate; |
| 161 | + |
| 162 | + type Ctx; |
| 163 | +} |
| 164 | + |
| 165 | +impl<M: HTTPModule + 'static, MS: HTTPModuleSupplement<M>> HttpModule for (M, MS) { |
| 166 | + const SELF: StaticRefMut<NgxHttpModule<(M, MS)>> = MS::SELF; |
| 167 | + const NAME: &'static CStr = MS::NAME; |
| 168 | + const COMMANDS: NgxHttpModuleCommandsRefMut<Self> = MS::COMMANDS; |
| 169 | + |
| 170 | + type MasterInitializer = MS::MasterInitializer; |
| 171 | + type ModuleDelegate = MS::ModuleDelegate; |
| 172 | + type ProcessDelegate = MS::ProcessDelegate; |
| 173 | + type ThreadDelegate = MS::ThreadDelegate; |
| 174 | + |
| 175 | + type PreConfiguration = HTTPModulePreConfiguration<M>; |
| 176 | + type PostConfiguration = HTTPModulePostConfiguration<M>; |
| 177 | + type MainConfSetting = HTTPModuleMainConfSetting<M>; |
| 178 | + type SrvConfSetting = HTTPModuleSrvConfSetting<M>; |
| 179 | + type LocConfSetting = HTTPModuleLocConfSetting<M>; |
| 180 | + |
| 181 | + type Ctx = MS::Ctx; |
| 182 | +} |
| 183 | + |
| 184 | +pub struct HTTPModulePreConfiguration<M: HTTPModule>(PhantomData<M>); |
| 185 | +impl<M: HTTPModule> ConfigurationDelegate for HTTPModulePreConfiguration<M> { |
| 186 | + fn configuration(_cf: &mut ngx_conf_t) -> Result<(), Status> { |
| 187 | + unimplemented!() |
| 188 | + } |
| 189 | + unsafe extern "C" fn configuration_unsafe(cf: *mut ngx_conf_t) -> ngx_int_t { |
| 190 | + M::preconfiguration(cf) |
| 191 | + } |
| 192 | +} |
| 193 | +pub struct HTTPModulePostConfiguration<M: HTTPModule>(PhantomData<M>); |
| 194 | +impl<M: HTTPModule> ConfigurationDelegate for HTTPModulePostConfiguration<M> { |
| 195 | + fn configuration(_cf: &mut ngx_conf_t) -> Result<(), Status> { |
| 196 | + unimplemented!() |
| 197 | + } |
| 198 | + unsafe extern "C" fn configuration_unsafe(cf: *mut ngx_conf_t) -> ngx_int_t { |
| 199 | + M::postconfiguration(cf) |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +pub struct HTTPModuleMainConfSetting<M: HTTPModule>(PhantomData<M>); |
| 204 | +impl<M: HTTPModule> InitConfSetting for HTTPModuleMainConfSetting<M> { |
| 205 | + type Conf = M::MainConf; |
| 206 | + |
| 207 | + fn create(cf: &mut ngx_conf_t) -> Result<Self::Conf, ()> { |
| 208 | + unimplemented!() |
| 209 | + } |
| 210 | + |
| 211 | + fn init(cf: &mut ngx_conf_t, conf: &mut Self::Conf) -> Result<(), ()> { |
| 212 | + unimplemented!() |
| 213 | + } |
| 214 | + unsafe extern "C" fn create_unsafe(cf: *mut ngx_conf_t) -> *mut c_void { |
| 215 | + M::create_main_conf(cf) |
| 216 | + } |
| 217 | + unsafe extern "C" fn init_unsafe(cf: *mut ngx_conf_t, conf: *mut c_void) -> *mut c_char { |
| 218 | + M::init_main_conf(cf, conf) |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +pub struct HTTPModuleSrvConfSetting<M: HTTPModule>(PhantomData<M>); |
| 223 | +impl<M: HTTPModule> MergeConfSetting for HTTPModuleSrvConfSetting<M> { |
| 224 | + type Conf = M::SrvConf; |
| 225 | + |
| 226 | + fn create(cf: &mut ngx_conf_t) -> Result<Self::Conf, ()> { |
| 227 | + unimplemented!() |
| 228 | + } |
| 229 | + fn merge(cf: &mut ngx_conf_t, prev: &mut Self::Conf, conf: &mut Self::Conf) -> Result<(), ()> { |
| 230 | + unimplemented!() |
| 231 | + } |
| 232 | + unsafe extern "C" fn create_unsafe(cf: *mut ngx_conf_t) -> *mut c_void { |
| 233 | + M::create_srv_conf(cf) |
| 234 | + } |
| 235 | + unsafe extern "C" fn merge_unsafe(cf: *mut ngx_conf_t, prev: *mut c_void, conf: *mut c_void) -> *mut c_char { |
| 236 | + M::merge_srv_conf(cf, prev, conf) |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +pub struct HTTPModuleLocConfSetting<M: HTTPModule>(PhantomData<M>); |
| 241 | +impl<M: HTTPModule> MergeConfSetting for HTTPModuleLocConfSetting<M> { |
| 242 | + type Conf = M::LocConf; |
| 243 | + |
| 244 | + fn create(cf: &mut ngx_conf_t) -> Result<Self::Conf, ()> { |
| 245 | + unimplemented!() |
| 246 | + } |
| 247 | + fn merge(cf: &mut ngx_conf_t, prev: &mut Self::Conf, conf: &mut Self::Conf) -> Result<(), ()> { |
| 248 | + unimplemented!() |
| 249 | + } |
| 250 | + unsafe extern "C" fn create_unsafe(cf: *mut ngx_conf_t) -> *mut c_void { |
| 251 | + M::create_loc_conf(cf) |
| 252 | + } |
| 253 | + unsafe extern "C" fn merge_unsafe(cf: *mut ngx_conf_t, prev: *mut c_void, conf: *mut c_void) -> *mut c_char { |
| 254 | + M::merge_loc_conf(cf, prev, conf) |
| 255 | + } |
| 256 | +} |
0 commit comments