Skip to content

Commit

Permalink
[FEAT]: additional functions exposed
Browse files Browse the repository at this point in the history
* added `&mut headers_out` getter
* added `&mut conf_main` getter
* exposed additional HTTP codes
* added basic headers and body handling
  • Loading branch information
Mateusz Matejuk committed Jan 30, 2024
1 parent 99508b8 commit 9ff8fd4
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 53 deletions.
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
[workspace]
resolver = "2"
resolver = "2"

members = [
"examples/simple"
]
members = [ "examples/simple" ]

[profile.release]
debug = true
debug = true
17 changes: 8 additions & 9 deletions examples/simple/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
[package]
name = "simple"
version = "0.1.0"
edition = "2021"
edition = "2021"
name = "simple"
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["staticlib"]

crate-type = [ "staticlib" ]


[dependencies]
anyhow = "1.0.71"
nginx_derive = { path = "../../nginx_derive" }
nginx_module = { path = "../../nginx_module" }
anyhow = "1.0.71"
nginx_derive = { path = "../../nginx_derive" }
nginx_module = { path = "../../nginx_module" }
28 changes: 14 additions & 14 deletions nginx_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[package]
name = "nginx_derive"
version = "0.1.1"
edition = "2021"
authors = ["Gabriel Oprisan <gabriel.oprisan@gcore.com>"]
description = "Helper crate for nginx-rust"
homepage = "https://github.com/g-Core/nginx-rust"
repository = "https://github.com/g-Core/nginx-rust"
readme = "README.md"
license = "Apache-2.0"
authors = [ "Gabriel Oprisan <gabriel.oprisan@gcore.com>" ]
description = "Helper crate for nginx-rust"
edition = "2021"
homepage = "https://github.com/g-Core/nginx-rust"
license = "Apache-2.0"
name = "nginx_derive"
readme = "README.md"
repository = "https://github.com/g-Core/nginx-rust"
version = "0.1.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
proc-macro = true
proc-macro = true

[dependencies]
syn = "2.0.18"
quote = "1.0.28"
proc-macro2 = "1.0.59"
proc-macro2 = { version = "1.0.59" }
quote = { version = "1.0.28" }
syn = { version = "2.0.18" }
30 changes: 15 additions & 15 deletions nginx_module/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
[package]
name = "nginx_module"
version = "0.1.4"
edition = "2021"
build = "build.rs"
authors = ["Gabriel Oprisan <gabriel.oprisan@gcore.com>"]
description = "Rust bindings for building Nginx modules"
homepage = "https://github.com/g-Core/nginx-rust"
repository = "https://github.com/g-Core/nginx-rust"
readme = "README.md"
license = "Apache-2.0"
authors = [ "Gabriel Oprisan <gabriel.oprisan@gcore.com>" ]
build = "build.rs"
description = "Rust bindings for building Nginx modules"
edition = "2021"
homepage = "https://github.com/g-Core/nginx-rust"
license = "Apache-2.0"
name = "nginx_module"
readme = "README.md"
repository = "https://github.com/g-Core/nginx-rust"
version = "0.1.4"


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
bindgen = "0.66.1"
bindgen = { version = "0.66.1" }

[dependencies]
anyhow = "1.0.71"
bitflags = "2.3.1"
libc = "0.2.144"
anyhow = { version = "1.0.71" }
bitflags = { version = "2.3.1" }
libc = { version = "0.2.144" }
76 changes: 71 additions & 5 deletions nginx_module/src/http_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@
* Copyright 2023 G-Core Innovations SARL
*/

#![allow(non_camel_case_types)]
#![allow(non_snake_case)]


use std::{
marker::PhantomData,
ops::{Deref, DerefMut},
};

#[cfg(not(nginx_version_1023000))]
use crate::{
bindings::{ngx_array_push, ngx_array_t, NGX_DECLINED, NGX_OK},
bindings::{ngx_array_push, ngx_array_t, NGX_DECLINED, NGX_OK, NGX_ERROR},
wrappers::array_init,
};
use crate::{
bindings::{
ngx_http_get_indexed_variable, ngx_http_parse_multi_header_lines, ngx_http_request_t,
ngx_list_push, ngx_list_t, ngx_module_t, ngx_table_elt_t,
},
ngx_str_t,
wrappers::IndexedVar,
ngx_list_push, ngx_list_t, ngx_module_t, ngx_table_elt_t, ngx_http_headers_out_t, ngx_buf_t,
ngx_http_send_header, ngx_str_t, ngx_alloc_chain_link, ngx_pcalloc, ngx_chain_s, ngx_http_output_filter, ngx_http_request_s},
wrappers::IndexedVar, NGX_ERROR,
};

use crate::{connection::Connection, Log};

use super::{NgxStr, Pool};
Expand Down Expand Up @@ -57,6 +61,56 @@ impl<'a, Ctx: Default> HttpRequestAndContext<'a, Ctx> {
Ok((req, ctx))
}
}

