Files
ipass-plugin-cache/rust/vendor/ngx-rust/examples/curl.rs
T
ssw 43718bd0c4 iPass cache gateway 源码
容量管理: zones.rs 直读 nginx file_cache 共享内存记账(双轴: 磁盘 + keys_zone 索引内存), ?scan=1 扫盘上限 20 万文件; POST /api/admin/cache/purge 整区清理(只删文件不删目录, 根治 [crit] mkdir failed); POST /api/admin/cache/invalidate 按制品精准失效(预览->确认两步式, glob/子串, 删除上限 5000/扫描上限 20 万); GET /api/admin/cache 新增 capacity 块; 前端容量面板 + 按制品失效工具条; 测试 test-cache-capacity.sh 48/48(容量 27 + 精准失效 21, 含 EXPECT_MIN 护栏与前端静态契约); docs 容量管理章节
2026-09-21 09:25:11 +08:00

137 lines
4.2 KiB
Rust

use core::ffi::{c_char, c_void};
use core::ptr;
use ngx::core::Status;
use ngx::ffi::{
NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE, NGX_LOG_EMERG,
ngx_command_t, ngx_conf_t, ngx_http_module_t, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t,
};
use ngx::http::{self, HttpModule, HttpModuleLocationConf, HttpRequestHandler, MergeConfigError};
use ngx::{ngx_conf_log_error, ngx_log_debug_http, ngx_string};
struct Module;
impl http::HttpModule for Module {
fn module() -> &'static ngx_module_t {
unsafe { &*::core::ptr::addr_of!(ngx_http_curl_module) }
}
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
// SAFETY: this function is called with non-NULL cf always
let cf = unsafe { &mut *cf };
http::add_phase_handler::<CurlRequestHandler>(cf)
.map_or(Status::NGX_ERROR, |_| Status::NGX_OK)
.into()
}
}
#[derive(Debug, Default)]
struct ModuleConfig {
enable: bool,
}
unsafe impl HttpModuleLocationConf for Module {
type LocationConf = ModuleConfig;
}
static mut NGX_HTTP_CURL_COMMANDS: [ngx_command_t; 2] = [
ngx_command_t {
name: ngx_string!("curl"),
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
set: Some(ngx_http_curl_commands_set_enable),
conf: NGX_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: ptr::null_mut(),
},
ngx_command_t::empty(),
];
static NGX_HTTP_CURL_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
preconfiguration: Some(Module::preconfiguration),
postconfiguration: Some(Module::postconfiguration),
create_main_conf: None,
init_main_conf: None,
create_srv_conf: None,
merge_srv_conf: None,
create_loc_conf: Some(Module::create_loc_conf),
merge_loc_conf: Some(Module::merge_loc_conf),
};
// Generate the `ngx_modules` table with exported modules.
// This feature is required to build a 'cdylib' dynamic module outside of the NGINX buildsystem.
#[cfg(feature = "export-modules")]
ngx::ngx_modules!(ngx_http_curl_module);
#[used]
#[allow(non_upper_case_globals)]
#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))]
pub static mut ngx_http_curl_module: ngx_module_t = ngx_module_t {
ctx: &raw const NGX_HTTP_CURL_MODULE_CTX as _,
commands: unsafe { &raw mut NGX_HTTP_CURL_COMMANDS[0] },
type_: NGX_HTTP_MODULE as _,
..ngx_module_t::default()
};
impl http::Merge for ModuleConfig {
fn merge(&mut self, prev: &ModuleConfig) -> Result<(), MergeConfigError> {
if prev.enable {
self.enable = true;
};
Ok(())
}
}
struct CurlRequestHandler;
impl HttpRequestHandler for CurlRequestHandler {
const PHASE: ngx::http::HttpPhase = ngx::http::HttpPhase::Access;
type Output = Status;
fn handler(request: &mut http::Request) -> Self::Output {
let co = Module::location_conf(request).expect("module config is none");
ngx_log_debug_http!(request, "curl module enabled: {}", co.enable);
match co.enable {
true => {
if request.user_agent().is_some_and(|ua| ua.as_bytes().starts_with(b"curl")) {
http::HTTPStatus::FORBIDDEN.into()
} else {
Status::NGX_DECLINED
}
}
false => Status::NGX_DECLINED,
}
}
}
extern "C" fn ngx_http_curl_commands_set_enable(
cf: *mut ngx_conf_t,
_cmd: *mut ngx_command_t,
conf: *mut c_void,
) -> *mut c_char {
unsafe {
let conf = &mut *(conf as *mut ModuleConfig);
let args: &[ngx_str_t] = (*(*cf).args).as_slice();
let val = match args[1].to_str() {
Ok(s) => s,
Err(_) => {
ngx_conf_log_error!(NGX_LOG_EMERG, cf, "`curl` argument is not utf-8 encoded");
return ngx::core::NGX_CONF_ERROR;
}
};
// set default value optionally
conf.enable = false;
if val.len() == 2 && val.eq_ignore_ascii_case("on") {
conf.enable = true;
} else if val.len() == 3 && val.eq_ignore_ascii_case("off") {
conf.enable = false;
}
};
ngx::core::NGX_CONF_OK
}