diff --git a/Cargo.lock b/Cargo.lock index b06b299..cdef571 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,7 +2,7 @@ name = "ngx-rust" version = "1.11.13" dependencies = [ - "bindgen 0.26.3 (registry+https://github.com/rust-lang/crates.io-index)", + "bindgen 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -39,7 +39,7 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.26.3" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "aster 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -55,6 +55,7 @@ dependencies = [ "quasi_codegen 0.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)", + "which 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -340,6 +341,14 @@ name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "which" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi" version = "0.2.8" @@ -355,7 +364,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6" "checksum aster 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4ccfdf7355d9db158df68f976ed030ab0f6578af811f5a7bb6dcf221ec24e0e0" "checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159" -"checksum bindgen 0.26.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c57d6c0f6e31f8dcf4d12720a3c2a9ffb70638772a5784976cf4fce52145f22a" +"checksum bindgen 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)" = "33024f55a754d920637461adf87fb485702a69bdf7ac1d307b7e18da93bae505" "checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4" "checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5" "checksum cexpr 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "cdbb21df6ff3497a61df5059994297f746267020ba38ce237aad9c875f7b4313" @@ -393,5 +402,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" "checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum which 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d238435618c0f298d2d75596c2d4fa7d4ea469c0c1c3ff824737ed50ad5ab61c" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/Cargo.toml b/Cargo.toml index 12a02bd..9b0f0d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,16 +5,16 @@ authors = ["Sehyo Chang sehyo@nginx.com"] [lib] doctest = false -test = false -crate-type = ["lib"] +test = true +crate-type = ["staticlib","rlib"] [dependencies] -libc = "0.2.26" +libc = "0.2.29" [build-dependencies] -bindgen = "0.26.3" +bindgen = "0.30.0" diff --git a/Makefile b/Makefile index 36e3c44..8ade3df 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,12 @@ darwin-configure: darwin-setup: darwin-source darwin-configure +# build module locally in mac + +darwin-module: + cd nginx/${DARWIN_NGINX}; \ + make modules; + # need to run inside container lx-compiler: docker run -it -v ${ROOT_DIR}:/src ${RUST_TOOL} /bin/bash diff --git a/build.rs b/build.rs index 559040b..e020450 100644 --- a/build.rs +++ b/build.rs @@ -1,29 +1,25 @@ extern crate bindgen; -use std::env; -use std::path::PathBuf; - #[cfg(target_os = "macos")] -const nginx_dir: &str = "./nginx/nginx-darwin"; +const NGIX_DIR: &str = "./nginx/nginx-darwin"; #[cfg(target_os = "linux")] -const nginx_dir: &str = "./nginx/nginx-linux"; +const NGIX_DIR: &str = "./nginx/nginx-linux"; fn main() { - - let bindings = bindgen::Builder::default() // The input header we would like to generate // bindings for. .header("wrapper.h") - .clang_arg(format!("-I{}/src/core",nginx_dir)) - .clang_arg(format!("-I{}/src/event",nginx_dir)) - .clang_arg(format!("-I{}/src/event/modules",nginx_dir)) - .clang_arg(format!("-I{}/src/os/unix",nginx_dir)) - .clang_arg(format!("-I{}/objs",nginx_dir)) - .clang_arg(format!("-I{}/src/http",nginx_dir)) - .clang_arg(format!("-I{}/src/http/modules",nginx_dir)) + .layout_tests(false) + .clang_arg(format!("-I{}/src/core",NGIX_DIR)) + .clang_arg(format!("-I{}/src/event",NGIX_DIR)) + .clang_arg(format!("-I{}/src/event/modules",NGIX_DIR)) + .clang_arg(format!("-I{}/src/os/unix",NGIX_DIR)) + .clang_arg(format!("-I{}/objs",NGIX_DIR)) + .clang_arg(format!("-I{}/src/http",NGIX_DIR)) + .clang_arg(format!("-I{}/src/http/modules",NGIX_DIR)) // Finish the builder and generate the bindings. .generate() // Unwrap the Result and panic on failure. @@ -33,5 +29,4 @@ fn main() { .write_to_file("src/bindings.rs") .expect("Couldn't write bindings!"); - } diff --git a/config b/config deleted file mode 100644 index fa6aa6f..0000000 --- a/config +++ /dev/null @@ -1,15 +0,0 @@ -ngx_addon_name=ngx_http_istio_mixer_module - -ngx_module_type=HTTP_FILTER -ngx_module_name=ngx_http_istio_mixer_module -ngx_module_srcs="$ngx_addon_dir/ngx_http_istio_mixer_module.c" -ngx_module_deps="cargo" -ngx_module_libs="$ngx_addon_dir/target/debug/libmixer.a" - - -. auto/module - - - - - diff --git a/module/config b/module/config index 29f5c31..88b592a 100644 --- a/module/config +++ b/module/config @@ -3,10 +3,10 @@ ngx_addon_name=ngx_http_rust_module ngx_module_type=HTTP_FILTER ngx_module_name=ngx_http_rust_module ngx_module_srcs="$ngx_addon_dir/ngx_http_rust_module.c" - -. auto/module - +ngx_module_deps="cargo" +ngx_module_libs="$ngx_addon_dir/../target/debug/libngx_rust.a" +. auto/module diff --git a/module/config.make b/module/config.make new file mode 100644 index 0000000..23559e8 --- /dev/null +++ b/module/config.make @@ -0,0 +1,7 @@ + +cat << END >> $NGX_MAKEFILE + +cargo: + cargo build --manifest-path $ngx_addon_dir/../Cargo.toml --lib + +END diff --git a/module/ngx_rust.c b/module/ngx_http_rust_module.c similarity index 79% rename from module/ngx_rust.c rename to module/ngx_http_rust_module.c index c4c5b6a..374a606 100644 --- a/module/ngx_rust.c +++ b/module/ngx_http_rust_module.c @@ -19,12 +19,9 @@ * @brief element mixer configuration */ typedef struct { + ngx_str_t name; /**< test name */ +} ngx_http_rust_main_conf_t; -} ngx_http_mixer_main_conf_t; - - -static ngx_int_t ngx_http_rust_filter(ngx_http_request_t *r); -static ngx_int_t ngx_http_rust_filter_init(ngx_conf_t *cf); static void *ngx_http_rust_create_main_conf(ngx_conf_t *cf); @@ -37,9 +34,9 @@ static ngx_command_t ngx_http_rust_commands[] = { { ngx_string("rust"), /* dummy directive */ NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, // server takes 1 // - NULL, /* configuration setup function */ - NULL, - NULL, + ngx_conf_set_str_slot, /* configuration setup function */ + NGX_HTTP_MAIN_CONF_OFFSET, + offsetof(ngx_http_rust_main_conf_t, name), NULL }, ngx_null_command /* command termination */ @@ -68,7 +65,7 @@ ngx_module_t ngx_http_rust_module = { NGX_HTTP_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ - mixer_init, /* init process */ + NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ @@ -80,16 +77,16 @@ ngx_module_t ngx_http_rust_module = { static void *ngx_http_rust_create_main_conf(ngx_conf_t *cf) { - ngx_http_mixer_main_conf_t *conf; + ngx_http_rust_main_conf_t *conf; ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "setting up main config"); - conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_mixer_main_conf_t)); + conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rust_main_conf_t)); if (conf == NULL) { return NULL; } - conf->mixer_port = NGX_CONF_UNSET_UINT; + return conf; } \ No newline at end of file diff --git a/ngx_rust.c b/ngx_rust.c deleted file mode 100644 index b81102f..0000000 --- a/ngx_rust.c +++ /dev/null @@ -1,242 +0,0 @@ -/** - * @file ngx_http_istio_mixer_module.c - * @author Sehyo Chang - * @date Wed Aug 19 2017 - * - * @brief Istio Mixer integration module for Nginx. - * - * @section LICENSE - * - * Copyright (C) 2011 by Nginx - * - */ -#include -#include -#include - - -typedef struct { - ngx_flag_t enable; // for every location, we need flag to enable/disable mixer - -} ngx_http_mixer_loc_conf_t; - - -/** - * @brief element mixer configuration - */ -typedef struct { - ngx_str_t mixer_server; /**< mixer server */ - ngx_int_t mixer_port; /** mixer port */ - - ngx_str_t target_ip; // target ip - ngx_str_t target_uid; // target uid - ngx_str_t target_service; // target service - - -} ngx_http_mixer_main_conf_t; - - -static ngx_int_t ngx_http_istio_mixer_filter(ngx_http_request_t *r); -static ngx_int_t ngx_http_mixer_filter_init(ngx_conf_t *cf); - -// create configuration -static void *ngx_http_mixer_create_loc_conf(ngx_conf_t *cf); -static char *ngx_http_mixer_merge_loc_conf(ngx_conf_t *cf, void *parent, - void *child); - -static void *ngx_http_mixer_create_main_conf(ngx_conf_t *cf); - - -void mixer_client(ngx_http_request_t *r, ngx_http_mixer_main_conf_t *main_conf); -ngx_int_t mixer_init(ngx_cycle_t *cycle); -void mixer_exit(); - -static ngx_http_output_header_filter_pt ngx_http_next_header_filter; - - -/** - * This module provide callback to istio for http traffic - * - */ -static ngx_command_t ngx_http_istio_mixer_commands[] = { - - { - ngx_string("mixer"), /* directive */ - NGX_HTTP_LOC_CONF | NGX_CONF_FLAG, - ngx_conf_set_flag_slot, /* configuration setup function */ - NGX_HTTP_LOC_CONF_OFFSET, - offsetof(ngx_http_mixer_loc_conf_t, enable), // store in the location configuration - NULL}, - - { - ngx_string("mixer_target_ip"), - NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1, - ngx_conf_set_str_slot, - NGX_HTTP_MAIN_CONF_OFFSET, - offsetof(ngx_http_mixer_main_conf_t, target_ip), // store in the location configuration - NULL - }, - - { - ngx_string("mixer_target_uid"), - NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1, - ngx_conf_set_str_slot, - NGX_HTTP_MAIN_CONF_OFFSET, - offsetof(ngx_http_mixer_main_conf_t, target_uid), // store in the location configuration - NULL - }, - { - ngx_string("mixer_target_service"), - NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1, - ngx_conf_set_str_slot, - NGX_HTTP_MAIN_CONF_OFFSET, - offsetof(ngx_http_mixer_main_conf_t, target_service), // store in the location configuration - NULL - }, - - { - ngx_string("mixer_server"), /* directive */ - NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, // server takes 1 // - ngx_conf_set_str_slot, /* configuration setup function */ - NGX_HTTP_MAIN_CONF_OFFSET, - offsetof(ngx_http_mixer_main_conf_t,mixer_server), - NULL - }, - { - ngx_string("mixer_port"), /* directive */ - NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, // server port takes 1 // - ngx_conf_set_num_slot, /* configuration setup function */ - NGX_HTTP_MAIN_CONF_OFFSET, - offsetof(ngx_http_mixer_main_conf_t,mixer_port), - NULL - }, - ngx_null_command /* command termination */ -}; - - -/* The module context. */ -static ngx_http_module_t ngx_http_istio_mixer_module_ctx = { - NULL, /* preconfiguration */ - ngx_http_mixer_filter_init, /* postconfiguration */ - ngx_http_mixer_create_main_conf, /* create main configuration */ - NULL, /* init main configuration */ - - NULL, /* create server configuration */ - NULL, /* merge server configuration */ - - ngx_http_mixer_create_loc_conf, /* create location configuration */ - ngx_http_mixer_merge_loc_conf /* merge location configuration */ -}; - -/* Module definition. */ -ngx_module_t ngx_http_istio_mixer_module = { - NGX_MODULE_V1, - &ngx_http_istio_mixer_module_ctx, /* module context */ - ngx_http_istio_mixer_commands, /* module directives */ - NGX_HTTP_MODULE, /* module type */ - NULL, /* init master */ - NULL, /* init module */ - mixer_init, /* init process */ - NULL, /* init thread */ - NULL, /* exit thread */ - NULL, /* exit process */ - NULL, /* exit master */ - NGX_MODULE_V1_PADDING -}; - -static ngx_int_t ngx_http_mixer_filter_init(ngx_conf_t *cf) { - - - ngx_http_next_header_filter = ngx_http_top_header_filter; - ngx_http_top_header_filter = ngx_http_istio_mixer_filter; - - return NGX_OK; -} - -/** - * Content handler. - * - * @param r - * Pointer to the request structure. See http_request.h. - * @return - * The status of the response generation. - */ -static ngx_int_t ngx_http_istio_mixer_filter(ngx_http_request_t *r) -{ - ngx_http_mixer_loc_conf_t *loc_conf; - ngx_http_mixer_main_conf_t *main_conf; - - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "start invoking istio mixer filter"); - - loc_conf = ngx_http_get_module_loc_conf(r, ngx_http_istio_mixer_module); - - if (!loc_conf->enable) { - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "mixer not enabled, just passing header"); - return ngx_http_next_header_filter(r); - } - - - main_conf = ngx_http_get_module_main_conf(r, ngx_http_istio_mixer_module); - - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "using server: %*s",main_conf->mixer_server.len,main_conf->mixer_server.data); - - // invoke mix client - mixer_client(r,main_conf); - - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "finish calling istio filter"); - - - return ngx_http_next_header_filter(r); - -} - - -static void *ngx_http_mixer_create_loc_conf(ngx_conf_t *cf) { - - ngx_http_mixer_loc_conf_t *conf; - - conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_mixer_loc_conf_t)); - if (conf == NULL) { - return NULL; - } - - conf->enable = NGX_CONF_UNSET; - - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "set up mixer location config"); - - return conf; -} - -static char * -ngx_http_mixer_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) -{ - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "merging loc conf"); - - ngx_http_mixer_loc_conf_t *prev = parent; - ngx_http_mixer_loc_conf_t *conf = child; - - ngx_conf_merge_value(conf->enable, prev->enable, 0); - - return NGX_CONF_OK; -} - - - - -static void *ngx_http_mixer_create_main_conf(ngx_conf_t *cf) -{ - ngx_http_mixer_main_conf_t *conf; - - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "setting up main config"); - - - conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_mixer_main_conf_t)); - if (conf == NULL) { - return NULL; - } - - conf->mixer_port = NGX_CONF_UNSET_UINT; - - - return conf; -} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 39b75b4..7a09f00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,4 +5,3 @@ pub mod nginx_http; - diff --git a/src/nginx_http.rs b/src/nginx_http.rs index ca45fc1..28f8f0e 100644 --- a/src/nginx_http.rs +++ b/src/nginx_http.rs @@ -4,8 +4,6 @@ extern crate libc; use std::str; use std::slice; -use std::ffi::CString; - use bindings::ngx_http_request_s; use bindings::ngx_http_headers_in_t; @@ -15,6 +13,7 @@ use bindings::ngx_table_elt_t; use bindings::ngx_list_t; use bindings::ngx_uint_t; use bindings::ngx_str_t; +use bindings::ngx_log_t; use bindings::ngx_log_error_core; use bindings::NGX_LOG_ERR; use bindings::ngx_cycle; @@ -162,11 +161,33 @@ impl Iterator for NgxListIterator { } -// log message -pub fn log(message: &str) { +// log message +pub fn log(message: &str) { unsafe { - ngx_log_error_core(NGX_LOG_ERR as usize, (*ngx_cycle).log, 0, message.as_ptr() as *const ::std::os::raw::c_char,message.len()); + ngx_log_error_core(NGX_LOG_ERR as usize, (*ngx_cycle).log, 0, message.as_ptr() as *const ::std::os::raw::c_char, message.len()); } - } + + // log with format +/* +macro_rules! ngx_log_format { + () => { println!("hello world"); +} +*/ + + + + + /* + #define ngx_log_error(level, log, ...) \ + if ((log)->log_level >= level) ngx_log_error_core(level, log, __VA_ARGS__) + +void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, + const char *fmt, ...); + */ + + /* + log(&format!("target uid founded!")); + */ + diff --git a/tests/log_test.rs b/tests/log_test.rs new file mode 100644 index 0000000..c4327cb --- /dev/null +++ b/tests/log_test.rs @@ -0,0 +1,18 @@ +extern crate ngx_rust; + + +#[cfg(test)] +mod tests { + + + + #[test] + fn test() { + + assert_eq!(1,1); + + + } + + +} \ No newline at end of file