// get raw pointer to request structure
pub fn get_request_pointer (
&mut self,
) -> anyhow::Result<*mut ngx_http_request_t> {
let req = &mut self.0 as *mut ngx_http_request_t;
if req.is_null() {
anyhow::bail!("tried to get request pointer which is null");
}
Ok(req)
}

// send prepared headers
// headers should be prepared in headers_out structure
pub fn send_headers(&mut self) -> anyhow::Result<()> {
unsafe {
if 0==ngx_http_send_header(&(self.0) as *const ngx_http_request_t as *mut ngx_http_request_t) {
Ok(())
} else {
Err(anyhow::anyhow!(NGX_ERROR))
}
}
}

// request allocation of chain_link structure from nginx's pool
// returns pointer to allocated structure
pub fn alloc_chain_link(&mut self) -> *mut ngx_chain_s {
unsafe {
let x = ngx_alloc_chain_link(self.0.pool);
x
}
}

// request allocation of chain_link structure from nginx's pool
// returns pointer to allocated structure
pub fn alloc_buf(&mut self) -> *mut ngx_buf_t {
unsafe {
ngx_pcalloc(self.0.pool, std::mem::size_of::<ngx_buf_t>()) as *mut ngx_buf_t
}
}

// request sending the prepared body
// body have to be prepared in ngx_chain_s structure
pub fn send_body(&mut self, chain: *mut ngx_chain_s) {
unsafe {
let x = ngx_http_output_filter(&mut self.0 as *mut ngx_http_request_s, chain);
println!("ngx_htt p_output_filter returned {}", x);
}
}

}

impl<'a, Ctx> Deref for HttpRequestAndContext<'a, Ctx> {
Expand All @@ -81,6 +135,13 @@ impl<'a> HttpRequest<'a> {
}
}

pub fn get_main_config<'b, Config>(&self, module: &ngx_module_t) -> Option<&'b Config> {
unsafe {
let conf = (*(*self.0.main).main_conf.add(module.ctx_index)) as *const Config;
conf.as_ref()
}
}

pub fn is_main(&self) -> bool {
std::ptr::eq(&self.0, self.0.main)
}
Expand Down Expand Up @@ -183,6 +244,10 @@ impl<'a> HttpRequest<'a> {
}
}

pub fn get_headers_out_ref(&mut self) -> &mut ngx_http_headers_out_t {
&mut self.0.headers_out
}

pub fn unparsed_uri(&self) -> NgxStr {
unsafe { NgxStr::from_raw(self.0.unparsed_uri) }
}
Expand Down Expand Up @@ -279,6 +344,7 @@ impl<'a> HttpRequest<'a> {
cc = slice[0];
};
}

#[cfg(nginx_version_1023000)]
{
cc = self.0.headers_out.cache_control;
Expand Down
18 changes: 13 additions & 5 deletions nginx_module/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 G-Core Innovations SARL
* Copyright 2024 G-Core Innovations SARL
*/

mod bindings;
Expand All @@ -10,12 +10,20 @@ use std::{
};

pub use bindings::{
nginx_version, ngx_chain_t, ngx_command_t, ngx_conf_t, ngx_cycle_t, ngx_http_conf_ctx_t,
nginx_version, ngx_chain_t, ngx_buf_t, ngx_command_t, ngx_conf_t, ngx_cycle_t, ngx_http_conf_ctx_t,
ngx_http_module_t, ngx_http_request_body_filter_pt, ngx_http_request_t, ngx_module_t,
ngx_str_t, NGX_CONF_TAKE1, NGX_DECLINED, NGX_ERROR, NGX_HTTP_FORBIDDEN, NGX_HTTP_LOC_CONF,
NGX_HTTP_MAIN_CONF, NGX_HTTP_MODULE, NGX_HTTP_SRV_CONF, NGX_HTTP_TEMPORARY_REDIRECT,
NGX_LOG_ERR, NGX_OK, NGX_RS_HTTP_LOC_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
ngx_str_t, NGX_CONF_TAKE1, NGX_ERROR, NGX_HTTP_LOC_CONF,
NGX_HTTP_MAIN_CONF, NGX_HTTP_MODULE, NGX_HTTP_SRV_CONF,
NGX_LOG_ERR, NGX_OK, NGX_RS_HTTP_LOC_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
};

// add common http return codes
pub use bindings::{
NGX_HTTP_FORBIDDEN, NGX_HTTP_TEMPORARY_REDIRECT,
NGX_HTTP_OK, NGX_HTTP_INTERNAL_SERVER_ERROR, NGX_DECLINED, NGX_HTTP_ACCEPTED,
NGX_HTTP_BAD_REQUEST, NGX_HTTP_NOT_ALLOWED, NGX_HTTP_NOT_FOUND, NGX_HTTP_NOT_IMPLEMENTED
};

use bindings::{
ngx_array_push, ngx_cycle, ngx_http_core_main_conf_t, ngx_http_core_module,
ngx_http_handler_pt, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_http_top_request_body_filter,
Expand Down

0 comments on commit 9ff8fd4

Please sign in to comment.